Rebase.
[official-gcc.git] / gcc / fortran / decl.c
blob25d92a46d3e94478b569fba2ec7598e59aa4e9f3
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 then we want to set the type & bail out. */
1908 if (gfc_option.flag_cray_pointer)
1910 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1911 if (sym != NULL && sym->attr.cray_pointee)
1913 sym->ts.type = current_ts.type;
1914 sym->ts.kind = current_ts.kind;
1915 sym->ts.u.cl = cl;
1916 sym->ts.u.derived = current_ts.u.derived;
1917 sym->ts.is_c_interop = current_ts.is_c_interop;
1918 sym->ts.is_iso_c = current_ts.is_iso_c;
1919 m = MATCH_YES;
1921 /* Check to see if we have an array specification. */
1922 if (cp_as != NULL)
1924 if (sym->as != NULL)
1926 gfc_error ("Duplicate array spec for Cray pointee at %C");
1927 gfc_free_array_spec (cp_as);
1928 m = MATCH_ERROR;
1929 goto cleanup;
1931 else
1933 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
1934 gfc_internal_error ("Couldn't set pointee array spec.");
1936 /* Fix the array spec. */
1937 m = gfc_mod_pointee_as (sym->as);
1938 if (m == MATCH_ERROR)
1939 goto cleanup;
1942 goto cleanup;
1944 else
1946 gfc_free_array_spec (cp_as);
1950 /* Procedure pointer as function result. */
1951 if (gfc_current_state () == COMP_FUNCTION
1952 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1953 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1954 strcpy (name, "ppr@");
1956 if (gfc_current_state () == COMP_FUNCTION
1957 && strcmp (name, gfc_current_block ()->name) == 0
1958 && gfc_current_block ()->result
1959 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1960 strcpy (name, "ppr@");
1962 /* OK, we've successfully matched the declaration. Now put the
1963 symbol in the current namespace, because it might be used in the
1964 optional initialization expression for this symbol, e.g. this is
1965 perfectly legal:
1967 integer, parameter :: i = huge(i)
1969 This is only true for parameters or variables of a basic type.
1970 For components of derived types, it is not true, so we don't
1971 create a symbol for those yet. If we fail to create the symbol,
1972 bail out. */
1973 if (gfc_current_state () != COMP_DERIVED
1974 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
1976 m = MATCH_ERROR;
1977 goto cleanup;
1980 if (!check_function_name (name))
1982 m = MATCH_ERROR;
1983 goto cleanup;
1986 /* We allow old-style initializations of the form
1987 integer i /2/, j(4) /3*3, 1/
1988 (if no colon has been seen). These are different from data
1989 statements in that initializers are only allowed to apply to the
1990 variable immediately preceding, i.e.
1991 integer i, j /1, 2/
1992 is not allowed. Therefore we have to do some work manually, that
1993 could otherwise be left to the matchers for DATA statements. */
1995 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1997 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
1998 "initialization at %C"))
1999 return MATCH_ERROR;
2000 else if (gfc_current_state () == COMP_DERIVED)
2002 gfc_error ("Invalid old style initialization for derived type "
2003 "component at %C");
2004 m = MATCH_ERROR;
2005 goto cleanup;
2008 return match_old_style_init (name);
2011 /* The double colon must be present in order to have initializers.
2012 Otherwise the statement is ambiguous with an assignment statement. */
2013 if (colon_seen)
2015 if (gfc_match (" =>") == MATCH_YES)
2017 if (!current_attr.pointer)
2019 gfc_error ("Initialization at %C isn't for a pointer variable");
2020 m = MATCH_ERROR;
2021 goto cleanup;
2024 m = match_pointer_init (&initializer, 0);
2025 if (m != MATCH_YES)
2026 goto cleanup;
2028 else if (gfc_match_char ('=') == MATCH_YES)
2030 if (current_attr.pointer)
2032 gfc_error ("Pointer initialization at %C requires '=>', "
2033 "not '='");
2034 m = MATCH_ERROR;
2035 goto cleanup;
2038 m = gfc_match_init_expr (&initializer);
2039 if (m == MATCH_NO)
2041 gfc_error ("Expected an initialization expression at %C");
2042 m = MATCH_ERROR;
2045 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2046 && gfc_state_stack->state != COMP_DERIVED)
2048 gfc_error ("Initialization of variable at %C is not allowed in "
2049 "a PURE procedure");
2050 m = MATCH_ERROR;
2053 if (current_attr.flavor != FL_PARAMETER
2054 && gfc_state_stack->state != COMP_DERIVED)
2055 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2057 if (m != MATCH_YES)
2058 goto cleanup;
2062 if (initializer != NULL && current_attr.allocatable
2063 && gfc_current_state () == COMP_DERIVED)
2065 gfc_error ("Initialization of allocatable component at %C is not "
2066 "allowed");
2067 m = MATCH_ERROR;
2068 goto cleanup;
2071 /* Add the initializer. Note that it is fine if initializer is
2072 NULL here, because we sometimes also need to check if a
2073 declaration *must* have an initialization expression. */
2074 if (gfc_current_state () != COMP_DERIVED)
2075 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2076 else
2078 if (current_ts.type == BT_DERIVED
2079 && !current_attr.pointer && !initializer)
2080 initializer = gfc_default_initializer (&current_ts);
2081 t = build_struct (name, cl, &initializer, &as);
2084 m = (t) ? MATCH_YES : MATCH_ERROR;
2086 cleanup:
2087 /* Free stuff up and return. */
2088 gfc_free_expr (initializer);
2089 gfc_free_array_spec (as);
2091 return m;
2095 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2096 This assumes that the byte size is equal to the kind number for
2097 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2099 match
2100 gfc_match_old_kind_spec (gfc_typespec *ts)
2102 match m;
2103 int original_kind;
2105 if (gfc_match_char ('*') != MATCH_YES)
2106 return MATCH_NO;
2108 m = gfc_match_small_literal_int (&ts->kind, NULL);
2109 if (m != MATCH_YES)
2110 return MATCH_ERROR;
2112 original_kind = ts->kind;
2114 /* Massage the kind numbers for complex types. */
2115 if (ts->type == BT_COMPLEX)
2117 if (ts->kind % 2)
2119 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2120 gfc_basic_typename (ts->type), original_kind);
2121 return MATCH_ERROR;
2123 ts->kind /= 2;
2127 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2128 ts->kind = 8;
2130 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2132 if (ts->kind == 4)
2134 if (gfc_option.flag_real4_kind == 8)
2135 ts->kind = 8;
2136 if (gfc_option.flag_real4_kind == 10)
2137 ts->kind = 10;
2138 if (gfc_option.flag_real4_kind == 16)
2139 ts->kind = 16;
2142 if (ts->kind == 8)
2144 if (gfc_option.flag_real8_kind == 4)
2145 ts->kind = 4;
2146 if (gfc_option.flag_real8_kind == 10)
2147 ts->kind = 10;
2148 if (gfc_option.flag_real8_kind == 16)
2149 ts->kind = 16;
2153 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2155 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2156 gfc_basic_typename (ts->type), original_kind);
2157 return MATCH_ERROR;
2160 if (!gfc_notify_std (GFC_STD_GNU,
2161 "Nonstandard type declaration %s*%d at %C",
2162 gfc_basic_typename(ts->type), original_kind))
2163 return MATCH_ERROR;
2165 return MATCH_YES;
2169 /* Match a kind specification. Since kinds are generally optional, we
2170 usually return MATCH_NO if something goes wrong. If a "kind="
2171 string is found, then we know we have an error. */
2173 match
2174 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2176 locus where, loc;
2177 gfc_expr *e;
2178 match m, n;
2179 char c;
2180 const char *msg;
2182 m = MATCH_NO;
2183 n = MATCH_YES;
2184 e = NULL;
2186 where = loc = gfc_current_locus;
2188 if (kind_expr_only)
2189 goto kind_expr;
2191 if (gfc_match_char ('(') == MATCH_NO)
2192 return MATCH_NO;
2194 /* Also gobbles optional text. */
2195 if (gfc_match (" kind = ") == MATCH_YES)
2196 m = MATCH_ERROR;
2198 loc = gfc_current_locus;
2200 kind_expr:
2201 n = gfc_match_init_expr (&e);
2203 if (n != MATCH_YES)
2205 if (gfc_matching_function)
2207 /* The function kind expression might include use associated or
2208 imported parameters and try again after the specification
2209 expressions..... */
2210 if (gfc_match_char (')') != MATCH_YES)
2212 gfc_error ("Missing right parenthesis at %C");
2213 m = MATCH_ERROR;
2214 goto no_match;
2217 gfc_free_expr (e);
2218 gfc_undo_symbols ();
2219 return MATCH_YES;
2221 else
2223 /* ....or else, the match is real. */
2224 if (n == MATCH_NO)
2225 gfc_error ("Expected initialization expression at %C");
2226 if (n != MATCH_YES)
2227 return MATCH_ERROR;
2231 if (e->rank != 0)
2233 gfc_error ("Expected scalar initialization expression at %C");
2234 m = MATCH_ERROR;
2235 goto no_match;
2238 msg = gfc_extract_int (e, &ts->kind);
2240 if (msg != NULL)
2242 gfc_error (msg);
2243 m = MATCH_ERROR;
2244 goto no_match;
2247 /* Before throwing away the expression, let's see if we had a
2248 C interoperable kind (and store the fact). */
2249 if (e->ts.is_c_interop == 1)
2251 /* Mark this as C interoperable if being declared with one
2252 of the named constants from iso_c_binding. */
2253 ts->is_c_interop = e->ts.is_iso_c;
2254 ts->f90_type = e->ts.f90_type;
2257 gfc_free_expr (e);
2258 e = NULL;
2260 /* Ignore errors to this point, if we've gotten here. This means
2261 we ignore the m=MATCH_ERROR from above. */
2262 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2264 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2265 gfc_basic_typename (ts->type));
2266 gfc_current_locus = where;
2267 return MATCH_ERROR;
2270 /* Warn if, e.g., c_int is used for a REAL variable, but not
2271 if, e.g., c_double is used for COMPLEX as the standard
2272 explicitly says that the kind type parameter for complex and real
2273 variable is the same, i.e. c_float == c_float_complex. */
2274 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2275 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2276 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2277 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2278 "is %s", gfc_basic_typename (ts->f90_type), &where,
2279 gfc_basic_typename (ts->type));
2281 gfc_gobble_whitespace ();
2282 if ((c = gfc_next_ascii_char ()) != ')'
2283 && (ts->type != BT_CHARACTER || c != ','))
2285 if (ts->type == BT_CHARACTER)
2286 gfc_error ("Missing right parenthesis or comma at %C");
2287 else
2288 gfc_error ("Missing right parenthesis at %C");
2289 m = MATCH_ERROR;
2291 else
2292 /* All tests passed. */
2293 m = MATCH_YES;
2295 if(m == MATCH_ERROR)
2296 gfc_current_locus = where;
2298 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2299 ts->kind = 8;
2301 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2303 if (ts->kind == 4)
2305 if (gfc_option.flag_real4_kind == 8)
2306 ts->kind = 8;
2307 if (gfc_option.flag_real4_kind == 10)
2308 ts->kind = 10;
2309 if (gfc_option.flag_real4_kind == 16)
2310 ts->kind = 16;
2313 if (ts->kind == 8)
2315 if (gfc_option.flag_real8_kind == 4)
2316 ts->kind = 4;
2317 if (gfc_option.flag_real8_kind == 10)
2318 ts->kind = 10;
2319 if (gfc_option.flag_real8_kind == 16)
2320 ts->kind = 16;
2324 /* Return what we know from the test(s). */
2325 return m;
2327 no_match:
2328 gfc_free_expr (e);
2329 gfc_current_locus = where;
2330 return m;
2334 static match
2335 match_char_kind (int * kind, int * is_iso_c)
2337 locus where;
2338 gfc_expr *e;
2339 match m, n;
2340 const char *msg;
2342 m = MATCH_NO;
2343 e = NULL;
2344 where = gfc_current_locus;
2346 n = gfc_match_init_expr (&e);
2348 if (n != MATCH_YES && gfc_matching_function)
2350 /* The expression might include use-associated or imported
2351 parameters and try again after the specification
2352 expressions. */
2353 gfc_free_expr (e);
2354 gfc_undo_symbols ();
2355 return MATCH_YES;
2358 if (n == MATCH_NO)
2359 gfc_error ("Expected initialization expression at %C");
2360 if (n != MATCH_YES)
2361 return MATCH_ERROR;
2363 if (e->rank != 0)
2365 gfc_error ("Expected scalar initialization expression at %C");
2366 m = MATCH_ERROR;
2367 goto no_match;
2370 msg = gfc_extract_int (e, kind);
2371 *is_iso_c = e->ts.is_iso_c;
2372 if (msg != NULL)
2374 gfc_error (msg);
2375 m = MATCH_ERROR;
2376 goto no_match;
2379 gfc_free_expr (e);
2381 /* Ignore errors to this point, if we've gotten here. This means
2382 we ignore the m=MATCH_ERROR from above. */
2383 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2385 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2386 m = MATCH_ERROR;
2388 else
2389 /* All tests passed. */
2390 m = MATCH_YES;
2392 if (m == MATCH_ERROR)
2393 gfc_current_locus = where;
2395 /* Return what we know from the test(s). */
2396 return m;
2398 no_match:
2399 gfc_free_expr (e);
2400 gfc_current_locus = where;
2401 return m;
2405 /* Match the various kind/length specifications in a CHARACTER
2406 declaration. We don't return MATCH_NO. */
2408 match
2409 gfc_match_char_spec (gfc_typespec *ts)
2411 int kind, seen_length, is_iso_c;
2412 gfc_charlen *cl;
2413 gfc_expr *len;
2414 match m;
2415 bool deferred;
2417 len = NULL;
2418 seen_length = 0;
2419 kind = 0;
2420 is_iso_c = 0;
2421 deferred = false;
2423 /* Try the old-style specification first. */
2424 old_char_selector = 0;
2426 m = match_char_length (&len, &deferred, true);
2427 if (m != MATCH_NO)
2429 if (m == MATCH_YES)
2430 old_char_selector = 1;
2431 seen_length = 1;
2432 goto done;
2435 m = gfc_match_char ('(');
2436 if (m != MATCH_YES)
2438 m = MATCH_YES; /* Character without length is a single char. */
2439 goto done;
2442 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2443 if (gfc_match (" kind =") == MATCH_YES)
2445 m = match_char_kind (&kind, &is_iso_c);
2447 if (m == MATCH_ERROR)
2448 goto done;
2449 if (m == MATCH_NO)
2450 goto syntax;
2452 if (gfc_match (" , len =") == MATCH_NO)
2453 goto rparen;
2455 m = char_len_param_value (&len, &deferred);
2456 if (m == MATCH_NO)
2457 goto syntax;
2458 if (m == MATCH_ERROR)
2459 goto done;
2460 seen_length = 1;
2462 goto rparen;
2465 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2466 if (gfc_match (" len =") == MATCH_YES)
2468 m = char_len_param_value (&len, &deferred);
2469 if (m == MATCH_NO)
2470 goto syntax;
2471 if (m == MATCH_ERROR)
2472 goto done;
2473 seen_length = 1;
2475 if (gfc_match_char (')') == MATCH_YES)
2476 goto done;
2478 if (gfc_match (" , kind =") != MATCH_YES)
2479 goto syntax;
2481 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2482 goto done;
2484 goto rparen;
2487 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2488 m = char_len_param_value (&len, &deferred);
2489 if (m == MATCH_NO)
2490 goto syntax;
2491 if (m == MATCH_ERROR)
2492 goto done;
2493 seen_length = 1;
2495 m = gfc_match_char (')');
2496 if (m == MATCH_YES)
2497 goto done;
2499 if (gfc_match_char (',') != MATCH_YES)
2500 goto syntax;
2502 gfc_match (" kind ="); /* Gobble optional text. */
2504 m = match_char_kind (&kind, &is_iso_c);
2505 if (m == MATCH_ERROR)
2506 goto done;
2507 if (m == MATCH_NO)
2508 goto syntax;
2510 rparen:
2511 /* Require a right-paren at this point. */
2512 m = gfc_match_char (')');
2513 if (m == MATCH_YES)
2514 goto done;
2516 syntax:
2517 gfc_error ("Syntax error in CHARACTER declaration at %C");
2518 m = MATCH_ERROR;
2519 gfc_free_expr (len);
2520 return m;
2522 done:
2523 /* Deal with character functions after USE and IMPORT statements. */
2524 if (gfc_matching_function)
2526 gfc_free_expr (len);
2527 gfc_undo_symbols ();
2528 return MATCH_YES;
2531 if (m != MATCH_YES)
2533 gfc_free_expr (len);
2534 return m;
2537 /* Do some final massaging of the length values. */
2538 cl = gfc_new_charlen (gfc_current_ns, NULL);
2540 if (seen_length == 0)
2541 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2542 else
2543 cl->length = len;
2545 ts->u.cl = cl;
2546 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2547 ts->deferred = deferred;
2549 /* We have to know if it was a C interoperable kind so we can
2550 do accurate type checking of bind(c) procs, etc. */
2551 if (kind != 0)
2552 /* Mark this as C interoperable if being declared with one
2553 of the named constants from iso_c_binding. */
2554 ts->is_c_interop = is_iso_c;
2555 else if (len != NULL)
2556 /* Here, we might have parsed something such as: character(c_char)
2557 In this case, the parsing code above grabs the c_char when
2558 looking for the length (line 1690, roughly). it's the last
2559 testcase for parsing the kind params of a character variable.
2560 However, it's not actually the length. this seems like it
2561 could be an error.
2562 To see if the user used a C interop kind, test the expr
2563 of the so called length, and see if it's C interoperable. */
2564 ts->is_c_interop = len->ts.is_iso_c;
2566 return MATCH_YES;
2570 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2571 structure to the matched specification. This is necessary for FUNCTION and
2572 IMPLICIT statements.
2574 If implicit_flag is nonzero, then we don't check for the optional
2575 kind specification. Not doing so is needed for matching an IMPLICIT
2576 statement correctly. */
2578 match
2579 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2581 char name[GFC_MAX_SYMBOL_LEN + 1];
2582 gfc_symbol *sym, *dt_sym;
2583 match m;
2584 char c;
2585 bool seen_deferred_kind, matched_type;
2586 const char *dt_name;
2588 /* A belt and braces check that the typespec is correctly being treated
2589 as a deferred characteristic association. */
2590 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2591 && (gfc_current_block ()->result->ts.kind == -1)
2592 && (ts->kind == -1);
2593 gfc_clear_ts (ts);
2594 if (seen_deferred_kind)
2595 ts->kind = -1;
2597 /* Clear the current binding label, in case one is given. */
2598 curr_binding_label = NULL;
2600 if (gfc_match (" byte") == MATCH_YES)
2602 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
2603 return MATCH_ERROR;
2605 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2607 gfc_error ("BYTE type used at %C "
2608 "is not available on the target machine");
2609 return MATCH_ERROR;
2612 ts->type = BT_INTEGER;
2613 ts->kind = 1;
2614 return MATCH_YES;
2618 m = gfc_match (" type (");
2619 matched_type = (m == MATCH_YES);
2620 if (matched_type)
2622 gfc_gobble_whitespace ();
2623 if (gfc_peek_ascii_char () == '*')
2625 if ((m = gfc_match ("*)")) != MATCH_YES)
2626 return m;
2627 if (gfc_current_state () == COMP_DERIVED)
2629 gfc_error ("Assumed type at %C is not allowed for components");
2630 return MATCH_ERROR;
2632 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
2633 "at %C"))
2634 return MATCH_ERROR;
2635 ts->type = BT_ASSUMED;
2636 return MATCH_YES;
2639 m = gfc_match ("%n", name);
2640 matched_type = (m == MATCH_YES);
2643 if ((matched_type && strcmp ("integer", name) == 0)
2644 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2646 ts->type = BT_INTEGER;
2647 ts->kind = gfc_default_integer_kind;
2648 goto get_kind;
2651 if ((matched_type && strcmp ("character", name) == 0)
2652 || (!matched_type && gfc_match (" character") == MATCH_YES))
2654 if (matched_type
2655 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2656 "intrinsic-type-spec at %C"))
2657 return MATCH_ERROR;
2659 ts->type = BT_CHARACTER;
2660 if (implicit_flag == 0)
2661 m = gfc_match_char_spec (ts);
2662 else
2663 m = MATCH_YES;
2665 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2666 m = MATCH_ERROR;
2668 return m;
2671 if ((matched_type && strcmp ("real", name) == 0)
2672 || (!matched_type && gfc_match (" real") == MATCH_YES))
2674 ts->type = BT_REAL;
2675 ts->kind = gfc_default_real_kind;
2676 goto get_kind;
2679 if ((matched_type
2680 && (strcmp ("doubleprecision", name) == 0
2681 || (strcmp ("double", name) == 0
2682 && gfc_match (" precision") == MATCH_YES)))
2683 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2685 if (matched_type
2686 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2687 "intrinsic-type-spec at %C"))
2688 return MATCH_ERROR;
2689 if (matched_type && gfc_match_char (')') != MATCH_YES)
2690 return MATCH_ERROR;
2692 ts->type = BT_REAL;
2693 ts->kind = gfc_default_double_kind;
2694 return MATCH_YES;
2697 if ((matched_type && strcmp ("complex", name) == 0)
2698 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2700 ts->type = BT_COMPLEX;
2701 ts->kind = gfc_default_complex_kind;
2702 goto get_kind;
2705 if ((matched_type
2706 && (strcmp ("doublecomplex", name) == 0
2707 || (strcmp ("double", name) == 0
2708 && gfc_match (" complex") == MATCH_YES)))
2709 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2711 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
2712 return MATCH_ERROR;
2714 if (matched_type
2715 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2716 "intrinsic-type-spec at %C"))
2717 return MATCH_ERROR;
2719 if (matched_type && gfc_match_char (')') != MATCH_YES)
2720 return MATCH_ERROR;
2722 ts->type = BT_COMPLEX;
2723 ts->kind = gfc_default_double_kind;
2724 return MATCH_YES;
2727 if ((matched_type && strcmp ("logical", name) == 0)
2728 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2730 ts->type = BT_LOGICAL;
2731 ts->kind = gfc_default_logical_kind;
2732 goto get_kind;
2735 if (matched_type)
2736 m = gfc_match_char (')');
2738 if (m == MATCH_YES)
2739 ts->type = BT_DERIVED;
2740 else
2742 /* Match CLASS declarations. */
2743 m = gfc_match (" class ( * )");
2744 if (m == MATCH_ERROR)
2745 return MATCH_ERROR;
2746 else if (m == MATCH_YES)
2748 gfc_symbol *upe;
2749 gfc_symtree *st;
2750 ts->type = BT_CLASS;
2751 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
2752 if (upe == NULL)
2754 upe = gfc_new_symbol ("STAR", gfc_current_ns);
2755 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2756 st->n.sym = upe;
2757 gfc_set_sym_referenced (upe);
2758 upe->refs++;
2759 upe->ts.type = BT_VOID;
2760 upe->attr.unlimited_polymorphic = 1;
2761 /* This is essential to force the construction of
2762 unlimited polymorphic component class containers. */
2763 upe->attr.zero_comp = 1;
2764 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
2765 &gfc_current_locus))
2766 return MATCH_ERROR;
2768 else
2770 st = gfc_find_symtree (gfc_current_ns->sym_root, "STAR");
2771 if (st == NULL)
2772 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2773 st->n.sym = upe;
2774 upe->refs++;
2776 ts->u.derived = upe;
2777 return m;
2780 m = gfc_match (" class ( %n )", name);
2781 if (m != MATCH_YES)
2782 return m;
2783 ts->type = BT_CLASS;
2785 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
2786 return MATCH_ERROR;
2789 /* Defer association of the derived type until the end of the
2790 specification block. However, if the derived type can be
2791 found, add it to the typespec. */
2792 if (gfc_matching_function)
2794 ts->u.derived = NULL;
2795 if (gfc_current_state () != COMP_INTERFACE
2796 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2798 sym = gfc_find_dt_in_generic (sym);
2799 ts->u.derived = sym;
2801 return MATCH_YES;
2804 /* Search for the name but allow the components to be defined later. If
2805 type = -1, this typespec has been seen in a function declaration but
2806 the type could not be accessed at that point. The actual derived type is
2807 stored in a symtree with the first letter of the name capitalized; the
2808 symtree with the all lower-case name contains the associated
2809 generic function. */
2810 dt_name = gfc_get_string ("%c%s",
2811 (char) TOUPPER ((unsigned char) name[0]),
2812 (const char*)&name[1]);
2813 sym = NULL;
2814 dt_sym = NULL;
2815 if (ts->kind != -1)
2817 gfc_get_ha_symbol (name, &sym);
2818 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2820 gfc_error ("Type name '%s' at %C is ambiguous", name);
2821 return MATCH_ERROR;
2823 if (sym->generic && !dt_sym)
2824 dt_sym = gfc_find_dt_in_generic (sym);
2826 else if (ts->kind == -1)
2828 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2829 || gfc_current_ns->has_import_set;
2830 gfc_find_symbol (name, NULL, iface, &sym);
2831 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2833 gfc_error ("Type name '%s' at %C is ambiguous", name);
2834 return MATCH_ERROR;
2836 if (sym && sym->generic && !dt_sym)
2837 dt_sym = gfc_find_dt_in_generic (sym);
2839 ts->kind = 0;
2840 if (sym == NULL)
2841 return MATCH_NO;
2844 if ((sym->attr.flavor != FL_UNKNOWN
2845 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2846 || sym->attr.subroutine)
2848 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2849 "entity at %L, which has the same name", name,
2850 &sym->declared_at);
2851 return MATCH_ERROR;
2854 gfc_set_sym_referenced (sym);
2855 if (!sym->attr.generic
2856 && !gfc_add_generic (&sym->attr, sym->name, NULL))
2857 return MATCH_ERROR;
2859 if (!sym->attr.function
2860 && !gfc_add_function (&sym->attr, sym->name, NULL))
2861 return MATCH_ERROR;
2863 if (!dt_sym)
2865 gfc_interface *intr, *head;
2867 /* Use upper case to save the actual derived-type symbol. */
2868 gfc_get_symbol (dt_name, NULL, &dt_sym);
2869 dt_sym->name = gfc_get_string (sym->name);
2870 head = sym->generic;
2871 intr = gfc_get_interface ();
2872 intr->sym = dt_sym;
2873 intr->where = gfc_current_locus;
2874 intr->next = head;
2875 sym->generic = intr;
2876 sym->attr.if_source = IFSRC_DECL;
2879 gfc_set_sym_referenced (dt_sym);
2881 if (dt_sym->attr.flavor != FL_DERIVED
2882 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
2883 return MATCH_ERROR;
2885 ts->u.derived = dt_sym;
2887 return MATCH_YES;
2889 get_kind:
2890 if (matched_type
2891 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2892 "intrinsic-type-spec at %C"))
2893 return MATCH_ERROR;
2895 /* For all types except double, derived and character, look for an
2896 optional kind specifier. MATCH_NO is actually OK at this point. */
2897 if (implicit_flag == 1)
2899 if (matched_type && gfc_match_char (')') != MATCH_YES)
2900 return MATCH_ERROR;
2902 return MATCH_YES;
2905 if (gfc_current_form == FORM_FREE)
2907 c = gfc_peek_ascii_char ();
2908 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2909 && c != ':' && c != ',')
2911 if (matched_type && c == ')')
2913 gfc_next_ascii_char ();
2914 return MATCH_YES;
2916 return MATCH_NO;
2920 m = gfc_match_kind_spec (ts, false);
2921 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2922 m = gfc_match_old_kind_spec (ts);
2924 if (matched_type && gfc_match_char (')') != MATCH_YES)
2925 return MATCH_ERROR;
2927 /* Defer association of the KIND expression of function results
2928 until after USE and IMPORT statements. */
2929 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2930 || gfc_matching_function)
2931 return MATCH_YES;
2933 if (m == MATCH_NO)
2934 m = MATCH_YES; /* No kind specifier found. */
2936 return m;
2940 /* Match an IMPLICIT NONE statement. Actually, this statement is
2941 already matched in parse.c, or we would not end up here in the
2942 first place. So the only thing we need to check, is if there is
2943 trailing garbage. If not, the match is successful. */
2945 match
2946 gfc_match_implicit_none (void)
2948 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2952 /* Match the letter range(s) of an IMPLICIT statement. */
2954 static match
2955 match_implicit_range (void)
2957 char c, c1, c2;
2958 int inner;
2959 locus cur_loc;
2961 cur_loc = gfc_current_locus;
2963 gfc_gobble_whitespace ();
2964 c = gfc_next_ascii_char ();
2965 if (c != '(')
2967 gfc_error ("Missing character range in IMPLICIT at %C");
2968 goto bad;
2971 inner = 1;
2972 while (inner)
2974 gfc_gobble_whitespace ();
2975 c1 = gfc_next_ascii_char ();
2976 if (!ISALPHA (c1))
2977 goto bad;
2979 gfc_gobble_whitespace ();
2980 c = gfc_next_ascii_char ();
2982 switch (c)
2984 case ')':
2985 inner = 0; /* Fall through. */
2987 case ',':
2988 c2 = c1;
2989 break;
2991 case '-':
2992 gfc_gobble_whitespace ();
2993 c2 = gfc_next_ascii_char ();
2994 if (!ISALPHA (c2))
2995 goto bad;
2997 gfc_gobble_whitespace ();
2998 c = gfc_next_ascii_char ();
3000 if ((c != ',') && (c != ')'))
3001 goto bad;
3002 if (c == ')')
3003 inner = 0;
3005 break;
3007 default:
3008 goto bad;
3011 if (c1 > c2)
3013 gfc_error ("Letters must be in alphabetic order in "
3014 "IMPLICIT statement at %C");
3015 goto bad;
3018 /* See if we can add the newly matched range to the pending
3019 implicits from this IMPLICIT statement. We do not check for
3020 conflicts with whatever earlier IMPLICIT statements may have
3021 set. This is done when we've successfully finished matching
3022 the current one. */
3023 if (!gfc_add_new_implicit_range (c1, c2))
3024 goto bad;
3027 return MATCH_YES;
3029 bad:
3030 gfc_syntax_error (ST_IMPLICIT);
3032 gfc_current_locus = cur_loc;
3033 return MATCH_ERROR;
3037 /* Match an IMPLICIT statement, storing the types for
3038 gfc_set_implicit() if the statement is accepted by the parser.
3039 There is a strange looking, but legal syntactic construction
3040 possible. It looks like:
3042 IMPLICIT INTEGER (a-b) (c-d)
3044 This is legal if "a-b" is a constant expression that happens to
3045 equal one of the legal kinds for integers. The real problem
3046 happens with an implicit specification that looks like:
3048 IMPLICIT INTEGER (a-b)
3050 In this case, a typespec matcher that is "greedy" (as most of the
3051 matchers are) gobbles the character range as a kindspec, leaving
3052 nothing left. We therefore have to go a bit more slowly in the
3053 matching process by inhibiting the kindspec checking during
3054 typespec matching and checking for a kind later. */
3056 match
3057 gfc_match_implicit (void)
3059 gfc_typespec ts;
3060 locus cur_loc;
3061 char c;
3062 match m;
3064 gfc_clear_ts (&ts);
3066 /* We don't allow empty implicit statements. */
3067 if (gfc_match_eos () == MATCH_YES)
3069 gfc_error ("Empty IMPLICIT statement at %C");
3070 return MATCH_ERROR;
3075 /* First cleanup. */
3076 gfc_clear_new_implicit ();
3078 /* A basic type is mandatory here. */
3079 m = gfc_match_decl_type_spec (&ts, 1);
3080 if (m == MATCH_ERROR)
3081 goto error;
3082 if (m == MATCH_NO)
3083 goto syntax;
3085 cur_loc = gfc_current_locus;
3086 m = match_implicit_range ();
3088 if (m == MATCH_YES)
3090 /* We may have <TYPE> (<RANGE>). */
3091 gfc_gobble_whitespace ();
3092 c = gfc_next_ascii_char ();
3093 if ((c == '\n') || (c == ','))
3095 /* Check for CHARACTER with no length parameter. */
3096 if (ts.type == BT_CHARACTER && !ts.u.cl)
3098 ts.kind = gfc_default_character_kind;
3099 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3100 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3101 NULL, 1);
3104 /* Record the Successful match. */
3105 if (!gfc_merge_new_implicit (&ts))
3106 return MATCH_ERROR;
3107 continue;
3110 gfc_current_locus = cur_loc;
3113 /* Discard the (incorrectly) matched range. */
3114 gfc_clear_new_implicit ();
3116 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3117 if (ts.type == BT_CHARACTER)
3118 m = gfc_match_char_spec (&ts);
3119 else
3121 m = gfc_match_kind_spec (&ts, false);
3122 if (m == MATCH_NO)
3124 m = gfc_match_old_kind_spec (&ts);
3125 if (m == MATCH_ERROR)
3126 goto error;
3127 if (m == MATCH_NO)
3128 goto syntax;
3131 if (m == MATCH_ERROR)
3132 goto error;
3134 m = match_implicit_range ();
3135 if (m == MATCH_ERROR)
3136 goto error;
3137 if (m == MATCH_NO)
3138 goto syntax;
3140 gfc_gobble_whitespace ();
3141 c = gfc_next_ascii_char ();
3142 if ((c != '\n') && (c != ','))
3143 goto syntax;
3145 if (!gfc_merge_new_implicit (&ts))
3146 return MATCH_ERROR;
3148 while (c == ',');
3150 return MATCH_YES;
3152 syntax:
3153 gfc_syntax_error (ST_IMPLICIT);
3155 error:
3156 return MATCH_ERROR;
3160 match
3161 gfc_match_import (void)
3163 char name[GFC_MAX_SYMBOL_LEN + 1];
3164 match m;
3165 gfc_symbol *sym;
3166 gfc_symtree *st;
3168 if (gfc_current_ns->proc_name == NULL
3169 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3171 gfc_error ("IMPORT statement at %C only permitted in "
3172 "an INTERFACE body");
3173 return MATCH_ERROR;
3176 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
3177 return MATCH_ERROR;
3179 if (gfc_match_eos () == MATCH_YES)
3181 /* All host variables should be imported. */
3182 gfc_current_ns->has_import_set = 1;
3183 return MATCH_YES;
3186 if (gfc_match (" ::") == MATCH_YES)
3188 if (gfc_match_eos () == MATCH_YES)
3190 gfc_error ("Expecting list of named entities at %C");
3191 return MATCH_ERROR;
3195 for(;;)
3197 sym = NULL;
3198 m = gfc_match (" %n", name);
3199 switch (m)
3201 case MATCH_YES:
3202 if (gfc_current_ns->parent != NULL
3203 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3205 gfc_error ("Type name '%s' at %C is ambiguous", name);
3206 return MATCH_ERROR;
3208 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3209 && gfc_find_symbol (name,
3210 gfc_current_ns->proc_name->ns->parent,
3211 1, &sym))
3213 gfc_error ("Type name '%s' at %C is ambiguous", name);
3214 return MATCH_ERROR;
3217 if (sym == NULL)
3219 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3220 "at %C - does not exist.", name);
3221 return MATCH_ERROR;
3224 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3226 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3227 "at %C.", name);
3228 goto next_item;
3231 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3232 st->n.sym = sym;
3233 sym->refs++;
3234 sym->attr.imported = 1;
3236 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3238 /* The actual derived type is stored in a symtree with the first
3239 letter of the name capitalized; the symtree with the all
3240 lower-case name contains the associated generic function. */
3241 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3242 gfc_get_string ("%c%s",
3243 (char) TOUPPER ((unsigned char) name[0]),
3244 &name[1]));
3245 st->n.sym = sym;
3246 sym->refs++;
3247 sym->attr.imported = 1;
3250 goto next_item;
3252 case MATCH_NO:
3253 break;
3255 case MATCH_ERROR:
3256 return MATCH_ERROR;
3259 next_item:
3260 if (gfc_match_eos () == MATCH_YES)
3261 break;
3262 if (gfc_match_char (',') != MATCH_YES)
3263 goto syntax;
3266 return MATCH_YES;
3268 syntax:
3269 gfc_error ("Syntax error in IMPORT statement at %C");
3270 return MATCH_ERROR;
3274 /* A minimal implementation of gfc_match without whitespace, escape
3275 characters or variable arguments. Returns true if the next
3276 characters match the TARGET template exactly. */
3278 static bool
3279 match_string_p (const char *target)
3281 const char *p;
3283 for (p = target; *p; p++)
3284 if ((char) gfc_next_ascii_char () != *p)
3285 return false;
3286 return true;
3289 /* Matches an attribute specification including array specs. If
3290 successful, leaves the variables current_attr and current_as
3291 holding the specification. Also sets the colon_seen variable for
3292 later use by matchers associated with initializations.
3294 This subroutine is a little tricky in the sense that we don't know
3295 if we really have an attr-spec until we hit the double colon.
3296 Until that time, we can only return MATCH_NO. This forces us to
3297 check for duplicate specification at this level. */
3299 static match
3300 match_attr_spec (void)
3302 /* Modifiers that can exist in a type statement. */
3303 enum
3304 { GFC_DECL_BEGIN = 0,
3305 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3306 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3307 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3308 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3309 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3310 DECL_NONE, GFC_DECL_END /* Sentinel */
3313 /* GFC_DECL_END is the sentinel, index starts at 0. */
3314 #define NUM_DECL GFC_DECL_END
3316 locus start, seen_at[NUM_DECL];
3317 int seen[NUM_DECL];
3318 unsigned int d;
3319 const char *attr;
3320 match m;
3321 bool t;
3323 gfc_clear_attr (&current_attr);
3324 start = gfc_current_locus;
3326 current_as = NULL;
3327 colon_seen = 0;
3329 /* See if we get all of the keywords up to the final double colon. */
3330 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3331 seen[d] = 0;
3333 for (;;)
3335 char ch;
3337 d = DECL_NONE;
3338 gfc_gobble_whitespace ();
3340 ch = gfc_next_ascii_char ();
3341 if (ch == ':')
3343 /* This is the successful exit condition for the loop. */
3344 if (gfc_next_ascii_char () == ':')
3345 break;
3347 else if (ch == ',')
3349 gfc_gobble_whitespace ();
3350 switch (gfc_peek_ascii_char ())
3352 case 'a':
3353 gfc_next_ascii_char ();
3354 switch (gfc_next_ascii_char ())
3356 case 'l':
3357 if (match_string_p ("locatable"))
3359 /* Matched "allocatable". */
3360 d = DECL_ALLOCATABLE;
3362 break;
3364 case 's':
3365 if (match_string_p ("ynchronous"))
3367 /* Matched "asynchronous". */
3368 d = DECL_ASYNCHRONOUS;
3370 break;
3372 break;
3374 case 'b':
3375 /* Try and match the bind(c). */
3376 m = gfc_match_bind_c (NULL, true);
3377 if (m == MATCH_YES)
3378 d = DECL_IS_BIND_C;
3379 else if (m == MATCH_ERROR)
3380 goto cleanup;
3381 break;
3383 case 'c':
3384 gfc_next_ascii_char ();
3385 if ('o' != gfc_next_ascii_char ())
3386 break;
3387 switch (gfc_next_ascii_char ())
3389 case 'd':
3390 if (match_string_p ("imension"))
3392 d = DECL_CODIMENSION;
3393 break;
3395 case 'n':
3396 if (match_string_p ("tiguous"))
3398 d = DECL_CONTIGUOUS;
3399 break;
3402 break;
3404 case 'd':
3405 if (match_string_p ("dimension"))
3406 d = DECL_DIMENSION;
3407 break;
3409 case 'e':
3410 if (match_string_p ("external"))
3411 d = DECL_EXTERNAL;
3412 break;
3414 case 'i':
3415 if (match_string_p ("int"))
3417 ch = gfc_next_ascii_char ();
3418 if (ch == 'e')
3420 if (match_string_p ("nt"))
3422 /* Matched "intent". */
3423 /* TODO: Call match_intent_spec from here. */
3424 if (gfc_match (" ( in out )") == MATCH_YES)
3425 d = DECL_INOUT;
3426 else if (gfc_match (" ( in )") == MATCH_YES)
3427 d = DECL_IN;
3428 else if (gfc_match (" ( out )") == MATCH_YES)
3429 d = DECL_OUT;
3432 else if (ch == 'r')
3434 if (match_string_p ("insic"))
3436 /* Matched "intrinsic". */
3437 d = DECL_INTRINSIC;
3441 break;
3443 case 'o':
3444 if (match_string_p ("optional"))
3445 d = DECL_OPTIONAL;
3446 break;
3448 case 'p':
3449 gfc_next_ascii_char ();
3450 switch (gfc_next_ascii_char ())
3452 case 'a':
3453 if (match_string_p ("rameter"))
3455 /* Matched "parameter". */
3456 d = DECL_PARAMETER;
3458 break;
3460 case 'o':
3461 if (match_string_p ("inter"))
3463 /* Matched "pointer". */
3464 d = DECL_POINTER;
3466 break;
3468 case 'r':
3469 ch = gfc_next_ascii_char ();
3470 if (ch == 'i')
3472 if (match_string_p ("vate"))
3474 /* Matched "private". */
3475 d = DECL_PRIVATE;
3478 else if (ch == 'o')
3480 if (match_string_p ("tected"))
3482 /* Matched "protected". */
3483 d = DECL_PROTECTED;
3486 break;
3488 case 'u':
3489 if (match_string_p ("blic"))
3491 /* Matched "public". */
3492 d = DECL_PUBLIC;
3494 break;
3496 break;
3498 case 's':
3499 if (match_string_p ("save"))
3500 d = DECL_SAVE;
3501 break;
3503 case 't':
3504 if (match_string_p ("target"))
3505 d = DECL_TARGET;
3506 break;
3508 case 'v':
3509 gfc_next_ascii_char ();
3510 ch = gfc_next_ascii_char ();
3511 if (ch == 'a')
3513 if (match_string_p ("lue"))
3515 /* Matched "value". */
3516 d = DECL_VALUE;
3519 else if (ch == 'o')
3521 if (match_string_p ("latile"))
3523 /* Matched "volatile". */
3524 d = DECL_VOLATILE;
3527 break;
3531 /* No double colon and no recognizable decl_type, so assume that
3532 we've been looking at something else the whole time. */
3533 if (d == DECL_NONE)
3535 m = MATCH_NO;
3536 goto cleanup;
3539 /* Check to make sure any parens are paired up correctly. */
3540 if (gfc_match_parens () == MATCH_ERROR)
3542 m = MATCH_ERROR;
3543 goto cleanup;
3546 seen[d]++;
3547 seen_at[d] = gfc_current_locus;
3549 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3551 gfc_array_spec *as = NULL;
3553 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3554 d == DECL_CODIMENSION);
3556 if (current_as == NULL)
3557 current_as = as;
3558 else if (m == MATCH_YES)
3560 if (!merge_array_spec (as, current_as, false))
3561 m = MATCH_ERROR;
3562 free (as);
3565 if (m == MATCH_NO)
3567 if (d == DECL_CODIMENSION)
3568 gfc_error ("Missing codimension specification at %C");
3569 else
3570 gfc_error ("Missing dimension specification at %C");
3571 m = MATCH_ERROR;
3574 if (m == MATCH_ERROR)
3575 goto cleanup;
3579 /* Since we've seen a double colon, we have to be looking at an
3580 attr-spec. This means that we can now issue errors. */
3581 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3582 if (seen[d] > 1)
3584 switch (d)
3586 case DECL_ALLOCATABLE:
3587 attr = "ALLOCATABLE";
3588 break;
3589 case DECL_ASYNCHRONOUS:
3590 attr = "ASYNCHRONOUS";
3591 break;
3592 case DECL_CODIMENSION:
3593 attr = "CODIMENSION";
3594 break;
3595 case DECL_CONTIGUOUS:
3596 attr = "CONTIGUOUS";
3597 break;
3598 case DECL_DIMENSION:
3599 attr = "DIMENSION";
3600 break;
3601 case DECL_EXTERNAL:
3602 attr = "EXTERNAL";
3603 break;
3604 case DECL_IN:
3605 attr = "INTENT (IN)";
3606 break;
3607 case DECL_OUT:
3608 attr = "INTENT (OUT)";
3609 break;
3610 case DECL_INOUT:
3611 attr = "INTENT (IN OUT)";
3612 break;
3613 case DECL_INTRINSIC:
3614 attr = "INTRINSIC";
3615 break;
3616 case DECL_OPTIONAL:
3617 attr = "OPTIONAL";
3618 break;
3619 case DECL_PARAMETER:
3620 attr = "PARAMETER";
3621 break;
3622 case DECL_POINTER:
3623 attr = "POINTER";
3624 break;
3625 case DECL_PROTECTED:
3626 attr = "PROTECTED";
3627 break;
3628 case DECL_PRIVATE:
3629 attr = "PRIVATE";
3630 break;
3631 case DECL_PUBLIC:
3632 attr = "PUBLIC";
3633 break;
3634 case DECL_SAVE:
3635 attr = "SAVE";
3636 break;
3637 case DECL_TARGET:
3638 attr = "TARGET";
3639 break;
3640 case DECL_IS_BIND_C:
3641 attr = "IS_BIND_C";
3642 break;
3643 case DECL_VALUE:
3644 attr = "VALUE";
3645 break;
3646 case DECL_VOLATILE:
3647 attr = "VOLATILE";
3648 break;
3649 default:
3650 attr = NULL; /* This shouldn't happen. */
3653 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3654 m = MATCH_ERROR;
3655 goto cleanup;
3658 /* Now that we've dealt with duplicate attributes, add the attributes
3659 to the current attribute. */
3660 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3662 if (seen[d] == 0)
3663 continue;
3665 if (gfc_current_state () == COMP_DERIVED
3666 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3667 && d != DECL_POINTER && d != DECL_PRIVATE
3668 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3670 if (d == DECL_ALLOCATABLE)
3672 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
3673 "attribute at %C in a TYPE definition"))
3675 m = MATCH_ERROR;
3676 goto cleanup;
3679 else
3681 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3682 &seen_at[d]);
3683 m = MATCH_ERROR;
3684 goto cleanup;
3688 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3689 && gfc_current_state () != COMP_MODULE)
3691 if (d == DECL_PRIVATE)
3692 attr = "PRIVATE";
3693 else
3694 attr = "PUBLIC";
3695 if (gfc_current_state () == COMP_DERIVED
3696 && gfc_state_stack->previous
3697 && gfc_state_stack->previous->state == COMP_MODULE)
3699 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
3700 "at %L in a TYPE definition", attr,
3701 &seen_at[d]))
3703 m = MATCH_ERROR;
3704 goto cleanup;
3707 else
3709 gfc_error ("%s attribute at %L is not allowed outside of the "
3710 "specification part of a module", attr, &seen_at[d]);
3711 m = MATCH_ERROR;
3712 goto cleanup;
3716 switch (d)
3718 case DECL_ALLOCATABLE:
3719 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3720 break;
3722 case DECL_ASYNCHRONOUS:
3723 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
3724 t = false;
3725 else
3726 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3727 break;
3729 case DECL_CODIMENSION:
3730 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3731 break;
3733 case DECL_CONTIGUOUS:
3734 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
3735 t = false;
3736 else
3737 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3738 break;
3740 case DECL_DIMENSION:
3741 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3742 break;
3744 case DECL_EXTERNAL:
3745 t = gfc_add_external (&current_attr, &seen_at[d]);
3746 break;
3748 case DECL_IN:
3749 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3750 break;
3752 case DECL_OUT:
3753 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3754 break;
3756 case DECL_INOUT:
3757 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3758 break;
3760 case DECL_INTRINSIC:
3761 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3762 break;
3764 case DECL_OPTIONAL:
3765 t = gfc_add_optional (&current_attr, &seen_at[d]);
3766 break;
3768 case DECL_PARAMETER:
3769 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3770 break;
3772 case DECL_POINTER:
3773 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3774 break;
3776 case DECL_PROTECTED:
3777 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3779 gfc_error ("PROTECTED at %C only allowed in specification "
3780 "part of a module");
3781 t = false;
3782 break;
3785 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
3786 t = false;
3787 else
3788 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3789 break;
3791 case DECL_PRIVATE:
3792 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3793 &seen_at[d]);
3794 break;
3796 case DECL_PUBLIC:
3797 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3798 &seen_at[d]);
3799 break;
3801 case DECL_SAVE:
3802 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3803 break;
3805 case DECL_TARGET:
3806 t = gfc_add_target (&current_attr, &seen_at[d]);
3807 break;
3809 case DECL_IS_BIND_C:
3810 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3811 break;
3813 case DECL_VALUE:
3814 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
3815 t = false;
3816 else
3817 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3818 break;
3820 case DECL_VOLATILE:
3821 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
3822 t = false;
3823 else
3824 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3825 break;
3827 default:
3828 gfc_internal_error ("match_attr_spec(): Bad attribute");
3831 if (!t)
3833 m = MATCH_ERROR;
3834 goto cleanup;
3838 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3839 if (gfc_current_state () == COMP_MODULE && !current_attr.save
3840 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
3841 current_attr.save = SAVE_IMPLICIT;
3843 colon_seen = 1;
3844 return MATCH_YES;
3846 cleanup:
3847 gfc_current_locus = start;
3848 gfc_free_array_spec (current_as);
3849 current_as = NULL;
3850 return m;
3854 /* Set the binding label, dest_label, either with the binding label
3855 stored in the given gfc_typespec, ts, or if none was provided, it
3856 will be the symbol name in all lower case, as required by the draft
3857 (J3/04-007, section 15.4.1). If a binding label was given and
3858 there is more than one argument (num_idents), it is an error. */
3860 static bool
3861 set_binding_label (const char **dest_label, const char *sym_name,
3862 int num_idents)
3864 if (num_idents > 1 && has_name_equals)
3866 gfc_error ("Multiple identifiers provided with "
3867 "single NAME= specifier at %C");
3868 return false;
3871 if (curr_binding_label)
3872 /* Binding label given; store in temp holder till have sym. */
3873 *dest_label = curr_binding_label;
3874 else
3876 /* No binding label given, and the NAME= specifier did not exist,
3877 which means there was no NAME="". */
3878 if (sym_name != NULL && has_name_equals == 0)
3879 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
3882 return true;
3886 /* Set the status of the given common block as being BIND(C) or not,
3887 depending on the given parameter, is_bind_c. */
3889 void
3890 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3892 com_block->is_bind_c = is_bind_c;
3893 return;
3897 /* Verify that the given gfc_typespec is for a C interoperable type. */
3899 bool
3900 gfc_verify_c_interop (gfc_typespec *ts)
3902 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3903 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3904 ? true : false;
3905 else if (ts->type == BT_CLASS)
3906 return false;
3907 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
3908 return false;
3910 return true;
3914 /* Verify that the variables of a given common block, which has been
3915 defined with the attribute specifier bind(c), to be of a C
3916 interoperable type. Errors will be reported here, if
3917 encountered. */
3919 bool
3920 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3922 gfc_symbol *curr_sym = NULL;
3923 bool retval = true;
3925 curr_sym = com_block->head;
3927 /* Make sure we have at least one symbol. */
3928 if (curr_sym == NULL)
3929 return retval;
3931 /* Here we know we have a symbol, so we'll execute this loop
3932 at least once. */
3935 /* The second to last param, 1, says this is in a common block. */
3936 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3937 curr_sym = curr_sym->common_next;
3938 } while (curr_sym != NULL);
3940 return retval;
3944 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3945 an appropriate error message is reported. */
3947 bool
3948 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3949 int is_in_common, gfc_common_head *com_block)
3951 bool bind_c_function = false;
3952 bool retval = true;
3954 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3955 bind_c_function = true;
3957 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3959 tmp_sym = tmp_sym->result;
3960 /* Make sure it wasn't an implicitly typed result. */
3961 if (tmp_sym->attr.implicit_type && gfc_option.warn_c_binding_type)
3963 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3964 "%L may not be C interoperable", tmp_sym->name,
3965 &tmp_sym->declared_at);
3966 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3967 /* Mark it as C interoperable to prevent duplicate warnings. */
3968 tmp_sym->ts.is_c_interop = 1;
3969 tmp_sym->attr.is_c_interop = 1;
3973 /* Here, we know we have the bind(c) attribute, so if we have
3974 enough type info, then verify that it's a C interop kind.
3975 The info could be in the symbol already, or possibly still in
3976 the given ts (current_ts), so look in both. */
3977 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3979 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
3981 /* See if we're dealing with a sym in a common block or not. */
3982 if (is_in_common == 1 && gfc_option.warn_c_binding_type)
3984 gfc_warning ("Variable '%s' in common block '%s' at %L "
3985 "may not be a C interoperable "
3986 "kind though common block '%s' is BIND(C)",
3987 tmp_sym->name, com_block->name,
3988 &(tmp_sym->declared_at), com_block->name);
3990 else
3992 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3993 gfc_error ("Type declaration '%s' at %L is not C "
3994 "interoperable but it is BIND(C)",
3995 tmp_sym->name, &(tmp_sym->declared_at));
3996 else if (gfc_option.warn_c_binding_type)
3997 gfc_warning ("Variable '%s' at %L "
3998 "may not be a C interoperable "
3999 "kind but it is bind(c)",
4000 tmp_sym->name, &(tmp_sym->declared_at));
4004 /* Variables declared w/in a common block can't be bind(c)
4005 since there's no way for C to see these variables, so there's
4006 semantically no reason for the attribute. */
4007 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
4009 gfc_error ("Variable '%s' in common block '%s' at "
4010 "%L cannot be declared with BIND(C) "
4011 "since it is not a global",
4012 tmp_sym->name, com_block->name,
4013 &(tmp_sym->declared_at));
4014 retval = false;
4017 /* Scalar variables that are bind(c) can not have the pointer
4018 or allocatable attributes. */
4019 if (tmp_sym->attr.is_bind_c == 1)
4021 if (tmp_sym->attr.pointer == 1)
4023 gfc_error ("Variable '%s' at %L cannot have both the "
4024 "POINTER and BIND(C) attributes",
4025 tmp_sym->name, &(tmp_sym->declared_at));
4026 retval = false;
4029 if (tmp_sym->attr.allocatable == 1)
4031 gfc_error ("Variable '%s' at %L cannot have both the "
4032 "ALLOCATABLE and BIND(C) attributes",
4033 tmp_sym->name, &(tmp_sym->declared_at));
4034 retval = false;
4039 /* If it is a BIND(C) function, make sure the return value is a
4040 scalar value. The previous tests in this function made sure
4041 the type is interoperable. */
4042 if (bind_c_function && tmp_sym->as != NULL)
4043 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4044 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
4046 /* BIND(C) functions can not return a character string. */
4047 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
4048 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
4049 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
4050 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4051 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4052 "be a character string", tmp_sym->name,
4053 &(tmp_sym->declared_at));
4056 /* See if the symbol has been marked as private. If it has, make sure
4057 there is no binding label and warn the user if there is one. */
4058 if (tmp_sym->attr.access == ACCESS_PRIVATE
4059 && tmp_sym->binding_label)
4060 /* Use gfc_warning_now because we won't say that the symbol fails
4061 just because of this. */
4062 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
4063 "given the binding label '%s'", tmp_sym->name,
4064 &(tmp_sym->declared_at), tmp_sym->binding_label);
4066 return retval;
4070 /* Set the appropriate fields for a symbol that's been declared as
4071 BIND(C) (the is_bind_c flag and the binding label), and verify that
4072 the type is C interoperable. Errors are reported by the functions
4073 used to set/test these fields. */
4075 bool
4076 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4078 bool retval = true;
4080 /* TODO: Do we need to make sure the vars aren't marked private? */
4082 /* Set the is_bind_c bit in symbol_attribute. */
4083 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4085 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
4086 return false;
4088 return retval;
4092 /* Set the fields marking the given common block as BIND(C), including
4093 a binding label, and report any errors encountered. */
4095 bool
4096 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4098 bool retval = true;
4100 /* destLabel, common name, typespec (which may have binding label). */
4101 if (!set_binding_label (&com_block->binding_label, com_block->name,
4102 num_idents))
4103 return false;
4105 /* Set the given common block (com_block) to being bind(c) (1). */
4106 set_com_block_bind_c (com_block, 1);
4108 return retval;
4112 /* Retrieve the list of one or more identifiers that the given bind(c)
4113 attribute applies to. */
4115 bool
4116 get_bind_c_idents (void)
4118 char name[GFC_MAX_SYMBOL_LEN + 1];
4119 int num_idents = 0;
4120 gfc_symbol *tmp_sym = NULL;
4121 match found_id;
4122 gfc_common_head *com_block = NULL;
4124 if (gfc_match_name (name) == MATCH_YES)
4126 found_id = MATCH_YES;
4127 gfc_get_ha_symbol (name, &tmp_sym);
4129 else if (match_common_name (name) == MATCH_YES)
4131 found_id = MATCH_YES;
4132 com_block = gfc_get_common (name, 0);
4134 else
4136 gfc_error ("Need either entity or common block name for "
4137 "attribute specification statement at %C");
4138 return false;
4141 /* Save the current identifier and look for more. */
4144 /* Increment the number of identifiers found for this spec stmt. */
4145 num_idents++;
4147 /* Make sure we have a sym or com block, and verify that it can
4148 be bind(c). Set the appropriate field(s) and look for more
4149 identifiers. */
4150 if (tmp_sym != NULL || com_block != NULL)
4152 if (tmp_sym != NULL)
4154 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
4155 return false;
4157 else
4159 if (!set_verify_bind_c_com_block (com_block, num_idents))
4160 return false;
4163 /* Look to see if we have another identifier. */
4164 tmp_sym = NULL;
4165 if (gfc_match_eos () == MATCH_YES)
4166 found_id = MATCH_NO;
4167 else if (gfc_match_char (',') != MATCH_YES)
4168 found_id = MATCH_NO;
4169 else if (gfc_match_name (name) == MATCH_YES)
4171 found_id = MATCH_YES;
4172 gfc_get_ha_symbol (name, &tmp_sym);
4174 else if (match_common_name (name) == MATCH_YES)
4176 found_id = MATCH_YES;
4177 com_block = gfc_get_common (name, 0);
4179 else
4181 gfc_error ("Missing entity or common block name for "
4182 "attribute specification statement at %C");
4183 return false;
4186 else
4188 gfc_internal_error ("Missing symbol");
4190 } while (found_id == MATCH_YES);
4192 /* if we get here we were successful */
4193 return true;
4197 /* Try and match a BIND(C) attribute specification statement. */
4199 match
4200 gfc_match_bind_c_stmt (void)
4202 match found_match = MATCH_NO;
4203 gfc_typespec *ts;
4205 ts = &current_ts;
4207 /* This may not be necessary. */
4208 gfc_clear_ts (ts);
4209 /* Clear the temporary binding label holder. */
4210 curr_binding_label = NULL;
4212 /* Look for the bind(c). */
4213 found_match = gfc_match_bind_c (NULL, true);
4215 if (found_match == MATCH_YES)
4217 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
4218 return MATCH_ERROR;
4220 /* Look for the :: now, but it is not required. */
4221 gfc_match (" :: ");
4223 /* Get the identifier(s) that needs to be updated. This may need to
4224 change to hand the flag(s) for the attr specified so all identifiers
4225 found can have all appropriate parts updated (assuming that the same
4226 spec stmt can have multiple attrs, such as both bind(c) and
4227 allocatable...). */
4228 if (!get_bind_c_idents ())
4229 /* Error message should have printed already. */
4230 return MATCH_ERROR;
4233 return found_match;
4237 /* Match a data declaration statement. */
4239 match
4240 gfc_match_data_decl (void)
4242 gfc_symbol *sym;
4243 match m;
4244 int elem;
4246 num_idents_on_line = 0;
4248 m = gfc_match_decl_type_spec (&current_ts, 0);
4249 if (m != MATCH_YES)
4250 return m;
4252 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4253 && gfc_current_state () != COMP_DERIVED)
4255 sym = gfc_use_derived (current_ts.u.derived);
4257 if (sym == NULL)
4259 m = MATCH_ERROR;
4260 goto cleanup;
4263 current_ts.u.derived = sym;
4266 m = match_attr_spec ();
4267 if (m == MATCH_ERROR)
4269 m = MATCH_NO;
4270 goto cleanup;
4273 if (current_ts.type == BT_CLASS
4274 && current_ts.u.derived->attr.unlimited_polymorphic)
4275 goto ok;
4277 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4278 && current_ts.u.derived->components == NULL
4279 && !current_ts.u.derived->attr.zero_comp)
4282 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4283 goto ok;
4285 gfc_find_symbol (current_ts.u.derived->name,
4286 current_ts.u.derived->ns, 1, &sym);
4288 /* Any symbol that we find had better be a type definition
4289 which has its components defined. */
4290 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4291 && (current_ts.u.derived->components != NULL
4292 || current_ts.u.derived->attr.zero_comp))
4293 goto ok;
4295 gfc_error ("Derived type at %C has not been previously defined "
4296 "and so cannot appear in a derived type definition");
4297 m = MATCH_ERROR;
4298 goto cleanup;
4302 /* If we have an old-style character declaration, and no new-style
4303 attribute specifications, then there a comma is optional between
4304 the type specification and the variable list. */
4305 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4306 gfc_match_char (',');
4308 /* Give the types/attributes to symbols that follow. Give the element
4309 a number so that repeat character length expressions can be copied. */
4310 elem = 1;
4311 for (;;)
4313 num_idents_on_line++;
4314 m = variable_decl (elem++);
4315 if (m == MATCH_ERROR)
4316 goto cleanup;
4317 if (m == MATCH_NO)
4318 break;
4320 if (gfc_match_eos () == MATCH_YES)
4321 goto cleanup;
4322 if (gfc_match_char (',') != MATCH_YES)
4323 break;
4326 if (gfc_error_flag_test () == 0)
4327 gfc_error ("Syntax error in data declaration at %C");
4328 m = MATCH_ERROR;
4330 gfc_free_data_all (gfc_current_ns);
4332 cleanup:
4333 gfc_free_array_spec (current_as);
4334 current_as = NULL;
4335 return m;
4339 /* Match a prefix associated with a function or subroutine
4340 declaration. If the typespec pointer is nonnull, then a typespec
4341 can be matched. Note that if nothing matches, MATCH_YES is
4342 returned (the null string was matched). */
4344 match
4345 gfc_match_prefix (gfc_typespec *ts)
4347 bool seen_type;
4348 bool seen_impure;
4349 bool found_prefix;
4351 gfc_clear_attr (&current_attr);
4352 seen_type = false;
4353 seen_impure = false;
4355 gcc_assert (!gfc_matching_prefix);
4356 gfc_matching_prefix = true;
4360 found_prefix = false;
4362 if (!seen_type && ts != NULL
4363 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4364 && gfc_match_space () == MATCH_YES)
4367 seen_type = true;
4368 found_prefix = true;
4371 if (gfc_match ("elemental% ") == MATCH_YES)
4373 if (!gfc_add_elemental (&current_attr, NULL))
4374 goto error;
4376 found_prefix = true;
4379 if (gfc_match ("pure% ") == MATCH_YES)
4381 if (!gfc_add_pure (&current_attr, NULL))
4382 goto error;
4384 found_prefix = true;
4387 if (gfc_match ("recursive% ") == MATCH_YES)
4389 if (!gfc_add_recursive (&current_attr, NULL))
4390 goto error;
4392 found_prefix = true;
4395 /* IMPURE is a somewhat special case, as it needs not set an actual
4396 attribute but rather only prevents ELEMENTAL routines from being
4397 automatically PURE. */
4398 if (gfc_match ("impure% ") == MATCH_YES)
4400 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
4401 goto error;
4403 seen_impure = true;
4404 found_prefix = true;
4407 while (found_prefix);
4409 /* IMPURE and PURE must not both appear, of course. */
4410 if (seen_impure && current_attr.pure)
4412 gfc_error ("PURE and IMPURE must not appear both at %C");
4413 goto error;
4416 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4417 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4419 if (!gfc_add_pure (&current_attr, NULL))
4420 goto error;
4423 /* At this point, the next item is not a prefix. */
4424 gcc_assert (gfc_matching_prefix);
4425 gfc_matching_prefix = false;
4426 return MATCH_YES;
4428 error:
4429 gcc_assert (gfc_matching_prefix);
4430 gfc_matching_prefix = false;
4431 return MATCH_ERROR;
4435 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4437 static bool
4438 copy_prefix (symbol_attribute *dest, locus *where)
4440 if (current_attr.pure && !gfc_add_pure (dest, where))
4441 return false;
4443 if (current_attr.elemental && !gfc_add_elemental (dest, where))
4444 return false;
4446 if (current_attr.recursive && !gfc_add_recursive (dest, where))
4447 return false;
4449 return true;
4453 /* Match a formal argument list. */
4455 match
4456 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4458 gfc_formal_arglist *head, *tail, *p, *q;
4459 char name[GFC_MAX_SYMBOL_LEN + 1];
4460 gfc_symbol *sym;
4461 match m;
4463 head = tail = NULL;
4465 if (gfc_match_char ('(') != MATCH_YES)
4467 if (null_flag)
4468 goto ok;
4469 return MATCH_NO;
4472 if (gfc_match_char (')') == MATCH_YES)
4473 goto ok;
4475 for (;;)
4477 if (gfc_match_char ('*') == MATCH_YES)
4479 sym = NULL;
4480 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
4481 "at %C"))
4483 m = MATCH_ERROR;
4484 goto cleanup;
4487 else
4489 m = gfc_match_name (name);
4490 if (m != MATCH_YES)
4491 goto cleanup;
4493 if (gfc_get_symbol (name, NULL, &sym))
4494 goto cleanup;
4497 p = gfc_get_formal_arglist ();
4499 if (head == NULL)
4500 head = tail = p;
4501 else
4503 tail->next = p;
4504 tail = p;
4507 tail->sym = sym;
4509 /* We don't add the VARIABLE flavor because the name could be a
4510 dummy procedure. We don't apply these attributes to formal
4511 arguments of statement functions. */
4512 if (sym != NULL && !st_flag
4513 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
4514 || !gfc_missing_attr (&sym->attr, NULL)))
4516 m = MATCH_ERROR;
4517 goto cleanup;
4520 /* The name of a program unit can be in a different namespace,
4521 so check for it explicitly. After the statement is accepted,
4522 the name is checked for especially in gfc_get_symbol(). */
4523 if (gfc_new_block != NULL && sym != NULL
4524 && strcmp (sym->name, gfc_new_block->name) == 0)
4526 gfc_error ("Name '%s' at %C is the name of the procedure",
4527 sym->name);
4528 m = MATCH_ERROR;
4529 goto cleanup;
4532 if (gfc_match_char (')') == MATCH_YES)
4533 goto ok;
4535 m = gfc_match_char (',');
4536 if (m != MATCH_YES)
4538 gfc_error ("Unexpected junk in formal argument list at %C");
4539 goto cleanup;
4544 /* Check for duplicate symbols in the formal argument list. */
4545 if (head != NULL)
4547 for (p = head; p->next; p = p->next)
4549 if (p->sym == NULL)
4550 continue;
4552 for (q = p->next; q; q = q->next)
4553 if (p->sym == q->sym)
4555 gfc_error ("Duplicate symbol '%s' in formal argument list "
4556 "at %C", p->sym->name);
4558 m = MATCH_ERROR;
4559 goto cleanup;
4564 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
4566 m = MATCH_ERROR;
4567 goto cleanup;
4570 return MATCH_YES;
4572 cleanup:
4573 gfc_free_formal_arglist (head);
4574 return m;
4578 /* Match a RESULT specification following a function declaration or
4579 ENTRY statement. Also matches the end-of-statement. */
4581 static match
4582 match_result (gfc_symbol *function, gfc_symbol **result)
4584 char name[GFC_MAX_SYMBOL_LEN + 1];
4585 gfc_symbol *r;
4586 match m;
4588 if (gfc_match (" result (") != MATCH_YES)
4589 return MATCH_NO;
4591 m = gfc_match_name (name);
4592 if (m != MATCH_YES)
4593 return m;
4595 /* Get the right paren, and that's it because there could be the
4596 bind(c) attribute after the result clause. */
4597 if (gfc_match_char (')') != MATCH_YES)
4599 /* TODO: should report the missing right paren here. */
4600 return MATCH_ERROR;
4603 if (strcmp (function->name, name) == 0)
4605 gfc_error ("RESULT variable at %C must be different than function name");
4606 return MATCH_ERROR;
4609 if (gfc_get_symbol (name, NULL, &r))
4610 return MATCH_ERROR;
4612 if (!gfc_add_result (&r->attr, r->name, NULL))
4613 return MATCH_ERROR;
4615 *result = r;
4617 return MATCH_YES;
4621 /* Match a function suffix, which could be a combination of a result
4622 clause and BIND(C), either one, or neither. The draft does not
4623 require them to come in a specific order. */
4625 match
4626 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4628 match is_bind_c; /* Found bind(c). */
4629 match is_result; /* Found result clause. */
4630 match found_match; /* Status of whether we've found a good match. */
4631 char peek_char; /* Character we're going to peek at. */
4632 bool allow_binding_name;
4634 /* Initialize to having found nothing. */
4635 found_match = MATCH_NO;
4636 is_bind_c = MATCH_NO;
4637 is_result = MATCH_NO;
4639 /* Get the next char to narrow between result and bind(c). */
4640 gfc_gobble_whitespace ();
4641 peek_char = gfc_peek_ascii_char ();
4643 /* C binding names are not allowed for internal procedures. */
4644 if (gfc_current_state () == COMP_CONTAINS
4645 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4646 allow_binding_name = false;
4647 else
4648 allow_binding_name = true;
4650 switch (peek_char)
4652 case 'r':
4653 /* Look for result clause. */
4654 is_result = match_result (sym, result);
4655 if (is_result == MATCH_YES)
4657 /* Now see if there is a bind(c) after it. */
4658 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4659 /* We've found the result clause and possibly bind(c). */
4660 found_match = MATCH_YES;
4662 else
4663 /* This should only be MATCH_ERROR. */
4664 found_match = is_result;
4665 break;
4666 case 'b':
4667 /* Look for bind(c) first. */
4668 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4669 if (is_bind_c == MATCH_YES)
4671 /* Now see if a result clause followed it. */
4672 is_result = match_result (sym, result);
4673 found_match = MATCH_YES;
4675 else
4677 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4678 found_match = MATCH_ERROR;
4680 break;
4681 default:
4682 gfc_error ("Unexpected junk after function declaration at %C");
4683 found_match = MATCH_ERROR;
4684 break;
4687 if (is_bind_c == MATCH_YES)
4689 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4690 if (gfc_current_state () == COMP_CONTAINS
4691 && sym->ns->proc_name->attr.flavor != FL_MODULE
4692 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
4693 "at %L may not be specified for an internal "
4694 "procedure", &gfc_current_locus))
4695 return MATCH_ERROR;
4697 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
4698 return MATCH_ERROR;
4701 return found_match;
4705 /* Procedure pointer return value without RESULT statement:
4706 Add "hidden" result variable named "ppr@". */
4708 static bool
4709 add_hidden_procptr_result (gfc_symbol *sym)
4711 bool case1,case2;
4713 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4714 return false;
4716 /* First usage case: PROCEDURE and EXTERNAL statements. */
4717 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4718 && strcmp (gfc_current_block ()->name, sym->name) == 0
4719 && sym->attr.external;
4720 /* Second usage case: INTERFACE statements. */
4721 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4722 && gfc_state_stack->previous->state == COMP_FUNCTION
4723 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4725 if (case1 || case2)
4727 gfc_symtree *stree;
4728 if (case1)
4729 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4730 else if (case2)
4732 gfc_symtree *st2;
4733 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4734 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4735 st2->n.sym = stree->n.sym;
4737 sym->result = stree->n.sym;
4739 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4740 sym->result->attr.pointer = sym->attr.pointer;
4741 sym->result->attr.external = sym->attr.external;
4742 sym->result->attr.referenced = sym->attr.referenced;
4743 sym->result->ts = sym->ts;
4744 sym->attr.proc_pointer = 0;
4745 sym->attr.pointer = 0;
4746 sym->attr.external = 0;
4747 if (sym->result->attr.external && sym->result->attr.pointer)
4749 sym->result->attr.pointer = 0;
4750 sym->result->attr.proc_pointer = 1;
4753 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4755 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4756 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4757 && sym->result && sym->result != sym && sym->result->attr.external
4758 && sym == gfc_current_ns->proc_name
4759 && sym == sym->result->ns->proc_name
4760 && strcmp ("ppr@", sym->result->name) == 0)
4762 sym->result->attr.proc_pointer = 1;
4763 sym->attr.pointer = 0;
4764 return true;
4766 else
4767 return false;
4771 /* Match the interface for a PROCEDURE declaration,
4772 including brackets (R1212). */
4774 static match
4775 match_procedure_interface (gfc_symbol **proc_if)
4777 match m;
4778 gfc_symtree *st;
4779 locus old_loc, entry_loc;
4780 gfc_namespace *old_ns = gfc_current_ns;
4781 char name[GFC_MAX_SYMBOL_LEN + 1];
4783 old_loc = entry_loc = gfc_current_locus;
4784 gfc_clear_ts (&current_ts);
4786 if (gfc_match (" (") != MATCH_YES)
4788 gfc_current_locus = entry_loc;
4789 return MATCH_NO;
4792 /* Get the type spec. for the procedure interface. */
4793 old_loc = gfc_current_locus;
4794 m = gfc_match_decl_type_spec (&current_ts, 0);
4795 gfc_gobble_whitespace ();
4796 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4797 goto got_ts;
4799 if (m == MATCH_ERROR)
4800 return m;
4802 /* Procedure interface is itself a procedure. */
4803 gfc_current_locus = old_loc;
4804 m = gfc_match_name (name);
4806 /* First look to see if it is already accessible in the current
4807 namespace because it is use associated or contained. */
4808 st = NULL;
4809 if (gfc_find_sym_tree (name, NULL, 0, &st))
4810 return MATCH_ERROR;
4812 /* If it is still not found, then try the parent namespace, if it
4813 exists and create the symbol there if it is still not found. */
4814 if (gfc_current_ns->parent)
4815 gfc_current_ns = gfc_current_ns->parent;
4816 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4817 return MATCH_ERROR;
4819 gfc_current_ns = old_ns;
4820 *proc_if = st->n.sym;
4822 if (*proc_if)
4824 (*proc_if)->refs++;
4825 /* Resolve interface if possible. That way, attr.procedure is only set
4826 if it is declared by a later procedure-declaration-stmt, which is
4827 invalid per F08:C1216 (cf. resolve_procedure_interface). */
4828 while ((*proc_if)->ts.interface)
4829 *proc_if = (*proc_if)->ts.interface;
4831 if ((*proc_if)->attr.flavor == FL_UNKNOWN
4832 && (*proc_if)->ts.type == BT_UNKNOWN
4833 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
4834 (*proc_if)->name, NULL))
4835 return MATCH_ERROR;
4838 got_ts:
4839 if (gfc_match (" )") != MATCH_YES)
4841 gfc_current_locus = entry_loc;
4842 return MATCH_NO;
4845 return MATCH_YES;
4849 /* Match a PROCEDURE declaration (R1211). */
4851 static match
4852 match_procedure_decl (void)
4854 match m;
4855 gfc_symbol *sym, *proc_if = NULL;
4856 int num;
4857 gfc_expr *initializer = NULL;
4859 /* Parse interface (with brackets). */
4860 m = match_procedure_interface (&proc_if);
4861 if (m != MATCH_YES)
4862 return m;
4864 /* Parse attributes (with colons). */
4865 m = match_attr_spec();
4866 if (m == MATCH_ERROR)
4867 return MATCH_ERROR;
4869 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
4871 current_attr.is_bind_c = 1;
4872 has_name_equals = 0;
4873 curr_binding_label = NULL;
4876 /* Get procedure symbols. */
4877 for(num=1;;num++)
4879 m = gfc_match_symbol (&sym, 0);
4880 if (m == MATCH_NO)
4881 goto syntax;
4882 else if (m == MATCH_ERROR)
4883 return m;
4885 /* Add current_attr to the symbol attributes. */
4886 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
4887 return MATCH_ERROR;
4889 if (sym->attr.is_bind_c)
4891 /* Check for C1218. */
4892 if (!proc_if || !proc_if->attr.is_bind_c)
4894 gfc_error ("BIND(C) attribute at %C requires "
4895 "an interface with BIND(C)");
4896 return MATCH_ERROR;
4898 /* Check for C1217. */
4899 if (has_name_equals && sym->attr.pointer)
4901 gfc_error ("BIND(C) procedure with NAME may not have "
4902 "POINTER attribute at %C");
4903 return MATCH_ERROR;
4905 if (has_name_equals && sym->attr.dummy)
4907 gfc_error ("Dummy procedure at %C may not have "
4908 "BIND(C) attribute with NAME");
4909 return MATCH_ERROR;
4911 /* Set binding label for BIND(C). */
4912 if (!set_binding_label (&sym->binding_label, sym->name, num))
4913 return MATCH_ERROR;
4916 if (!gfc_add_external (&sym->attr, NULL))
4917 return MATCH_ERROR;
4919 if (add_hidden_procptr_result (sym))
4920 sym = sym->result;
4922 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
4923 return MATCH_ERROR;
4925 /* Set interface. */
4926 if (proc_if != NULL)
4928 if (sym->ts.type != BT_UNKNOWN)
4930 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4931 sym->name, &gfc_current_locus,
4932 gfc_basic_typename (sym->ts.type));
4933 return MATCH_ERROR;
4935 sym->ts.interface = proc_if;
4936 sym->attr.untyped = 1;
4937 sym->attr.if_source = IFSRC_IFBODY;
4939 else if (current_ts.type != BT_UNKNOWN)
4941 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
4942 return MATCH_ERROR;
4943 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4944 sym->ts.interface->ts = current_ts;
4945 sym->ts.interface->attr.flavor = FL_PROCEDURE;
4946 sym->ts.interface->attr.function = 1;
4947 sym->attr.function = 1;
4948 sym->attr.if_source = IFSRC_UNKNOWN;
4951 if (gfc_match (" =>") == MATCH_YES)
4953 if (!current_attr.pointer)
4955 gfc_error ("Initialization at %C isn't for a pointer variable");
4956 m = MATCH_ERROR;
4957 goto cleanup;
4960 m = match_pointer_init (&initializer, 1);
4961 if (m != MATCH_YES)
4962 goto cleanup;
4964 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
4965 goto cleanup;
4969 if (gfc_match_eos () == MATCH_YES)
4970 return MATCH_YES;
4971 if (gfc_match_char (',') != MATCH_YES)
4972 goto syntax;
4975 syntax:
4976 gfc_error ("Syntax error in PROCEDURE statement at %C");
4977 return MATCH_ERROR;
4979 cleanup:
4980 /* Free stuff up and return. */
4981 gfc_free_expr (initializer);
4982 return m;
4986 static match
4987 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4990 /* Match a procedure pointer component declaration (R445). */
4992 static match
4993 match_ppc_decl (void)
4995 match m;
4996 gfc_symbol *proc_if = NULL;
4997 gfc_typespec ts;
4998 int num;
4999 gfc_component *c;
5000 gfc_expr *initializer = NULL;
5001 gfc_typebound_proc* tb;
5002 char name[GFC_MAX_SYMBOL_LEN + 1];
5004 /* Parse interface (with brackets). */
5005 m = match_procedure_interface (&proc_if);
5006 if (m != MATCH_YES)
5007 goto syntax;
5009 /* Parse attributes. */
5010 tb = XCNEW (gfc_typebound_proc);
5011 tb->where = gfc_current_locus;
5012 m = match_binding_attributes (tb, false, true);
5013 if (m == MATCH_ERROR)
5014 return m;
5016 gfc_clear_attr (&current_attr);
5017 current_attr.procedure = 1;
5018 current_attr.proc_pointer = 1;
5019 current_attr.access = tb->access;
5020 current_attr.flavor = FL_PROCEDURE;
5022 /* Match the colons (required). */
5023 if (gfc_match (" ::") != MATCH_YES)
5025 gfc_error ("Expected '::' after binding-attributes at %C");
5026 return MATCH_ERROR;
5029 /* Check for C450. */
5030 if (!tb->nopass && proc_if == NULL)
5032 gfc_error("NOPASS or explicit interface required at %C");
5033 return MATCH_ERROR;
5036 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
5037 return MATCH_ERROR;
5039 /* Match PPC names. */
5040 ts = current_ts;
5041 for(num=1;;num++)
5043 m = gfc_match_name (name);
5044 if (m == MATCH_NO)
5045 goto syntax;
5046 else if (m == MATCH_ERROR)
5047 return m;
5049 if (!gfc_add_component (gfc_current_block(), name, &c))
5050 return MATCH_ERROR;
5052 /* Add current_attr to the symbol attributes. */
5053 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
5054 return MATCH_ERROR;
5056 if (!gfc_add_external (&c->attr, NULL))
5057 return MATCH_ERROR;
5059 if (!gfc_add_proc (&c->attr, name, NULL))
5060 return MATCH_ERROR;
5062 if (num == 1)
5063 c->tb = tb;
5064 else
5066 c->tb = XCNEW (gfc_typebound_proc);
5067 c->tb->where = gfc_current_locus;
5068 *c->tb = *tb;
5071 /* Set interface. */
5072 if (proc_if != NULL)
5074 c->ts.interface = proc_if;
5075 c->attr.untyped = 1;
5076 c->attr.if_source = IFSRC_IFBODY;
5078 else if (ts.type != BT_UNKNOWN)
5080 c->ts = ts;
5081 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5082 c->ts.interface->result = c->ts.interface;
5083 c->ts.interface->ts = ts;
5084 c->ts.interface->attr.flavor = FL_PROCEDURE;
5085 c->ts.interface->attr.function = 1;
5086 c->attr.function = 1;
5087 c->attr.if_source = IFSRC_UNKNOWN;
5090 if (gfc_match (" =>") == MATCH_YES)
5092 m = match_pointer_init (&initializer, 1);
5093 if (m != MATCH_YES)
5095 gfc_free_expr (initializer);
5096 return m;
5098 c->initializer = initializer;
5101 if (gfc_match_eos () == MATCH_YES)
5102 return MATCH_YES;
5103 if (gfc_match_char (',') != MATCH_YES)
5104 goto syntax;
5107 syntax:
5108 gfc_error ("Syntax error in procedure pointer component at %C");
5109 return MATCH_ERROR;
5113 /* Match a PROCEDURE declaration inside an interface (R1206). */
5115 static match
5116 match_procedure_in_interface (void)
5118 match m;
5119 gfc_symbol *sym;
5120 char name[GFC_MAX_SYMBOL_LEN + 1];
5121 locus old_locus;
5123 if (current_interface.type == INTERFACE_NAMELESS
5124 || current_interface.type == INTERFACE_ABSTRACT)
5126 gfc_error ("PROCEDURE at %C must be in a generic interface");
5127 return MATCH_ERROR;
5130 /* Check if the F2008 optional double colon appears. */
5131 gfc_gobble_whitespace ();
5132 old_locus = gfc_current_locus;
5133 if (gfc_match ("::") == MATCH_YES)
5135 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
5136 "MODULE PROCEDURE statement at %L", &old_locus))
5137 return MATCH_ERROR;
5139 else
5140 gfc_current_locus = old_locus;
5142 for(;;)
5144 m = gfc_match_name (name);
5145 if (m == MATCH_NO)
5146 goto syntax;
5147 else if (m == MATCH_ERROR)
5148 return m;
5149 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5150 return MATCH_ERROR;
5152 if (!gfc_add_interface (sym))
5153 return MATCH_ERROR;
5155 if (gfc_match_eos () == MATCH_YES)
5156 break;
5157 if (gfc_match_char (',') != MATCH_YES)
5158 goto syntax;
5161 return MATCH_YES;
5163 syntax:
5164 gfc_error ("Syntax error in PROCEDURE statement at %C");
5165 return MATCH_ERROR;
5169 /* General matcher for PROCEDURE declarations. */
5171 static match match_procedure_in_type (void);
5173 match
5174 gfc_match_procedure (void)
5176 match m;
5178 switch (gfc_current_state ())
5180 case COMP_NONE:
5181 case COMP_PROGRAM:
5182 case COMP_MODULE:
5183 case COMP_SUBROUTINE:
5184 case COMP_FUNCTION:
5185 case COMP_BLOCK:
5186 m = match_procedure_decl ();
5187 break;
5188 case COMP_INTERFACE:
5189 m = match_procedure_in_interface ();
5190 break;
5191 case COMP_DERIVED:
5192 m = match_ppc_decl ();
5193 break;
5194 case COMP_DERIVED_CONTAINS:
5195 m = match_procedure_in_type ();
5196 break;
5197 default:
5198 return MATCH_NO;
5201 if (m != MATCH_YES)
5202 return m;
5204 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
5205 return MATCH_ERROR;
5207 return m;
5211 /* Warn if a matched procedure has the same name as an intrinsic; this is
5212 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5213 parser-state-stack to find out whether we're in a module. */
5215 static void
5216 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5218 bool in_module;
5220 in_module = (gfc_state_stack->previous
5221 && gfc_state_stack->previous->state == COMP_MODULE);
5223 gfc_warn_intrinsic_shadow (sym, in_module, func);
5227 /* Match a function declaration. */
5229 match
5230 gfc_match_function_decl (void)
5232 char name[GFC_MAX_SYMBOL_LEN + 1];
5233 gfc_symbol *sym, *result;
5234 locus old_loc;
5235 match m;
5236 match suffix_match;
5237 match found_match; /* Status returned by match func. */
5239 if (gfc_current_state () != COMP_NONE
5240 && gfc_current_state () != COMP_INTERFACE
5241 && gfc_current_state () != COMP_CONTAINS)
5242 return MATCH_NO;
5244 gfc_clear_ts (&current_ts);
5246 old_loc = gfc_current_locus;
5248 m = gfc_match_prefix (&current_ts);
5249 if (m != MATCH_YES)
5251 gfc_current_locus = old_loc;
5252 return m;
5255 if (gfc_match ("function% %n", name) != MATCH_YES)
5257 gfc_current_locus = old_loc;
5258 return MATCH_NO;
5260 if (get_proc_name (name, &sym, false))
5261 return MATCH_ERROR;
5263 if (add_hidden_procptr_result (sym))
5264 sym = sym->result;
5266 gfc_new_block = sym;
5268 m = gfc_match_formal_arglist (sym, 0, 0);
5269 if (m == MATCH_NO)
5271 gfc_error ("Expected formal argument list in function "
5272 "definition at %C");
5273 m = MATCH_ERROR;
5274 goto cleanup;
5276 else if (m == MATCH_ERROR)
5277 goto cleanup;
5279 result = NULL;
5281 /* According to the draft, the bind(c) and result clause can
5282 come in either order after the formal_arg_list (i.e., either
5283 can be first, both can exist together or by themselves or neither
5284 one). Therefore, the match_result can't match the end of the
5285 string, and check for the bind(c) or result clause in either order. */
5286 found_match = gfc_match_eos ();
5288 /* Make sure that it isn't already declared as BIND(C). If it is, it
5289 must have been marked BIND(C) with a BIND(C) attribute and that is
5290 not allowed for procedures. */
5291 if (sym->attr.is_bind_c == 1)
5293 sym->attr.is_bind_c = 0;
5294 if (sym->old_symbol != NULL)
5295 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5296 "variables or common blocks",
5297 &(sym->old_symbol->declared_at));
5298 else
5299 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5300 "variables or common blocks", &gfc_current_locus);
5303 if (found_match != MATCH_YES)
5305 /* If we haven't found the end-of-statement, look for a suffix. */
5306 suffix_match = gfc_match_suffix (sym, &result);
5307 if (suffix_match == MATCH_YES)
5308 /* Need to get the eos now. */
5309 found_match = gfc_match_eos ();
5310 else
5311 found_match = suffix_match;
5314 if(found_match != MATCH_YES)
5315 m = MATCH_ERROR;
5316 else
5318 /* Make changes to the symbol. */
5319 m = MATCH_ERROR;
5321 if (!gfc_add_function (&sym->attr, sym->name, NULL))
5322 goto cleanup;
5324 if (!gfc_missing_attr (&sym->attr, NULL)
5325 || !copy_prefix (&sym->attr, &sym->declared_at))
5326 goto cleanup;
5328 /* Delay matching the function characteristics until after the
5329 specification block by signalling kind=-1. */
5330 sym->declared_at = old_loc;
5331 if (current_ts.type != BT_UNKNOWN)
5332 current_ts.kind = -1;
5333 else
5334 current_ts.kind = 0;
5336 if (result == NULL)
5338 if (current_ts.type != BT_UNKNOWN
5339 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
5340 goto cleanup;
5341 sym->result = sym;
5343 else
5345 if (current_ts.type != BT_UNKNOWN
5346 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
5347 goto cleanup;
5348 sym->result = result;
5351 /* Warn if this procedure has the same name as an intrinsic. */
5352 warn_intrinsic_shadow (sym, true);
5354 return MATCH_YES;
5357 cleanup:
5358 gfc_current_locus = old_loc;
5359 return m;
5363 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5364 pass the name of the entry, rather than the gfc_current_block name, and
5365 to return false upon finding an existing global entry. */
5367 static bool
5368 add_global_entry (const char *name, const char *binding_label, bool sub,
5369 locus *where)
5371 gfc_gsymbol *s;
5372 enum gfc_symbol_type type;
5374 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5376 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5377 name is a global identifier. */
5378 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
5380 s = gfc_get_gsymbol (name);
5382 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5384 gfc_global_used (s, where);
5385 return false;
5387 else
5389 s->type = type;
5390 s->sym_name = name;
5391 s->where = *where;
5392 s->defined = 1;
5393 s->ns = gfc_current_ns;
5397 /* Don't add the symbol multiple times. */
5398 if (binding_label
5399 && (!gfc_notification_std (GFC_STD_F2008)
5400 || strcmp (name, binding_label) != 0))
5402 s = gfc_get_gsymbol (binding_label);
5404 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5406 gfc_global_used (s, where);
5407 return false;
5409 else
5411 s->type = type;
5412 s->sym_name = name;
5413 s->binding_label = binding_label;
5414 s->where = *where;
5415 s->defined = 1;
5416 s->ns = gfc_current_ns;
5420 return true;
5424 /* Match an ENTRY statement. */
5426 match
5427 gfc_match_entry (void)
5429 gfc_symbol *proc;
5430 gfc_symbol *result;
5431 gfc_symbol *entry;
5432 char name[GFC_MAX_SYMBOL_LEN + 1];
5433 gfc_compile_state state;
5434 match m;
5435 gfc_entry_list *el;
5436 locus old_loc;
5437 bool module_procedure;
5438 char peek_char;
5439 match is_bind_c;
5441 m = gfc_match_name (name);
5442 if (m != MATCH_YES)
5443 return m;
5445 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
5446 return MATCH_ERROR;
5448 state = gfc_current_state ();
5449 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5451 switch (state)
5453 case COMP_PROGRAM:
5454 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5455 break;
5456 case COMP_MODULE:
5457 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5458 break;
5459 case COMP_BLOCK_DATA:
5460 gfc_error ("ENTRY statement at %C cannot appear within "
5461 "a BLOCK DATA");
5462 break;
5463 case COMP_INTERFACE:
5464 gfc_error ("ENTRY statement at %C cannot appear within "
5465 "an INTERFACE");
5466 break;
5467 case COMP_DERIVED:
5468 gfc_error ("ENTRY statement at %C cannot appear within "
5469 "a DERIVED TYPE block");
5470 break;
5471 case COMP_IF:
5472 gfc_error ("ENTRY statement at %C cannot appear within "
5473 "an IF-THEN block");
5474 break;
5475 case COMP_DO:
5476 case COMP_DO_CONCURRENT:
5477 gfc_error ("ENTRY statement at %C cannot appear within "
5478 "a DO block");
5479 break;
5480 case COMP_SELECT:
5481 gfc_error ("ENTRY statement at %C cannot appear within "
5482 "a SELECT block");
5483 break;
5484 case COMP_FORALL:
5485 gfc_error ("ENTRY statement at %C cannot appear within "
5486 "a FORALL block");
5487 break;
5488 case COMP_WHERE:
5489 gfc_error ("ENTRY statement at %C cannot appear within "
5490 "a WHERE block");
5491 break;
5492 case COMP_CONTAINS:
5493 gfc_error ("ENTRY statement at %C cannot appear within "
5494 "a contained subprogram");
5495 break;
5496 default:
5497 gfc_internal_error ("gfc_match_entry(): Bad state");
5499 return MATCH_ERROR;
5502 module_procedure = gfc_current_ns->parent != NULL
5503 && gfc_current_ns->parent->proc_name
5504 && gfc_current_ns->parent->proc_name->attr.flavor
5505 == FL_MODULE;
5507 if (gfc_current_ns->parent != NULL
5508 && gfc_current_ns->parent->proc_name
5509 && !module_procedure)
5511 gfc_error("ENTRY statement at %C cannot appear in a "
5512 "contained procedure");
5513 return MATCH_ERROR;
5516 /* Module function entries need special care in get_proc_name
5517 because previous references within the function will have
5518 created symbols attached to the current namespace. */
5519 if (get_proc_name (name, &entry,
5520 gfc_current_ns->parent != NULL
5521 && module_procedure))
5522 return MATCH_ERROR;
5524 proc = gfc_current_block ();
5526 /* Make sure that it isn't already declared as BIND(C). If it is, it
5527 must have been marked BIND(C) with a BIND(C) attribute and that is
5528 not allowed for procedures. */
5529 if (entry->attr.is_bind_c == 1)
5531 entry->attr.is_bind_c = 0;
5532 if (entry->old_symbol != NULL)
5533 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5534 "variables or common blocks",
5535 &(entry->old_symbol->declared_at));
5536 else
5537 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5538 "variables or common blocks", &gfc_current_locus);
5541 /* Check what next non-whitespace character is so we can tell if there
5542 is the required parens if we have a BIND(C). */
5543 old_loc = gfc_current_locus;
5544 gfc_gobble_whitespace ();
5545 peek_char = gfc_peek_ascii_char ();
5547 if (state == COMP_SUBROUTINE)
5549 m = gfc_match_formal_arglist (entry, 0, 1);
5550 if (m != MATCH_YES)
5551 return MATCH_ERROR;
5553 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5554 never be an internal procedure. */
5555 is_bind_c = gfc_match_bind_c (entry, true);
5556 if (is_bind_c == MATCH_ERROR)
5557 return MATCH_ERROR;
5558 if (is_bind_c == MATCH_YES)
5560 if (peek_char != '(')
5562 gfc_error ("Missing required parentheses before BIND(C) at %C");
5563 return MATCH_ERROR;
5565 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
5566 &(entry->declared_at), 1))
5567 return MATCH_ERROR;
5570 if (!gfc_current_ns->parent
5571 && !add_global_entry (name, entry->binding_label, true,
5572 &old_loc))
5573 return MATCH_ERROR;
5575 /* An entry in a subroutine. */
5576 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5577 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
5578 return MATCH_ERROR;
5580 else
5582 /* An entry in a function.
5583 We need to take special care because writing
5584 ENTRY f()
5586 ENTRY f
5587 is allowed, whereas
5588 ENTRY f() RESULT (r)
5589 can't be written as
5590 ENTRY f RESULT (r). */
5591 if (gfc_match_eos () == MATCH_YES)
5593 gfc_current_locus = old_loc;
5594 /* Match the empty argument list, and add the interface to
5595 the symbol. */
5596 m = gfc_match_formal_arglist (entry, 0, 1);
5598 else
5599 m = gfc_match_formal_arglist (entry, 0, 0);
5601 if (m != MATCH_YES)
5602 return MATCH_ERROR;
5604 result = NULL;
5606 if (gfc_match_eos () == MATCH_YES)
5608 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5609 || !gfc_add_function (&entry->attr, entry->name, NULL))
5610 return MATCH_ERROR;
5612 entry->result = entry;
5614 else
5616 m = gfc_match_suffix (entry, &result);
5617 if (m == MATCH_NO)
5618 gfc_syntax_error (ST_ENTRY);
5619 if (m != MATCH_YES)
5620 return MATCH_ERROR;
5622 if (result)
5624 if (!gfc_add_result (&result->attr, result->name, NULL)
5625 || !gfc_add_entry (&entry->attr, result->name, NULL)
5626 || !gfc_add_function (&entry->attr, result->name, NULL))
5627 return MATCH_ERROR;
5628 entry->result = result;
5630 else
5632 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5633 || !gfc_add_function (&entry->attr, entry->name, NULL))
5634 return MATCH_ERROR;
5635 entry->result = entry;
5639 if (!gfc_current_ns->parent
5640 && !add_global_entry (name, entry->binding_label, false,
5641 &old_loc))
5642 return MATCH_ERROR;
5645 if (gfc_match_eos () != MATCH_YES)
5647 gfc_syntax_error (ST_ENTRY);
5648 return MATCH_ERROR;
5651 entry->attr.recursive = proc->attr.recursive;
5652 entry->attr.elemental = proc->attr.elemental;
5653 entry->attr.pure = proc->attr.pure;
5655 el = gfc_get_entry_list ();
5656 el->sym = entry;
5657 el->next = gfc_current_ns->entries;
5658 gfc_current_ns->entries = el;
5659 if (el->next)
5660 el->id = el->next->id + 1;
5661 else
5662 el->id = 1;
5664 new_st.op = EXEC_ENTRY;
5665 new_st.ext.entry = el;
5667 return MATCH_YES;
5671 /* Match a subroutine statement, including optional prefixes. */
5673 match
5674 gfc_match_subroutine (void)
5676 char name[GFC_MAX_SYMBOL_LEN + 1];
5677 gfc_symbol *sym;
5678 match m;
5679 match is_bind_c;
5680 char peek_char;
5681 bool allow_binding_name;
5683 if (gfc_current_state () != COMP_NONE
5684 && gfc_current_state () != COMP_INTERFACE
5685 && gfc_current_state () != COMP_CONTAINS)
5686 return MATCH_NO;
5688 m = gfc_match_prefix (NULL);
5689 if (m != MATCH_YES)
5690 return m;
5692 m = gfc_match ("subroutine% %n", name);
5693 if (m != MATCH_YES)
5694 return m;
5696 if (get_proc_name (name, &sym, false))
5697 return MATCH_ERROR;
5699 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5700 the symbol existed before. */
5701 sym->declared_at = gfc_current_locus;
5703 if (add_hidden_procptr_result (sym))
5704 sym = sym->result;
5706 gfc_new_block = sym;
5708 /* Check what next non-whitespace character is so we can tell if there
5709 is the required parens if we have a BIND(C). */
5710 gfc_gobble_whitespace ();
5711 peek_char = gfc_peek_ascii_char ();
5713 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
5714 return MATCH_ERROR;
5716 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5717 return MATCH_ERROR;
5719 /* Make sure that it isn't already declared as BIND(C). If it is, it
5720 must have been marked BIND(C) with a BIND(C) attribute and that is
5721 not allowed for procedures. */
5722 if (sym->attr.is_bind_c == 1)
5724 sym->attr.is_bind_c = 0;
5725 if (sym->old_symbol != NULL)
5726 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5727 "variables or common blocks",
5728 &(sym->old_symbol->declared_at));
5729 else
5730 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5731 "variables or common blocks", &gfc_current_locus);
5734 /* C binding names are not allowed for internal procedures. */
5735 if (gfc_current_state () == COMP_CONTAINS
5736 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5737 allow_binding_name = false;
5738 else
5739 allow_binding_name = true;
5741 /* Here, we are just checking if it has the bind(c) attribute, and if
5742 so, then we need to make sure it's all correct. If it doesn't,
5743 we still need to continue matching the rest of the subroutine line. */
5744 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5745 if (is_bind_c == MATCH_ERROR)
5747 /* There was an attempt at the bind(c), but it was wrong. An
5748 error message should have been printed w/in the gfc_match_bind_c
5749 so here we'll just return the MATCH_ERROR. */
5750 return MATCH_ERROR;
5753 if (is_bind_c == MATCH_YES)
5755 /* The following is allowed in the Fortran 2008 draft. */
5756 if (gfc_current_state () == COMP_CONTAINS
5757 && sym->ns->proc_name->attr.flavor != FL_MODULE
5758 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
5759 "at %L may not be specified for an internal "
5760 "procedure", &gfc_current_locus))
5761 return MATCH_ERROR;
5763 if (peek_char != '(')
5765 gfc_error ("Missing required parentheses before BIND(C) at %C");
5766 return MATCH_ERROR;
5768 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
5769 &(sym->declared_at), 1))
5770 return MATCH_ERROR;
5773 if (gfc_match_eos () != MATCH_YES)
5775 gfc_syntax_error (ST_SUBROUTINE);
5776 return MATCH_ERROR;
5779 if (!copy_prefix (&sym->attr, &sym->declared_at))
5780 return MATCH_ERROR;
5782 /* Warn if it has the same name as an intrinsic. */
5783 warn_intrinsic_shadow (sym, false);
5785 return MATCH_YES;
5789 /* Check that the NAME identifier in a BIND attribute or statement
5790 is conform to C identifier rules. */
5792 match
5793 check_bind_name_identifier (char **name)
5795 char *n = *name, *p;
5797 /* Remove leading spaces. */
5798 while (*n == ' ')
5799 n++;
5801 /* On an empty string, free memory and set name to NULL. */
5802 if (*n == '\0')
5804 free (*name);
5805 *name = NULL;
5806 return MATCH_YES;
5809 /* Remove trailing spaces. */
5810 p = n + strlen(n) - 1;
5811 while (*p == ' ')
5812 *(p--) = '\0';
5814 /* Insert the identifier into the symbol table. */
5815 p = xstrdup (n);
5816 free (*name);
5817 *name = p;
5819 /* Now check that identifier is valid under C rules. */
5820 if (ISDIGIT (*p))
5822 gfc_error ("Invalid C identifier in NAME= specifier at %C");
5823 return MATCH_ERROR;
5826 for (; *p; p++)
5827 if (!(ISALNUM (*p) || *p == '_' || *p == '$'))
5829 gfc_error ("Invalid C identifier in NAME= specifier at %C");
5830 return MATCH_ERROR;
5833 return MATCH_YES;
5837 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5838 given, and set the binding label in either the given symbol (if not
5839 NULL), or in the current_ts. The symbol may be NULL because we may
5840 encounter the BIND(C) before the declaration itself. Return
5841 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5842 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5843 or MATCH_YES if the specifier was correct and the binding label and
5844 bind(c) fields were set correctly for the given symbol or the
5845 current_ts. If allow_binding_name is false, no binding name may be
5846 given. */
5848 match
5849 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5851 char *binding_label = NULL;
5852 gfc_expr *e = NULL;
5854 /* Initialize the flag that specifies whether we encountered a NAME=
5855 specifier or not. */
5856 has_name_equals = 0;
5858 /* This much we have to be able to match, in this order, if
5859 there is a bind(c) label. */
5860 if (gfc_match (" bind ( c ") != MATCH_YES)
5861 return MATCH_NO;
5863 /* Now see if there is a binding label, or if we've reached the
5864 end of the bind(c) attribute without one. */
5865 if (gfc_match_char (',') == MATCH_YES)
5867 if (gfc_match (" name = ") != MATCH_YES)
5869 gfc_error ("Syntax error in NAME= specifier for binding label "
5870 "at %C");
5871 /* should give an error message here */
5872 return MATCH_ERROR;
5875 has_name_equals = 1;
5877 if (gfc_match_init_expr (&e) != MATCH_YES)
5879 gfc_free_expr (e);
5880 return MATCH_ERROR;
5883 if (!gfc_simplify_expr(e, 0))
5885 gfc_error ("NAME= specifier at %C should be a constant expression");
5886 gfc_free_expr (e);
5887 return MATCH_ERROR;
5890 if (e->expr_type != EXPR_CONSTANT || e->ts.type != BT_CHARACTER
5891 || e->ts.kind != gfc_default_character_kind || e->rank != 0)
5893 gfc_error ("NAME= specifier at %C should be a scalar of "
5894 "default character kind");
5895 gfc_free_expr(e);
5896 return MATCH_ERROR;
5899 // Get a C string from the Fortran string constant
5900 binding_label = gfc_widechar_to_char (e->value.character.string,
5901 e->value.character.length);
5902 gfc_free_expr(e);
5904 // Check that it is valid (old gfc_match_name_C)
5905 if (check_bind_name_identifier (&binding_label) != MATCH_YES)
5906 return MATCH_ERROR;
5909 /* Get the required right paren. */
5910 if (gfc_match_char (')') != MATCH_YES)
5912 gfc_error ("Missing closing paren for binding label at %C");
5913 return MATCH_ERROR;
5916 if (has_name_equals && !allow_binding_name)
5918 gfc_error ("No binding name is allowed in BIND(C) at %C");
5919 return MATCH_ERROR;
5922 if (has_name_equals && sym != NULL && sym->attr.dummy)
5924 gfc_error ("For dummy procedure %s, no binding name is "
5925 "allowed in BIND(C) at %C", sym->name);
5926 return MATCH_ERROR;
5930 /* Save the binding label to the symbol. If sym is null, we're
5931 probably matching the typespec attributes of a declaration and
5932 haven't gotten the name yet, and therefore, no symbol yet. */
5933 if (binding_label)
5935 if (sym != NULL)
5936 sym->binding_label = binding_label;
5937 else
5938 curr_binding_label = binding_label;
5940 else if (allow_binding_name)
5942 /* No binding label, but if symbol isn't null, we
5943 can set the label for it here.
5944 If name="" or allow_binding_name is false, no C binding name is
5945 created. */
5946 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5947 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
5950 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5951 && current_interface.type == INTERFACE_ABSTRACT)
5953 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5954 return MATCH_ERROR;
5957 return MATCH_YES;
5961 /* Return nonzero if we're currently compiling a contained procedure. */
5963 static int
5964 contained_procedure (void)
5966 gfc_state_data *s = gfc_state_stack;
5968 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5969 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5970 return 1;
5972 return 0;
5975 /* Set the kind of each enumerator. The kind is selected such that it is
5976 interoperable with the corresponding C enumeration type, making
5977 sure that -fshort-enums is honored. */
5979 static void
5980 set_enum_kind(void)
5982 enumerator_history *current_history = NULL;
5983 int kind;
5984 int i;
5986 if (max_enum == NULL || enum_history == NULL)
5987 return;
5989 if (!flag_short_enums)
5990 return;
5992 i = 0;
5995 kind = gfc_integer_kinds[i++].kind;
5997 while (kind < gfc_c_int_kind
5998 && gfc_check_integer_range (max_enum->initializer->value.integer,
5999 kind) != ARITH_OK);
6001 current_history = enum_history;
6002 while (current_history != NULL)
6004 current_history->sym->ts.kind = kind;
6005 current_history = current_history->next;
6010 /* Match any of the various end-block statements. Returns the type of
6011 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6012 and END BLOCK statements cannot be replaced by a single END statement. */
6014 match
6015 gfc_match_end (gfc_statement *st)
6017 char name[GFC_MAX_SYMBOL_LEN + 1];
6018 gfc_compile_state state;
6019 locus old_loc;
6020 const char *block_name;
6021 const char *target;
6022 int eos_ok;
6023 match m;
6024 gfc_namespace *parent_ns, *ns, *prev_ns;
6025 gfc_namespace **nsp;
6027 old_loc = gfc_current_locus;
6028 if (gfc_match ("end") != MATCH_YES)
6029 return MATCH_NO;
6031 state = gfc_current_state ();
6032 block_name = gfc_current_block () == NULL
6033 ? NULL : gfc_current_block ()->name;
6035 switch (state)
6037 case COMP_ASSOCIATE:
6038 case COMP_BLOCK:
6039 if (!strncmp (block_name, "block@", strlen("block@")))
6040 block_name = NULL;
6041 break;
6043 case COMP_CONTAINS:
6044 case COMP_DERIVED_CONTAINS:
6045 state = gfc_state_stack->previous->state;
6046 block_name = gfc_state_stack->previous->sym == NULL
6047 ? NULL : gfc_state_stack->previous->sym->name;
6048 break;
6050 default:
6051 break;
6054 switch (state)
6056 case COMP_NONE:
6057 case COMP_PROGRAM:
6058 *st = ST_END_PROGRAM;
6059 target = " program";
6060 eos_ok = 1;
6061 break;
6063 case COMP_SUBROUTINE:
6064 *st = ST_END_SUBROUTINE;
6065 target = " subroutine";
6066 eos_ok = !contained_procedure ();
6067 break;
6069 case COMP_FUNCTION:
6070 *st = ST_END_FUNCTION;
6071 target = " function";
6072 eos_ok = !contained_procedure ();
6073 break;
6075 case COMP_BLOCK_DATA:
6076 *st = ST_END_BLOCK_DATA;
6077 target = " block data";
6078 eos_ok = 1;
6079 break;
6081 case COMP_MODULE:
6082 *st = ST_END_MODULE;
6083 target = " module";
6084 eos_ok = 1;
6085 break;
6087 case COMP_INTERFACE:
6088 *st = ST_END_INTERFACE;
6089 target = " interface";
6090 eos_ok = 0;
6091 break;
6093 case COMP_DERIVED:
6094 case COMP_DERIVED_CONTAINS:
6095 *st = ST_END_TYPE;
6096 target = " type";
6097 eos_ok = 0;
6098 break;
6100 case COMP_ASSOCIATE:
6101 *st = ST_END_ASSOCIATE;
6102 target = " associate";
6103 eos_ok = 0;
6104 break;
6106 case COMP_BLOCK:
6107 *st = ST_END_BLOCK;
6108 target = " block";
6109 eos_ok = 0;
6110 break;
6112 case COMP_IF:
6113 *st = ST_ENDIF;
6114 target = " if";
6115 eos_ok = 0;
6116 break;
6118 case COMP_DO:
6119 case COMP_DO_CONCURRENT:
6120 *st = ST_ENDDO;
6121 target = " do";
6122 eos_ok = 0;
6123 break;
6125 case COMP_CRITICAL:
6126 *st = ST_END_CRITICAL;
6127 target = " critical";
6128 eos_ok = 0;
6129 break;
6131 case COMP_SELECT:
6132 case COMP_SELECT_TYPE:
6133 *st = ST_END_SELECT;
6134 target = " select";
6135 eos_ok = 0;
6136 break;
6138 case COMP_FORALL:
6139 *st = ST_END_FORALL;
6140 target = " forall";
6141 eos_ok = 0;
6142 break;
6144 case COMP_WHERE:
6145 *st = ST_END_WHERE;
6146 target = " where";
6147 eos_ok = 0;
6148 break;
6150 case COMP_ENUM:
6151 *st = ST_END_ENUM;
6152 target = " enum";
6153 eos_ok = 0;
6154 last_initializer = NULL;
6155 set_enum_kind ();
6156 gfc_free_enum_history ();
6157 break;
6159 default:
6160 gfc_error ("Unexpected END statement at %C");
6161 goto cleanup;
6164 old_loc = gfc_current_locus;
6165 if (gfc_match_eos () == MATCH_YES)
6167 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6169 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
6170 "instead of %s statement at %L",
6171 gfc_ascii_statement(*st), &old_loc))
6172 goto cleanup;
6174 else if (!eos_ok)
6176 /* We would have required END [something]. */
6177 gfc_error ("%s statement expected at %L",
6178 gfc_ascii_statement (*st), &old_loc);
6179 goto cleanup;
6182 return MATCH_YES;
6185 /* Verify that we've got the sort of end-block that we're expecting. */
6186 if (gfc_match (target) != MATCH_YES)
6188 gfc_error ("Expecting %s statement at %L", gfc_ascii_statement (*st),
6189 &old_loc);
6190 goto cleanup;
6193 old_loc = gfc_current_locus;
6194 /* If we're at the end, make sure a block name wasn't required. */
6195 if (gfc_match_eos () == MATCH_YES)
6198 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6199 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6200 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6201 return MATCH_YES;
6203 if (!block_name)
6204 return MATCH_YES;
6206 gfc_error ("Expected block name of '%s' in %s statement at %L",
6207 block_name, gfc_ascii_statement (*st), &old_loc);
6209 return MATCH_ERROR;
6212 /* END INTERFACE has a special handler for its several possible endings. */
6213 if (*st == ST_END_INTERFACE)
6214 return gfc_match_end_interface ();
6216 /* We haven't hit the end of statement, so what is left must be an
6217 end-name. */
6218 m = gfc_match_space ();
6219 if (m == MATCH_YES)
6220 m = gfc_match_name (name);
6222 if (m == MATCH_NO)
6223 gfc_error ("Expected terminating name at %C");
6224 if (m != MATCH_YES)
6225 goto cleanup;
6227 if (block_name == NULL)
6228 goto syntax;
6230 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6232 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
6233 gfc_ascii_statement (*st));
6234 goto cleanup;
6236 /* Procedure pointer as function result. */
6237 else if (strcmp (block_name, "ppr@") == 0
6238 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6240 gfc_error ("Expected label '%s' for %s statement at %C",
6241 gfc_current_block ()->ns->proc_name->name,
6242 gfc_ascii_statement (*st));
6243 goto cleanup;
6246 if (gfc_match_eos () == MATCH_YES)
6247 return MATCH_YES;
6249 syntax:
6250 gfc_syntax_error (*st);
6252 cleanup:
6253 gfc_current_locus = old_loc;
6255 /* If we are missing an END BLOCK, we created a half-ready namespace.
6256 Remove it from the parent namespace's sibling list. */
6258 if (state == COMP_BLOCK)
6260 parent_ns = gfc_current_ns->parent;
6262 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
6264 prev_ns = NULL;
6265 ns = *nsp;
6266 while (ns)
6268 if (ns == gfc_current_ns)
6270 if (prev_ns == NULL)
6271 *nsp = NULL;
6272 else
6273 prev_ns->sibling = ns->sibling;
6275 prev_ns = ns;
6276 ns = ns->sibling;
6279 gfc_free_namespace (gfc_current_ns);
6280 gfc_current_ns = parent_ns;
6283 return MATCH_ERROR;
6288 /***************** Attribute declaration statements ****************/
6290 /* Set the attribute of a single variable. */
6292 static match
6293 attr_decl1 (void)
6295 char name[GFC_MAX_SYMBOL_LEN + 1];
6296 gfc_array_spec *as;
6297 gfc_symbol *sym;
6298 locus var_locus;
6299 match m;
6301 as = NULL;
6303 m = gfc_match_name (name);
6304 if (m != MATCH_YES)
6305 goto cleanup;
6307 if (find_special (name, &sym, false))
6308 return MATCH_ERROR;
6310 if (!check_function_name (name))
6312 m = MATCH_ERROR;
6313 goto cleanup;
6316 var_locus = gfc_current_locus;
6318 /* Deal with possible array specification for certain attributes. */
6319 if (current_attr.dimension
6320 || current_attr.codimension
6321 || current_attr.allocatable
6322 || current_attr.pointer
6323 || current_attr.target)
6325 m = gfc_match_array_spec (&as, !current_attr.codimension,
6326 !current_attr.dimension
6327 && !current_attr.pointer
6328 && !current_attr.target);
6329 if (m == MATCH_ERROR)
6330 goto cleanup;
6332 if (current_attr.dimension && m == MATCH_NO)
6334 gfc_error ("Missing array specification at %L in DIMENSION "
6335 "statement", &var_locus);
6336 m = MATCH_ERROR;
6337 goto cleanup;
6340 if (current_attr.dimension && sym->value)
6342 gfc_error ("Dimensions specified for %s at %L after its "
6343 "initialisation", sym->name, &var_locus);
6344 m = MATCH_ERROR;
6345 goto cleanup;
6348 if (current_attr.codimension && m == MATCH_NO)
6350 gfc_error ("Missing array specification at %L in CODIMENSION "
6351 "statement", &var_locus);
6352 m = MATCH_ERROR;
6353 goto cleanup;
6356 if ((current_attr.allocatable || current_attr.pointer)
6357 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6359 gfc_error ("Array specification must be deferred at %L", &var_locus);
6360 m = MATCH_ERROR;
6361 goto cleanup;
6365 /* Update symbol table. DIMENSION attribute is set in
6366 gfc_set_array_spec(). For CLASS variables, this must be applied
6367 to the first component, or '_data' field. */
6368 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6370 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
6372 m = MATCH_ERROR;
6373 goto cleanup;
6376 else
6378 if (current_attr.dimension == 0 && current_attr.codimension == 0
6379 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
6381 m = MATCH_ERROR;
6382 goto cleanup;
6386 if (sym->ts.type == BT_CLASS
6387 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
6389 m = MATCH_ERROR;
6390 goto cleanup;
6393 if (!gfc_set_array_spec (sym, as, &var_locus))
6395 m = MATCH_ERROR;
6396 goto cleanup;
6399 if (sym->attr.cray_pointee && sym->as != NULL)
6401 /* Fix the array spec. */
6402 m = gfc_mod_pointee_as (sym->as);
6403 if (m == MATCH_ERROR)
6404 goto cleanup;
6407 if (!gfc_add_attribute (&sym->attr, &var_locus))
6409 m = MATCH_ERROR;
6410 goto cleanup;
6413 if ((current_attr.external || current_attr.intrinsic)
6414 && sym->attr.flavor != FL_PROCEDURE
6415 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
6417 m = MATCH_ERROR;
6418 goto cleanup;
6421 add_hidden_procptr_result (sym);
6423 return MATCH_YES;
6425 cleanup:
6426 gfc_free_array_spec (as);
6427 return m;
6431 /* Generic attribute declaration subroutine. Used for attributes that
6432 just have a list of names. */
6434 static match
6435 attr_decl (void)
6437 match m;
6439 /* Gobble the optional double colon, by simply ignoring the result
6440 of gfc_match(). */
6441 gfc_match (" ::");
6443 for (;;)
6445 m = attr_decl1 ();
6446 if (m != MATCH_YES)
6447 break;
6449 if (gfc_match_eos () == MATCH_YES)
6451 m = MATCH_YES;
6452 break;
6455 if (gfc_match_char (',') != MATCH_YES)
6457 gfc_error ("Unexpected character in variable list at %C");
6458 m = MATCH_ERROR;
6459 break;
6463 return m;
6467 /* This routine matches Cray Pointer declarations of the form:
6468 pointer ( <pointer>, <pointee> )
6470 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6471 The pointer, if already declared, should be an integer. Otherwise, we
6472 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6473 be either a scalar, or an array declaration. No space is allocated for
6474 the pointee. For the statement
6475 pointer (ipt, ar(10))
6476 any subsequent uses of ar will be translated (in C-notation) as
6477 ar(i) => ((<type> *) ipt)(i)
6478 After gimplification, pointee variable will disappear in the code. */
6480 static match
6481 cray_pointer_decl (void)
6483 match m;
6484 gfc_array_spec *as = NULL;
6485 gfc_symbol *cptr; /* Pointer symbol. */
6486 gfc_symbol *cpte; /* Pointee symbol. */
6487 locus var_locus;
6488 bool done = false;
6490 while (!done)
6492 if (gfc_match_char ('(') != MATCH_YES)
6494 gfc_error ("Expected '(' at %C");
6495 return MATCH_ERROR;
6498 /* Match pointer. */
6499 var_locus = gfc_current_locus;
6500 gfc_clear_attr (&current_attr);
6501 gfc_add_cray_pointer (&current_attr, &var_locus);
6502 current_ts.type = BT_INTEGER;
6503 current_ts.kind = gfc_index_integer_kind;
6505 m = gfc_match_symbol (&cptr, 0);
6506 if (m != MATCH_YES)
6508 gfc_error ("Expected variable name at %C");
6509 return m;
6512 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
6513 return MATCH_ERROR;
6515 gfc_set_sym_referenced (cptr);
6517 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6519 cptr->ts.type = BT_INTEGER;
6520 cptr->ts.kind = gfc_index_integer_kind;
6522 else if (cptr->ts.type != BT_INTEGER)
6524 gfc_error ("Cray pointer at %C must be an integer");
6525 return MATCH_ERROR;
6527 else if (cptr->ts.kind < gfc_index_integer_kind)
6528 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6529 " memory addresses require %d bytes",
6530 cptr->ts.kind, gfc_index_integer_kind);
6532 if (gfc_match_char (',') != MATCH_YES)
6534 gfc_error ("Expected \",\" at %C");
6535 return MATCH_ERROR;
6538 /* Match Pointee. */
6539 var_locus = gfc_current_locus;
6540 gfc_clear_attr (&current_attr);
6541 gfc_add_cray_pointee (&current_attr, &var_locus);
6542 current_ts.type = BT_UNKNOWN;
6543 current_ts.kind = 0;
6545 m = gfc_match_symbol (&cpte, 0);
6546 if (m != MATCH_YES)
6548 gfc_error ("Expected variable name at %C");
6549 return m;
6552 /* Check for an optional array spec. */
6553 m = gfc_match_array_spec (&as, true, false);
6554 if (m == MATCH_ERROR)
6556 gfc_free_array_spec (as);
6557 return m;
6559 else if (m == MATCH_NO)
6561 gfc_free_array_spec (as);
6562 as = NULL;
6565 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
6566 return MATCH_ERROR;
6568 gfc_set_sym_referenced (cpte);
6570 if (cpte->as == NULL)
6572 if (!gfc_set_array_spec (cpte, as, &var_locus))
6573 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6575 else if (as != NULL)
6577 gfc_error ("Duplicate array spec for Cray pointee at %C");
6578 gfc_free_array_spec (as);
6579 return MATCH_ERROR;
6582 as = NULL;
6584 if (cpte->as != NULL)
6586 /* Fix array spec. */
6587 m = gfc_mod_pointee_as (cpte->as);
6588 if (m == MATCH_ERROR)
6589 return m;
6592 /* Point the Pointee at the Pointer. */
6593 cpte->cp_pointer = cptr;
6595 if (gfc_match_char (')') != MATCH_YES)
6597 gfc_error ("Expected \")\" at %C");
6598 return MATCH_ERROR;
6600 m = gfc_match_char (',');
6601 if (m != MATCH_YES)
6602 done = true; /* Stop searching for more declarations. */
6606 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6607 || gfc_match_eos () != MATCH_YES)
6609 gfc_error ("Expected \",\" or end of statement at %C");
6610 return MATCH_ERROR;
6612 return MATCH_YES;
6616 match
6617 gfc_match_external (void)
6620 gfc_clear_attr (&current_attr);
6621 current_attr.external = 1;
6623 return attr_decl ();
6627 match
6628 gfc_match_intent (void)
6630 sym_intent intent;
6632 /* This is not allowed within a BLOCK construct! */
6633 if (gfc_current_state () == COMP_BLOCK)
6635 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6636 return MATCH_ERROR;
6639 intent = match_intent_spec ();
6640 if (intent == INTENT_UNKNOWN)
6641 return MATCH_ERROR;
6643 gfc_clear_attr (&current_attr);
6644 current_attr.intent = intent;
6646 return attr_decl ();
6650 match
6651 gfc_match_intrinsic (void)
6654 gfc_clear_attr (&current_attr);
6655 current_attr.intrinsic = 1;
6657 return attr_decl ();
6661 match
6662 gfc_match_optional (void)
6664 /* This is not allowed within a BLOCK construct! */
6665 if (gfc_current_state () == COMP_BLOCK)
6667 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6668 return MATCH_ERROR;
6671 gfc_clear_attr (&current_attr);
6672 current_attr.optional = 1;
6674 return attr_decl ();
6678 match
6679 gfc_match_pointer (void)
6681 gfc_gobble_whitespace ();
6682 if (gfc_peek_ascii_char () == '(')
6684 if (!gfc_option.flag_cray_pointer)
6686 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6687 "flag");
6688 return MATCH_ERROR;
6690 return cray_pointer_decl ();
6692 else
6694 gfc_clear_attr (&current_attr);
6695 current_attr.pointer = 1;
6697 return attr_decl ();
6702 match
6703 gfc_match_allocatable (void)
6705 gfc_clear_attr (&current_attr);
6706 current_attr.allocatable = 1;
6708 return attr_decl ();
6712 match
6713 gfc_match_codimension (void)
6715 gfc_clear_attr (&current_attr);
6716 current_attr.codimension = 1;
6718 return attr_decl ();
6722 match
6723 gfc_match_contiguous (void)
6725 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
6726 return MATCH_ERROR;
6728 gfc_clear_attr (&current_attr);
6729 current_attr.contiguous = 1;
6731 return attr_decl ();
6735 match
6736 gfc_match_dimension (void)
6738 gfc_clear_attr (&current_attr);
6739 current_attr.dimension = 1;
6741 return attr_decl ();
6745 match
6746 gfc_match_target (void)
6748 gfc_clear_attr (&current_attr);
6749 current_attr.target = 1;
6751 return attr_decl ();
6755 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6756 statement. */
6758 static match
6759 access_attr_decl (gfc_statement st)
6761 char name[GFC_MAX_SYMBOL_LEN + 1];
6762 interface_type type;
6763 gfc_user_op *uop;
6764 gfc_symbol *sym, *dt_sym;
6765 gfc_intrinsic_op op;
6766 match m;
6768 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6769 goto done;
6771 for (;;)
6773 m = gfc_match_generic_spec (&type, name, &op);
6774 if (m == MATCH_NO)
6775 goto syntax;
6776 if (m == MATCH_ERROR)
6777 return MATCH_ERROR;
6779 switch (type)
6781 case INTERFACE_NAMELESS:
6782 case INTERFACE_ABSTRACT:
6783 goto syntax;
6785 case INTERFACE_GENERIC:
6786 if (gfc_get_symbol (name, NULL, &sym))
6787 goto done;
6789 if (!gfc_add_access (&sym->attr,
6790 (st == ST_PUBLIC)
6791 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6792 sym->name, NULL))
6793 return MATCH_ERROR;
6795 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
6796 && !gfc_add_access (&dt_sym->attr,
6797 (st == ST_PUBLIC)
6798 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6799 sym->name, NULL))
6800 return MATCH_ERROR;
6802 break;
6804 case INTERFACE_INTRINSIC_OP:
6805 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6807 gfc_intrinsic_op other_op;
6809 gfc_current_ns->operator_access[op] =
6810 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6812 /* Handle the case if there is another op with the same
6813 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6814 other_op = gfc_equivalent_op (op);
6816 if (other_op != INTRINSIC_NONE)
6817 gfc_current_ns->operator_access[other_op] =
6818 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6821 else
6823 gfc_error ("Access specification of the %s operator at %C has "
6824 "already been specified", gfc_op2string (op));
6825 goto done;
6828 break;
6830 case INTERFACE_USER_OP:
6831 uop = gfc_get_uop (name);
6833 if (uop->access == ACCESS_UNKNOWN)
6835 uop->access = (st == ST_PUBLIC)
6836 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6838 else
6840 gfc_error ("Access specification of the .%s. operator at %C "
6841 "has already been specified", sym->name);
6842 goto done;
6845 break;
6848 if (gfc_match_char (',') == MATCH_NO)
6849 break;
6852 if (gfc_match_eos () != MATCH_YES)
6853 goto syntax;
6854 return MATCH_YES;
6856 syntax:
6857 gfc_syntax_error (st);
6859 done:
6860 return MATCH_ERROR;
6864 match
6865 gfc_match_protected (void)
6867 gfc_symbol *sym;
6868 match m;
6870 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6872 gfc_error ("PROTECTED at %C only allowed in specification "
6873 "part of a module");
6874 return MATCH_ERROR;
6878 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
6879 return MATCH_ERROR;
6881 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6883 return MATCH_ERROR;
6886 if (gfc_match_eos () == MATCH_YES)
6887 goto syntax;
6889 for(;;)
6891 m = gfc_match_symbol (&sym, 0);
6892 switch (m)
6894 case MATCH_YES:
6895 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
6896 return MATCH_ERROR;
6897 goto next_item;
6899 case MATCH_NO:
6900 break;
6902 case MATCH_ERROR:
6903 return MATCH_ERROR;
6906 next_item:
6907 if (gfc_match_eos () == MATCH_YES)
6908 break;
6909 if (gfc_match_char (',') != MATCH_YES)
6910 goto syntax;
6913 return MATCH_YES;
6915 syntax:
6916 gfc_error ("Syntax error in PROTECTED statement at %C");
6917 return MATCH_ERROR;
6921 /* The PRIVATE statement is a bit weird in that it can be an attribute
6922 declaration, but also works as a standalone statement inside of a
6923 type declaration or a module. */
6925 match
6926 gfc_match_private (gfc_statement *st)
6929 if (gfc_match ("private") != MATCH_YES)
6930 return MATCH_NO;
6932 if (gfc_current_state () != COMP_MODULE
6933 && !(gfc_current_state () == COMP_DERIVED
6934 && gfc_state_stack->previous
6935 && gfc_state_stack->previous->state == COMP_MODULE)
6936 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6937 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6938 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6940 gfc_error ("PRIVATE statement at %C is only allowed in the "
6941 "specification part of a module");
6942 return MATCH_ERROR;
6945 if (gfc_current_state () == COMP_DERIVED)
6947 if (gfc_match_eos () == MATCH_YES)
6949 *st = ST_PRIVATE;
6950 return MATCH_YES;
6953 gfc_syntax_error (ST_PRIVATE);
6954 return MATCH_ERROR;
6957 if (gfc_match_eos () == MATCH_YES)
6959 *st = ST_PRIVATE;
6960 return MATCH_YES;
6963 *st = ST_ATTR_DECL;
6964 return access_attr_decl (ST_PRIVATE);
6968 match
6969 gfc_match_public (gfc_statement *st)
6972 if (gfc_match ("public") != MATCH_YES)
6973 return MATCH_NO;
6975 if (gfc_current_state () != COMP_MODULE)
6977 gfc_error ("PUBLIC statement at %C is only allowed in the "
6978 "specification part of a module");
6979 return MATCH_ERROR;
6982 if (gfc_match_eos () == MATCH_YES)
6984 *st = ST_PUBLIC;
6985 return MATCH_YES;
6988 *st = ST_ATTR_DECL;
6989 return access_attr_decl (ST_PUBLIC);
6993 /* Workhorse for gfc_match_parameter. */
6995 static match
6996 do_parm (void)
6998 gfc_symbol *sym;
6999 gfc_expr *init;
7000 match m;
7001 bool t;
7003 m = gfc_match_symbol (&sym, 0);
7004 if (m == MATCH_NO)
7005 gfc_error ("Expected variable name at %C in PARAMETER statement");
7007 if (m != MATCH_YES)
7008 return m;
7010 if (gfc_match_char ('=') == MATCH_NO)
7012 gfc_error ("Expected = sign in PARAMETER statement at %C");
7013 return MATCH_ERROR;
7016 m = gfc_match_init_expr (&init);
7017 if (m == MATCH_NO)
7018 gfc_error ("Expected expression at %C in PARAMETER statement");
7019 if (m != MATCH_YES)
7020 return m;
7022 if (sym->ts.type == BT_UNKNOWN
7023 && !gfc_set_default_type (sym, 1, NULL))
7025 m = MATCH_ERROR;
7026 goto cleanup;
7029 if (!gfc_check_assign_symbol (sym, NULL, init)
7030 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
7032 m = MATCH_ERROR;
7033 goto cleanup;
7036 if (sym->value)
7038 gfc_error ("Initializing already initialized variable at %C");
7039 m = MATCH_ERROR;
7040 goto cleanup;
7043 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
7044 return (t) ? MATCH_YES : MATCH_ERROR;
7046 cleanup:
7047 gfc_free_expr (init);
7048 return m;
7052 /* Match a parameter statement, with the weird syntax that these have. */
7054 match
7055 gfc_match_parameter (void)
7057 match m;
7059 if (gfc_match_char ('(') == MATCH_NO)
7060 return MATCH_NO;
7062 for (;;)
7064 m = do_parm ();
7065 if (m != MATCH_YES)
7066 break;
7068 if (gfc_match (" )%t") == MATCH_YES)
7069 break;
7071 if (gfc_match_char (',') != MATCH_YES)
7073 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7074 m = MATCH_ERROR;
7075 break;
7079 return m;
7083 /* Save statements have a special syntax. */
7085 match
7086 gfc_match_save (void)
7088 char n[GFC_MAX_SYMBOL_LEN+1];
7089 gfc_common_head *c;
7090 gfc_symbol *sym;
7091 match m;
7093 if (gfc_match_eos () == MATCH_YES)
7095 if (gfc_current_ns->seen_save)
7097 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
7098 "follows previous SAVE statement"))
7099 return MATCH_ERROR;
7102 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
7103 return MATCH_YES;
7106 if (gfc_current_ns->save_all)
7108 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
7109 "blanket SAVE statement"))
7110 return MATCH_ERROR;
7113 gfc_match (" ::");
7115 for (;;)
7117 m = gfc_match_symbol (&sym, 0);
7118 switch (m)
7120 case MATCH_YES:
7121 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
7122 &gfc_current_locus))
7123 return MATCH_ERROR;
7124 goto next_item;
7126 case MATCH_NO:
7127 break;
7129 case MATCH_ERROR:
7130 return MATCH_ERROR;
7133 m = gfc_match (" / %n /", &n);
7134 if (m == MATCH_ERROR)
7135 return MATCH_ERROR;
7136 if (m == MATCH_NO)
7137 goto syntax;
7139 c = gfc_get_common (n, 0);
7140 c->saved = 1;
7142 gfc_current_ns->seen_save = 1;
7144 next_item:
7145 if (gfc_match_eos () == MATCH_YES)
7146 break;
7147 if (gfc_match_char (',') != MATCH_YES)
7148 goto syntax;
7151 return MATCH_YES;
7153 syntax:
7154 gfc_error ("Syntax error in SAVE statement at %C");
7155 return MATCH_ERROR;
7159 match
7160 gfc_match_value (void)
7162 gfc_symbol *sym;
7163 match m;
7165 /* This is not allowed within a BLOCK construct! */
7166 if (gfc_current_state () == COMP_BLOCK)
7168 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7169 return MATCH_ERROR;
7172 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
7173 return MATCH_ERROR;
7175 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7177 return MATCH_ERROR;
7180 if (gfc_match_eos () == MATCH_YES)
7181 goto syntax;
7183 for(;;)
7185 m = gfc_match_symbol (&sym, 0);
7186 switch (m)
7188 case MATCH_YES:
7189 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
7190 return MATCH_ERROR;
7191 goto next_item;
7193 case MATCH_NO:
7194 break;
7196 case MATCH_ERROR:
7197 return MATCH_ERROR;
7200 next_item:
7201 if (gfc_match_eos () == MATCH_YES)
7202 break;
7203 if (gfc_match_char (',') != MATCH_YES)
7204 goto syntax;
7207 return MATCH_YES;
7209 syntax:
7210 gfc_error ("Syntax error in VALUE statement at %C");
7211 return MATCH_ERROR;
7215 match
7216 gfc_match_volatile (void)
7218 gfc_symbol *sym;
7219 match m;
7221 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
7222 return MATCH_ERROR;
7224 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7226 return MATCH_ERROR;
7229 if (gfc_match_eos () == MATCH_YES)
7230 goto syntax;
7232 for(;;)
7234 /* VOLATILE is special because it can be added to host-associated
7235 symbols locally. Except for coarrays. */
7236 m = gfc_match_symbol (&sym, 1);
7237 switch (m)
7239 case MATCH_YES:
7240 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7241 for variable in a BLOCK which is defined outside of the BLOCK. */
7242 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7244 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
7245 "%C, which is use-/host-associated", sym->name);
7246 return MATCH_ERROR;
7248 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
7249 return MATCH_ERROR;
7250 goto next_item;
7252 case MATCH_NO:
7253 break;
7255 case MATCH_ERROR:
7256 return MATCH_ERROR;
7259 next_item:
7260 if (gfc_match_eos () == MATCH_YES)
7261 break;
7262 if (gfc_match_char (',') != MATCH_YES)
7263 goto syntax;
7266 return MATCH_YES;
7268 syntax:
7269 gfc_error ("Syntax error in VOLATILE statement at %C");
7270 return MATCH_ERROR;
7274 match
7275 gfc_match_asynchronous (void)
7277 gfc_symbol *sym;
7278 match m;
7280 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
7281 return MATCH_ERROR;
7283 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7285 return MATCH_ERROR;
7288 if (gfc_match_eos () == MATCH_YES)
7289 goto syntax;
7291 for(;;)
7293 /* ASYNCHRONOUS is special because it can be added to host-associated
7294 symbols locally. */
7295 m = gfc_match_symbol (&sym, 1);
7296 switch (m)
7298 case MATCH_YES:
7299 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
7300 return MATCH_ERROR;
7301 goto next_item;
7303 case MATCH_NO:
7304 break;
7306 case MATCH_ERROR:
7307 return MATCH_ERROR;
7310 next_item:
7311 if (gfc_match_eos () == MATCH_YES)
7312 break;
7313 if (gfc_match_char (',') != MATCH_YES)
7314 goto syntax;
7317 return MATCH_YES;
7319 syntax:
7320 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7321 return MATCH_ERROR;
7325 /* Match a module procedure statement. Note that we have to modify
7326 symbols in the parent's namespace because the current one was there
7327 to receive symbols that are in an interface's formal argument list. */
7329 match
7330 gfc_match_modproc (void)
7332 char name[GFC_MAX_SYMBOL_LEN + 1];
7333 gfc_symbol *sym;
7334 match m;
7335 locus old_locus;
7336 gfc_namespace *module_ns;
7337 gfc_interface *old_interface_head, *interface;
7339 if (gfc_state_stack->state != COMP_INTERFACE
7340 || gfc_state_stack->previous == NULL
7341 || current_interface.type == INTERFACE_NAMELESS
7342 || current_interface.type == INTERFACE_ABSTRACT)
7344 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7345 "interface");
7346 return MATCH_ERROR;
7349 module_ns = gfc_current_ns->parent;
7350 for (; module_ns; module_ns = module_ns->parent)
7351 if (module_ns->proc_name->attr.flavor == FL_MODULE
7352 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7353 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7354 && !module_ns->proc_name->attr.contained))
7355 break;
7357 if (module_ns == NULL)
7358 return MATCH_ERROR;
7360 /* Store the current state of the interface. We will need it if we
7361 end up with a syntax error and need to recover. */
7362 old_interface_head = gfc_current_interface_head ();
7364 /* Check if the F2008 optional double colon appears. */
7365 gfc_gobble_whitespace ();
7366 old_locus = gfc_current_locus;
7367 if (gfc_match ("::") == MATCH_YES)
7369 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7370 "MODULE PROCEDURE statement at %L", &old_locus))
7371 return MATCH_ERROR;
7373 else
7374 gfc_current_locus = old_locus;
7376 for (;;)
7378 bool last = false;
7379 old_locus = gfc_current_locus;
7381 m = gfc_match_name (name);
7382 if (m == MATCH_NO)
7383 goto syntax;
7384 if (m != MATCH_YES)
7385 return MATCH_ERROR;
7387 /* Check for syntax error before starting to add symbols to the
7388 current namespace. */
7389 if (gfc_match_eos () == MATCH_YES)
7390 last = true;
7392 if (!last && gfc_match_char (',') != MATCH_YES)
7393 goto syntax;
7395 /* Now we're sure the syntax is valid, we process this item
7396 further. */
7397 if (gfc_get_symbol (name, module_ns, &sym))
7398 return MATCH_ERROR;
7400 if (sym->attr.intrinsic)
7402 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7403 "PROCEDURE", &old_locus);
7404 return MATCH_ERROR;
7407 if (sym->attr.proc != PROC_MODULE
7408 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
7409 return MATCH_ERROR;
7411 if (!gfc_add_interface (sym))
7412 return MATCH_ERROR;
7414 sym->attr.mod_proc = 1;
7415 sym->declared_at = old_locus;
7417 if (last)
7418 break;
7421 return MATCH_YES;
7423 syntax:
7424 /* Restore the previous state of the interface. */
7425 interface = gfc_current_interface_head ();
7426 gfc_set_current_interface_head (old_interface_head);
7428 /* Free the new interfaces. */
7429 while (interface != old_interface_head)
7431 gfc_interface *i = interface->next;
7432 free (interface);
7433 interface = i;
7436 /* And issue a syntax error. */
7437 gfc_syntax_error (ST_MODULE_PROC);
7438 return MATCH_ERROR;
7442 /* Check a derived type that is being extended. */
7444 static gfc_symbol*
7445 check_extended_derived_type (char *name)
7447 gfc_symbol *extended;
7449 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7451 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7452 return NULL;
7455 extended = gfc_find_dt_in_generic (extended);
7457 /* F08:C428. */
7458 if (!extended)
7460 gfc_error ("Symbol '%s' at %C has not been previously defined", name);
7461 return NULL;
7464 if (extended->attr.flavor != FL_DERIVED)
7466 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7467 "derived type", name);
7468 return NULL;
7471 if (extended->attr.is_bind_c)
7473 gfc_error ("'%s' cannot be extended at %C because it "
7474 "is BIND(C)", extended->name);
7475 return NULL;
7478 if (extended->attr.sequence)
7480 gfc_error ("'%s' cannot be extended at %C because it "
7481 "is a SEQUENCE type", extended->name);
7482 return NULL;
7485 return extended;
7489 /* Match the optional attribute specifiers for a type declaration.
7490 Return MATCH_ERROR if an error is encountered in one of the handled
7491 attributes (public, private, bind(c)), MATCH_NO if what's found is
7492 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7493 checking on attribute conflicts needs to be done. */
7495 match
7496 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7498 /* See if the derived type is marked as private. */
7499 if (gfc_match (" , private") == MATCH_YES)
7501 if (gfc_current_state () != COMP_MODULE)
7503 gfc_error ("Derived type at %C can only be PRIVATE in the "
7504 "specification part of a module");
7505 return MATCH_ERROR;
7508 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
7509 return MATCH_ERROR;
7511 else if (gfc_match (" , public") == MATCH_YES)
7513 if (gfc_current_state () != COMP_MODULE)
7515 gfc_error ("Derived type at %C can only be PUBLIC in the "
7516 "specification part of a module");
7517 return MATCH_ERROR;
7520 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
7521 return MATCH_ERROR;
7523 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7525 /* If the type is defined to be bind(c) it then needs to make
7526 sure that all fields are interoperable. This will
7527 need to be a semantic check on the finished derived type.
7528 See 15.2.3 (lines 9-12) of F2003 draft. */
7529 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
7530 return MATCH_ERROR;
7532 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7534 else if (gfc_match (" , abstract") == MATCH_YES)
7536 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
7537 return MATCH_ERROR;
7539 if (!gfc_add_abstract (attr, &gfc_current_locus))
7540 return MATCH_ERROR;
7542 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
7544 if (!gfc_add_extension (attr, &gfc_current_locus))
7545 return MATCH_ERROR;
7547 else
7548 return MATCH_NO;
7550 /* If we get here, something matched. */
7551 return MATCH_YES;
7555 /* Match the beginning of a derived type declaration. If a type name
7556 was the result of a function, then it is possible to have a symbol
7557 already to be known as a derived type yet have no components. */
7559 match
7560 gfc_match_derived_decl (void)
7562 char name[GFC_MAX_SYMBOL_LEN + 1];
7563 char parent[GFC_MAX_SYMBOL_LEN + 1];
7564 symbol_attribute attr;
7565 gfc_symbol *sym, *gensym;
7566 gfc_symbol *extended;
7567 match m;
7568 match is_type_attr_spec = MATCH_NO;
7569 bool seen_attr = false;
7570 gfc_interface *intr = NULL, *head;
7572 if (gfc_current_state () == COMP_DERIVED)
7573 return MATCH_NO;
7575 name[0] = '\0';
7576 parent[0] = '\0';
7577 gfc_clear_attr (&attr);
7578 extended = NULL;
7582 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7583 if (is_type_attr_spec == MATCH_ERROR)
7584 return MATCH_ERROR;
7585 if (is_type_attr_spec == MATCH_YES)
7586 seen_attr = true;
7587 } while (is_type_attr_spec == MATCH_YES);
7589 /* Deal with derived type extensions. The extension attribute has
7590 been added to 'attr' but now the parent type must be found and
7591 checked. */
7592 if (parent[0])
7593 extended = check_extended_derived_type (parent);
7595 if (parent[0] && !extended)
7596 return MATCH_ERROR;
7598 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7600 gfc_error ("Expected :: in TYPE definition at %C");
7601 return MATCH_ERROR;
7604 m = gfc_match (" %n%t", name);
7605 if (m != MATCH_YES)
7606 return m;
7608 /* Make sure the name is not the name of an intrinsic type. */
7609 if (gfc_is_intrinsic_typename (name))
7611 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7612 "type", name);
7613 return MATCH_ERROR;
7616 if (gfc_get_symbol (name, NULL, &gensym))
7617 return MATCH_ERROR;
7619 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
7621 gfc_error ("Derived type name '%s' at %C already has a basic type "
7622 "of %s", gensym->name, gfc_typename (&gensym->ts));
7623 return MATCH_ERROR;
7626 if (!gensym->attr.generic
7627 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
7628 return MATCH_ERROR;
7630 if (!gensym->attr.function
7631 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
7632 return MATCH_ERROR;
7634 sym = gfc_find_dt_in_generic (gensym);
7636 if (sym && (sym->components != NULL || sym->attr.zero_comp))
7638 gfc_error ("Derived type definition of '%s' at %C has already been "
7639 "defined", sym->name);
7640 return MATCH_ERROR;
7643 if (!sym)
7645 /* Use upper case to save the actual derived-type symbol. */
7646 gfc_get_symbol (gfc_get_string ("%c%s",
7647 (char) TOUPPER ((unsigned char) gensym->name[0]),
7648 &gensym->name[1]), NULL, &sym);
7649 sym->name = gfc_get_string (gensym->name);
7650 head = gensym->generic;
7651 intr = gfc_get_interface ();
7652 intr->sym = sym;
7653 intr->where = gfc_current_locus;
7654 intr->sym->declared_at = gfc_current_locus;
7655 intr->next = head;
7656 gensym->generic = intr;
7657 gensym->attr.if_source = IFSRC_DECL;
7660 /* The symbol may already have the derived attribute without the
7661 components. The ways this can happen is via a function
7662 definition, an INTRINSIC statement or a subtype in another
7663 derived type that is a pointer. The first part of the AND clause
7664 is true if the symbol is not the return value of a function. */
7665 if (sym->attr.flavor != FL_DERIVED
7666 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
7667 return MATCH_ERROR;
7669 if (attr.access != ACCESS_UNKNOWN
7670 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
7671 return MATCH_ERROR;
7672 else if (sym->attr.access == ACCESS_UNKNOWN
7673 && gensym->attr.access != ACCESS_UNKNOWN
7674 && !gfc_add_access (&sym->attr, gensym->attr.access,
7675 sym->name, NULL))
7676 return MATCH_ERROR;
7678 if (sym->attr.access != ACCESS_UNKNOWN
7679 && gensym->attr.access == ACCESS_UNKNOWN)
7680 gensym->attr.access = sym->attr.access;
7682 /* See if the derived type was labeled as bind(c). */
7683 if (attr.is_bind_c != 0)
7684 sym->attr.is_bind_c = attr.is_bind_c;
7686 /* Construct the f2k_derived namespace if it is not yet there. */
7687 if (!sym->f2k_derived)
7688 sym->f2k_derived = gfc_get_namespace (NULL, 0);
7690 if (extended && !sym->components)
7692 gfc_component *p;
7693 gfc_symtree *st;
7695 /* Add the extended derived type as the first component. */
7696 gfc_add_component (sym, parent, &p);
7697 extended->refs++;
7698 gfc_set_sym_referenced (extended);
7700 p->ts.type = BT_DERIVED;
7701 p->ts.u.derived = extended;
7702 p->initializer = gfc_default_initializer (&p->ts);
7704 /* Set extension level. */
7705 if (extended->attr.extension == 255)
7707 /* Since the extension field is 8 bit wide, we can only have
7708 up to 255 extension levels. */
7709 gfc_error ("Maximum extension level reached with type '%s' at %L",
7710 extended->name, &extended->declared_at);
7711 return MATCH_ERROR;
7713 sym->attr.extension = extended->attr.extension + 1;
7715 /* Provide the links between the extended type and its extension. */
7716 if (!extended->f2k_derived)
7717 extended->f2k_derived = gfc_get_namespace (NULL, 0);
7718 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7719 st->n.sym = sym;
7722 if (!sym->hash_value)
7723 /* Set the hash for the compound name for this type. */
7724 sym->hash_value = gfc_hash_value (sym);
7726 /* Take over the ABSTRACT attribute. */
7727 sym->attr.abstract = attr.abstract;
7729 gfc_new_block = sym;
7731 return MATCH_YES;
7735 /* Cray Pointees can be declared as:
7736 pointer (ipt, a (n,m,...,*)) */
7738 match
7739 gfc_mod_pointee_as (gfc_array_spec *as)
7741 as->cray_pointee = true; /* This will be useful to know later. */
7742 if (as->type == AS_ASSUMED_SIZE)
7743 as->cp_was_assumed = true;
7744 else if (as->type == AS_ASSUMED_SHAPE)
7746 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7747 return MATCH_ERROR;
7749 return MATCH_YES;
7753 /* Match the enum definition statement, here we are trying to match
7754 the first line of enum definition statement.
7755 Returns MATCH_YES if match is found. */
7757 match
7758 gfc_match_enum (void)
7760 match m;
7762 m = gfc_match_eos ();
7763 if (m != MATCH_YES)
7764 return m;
7766 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
7767 return MATCH_ERROR;
7769 return MATCH_YES;
7773 /* Returns an initializer whose value is one higher than the value of the
7774 LAST_INITIALIZER argument. If the argument is NULL, the
7775 initializers value will be set to zero. The initializer's kind
7776 will be set to gfc_c_int_kind.
7778 If -fshort-enums is given, the appropriate kind will be selected
7779 later after all enumerators have been parsed. A warning is issued
7780 here if an initializer exceeds gfc_c_int_kind. */
7782 static gfc_expr *
7783 enum_initializer (gfc_expr *last_initializer, locus where)
7785 gfc_expr *result;
7786 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7788 mpz_init (result->value.integer);
7790 if (last_initializer != NULL)
7792 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7793 result->where = last_initializer->where;
7795 if (gfc_check_integer_range (result->value.integer,
7796 gfc_c_int_kind) != ARITH_OK)
7798 gfc_error ("Enumerator exceeds the C integer type at %C");
7799 return NULL;
7802 else
7804 /* Control comes here, if it's the very first enumerator and no
7805 initializer has been given. It will be initialized to zero. */
7806 mpz_set_si (result->value.integer, 0);
7809 return result;
7813 /* Match a variable name with an optional initializer. When this
7814 subroutine is called, a variable is expected to be parsed next.
7815 Depending on what is happening at the moment, updates either the
7816 symbol table or the current interface. */
7818 static match
7819 enumerator_decl (void)
7821 char name[GFC_MAX_SYMBOL_LEN + 1];
7822 gfc_expr *initializer;
7823 gfc_array_spec *as = NULL;
7824 gfc_symbol *sym;
7825 locus var_locus;
7826 match m;
7827 bool t;
7828 locus old_locus;
7830 initializer = NULL;
7831 old_locus = gfc_current_locus;
7833 /* When we get here, we've just matched a list of attributes and
7834 maybe a type and a double colon. The next thing we expect to see
7835 is the name of the symbol. */
7836 m = gfc_match_name (name);
7837 if (m != MATCH_YES)
7838 goto cleanup;
7840 var_locus = gfc_current_locus;
7842 /* OK, we've successfully matched the declaration. Now put the
7843 symbol in the current namespace. If we fail to create the symbol,
7844 bail out. */
7845 if (!build_sym (name, NULL, false, &as, &var_locus))
7847 m = MATCH_ERROR;
7848 goto cleanup;
7851 /* The double colon must be present in order to have initializers.
7852 Otherwise the statement is ambiguous with an assignment statement. */
7853 if (colon_seen)
7855 if (gfc_match_char ('=') == MATCH_YES)
7857 m = gfc_match_init_expr (&initializer);
7858 if (m == MATCH_NO)
7860 gfc_error ("Expected an initialization expression at %C");
7861 m = MATCH_ERROR;
7864 if (m != MATCH_YES)
7865 goto cleanup;
7869 /* If we do not have an initializer, the initialization value of the
7870 previous enumerator (stored in last_initializer) is incremented
7871 by 1 and is used to initialize the current enumerator. */
7872 if (initializer == NULL)
7873 initializer = enum_initializer (last_initializer, old_locus);
7875 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7877 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7878 &var_locus);
7879 m = MATCH_ERROR;
7880 goto cleanup;
7883 /* Store this current initializer, for the next enumerator variable
7884 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7885 use last_initializer below. */
7886 last_initializer = initializer;
7887 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7889 /* Maintain enumerator history. */
7890 gfc_find_symbol (name, NULL, 0, &sym);
7891 create_enum_history (sym, last_initializer);
7893 return (t) ? MATCH_YES : MATCH_ERROR;
7895 cleanup:
7896 /* Free stuff up and return. */
7897 gfc_free_expr (initializer);
7899 return m;
7903 /* Match the enumerator definition statement. */
7905 match
7906 gfc_match_enumerator_def (void)
7908 match m;
7909 bool t;
7911 gfc_clear_ts (&current_ts);
7913 m = gfc_match (" enumerator");
7914 if (m != MATCH_YES)
7915 return m;
7917 m = gfc_match (" :: ");
7918 if (m == MATCH_ERROR)
7919 return m;
7921 colon_seen = (m == MATCH_YES);
7923 if (gfc_current_state () != COMP_ENUM)
7925 gfc_error ("ENUM definition statement expected before %C");
7926 gfc_free_enum_history ();
7927 return MATCH_ERROR;
7930 (&current_ts)->type = BT_INTEGER;
7931 (&current_ts)->kind = gfc_c_int_kind;
7933 gfc_clear_attr (&current_attr);
7934 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
7935 if (!t)
7937 m = MATCH_ERROR;
7938 goto cleanup;
7941 for (;;)
7943 m = enumerator_decl ();
7944 if (m == MATCH_ERROR)
7946 gfc_free_enum_history ();
7947 goto cleanup;
7949 if (m == MATCH_NO)
7950 break;
7952 if (gfc_match_eos () == MATCH_YES)
7953 goto cleanup;
7954 if (gfc_match_char (',') != MATCH_YES)
7955 break;
7958 if (gfc_current_state () == COMP_ENUM)
7960 gfc_free_enum_history ();
7961 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7962 m = MATCH_ERROR;
7965 cleanup:
7966 gfc_free_array_spec (current_as);
7967 current_as = NULL;
7968 return m;
7973 /* Match binding attributes. */
7975 static match
7976 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7978 bool found_passing = false;
7979 bool seen_ptr = false;
7980 match m = MATCH_YES;
7982 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
7983 this case the defaults are in there. */
7984 ba->access = ACCESS_UNKNOWN;
7985 ba->pass_arg = NULL;
7986 ba->pass_arg_num = 0;
7987 ba->nopass = 0;
7988 ba->non_overridable = 0;
7989 ba->deferred = 0;
7990 ba->ppc = ppc;
7992 /* If we find a comma, we believe there are binding attributes. */
7993 m = gfc_match_char (',');
7994 if (m == MATCH_NO)
7995 goto done;
7999 /* Access specifier. */
8001 m = gfc_match (" public");
8002 if (m == MATCH_ERROR)
8003 goto error;
8004 if (m == MATCH_YES)
8006 if (ba->access != ACCESS_UNKNOWN)
8008 gfc_error ("Duplicate access-specifier at %C");
8009 goto error;
8012 ba->access = ACCESS_PUBLIC;
8013 continue;
8016 m = gfc_match (" private");
8017 if (m == MATCH_ERROR)
8018 goto error;
8019 if (m == MATCH_YES)
8021 if (ba->access != ACCESS_UNKNOWN)
8023 gfc_error ("Duplicate access-specifier at %C");
8024 goto error;
8027 ba->access = ACCESS_PRIVATE;
8028 continue;
8031 /* If inside GENERIC, the following is not allowed. */
8032 if (!generic)
8035 /* NOPASS flag. */
8036 m = gfc_match (" nopass");
8037 if (m == MATCH_ERROR)
8038 goto error;
8039 if (m == MATCH_YES)
8041 if (found_passing)
8043 gfc_error ("Binding attributes already specify passing,"
8044 " illegal NOPASS at %C");
8045 goto error;
8048 found_passing = true;
8049 ba->nopass = 1;
8050 continue;
8053 /* PASS possibly including argument. */
8054 m = gfc_match (" pass");
8055 if (m == MATCH_ERROR)
8056 goto error;
8057 if (m == MATCH_YES)
8059 char arg[GFC_MAX_SYMBOL_LEN + 1];
8061 if (found_passing)
8063 gfc_error ("Binding attributes already specify passing,"
8064 " illegal PASS at %C");
8065 goto error;
8068 m = gfc_match (" ( %n )", arg);
8069 if (m == MATCH_ERROR)
8070 goto error;
8071 if (m == MATCH_YES)
8072 ba->pass_arg = gfc_get_string (arg);
8073 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
8075 found_passing = true;
8076 ba->nopass = 0;
8077 continue;
8080 if (ppc)
8082 /* POINTER flag. */
8083 m = gfc_match (" pointer");
8084 if (m == MATCH_ERROR)
8085 goto error;
8086 if (m == MATCH_YES)
8088 if (seen_ptr)
8090 gfc_error ("Duplicate POINTER attribute at %C");
8091 goto error;
8094 seen_ptr = true;
8095 continue;
8098 else
8100 /* NON_OVERRIDABLE flag. */
8101 m = gfc_match (" non_overridable");
8102 if (m == MATCH_ERROR)
8103 goto error;
8104 if (m == MATCH_YES)
8106 if (ba->non_overridable)
8108 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8109 goto error;
8112 ba->non_overridable = 1;
8113 continue;
8116 /* DEFERRED flag. */
8117 m = gfc_match (" deferred");
8118 if (m == MATCH_ERROR)
8119 goto error;
8120 if (m == MATCH_YES)
8122 if (ba->deferred)
8124 gfc_error ("Duplicate DEFERRED at %C");
8125 goto error;
8128 ba->deferred = 1;
8129 continue;
8135 /* Nothing matching found. */
8136 if (generic)
8137 gfc_error ("Expected access-specifier at %C");
8138 else
8139 gfc_error ("Expected binding attribute at %C");
8140 goto error;
8142 while (gfc_match_char (',') == MATCH_YES);
8144 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8145 if (ba->non_overridable && ba->deferred)
8147 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8148 goto error;
8151 m = MATCH_YES;
8153 done:
8154 if (ba->access == ACCESS_UNKNOWN)
8155 ba->access = gfc_typebound_default_access;
8157 if (ppc && !seen_ptr)
8159 gfc_error ("POINTER attribute is required for procedure pointer component"
8160 " at %C");
8161 goto error;
8164 return m;
8166 error:
8167 return MATCH_ERROR;
8171 /* Match a PROCEDURE specific binding inside a derived type. */
8173 static match
8174 match_procedure_in_type (void)
8176 char name[GFC_MAX_SYMBOL_LEN + 1];
8177 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
8178 char* target = NULL, *ifc = NULL;
8179 gfc_typebound_proc tb;
8180 bool seen_colons;
8181 bool seen_attrs;
8182 match m;
8183 gfc_symtree* stree;
8184 gfc_namespace* ns;
8185 gfc_symbol* block;
8186 int num;
8188 /* Check current state. */
8189 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8190 block = gfc_state_stack->previous->sym;
8191 gcc_assert (block);
8193 /* Try to match PROCEDURE(interface). */
8194 if (gfc_match (" (") == MATCH_YES)
8196 m = gfc_match_name (target_buf);
8197 if (m == MATCH_ERROR)
8198 return m;
8199 if (m != MATCH_YES)
8201 gfc_error ("Interface-name expected after '(' at %C");
8202 return MATCH_ERROR;
8205 if (gfc_match (" )") != MATCH_YES)
8207 gfc_error ("')' expected at %C");
8208 return MATCH_ERROR;
8211 ifc = target_buf;
8214 /* Construct the data structure. */
8215 memset (&tb, 0, sizeof (tb));
8216 tb.where = gfc_current_locus;
8218 /* Match binding attributes. */
8219 m = match_binding_attributes (&tb, false, false);
8220 if (m == MATCH_ERROR)
8221 return m;
8222 seen_attrs = (m == MATCH_YES);
8224 /* Check that attribute DEFERRED is given if an interface is specified. */
8225 if (tb.deferred && !ifc)
8227 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8228 return MATCH_ERROR;
8230 if (ifc && !tb.deferred)
8232 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8233 return MATCH_ERROR;
8236 /* Match the colons. */
8237 m = gfc_match (" ::");
8238 if (m == MATCH_ERROR)
8239 return m;
8240 seen_colons = (m == MATCH_YES);
8241 if (seen_attrs && !seen_colons)
8243 gfc_error ("Expected '::' after binding-attributes at %C");
8244 return MATCH_ERROR;
8247 /* Match the binding names. */
8248 for(num=1;;num++)
8250 m = gfc_match_name (name);
8251 if (m == MATCH_ERROR)
8252 return m;
8253 if (m == MATCH_NO)
8255 gfc_error ("Expected binding name at %C");
8256 return MATCH_ERROR;
8259 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
8260 return MATCH_ERROR;
8262 /* Try to match the '=> target', if it's there. */
8263 target = ifc;
8264 m = gfc_match (" =>");
8265 if (m == MATCH_ERROR)
8266 return m;
8267 if (m == MATCH_YES)
8269 if (tb.deferred)
8271 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
8272 return MATCH_ERROR;
8275 if (!seen_colons)
8277 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
8278 " at %C");
8279 return MATCH_ERROR;
8282 m = gfc_match_name (target_buf);
8283 if (m == MATCH_ERROR)
8284 return m;
8285 if (m == MATCH_NO)
8287 gfc_error ("Expected binding target after '=>' at %C");
8288 return MATCH_ERROR;
8290 target = target_buf;
8293 /* If no target was found, it has the same name as the binding. */
8294 if (!target)
8295 target = name;
8297 /* Get the namespace to insert the symbols into. */
8298 ns = block->f2k_derived;
8299 gcc_assert (ns);
8301 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8302 if (tb.deferred && !block->attr.abstract)
8304 gfc_error ("Type '%s' containing DEFERRED binding at %C "
8305 "is not ABSTRACT", block->name);
8306 return MATCH_ERROR;
8309 /* See if we already have a binding with this name in the symtree which
8310 would be an error. If a GENERIC already targeted this binding, it may
8311 be already there but then typebound is still NULL. */
8312 stree = gfc_find_symtree (ns->tb_sym_root, name);
8313 if (stree && stree->n.tb)
8315 gfc_error ("There is already a procedure with binding name '%s' for "
8316 "the derived type '%s' at %C", name, block->name);
8317 return MATCH_ERROR;
8320 /* Insert it and set attributes. */
8322 if (!stree)
8324 stree = gfc_new_symtree (&ns->tb_sym_root, name);
8325 gcc_assert (stree);
8327 stree->n.tb = gfc_get_typebound_proc (&tb);
8329 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8330 false))
8331 return MATCH_ERROR;
8332 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
8334 if (gfc_match_eos () == MATCH_YES)
8335 return MATCH_YES;
8336 if (gfc_match_char (',') != MATCH_YES)
8337 goto syntax;
8340 syntax:
8341 gfc_error ("Syntax error in PROCEDURE statement at %C");
8342 return MATCH_ERROR;
8346 /* Match a GENERIC procedure binding inside a derived type. */
8348 match
8349 gfc_match_generic (void)
8351 char name[GFC_MAX_SYMBOL_LEN + 1];
8352 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
8353 gfc_symbol* block;
8354 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8355 gfc_typebound_proc* tb;
8356 gfc_namespace* ns;
8357 interface_type op_type;
8358 gfc_intrinsic_op op;
8359 match m;
8361 /* Check current state. */
8362 if (gfc_current_state () == COMP_DERIVED)
8364 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8365 return MATCH_ERROR;
8367 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8368 return MATCH_NO;
8369 block = gfc_state_stack->previous->sym;
8370 ns = block->f2k_derived;
8371 gcc_assert (block && ns);
8373 memset (&tbattr, 0, sizeof (tbattr));
8374 tbattr.where = gfc_current_locus;
8376 /* See if we get an access-specifier. */
8377 m = match_binding_attributes (&tbattr, true, false);
8378 if (m == MATCH_ERROR)
8379 goto error;
8381 /* Now the colons, those are required. */
8382 if (gfc_match (" ::") != MATCH_YES)
8384 gfc_error ("Expected '::' at %C");
8385 goto error;
8388 /* Match the binding name; depending on type (operator / generic) format
8389 it for future error messages into bind_name. */
8391 m = gfc_match_generic_spec (&op_type, name, &op);
8392 if (m == MATCH_ERROR)
8393 return MATCH_ERROR;
8394 if (m == MATCH_NO)
8396 gfc_error ("Expected generic name or operator descriptor at %C");
8397 goto error;
8400 switch (op_type)
8402 case INTERFACE_GENERIC:
8403 snprintf (bind_name, sizeof (bind_name), "%s", name);
8404 break;
8406 case INTERFACE_USER_OP:
8407 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8408 break;
8410 case INTERFACE_INTRINSIC_OP:
8411 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8412 gfc_op2string (op));
8413 break;
8415 default:
8416 gcc_unreachable ();
8419 /* Match the required =>. */
8420 if (gfc_match (" =>") != MATCH_YES)
8422 gfc_error ("Expected '=>' at %C");
8423 goto error;
8426 /* Try to find existing GENERIC binding with this name / for this operator;
8427 if there is something, check that it is another GENERIC and then extend
8428 it rather than building a new node. Otherwise, create it and put it
8429 at the right position. */
8431 switch (op_type)
8433 case INTERFACE_USER_OP:
8434 case INTERFACE_GENERIC:
8436 const bool is_op = (op_type == INTERFACE_USER_OP);
8437 gfc_symtree* st;
8439 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8440 if (st)
8442 tb = st->n.tb;
8443 gcc_assert (tb);
8445 else
8446 tb = NULL;
8448 break;
8451 case INTERFACE_INTRINSIC_OP:
8452 tb = ns->tb_op[op];
8453 break;
8455 default:
8456 gcc_unreachable ();
8459 if (tb)
8461 if (!tb->is_generic)
8463 gcc_assert (op_type == INTERFACE_GENERIC);
8464 gfc_error ("There's already a non-generic procedure with binding name"
8465 " '%s' for the derived type '%s' at %C",
8466 bind_name, block->name);
8467 goto error;
8470 if (tb->access != tbattr.access)
8472 gfc_error ("Binding at %C must have the same access as already"
8473 " defined binding '%s'", bind_name);
8474 goto error;
8477 else
8479 tb = gfc_get_typebound_proc (NULL);
8480 tb->where = gfc_current_locus;
8481 tb->access = tbattr.access;
8482 tb->is_generic = 1;
8483 tb->u.generic = NULL;
8485 switch (op_type)
8487 case INTERFACE_GENERIC:
8488 case INTERFACE_USER_OP:
8490 const bool is_op = (op_type == INTERFACE_USER_OP);
8491 gfc_symtree* st;
8493 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8494 name);
8495 gcc_assert (st);
8496 st->n.tb = tb;
8498 break;
8501 case INTERFACE_INTRINSIC_OP:
8502 ns->tb_op[op] = tb;
8503 break;
8505 default:
8506 gcc_unreachable ();
8510 /* Now, match all following names as specific targets. */
8513 gfc_symtree* target_st;
8514 gfc_tbp_generic* target;
8516 m = gfc_match_name (name);
8517 if (m == MATCH_ERROR)
8518 goto error;
8519 if (m == MATCH_NO)
8521 gfc_error ("Expected specific binding name at %C");
8522 goto error;
8525 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8527 /* See if this is a duplicate specification. */
8528 for (target = tb->u.generic; target; target = target->next)
8529 if (target_st == target->specific_st)
8531 gfc_error ("'%s' already defined as specific binding for the"
8532 " generic '%s' at %C", name, bind_name);
8533 goto error;
8536 target = gfc_get_tbp_generic ();
8537 target->specific_st = target_st;
8538 target->specific = NULL;
8539 target->next = tb->u.generic;
8540 target->is_operator = ((op_type == INTERFACE_USER_OP)
8541 || (op_type == INTERFACE_INTRINSIC_OP));
8542 tb->u.generic = target;
8544 while (gfc_match (" ,") == MATCH_YES);
8546 /* Here should be the end. */
8547 if (gfc_match_eos () != MATCH_YES)
8549 gfc_error ("Junk after GENERIC binding at %C");
8550 goto error;
8553 return MATCH_YES;
8555 error:
8556 return MATCH_ERROR;
8560 /* Match a FINAL declaration inside a derived type. */
8562 match
8563 gfc_match_final_decl (void)
8565 char name[GFC_MAX_SYMBOL_LEN + 1];
8566 gfc_symbol* sym;
8567 match m;
8568 gfc_namespace* module_ns;
8569 bool first, last;
8570 gfc_symbol* block;
8572 if (gfc_current_form == FORM_FREE)
8574 char c = gfc_peek_ascii_char ();
8575 if (!gfc_is_whitespace (c) && c != ':')
8576 return MATCH_NO;
8579 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8581 if (gfc_current_form == FORM_FIXED)
8582 return MATCH_NO;
8584 gfc_error ("FINAL declaration at %C must be inside a derived type "
8585 "CONTAINS section");
8586 return MATCH_ERROR;
8589 block = gfc_state_stack->previous->sym;
8590 gcc_assert (block);
8592 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8593 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8595 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8596 " specification part of a MODULE");
8597 return MATCH_ERROR;
8600 module_ns = gfc_current_ns;
8601 gcc_assert (module_ns);
8602 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8604 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8605 if (gfc_match (" ::") == MATCH_ERROR)
8606 return MATCH_ERROR;
8608 /* Match the sequence of procedure names. */
8609 first = true;
8610 last = false;
8613 gfc_finalizer* f;
8615 if (first && gfc_match_eos () == MATCH_YES)
8617 gfc_error ("Empty FINAL at %C");
8618 return MATCH_ERROR;
8621 m = gfc_match_name (name);
8622 if (m == MATCH_NO)
8624 gfc_error ("Expected module procedure name at %C");
8625 return MATCH_ERROR;
8627 else if (m != MATCH_YES)
8628 return MATCH_ERROR;
8630 if (gfc_match_eos () == MATCH_YES)
8631 last = true;
8632 if (!last && gfc_match_char (',') != MATCH_YES)
8634 gfc_error ("Expected ',' at %C");
8635 return MATCH_ERROR;
8638 if (gfc_get_symbol (name, module_ns, &sym))
8640 gfc_error ("Unknown procedure name \"%s\" at %C", name);
8641 return MATCH_ERROR;
8644 /* Mark the symbol as module procedure. */
8645 if (sym->attr.proc != PROC_MODULE
8646 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
8647 return MATCH_ERROR;
8649 /* Check if we already have this symbol in the list, this is an error. */
8650 for (f = block->f2k_derived->finalizers; f; f = f->next)
8651 if (f->proc_sym == sym)
8653 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8654 name);
8655 return MATCH_ERROR;
8658 /* Add this symbol to the list of finalizers. */
8659 gcc_assert (block->f2k_derived);
8660 ++sym->refs;
8661 f = XCNEW (gfc_finalizer);
8662 f->proc_sym = sym;
8663 f->proc_tree = NULL;
8664 f->where = gfc_current_locus;
8665 f->next = block->f2k_derived->finalizers;
8666 block->f2k_derived->finalizers = f;
8668 first = false;
8670 while (!last);
8672 return MATCH_YES;
8676 const ext_attr_t ext_attr_list[] = {
8677 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8678 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8679 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
8680 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
8681 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
8682 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
8683 { NULL, EXT_ATTR_LAST, NULL }
8686 /* Match a !GCC$ ATTRIBUTES statement of the form:
8687 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8688 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8690 TODO: We should support all GCC attributes using the same syntax for
8691 the attribute list, i.e. the list in C
8692 __attributes(( attribute-list ))
8693 matches then
8694 !GCC$ ATTRIBUTES attribute-list ::
8695 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8696 saved into a TREE.
8698 As there is absolutely no risk of confusion, we should never return
8699 MATCH_NO. */
8700 match
8701 gfc_match_gcc_attributes (void)
8703 symbol_attribute attr;
8704 char name[GFC_MAX_SYMBOL_LEN + 1];
8705 unsigned id;
8706 gfc_symbol *sym;
8707 match m;
8709 gfc_clear_attr (&attr);
8710 for(;;)
8712 char ch;
8714 if (gfc_match_name (name) != MATCH_YES)
8715 return MATCH_ERROR;
8717 for (id = 0; id < EXT_ATTR_LAST; id++)
8718 if (strcmp (name, ext_attr_list[id].name) == 0)
8719 break;
8721 if (id == EXT_ATTR_LAST)
8723 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8724 return MATCH_ERROR;
8727 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
8728 return MATCH_ERROR;
8730 gfc_gobble_whitespace ();
8731 ch = gfc_next_ascii_char ();
8732 if (ch == ':')
8734 /* This is the successful exit condition for the loop. */
8735 if (gfc_next_ascii_char () == ':')
8736 break;
8739 if (ch == ',')
8740 continue;
8742 goto syntax;
8745 if (gfc_match_eos () == MATCH_YES)
8746 goto syntax;
8748 for(;;)
8750 m = gfc_match_name (name);
8751 if (m != MATCH_YES)
8752 return m;
8754 if (find_special (name, &sym, true))
8755 return MATCH_ERROR;
8757 sym->attr.ext_attr |= attr.ext_attr;
8759 if (gfc_match_eos () == MATCH_YES)
8760 break;
8762 if (gfc_match_char (',') != MATCH_YES)
8763 goto syntax;
8766 return MATCH_YES;
8768 syntax:
8769 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
8770 return MATCH_ERROR;