2013-10-21 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / fortran / decl.c
blob9c9fd4ffbf0488ea1e2b2d1acae312c42a7eba08
1 /* Declaration statement matcher
2 Copyright (C) 2002-2013 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"
31 /* Macros to access allocate memory for gfc_data_variable,
32 gfc_data_value and gfc_data. */
33 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
34 #define gfc_get_data_value() XCNEW (gfc_data_value)
35 #define gfc_get_data() XCNEW (gfc_data)
38 static bool set_binding_label (const char **, const char *, int);
41 /* This flag is set if an old-style length selector is matched
42 during a type-declaration statement. */
44 static int old_char_selector;
46 /* When variables acquire types and attributes from a declaration
47 statement, they get them from the following static variables. The
48 first part of a declaration sets these variables and the second
49 part copies these into symbol structures. */
51 static gfc_typespec current_ts;
53 static symbol_attribute current_attr;
54 static gfc_array_spec *current_as;
55 static int colon_seen;
57 /* The current binding label (if any). */
58 static const char* curr_binding_label;
59 /* Need to know how many identifiers are on the current data declaration
60 line in case we're given the BIND(C) attribute with a NAME= specifier. */
61 static int num_idents_on_line;
62 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
63 can supply a name if the curr_binding_label is nil and NAME= was not. */
64 static int has_name_equals = 0;
66 /* Initializer of the previous enumerator. */
68 static gfc_expr *last_initializer;
70 /* History of all the enumerators is maintained, so that
71 kind values of all the enumerators could be updated depending
72 upon the maximum initialized value. */
74 typedef struct enumerator_history
76 gfc_symbol *sym;
77 gfc_expr *initializer;
78 struct enumerator_history *next;
80 enumerator_history;
82 /* Header of enum history chain. */
84 static enumerator_history *enum_history = NULL;
86 /* Pointer of enum history node containing largest initializer. */
88 static enumerator_history *max_enum = NULL;
90 /* gfc_new_block points to the symbol of a newly matched block. */
92 gfc_symbol *gfc_new_block;
94 bool gfc_matching_function;
97 /********************* DATA statement subroutines *********************/
99 static bool in_match_data = false;
101 bool
102 gfc_in_match_data (void)
104 return in_match_data;
107 static void
108 set_in_match_data (bool set_value)
110 in_match_data = set_value;
113 /* Free a gfc_data_variable structure and everything beneath it. */
115 static void
116 free_variable (gfc_data_variable *p)
118 gfc_data_variable *q;
120 for (; p; p = q)
122 q = p->next;
123 gfc_free_expr (p->expr);
124 gfc_free_iterator (&p->iter, 0);
125 free_variable (p->list);
126 free (p);
131 /* Free a gfc_data_value structure and everything beneath it. */
133 static void
134 free_value (gfc_data_value *p)
136 gfc_data_value *q;
138 for (; p; p = q)
140 q = p->next;
141 mpz_clear (p->repeat);
142 gfc_free_expr (p->expr);
143 free (p);
148 /* Free a list of gfc_data structures. */
150 void
151 gfc_free_data (gfc_data *p)
153 gfc_data *q;
155 for (; p; p = q)
157 q = p->next;
158 free_variable (p->var);
159 free_value (p->value);
160 free (p);
165 /* Free all data in a namespace. */
167 static void
168 gfc_free_data_all (gfc_namespace *ns)
170 gfc_data *d;
172 for (;ns->data;)
174 d = ns->data->next;
175 free (ns->data);
176 ns->data = d;
181 static match var_element (gfc_data_variable *);
183 /* Match a list of variables terminated by an iterator and a right
184 parenthesis. */
186 static match
187 var_list (gfc_data_variable *parent)
189 gfc_data_variable *tail, var;
190 match m;
192 m = var_element (&var);
193 if (m == MATCH_ERROR)
194 return MATCH_ERROR;
195 if (m == MATCH_NO)
196 goto syntax;
198 tail = gfc_get_data_variable ();
199 *tail = var;
201 parent->list = tail;
203 for (;;)
205 if (gfc_match_char (',') != MATCH_YES)
206 goto syntax;
208 m = gfc_match_iterator (&parent->iter, 1);
209 if (m == MATCH_YES)
210 break;
211 if (m == MATCH_ERROR)
212 return MATCH_ERROR;
214 m = var_element (&var);
215 if (m == MATCH_ERROR)
216 return MATCH_ERROR;
217 if (m == MATCH_NO)
218 goto syntax;
220 tail->next = gfc_get_data_variable ();
221 tail = tail->next;
223 *tail = var;
226 if (gfc_match_char (')') != MATCH_YES)
227 goto syntax;
228 return MATCH_YES;
230 syntax:
231 gfc_syntax_error (ST_DATA);
232 return MATCH_ERROR;
236 /* Match a single element in a data variable list, which can be a
237 variable-iterator list. */
239 static match
240 var_element (gfc_data_variable *new_var)
242 match m;
243 gfc_symbol *sym;
245 memset (new_var, 0, sizeof (gfc_data_variable));
247 if (gfc_match_char ('(') == MATCH_YES)
248 return var_list (new_var);
250 m = gfc_match_variable (&new_var->expr, 0);
251 if (m != MATCH_YES)
252 return m;
254 sym = new_var->expr->symtree->n.sym;
256 /* Symbol should already have an associated type. */
257 if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
258 return MATCH_ERROR;
260 if (!sym->attr.function && gfc_current_ns->parent
261 && gfc_current_ns->parent == sym->ns)
263 gfc_error ("Host associated variable '%s' may not be in the DATA "
264 "statement at %C", sym->name);
265 return MATCH_ERROR;
268 if (gfc_current_state () != COMP_BLOCK_DATA
269 && sym->attr.in_common
270 && !gfc_notify_std (GFC_STD_GNU, "initialization of "
271 "common block variable '%s' in DATA statement at %C",
272 sym->name))
273 return MATCH_ERROR;
275 if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
276 return MATCH_ERROR;
278 return MATCH_YES;
282 /* Match the top-level list of data variables. */
284 static match
285 top_var_list (gfc_data *d)
287 gfc_data_variable var, *tail, *new_var;
288 match m;
290 tail = NULL;
292 for (;;)
294 m = var_element (&var);
295 if (m == MATCH_NO)
296 goto syntax;
297 if (m == MATCH_ERROR)
298 return MATCH_ERROR;
300 new_var = gfc_get_data_variable ();
301 *new_var = var;
303 if (tail == NULL)
304 d->var = new_var;
305 else
306 tail->next = new_var;
308 tail = new_var;
310 if (gfc_match_char ('/') == MATCH_YES)
311 break;
312 if (gfc_match_char (',') != MATCH_YES)
313 goto syntax;
316 return MATCH_YES;
318 syntax:
319 gfc_syntax_error (ST_DATA);
320 gfc_free_data_all (gfc_current_ns);
321 return MATCH_ERROR;
325 static match
326 match_data_constant (gfc_expr **result)
328 char name[GFC_MAX_SYMBOL_LEN + 1];
329 gfc_symbol *sym, *dt_sym = NULL;
330 gfc_expr *expr;
331 match m;
332 locus old_loc;
334 m = gfc_match_literal_constant (&expr, 1);
335 if (m == MATCH_YES)
337 *result = expr;
338 return MATCH_YES;
341 if (m == MATCH_ERROR)
342 return MATCH_ERROR;
344 m = gfc_match_null (result);
345 if (m != MATCH_NO)
346 return m;
348 old_loc = gfc_current_locus;
350 /* Should this be a structure component, try to match it
351 before matching a name. */
352 m = gfc_match_rvalue (result);
353 if (m == MATCH_ERROR)
354 return m;
356 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
358 if (!gfc_simplify_expr (*result, 0))
359 m = MATCH_ERROR;
360 return m;
362 else if (m == MATCH_YES)
363 gfc_free_expr (*result);
365 gfc_current_locus = old_loc;
367 m = gfc_match_name (name);
368 if (m != MATCH_YES)
369 return m;
371 if (gfc_find_symbol (name, NULL, 1, &sym))
372 return MATCH_ERROR;
374 if (sym && sym->attr.generic)
375 dt_sym = gfc_find_dt_in_generic (sym);
377 if (sym == NULL
378 || (sym->attr.flavor != FL_PARAMETER
379 && (!dt_sym || dt_sym->attr.flavor != FL_DERIVED)))
381 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
382 name);
383 return MATCH_ERROR;
385 else if (dt_sym && dt_sym->attr.flavor == FL_DERIVED)
386 return gfc_match_structure_constructor (dt_sym, result);
388 /* Check to see if the value is an initialization array expression. */
389 if (sym->value->expr_type == EXPR_ARRAY)
391 gfc_current_locus = old_loc;
393 m = gfc_match_init_expr (result);
394 if (m == MATCH_ERROR)
395 return m;
397 if (m == MATCH_YES)
399 if (!gfc_simplify_expr (*result, 0))
400 m = MATCH_ERROR;
402 if ((*result)->expr_type == EXPR_CONSTANT)
403 return m;
404 else
406 gfc_error ("Invalid initializer %s in Data statement at %C", name);
407 return MATCH_ERROR;
412 *result = gfc_copy_expr (sym->value);
413 return MATCH_YES;
417 /* Match a list of values in a DATA statement. The leading '/' has
418 already been seen at this point. */
420 static match
421 top_val_list (gfc_data *data)
423 gfc_data_value *new_val, *tail;
424 gfc_expr *expr;
425 match m;
427 tail = NULL;
429 for (;;)
431 m = match_data_constant (&expr);
432 if (m == MATCH_NO)
433 goto syntax;
434 if (m == MATCH_ERROR)
435 return MATCH_ERROR;
437 new_val = gfc_get_data_value ();
438 mpz_init (new_val->repeat);
440 if (tail == NULL)
441 data->value = new_val;
442 else
443 tail->next = new_val;
445 tail = new_val;
447 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
449 tail->expr = expr;
450 mpz_set_ui (tail->repeat, 1);
452 else
454 mpz_set (tail->repeat, expr->value.integer);
455 gfc_free_expr (expr);
457 m = match_data_constant (&tail->expr);
458 if (m == MATCH_NO)
459 goto syntax;
460 if (m == MATCH_ERROR)
461 return MATCH_ERROR;
464 if (gfc_match_char ('/') == MATCH_YES)
465 break;
466 if (gfc_match_char (',') == MATCH_NO)
467 goto syntax;
470 return MATCH_YES;
472 syntax:
473 gfc_syntax_error (ST_DATA);
474 gfc_free_data_all (gfc_current_ns);
475 return MATCH_ERROR;
479 /* Matches an old style initialization. */
481 static match
482 match_old_style_init (const char *name)
484 match m;
485 gfc_symtree *st;
486 gfc_symbol *sym;
487 gfc_data *newdata;
489 /* Set up data structure to hold initializers. */
490 gfc_find_sym_tree (name, NULL, 0, &st);
491 sym = st->n.sym;
493 newdata = gfc_get_data ();
494 newdata->var = gfc_get_data_variable ();
495 newdata->var->expr = gfc_get_variable_expr (st);
496 newdata->where = gfc_current_locus;
498 /* Match initial value list. This also eats the terminal '/'. */
499 m = top_val_list (newdata);
500 if (m != MATCH_YES)
502 free (newdata);
503 return m;
506 if (gfc_pure (NULL))
508 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
509 free (newdata);
510 return MATCH_ERROR;
513 if (gfc_implicit_pure (NULL))
514 gfc_current_ns->proc_name->attr.implicit_pure = 0;
516 /* Mark the variable as having appeared in a data statement. */
517 if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
519 free (newdata);
520 return MATCH_ERROR;
523 /* Chain in namespace list of DATA initializers. */
524 newdata->next = gfc_current_ns->data;
525 gfc_current_ns->data = newdata;
527 return m;
531 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
532 we are matching a DATA statement and are therefore issuing an error
533 if we encounter something unexpected, if not, we're trying to match
534 an old-style initialization expression of the form INTEGER I /2/. */
536 match
537 gfc_match_data (void)
539 gfc_data *new_data;
540 match m;
542 set_in_match_data (true);
544 for (;;)
546 new_data = gfc_get_data ();
547 new_data->where = gfc_current_locus;
549 m = top_var_list (new_data);
550 if (m != MATCH_YES)
551 goto cleanup;
553 m = top_val_list (new_data);
554 if (m != MATCH_YES)
555 goto cleanup;
557 new_data->next = gfc_current_ns->data;
558 gfc_current_ns->data = new_data;
560 if (gfc_match_eos () == MATCH_YES)
561 break;
563 gfc_match_char (','); /* Optional comma */
566 set_in_match_data (false);
568 if (gfc_pure (NULL))
570 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
571 return MATCH_ERROR;
574 if (gfc_implicit_pure (NULL))
575 gfc_current_ns->proc_name->attr.implicit_pure = 0;
577 return MATCH_YES;
579 cleanup:
580 set_in_match_data (false);
581 gfc_free_data (new_data);
582 return MATCH_ERROR;
586 /************************ Declaration statements *********************/
589 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
591 static bool
592 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
594 int i;
596 if ((from->type == AS_ASSUMED_RANK && to->corank)
597 || (to->type == AS_ASSUMED_RANK && from->corank))
599 gfc_error ("The assumed-rank array at %C shall not have a codimension");
600 return false;
603 if (to->rank == 0 && from->rank > 0)
605 to->rank = from->rank;
606 to->type = from->type;
607 to->cray_pointee = from->cray_pointee;
608 to->cp_was_assumed = from->cp_was_assumed;
610 for (i = 0; i < to->corank; i++)
612 to->lower[from->rank + i] = to->lower[i];
613 to->upper[from->rank + i] = to->upper[i];
615 for (i = 0; i < from->rank; i++)
617 if (copy)
619 to->lower[i] = gfc_copy_expr (from->lower[i]);
620 to->upper[i] = gfc_copy_expr (from->upper[i]);
622 else
624 to->lower[i] = from->lower[i];
625 to->upper[i] = from->upper[i];
629 else if (to->corank == 0 && from->corank > 0)
631 to->corank = from->corank;
632 to->cotype = from->cotype;
634 for (i = 0; i < from->corank; i++)
636 if (copy)
638 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
639 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
641 else
643 to->lower[to->rank + i] = from->lower[i];
644 to->upper[to->rank + i] = from->upper[i];
649 return true;
653 /* Match an intent specification. Since this can only happen after an
654 INTENT word, a legal intent-spec must follow. */
656 static sym_intent
657 match_intent_spec (void)
660 if (gfc_match (" ( in out )") == MATCH_YES)
661 return INTENT_INOUT;
662 if (gfc_match (" ( in )") == MATCH_YES)
663 return INTENT_IN;
664 if (gfc_match (" ( out )") == MATCH_YES)
665 return INTENT_OUT;
667 gfc_error ("Bad INTENT specification at %C");
668 return INTENT_UNKNOWN;
672 /* Matches a character length specification, which is either a
673 specification expression, '*', or ':'. */
675 static match
676 char_len_param_value (gfc_expr **expr, bool *deferred)
678 match m;
680 *expr = NULL;
681 *deferred = false;
683 if (gfc_match_char ('*') == MATCH_YES)
684 return MATCH_YES;
686 if (gfc_match_char (':') == MATCH_YES)
688 if (!gfc_notify_std (GFC_STD_F2003, "deferred type "
689 "parameter at %C"))
690 return MATCH_ERROR;
692 *deferred = true;
694 return MATCH_YES;
697 m = gfc_match_expr (expr);
699 if (m == MATCH_YES
700 && !gfc_expr_check_typed (*expr, gfc_current_ns, false))
701 return MATCH_ERROR;
703 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
705 if ((*expr)->value.function.actual
706 && (*expr)->value.function.actual->expr->symtree)
708 gfc_expr *e;
709 e = (*expr)->value.function.actual->expr;
710 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
711 && e->expr_type == EXPR_VARIABLE)
713 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
714 goto syntax;
715 if (e->symtree->n.sym->ts.type == BT_CHARACTER
716 && e->symtree->n.sym->ts.u.cl
717 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
718 goto syntax;
722 return m;
724 syntax:
725 gfc_error ("Conflict in attributes of function argument at %C");
726 return MATCH_ERROR;
730 /* A character length is a '*' followed by a literal integer or a
731 char_len_param_value in parenthesis. */
733 static match
734 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
736 int length;
737 match m;
739 *deferred = false;
740 m = gfc_match_char ('*');
741 if (m != MATCH_YES)
742 return m;
744 m = gfc_match_small_literal_int (&length, NULL);
745 if (m == MATCH_ERROR)
746 return m;
748 if (m == MATCH_YES)
750 if (obsolescent_check
751 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
752 return MATCH_ERROR;
753 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
754 return m;
757 if (gfc_match_char ('(') == MATCH_NO)
758 goto syntax;
760 m = char_len_param_value (expr, deferred);
761 if (m != MATCH_YES && gfc_matching_function)
763 gfc_undo_symbols ();
764 m = MATCH_YES;
767 if (m == MATCH_ERROR)
768 return m;
769 if (m == MATCH_NO)
770 goto syntax;
772 if (gfc_match_char (')') == MATCH_NO)
774 gfc_free_expr (*expr);
775 *expr = NULL;
776 goto syntax;
779 return MATCH_YES;
781 syntax:
782 gfc_error ("Syntax error in character length specification at %C");
783 return MATCH_ERROR;
787 /* Special subroutine for finding a symbol. Check if the name is found
788 in the current name space. If not, and we're compiling a function or
789 subroutine and the parent compilation unit is an interface, then check
790 to see if the name we've been given is the name of the interface
791 (located in another namespace). */
793 static int
794 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
796 gfc_state_data *s;
797 gfc_symtree *st;
798 int i;
800 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
801 if (i == 0)
803 *result = st ? st->n.sym : NULL;
804 goto end;
807 if (gfc_current_state () != COMP_SUBROUTINE
808 && gfc_current_state () != COMP_FUNCTION)
809 goto end;
811 s = gfc_state_stack->previous;
812 if (s == NULL)
813 goto end;
815 if (s->state != COMP_INTERFACE)
816 goto end;
817 if (s->sym == NULL)
818 goto end; /* Nameless interface. */
820 if (strcmp (name, s->sym->name) == 0)
822 *result = s->sym;
823 return 0;
826 end:
827 return i;
831 /* Special subroutine for getting a symbol node associated with a
832 procedure name, used in SUBROUTINE and FUNCTION statements. The
833 symbol is created in the parent using with symtree node in the
834 child unit pointing to the symbol. If the current namespace has no
835 parent, then the symbol is just created in the current unit. */
837 static int
838 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
840 gfc_symtree *st;
841 gfc_symbol *sym;
842 int rc = 0;
844 /* Module functions have to be left in their own namespace because
845 they have potentially (almost certainly!) already been referenced.
846 In this sense, they are rather like external functions. This is
847 fixed up in resolve.c(resolve_entries), where the symbol name-
848 space is set to point to the master function, so that the fake
849 result mechanism can work. */
850 if (module_fcn_entry)
852 /* Present if entry is declared to be a module procedure. */
853 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
855 if (*result == NULL)
856 rc = gfc_get_symbol (name, NULL, result);
857 else if (!gfc_get_symbol (name, NULL, &sym) && sym
858 && (*result)->ts.type == BT_UNKNOWN
859 && sym->attr.flavor == FL_UNKNOWN)
860 /* Pick up the typespec for the entry, if declared in the function
861 body. Note that this symbol is FL_UNKNOWN because it will
862 only have appeared in a type declaration. The local symtree
863 is set to point to the module symbol and a unique symtree
864 to the local version. This latter ensures a correct clearing
865 of the symbols. */
867 /* If the ENTRY proceeds its specification, we need to ensure
868 that this does not raise a "has no IMPLICIT type" error. */
869 if (sym->ts.type == BT_UNKNOWN)
870 sym->attr.untyped = 1;
872 (*result)->ts = sym->ts;
874 /* Put the symbol in the procedure namespace so that, should
875 the ENTRY precede its specification, the specification
876 can be applied. */
877 (*result)->ns = gfc_current_ns;
879 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
880 st->n.sym = *result;
881 st = gfc_get_unique_symtree (gfc_current_ns);
882 st->n.sym = sym;
885 else
886 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
888 if (rc)
889 return rc;
891 sym = *result;
893 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
895 /* Trap another encompassed procedure with the same name. All
896 these conditions are necessary to avoid picking up an entry
897 whose name clashes with that of the encompassing procedure;
898 this is handled using gsymbols to register unique,globally
899 accessible names. */
900 if (sym->attr.flavor != 0
901 && sym->attr.proc != 0
902 && (sym->attr.subroutine || sym->attr.function)
903 && sym->attr.if_source != IFSRC_UNKNOWN)
904 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
905 name, &sym->declared_at);
907 /* Trap a procedure with a name the same as interface in the
908 encompassing scope. */
909 if (sym->attr.generic != 0
910 && (sym->attr.subroutine || sym->attr.function)
911 && !sym->attr.mod_proc)
912 gfc_error_now ("Name '%s' at %C is already defined"
913 " as a generic interface at %L",
914 name, &sym->declared_at);
916 /* Trap declarations of attributes in encompassing scope. The
917 signature for this is that ts.kind is set. Legitimate
918 references only set ts.type. */
919 if (sym->ts.kind != 0
920 && !sym->attr.implicit_type
921 && sym->attr.proc == 0
922 && gfc_current_ns->parent != NULL
923 && sym->attr.access == 0
924 && !module_fcn_entry)
925 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
926 "and must not have attributes declared at %L",
927 name, &sym->declared_at);
930 if (gfc_current_ns->parent == NULL || *result == NULL)
931 return rc;
933 /* Module function entries will already have a symtree in
934 the current namespace but will need one at module level. */
935 if (module_fcn_entry)
937 /* Present if entry is declared to be a module procedure. */
938 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
939 if (st == NULL)
940 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
942 else
943 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
945 st->n.sym = sym;
946 sym->refs++;
948 /* See if the procedure should be a module procedure. */
950 if (((sym->ns->proc_name != NULL
951 && sym->ns->proc_name->attr.flavor == FL_MODULE
952 && sym->attr.proc != PROC_MODULE)
953 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
954 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
955 rc = 2;
957 return rc;
961 /* Verify that the given symbol representing a parameter is C
962 interoperable, by checking to see if it was marked as such after
963 its declaration. If the given symbol is not interoperable, a
964 warning is reported, thus removing the need to return the status to
965 the calling function. The standard does not require the user use
966 one of the iso_c_binding named constants to declare an
967 interoperable parameter, but we can't be sure if the param is C
968 interop or not if the user doesn't. For example, integer(4) may be
969 legal Fortran, but doesn't have meaning in C. It may interop with
970 a number of the C types, which causes a problem because the
971 compiler can't know which one. This code is almost certainly not
972 portable, and the user will get what they deserve if the C type
973 across platforms isn't always interoperable with integer(4). If
974 the user had used something like integer(c_int) or integer(c_long),
975 the compiler could have automatically handled the varying sizes
976 across platforms. */
978 bool
979 gfc_verify_c_interop_param (gfc_symbol *sym)
981 int is_c_interop = 0;
982 bool retval = true;
984 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
985 Don't repeat the checks here. */
986 if (sym->attr.implicit_type)
987 return true;
989 /* For subroutines or functions that are passed to a BIND(C) procedure,
990 they're interoperable if they're BIND(C) and their params are all
991 interoperable. */
992 if (sym->attr.flavor == FL_PROCEDURE)
994 if (sym->attr.is_bind_c == 0)
996 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
997 "attribute to be C interoperable", sym->name,
998 &(sym->declared_at));
1000 return false;
1002 else
1004 if (sym->attr.is_c_interop == 1)
1005 /* We've already checked this procedure; don't check it again. */
1006 return true;
1007 else
1008 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1009 sym->common_block);
1013 /* See if we've stored a reference to a procedure that owns sym. */
1014 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1016 if (sym->ns->proc_name->attr.is_bind_c == 1)
1018 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1020 if (is_c_interop != 1)
1022 /* Make personalized messages to give better feedback. */
1023 if (sym->ts.type == BT_DERIVED)
1024 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1025 "BIND(C) procedure '%s' but is not C interoperable "
1026 "because derived type '%s' is not C interoperable",
1027 sym->name, &(sym->declared_at),
1028 sym->ns->proc_name->name,
1029 sym->ts.u.derived->name);
1030 else if (sym->ts.type == BT_CLASS)
1031 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1032 "BIND(C) procedure '%s' but is not C interoperable "
1033 "because it is polymorphic",
1034 sym->name, &(sym->declared_at),
1035 sym->ns->proc_name->name);
1036 else if (gfc_option.warn_c_binding_type)
1037 gfc_warning ("Variable '%s' at %L is a dummy argument of the "
1038 "BIND(C) procedure '%s' but may not be C "
1039 "interoperable",
1040 sym->name, &(sym->declared_at),
1041 sym->ns->proc_name->name);
1044 /* Character strings are only C interoperable if they have a
1045 length of 1. */
1046 if (sym->ts.type == BT_CHARACTER)
1048 gfc_charlen *cl = sym->ts.u.cl;
1049 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1050 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1052 gfc_error ("Character argument '%s' at %L "
1053 "must be length 1 because "
1054 "procedure '%s' is BIND(C)",
1055 sym->name, &sym->declared_at,
1056 sym->ns->proc_name->name);
1057 retval = false;
1061 /* We have to make sure that any param to a bind(c) routine does
1062 not have the allocatable, pointer, or optional attributes,
1063 according to J3/04-007, section 5.1. */
1064 if (sym->attr.allocatable == 1
1065 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable '%s' at %L with "
1066 "ALLOCATABLE attribute in procedure '%s' "
1067 "with BIND(C)", sym->name,
1068 &(sym->declared_at),
1069 sym->ns->proc_name->name))
1070 retval = false;
1072 if (sym->attr.pointer == 1
1073 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable '%s' at %L with "
1074 "POINTER attribute in procedure '%s' "
1075 "with BIND(C)", sym->name,
1076 &(sym->declared_at),
1077 sym->ns->proc_name->name))
1078 retval = false;
1080 if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as)
1082 gfc_error ("Scalar variable '%s' at %L with POINTER or "
1083 "ALLOCATABLE in procedure '%s' with BIND(C) is not yet"
1084 " supported", sym->name, &(sym->declared_at),
1085 sym->ns->proc_name->name);
1086 retval = false;
1089 if (sym->attr.optional == 1 && sym->attr.value)
1091 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1092 "and the VALUE attribute because procedure '%s' "
1093 "is BIND(C)", sym->name, &(sym->declared_at),
1094 sym->ns->proc_name->name);
1095 retval = false;
1097 else if (sym->attr.optional == 1
1098 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable '%s' "
1099 "at %L with OPTIONAL attribute in "
1100 "procedure '%s' which is BIND(C)",
1101 sym->name, &(sym->declared_at),
1102 sym->ns->proc_name->name))
1103 retval = false;
1105 /* Make sure that if it has the dimension attribute, that it is
1106 either assumed size or explicit shape. Deferred shape is already
1107 covered by the pointer/allocatable attribute. */
1108 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1109 && !gfc_notify_std (GFC_STD_F2008_TS, "Assumed-shape array '%s' "
1110 "at %L as dummy argument to the BIND(C) "
1111 "procedure '%s' at %L", sym->name,
1112 &(sym->declared_at),
1113 sym->ns->proc_name->name,
1114 &(sym->ns->proc_name->declared_at)))
1115 retval = false;
1119 return retval;
1124 /* Function called by variable_decl() that adds a name to the symbol table. */
1126 static bool
1127 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1128 gfc_array_spec **as, locus *var_locus)
1130 symbol_attribute attr;
1131 gfc_symbol *sym;
1133 if (gfc_get_symbol (name, NULL, &sym))
1134 return false;
1136 /* Start updating the symbol table. Add basic type attribute if present. */
1137 if (current_ts.type != BT_UNKNOWN
1138 && (sym->attr.implicit_type == 0
1139 || !gfc_compare_types (&sym->ts, &current_ts))
1140 && !gfc_add_type (sym, &current_ts, var_locus))
1141 return false;
1143 if (sym->ts.type == BT_CHARACTER)
1145 sym->ts.u.cl = cl;
1146 sym->ts.deferred = cl_deferred;
1149 /* Add dimension attribute if present. */
1150 if (!gfc_set_array_spec (sym, *as, var_locus))
1151 return false;
1152 *as = NULL;
1154 /* Add attribute to symbol. The copy is so that we can reset the
1155 dimension attribute. */
1156 attr = current_attr;
1157 attr.dimension = 0;
1158 attr.codimension = 0;
1160 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1161 return false;
1163 /* Finish any work that may need to be done for the binding label,
1164 if it's a bind(c). The bind(c) attr is found before the symbol
1165 is made, and before the symbol name (for data decls), so the
1166 current_ts is holding the binding label, or nothing if the
1167 name= attr wasn't given. Therefore, test here if we're dealing
1168 with a bind(c) and make sure the binding label is set correctly. */
1169 if (sym->attr.is_bind_c == 1)
1171 if (!sym->binding_label)
1173 /* Set the binding label and verify that if a NAME= was specified
1174 then only one identifier was in the entity-decl-list. */
1175 if (!set_binding_label (&sym->binding_label, sym->name,
1176 num_idents_on_line))
1177 return false;
1181 /* See if we know we're in a common block, and if it's a bind(c)
1182 common then we need to make sure we're an interoperable type. */
1183 if (sym->attr.in_common == 1)
1185 /* Test the common block object. */
1186 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1187 && sym->ts.is_c_interop != 1)
1189 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1190 "must be declared with a C interoperable "
1191 "kind since common block '%s' is BIND(C)",
1192 sym->name, sym->common_block->name,
1193 sym->common_block->name);
1194 gfc_clear_error ();
1198 sym->attr.implied_index = 0;
1200 if (sym->ts.type == BT_CLASS)
1201 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1203 return true;
1207 /* Set character constant to the given length. The constant will be padded or
1208 truncated. If we're inside an array constructor without a typespec, we
1209 additionally check that all elements have the same length; check_len -1
1210 means no checking. */
1212 void
1213 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1215 gfc_char_t *s;
1216 int slen;
1218 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1219 gcc_assert (expr->ts.type == BT_CHARACTER);
1221 slen = expr->value.character.length;
1222 if (len != slen)
1224 s = gfc_get_wide_string (len + 1);
1225 memcpy (s, expr->value.character.string,
1226 MIN (len, slen) * sizeof (gfc_char_t));
1227 if (len > slen)
1228 gfc_wide_memset (&s[slen], ' ', len - slen);
1230 if (gfc_option.warn_character_truncation && slen > len)
1231 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1232 "(%d/%d)", &expr->where, slen, len);
1234 /* Apply the standard by 'hand' otherwise it gets cleared for
1235 initializers. */
1236 if (check_len != -1 && slen != check_len
1237 && !(gfc_option.allow_std & GFC_STD_GNU))
1238 gfc_error_now ("The CHARACTER elements of the array constructor "
1239 "at %L must have the same length (%d/%d)",
1240 &expr->where, slen, check_len);
1242 s[len] = '\0';
1243 free (expr->value.character.string);
1244 expr->value.character.string = s;
1245 expr->value.character.length = len;
1250 /* Function to create and update the enumerator history
1251 using the information passed as arguments.
1252 Pointer "max_enum" is also updated, to point to
1253 enum history node containing largest initializer.
1255 SYM points to the symbol node of enumerator.
1256 INIT points to its enumerator value. */
1258 static void
1259 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1261 enumerator_history *new_enum_history;
1262 gcc_assert (sym != NULL && init != NULL);
1264 new_enum_history = XCNEW (enumerator_history);
1266 new_enum_history->sym = sym;
1267 new_enum_history->initializer = init;
1268 new_enum_history->next = NULL;
1270 if (enum_history == NULL)
1272 enum_history = new_enum_history;
1273 max_enum = enum_history;
1275 else
1277 new_enum_history->next = enum_history;
1278 enum_history = new_enum_history;
1280 if (mpz_cmp (max_enum->initializer->value.integer,
1281 new_enum_history->initializer->value.integer) < 0)
1282 max_enum = new_enum_history;
1287 /* Function to free enum kind history. */
1289 void
1290 gfc_free_enum_history (void)
1292 enumerator_history *current = enum_history;
1293 enumerator_history *next;
1295 while (current != NULL)
1297 next = current->next;
1298 free (current);
1299 current = next;
1301 max_enum = NULL;
1302 enum_history = NULL;
1306 /* Function called by variable_decl() that adds an initialization
1307 expression to a symbol. */
1309 static bool
1310 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1312 symbol_attribute attr;
1313 gfc_symbol *sym;
1314 gfc_expr *init;
1316 init = *initp;
1317 if (find_special (name, &sym, false))
1318 return false;
1320 attr = sym->attr;
1322 /* If this symbol is confirming an implicit parameter type,
1323 then an initialization expression is not allowed. */
1324 if (attr.flavor == FL_PARAMETER
1325 && sym->value != NULL
1326 && *initp != NULL)
1328 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1329 sym->name);
1330 return false;
1333 if (init == NULL)
1335 /* An initializer is required for PARAMETER declarations. */
1336 if (attr.flavor == FL_PARAMETER)
1338 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1339 return false;
1342 else
1344 /* If a variable appears in a DATA block, it cannot have an
1345 initializer. */
1346 if (sym->attr.data)
1348 gfc_error ("Variable '%s' at %C with an initializer already "
1349 "appears in a DATA statement", sym->name);
1350 return false;
1353 /* Check if the assignment can happen. This has to be put off
1354 until later for derived type variables and procedure pointers. */
1355 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1356 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1357 && !sym->attr.proc_pointer
1358 && !gfc_check_assign_symbol (sym, NULL, init))
1359 return false;
1361 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1362 && init->ts.type == BT_CHARACTER)
1364 /* Update symbol character length according initializer. */
1365 if (!gfc_check_assign_symbol (sym, NULL, init))
1366 return false;
1368 if (sym->ts.u.cl->length == NULL)
1370 int clen;
1371 /* If there are multiple CHARACTER variables declared on the
1372 same line, we don't want them to share the same length. */
1373 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1375 if (sym->attr.flavor == FL_PARAMETER)
1377 if (init->expr_type == EXPR_CONSTANT)
1379 clen = init->value.character.length;
1380 sym->ts.u.cl->length
1381 = gfc_get_int_expr (gfc_default_integer_kind,
1382 NULL, clen);
1384 else if (init->expr_type == EXPR_ARRAY)
1386 gfc_constructor *c;
1387 c = gfc_constructor_first (init->value.constructor);
1388 clen = c->expr->value.character.length;
1389 sym->ts.u.cl->length
1390 = gfc_get_int_expr (gfc_default_integer_kind,
1391 NULL, clen);
1393 else if (init->ts.u.cl && init->ts.u.cl->length)
1394 sym->ts.u.cl->length =
1395 gfc_copy_expr (sym->value->ts.u.cl->length);
1398 /* Update initializer character length according symbol. */
1399 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1401 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1403 if (init->expr_type == EXPR_CONSTANT)
1404 gfc_set_constant_character_len (len, init, -1);
1405 else if (init->expr_type == EXPR_ARRAY)
1407 gfc_constructor *c;
1409 /* Build a new charlen to prevent simplification from
1410 deleting the length before it is resolved. */
1411 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1412 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1414 for (c = gfc_constructor_first (init->value.constructor);
1415 c; c = gfc_constructor_next (c))
1416 gfc_set_constant_character_len (len, c->expr, -1);
1421 /* If sym is implied-shape, set its upper bounds from init. */
1422 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1423 && sym->as->type == AS_IMPLIED_SHAPE)
1425 int dim;
1427 if (init->rank == 0)
1429 gfc_error ("Can't initialize implied-shape array at %L"
1430 " with scalar", &sym->declared_at);
1431 return false;
1433 gcc_assert (sym->as->rank == init->rank);
1435 /* Shape should be present, we get an initialization expression. */
1436 gcc_assert (init->shape);
1438 for (dim = 0; dim < sym->as->rank; ++dim)
1440 int k;
1441 gfc_expr* lower;
1442 gfc_expr* e;
1444 lower = sym->as->lower[dim];
1445 if (lower->expr_type != EXPR_CONSTANT)
1447 gfc_error ("Non-constant lower bound in implied-shape"
1448 " declaration at %L", &lower->where);
1449 return false;
1452 /* All dimensions must be without upper bound. */
1453 gcc_assert (!sym->as->upper[dim]);
1455 k = lower->ts.kind;
1456 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1457 mpz_add (e->value.integer,
1458 lower->value.integer, init->shape[dim]);
1459 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1460 sym->as->upper[dim] = e;
1463 sym->as->type = AS_EXPLICIT;
1466 /* Need to check if the expression we initialized this
1467 to was one of the iso_c_binding named constants. If so,
1468 and we're a parameter (constant), let it be iso_c.
1469 For example:
1470 integer(c_int), parameter :: my_int = c_int
1471 integer(my_int) :: my_int_2
1472 If we mark my_int as iso_c (since we can see it's value
1473 is equal to one of the named constants), then my_int_2
1474 will be considered C interoperable. */
1475 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1477 sym->ts.is_iso_c |= init->ts.is_iso_c;
1478 sym->ts.is_c_interop |= init->ts.is_c_interop;
1479 /* attr bits needed for module files. */
1480 sym->attr.is_iso_c |= init->ts.is_iso_c;
1481 sym->attr.is_c_interop |= init->ts.is_c_interop;
1482 if (init->ts.is_iso_c)
1483 sym->ts.f90_type = init->ts.f90_type;
1486 /* Add initializer. Make sure we keep the ranks sane. */
1487 if (sym->attr.dimension && init->rank == 0)
1489 mpz_t size;
1490 gfc_expr *array;
1491 int n;
1492 if (sym->attr.flavor == FL_PARAMETER
1493 && init->expr_type == EXPR_CONSTANT
1494 && spec_size (sym->as, &size)
1495 && mpz_cmp_si (size, 0) > 0)
1497 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1498 &init->where);
1499 for (n = 0; n < (int)mpz_get_si (size); n++)
1500 gfc_constructor_append_expr (&array->value.constructor,
1501 n == 0
1502 ? init
1503 : gfc_copy_expr (init),
1504 &init->where);
1506 array->shape = gfc_get_shape (sym->as->rank);
1507 for (n = 0; n < sym->as->rank; n++)
1508 spec_dimen_size (sym->as, n, &array->shape[n]);
1510 init = array;
1511 mpz_clear (size);
1513 init->rank = sym->as->rank;
1516 sym->value = init;
1517 if (sym->attr.save == SAVE_NONE)
1518 sym->attr.save = SAVE_IMPLICIT;
1519 *initp = NULL;
1522 return true;
1526 /* Function called by variable_decl() that adds a name to a structure
1527 being built. */
1529 static bool
1530 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1531 gfc_array_spec **as)
1533 gfc_component *c;
1534 bool t = true;
1536 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1537 constructing, it must have the pointer attribute. */
1538 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1539 && current_ts.u.derived == gfc_current_block ()
1540 && current_attr.pointer == 0)
1542 gfc_error ("Component at %C must have the POINTER attribute");
1543 return false;
1546 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1548 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1550 gfc_error ("Array component of structure at %C must have explicit "
1551 "or deferred shape");
1552 return false;
1556 if (!gfc_add_component (gfc_current_block(), name, &c))
1557 return false;
1559 c->ts = current_ts;
1560 if (c->ts.type == BT_CHARACTER)
1561 c->ts.u.cl = cl;
1562 c->attr = current_attr;
1564 c->initializer = *init;
1565 *init = NULL;
1567 c->as = *as;
1568 if (c->as != NULL)
1570 if (c->as->corank)
1571 c->attr.codimension = 1;
1572 if (c->as->rank)
1573 c->attr.dimension = 1;
1575 *as = NULL;
1577 /* Should this ever get more complicated, combine with similar section
1578 in add_init_expr_to_sym into a separate function. */
1579 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1580 && c->ts.u.cl
1581 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1583 int len;
1585 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1586 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1587 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1589 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1591 if (c->initializer->expr_type == EXPR_CONSTANT)
1592 gfc_set_constant_character_len (len, c->initializer, -1);
1593 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1594 c->initializer->ts.u.cl->length->value.integer))
1596 gfc_constructor *ctor;
1597 ctor = gfc_constructor_first (c->initializer->value.constructor);
1599 if (ctor)
1601 int first_len;
1602 bool has_ts = (c->initializer->ts.u.cl
1603 && c->initializer->ts.u.cl->length_from_typespec);
1605 /* Remember the length of the first element for checking
1606 that all elements *in the constructor* have the same
1607 length. This need not be the length of the LHS! */
1608 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1609 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1610 first_len = ctor->expr->value.character.length;
1612 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1613 if (ctor->expr->expr_type == EXPR_CONSTANT)
1615 gfc_set_constant_character_len (len, ctor->expr,
1616 has_ts ? -1 : first_len);
1617 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1623 /* Check array components. */
1624 if (!c->attr.dimension)
1625 goto scalar;
1627 if (c->attr.pointer)
1629 if (c->as->type != AS_DEFERRED)
1631 gfc_error ("Pointer array component of structure at %C must have a "
1632 "deferred shape");
1633 t = false;
1636 else if (c->attr.allocatable)
1638 if (c->as->type != AS_DEFERRED)
1640 gfc_error ("Allocatable component of structure at %C must have a "
1641 "deferred shape");
1642 t = false;
1645 else
1647 if (c->as->type != AS_EXPLICIT)
1649 gfc_error ("Array component of structure at %C must have an "
1650 "explicit shape");
1651 t = false;
1655 scalar:
1656 if (c->ts.type == BT_CLASS)
1658 bool delayed = (gfc_state_stack->sym == c->ts.u.derived)
1659 || (!c->ts.u.derived->components
1660 && !c->ts.u.derived->attr.zero_comp);
1661 bool t2 = gfc_build_class_symbol (&c->ts, &c->attr, &c->as, delayed);
1663 if (t)
1664 t = t2;
1667 return t;
1671 /* Match a 'NULL()', and possibly take care of some side effects. */
1673 match
1674 gfc_match_null (gfc_expr **result)
1676 gfc_symbol *sym;
1677 match m, m2 = MATCH_NO;
1679 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
1680 return MATCH_ERROR;
1682 if (m == MATCH_NO)
1684 locus old_loc;
1685 char name[GFC_MAX_SYMBOL_LEN + 1];
1687 if ((m2 = gfc_match (" null (")) != MATCH_YES)
1688 return m2;
1690 old_loc = gfc_current_locus;
1691 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
1692 return MATCH_ERROR;
1693 if (m2 != MATCH_YES
1694 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
1695 return MATCH_ERROR;
1696 if (m2 == MATCH_NO)
1698 gfc_current_locus = old_loc;
1699 return MATCH_NO;
1703 /* The NULL symbol now has to be/become an intrinsic function. */
1704 if (gfc_get_symbol ("null", NULL, &sym))
1706 gfc_error ("NULL() initialization at %C is ambiguous");
1707 return MATCH_ERROR;
1710 gfc_intrinsic_symbol (sym);
1712 if (sym->attr.proc != PROC_INTRINSIC
1713 && !(sym->attr.use_assoc && sym->attr.intrinsic)
1714 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
1715 || !gfc_add_function (&sym->attr, sym->name, NULL)))
1716 return MATCH_ERROR;
1718 *result = gfc_get_null_expr (&gfc_current_locus);
1720 /* Invalid per F2008, C512. */
1721 if (m2 == MATCH_YES)
1723 gfc_error ("NULL() initialization at %C may not have MOLD");
1724 return MATCH_ERROR;
1727 return MATCH_YES;
1731 /* Match the initialization expr for a data pointer or procedure pointer. */
1733 static match
1734 match_pointer_init (gfc_expr **init, int procptr)
1736 match m;
1738 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1740 gfc_error ("Initialization of pointer at %C is not allowed in "
1741 "a PURE procedure");
1742 return MATCH_ERROR;
1745 /* Match NULL() initialization. */
1746 m = gfc_match_null (init);
1747 if (m != MATCH_NO)
1748 return m;
1750 /* Match non-NULL initialization. */
1751 gfc_matching_ptr_assignment = !procptr;
1752 gfc_matching_procptr_assignment = procptr;
1753 m = gfc_match_rvalue (init);
1754 gfc_matching_ptr_assignment = 0;
1755 gfc_matching_procptr_assignment = 0;
1756 if (m == MATCH_ERROR)
1757 return MATCH_ERROR;
1758 else if (m == MATCH_NO)
1760 gfc_error ("Error in pointer initialization at %C");
1761 return MATCH_ERROR;
1764 if (!procptr)
1765 gfc_resolve_expr (*init);
1767 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
1768 "initialization at %C"))
1769 return MATCH_ERROR;
1771 return MATCH_YES;
1775 static bool
1776 check_function_name (char *name)
1778 /* In functions that have a RESULT variable defined, the function name always
1779 refers to function calls. Therefore, the name is not allowed to appear in
1780 specification statements. When checking this, be careful about
1781 'hidden' procedure pointer results ('ppr@'). */
1783 if (gfc_current_state () == COMP_FUNCTION)
1785 gfc_symbol *block = gfc_current_block ();
1786 if (block && block->result && block->result != block
1787 && strcmp (block->result->name, "ppr@") != 0
1788 && strcmp (block->name, name) == 0)
1790 gfc_error ("Function name '%s' not allowed at %C", name);
1791 return false;
1795 return true;
1799 /* Match a variable name with an optional initializer. When this
1800 subroutine is called, a variable is expected to be parsed next.
1801 Depending on what is happening at the moment, updates either the
1802 symbol table or the current interface. */
1804 static match
1805 variable_decl (int elem)
1807 char name[GFC_MAX_SYMBOL_LEN + 1];
1808 gfc_expr *initializer, *char_len;
1809 gfc_array_spec *as;
1810 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1811 gfc_charlen *cl;
1812 bool cl_deferred;
1813 locus var_locus;
1814 match m;
1815 bool t;
1816 gfc_symbol *sym;
1818 initializer = NULL;
1819 as = NULL;
1820 cp_as = NULL;
1822 /* When we get here, we've just matched a list of attributes and
1823 maybe a type and a double colon. The next thing we expect to see
1824 is the name of the symbol. */
1825 m = gfc_match_name (name);
1826 if (m != MATCH_YES)
1827 goto cleanup;
1829 var_locus = gfc_current_locus;
1831 /* Now we could see the optional array spec. or character length. */
1832 m = gfc_match_array_spec (&as, true, true);
1833 if (m == MATCH_ERROR)
1834 goto cleanup;
1836 if (m == MATCH_NO)
1837 as = gfc_copy_array_spec (current_as);
1838 else if (current_as
1839 && !merge_array_spec (current_as, as, true))
1841 m = MATCH_ERROR;
1842 goto cleanup;
1845 if (gfc_option.flag_cray_pointer)
1846 cp_as = gfc_copy_array_spec (as);
1848 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1849 determine (and check) whether it can be implied-shape. If it
1850 was parsed as assumed-size, change it because PARAMETERs can not
1851 be assumed-size. */
1852 if (as)
1854 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1856 m = MATCH_ERROR;
1857 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1858 name, &var_locus);
1859 goto cleanup;
1862 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1863 && current_attr.flavor == FL_PARAMETER)
1864 as->type = AS_IMPLIED_SHAPE;
1866 if (as->type == AS_IMPLIED_SHAPE
1867 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
1868 &var_locus))
1870 m = MATCH_ERROR;
1871 goto cleanup;
1875 char_len = NULL;
1876 cl = NULL;
1877 cl_deferred = false;
1879 if (current_ts.type == BT_CHARACTER)
1881 switch (match_char_length (&char_len, &cl_deferred, false))
1883 case MATCH_YES:
1884 cl = gfc_new_charlen (gfc_current_ns, NULL);
1886 cl->length = char_len;
1887 break;
1889 /* Non-constant lengths need to be copied after the first
1890 element. Also copy assumed lengths. */
1891 case MATCH_NO:
1892 if (elem > 1
1893 && (current_ts.u.cl->length == NULL
1894 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1896 cl = gfc_new_charlen (gfc_current_ns, NULL);
1897 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1899 else
1900 cl = current_ts.u.cl;
1902 cl_deferred = current_ts.deferred;
1904 break;
1906 case MATCH_ERROR:
1907 goto cleanup;
1911 /* If this symbol has already shown up in a Cray Pointer declaration,
1912 then we want to set the type & bail out. */
1913 if (gfc_option.flag_cray_pointer)
1915 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1916 if (sym != NULL && sym->attr.cray_pointee)
1918 sym->ts.type = current_ts.type;
1919 sym->ts.kind = current_ts.kind;
1920 sym->ts.u.cl = cl;
1921 sym->ts.u.derived = current_ts.u.derived;
1922 sym->ts.is_c_interop = current_ts.is_c_interop;
1923 sym->ts.is_iso_c = current_ts.is_iso_c;
1924 m = MATCH_YES;
1926 /* Check to see if we have an array specification. */
1927 if (cp_as != NULL)
1929 if (sym->as != NULL)
1931 gfc_error ("Duplicate array spec for Cray pointee at %C");
1932 gfc_free_array_spec (cp_as);
1933 m = MATCH_ERROR;
1934 goto cleanup;
1936 else
1938 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
1939 gfc_internal_error ("Couldn't set pointee array spec.");
1941 /* Fix the array spec. */
1942 m = gfc_mod_pointee_as (sym->as);
1943 if (m == MATCH_ERROR)
1944 goto cleanup;
1947 goto cleanup;
1949 else
1951 gfc_free_array_spec (cp_as);
1955 /* Procedure pointer as function result. */
1956 if (gfc_current_state () == COMP_FUNCTION
1957 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1958 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1959 strcpy (name, "ppr@");
1961 if (gfc_current_state () == COMP_FUNCTION
1962 && strcmp (name, gfc_current_block ()->name) == 0
1963 && gfc_current_block ()->result
1964 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1965 strcpy (name, "ppr@");
1967 /* OK, we've successfully matched the declaration. Now put the
1968 symbol in the current namespace, because it might be used in the
1969 optional initialization expression for this symbol, e.g. this is
1970 perfectly legal:
1972 integer, parameter :: i = huge(i)
1974 This is only true for parameters or variables of a basic type.
1975 For components of derived types, it is not true, so we don't
1976 create a symbol for those yet. If we fail to create the symbol,
1977 bail out. */
1978 if (gfc_current_state () != COMP_DERIVED
1979 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
1981 m = MATCH_ERROR;
1982 goto cleanup;
1985 if (!check_function_name (name))
1987 m = MATCH_ERROR;
1988 goto cleanup;
1991 /* We allow old-style initializations of the form
1992 integer i /2/, j(4) /3*3, 1/
1993 (if no colon has been seen). These are different from data
1994 statements in that initializers are only allowed to apply to the
1995 variable immediately preceding, i.e.
1996 integer i, j /1, 2/
1997 is not allowed. Therefore we have to do some work manually, that
1998 could otherwise be left to the matchers for DATA statements. */
2000 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2002 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2003 "initialization at %C"))
2004 return MATCH_ERROR;
2006 return match_old_style_init (name);
2009 /* The double colon must be present in order to have initializers.
2010 Otherwise the statement is ambiguous with an assignment statement. */
2011 if (colon_seen)
2013 if (gfc_match (" =>") == MATCH_YES)
2015 if (!current_attr.pointer)
2017 gfc_error ("Initialization at %C isn't for a pointer variable");
2018 m = MATCH_ERROR;
2019 goto cleanup;
2022 m = match_pointer_init (&initializer, 0);
2023 if (m != MATCH_YES)
2024 goto cleanup;
2026 else if (gfc_match_char ('=') == MATCH_YES)
2028 if (current_attr.pointer)
2030 gfc_error ("Pointer initialization at %C requires '=>', "
2031 "not '='");
2032 m = MATCH_ERROR;
2033 goto cleanup;
2036 m = gfc_match_init_expr (&initializer);
2037 if (m == MATCH_NO)
2039 gfc_error ("Expected an initialization expression at %C");
2040 m = MATCH_ERROR;
2043 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2044 && gfc_state_stack->state != COMP_DERIVED)
2046 gfc_error ("Initialization of variable at %C is not allowed in "
2047 "a PURE procedure");
2048 m = MATCH_ERROR;
2051 if (m != MATCH_YES)
2052 goto cleanup;
2056 if (initializer != NULL && current_attr.allocatable
2057 && gfc_current_state () == COMP_DERIVED)
2059 gfc_error ("Initialization of allocatable component at %C is not "
2060 "allowed");
2061 m = MATCH_ERROR;
2062 goto cleanup;
2065 /* Add the initializer. Note that it is fine if initializer is
2066 NULL here, because we sometimes also need to check if a
2067 declaration *must* have an initialization expression. */
2068 if (gfc_current_state () != COMP_DERIVED)
2069 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2070 else
2072 if (current_ts.type == BT_DERIVED
2073 && !current_attr.pointer && !initializer)
2074 initializer = gfc_default_initializer (&current_ts);
2075 t = build_struct (name, cl, &initializer, &as);
2078 m = (t) ? MATCH_YES : MATCH_ERROR;
2080 cleanup:
2081 /* Free stuff up and return. */
2082 gfc_free_expr (initializer);
2083 gfc_free_array_spec (as);
2085 return m;
2089 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2090 This assumes that the byte size is equal to the kind number for
2091 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2093 match
2094 gfc_match_old_kind_spec (gfc_typespec *ts)
2096 match m;
2097 int original_kind;
2099 if (gfc_match_char ('*') != MATCH_YES)
2100 return MATCH_NO;
2102 m = gfc_match_small_literal_int (&ts->kind, NULL);
2103 if (m != MATCH_YES)
2104 return MATCH_ERROR;
2106 original_kind = ts->kind;
2108 /* Massage the kind numbers for complex types. */
2109 if (ts->type == BT_COMPLEX)
2111 if (ts->kind % 2)
2113 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2114 gfc_basic_typename (ts->type), original_kind);
2115 return MATCH_ERROR;
2117 ts->kind /= 2;
2121 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2122 ts->kind = 8;
2124 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2126 if (ts->kind == 4)
2128 if (gfc_option.flag_real4_kind == 8)
2129 ts->kind = 8;
2130 if (gfc_option.flag_real4_kind == 10)
2131 ts->kind = 10;
2132 if (gfc_option.flag_real4_kind == 16)
2133 ts->kind = 16;
2136 if (ts->kind == 8)
2138 if (gfc_option.flag_real8_kind == 4)
2139 ts->kind = 4;
2140 if (gfc_option.flag_real8_kind == 10)
2141 ts->kind = 10;
2142 if (gfc_option.flag_real8_kind == 16)
2143 ts->kind = 16;
2147 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2149 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2150 gfc_basic_typename (ts->type), original_kind);
2151 return MATCH_ERROR;
2154 if (!gfc_notify_std (GFC_STD_GNU,
2155 "Nonstandard type declaration %s*%d at %C",
2156 gfc_basic_typename(ts->type), original_kind))
2157 return MATCH_ERROR;
2159 return MATCH_YES;
2163 /* Match a kind specification. Since kinds are generally optional, we
2164 usually return MATCH_NO if something goes wrong. If a "kind="
2165 string is found, then we know we have an error. */
2167 match
2168 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2170 locus where, loc;
2171 gfc_expr *e;
2172 match m, n;
2173 char c;
2174 const char *msg;
2176 m = MATCH_NO;
2177 n = MATCH_YES;
2178 e = NULL;
2180 where = loc = gfc_current_locus;
2182 if (kind_expr_only)
2183 goto kind_expr;
2185 if (gfc_match_char ('(') == MATCH_NO)
2186 return MATCH_NO;
2188 /* Also gobbles optional text. */
2189 if (gfc_match (" kind = ") == MATCH_YES)
2190 m = MATCH_ERROR;
2192 loc = gfc_current_locus;
2194 kind_expr:
2195 n = gfc_match_init_expr (&e);
2197 if (n != MATCH_YES)
2199 if (gfc_matching_function)
2201 /* The function kind expression might include use associated or
2202 imported parameters and try again after the specification
2203 expressions..... */
2204 if (gfc_match_char (')') != MATCH_YES)
2206 gfc_error ("Missing right parenthesis at %C");
2207 m = MATCH_ERROR;
2208 goto no_match;
2211 gfc_free_expr (e);
2212 gfc_undo_symbols ();
2213 return MATCH_YES;
2215 else
2217 /* ....or else, the match is real. */
2218 if (n == MATCH_NO)
2219 gfc_error ("Expected initialization expression at %C");
2220 if (n != MATCH_YES)
2221 return MATCH_ERROR;
2225 if (e->rank != 0)
2227 gfc_error ("Expected scalar initialization expression at %C");
2228 m = MATCH_ERROR;
2229 goto no_match;
2232 msg = gfc_extract_int (e, &ts->kind);
2234 if (msg != NULL)
2236 gfc_error (msg);
2237 m = MATCH_ERROR;
2238 goto no_match;
2241 /* Before throwing away the expression, let's see if we had a
2242 C interoperable kind (and store the fact). */
2243 if (e->ts.is_c_interop == 1)
2245 /* Mark this as C interoperable if being declared with one
2246 of the named constants from iso_c_binding. */
2247 ts->is_c_interop = e->ts.is_iso_c;
2248 ts->f90_type = e->ts.f90_type;
2251 gfc_free_expr (e);
2252 e = NULL;
2254 /* Ignore errors to this point, if we've gotten here. This means
2255 we ignore the m=MATCH_ERROR from above. */
2256 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2258 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2259 gfc_basic_typename (ts->type));
2260 gfc_current_locus = where;
2261 return MATCH_ERROR;
2264 /* Warn if, e.g., c_int is used for a REAL variable, but not
2265 if, e.g., c_double is used for COMPLEX as the standard
2266 explicitly says that the kind type parameter for complex and real
2267 variable is the same, i.e. c_float == c_float_complex. */
2268 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2269 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2270 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2271 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2272 "is %s", gfc_basic_typename (ts->f90_type), &where,
2273 gfc_basic_typename (ts->type));
2275 gfc_gobble_whitespace ();
2276 if ((c = gfc_next_ascii_char ()) != ')'
2277 && (ts->type != BT_CHARACTER || c != ','))
2279 if (ts->type == BT_CHARACTER)
2280 gfc_error ("Missing right parenthesis or comma at %C");
2281 else
2282 gfc_error ("Missing right parenthesis at %C");
2283 m = MATCH_ERROR;
2285 else
2286 /* All tests passed. */
2287 m = MATCH_YES;
2289 if(m == MATCH_ERROR)
2290 gfc_current_locus = where;
2292 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2293 ts->kind = 8;
2295 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2297 if (ts->kind == 4)
2299 if (gfc_option.flag_real4_kind == 8)
2300 ts->kind = 8;
2301 if (gfc_option.flag_real4_kind == 10)
2302 ts->kind = 10;
2303 if (gfc_option.flag_real4_kind == 16)
2304 ts->kind = 16;
2307 if (ts->kind == 8)
2309 if (gfc_option.flag_real8_kind == 4)
2310 ts->kind = 4;
2311 if (gfc_option.flag_real8_kind == 10)
2312 ts->kind = 10;
2313 if (gfc_option.flag_real8_kind == 16)
2314 ts->kind = 16;
2318 /* Return what we know from the test(s). */
2319 return m;
2321 no_match:
2322 gfc_free_expr (e);
2323 gfc_current_locus = where;
2324 return m;
2328 static match
2329 match_char_kind (int * kind, int * is_iso_c)
2331 locus where;
2332 gfc_expr *e;
2333 match m, n;
2334 const char *msg;
2336 m = MATCH_NO;
2337 e = NULL;
2338 where = gfc_current_locus;
2340 n = gfc_match_init_expr (&e);
2342 if (n != MATCH_YES && gfc_matching_function)
2344 /* The expression might include use-associated or imported
2345 parameters and try again after the specification
2346 expressions. */
2347 gfc_free_expr (e);
2348 gfc_undo_symbols ();
2349 return MATCH_YES;
2352 if (n == MATCH_NO)
2353 gfc_error ("Expected initialization expression at %C");
2354 if (n != MATCH_YES)
2355 return MATCH_ERROR;
2357 if (e->rank != 0)
2359 gfc_error ("Expected scalar initialization expression at %C");
2360 m = MATCH_ERROR;
2361 goto no_match;
2364 msg = gfc_extract_int (e, kind);
2365 *is_iso_c = e->ts.is_iso_c;
2366 if (msg != NULL)
2368 gfc_error (msg);
2369 m = MATCH_ERROR;
2370 goto no_match;
2373 gfc_free_expr (e);
2375 /* Ignore errors to this point, if we've gotten here. This means
2376 we ignore the m=MATCH_ERROR from above. */
2377 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2379 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2380 m = MATCH_ERROR;
2382 else
2383 /* All tests passed. */
2384 m = MATCH_YES;
2386 if (m == MATCH_ERROR)
2387 gfc_current_locus = where;
2389 /* Return what we know from the test(s). */
2390 return m;
2392 no_match:
2393 gfc_free_expr (e);
2394 gfc_current_locus = where;
2395 return m;
2399 /* Match the various kind/length specifications in a CHARACTER
2400 declaration. We don't return MATCH_NO. */
2402 match
2403 gfc_match_char_spec (gfc_typespec *ts)
2405 int kind, seen_length, is_iso_c;
2406 gfc_charlen *cl;
2407 gfc_expr *len;
2408 match m;
2409 bool deferred;
2411 len = NULL;
2412 seen_length = 0;
2413 kind = 0;
2414 is_iso_c = 0;
2415 deferred = false;
2417 /* Try the old-style specification first. */
2418 old_char_selector = 0;
2420 m = match_char_length (&len, &deferred, true);
2421 if (m != MATCH_NO)
2423 if (m == MATCH_YES)
2424 old_char_selector = 1;
2425 seen_length = 1;
2426 goto done;
2429 m = gfc_match_char ('(');
2430 if (m != MATCH_YES)
2432 m = MATCH_YES; /* Character without length is a single char. */
2433 goto done;
2436 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2437 if (gfc_match (" kind =") == MATCH_YES)
2439 m = match_char_kind (&kind, &is_iso_c);
2441 if (m == MATCH_ERROR)
2442 goto done;
2443 if (m == MATCH_NO)
2444 goto syntax;
2446 if (gfc_match (" , len =") == MATCH_NO)
2447 goto rparen;
2449 m = char_len_param_value (&len, &deferred);
2450 if (m == MATCH_NO)
2451 goto syntax;
2452 if (m == MATCH_ERROR)
2453 goto done;
2454 seen_length = 1;
2456 goto rparen;
2459 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2460 if (gfc_match (" len =") == MATCH_YES)
2462 m = char_len_param_value (&len, &deferred);
2463 if (m == MATCH_NO)
2464 goto syntax;
2465 if (m == MATCH_ERROR)
2466 goto done;
2467 seen_length = 1;
2469 if (gfc_match_char (')') == MATCH_YES)
2470 goto done;
2472 if (gfc_match (" , kind =") != MATCH_YES)
2473 goto syntax;
2475 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2476 goto done;
2478 goto rparen;
2481 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2482 m = char_len_param_value (&len, &deferred);
2483 if (m == MATCH_NO)
2484 goto syntax;
2485 if (m == MATCH_ERROR)
2486 goto done;
2487 seen_length = 1;
2489 m = gfc_match_char (')');
2490 if (m == MATCH_YES)
2491 goto done;
2493 if (gfc_match_char (',') != MATCH_YES)
2494 goto syntax;
2496 gfc_match (" kind ="); /* Gobble optional text. */
2498 m = match_char_kind (&kind, &is_iso_c);
2499 if (m == MATCH_ERROR)
2500 goto done;
2501 if (m == MATCH_NO)
2502 goto syntax;
2504 rparen:
2505 /* Require a right-paren at this point. */
2506 m = gfc_match_char (')');
2507 if (m == MATCH_YES)
2508 goto done;
2510 syntax:
2511 gfc_error ("Syntax error in CHARACTER declaration at %C");
2512 m = MATCH_ERROR;
2513 gfc_free_expr (len);
2514 return m;
2516 done:
2517 /* Deal with character functions after USE and IMPORT statements. */
2518 if (gfc_matching_function)
2520 gfc_free_expr (len);
2521 gfc_undo_symbols ();
2522 return MATCH_YES;
2525 if (m != MATCH_YES)
2527 gfc_free_expr (len);
2528 return m;
2531 /* Do some final massaging of the length values. */
2532 cl = gfc_new_charlen (gfc_current_ns, NULL);
2534 if (seen_length == 0)
2535 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2536 else
2537 cl->length = len;
2539 ts->u.cl = cl;
2540 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2541 ts->deferred = deferred;
2543 /* We have to know if it was a C interoperable kind so we can
2544 do accurate type checking of bind(c) procs, etc. */
2545 if (kind != 0)
2546 /* Mark this as C interoperable if being declared with one
2547 of the named constants from iso_c_binding. */
2548 ts->is_c_interop = is_iso_c;
2549 else if (len != NULL)
2550 /* Here, we might have parsed something such as: character(c_char)
2551 In this case, the parsing code above grabs the c_char when
2552 looking for the length (line 1690, roughly). it's the last
2553 testcase for parsing the kind params of a character variable.
2554 However, it's not actually the length. this seems like it
2555 could be an error.
2556 To see if the user used a C interop kind, test the expr
2557 of the so called length, and see if it's C interoperable. */
2558 ts->is_c_interop = len->ts.is_iso_c;
2560 return MATCH_YES;
2564 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2565 structure to the matched specification. This is necessary for FUNCTION and
2566 IMPLICIT statements.
2568 If implicit_flag is nonzero, then we don't check for the optional
2569 kind specification. Not doing so is needed for matching an IMPLICIT
2570 statement correctly. */
2572 match
2573 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2575 char name[GFC_MAX_SYMBOL_LEN + 1];
2576 gfc_symbol *sym, *dt_sym;
2577 match m;
2578 char c;
2579 bool seen_deferred_kind, matched_type;
2580 const char *dt_name;
2582 /* A belt and braces check that the typespec is correctly being treated
2583 as a deferred characteristic association. */
2584 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2585 && (gfc_current_block ()->result->ts.kind == -1)
2586 && (ts->kind == -1);
2587 gfc_clear_ts (ts);
2588 if (seen_deferred_kind)
2589 ts->kind = -1;
2591 /* Clear the current binding label, in case one is given. */
2592 curr_binding_label = NULL;
2594 if (gfc_match (" byte") == MATCH_YES)
2596 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
2597 return MATCH_ERROR;
2599 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2601 gfc_error ("BYTE type used at %C "
2602 "is not available on the target machine");
2603 return MATCH_ERROR;
2606 ts->type = BT_INTEGER;
2607 ts->kind = 1;
2608 return MATCH_YES;
2612 m = gfc_match (" type (");
2613 matched_type = (m == MATCH_YES);
2614 if (matched_type)
2616 gfc_gobble_whitespace ();
2617 if (gfc_peek_ascii_char () == '*')
2619 if ((m = gfc_match ("*)")) != MATCH_YES)
2620 return m;
2621 if (gfc_current_state () == COMP_DERIVED)
2623 gfc_error ("Assumed type at %C is not allowed for components");
2624 return MATCH_ERROR;
2626 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
2627 "at %C"))
2628 return MATCH_ERROR;
2629 ts->type = BT_ASSUMED;
2630 return MATCH_YES;
2633 m = gfc_match ("%n", name);
2634 matched_type = (m == MATCH_YES);
2637 if ((matched_type && strcmp ("integer", name) == 0)
2638 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2640 ts->type = BT_INTEGER;
2641 ts->kind = gfc_default_integer_kind;
2642 goto get_kind;
2645 if ((matched_type && strcmp ("character", name) == 0)
2646 || (!matched_type && gfc_match (" character") == MATCH_YES))
2648 if (matched_type
2649 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2650 "intrinsic-type-spec at %C"))
2651 return MATCH_ERROR;
2653 ts->type = BT_CHARACTER;
2654 if (implicit_flag == 0)
2655 m = gfc_match_char_spec (ts);
2656 else
2657 m = MATCH_YES;
2659 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2660 m = MATCH_ERROR;
2662 return m;
2665 if ((matched_type && strcmp ("real", name) == 0)
2666 || (!matched_type && gfc_match (" real") == MATCH_YES))
2668 ts->type = BT_REAL;
2669 ts->kind = gfc_default_real_kind;
2670 goto get_kind;
2673 if ((matched_type
2674 && (strcmp ("doubleprecision", name) == 0
2675 || (strcmp ("double", name) == 0
2676 && gfc_match (" precision") == MATCH_YES)))
2677 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2679 if (matched_type
2680 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2681 "intrinsic-type-spec at %C"))
2682 return MATCH_ERROR;
2683 if (matched_type && gfc_match_char (')') != MATCH_YES)
2684 return MATCH_ERROR;
2686 ts->type = BT_REAL;
2687 ts->kind = gfc_default_double_kind;
2688 return MATCH_YES;
2691 if ((matched_type && strcmp ("complex", name) == 0)
2692 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2694 ts->type = BT_COMPLEX;
2695 ts->kind = gfc_default_complex_kind;
2696 goto get_kind;
2699 if ((matched_type
2700 && (strcmp ("doublecomplex", name) == 0
2701 || (strcmp ("double", name) == 0
2702 && gfc_match (" complex") == MATCH_YES)))
2703 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2705 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
2706 return MATCH_ERROR;
2708 if (matched_type
2709 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2710 "intrinsic-type-spec at %C"))
2711 return MATCH_ERROR;
2713 if (matched_type && gfc_match_char (')') != MATCH_YES)
2714 return MATCH_ERROR;
2716 ts->type = BT_COMPLEX;
2717 ts->kind = gfc_default_double_kind;
2718 return MATCH_YES;
2721 if ((matched_type && strcmp ("logical", name) == 0)
2722 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2724 ts->type = BT_LOGICAL;
2725 ts->kind = gfc_default_logical_kind;
2726 goto get_kind;
2729 if (matched_type)
2730 m = gfc_match_char (')');
2732 if (m == MATCH_YES)
2733 ts->type = BT_DERIVED;
2734 else
2736 /* Match CLASS declarations. */
2737 m = gfc_match (" class ( * )");
2738 if (m == MATCH_ERROR)
2739 return MATCH_ERROR;
2740 else if (m == MATCH_YES)
2742 gfc_symbol *upe;
2743 gfc_symtree *st;
2744 ts->type = BT_CLASS;
2745 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
2746 if (upe == NULL)
2748 upe = gfc_new_symbol ("STAR", gfc_current_ns);
2749 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2750 st->n.sym = upe;
2751 gfc_set_sym_referenced (upe);
2752 upe->refs++;
2753 upe->ts.type = BT_VOID;
2754 upe->attr.unlimited_polymorphic = 1;
2755 /* This is essential to force the construction of
2756 unlimited polymorphic component class containers. */
2757 upe->attr.zero_comp = 1;
2758 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
2759 &gfc_current_locus))
2760 return MATCH_ERROR;
2762 else
2764 st = gfc_find_symtree (gfc_current_ns->sym_root, "STAR");
2765 if (st == NULL)
2766 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2767 st->n.sym = upe;
2768 upe->refs++;
2770 ts->u.derived = upe;
2771 return m;
2774 m = gfc_match (" class ( %n )", name);
2775 if (m != MATCH_YES)
2776 return m;
2777 ts->type = BT_CLASS;
2779 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
2780 return MATCH_ERROR;
2783 /* Defer association of the derived type until the end of the
2784 specification block. However, if the derived type can be
2785 found, add it to the typespec. */
2786 if (gfc_matching_function)
2788 ts->u.derived = NULL;
2789 if (gfc_current_state () != COMP_INTERFACE
2790 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2792 sym = gfc_find_dt_in_generic (sym);
2793 ts->u.derived = sym;
2795 return MATCH_YES;
2798 /* Search for the name but allow the components to be defined later. If
2799 type = -1, this typespec has been seen in a function declaration but
2800 the type could not be accessed at that point. The actual derived type is
2801 stored in a symtree with the first letter of the name capitalized; the
2802 symtree with the all lower-case name contains the associated
2803 generic function. */
2804 dt_name = gfc_get_string ("%c%s",
2805 (char) TOUPPER ((unsigned char) name[0]),
2806 (const char*)&name[1]);
2807 sym = NULL;
2808 dt_sym = NULL;
2809 if (ts->kind != -1)
2811 gfc_get_ha_symbol (name, &sym);
2812 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2814 gfc_error ("Type name '%s' at %C is ambiguous", name);
2815 return MATCH_ERROR;
2817 if (sym->generic && !dt_sym)
2818 dt_sym = gfc_find_dt_in_generic (sym);
2820 else if (ts->kind == -1)
2822 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2823 || gfc_current_ns->has_import_set;
2824 gfc_find_symbol (name, NULL, iface, &sym);
2825 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2827 gfc_error ("Type name '%s' at %C is ambiguous", name);
2828 return MATCH_ERROR;
2830 if (sym && sym->generic && !dt_sym)
2831 dt_sym = gfc_find_dt_in_generic (sym);
2833 ts->kind = 0;
2834 if (sym == NULL)
2835 return MATCH_NO;
2838 if ((sym->attr.flavor != FL_UNKNOWN
2839 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2840 || sym->attr.subroutine)
2842 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2843 "entity at %L, which has the same name", name,
2844 &sym->declared_at);
2845 return MATCH_ERROR;
2848 gfc_set_sym_referenced (sym);
2849 if (!sym->attr.generic
2850 && !gfc_add_generic (&sym->attr, sym->name, NULL))
2851 return MATCH_ERROR;
2853 if (!sym->attr.function
2854 && !gfc_add_function (&sym->attr, sym->name, NULL))
2855 return MATCH_ERROR;
2857 if (!dt_sym)
2859 gfc_interface *intr, *head;
2861 /* Use upper case to save the actual derived-type symbol. */
2862 gfc_get_symbol (dt_name, NULL, &dt_sym);
2863 dt_sym->name = gfc_get_string (sym->name);
2864 head = sym->generic;
2865 intr = gfc_get_interface ();
2866 intr->sym = dt_sym;
2867 intr->where = gfc_current_locus;
2868 intr->next = head;
2869 sym->generic = intr;
2870 sym->attr.if_source = IFSRC_DECL;
2873 gfc_set_sym_referenced (dt_sym);
2875 if (dt_sym->attr.flavor != FL_DERIVED
2876 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
2877 return MATCH_ERROR;
2879 ts->u.derived = dt_sym;
2881 return MATCH_YES;
2883 get_kind:
2884 if (matched_type
2885 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2886 "intrinsic-type-spec at %C"))
2887 return MATCH_ERROR;
2889 /* For all types except double, derived and character, look for an
2890 optional kind specifier. MATCH_NO is actually OK at this point. */
2891 if (implicit_flag == 1)
2893 if (matched_type && gfc_match_char (')') != MATCH_YES)
2894 return MATCH_ERROR;
2896 return MATCH_YES;
2899 if (gfc_current_form == FORM_FREE)
2901 c = gfc_peek_ascii_char ();
2902 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2903 && c != ':' && c != ',')
2905 if (matched_type && c == ')')
2907 gfc_next_ascii_char ();
2908 return MATCH_YES;
2910 return MATCH_NO;
2914 m = gfc_match_kind_spec (ts, false);
2915 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2916 m = gfc_match_old_kind_spec (ts);
2918 if (matched_type && gfc_match_char (')') != MATCH_YES)
2919 return MATCH_ERROR;
2921 /* Defer association of the KIND expression of function results
2922 until after USE and IMPORT statements. */
2923 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2924 || gfc_matching_function)
2925 return MATCH_YES;
2927 if (m == MATCH_NO)
2928 m = MATCH_YES; /* No kind specifier found. */
2930 return m;
2934 /* Match an IMPLICIT NONE statement. Actually, this statement is
2935 already matched in parse.c, or we would not end up here in the
2936 first place. So the only thing we need to check, is if there is
2937 trailing garbage. If not, the match is successful. */
2939 match
2940 gfc_match_implicit_none (void)
2942 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2946 /* Match the letter range(s) of an IMPLICIT statement. */
2948 static match
2949 match_implicit_range (void)
2951 char c, c1, c2;
2952 int inner;
2953 locus cur_loc;
2955 cur_loc = gfc_current_locus;
2957 gfc_gobble_whitespace ();
2958 c = gfc_next_ascii_char ();
2959 if (c != '(')
2961 gfc_error ("Missing character range in IMPLICIT at %C");
2962 goto bad;
2965 inner = 1;
2966 while (inner)
2968 gfc_gobble_whitespace ();
2969 c1 = gfc_next_ascii_char ();
2970 if (!ISALPHA (c1))
2971 goto bad;
2973 gfc_gobble_whitespace ();
2974 c = gfc_next_ascii_char ();
2976 switch (c)
2978 case ')':
2979 inner = 0; /* Fall through. */
2981 case ',':
2982 c2 = c1;
2983 break;
2985 case '-':
2986 gfc_gobble_whitespace ();
2987 c2 = gfc_next_ascii_char ();
2988 if (!ISALPHA (c2))
2989 goto bad;
2991 gfc_gobble_whitespace ();
2992 c = gfc_next_ascii_char ();
2994 if ((c != ',') && (c != ')'))
2995 goto bad;
2996 if (c == ')')
2997 inner = 0;
2999 break;
3001 default:
3002 goto bad;
3005 if (c1 > c2)
3007 gfc_error ("Letters must be in alphabetic order in "
3008 "IMPLICIT statement at %C");
3009 goto bad;
3012 /* See if we can add the newly matched range to the pending
3013 implicits from this IMPLICIT statement. We do not check for
3014 conflicts with whatever earlier IMPLICIT statements may have
3015 set. This is done when we've successfully finished matching
3016 the current one. */
3017 if (!gfc_add_new_implicit_range (c1, c2))
3018 goto bad;
3021 return MATCH_YES;
3023 bad:
3024 gfc_syntax_error (ST_IMPLICIT);
3026 gfc_current_locus = cur_loc;
3027 return MATCH_ERROR;
3031 /* Match an IMPLICIT statement, storing the types for
3032 gfc_set_implicit() if the statement is accepted by the parser.
3033 There is a strange looking, but legal syntactic construction
3034 possible. It looks like:
3036 IMPLICIT INTEGER (a-b) (c-d)
3038 This is legal if "a-b" is a constant expression that happens to
3039 equal one of the legal kinds for integers. The real problem
3040 happens with an implicit specification that looks like:
3042 IMPLICIT INTEGER (a-b)
3044 In this case, a typespec matcher that is "greedy" (as most of the
3045 matchers are) gobbles the character range as a kindspec, leaving
3046 nothing left. We therefore have to go a bit more slowly in the
3047 matching process by inhibiting the kindspec checking during
3048 typespec matching and checking for a kind later. */
3050 match
3051 gfc_match_implicit (void)
3053 gfc_typespec ts;
3054 locus cur_loc;
3055 char c;
3056 match m;
3058 gfc_clear_ts (&ts);
3060 /* We don't allow empty implicit statements. */
3061 if (gfc_match_eos () == MATCH_YES)
3063 gfc_error ("Empty IMPLICIT statement at %C");
3064 return MATCH_ERROR;
3069 /* First cleanup. */
3070 gfc_clear_new_implicit ();
3072 /* A basic type is mandatory here. */
3073 m = gfc_match_decl_type_spec (&ts, 1);
3074 if (m == MATCH_ERROR)
3075 goto error;
3076 if (m == MATCH_NO)
3077 goto syntax;
3079 cur_loc = gfc_current_locus;
3080 m = match_implicit_range ();
3082 if (m == MATCH_YES)
3084 /* We may have <TYPE> (<RANGE>). */
3085 gfc_gobble_whitespace ();
3086 c = gfc_next_ascii_char ();
3087 if ((c == '\n') || (c == ','))
3089 /* Check for CHARACTER with no length parameter. */
3090 if (ts.type == BT_CHARACTER && !ts.u.cl)
3092 ts.kind = gfc_default_character_kind;
3093 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3094 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3095 NULL, 1);
3098 /* Record the Successful match. */
3099 if (!gfc_merge_new_implicit (&ts))
3100 return MATCH_ERROR;
3101 continue;
3104 gfc_current_locus = cur_loc;
3107 /* Discard the (incorrectly) matched range. */
3108 gfc_clear_new_implicit ();
3110 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3111 if (ts.type == BT_CHARACTER)
3112 m = gfc_match_char_spec (&ts);
3113 else
3115 m = gfc_match_kind_spec (&ts, false);
3116 if (m == MATCH_NO)
3118 m = gfc_match_old_kind_spec (&ts);
3119 if (m == MATCH_ERROR)
3120 goto error;
3121 if (m == MATCH_NO)
3122 goto syntax;
3125 if (m == MATCH_ERROR)
3126 goto error;
3128 m = match_implicit_range ();
3129 if (m == MATCH_ERROR)
3130 goto error;
3131 if (m == MATCH_NO)
3132 goto syntax;
3134 gfc_gobble_whitespace ();
3135 c = gfc_next_ascii_char ();
3136 if ((c != '\n') && (c != ','))
3137 goto syntax;
3139 if (!gfc_merge_new_implicit (&ts))
3140 return MATCH_ERROR;
3142 while (c == ',');
3144 return MATCH_YES;
3146 syntax:
3147 gfc_syntax_error (ST_IMPLICIT);
3149 error:
3150 return MATCH_ERROR;
3154 match
3155 gfc_match_import (void)
3157 char name[GFC_MAX_SYMBOL_LEN + 1];
3158 match m;
3159 gfc_symbol *sym;
3160 gfc_symtree *st;
3162 if (gfc_current_ns->proc_name == NULL
3163 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3165 gfc_error ("IMPORT statement at %C only permitted in "
3166 "an INTERFACE body");
3167 return MATCH_ERROR;
3170 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
3171 return MATCH_ERROR;
3173 if (gfc_match_eos () == MATCH_YES)
3175 /* All host variables should be imported. */
3176 gfc_current_ns->has_import_set = 1;
3177 return MATCH_YES;
3180 if (gfc_match (" ::") == MATCH_YES)
3182 if (gfc_match_eos () == MATCH_YES)
3184 gfc_error ("Expecting list of named entities at %C");
3185 return MATCH_ERROR;
3189 for(;;)
3191 sym = NULL;
3192 m = gfc_match (" %n", name);
3193 switch (m)
3195 case MATCH_YES:
3196 if (gfc_current_ns->parent != NULL
3197 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3199 gfc_error ("Type name '%s' at %C is ambiguous", name);
3200 return MATCH_ERROR;
3202 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3203 && gfc_find_symbol (name,
3204 gfc_current_ns->proc_name->ns->parent,
3205 1, &sym))
3207 gfc_error ("Type name '%s' at %C is ambiguous", name);
3208 return MATCH_ERROR;
3211 if (sym == NULL)
3213 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3214 "at %C - does not exist.", name);
3215 return MATCH_ERROR;
3218 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3220 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3221 "at %C.", name);
3222 goto next_item;
3225 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3226 st->n.sym = sym;
3227 sym->refs++;
3228 sym->attr.imported = 1;
3230 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3232 /* The actual derived type is stored in a symtree with the first
3233 letter of the name capitalized; the symtree with the all
3234 lower-case name contains the associated generic function. */
3235 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3236 gfc_get_string ("%c%s",
3237 (char) TOUPPER ((unsigned char) name[0]),
3238 &name[1]));
3239 st->n.sym = sym;
3240 sym->refs++;
3241 sym->attr.imported = 1;
3244 goto next_item;
3246 case MATCH_NO:
3247 break;
3249 case MATCH_ERROR:
3250 return MATCH_ERROR;
3253 next_item:
3254 if (gfc_match_eos () == MATCH_YES)
3255 break;
3256 if (gfc_match_char (',') != MATCH_YES)
3257 goto syntax;
3260 return MATCH_YES;
3262 syntax:
3263 gfc_error ("Syntax error in IMPORT statement at %C");
3264 return MATCH_ERROR;
3268 /* A minimal implementation of gfc_match without whitespace, escape
3269 characters or variable arguments. Returns true if the next
3270 characters match the TARGET template exactly. */
3272 static bool
3273 match_string_p (const char *target)
3275 const char *p;
3277 for (p = target; *p; p++)
3278 if ((char) gfc_next_ascii_char () != *p)
3279 return false;
3280 return true;
3283 /* Matches an attribute specification including array specs. If
3284 successful, leaves the variables current_attr and current_as
3285 holding the specification. Also sets the colon_seen variable for
3286 later use by matchers associated with initializations.
3288 This subroutine is a little tricky in the sense that we don't know
3289 if we really have an attr-spec until we hit the double colon.
3290 Until that time, we can only return MATCH_NO. This forces us to
3291 check for duplicate specification at this level. */
3293 static match
3294 match_attr_spec (void)
3296 /* Modifiers that can exist in a type statement. */
3297 enum
3298 { GFC_DECL_BEGIN = 0,
3299 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3300 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3301 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3302 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3303 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3304 DECL_NONE, GFC_DECL_END /* Sentinel */
3307 /* GFC_DECL_END is the sentinel, index starts at 0. */
3308 #define NUM_DECL GFC_DECL_END
3310 locus start, seen_at[NUM_DECL];
3311 int seen[NUM_DECL];
3312 unsigned int d;
3313 const char *attr;
3314 match m;
3315 bool t;
3317 gfc_clear_attr (&current_attr);
3318 start = gfc_current_locus;
3320 current_as = NULL;
3321 colon_seen = 0;
3323 /* See if we get all of the keywords up to the final double colon. */
3324 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3325 seen[d] = 0;
3327 for (;;)
3329 char ch;
3331 d = DECL_NONE;
3332 gfc_gobble_whitespace ();
3334 ch = gfc_next_ascii_char ();
3335 if (ch == ':')
3337 /* This is the successful exit condition for the loop. */
3338 if (gfc_next_ascii_char () == ':')
3339 break;
3341 else if (ch == ',')
3343 gfc_gobble_whitespace ();
3344 switch (gfc_peek_ascii_char ())
3346 case 'a':
3347 gfc_next_ascii_char ();
3348 switch (gfc_next_ascii_char ())
3350 case 'l':
3351 if (match_string_p ("locatable"))
3353 /* Matched "allocatable". */
3354 d = DECL_ALLOCATABLE;
3356 break;
3358 case 's':
3359 if (match_string_p ("ynchronous"))
3361 /* Matched "asynchronous". */
3362 d = DECL_ASYNCHRONOUS;
3364 break;
3366 break;
3368 case 'b':
3369 /* Try and match the bind(c). */
3370 m = gfc_match_bind_c (NULL, true);
3371 if (m == MATCH_YES)
3372 d = DECL_IS_BIND_C;
3373 else if (m == MATCH_ERROR)
3374 goto cleanup;
3375 break;
3377 case 'c':
3378 gfc_next_ascii_char ();
3379 if ('o' != gfc_next_ascii_char ())
3380 break;
3381 switch (gfc_next_ascii_char ())
3383 case 'd':
3384 if (match_string_p ("imension"))
3386 d = DECL_CODIMENSION;
3387 break;
3389 case 'n':
3390 if (match_string_p ("tiguous"))
3392 d = DECL_CONTIGUOUS;
3393 break;
3396 break;
3398 case 'd':
3399 if (match_string_p ("dimension"))
3400 d = DECL_DIMENSION;
3401 break;
3403 case 'e':
3404 if (match_string_p ("external"))
3405 d = DECL_EXTERNAL;
3406 break;
3408 case 'i':
3409 if (match_string_p ("int"))
3411 ch = gfc_next_ascii_char ();
3412 if (ch == 'e')
3414 if (match_string_p ("nt"))
3416 /* Matched "intent". */
3417 /* TODO: Call match_intent_spec from here. */
3418 if (gfc_match (" ( in out )") == MATCH_YES)
3419 d = DECL_INOUT;
3420 else if (gfc_match (" ( in )") == MATCH_YES)
3421 d = DECL_IN;
3422 else if (gfc_match (" ( out )") == MATCH_YES)
3423 d = DECL_OUT;
3426 else if (ch == 'r')
3428 if (match_string_p ("insic"))
3430 /* Matched "intrinsic". */
3431 d = DECL_INTRINSIC;
3435 break;
3437 case 'o':
3438 if (match_string_p ("optional"))
3439 d = DECL_OPTIONAL;
3440 break;
3442 case 'p':
3443 gfc_next_ascii_char ();
3444 switch (gfc_next_ascii_char ())
3446 case 'a':
3447 if (match_string_p ("rameter"))
3449 /* Matched "parameter". */
3450 d = DECL_PARAMETER;
3452 break;
3454 case 'o':
3455 if (match_string_p ("inter"))
3457 /* Matched "pointer". */
3458 d = DECL_POINTER;
3460 break;
3462 case 'r':
3463 ch = gfc_next_ascii_char ();
3464 if (ch == 'i')
3466 if (match_string_p ("vate"))
3468 /* Matched "private". */
3469 d = DECL_PRIVATE;
3472 else if (ch == 'o')
3474 if (match_string_p ("tected"))
3476 /* Matched "protected". */
3477 d = DECL_PROTECTED;
3480 break;
3482 case 'u':
3483 if (match_string_p ("blic"))
3485 /* Matched "public". */
3486 d = DECL_PUBLIC;
3488 break;
3490 break;
3492 case 's':
3493 if (match_string_p ("save"))
3494 d = DECL_SAVE;
3495 break;
3497 case 't':
3498 if (match_string_p ("target"))
3499 d = DECL_TARGET;
3500 break;
3502 case 'v':
3503 gfc_next_ascii_char ();
3504 ch = gfc_next_ascii_char ();
3505 if (ch == 'a')
3507 if (match_string_p ("lue"))
3509 /* Matched "value". */
3510 d = DECL_VALUE;
3513 else if (ch == 'o')
3515 if (match_string_p ("latile"))
3517 /* Matched "volatile". */
3518 d = DECL_VOLATILE;
3521 break;
3525 /* No double colon and no recognizable decl_type, so assume that
3526 we've been looking at something else the whole time. */
3527 if (d == DECL_NONE)
3529 m = MATCH_NO;
3530 goto cleanup;
3533 /* Check to make sure any parens are paired up correctly. */
3534 if (gfc_match_parens () == MATCH_ERROR)
3536 m = MATCH_ERROR;
3537 goto cleanup;
3540 seen[d]++;
3541 seen_at[d] = gfc_current_locus;
3543 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3545 gfc_array_spec *as = NULL;
3547 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3548 d == DECL_CODIMENSION);
3550 if (current_as == NULL)
3551 current_as = as;
3552 else if (m == MATCH_YES)
3554 if (!merge_array_spec (as, current_as, false))
3555 m = MATCH_ERROR;
3556 free (as);
3559 if (m == MATCH_NO)
3561 if (d == DECL_CODIMENSION)
3562 gfc_error ("Missing codimension specification at %C");
3563 else
3564 gfc_error ("Missing dimension specification at %C");
3565 m = MATCH_ERROR;
3568 if (m == MATCH_ERROR)
3569 goto cleanup;
3573 /* Since we've seen a double colon, we have to be looking at an
3574 attr-spec. This means that we can now issue errors. */
3575 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3576 if (seen[d] > 1)
3578 switch (d)
3580 case DECL_ALLOCATABLE:
3581 attr = "ALLOCATABLE";
3582 break;
3583 case DECL_ASYNCHRONOUS:
3584 attr = "ASYNCHRONOUS";
3585 break;
3586 case DECL_CODIMENSION:
3587 attr = "CODIMENSION";
3588 break;
3589 case DECL_CONTIGUOUS:
3590 attr = "CONTIGUOUS";
3591 break;
3592 case DECL_DIMENSION:
3593 attr = "DIMENSION";
3594 break;
3595 case DECL_EXTERNAL:
3596 attr = "EXTERNAL";
3597 break;
3598 case DECL_IN:
3599 attr = "INTENT (IN)";
3600 break;
3601 case DECL_OUT:
3602 attr = "INTENT (OUT)";
3603 break;
3604 case DECL_INOUT:
3605 attr = "INTENT (IN OUT)";
3606 break;
3607 case DECL_INTRINSIC:
3608 attr = "INTRINSIC";
3609 break;
3610 case DECL_OPTIONAL:
3611 attr = "OPTIONAL";
3612 break;
3613 case DECL_PARAMETER:
3614 attr = "PARAMETER";
3615 break;
3616 case DECL_POINTER:
3617 attr = "POINTER";
3618 break;
3619 case DECL_PROTECTED:
3620 attr = "PROTECTED";
3621 break;
3622 case DECL_PRIVATE:
3623 attr = "PRIVATE";
3624 break;
3625 case DECL_PUBLIC:
3626 attr = "PUBLIC";
3627 break;
3628 case DECL_SAVE:
3629 attr = "SAVE";
3630 break;
3631 case DECL_TARGET:
3632 attr = "TARGET";
3633 break;
3634 case DECL_IS_BIND_C:
3635 attr = "IS_BIND_C";
3636 break;
3637 case DECL_VALUE:
3638 attr = "VALUE";
3639 break;
3640 case DECL_VOLATILE:
3641 attr = "VOLATILE";
3642 break;
3643 default:
3644 attr = NULL; /* This shouldn't happen. */
3647 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3648 m = MATCH_ERROR;
3649 goto cleanup;
3652 /* Now that we've dealt with duplicate attributes, add the attributes
3653 to the current attribute. */
3654 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3656 if (seen[d] == 0)
3657 continue;
3659 if (gfc_current_state () == COMP_DERIVED
3660 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3661 && d != DECL_POINTER && d != DECL_PRIVATE
3662 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3664 if (d == DECL_ALLOCATABLE)
3666 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
3667 "attribute at %C in a TYPE definition"))
3669 m = MATCH_ERROR;
3670 goto cleanup;
3673 else
3675 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3676 &seen_at[d]);
3677 m = MATCH_ERROR;
3678 goto cleanup;
3682 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3683 && gfc_current_state () != COMP_MODULE)
3685 if (d == DECL_PRIVATE)
3686 attr = "PRIVATE";
3687 else
3688 attr = "PUBLIC";
3689 if (gfc_current_state () == COMP_DERIVED
3690 && gfc_state_stack->previous
3691 && gfc_state_stack->previous->state == COMP_MODULE)
3693 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
3694 "at %L in a TYPE definition", attr,
3695 &seen_at[d]))
3697 m = MATCH_ERROR;
3698 goto cleanup;
3701 else
3703 gfc_error ("%s attribute at %L is not allowed outside of the "
3704 "specification part of a module", attr, &seen_at[d]);
3705 m = MATCH_ERROR;
3706 goto cleanup;
3710 switch (d)
3712 case DECL_ALLOCATABLE:
3713 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3714 break;
3716 case DECL_ASYNCHRONOUS:
3717 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
3718 t = false;
3719 else
3720 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3721 break;
3723 case DECL_CODIMENSION:
3724 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3725 break;
3727 case DECL_CONTIGUOUS:
3728 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
3729 t = false;
3730 else
3731 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3732 break;
3734 case DECL_DIMENSION:
3735 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3736 break;
3738 case DECL_EXTERNAL:
3739 t = gfc_add_external (&current_attr, &seen_at[d]);
3740 break;
3742 case DECL_IN:
3743 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3744 break;
3746 case DECL_OUT:
3747 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3748 break;
3750 case DECL_INOUT:
3751 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3752 break;
3754 case DECL_INTRINSIC:
3755 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3756 break;
3758 case DECL_OPTIONAL:
3759 t = gfc_add_optional (&current_attr, &seen_at[d]);
3760 break;
3762 case DECL_PARAMETER:
3763 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3764 break;
3766 case DECL_POINTER:
3767 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3768 break;
3770 case DECL_PROTECTED:
3771 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3773 gfc_error ("PROTECTED at %C only allowed in specification "
3774 "part of a module");
3775 t = false;
3776 break;
3779 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
3780 t = false;
3781 else
3782 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3783 break;
3785 case DECL_PRIVATE:
3786 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3787 &seen_at[d]);
3788 break;
3790 case DECL_PUBLIC:
3791 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3792 &seen_at[d]);
3793 break;
3795 case DECL_SAVE:
3796 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3797 break;
3799 case DECL_TARGET:
3800 t = gfc_add_target (&current_attr, &seen_at[d]);
3801 break;
3803 case DECL_IS_BIND_C:
3804 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3805 break;
3807 case DECL_VALUE:
3808 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
3809 t = false;
3810 else
3811 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3812 break;
3814 case DECL_VOLATILE:
3815 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
3816 t = false;
3817 else
3818 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3819 break;
3821 default:
3822 gfc_internal_error ("match_attr_spec(): Bad attribute");
3825 if (!t)
3827 m = MATCH_ERROR;
3828 goto cleanup;
3832 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3833 if (gfc_current_state () == COMP_MODULE && !current_attr.save
3834 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
3835 current_attr.save = SAVE_IMPLICIT;
3837 colon_seen = 1;
3838 return MATCH_YES;
3840 cleanup:
3841 gfc_current_locus = start;
3842 gfc_free_array_spec (current_as);
3843 current_as = NULL;
3844 return m;
3848 /* Set the binding label, dest_label, either with the binding label
3849 stored in the given gfc_typespec, ts, or if none was provided, it
3850 will be the symbol name in all lower case, as required by the draft
3851 (J3/04-007, section 15.4.1). If a binding label was given and
3852 there is more than one argument (num_idents), it is an error. */
3854 static bool
3855 set_binding_label (const char **dest_label, const char *sym_name,
3856 int num_idents)
3858 if (num_idents > 1 && has_name_equals)
3860 gfc_error ("Multiple identifiers provided with "
3861 "single NAME= specifier at %C");
3862 return false;
3865 if (curr_binding_label)
3866 /* Binding label given; store in temp holder till have sym. */
3867 *dest_label = curr_binding_label;
3868 else
3870 /* No binding label given, and the NAME= specifier did not exist,
3871 which means there was no NAME="". */
3872 if (sym_name != NULL && has_name_equals == 0)
3873 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
3876 return true;
3880 /* Set the status of the given common block as being BIND(C) or not,
3881 depending on the given parameter, is_bind_c. */
3883 void
3884 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3886 com_block->is_bind_c = is_bind_c;
3887 return;
3891 /* Verify that the given gfc_typespec is for a C interoperable type. */
3893 bool
3894 gfc_verify_c_interop (gfc_typespec *ts)
3896 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3897 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3898 ? true : false;
3899 else if (ts->type == BT_CLASS)
3900 return false;
3901 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
3902 return false;
3904 return true;
3908 /* Verify that the variables of a given common block, which has been
3909 defined with the attribute specifier bind(c), to be of a C
3910 interoperable type. Errors will be reported here, if
3911 encountered. */
3913 bool
3914 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3916 gfc_symbol *curr_sym = NULL;
3917 bool retval = true;
3919 curr_sym = com_block->head;
3921 /* Make sure we have at least one symbol. */
3922 if (curr_sym == NULL)
3923 return retval;
3925 /* Here we know we have a symbol, so we'll execute this loop
3926 at least once. */
3929 /* The second to last param, 1, says this is in a common block. */
3930 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3931 curr_sym = curr_sym->common_next;
3932 } while (curr_sym != NULL);
3934 return retval;
3938 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3939 an appropriate error message is reported. */
3941 bool
3942 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3943 int is_in_common, gfc_common_head *com_block)
3945 bool bind_c_function = false;
3946 bool retval = true;
3948 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3949 bind_c_function = true;
3951 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3953 tmp_sym = tmp_sym->result;
3954 /* Make sure it wasn't an implicitly typed result. */
3955 if (tmp_sym->attr.implicit_type && gfc_option.warn_c_binding_type)
3957 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3958 "%L may not be C interoperable", tmp_sym->name,
3959 &tmp_sym->declared_at);
3960 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3961 /* Mark it as C interoperable to prevent duplicate warnings. */
3962 tmp_sym->ts.is_c_interop = 1;
3963 tmp_sym->attr.is_c_interop = 1;
3967 /* Here, we know we have the bind(c) attribute, so if we have
3968 enough type info, then verify that it's a C interop kind.
3969 The info could be in the symbol already, or possibly still in
3970 the given ts (current_ts), so look in both. */
3971 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3973 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
3975 /* See if we're dealing with a sym in a common block or not. */
3976 if (is_in_common == 1 && gfc_option.warn_c_binding_type)
3978 gfc_warning ("Variable '%s' in common block '%s' at %L "
3979 "may not be a C interoperable "
3980 "kind though common block '%s' is BIND(C)",
3981 tmp_sym->name, com_block->name,
3982 &(tmp_sym->declared_at), com_block->name);
3984 else
3986 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3987 gfc_error ("Type declaration '%s' at %L is not C "
3988 "interoperable but it is BIND(C)",
3989 tmp_sym->name, &(tmp_sym->declared_at));
3990 else if (gfc_option.warn_c_binding_type)
3991 gfc_warning ("Variable '%s' at %L "
3992 "may not be a C interoperable "
3993 "kind but it is bind(c)",
3994 tmp_sym->name, &(tmp_sym->declared_at));
3998 /* Variables declared w/in a common block can't be bind(c)
3999 since there's no way for C to see these variables, so there's
4000 semantically no reason for the attribute. */
4001 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
4003 gfc_error ("Variable '%s' in common block '%s' at "
4004 "%L cannot be declared with BIND(C) "
4005 "since it is not a global",
4006 tmp_sym->name, com_block->name,
4007 &(tmp_sym->declared_at));
4008 retval = false;
4011 /* Scalar variables that are bind(c) can not have the pointer
4012 or allocatable attributes. */
4013 if (tmp_sym->attr.is_bind_c == 1)
4015 if (tmp_sym->attr.pointer == 1)
4017 gfc_error ("Variable '%s' at %L cannot have both the "
4018 "POINTER and BIND(C) attributes",
4019 tmp_sym->name, &(tmp_sym->declared_at));
4020 retval = false;
4023 if (tmp_sym->attr.allocatable == 1)
4025 gfc_error ("Variable '%s' at %L cannot have both the "
4026 "ALLOCATABLE and BIND(C) attributes",
4027 tmp_sym->name, &(tmp_sym->declared_at));
4028 retval = false;
4033 /* If it is a BIND(C) function, make sure the return value is a
4034 scalar value. The previous tests in this function made sure
4035 the type is interoperable. */
4036 if (bind_c_function && tmp_sym->as != NULL)
4037 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4038 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
4040 /* BIND(C) functions can not return a character string. */
4041 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
4042 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
4043 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
4044 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4045 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4046 "be a character string", tmp_sym->name,
4047 &(tmp_sym->declared_at));
4050 /* See if the symbol has been marked as private. If it has, make sure
4051 there is no binding label and warn the user if there is one. */
4052 if (tmp_sym->attr.access == ACCESS_PRIVATE
4053 && tmp_sym->binding_label)
4054 /* Use gfc_warning_now because we won't say that the symbol fails
4055 just because of this. */
4056 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
4057 "given the binding label '%s'", tmp_sym->name,
4058 &(tmp_sym->declared_at), tmp_sym->binding_label);
4060 return retval;
4064 /* Set the appropriate fields for a symbol that's been declared as
4065 BIND(C) (the is_bind_c flag and the binding label), and verify that
4066 the type is C interoperable. Errors are reported by the functions
4067 used to set/test these fields. */
4069 bool
4070 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4072 bool retval = true;
4074 /* TODO: Do we need to make sure the vars aren't marked private? */
4076 /* Set the is_bind_c bit in symbol_attribute. */
4077 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4079 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
4080 return false;
4082 return retval;
4086 /* Set the fields marking the given common block as BIND(C), including
4087 a binding label, and report any errors encountered. */
4089 bool
4090 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4092 bool retval = true;
4094 /* destLabel, common name, typespec (which may have binding label). */
4095 if (!set_binding_label (&com_block->binding_label, com_block->name,
4096 num_idents))
4097 return false;
4099 /* Set the given common block (com_block) to being bind(c) (1). */
4100 set_com_block_bind_c (com_block, 1);
4102 return retval;
4106 /* Retrieve the list of one or more identifiers that the given bind(c)
4107 attribute applies to. */
4109 bool
4110 get_bind_c_idents (void)
4112 char name[GFC_MAX_SYMBOL_LEN + 1];
4113 int num_idents = 0;
4114 gfc_symbol *tmp_sym = NULL;
4115 match found_id;
4116 gfc_common_head *com_block = NULL;
4118 if (gfc_match_name (name) == MATCH_YES)
4120 found_id = MATCH_YES;
4121 gfc_get_ha_symbol (name, &tmp_sym);
4123 else if (match_common_name (name) == MATCH_YES)
4125 found_id = MATCH_YES;
4126 com_block = gfc_get_common (name, 0);
4128 else
4130 gfc_error ("Need either entity or common block name for "
4131 "attribute specification statement at %C");
4132 return false;
4135 /* Save the current identifier and look for more. */
4138 /* Increment the number of identifiers found for this spec stmt. */
4139 num_idents++;
4141 /* Make sure we have a sym or com block, and verify that it can
4142 be bind(c). Set the appropriate field(s) and look for more
4143 identifiers. */
4144 if (tmp_sym != NULL || com_block != NULL)
4146 if (tmp_sym != NULL)
4148 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
4149 return false;
4151 else
4153 if (!set_verify_bind_c_com_block (com_block, num_idents))
4154 return false;
4157 /* Look to see if we have another identifier. */
4158 tmp_sym = NULL;
4159 if (gfc_match_eos () == MATCH_YES)
4160 found_id = MATCH_NO;
4161 else if (gfc_match_char (',') != MATCH_YES)
4162 found_id = MATCH_NO;
4163 else if (gfc_match_name (name) == MATCH_YES)
4165 found_id = MATCH_YES;
4166 gfc_get_ha_symbol (name, &tmp_sym);
4168 else if (match_common_name (name) == MATCH_YES)
4170 found_id = MATCH_YES;
4171 com_block = gfc_get_common (name, 0);
4173 else
4175 gfc_error ("Missing entity or common block name for "
4176 "attribute specification statement at %C");
4177 return false;
4180 else
4182 gfc_internal_error ("Missing symbol");
4184 } while (found_id == MATCH_YES);
4186 /* if we get here we were successful */
4187 return true;
4191 /* Try and match a BIND(C) attribute specification statement. */
4193 match
4194 gfc_match_bind_c_stmt (void)
4196 match found_match = MATCH_NO;
4197 gfc_typespec *ts;
4199 ts = &current_ts;
4201 /* This may not be necessary. */
4202 gfc_clear_ts (ts);
4203 /* Clear the temporary binding label holder. */
4204 curr_binding_label = NULL;
4206 /* Look for the bind(c). */
4207 found_match = gfc_match_bind_c (NULL, true);
4209 if (found_match == MATCH_YES)
4211 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
4212 return MATCH_ERROR;
4214 /* Look for the :: now, but it is not required. */
4215 gfc_match (" :: ");
4217 /* Get the identifier(s) that needs to be updated. This may need to
4218 change to hand the flag(s) for the attr specified so all identifiers
4219 found can have all appropriate parts updated (assuming that the same
4220 spec stmt can have multiple attrs, such as both bind(c) and
4221 allocatable...). */
4222 if (!get_bind_c_idents ())
4223 /* Error message should have printed already. */
4224 return MATCH_ERROR;
4227 return found_match;
4231 /* Match a data declaration statement. */
4233 match
4234 gfc_match_data_decl (void)
4236 gfc_symbol *sym;
4237 match m;
4238 int elem;
4240 num_idents_on_line = 0;
4242 m = gfc_match_decl_type_spec (&current_ts, 0);
4243 if (m != MATCH_YES)
4244 return m;
4246 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4247 && gfc_current_state () != COMP_DERIVED)
4249 sym = gfc_use_derived (current_ts.u.derived);
4251 if (sym == NULL)
4253 m = MATCH_ERROR;
4254 goto cleanup;
4257 current_ts.u.derived = sym;
4260 m = match_attr_spec ();
4261 if (m == MATCH_ERROR)
4263 m = MATCH_NO;
4264 goto cleanup;
4267 if (current_ts.type == BT_CLASS
4268 && current_ts.u.derived->attr.unlimited_polymorphic)
4269 goto ok;
4271 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4272 && current_ts.u.derived->components == NULL
4273 && !current_ts.u.derived->attr.zero_comp)
4276 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4277 goto ok;
4279 gfc_find_symbol (current_ts.u.derived->name,
4280 current_ts.u.derived->ns, 1, &sym);
4282 /* Any symbol that we find had better be a type definition
4283 which has its components defined. */
4284 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4285 && (current_ts.u.derived->components != NULL
4286 || current_ts.u.derived->attr.zero_comp))
4287 goto ok;
4289 /* Now we have an error, which we signal, and then fix up
4290 because the knock-on is plain and simple confusing. */
4291 gfc_error_now ("Derived type at %C has not been previously defined "
4292 "and so cannot appear in a derived type definition");
4293 current_attr.pointer = 1;
4294 goto ok;
4298 /* If we have an old-style character declaration, and no new-style
4299 attribute specifications, then there a comma is optional between
4300 the type specification and the variable list. */
4301 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4302 gfc_match_char (',');
4304 /* Give the types/attributes to symbols that follow. Give the element
4305 a number so that repeat character length expressions can be copied. */
4306 elem = 1;
4307 for (;;)
4309 num_idents_on_line++;
4310 m = variable_decl (elem++);
4311 if (m == MATCH_ERROR)
4312 goto cleanup;
4313 if (m == MATCH_NO)
4314 break;
4316 if (gfc_match_eos () == MATCH_YES)
4317 goto cleanup;
4318 if (gfc_match_char (',') != MATCH_YES)
4319 break;
4322 if (gfc_error_flag_test () == 0)
4323 gfc_error ("Syntax error in data declaration at %C");
4324 m = MATCH_ERROR;
4326 gfc_free_data_all (gfc_current_ns);
4328 cleanup:
4329 gfc_free_array_spec (current_as);
4330 current_as = NULL;
4331 return m;
4335 /* Match a prefix associated with a function or subroutine
4336 declaration. If the typespec pointer is nonnull, then a typespec
4337 can be matched. Note that if nothing matches, MATCH_YES is
4338 returned (the null string was matched). */
4340 match
4341 gfc_match_prefix (gfc_typespec *ts)
4343 bool seen_type;
4344 bool seen_impure;
4345 bool found_prefix;
4347 gfc_clear_attr (&current_attr);
4348 seen_type = false;
4349 seen_impure = false;
4351 gcc_assert (!gfc_matching_prefix);
4352 gfc_matching_prefix = true;
4356 found_prefix = false;
4358 if (!seen_type && ts != NULL
4359 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4360 && gfc_match_space () == MATCH_YES)
4363 seen_type = true;
4364 found_prefix = true;
4367 if (gfc_match ("elemental% ") == MATCH_YES)
4369 if (!gfc_add_elemental (&current_attr, NULL))
4370 goto error;
4372 found_prefix = true;
4375 if (gfc_match ("pure% ") == MATCH_YES)
4377 if (!gfc_add_pure (&current_attr, NULL))
4378 goto error;
4380 found_prefix = true;
4383 if (gfc_match ("recursive% ") == MATCH_YES)
4385 if (!gfc_add_recursive (&current_attr, NULL))
4386 goto error;
4388 found_prefix = true;
4391 /* IMPURE is a somewhat special case, as it needs not set an actual
4392 attribute but rather only prevents ELEMENTAL routines from being
4393 automatically PURE. */
4394 if (gfc_match ("impure% ") == MATCH_YES)
4396 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
4397 goto error;
4399 seen_impure = true;
4400 found_prefix = true;
4403 while (found_prefix);
4405 /* IMPURE and PURE must not both appear, of course. */
4406 if (seen_impure && current_attr.pure)
4408 gfc_error ("PURE and IMPURE must not appear both at %C");
4409 goto error;
4412 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4413 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4415 if (!gfc_add_pure (&current_attr, NULL))
4416 goto error;
4419 /* At this point, the next item is not a prefix. */
4420 gcc_assert (gfc_matching_prefix);
4421 gfc_matching_prefix = false;
4422 return MATCH_YES;
4424 error:
4425 gcc_assert (gfc_matching_prefix);
4426 gfc_matching_prefix = false;
4427 return MATCH_ERROR;
4431 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4433 static bool
4434 copy_prefix (symbol_attribute *dest, locus *where)
4436 if (current_attr.pure && !gfc_add_pure (dest, where))
4437 return false;
4439 if (current_attr.elemental && !gfc_add_elemental (dest, where))
4440 return false;
4442 if (current_attr.recursive && !gfc_add_recursive (dest, where))
4443 return false;
4445 return true;
4449 /* Match a formal argument list. */
4451 match
4452 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4454 gfc_formal_arglist *head, *tail, *p, *q;
4455 char name[GFC_MAX_SYMBOL_LEN + 1];
4456 gfc_symbol *sym;
4457 match m;
4459 head = tail = NULL;
4461 if (gfc_match_char ('(') != MATCH_YES)
4463 if (null_flag)
4464 goto ok;
4465 return MATCH_NO;
4468 if (gfc_match_char (')') == MATCH_YES)
4469 goto ok;
4471 for (;;)
4473 if (gfc_match_char ('*') == MATCH_YES)
4475 sym = NULL;
4476 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
4477 "at %C"))
4479 m = MATCH_ERROR;
4480 goto cleanup;
4483 else
4485 m = gfc_match_name (name);
4486 if (m != MATCH_YES)
4487 goto cleanup;
4489 if (gfc_get_symbol (name, NULL, &sym))
4490 goto cleanup;
4493 p = gfc_get_formal_arglist ();
4495 if (head == NULL)
4496 head = tail = p;
4497 else
4499 tail->next = p;
4500 tail = p;
4503 tail->sym = sym;
4505 /* We don't add the VARIABLE flavor because the name could be a
4506 dummy procedure. We don't apply these attributes to formal
4507 arguments of statement functions. */
4508 if (sym != NULL && !st_flag
4509 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
4510 || !gfc_missing_attr (&sym->attr, NULL)))
4512 m = MATCH_ERROR;
4513 goto cleanup;
4516 /* The name of a program unit can be in a different namespace,
4517 so check for it explicitly. After the statement is accepted,
4518 the name is checked for especially in gfc_get_symbol(). */
4519 if (gfc_new_block != NULL && sym != NULL
4520 && strcmp (sym->name, gfc_new_block->name) == 0)
4522 gfc_error ("Name '%s' at %C is the name of the procedure",
4523 sym->name);
4524 m = MATCH_ERROR;
4525 goto cleanup;
4528 if (gfc_match_char (')') == MATCH_YES)
4529 goto ok;
4531 m = gfc_match_char (',');
4532 if (m != MATCH_YES)
4534 gfc_error ("Unexpected junk in formal argument list at %C");
4535 goto cleanup;
4540 /* Check for duplicate symbols in the formal argument list. */
4541 if (head != NULL)
4543 for (p = head; p->next; p = p->next)
4545 if (p->sym == NULL)
4546 continue;
4548 for (q = p->next; q; q = q->next)
4549 if (p->sym == q->sym)
4551 gfc_error ("Duplicate symbol '%s' in formal argument list "
4552 "at %C", p->sym->name);
4554 m = MATCH_ERROR;
4555 goto cleanup;
4560 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
4562 m = MATCH_ERROR;
4563 goto cleanup;
4566 return MATCH_YES;
4568 cleanup:
4569 gfc_free_formal_arglist (head);
4570 return m;
4574 /* Match a RESULT specification following a function declaration or
4575 ENTRY statement. Also matches the end-of-statement. */
4577 static match
4578 match_result (gfc_symbol *function, gfc_symbol **result)
4580 char name[GFC_MAX_SYMBOL_LEN + 1];
4581 gfc_symbol *r;
4582 match m;
4584 if (gfc_match (" result (") != MATCH_YES)
4585 return MATCH_NO;
4587 m = gfc_match_name (name);
4588 if (m != MATCH_YES)
4589 return m;
4591 /* Get the right paren, and that's it because there could be the
4592 bind(c) attribute after the result clause. */
4593 if (gfc_match_char (')') != MATCH_YES)
4595 /* TODO: should report the missing right paren here. */
4596 return MATCH_ERROR;
4599 if (strcmp (function->name, name) == 0)
4601 gfc_error ("RESULT variable at %C must be different than function name");
4602 return MATCH_ERROR;
4605 if (gfc_get_symbol (name, NULL, &r))
4606 return MATCH_ERROR;
4608 if (!gfc_add_result (&r->attr, r->name, NULL))
4609 return MATCH_ERROR;
4611 *result = r;
4613 return MATCH_YES;
4617 /* Match a function suffix, which could be a combination of a result
4618 clause and BIND(C), either one, or neither. The draft does not
4619 require them to come in a specific order. */
4621 match
4622 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4624 match is_bind_c; /* Found bind(c). */
4625 match is_result; /* Found result clause. */
4626 match found_match; /* Status of whether we've found a good match. */
4627 char peek_char; /* Character we're going to peek at. */
4628 bool allow_binding_name;
4630 /* Initialize to having found nothing. */
4631 found_match = MATCH_NO;
4632 is_bind_c = MATCH_NO;
4633 is_result = MATCH_NO;
4635 /* Get the next char to narrow between result and bind(c). */
4636 gfc_gobble_whitespace ();
4637 peek_char = gfc_peek_ascii_char ();
4639 /* C binding names are not allowed for internal procedures. */
4640 if (gfc_current_state () == COMP_CONTAINS
4641 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4642 allow_binding_name = false;
4643 else
4644 allow_binding_name = true;
4646 switch (peek_char)
4648 case 'r':
4649 /* Look for result clause. */
4650 is_result = match_result (sym, result);
4651 if (is_result == MATCH_YES)
4653 /* Now see if there is a bind(c) after it. */
4654 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4655 /* We've found the result clause and possibly bind(c). */
4656 found_match = MATCH_YES;
4658 else
4659 /* This should only be MATCH_ERROR. */
4660 found_match = is_result;
4661 break;
4662 case 'b':
4663 /* Look for bind(c) first. */
4664 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4665 if (is_bind_c == MATCH_YES)
4667 /* Now see if a result clause followed it. */
4668 is_result = match_result (sym, result);
4669 found_match = MATCH_YES;
4671 else
4673 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4674 found_match = MATCH_ERROR;
4676 break;
4677 default:
4678 gfc_error ("Unexpected junk after function declaration at %C");
4679 found_match = MATCH_ERROR;
4680 break;
4683 if (is_bind_c == MATCH_YES)
4685 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4686 if (gfc_current_state () == COMP_CONTAINS
4687 && sym->ns->proc_name->attr.flavor != FL_MODULE
4688 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
4689 "at %L may not be specified for an internal "
4690 "procedure", &gfc_current_locus))
4691 return MATCH_ERROR;
4693 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
4694 return MATCH_ERROR;
4697 return found_match;
4701 /* Procedure pointer return value without RESULT statement:
4702 Add "hidden" result variable named "ppr@". */
4704 static bool
4705 add_hidden_procptr_result (gfc_symbol *sym)
4707 bool case1,case2;
4709 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4710 return false;
4712 /* First usage case: PROCEDURE and EXTERNAL statements. */
4713 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4714 && strcmp (gfc_current_block ()->name, sym->name) == 0
4715 && sym->attr.external;
4716 /* Second usage case: INTERFACE statements. */
4717 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4718 && gfc_state_stack->previous->state == COMP_FUNCTION
4719 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4721 if (case1 || case2)
4723 gfc_symtree *stree;
4724 if (case1)
4725 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4726 else if (case2)
4728 gfc_symtree *st2;
4729 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4730 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4731 st2->n.sym = stree->n.sym;
4733 sym->result = stree->n.sym;
4735 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4736 sym->result->attr.pointer = sym->attr.pointer;
4737 sym->result->attr.external = sym->attr.external;
4738 sym->result->attr.referenced = sym->attr.referenced;
4739 sym->result->ts = sym->ts;
4740 sym->attr.proc_pointer = 0;
4741 sym->attr.pointer = 0;
4742 sym->attr.external = 0;
4743 if (sym->result->attr.external && sym->result->attr.pointer)
4745 sym->result->attr.pointer = 0;
4746 sym->result->attr.proc_pointer = 1;
4749 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4751 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4752 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4753 && sym->result && sym->result != sym && sym->result->attr.external
4754 && sym == gfc_current_ns->proc_name
4755 && sym == sym->result->ns->proc_name
4756 && strcmp ("ppr@", sym->result->name) == 0)
4758 sym->result->attr.proc_pointer = 1;
4759 sym->attr.pointer = 0;
4760 return true;
4762 else
4763 return false;
4767 /* Match the interface for a PROCEDURE declaration,
4768 including brackets (R1212). */
4770 static match
4771 match_procedure_interface (gfc_symbol **proc_if)
4773 match m;
4774 gfc_symtree *st;
4775 locus old_loc, entry_loc;
4776 gfc_namespace *old_ns = gfc_current_ns;
4777 char name[GFC_MAX_SYMBOL_LEN + 1];
4779 old_loc = entry_loc = gfc_current_locus;
4780 gfc_clear_ts (&current_ts);
4782 if (gfc_match (" (") != MATCH_YES)
4784 gfc_current_locus = entry_loc;
4785 return MATCH_NO;
4788 /* Get the type spec. for the procedure interface. */
4789 old_loc = gfc_current_locus;
4790 m = gfc_match_decl_type_spec (&current_ts, 0);
4791 gfc_gobble_whitespace ();
4792 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4793 goto got_ts;
4795 if (m == MATCH_ERROR)
4796 return m;
4798 /* Procedure interface is itself a procedure. */
4799 gfc_current_locus = old_loc;
4800 m = gfc_match_name (name);
4802 /* First look to see if it is already accessible in the current
4803 namespace because it is use associated or contained. */
4804 st = NULL;
4805 if (gfc_find_sym_tree (name, NULL, 0, &st))
4806 return MATCH_ERROR;
4808 /* If it is still not found, then try the parent namespace, if it
4809 exists and create the symbol there if it is still not found. */
4810 if (gfc_current_ns->parent)
4811 gfc_current_ns = gfc_current_ns->parent;
4812 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4813 return MATCH_ERROR;
4815 gfc_current_ns = old_ns;
4816 *proc_if = st->n.sym;
4818 if (*proc_if)
4820 (*proc_if)->refs++;
4821 /* Resolve interface if possible. That way, attr.procedure is only set
4822 if it is declared by a later procedure-declaration-stmt, which is
4823 invalid per F08:C1216 (cf. resolve_procedure_interface). */
4824 while ((*proc_if)->ts.interface)
4825 *proc_if = (*proc_if)->ts.interface;
4827 if ((*proc_if)->attr.flavor == FL_UNKNOWN
4828 && (*proc_if)->ts.type == BT_UNKNOWN
4829 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
4830 (*proc_if)->name, NULL))
4831 return MATCH_ERROR;
4834 got_ts:
4835 if (gfc_match (" )") != MATCH_YES)
4837 gfc_current_locus = entry_loc;
4838 return MATCH_NO;
4841 return MATCH_YES;
4845 /* Match a PROCEDURE declaration (R1211). */
4847 static match
4848 match_procedure_decl (void)
4850 match m;
4851 gfc_symbol *sym, *proc_if = NULL;
4852 int num;
4853 gfc_expr *initializer = NULL;
4855 /* Parse interface (with brackets). */
4856 m = match_procedure_interface (&proc_if);
4857 if (m != MATCH_YES)
4858 return m;
4860 /* Parse attributes (with colons). */
4861 m = match_attr_spec();
4862 if (m == MATCH_ERROR)
4863 return MATCH_ERROR;
4865 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
4867 current_attr.is_bind_c = 1;
4868 has_name_equals = 0;
4869 curr_binding_label = NULL;
4872 /* Get procedure symbols. */
4873 for(num=1;;num++)
4875 m = gfc_match_symbol (&sym, 0);
4876 if (m == MATCH_NO)
4877 goto syntax;
4878 else if (m == MATCH_ERROR)
4879 return m;
4881 /* Add current_attr to the symbol attributes. */
4882 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
4883 return MATCH_ERROR;
4885 if (sym->attr.is_bind_c)
4887 /* Check for C1218. */
4888 if (!proc_if || !proc_if->attr.is_bind_c)
4890 gfc_error ("BIND(C) attribute at %C requires "
4891 "an interface with BIND(C)");
4892 return MATCH_ERROR;
4894 /* Check for C1217. */
4895 if (has_name_equals && sym->attr.pointer)
4897 gfc_error ("BIND(C) procedure with NAME may not have "
4898 "POINTER attribute at %C");
4899 return MATCH_ERROR;
4901 if (has_name_equals && sym->attr.dummy)
4903 gfc_error ("Dummy procedure at %C may not have "
4904 "BIND(C) attribute with NAME");
4905 return MATCH_ERROR;
4907 /* Set binding label for BIND(C). */
4908 if (!set_binding_label (&sym->binding_label, sym->name, num))
4909 return MATCH_ERROR;
4912 if (!gfc_add_external (&sym->attr, NULL))
4913 return MATCH_ERROR;
4915 if (add_hidden_procptr_result (sym))
4916 sym = sym->result;
4918 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
4919 return MATCH_ERROR;
4921 /* Set interface. */
4922 if (proc_if != NULL)
4924 if (sym->ts.type != BT_UNKNOWN)
4926 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4927 sym->name, &gfc_current_locus,
4928 gfc_basic_typename (sym->ts.type));
4929 return MATCH_ERROR;
4931 sym->ts.interface = proc_if;
4932 sym->attr.untyped = 1;
4933 sym->attr.if_source = IFSRC_IFBODY;
4935 else if (current_ts.type != BT_UNKNOWN)
4937 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
4938 return MATCH_ERROR;
4939 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4940 sym->ts.interface->ts = current_ts;
4941 sym->ts.interface->attr.flavor = FL_PROCEDURE;
4942 sym->ts.interface->attr.function = 1;
4943 sym->attr.function = 1;
4944 sym->attr.if_source = IFSRC_UNKNOWN;
4947 if (gfc_match (" =>") == MATCH_YES)
4949 if (!current_attr.pointer)
4951 gfc_error ("Initialization at %C isn't for a pointer variable");
4952 m = MATCH_ERROR;
4953 goto cleanup;
4956 m = match_pointer_init (&initializer, 1);
4957 if (m != MATCH_YES)
4958 goto cleanup;
4960 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
4961 goto cleanup;
4965 if (gfc_match_eos () == MATCH_YES)
4966 return MATCH_YES;
4967 if (gfc_match_char (',') != MATCH_YES)
4968 goto syntax;
4971 syntax:
4972 gfc_error ("Syntax error in PROCEDURE statement at %C");
4973 return MATCH_ERROR;
4975 cleanup:
4976 /* Free stuff up and return. */
4977 gfc_free_expr (initializer);
4978 return m;
4982 static match
4983 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4986 /* Match a procedure pointer component declaration (R445). */
4988 static match
4989 match_ppc_decl (void)
4991 match m;
4992 gfc_symbol *proc_if = NULL;
4993 gfc_typespec ts;
4994 int num;
4995 gfc_component *c;
4996 gfc_expr *initializer = NULL;
4997 gfc_typebound_proc* tb;
4998 char name[GFC_MAX_SYMBOL_LEN + 1];
5000 /* Parse interface (with brackets). */
5001 m = match_procedure_interface (&proc_if);
5002 if (m != MATCH_YES)
5003 goto syntax;
5005 /* Parse attributes. */
5006 tb = XCNEW (gfc_typebound_proc);
5007 tb->where = gfc_current_locus;
5008 m = match_binding_attributes (tb, false, true);
5009 if (m == MATCH_ERROR)
5010 return m;
5012 gfc_clear_attr (&current_attr);
5013 current_attr.procedure = 1;
5014 current_attr.proc_pointer = 1;
5015 current_attr.access = tb->access;
5016 current_attr.flavor = FL_PROCEDURE;
5018 /* Match the colons (required). */
5019 if (gfc_match (" ::") != MATCH_YES)
5021 gfc_error ("Expected '::' after binding-attributes at %C");
5022 return MATCH_ERROR;
5025 /* Check for C450. */
5026 if (!tb->nopass && proc_if == NULL)
5028 gfc_error("NOPASS or explicit interface required at %C");
5029 return MATCH_ERROR;
5032 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
5033 return MATCH_ERROR;
5035 /* Match PPC names. */
5036 ts = current_ts;
5037 for(num=1;;num++)
5039 m = gfc_match_name (name);
5040 if (m == MATCH_NO)
5041 goto syntax;
5042 else if (m == MATCH_ERROR)
5043 return m;
5045 if (!gfc_add_component (gfc_current_block(), name, &c))
5046 return MATCH_ERROR;
5048 /* Add current_attr to the symbol attributes. */
5049 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
5050 return MATCH_ERROR;
5052 if (!gfc_add_external (&c->attr, NULL))
5053 return MATCH_ERROR;
5055 if (!gfc_add_proc (&c->attr, name, NULL))
5056 return MATCH_ERROR;
5058 if (num == 1)
5059 c->tb = tb;
5060 else
5062 c->tb = XCNEW (gfc_typebound_proc);
5063 c->tb->where = gfc_current_locus;
5064 *c->tb = *tb;
5067 /* Set interface. */
5068 if (proc_if != NULL)
5070 c->ts.interface = proc_if;
5071 c->attr.untyped = 1;
5072 c->attr.if_source = IFSRC_IFBODY;
5074 else if (ts.type != BT_UNKNOWN)
5076 c->ts = ts;
5077 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5078 c->ts.interface->result = c->ts.interface;
5079 c->ts.interface->ts = ts;
5080 c->ts.interface->attr.flavor = FL_PROCEDURE;
5081 c->ts.interface->attr.function = 1;
5082 c->attr.function = 1;
5083 c->attr.if_source = IFSRC_UNKNOWN;
5086 if (gfc_match (" =>") == MATCH_YES)
5088 m = match_pointer_init (&initializer, 1);
5089 if (m != MATCH_YES)
5091 gfc_free_expr (initializer);
5092 return m;
5094 c->initializer = initializer;
5097 if (gfc_match_eos () == MATCH_YES)
5098 return MATCH_YES;
5099 if (gfc_match_char (',') != MATCH_YES)
5100 goto syntax;
5103 syntax:
5104 gfc_error ("Syntax error in procedure pointer component at %C");
5105 return MATCH_ERROR;
5109 /* Match a PROCEDURE declaration inside an interface (R1206). */
5111 static match
5112 match_procedure_in_interface (void)
5114 match m;
5115 gfc_symbol *sym;
5116 char name[GFC_MAX_SYMBOL_LEN + 1];
5117 locus old_locus;
5119 if (current_interface.type == INTERFACE_NAMELESS
5120 || current_interface.type == INTERFACE_ABSTRACT)
5122 gfc_error ("PROCEDURE at %C must be in a generic interface");
5123 return MATCH_ERROR;
5126 /* Check if the F2008 optional double colon appears. */
5127 gfc_gobble_whitespace ();
5128 old_locus = gfc_current_locus;
5129 if (gfc_match ("::") == MATCH_YES)
5131 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
5132 "MODULE PROCEDURE statement at %L", &old_locus))
5133 return MATCH_ERROR;
5135 else
5136 gfc_current_locus = old_locus;
5138 for(;;)
5140 m = gfc_match_name (name);
5141 if (m == MATCH_NO)
5142 goto syntax;
5143 else if (m == MATCH_ERROR)
5144 return m;
5145 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5146 return MATCH_ERROR;
5148 if (!gfc_add_interface (sym))
5149 return MATCH_ERROR;
5151 if (gfc_match_eos () == MATCH_YES)
5152 break;
5153 if (gfc_match_char (',') != MATCH_YES)
5154 goto syntax;
5157 return MATCH_YES;
5159 syntax:
5160 gfc_error ("Syntax error in PROCEDURE statement at %C");
5161 return MATCH_ERROR;
5165 /* General matcher for PROCEDURE declarations. */
5167 static match match_procedure_in_type (void);
5169 match
5170 gfc_match_procedure (void)
5172 match m;
5174 switch (gfc_current_state ())
5176 case COMP_NONE:
5177 case COMP_PROGRAM:
5178 case COMP_MODULE:
5179 case COMP_SUBROUTINE:
5180 case COMP_FUNCTION:
5181 case COMP_BLOCK:
5182 m = match_procedure_decl ();
5183 break;
5184 case COMP_INTERFACE:
5185 m = match_procedure_in_interface ();
5186 break;
5187 case COMP_DERIVED:
5188 m = match_ppc_decl ();
5189 break;
5190 case COMP_DERIVED_CONTAINS:
5191 m = match_procedure_in_type ();
5192 break;
5193 default:
5194 return MATCH_NO;
5197 if (m != MATCH_YES)
5198 return m;
5200 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
5201 return MATCH_ERROR;
5203 return m;
5207 /* Warn if a matched procedure has the same name as an intrinsic; this is
5208 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5209 parser-state-stack to find out whether we're in a module. */
5211 static void
5212 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5214 bool in_module;
5216 in_module = (gfc_state_stack->previous
5217 && gfc_state_stack->previous->state == COMP_MODULE);
5219 gfc_warn_intrinsic_shadow (sym, in_module, func);
5223 /* Match a function declaration. */
5225 match
5226 gfc_match_function_decl (void)
5228 char name[GFC_MAX_SYMBOL_LEN + 1];
5229 gfc_symbol *sym, *result;
5230 locus old_loc;
5231 match m;
5232 match suffix_match;
5233 match found_match; /* Status returned by match func. */
5235 if (gfc_current_state () != COMP_NONE
5236 && gfc_current_state () != COMP_INTERFACE
5237 && gfc_current_state () != COMP_CONTAINS)
5238 return MATCH_NO;
5240 gfc_clear_ts (&current_ts);
5242 old_loc = gfc_current_locus;
5244 m = gfc_match_prefix (&current_ts);
5245 if (m != MATCH_YES)
5247 gfc_current_locus = old_loc;
5248 return m;
5251 if (gfc_match ("function% %n", name) != MATCH_YES)
5253 gfc_current_locus = old_loc;
5254 return MATCH_NO;
5256 if (get_proc_name (name, &sym, false))
5257 return MATCH_ERROR;
5259 if (add_hidden_procptr_result (sym))
5260 sym = sym->result;
5262 gfc_new_block = sym;
5264 m = gfc_match_formal_arglist (sym, 0, 0);
5265 if (m == MATCH_NO)
5267 gfc_error ("Expected formal argument list in function "
5268 "definition at %C");
5269 m = MATCH_ERROR;
5270 goto cleanup;
5272 else if (m == MATCH_ERROR)
5273 goto cleanup;
5275 result = NULL;
5277 /* According to the draft, the bind(c) and result clause can
5278 come in either order after the formal_arg_list (i.e., either
5279 can be first, both can exist together or by themselves or neither
5280 one). Therefore, the match_result can't match the end of the
5281 string, and check for the bind(c) or result clause in either order. */
5282 found_match = gfc_match_eos ();
5284 /* Make sure that it isn't already declared as BIND(C). If it is, it
5285 must have been marked BIND(C) with a BIND(C) attribute and that is
5286 not allowed for procedures. */
5287 if (sym->attr.is_bind_c == 1)
5289 sym->attr.is_bind_c = 0;
5290 if (sym->old_symbol != NULL)
5291 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5292 "variables or common blocks",
5293 &(sym->old_symbol->declared_at));
5294 else
5295 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5296 "variables or common blocks", &gfc_current_locus);
5299 if (found_match != MATCH_YES)
5301 /* If we haven't found the end-of-statement, look for a suffix. */
5302 suffix_match = gfc_match_suffix (sym, &result);
5303 if (suffix_match == MATCH_YES)
5304 /* Need to get the eos now. */
5305 found_match = gfc_match_eos ();
5306 else
5307 found_match = suffix_match;
5310 if(found_match != MATCH_YES)
5311 m = MATCH_ERROR;
5312 else
5314 /* Make changes to the symbol. */
5315 m = MATCH_ERROR;
5317 if (!gfc_add_function (&sym->attr, sym->name, NULL))
5318 goto cleanup;
5320 if (!gfc_missing_attr (&sym->attr, NULL)
5321 || !copy_prefix (&sym->attr, &sym->declared_at))
5322 goto cleanup;
5324 /* Delay matching the function characteristics until after the
5325 specification block by signalling kind=-1. */
5326 sym->declared_at = old_loc;
5327 if (current_ts.type != BT_UNKNOWN)
5328 current_ts.kind = -1;
5329 else
5330 current_ts.kind = 0;
5332 if (result == NULL)
5334 if (current_ts.type != BT_UNKNOWN
5335 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
5336 goto cleanup;
5337 sym->result = sym;
5339 else
5341 if (current_ts.type != BT_UNKNOWN
5342 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
5343 goto cleanup;
5344 sym->result = result;
5347 /* Warn if this procedure has the same name as an intrinsic. */
5348 warn_intrinsic_shadow (sym, true);
5350 return MATCH_YES;
5353 cleanup:
5354 gfc_current_locus = old_loc;
5355 return m;
5359 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5360 pass the name of the entry, rather than the gfc_current_block name, and
5361 to return false upon finding an existing global entry. */
5363 static bool
5364 add_global_entry (const char *name, const char *binding_label, bool sub,
5365 locus *where)
5367 gfc_gsymbol *s;
5368 enum gfc_symbol_type type;
5370 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5372 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5373 name is a global identifier. */
5374 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
5376 s = gfc_get_gsymbol (name);
5378 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5380 gfc_global_used (s, where);
5381 return false;
5383 else
5385 s->type = type;
5386 s->sym_name = name;
5387 s->where = *where;
5388 s->defined = 1;
5389 s->ns = gfc_current_ns;
5393 /* Don't add the symbol multiple times. */
5394 if (binding_label
5395 && (!gfc_notification_std (GFC_STD_F2008)
5396 || strcmp (name, binding_label) != 0))
5398 s = gfc_get_gsymbol (binding_label);
5400 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5402 gfc_global_used (s, where);
5403 return false;
5405 else
5407 s->type = type;
5408 s->sym_name = name;
5409 s->binding_label = binding_label;
5410 s->where = *where;
5411 s->defined = 1;
5412 s->ns = gfc_current_ns;
5416 return true;
5420 /* Match an ENTRY statement. */
5422 match
5423 gfc_match_entry (void)
5425 gfc_symbol *proc;
5426 gfc_symbol *result;
5427 gfc_symbol *entry;
5428 char name[GFC_MAX_SYMBOL_LEN + 1];
5429 gfc_compile_state state;
5430 match m;
5431 gfc_entry_list *el;
5432 locus old_loc;
5433 bool module_procedure;
5434 char peek_char;
5435 match is_bind_c;
5437 m = gfc_match_name (name);
5438 if (m != MATCH_YES)
5439 return m;
5441 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
5442 return MATCH_ERROR;
5444 state = gfc_current_state ();
5445 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5447 switch (state)
5449 case COMP_PROGRAM:
5450 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5451 break;
5452 case COMP_MODULE:
5453 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5454 break;
5455 case COMP_BLOCK_DATA:
5456 gfc_error ("ENTRY statement at %C cannot appear within "
5457 "a BLOCK DATA");
5458 break;
5459 case COMP_INTERFACE:
5460 gfc_error ("ENTRY statement at %C cannot appear within "
5461 "an INTERFACE");
5462 break;
5463 case COMP_DERIVED:
5464 gfc_error ("ENTRY statement at %C cannot appear within "
5465 "a DERIVED TYPE block");
5466 break;
5467 case COMP_IF:
5468 gfc_error ("ENTRY statement at %C cannot appear within "
5469 "an IF-THEN block");
5470 break;
5471 case COMP_DO:
5472 case COMP_DO_CONCURRENT:
5473 gfc_error ("ENTRY statement at %C cannot appear within "
5474 "a DO block");
5475 break;
5476 case COMP_SELECT:
5477 gfc_error ("ENTRY statement at %C cannot appear within "
5478 "a SELECT block");
5479 break;
5480 case COMP_FORALL:
5481 gfc_error ("ENTRY statement at %C cannot appear within "
5482 "a FORALL block");
5483 break;
5484 case COMP_WHERE:
5485 gfc_error ("ENTRY statement at %C cannot appear within "
5486 "a WHERE block");
5487 break;
5488 case COMP_CONTAINS:
5489 gfc_error ("ENTRY statement at %C cannot appear within "
5490 "a contained subprogram");
5491 break;
5492 default:
5493 gfc_internal_error ("gfc_match_entry(): Bad state");
5495 return MATCH_ERROR;
5498 module_procedure = gfc_current_ns->parent != NULL
5499 && gfc_current_ns->parent->proc_name
5500 && gfc_current_ns->parent->proc_name->attr.flavor
5501 == FL_MODULE;
5503 if (gfc_current_ns->parent != NULL
5504 && gfc_current_ns->parent->proc_name
5505 && !module_procedure)
5507 gfc_error("ENTRY statement at %C cannot appear in a "
5508 "contained procedure");
5509 return MATCH_ERROR;
5512 /* Module function entries need special care in get_proc_name
5513 because previous references within the function will have
5514 created symbols attached to the current namespace. */
5515 if (get_proc_name (name, &entry,
5516 gfc_current_ns->parent != NULL
5517 && module_procedure))
5518 return MATCH_ERROR;
5520 proc = gfc_current_block ();
5522 /* Make sure that it isn't already declared as BIND(C). If it is, it
5523 must have been marked BIND(C) with a BIND(C) attribute and that is
5524 not allowed for procedures. */
5525 if (entry->attr.is_bind_c == 1)
5527 entry->attr.is_bind_c = 0;
5528 if (entry->old_symbol != NULL)
5529 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5530 "variables or common blocks",
5531 &(entry->old_symbol->declared_at));
5532 else
5533 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5534 "variables or common blocks", &gfc_current_locus);
5537 /* Check what next non-whitespace character is so we can tell if there
5538 is the required parens if we have a BIND(C). */
5539 old_loc = gfc_current_locus;
5540 gfc_gobble_whitespace ();
5541 peek_char = gfc_peek_ascii_char ();
5543 if (state == COMP_SUBROUTINE)
5545 m = gfc_match_formal_arglist (entry, 0, 1);
5546 if (m != MATCH_YES)
5547 return MATCH_ERROR;
5549 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5550 never be an internal procedure. */
5551 is_bind_c = gfc_match_bind_c (entry, true);
5552 if (is_bind_c == MATCH_ERROR)
5553 return MATCH_ERROR;
5554 if (is_bind_c == MATCH_YES)
5556 if (peek_char != '(')
5558 gfc_error ("Missing required parentheses before BIND(C) at %C");
5559 return MATCH_ERROR;
5561 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
5562 &(entry->declared_at), 1))
5563 return MATCH_ERROR;
5566 if (!gfc_current_ns->parent
5567 && !add_global_entry (name, entry->binding_label, true,
5568 &old_loc))
5569 return MATCH_ERROR;
5571 /* An entry in a subroutine. */
5572 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5573 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
5574 return MATCH_ERROR;
5576 else
5578 /* An entry in a function.
5579 We need to take special care because writing
5580 ENTRY f()
5582 ENTRY f
5583 is allowed, whereas
5584 ENTRY f() RESULT (r)
5585 can't be written as
5586 ENTRY f RESULT (r). */
5587 if (gfc_match_eos () == MATCH_YES)
5589 gfc_current_locus = old_loc;
5590 /* Match the empty argument list, and add the interface to
5591 the symbol. */
5592 m = gfc_match_formal_arglist (entry, 0, 1);
5594 else
5595 m = gfc_match_formal_arglist (entry, 0, 0);
5597 if (m != MATCH_YES)
5598 return MATCH_ERROR;
5600 result = NULL;
5602 if (gfc_match_eos () == MATCH_YES)
5604 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5605 || !gfc_add_function (&entry->attr, entry->name, NULL))
5606 return MATCH_ERROR;
5608 entry->result = entry;
5610 else
5612 m = gfc_match_suffix (entry, &result);
5613 if (m == MATCH_NO)
5614 gfc_syntax_error (ST_ENTRY);
5615 if (m != MATCH_YES)
5616 return MATCH_ERROR;
5618 if (result)
5620 if (!gfc_add_result (&result->attr, result->name, NULL)
5621 || !gfc_add_entry (&entry->attr, result->name, NULL)
5622 || !gfc_add_function (&entry->attr, result->name, NULL))
5623 return MATCH_ERROR;
5624 entry->result = result;
5626 else
5628 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5629 || !gfc_add_function (&entry->attr, entry->name, NULL))
5630 return MATCH_ERROR;
5631 entry->result = entry;
5635 if (!gfc_current_ns->parent
5636 && !add_global_entry (name, entry->binding_label, false,
5637 &old_loc))
5638 return MATCH_ERROR;
5641 if (gfc_match_eos () != MATCH_YES)
5643 gfc_syntax_error (ST_ENTRY);
5644 return MATCH_ERROR;
5647 entry->attr.recursive = proc->attr.recursive;
5648 entry->attr.elemental = proc->attr.elemental;
5649 entry->attr.pure = proc->attr.pure;
5651 el = gfc_get_entry_list ();
5652 el->sym = entry;
5653 el->next = gfc_current_ns->entries;
5654 gfc_current_ns->entries = el;
5655 if (el->next)
5656 el->id = el->next->id + 1;
5657 else
5658 el->id = 1;
5660 new_st.op = EXEC_ENTRY;
5661 new_st.ext.entry = el;
5663 return MATCH_YES;
5667 /* Match a subroutine statement, including optional prefixes. */
5669 match
5670 gfc_match_subroutine (void)
5672 char name[GFC_MAX_SYMBOL_LEN + 1];
5673 gfc_symbol *sym;
5674 match m;
5675 match is_bind_c;
5676 char peek_char;
5677 bool allow_binding_name;
5679 if (gfc_current_state () != COMP_NONE
5680 && gfc_current_state () != COMP_INTERFACE
5681 && gfc_current_state () != COMP_CONTAINS)
5682 return MATCH_NO;
5684 m = gfc_match_prefix (NULL);
5685 if (m != MATCH_YES)
5686 return m;
5688 m = gfc_match ("subroutine% %n", name);
5689 if (m != MATCH_YES)
5690 return m;
5692 if (get_proc_name (name, &sym, false))
5693 return MATCH_ERROR;
5695 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5696 the symbol existed before. */
5697 sym->declared_at = gfc_current_locus;
5699 if (add_hidden_procptr_result (sym))
5700 sym = sym->result;
5702 gfc_new_block = sym;
5704 /* Check what next non-whitespace character is so we can tell if there
5705 is the required parens if we have a BIND(C). */
5706 gfc_gobble_whitespace ();
5707 peek_char = gfc_peek_ascii_char ();
5709 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
5710 return MATCH_ERROR;
5712 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5713 return MATCH_ERROR;
5715 /* Make sure that it isn't already declared as BIND(C). If it is, it
5716 must have been marked BIND(C) with a BIND(C) attribute and that is
5717 not allowed for procedures. */
5718 if (sym->attr.is_bind_c == 1)
5720 sym->attr.is_bind_c = 0;
5721 if (sym->old_symbol != NULL)
5722 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5723 "variables or common blocks",
5724 &(sym->old_symbol->declared_at));
5725 else
5726 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5727 "variables or common blocks", &gfc_current_locus);
5730 /* C binding names are not allowed for internal procedures. */
5731 if (gfc_current_state () == COMP_CONTAINS
5732 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5733 allow_binding_name = false;
5734 else
5735 allow_binding_name = true;
5737 /* Here, we are just checking if it has the bind(c) attribute, and if
5738 so, then we need to make sure it's all correct. If it doesn't,
5739 we still need to continue matching the rest of the subroutine line. */
5740 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5741 if (is_bind_c == MATCH_ERROR)
5743 /* There was an attempt at the bind(c), but it was wrong. An
5744 error message should have been printed w/in the gfc_match_bind_c
5745 so here we'll just return the MATCH_ERROR. */
5746 return MATCH_ERROR;
5749 if (is_bind_c == MATCH_YES)
5751 /* The following is allowed in the Fortran 2008 draft. */
5752 if (gfc_current_state () == COMP_CONTAINS
5753 && sym->ns->proc_name->attr.flavor != FL_MODULE
5754 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
5755 "at %L may not be specified for an internal "
5756 "procedure", &gfc_current_locus))
5757 return MATCH_ERROR;
5759 if (peek_char != '(')
5761 gfc_error ("Missing required parentheses before BIND(C) at %C");
5762 return MATCH_ERROR;
5764 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
5765 &(sym->declared_at), 1))
5766 return MATCH_ERROR;
5769 if (gfc_match_eos () != MATCH_YES)
5771 gfc_syntax_error (ST_SUBROUTINE);
5772 return MATCH_ERROR;
5775 if (!copy_prefix (&sym->attr, &sym->declared_at))
5776 return MATCH_ERROR;
5778 /* Warn if it has the same name as an intrinsic. */
5779 warn_intrinsic_shadow (sym, false);
5781 return MATCH_YES;
5785 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5786 given, and set the binding label in either the given symbol (if not
5787 NULL), or in the current_ts. The symbol may be NULL because we may
5788 encounter the BIND(C) before the declaration itself. Return
5789 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5790 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5791 or MATCH_YES if the specifier was correct and the binding label and
5792 bind(c) fields were set correctly for the given symbol or the
5793 current_ts. If allow_binding_name is false, no binding name may be
5794 given. */
5796 match
5797 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5799 /* binding label, if exists */
5800 const char* binding_label = NULL;
5801 match double_quote;
5802 match single_quote;
5804 /* Initialize the flag that specifies whether we encountered a NAME=
5805 specifier or not. */
5806 has_name_equals = 0;
5808 /* This much we have to be able to match, in this order, if
5809 there is a bind(c) label. */
5810 if (gfc_match (" bind ( c ") != MATCH_YES)
5811 return MATCH_NO;
5813 /* Now see if there is a binding label, or if we've reached the
5814 end of the bind(c) attribute without one. */
5815 if (gfc_match_char (',') == MATCH_YES)
5817 if (gfc_match (" name = ") != MATCH_YES)
5819 gfc_error ("Syntax error in NAME= specifier for binding label "
5820 "at %C");
5821 /* should give an error message here */
5822 return MATCH_ERROR;
5825 has_name_equals = 1;
5827 /* Get the opening quote. */
5828 double_quote = MATCH_YES;
5829 single_quote = MATCH_YES;
5830 double_quote = gfc_match_char ('"');
5831 if (double_quote != MATCH_YES)
5832 single_quote = gfc_match_char ('\'');
5833 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5835 gfc_error ("Syntax error in NAME= specifier for binding label "
5836 "at %C");
5837 return MATCH_ERROR;
5840 /* Grab the binding label, using functions that will not lower
5841 case the names automatically. */
5842 if (gfc_match_name_C (&binding_label) != MATCH_YES)
5843 return MATCH_ERROR;
5845 /* Get the closing quotation. */
5846 if (double_quote == MATCH_YES)
5848 if (gfc_match_char ('"') != MATCH_YES)
5850 gfc_error ("Missing closing quote '\"' for binding label at %C");
5851 /* User started string with '"' so looked to match it. */
5852 return MATCH_ERROR;
5855 else
5857 if (gfc_match_char ('\'') != MATCH_YES)
5859 gfc_error ("Missing closing quote '\'' for binding label at %C");
5860 /* User started string with "'" char. */
5861 return MATCH_ERROR;
5866 /* Get the required right paren. */
5867 if (gfc_match_char (')') != MATCH_YES)
5869 gfc_error ("Missing closing paren for binding label at %C");
5870 return MATCH_ERROR;
5873 if (has_name_equals && !allow_binding_name)
5875 gfc_error ("No binding name is allowed in BIND(C) at %C");
5876 return MATCH_ERROR;
5879 if (has_name_equals && sym != NULL && sym->attr.dummy)
5881 gfc_error ("For dummy procedure %s, no binding name is "
5882 "allowed in BIND(C) at %C", sym->name);
5883 return MATCH_ERROR;
5887 /* Save the binding label to the symbol. If sym is null, we're
5888 probably matching the typespec attributes of a declaration and
5889 haven't gotten the name yet, and therefore, no symbol yet. */
5890 if (binding_label)
5892 if (sym != NULL)
5893 sym->binding_label = binding_label;
5894 else
5895 curr_binding_label = binding_label;
5897 else if (allow_binding_name)
5899 /* No binding label, but if symbol isn't null, we
5900 can set the label for it here.
5901 If name="" or allow_binding_name is false, no C binding name is
5902 created. */
5903 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5904 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
5907 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5908 && current_interface.type == INTERFACE_ABSTRACT)
5910 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5911 return MATCH_ERROR;
5914 return MATCH_YES;
5918 /* Return nonzero if we're currently compiling a contained procedure. */
5920 static int
5921 contained_procedure (void)
5923 gfc_state_data *s = gfc_state_stack;
5925 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5926 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5927 return 1;
5929 return 0;
5932 /* Set the kind of each enumerator. The kind is selected such that it is
5933 interoperable with the corresponding C enumeration type, making
5934 sure that -fshort-enums is honored. */
5936 static void
5937 set_enum_kind(void)
5939 enumerator_history *current_history = NULL;
5940 int kind;
5941 int i;
5943 if (max_enum == NULL || enum_history == NULL)
5944 return;
5946 if (!flag_short_enums)
5947 return;
5949 i = 0;
5952 kind = gfc_integer_kinds[i++].kind;
5954 while (kind < gfc_c_int_kind
5955 && gfc_check_integer_range (max_enum->initializer->value.integer,
5956 kind) != ARITH_OK);
5958 current_history = enum_history;
5959 while (current_history != NULL)
5961 current_history->sym->ts.kind = kind;
5962 current_history = current_history->next;
5967 /* Match any of the various end-block statements. Returns the type of
5968 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5969 and END BLOCK statements cannot be replaced by a single END statement. */
5971 match
5972 gfc_match_end (gfc_statement *st)
5974 char name[GFC_MAX_SYMBOL_LEN + 1];
5975 gfc_compile_state state;
5976 locus old_loc;
5977 const char *block_name;
5978 const char *target;
5979 int eos_ok;
5980 match m;
5981 gfc_namespace *parent_ns, *ns, *prev_ns;
5982 gfc_namespace **nsp;
5984 old_loc = gfc_current_locus;
5985 if (gfc_match ("end") != MATCH_YES)
5986 return MATCH_NO;
5988 state = gfc_current_state ();
5989 block_name = gfc_current_block () == NULL
5990 ? NULL : gfc_current_block ()->name;
5992 switch (state)
5994 case COMP_ASSOCIATE:
5995 case COMP_BLOCK:
5996 if (!strncmp (block_name, "block@", strlen("block@")))
5997 block_name = NULL;
5998 break;
6000 case COMP_CONTAINS:
6001 case COMP_DERIVED_CONTAINS:
6002 state = gfc_state_stack->previous->state;
6003 block_name = gfc_state_stack->previous->sym == NULL
6004 ? NULL : gfc_state_stack->previous->sym->name;
6005 break;
6007 default:
6008 break;
6011 switch (state)
6013 case COMP_NONE:
6014 case COMP_PROGRAM:
6015 *st = ST_END_PROGRAM;
6016 target = " program";
6017 eos_ok = 1;
6018 break;
6020 case COMP_SUBROUTINE:
6021 *st = ST_END_SUBROUTINE;
6022 target = " subroutine";
6023 eos_ok = !contained_procedure ();
6024 break;
6026 case COMP_FUNCTION:
6027 *st = ST_END_FUNCTION;
6028 target = " function";
6029 eos_ok = !contained_procedure ();
6030 break;
6032 case COMP_BLOCK_DATA:
6033 *st = ST_END_BLOCK_DATA;
6034 target = " block data";
6035 eos_ok = 1;
6036 break;
6038 case COMP_MODULE:
6039 *st = ST_END_MODULE;
6040 target = " module";
6041 eos_ok = 1;
6042 break;
6044 case COMP_INTERFACE:
6045 *st = ST_END_INTERFACE;
6046 target = " interface";
6047 eos_ok = 0;
6048 break;
6050 case COMP_DERIVED:
6051 case COMP_DERIVED_CONTAINS:
6052 *st = ST_END_TYPE;
6053 target = " type";
6054 eos_ok = 0;
6055 break;
6057 case COMP_ASSOCIATE:
6058 *st = ST_END_ASSOCIATE;
6059 target = " associate";
6060 eos_ok = 0;
6061 break;
6063 case COMP_BLOCK:
6064 *st = ST_END_BLOCK;
6065 target = " block";
6066 eos_ok = 0;
6067 break;
6069 case COMP_IF:
6070 *st = ST_ENDIF;
6071 target = " if";
6072 eos_ok = 0;
6073 break;
6075 case COMP_DO:
6076 case COMP_DO_CONCURRENT:
6077 *st = ST_ENDDO;
6078 target = " do";
6079 eos_ok = 0;
6080 break;
6082 case COMP_CRITICAL:
6083 *st = ST_END_CRITICAL;
6084 target = " critical";
6085 eos_ok = 0;
6086 break;
6088 case COMP_SELECT:
6089 case COMP_SELECT_TYPE:
6090 *st = ST_END_SELECT;
6091 target = " select";
6092 eos_ok = 0;
6093 break;
6095 case COMP_FORALL:
6096 *st = ST_END_FORALL;
6097 target = " forall";
6098 eos_ok = 0;
6099 break;
6101 case COMP_WHERE:
6102 *st = ST_END_WHERE;
6103 target = " where";
6104 eos_ok = 0;
6105 break;
6107 case COMP_ENUM:
6108 *st = ST_END_ENUM;
6109 target = " enum";
6110 eos_ok = 0;
6111 last_initializer = NULL;
6112 set_enum_kind ();
6113 gfc_free_enum_history ();
6114 break;
6116 default:
6117 gfc_error ("Unexpected END statement at %C");
6118 goto cleanup;
6121 old_loc = gfc_current_locus;
6122 if (gfc_match_eos () == MATCH_YES)
6124 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6126 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
6127 "instead of %s statement at %L",
6128 gfc_ascii_statement(*st), &old_loc))
6129 goto cleanup;
6131 else if (!eos_ok)
6133 /* We would have required END [something]. */
6134 gfc_error ("%s statement expected at %L",
6135 gfc_ascii_statement (*st), &old_loc);
6136 goto cleanup;
6139 return MATCH_YES;
6142 /* Verify that we've got the sort of end-block that we're expecting. */
6143 if (gfc_match (target) != MATCH_YES)
6145 gfc_error ("Expecting %s statement at %L", gfc_ascii_statement (*st),
6146 &old_loc);
6147 goto cleanup;
6150 old_loc = gfc_current_locus;
6151 /* If we're at the end, make sure a block name wasn't required. */
6152 if (gfc_match_eos () == MATCH_YES)
6155 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6156 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6157 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6158 return MATCH_YES;
6160 if (!block_name)
6161 return MATCH_YES;
6163 gfc_error ("Expected block name of '%s' in %s statement at %L",
6164 block_name, gfc_ascii_statement (*st), &old_loc);
6166 return MATCH_ERROR;
6169 /* END INTERFACE has a special handler for its several possible endings. */
6170 if (*st == ST_END_INTERFACE)
6171 return gfc_match_end_interface ();
6173 /* We haven't hit the end of statement, so what is left must be an
6174 end-name. */
6175 m = gfc_match_space ();
6176 if (m == MATCH_YES)
6177 m = gfc_match_name (name);
6179 if (m == MATCH_NO)
6180 gfc_error ("Expected terminating name at %C");
6181 if (m != MATCH_YES)
6182 goto cleanup;
6184 if (block_name == NULL)
6185 goto syntax;
6187 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6189 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
6190 gfc_ascii_statement (*st));
6191 goto cleanup;
6193 /* Procedure pointer as function result. */
6194 else if (strcmp (block_name, "ppr@") == 0
6195 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6197 gfc_error ("Expected label '%s' for %s statement at %C",
6198 gfc_current_block ()->ns->proc_name->name,
6199 gfc_ascii_statement (*st));
6200 goto cleanup;
6203 if (gfc_match_eos () == MATCH_YES)
6204 return MATCH_YES;
6206 syntax:
6207 gfc_syntax_error (*st);
6209 cleanup:
6210 gfc_current_locus = old_loc;
6212 /* If we are missing an END BLOCK, we created a half-ready namespace.
6213 Remove it from the parent namespace's sibling list. */
6215 if (state == COMP_BLOCK)
6217 parent_ns = gfc_current_ns->parent;
6219 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
6221 prev_ns = NULL;
6222 ns = *nsp;
6223 while (ns)
6225 if (ns == gfc_current_ns)
6227 if (prev_ns == NULL)
6228 *nsp = NULL;
6229 else
6230 prev_ns->sibling = ns->sibling;
6232 prev_ns = ns;
6233 ns = ns->sibling;
6236 gfc_free_namespace (gfc_current_ns);
6237 gfc_current_ns = parent_ns;
6240 return MATCH_ERROR;
6245 /***************** Attribute declaration statements ****************/
6247 /* Set the attribute of a single variable. */
6249 static match
6250 attr_decl1 (void)
6252 char name[GFC_MAX_SYMBOL_LEN + 1];
6253 gfc_array_spec *as;
6254 gfc_symbol *sym;
6255 locus var_locus;
6256 match m;
6258 as = NULL;
6260 m = gfc_match_name (name);
6261 if (m != MATCH_YES)
6262 goto cleanup;
6264 if (find_special (name, &sym, false))
6265 return MATCH_ERROR;
6267 if (!check_function_name (name))
6269 m = MATCH_ERROR;
6270 goto cleanup;
6273 var_locus = gfc_current_locus;
6275 /* Deal with possible array specification for certain attributes. */
6276 if (current_attr.dimension
6277 || current_attr.codimension
6278 || current_attr.allocatable
6279 || current_attr.pointer
6280 || current_attr.target)
6282 m = gfc_match_array_spec (&as, !current_attr.codimension,
6283 !current_attr.dimension
6284 && !current_attr.pointer
6285 && !current_attr.target);
6286 if (m == MATCH_ERROR)
6287 goto cleanup;
6289 if (current_attr.dimension && m == MATCH_NO)
6291 gfc_error ("Missing array specification at %L in DIMENSION "
6292 "statement", &var_locus);
6293 m = MATCH_ERROR;
6294 goto cleanup;
6297 if (current_attr.dimension && sym->value)
6299 gfc_error ("Dimensions specified for %s at %L after its "
6300 "initialisation", sym->name, &var_locus);
6301 m = MATCH_ERROR;
6302 goto cleanup;
6305 if (current_attr.codimension && m == MATCH_NO)
6307 gfc_error ("Missing array specification at %L in CODIMENSION "
6308 "statement", &var_locus);
6309 m = MATCH_ERROR;
6310 goto cleanup;
6313 if ((current_attr.allocatable || current_attr.pointer)
6314 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6316 gfc_error ("Array specification must be deferred at %L", &var_locus);
6317 m = MATCH_ERROR;
6318 goto cleanup;
6322 /* Update symbol table. DIMENSION attribute is set in
6323 gfc_set_array_spec(). For CLASS variables, this must be applied
6324 to the first component, or '_data' field. */
6325 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6327 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
6329 m = MATCH_ERROR;
6330 goto cleanup;
6333 else
6335 if (current_attr.dimension == 0 && current_attr.codimension == 0
6336 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
6338 m = MATCH_ERROR;
6339 goto cleanup;
6343 if (sym->ts.type == BT_CLASS
6344 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false))
6346 m = MATCH_ERROR;
6347 goto cleanup;
6350 if (!gfc_set_array_spec (sym, as, &var_locus))
6352 m = MATCH_ERROR;
6353 goto cleanup;
6356 if (sym->attr.cray_pointee && sym->as != NULL)
6358 /* Fix the array spec. */
6359 m = gfc_mod_pointee_as (sym->as);
6360 if (m == MATCH_ERROR)
6361 goto cleanup;
6364 if (!gfc_add_attribute (&sym->attr, &var_locus))
6366 m = MATCH_ERROR;
6367 goto cleanup;
6370 if ((current_attr.external || current_attr.intrinsic)
6371 && sym->attr.flavor != FL_PROCEDURE
6372 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
6374 m = MATCH_ERROR;
6375 goto cleanup;
6378 add_hidden_procptr_result (sym);
6380 return MATCH_YES;
6382 cleanup:
6383 gfc_free_array_spec (as);
6384 return m;
6388 /* Generic attribute declaration subroutine. Used for attributes that
6389 just have a list of names. */
6391 static match
6392 attr_decl (void)
6394 match m;
6396 /* Gobble the optional double colon, by simply ignoring the result
6397 of gfc_match(). */
6398 gfc_match (" ::");
6400 for (;;)
6402 m = attr_decl1 ();
6403 if (m != MATCH_YES)
6404 break;
6406 if (gfc_match_eos () == MATCH_YES)
6408 m = MATCH_YES;
6409 break;
6412 if (gfc_match_char (',') != MATCH_YES)
6414 gfc_error ("Unexpected character in variable list at %C");
6415 m = MATCH_ERROR;
6416 break;
6420 return m;
6424 /* This routine matches Cray Pointer declarations of the form:
6425 pointer ( <pointer>, <pointee> )
6427 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6428 The pointer, if already declared, should be an integer. Otherwise, we
6429 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6430 be either a scalar, or an array declaration. No space is allocated for
6431 the pointee. For the statement
6432 pointer (ipt, ar(10))
6433 any subsequent uses of ar will be translated (in C-notation) as
6434 ar(i) => ((<type> *) ipt)(i)
6435 After gimplification, pointee variable will disappear in the code. */
6437 static match
6438 cray_pointer_decl (void)
6440 match m;
6441 gfc_array_spec *as = NULL;
6442 gfc_symbol *cptr; /* Pointer symbol. */
6443 gfc_symbol *cpte; /* Pointee symbol. */
6444 locus var_locus;
6445 bool done = false;
6447 while (!done)
6449 if (gfc_match_char ('(') != MATCH_YES)
6451 gfc_error ("Expected '(' at %C");
6452 return MATCH_ERROR;
6455 /* Match pointer. */
6456 var_locus = gfc_current_locus;
6457 gfc_clear_attr (&current_attr);
6458 gfc_add_cray_pointer (&current_attr, &var_locus);
6459 current_ts.type = BT_INTEGER;
6460 current_ts.kind = gfc_index_integer_kind;
6462 m = gfc_match_symbol (&cptr, 0);
6463 if (m != MATCH_YES)
6465 gfc_error ("Expected variable name at %C");
6466 return m;
6469 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
6470 return MATCH_ERROR;
6472 gfc_set_sym_referenced (cptr);
6474 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6476 cptr->ts.type = BT_INTEGER;
6477 cptr->ts.kind = gfc_index_integer_kind;
6479 else if (cptr->ts.type != BT_INTEGER)
6481 gfc_error ("Cray pointer at %C must be an integer");
6482 return MATCH_ERROR;
6484 else if (cptr->ts.kind < gfc_index_integer_kind)
6485 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6486 " memory addresses require %d bytes",
6487 cptr->ts.kind, gfc_index_integer_kind);
6489 if (gfc_match_char (',') != MATCH_YES)
6491 gfc_error ("Expected \",\" at %C");
6492 return MATCH_ERROR;
6495 /* Match Pointee. */
6496 var_locus = gfc_current_locus;
6497 gfc_clear_attr (&current_attr);
6498 gfc_add_cray_pointee (&current_attr, &var_locus);
6499 current_ts.type = BT_UNKNOWN;
6500 current_ts.kind = 0;
6502 m = gfc_match_symbol (&cpte, 0);
6503 if (m != MATCH_YES)
6505 gfc_error ("Expected variable name at %C");
6506 return m;
6509 /* Check for an optional array spec. */
6510 m = gfc_match_array_spec (&as, true, false);
6511 if (m == MATCH_ERROR)
6513 gfc_free_array_spec (as);
6514 return m;
6516 else if (m == MATCH_NO)
6518 gfc_free_array_spec (as);
6519 as = NULL;
6522 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
6523 return MATCH_ERROR;
6525 gfc_set_sym_referenced (cpte);
6527 if (cpte->as == NULL)
6529 if (!gfc_set_array_spec (cpte, as, &var_locus))
6530 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6532 else if (as != NULL)
6534 gfc_error ("Duplicate array spec for Cray pointee at %C");
6535 gfc_free_array_spec (as);
6536 return MATCH_ERROR;
6539 as = NULL;
6541 if (cpte->as != NULL)
6543 /* Fix array spec. */
6544 m = gfc_mod_pointee_as (cpte->as);
6545 if (m == MATCH_ERROR)
6546 return m;
6549 /* Point the Pointee at the Pointer. */
6550 cpte->cp_pointer = cptr;
6552 if (gfc_match_char (')') != MATCH_YES)
6554 gfc_error ("Expected \")\" at %C");
6555 return MATCH_ERROR;
6557 m = gfc_match_char (',');
6558 if (m != MATCH_YES)
6559 done = true; /* Stop searching for more declarations. */
6563 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6564 || gfc_match_eos () != MATCH_YES)
6566 gfc_error ("Expected \",\" or end of statement at %C");
6567 return MATCH_ERROR;
6569 return MATCH_YES;
6573 match
6574 gfc_match_external (void)
6577 gfc_clear_attr (&current_attr);
6578 current_attr.external = 1;
6580 return attr_decl ();
6584 match
6585 gfc_match_intent (void)
6587 sym_intent intent;
6589 /* This is not allowed within a BLOCK construct! */
6590 if (gfc_current_state () == COMP_BLOCK)
6592 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6593 return MATCH_ERROR;
6596 intent = match_intent_spec ();
6597 if (intent == INTENT_UNKNOWN)
6598 return MATCH_ERROR;
6600 gfc_clear_attr (&current_attr);
6601 current_attr.intent = intent;
6603 return attr_decl ();
6607 match
6608 gfc_match_intrinsic (void)
6611 gfc_clear_attr (&current_attr);
6612 current_attr.intrinsic = 1;
6614 return attr_decl ();
6618 match
6619 gfc_match_optional (void)
6621 /* This is not allowed within a BLOCK construct! */
6622 if (gfc_current_state () == COMP_BLOCK)
6624 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6625 return MATCH_ERROR;
6628 gfc_clear_attr (&current_attr);
6629 current_attr.optional = 1;
6631 return attr_decl ();
6635 match
6636 gfc_match_pointer (void)
6638 gfc_gobble_whitespace ();
6639 if (gfc_peek_ascii_char () == '(')
6641 if (!gfc_option.flag_cray_pointer)
6643 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6644 "flag");
6645 return MATCH_ERROR;
6647 return cray_pointer_decl ();
6649 else
6651 gfc_clear_attr (&current_attr);
6652 current_attr.pointer = 1;
6654 return attr_decl ();
6659 match
6660 gfc_match_allocatable (void)
6662 gfc_clear_attr (&current_attr);
6663 current_attr.allocatable = 1;
6665 return attr_decl ();
6669 match
6670 gfc_match_codimension (void)
6672 gfc_clear_attr (&current_attr);
6673 current_attr.codimension = 1;
6675 return attr_decl ();
6679 match
6680 gfc_match_contiguous (void)
6682 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
6683 return MATCH_ERROR;
6685 gfc_clear_attr (&current_attr);
6686 current_attr.contiguous = 1;
6688 return attr_decl ();
6692 match
6693 gfc_match_dimension (void)
6695 gfc_clear_attr (&current_attr);
6696 current_attr.dimension = 1;
6698 return attr_decl ();
6702 match
6703 gfc_match_target (void)
6705 gfc_clear_attr (&current_attr);
6706 current_attr.target = 1;
6708 return attr_decl ();
6712 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6713 statement. */
6715 static match
6716 access_attr_decl (gfc_statement st)
6718 char name[GFC_MAX_SYMBOL_LEN + 1];
6719 interface_type type;
6720 gfc_user_op *uop;
6721 gfc_symbol *sym, *dt_sym;
6722 gfc_intrinsic_op op;
6723 match m;
6725 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6726 goto done;
6728 for (;;)
6730 m = gfc_match_generic_spec (&type, name, &op);
6731 if (m == MATCH_NO)
6732 goto syntax;
6733 if (m == MATCH_ERROR)
6734 return MATCH_ERROR;
6736 switch (type)
6738 case INTERFACE_NAMELESS:
6739 case INTERFACE_ABSTRACT:
6740 goto syntax;
6742 case INTERFACE_GENERIC:
6743 if (gfc_get_symbol (name, NULL, &sym))
6744 goto done;
6746 if (!gfc_add_access (&sym->attr,
6747 (st == ST_PUBLIC)
6748 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6749 sym->name, NULL))
6750 return MATCH_ERROR;
6752 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
6753 && !gfc_add_access (&dt_sym->attr,
6754 (st == ST_PUBLIC)
6755 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6756 sym->name, NULL))
6757 return MATCH_ERROR;
6759 break;
6761 case INTERFACE_INTRINSIC_OP:
6762 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6764 gfc_intrinsic_op other_op;
6766 gfc_current_ns->operator_access[op] =
6767 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6769 /* Handle the case if there is another op with the same
6770 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6771 other_op = gfc_equivalent_op (op);
6773 if (other_op != INTRINSIC_NONE)
6774 gfc_current_ns->operator_access[other_op] =
6775 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6778 else
6780 gfc_error ("Access specification of the %s operator at %C has "
6781 "already been specified", gfc_op2string (op));
6782 goto done;
6785 break;
6787 case INTERFACE_USER_OP:
6788 uop = gfc_get_uop (name);
6790 if (uop->access == ACCESS_UNKNOWN)
6792 uop->access = (st == ST_PUBLIC)
6793 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6795 else
6797 gfc_error ("Access specification of the .%s. operator at %C "
6798 "has already been specified", sym->name);
6799 goto done;
6802 break;
6805 if (gfc_match_char (',') == MATCH_NO)
6806 break;
6809 if (gfc_match_eos () != MATCH_YES)
6810 goto syntax;
6811 return MATCH_YES;
6813 syntax:
6814 gfc_syntax_error (st);
6816 done:
6817 return MATCH_ERROR;
6821 match
6822 gfc_match_protected (void)
6824 gfc_symbol *sym;
6825 match m;
6827 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6829 gfc_error ("PROTECTED at %C only allowed in specification "
6830 "part of a module");
6831 return MATCH_ERROR;
6835 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
6836 return MATCH_ERROR;
6838 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6840 return MATCH_ERROR;
6843 if (gfc_match_eos () == MATCH_YES)
6844 goto syntax;
6846 for(;;)
6848 m = gfc_match_symbol (&sym, 0);
6849 switch (m)
6851 case MATCH_YES:
6852 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
6853 return MATCH_ERROR;
6854 goto next_item;
6856 case MATCH_NO:
6857 break;
6859 case MATCH_ERROR:
6860 return MATCH_ERROR;
6863 next_item:
6864 if (gfc_match_eos () == MATCH_YES)
6865 break;
6866 if (gfc_match_char (',') != MATCH_YES)
6867 goto syntax;
6870 return MATCH_YES;
6872 syntax:
6873 gfc_error ("Syntax error in PROTECTED statement at %C");
6874 return MATCH_ERROR;
6878 /* The PRIVATE statement is a bit weird in that it can be an attribute
6879 declaration, but also works as a standalone statement inside of a
6880 type declaration or a module. */
6882 match
6883 gfc_match_private (gfc_statement *st)
6886 if (gfc_match ("private") != MATCH_YES)
6887 return MATCH_NO;
6889 if (gfc_current_state () != COMP_MODULE
6890 && !(gfc_current_state () == COMP_DERIVED
6891 && gfc_state_stack->previous
6892 && gfc_state_stack->previous->state == COMP_MODULE)
6893 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6894 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6895 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6897 gfc_error ("PRIVATE statement at %C is only allowed in the "
6898 "specification part of a module");
6899 return MATCH_ERROR;
6902 if (gfc_current_state () == COMP_DERIVED)
6904 if (gfc_match_eos () == MATCH_YES)
6906 *st = ST_PRIVATE;
6907 return MATCH_YES;
6910 gfc_syntax_error (ST_PRIVATE);
6911 return MATCH_ERROR;
6914 if (gfc_match_eos () == MATCH_YES)
6916 *st = ST_PRIVATE;
6917 return MATCH_YES;
6920 *st = ST_ATTR_DECL;
6921 return access_attr_decl (ST_PRIVATE);
6925 match
6926 gfc_match_public (gfc_statement *st)
6929 if (gfc_match ("public") != MATCH_YES)
6930 return MATCH_NO;
6932 if (gfc_current_state () != COMP_MODULE)
6934 gfc_error ("PUBLIC statement at %C is only allowed in the "
6935 "specification part of a module");
6936 return MATCH_ERROR;
6939 if (gfc_match_eos () == MATCH_YES)
6941 *st = ST_PUBLIC;
6942 return MATCH_YES;
6945 *st = ST_ATTR_DECL;
6946 return access_attr_decl (ST_PUBLIC);
6950 /* Workhorse for gfc_match_parameter. */
6952 static match
6953 do_parm (void)
6955 gfc_symbol *sym;
6956 gfc_expr *init;
6957 match m;
6958 bool t;
6960 m = gfc_match_symbol (&sym, 0);
6961 if (m == MATCH_NO)
6962 gfc_error ("Expected variable name at %C in PARAMETER statement");
6964 if (m != MATCH_YES)
6965 return m;
6967 if (gfc_match_char ('=') == MATCH_NO)
6969 gfc_error ("Expected = sign in PARAMETER statement at %C");
6970 return MATCH_ERROR;
6973 m = gfc_match_init_expr (&init);
6974 if (m == MATCH_NO)
6975 gfc_error ("Expected expression at %C in PARAMETER statement");
6976 if (m != MATCH_YES)
6977 return m;
6979 if (sym->ts.type == BT_UNKNOWN
6980 && !gfc_set_default_type (sym, 1, NULL))
6982 m = MATCH_ERROR;
6983 goto cleanup;
6986 if (!gfc_check_assign_symbol (sym, NULL, init)
6987 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
6989 m = MATCH_ERROR;
6990 goto cleanup;
6993 if (sym->value)
6995 gfc_error ("Initializing already initialized variable at %C");
6996 m = MATCH_ERROR;
6997 goto cleanup;
7000 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
7001 return (t) ? MATCH_YES : MATCH_ERROR;
7003 cleanup:
7004 gfc_free_expr (init);
7005 return m;
7009 /* Match a parameter statement, with the weird syntax that these have. */
7011 match
7012 gfc_match_parameter (void)
7014 match m;
7016 if (gfc_match_char ('(') == MATCH_NO)
7017 return MATCH_NO;
7019 for (;;)
7021 m = do_parm ();
7022 if (m != MATCH_YES)
7023 break;
7025 if (gfc_match (" )%t") == MATCH_YES)
7026 break;
7028 if (gfc_match_char (',') != MATCH_YES)
7030 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7031 m = MATCH_ERROR;
7032 break;
7036 return m;
7040 /* Save statements have a special syntax. */
7042 match
7043 gfc_match_save (void)
7045 char n[GFC_MAX_SYMBOL_LEN+1];
7046 gfc_common_head *c;
7047 gfc_symbol *sym;
7048 match m;
7050 if (gfc_match_eos () == MATCH_YES)
7052 if (gfc_current_ns->seen_save)
7054 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
7055 "follows previous SAVE statement"))
7056 return MATCH_ERROR;
7059 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
7060 return MATCH_YES;
7063 if (gfc_current_ns->save_all)
7065 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
7066 "blanket SAVE statement"))
7067 return MATCH_ERROR;
7070 gfc_match (" ::");
7072 for (;;)
7074 m = gfc_match_symbol (&sym, 0);
7075 switch (m)
7077 case MATCH_YES:
7078 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
7079 &gfc_current_locus))
7080 return MATCH_ERROR;
7081 goto next_item;
7083 case MATCH_NO:
7084 break;
7086 case MATCH_ERROR:
7087 return MATCH_ERROR;
7090 m = gfc_match (" / %n /", &n);
7091 if (m == MATCH_ERROR)
7092 return MATCH_ERROR;
7093 if (m == MATCH_NO)
7094 goto syntax;
7096 c = gfc_get_common (n, 0);
7097 c->saved = 1;
7099 gfc_current_ns->seen_save = 1;
7101 next_item:
7102 if (gfc_match_eos () == MATCH_YES)
7103 break;
7104 if (gfc_match_char (',') != MATCH_YES)
7105 goto syntax;
7108 return MATCH_YES;
7110 syntax:
7111 gfc_error ("Syntax error in SAVE statement at %C");
7112 return MATCH_ERROR;
7116 match
7117 gfc_match_value (void)
7119 gfc_symbol *sym;
7120 match m;
7122 /* This is not allowed within a BLOCK construct! */
7123 if (gfc_current_state () == COMP_BLOCK)
7125 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7126 return MATCH_ERROR;
7129 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
7130 return MATCH_ERROR;
7132 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7134 return MATCH_ERROR;
7137 if (gfc_match_eos () == MATCH_YES)
7138 goto syntax;
7140 for(;;)
7142 m = gfc_match_symbol (&sym, 0);
7143 switch (m)
7145 case MATCH_YES:
7146 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
7147 return MATCH_ERROR;
7148 goto next_item;
7150 case MATCH_NO:
7151 break;
7153 case MATCH_ERROR:
7154 return MATCH_ERROR;
7157 next_item:
7158 if (gfc_match_eos () == MATCH_YES)
7159 break;
7160 if (gfc_match_char (',') != MATCH_YES)
7161 goto syntax;
7164 return MATCH_YES;
7166 syntax:
7167 gfc_error ("Syntax error in VALUE statement at %C");
7168 return MATCH_ERROR;
7172 match
7173 gfc_match_volatile (void)
7175 gfc_symbol *sym;
7176 match m;
7178 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
7179 return MATCH_ERROR;
7181 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7183 return MATCH_ERROR;
7186 if (gfc_match_eos () == MATCH_YES)
7187 goto syntax;
7189 for(;;)
7191 /* VOLATILE is special because it can be added to host-associated
7192 symbols locally. Except for coarrays. */
7193 m = gfc_match_symbol (&sym, 1);
7194 switch (m)
7196 case MATCH_YES:
7197 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7198 for variable in a BLOCK which is defined outside of the BLOCK. */
7199 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7201 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
7202 "%C, which is use-/host-associated", sym->name);
7203 return MATCH_ERROR;
7205 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
7206 return MATCH_ERROR;
7207 goto next_item;
7209 case MATCH_NO:
7210 break;
7212 case MATCH_ERROR:
7213 return MATCH_ERROR;
7216 next_item:
7217 if (gfc_match_eos () == MATCH_YES)
7218 break;
7219 if (gfc_match_char (',') != MATCH_YES)
7220 goto syntax;
7223 return MATCH_YES;
7225 syntax:
7226 gfc_error ("Syntax error in VOLATILE statement at %C");
7227 return MATCH_ERROR;
7231 match
7232 gfc_match_asynchronous (void)
7234 gfc_symbol *sym;
7235 match m;
7237 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
7238 return MATCH_ERROR;
7240 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7242 return MATCH_ERROR;
7245 if (gfc_match_eos () == MATCH_YES)
7246 goto syntax;
7248 for(;;)
7250 /* ASYNCHRONOUS is special because it can be added to host-associated
7251 symbols locally. */
7252 m = gfc_match_symbol (&sym, 1);
7253 switch (m)
7255 case MATCH_YES:
7256 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
7257 return MATCH_ERROR;
7258 goto next_item;
7260 case MATCH_NO:
7261 break;
7263 case MATCH_ERROR:
7264 return MATCH_ERROR;
7267 next_item:
7268 if (gfc_match_eos () == MATCH_YES)
7269 break;
7270 if (gfc_match_char (',') != MATCH_YES)
7271 goto syntax;
7274 return MATCH_YES;
7276 syntax:
7277 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7278 return MATCH_ERROR;
7282 /* Match a module procedure statement. Note that we have to modify
7283 symbols in the parent's namespace because the current one was there
7284 to receive symbols that are in an interface's formal argument list. */
7286 match
7287 gfc_match_modproc (void)
7289 char name[GFC_MAX_SYMBOL_LEN + 1];
7290 gfc_symbol *sym;
7291 match m;
7292 locus old_locus;
7293 gfc_namespace *module_ns;
7294 gfc_interface *old_interface_head, *interface;
7296 if (gfc_state_stack->state != COMP_INTERFACE
7297 || gfc_state_stack->previous == NULL
7298 || current_interface.type == INTERFACE_NAMELESS
7299 || current_interface.type == INTERFACE_ABSTRACT)
7301 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7302 "interface");
7303 return MATCH_ERROR;
7306 module_ns = gfc_current_ns->parent;
7307 for (; module_ns; module_ns = module_ns->parent)
7308 if (module_ns->proc_name->attr.flavor == FL_MODULE
7309 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7310 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7311 && !module_ns->proc_name->attr.contained))
7312 break;
7314 if (module_ns == NULL)
7315 return MATCH_ERROR;
7317 /* Store the current state of the interface. We will need it if we
7318 end up with a syntax error and need to recover. */
7319 old_interface_head = gfc_current_interface_head ();
7321 /* Check if the F2008 optional double colon appears. */
7322 gfc_gobble_whitespace ();
7323 old_locus = gfc_current_locus;
7324 if (gfc_match ("::") == MATCH_YES)
7326 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7327 "MODULE PROCEDURE statement at %L", &old_locus))
7328 return MATCH_ERROR;
7330 else
7331 gfc_current_locus = old_locus;
7333 for (;;)
7335 bool last = false;
7336 old_locus = gfc_current_locus;
7338 m = gfc_match_name (name);
7339 if (m == MATCH_NO)
7340 goto syntax;
7341 if (m != MATCH_YES)
7342 return MATCH_ERROR;
7344 /* Check for syntax error before starting to add symbols to the
7345 current namespace. */
7346 if (gfc_match_eos () == MATCH_YES)
7347 last = true;
7349 if (!last && gfc_match_char (',') != MATCH_YES)
7350 goto syntax;
7352 /* Now we're sure the syntax is valid, we process this item
7353 further. */
7354 if (gfc_get_symbol (name, module_ns, &sym))
7355 return MATCH_ERROR;
7357 if (sym->attr.intrinsic)
7359 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7360 "PROCEDURE", &old_locus);
7361 return MATCH_ERROR;
7364 if (sym->attr.proc != PROC_MODULE
7365 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
7366 return MATCH_ERROR;
7368 if (!gfc_add_interface (sym))
7369 return MATCH_ERROR;
7371 sym->attr.mod_proc = 1;
7372 sym->declared_at = old_locus;
7374 if (last)
7375 break;
7378 return MATCH_YES;
7380 syntax:
7381 /* Restore the previous state of the interface. */
7382 interface = gfc_current_interface_head ();
7383 gfc_set_current_interface_head (old_interface_head);
7385 /* Free the new interfaces. */
7386 while (interface != old_interface_head)
7388 gfc_interface *i = interface->next;
7389 free (interface);
7390 interface = i;
7393 /* And issue a syntax error. */
7394 gfc_syntax_error (ST_MODULE_PROC);
7395 return MATCH_ERROR;
7399 /* Check a derived type that is being extended. */
7401 static gfc_symbol*
7402 check_extended_derived_type (char *name)
7404 gfc_symbol *extended;
7406 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7408 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7409 return NULL;
7412 extended = gfc_find_dt_in_generic (extended);
7414 /* F08:C428. */
7415 if (!extended)
7417 gfc_error ("Symbol '%s' at %C has not been previously defined", name);
7418 return NULL;
7421 if (extended->attr.flavor != FL_DERIVED)
7423 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7424 "derived type", name);
7425 return NULL;
7428 if (extended->attr.is_bind_c)
7430 gfc_error ("'%s' cannot be extended at %C because it "
7431 "is BIND(C)", extended->name);
7432 return NULL;
7435 if (extended->attr.sequence)
7437 gfc_error ("'%s' cannot be extended at %C because it "
7438 "is a SEQUENCE type", extended->name);
7439 return NULL;
7442 return extended;
7446 /* Match the optional attribute specifiers for a type declaration.
7447 Return MATCH_ERROR if an error is encountered in one of the handled
7448 attributes (public, private, bind(c)), MATCH_NO if what's found is
7449 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7450 checking on attribute conflicts needs to be done. */
7452 match
7453 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7455 /* See if the derived type is marked as private. */
7456 if (gfc_match (" , private") == MATCH_YES)
7458 if (gfc_current_state () != COMP_MODULE)
7460 gfc_error ("Derived type at %C can only be PRIVATE in the "
7461 "specification part of a module");
7462 return MATCH_ERROR;
7465 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
7466 return MATCH_ERROR;
7468 else if (gfc_match (" , public") == MATCH_YES)
7470 if (gfc_current_state () != COMP_MODULE)
7472 gfc_error ("Derived type at %C can only be PUBLIC in the "
7473 "specification part of a module");
7474 return MATCH_ERROR;
7477 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
7478 return MATCH_ERROR;
7480 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7482 /* If the type is defined to be bind(c) it then needs to make
7483 sure that all fields are interoperable. This will
7484 need to be a semantic check on the finished derived type.
7485 See 15.2.3 (lines 9-12) of F2003 draft. */
7486 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
7487 return MATCH_ERROR;
7489 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7491 else if (gfc_match (" , abstract") == MATCH_YES)
7493 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
7494 return MATCH_ERROR;
7496 if (!gfc_add_abstract (attr, &gfc_current_locus))
7497 return MATCH_ERROR;
7499 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
7501 if (!gfc_add_extension (attr, &gfc_current_locus))
7502 return MATCH_ERROR;
7504 else
7505 return MATCH_NO;
7507 /* If we get here, something matched. */
7508 return MATCH_YES;
7512 /* Match the beginning of a derived type declaration. If a type name
7513 was the result of a function, then it is possible to have a symbol
7514 already to be known as a derived type yet have no components. */
7516 match
7517 gfc_match_derived_decl (void)
7519 char name[GFC_MAX_SYMBOL_LEN + 1];
7520 char parent[GFC_MAX_SYMBOL_LEN + 1];
7521 symbol_attribute attr;
7522 gfc_symbol *sym, *gensym;
7523 gfc_symbol *extended;
7524 match m;
7525 match is_type_attr_spec = MATCH_NO;
7526 bool seen_attr = false;
7527 gfc_interface *intr = NULL, *head;
7529 if (gfc_current_state () == COMP_DERIVED)
7530 return MATCH_NO;
7532 name[0] = '\0';
7533 parent[0] = '\0';
7534 gfc_clear_attr (&attr);
7535 extended = NULL;
7539 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7540 if (is_type_attr_spec == MATCH_ERROR)
7541 return MATCH_ERROR;
7542 if (is_type_attr_spec == MATCH_YES)
7543 seen_attr = true;
7544 } while (is_type_attr_spec == MATCH_YES);
7546 /* Deal with derived type extensions. The extension attribute has
7547 been added to 'attr' but now the parent type must be found and
7548 checked. */
7549 if (parent[0])
7550 extended = check_extended_derived_type (parent);
7552 if (parent[0] && !extended)
7553 return MATCH_ERROR;
7555 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7557 gfc_error ("Expected :: in TYPE definition at %C");
7558 return MATCH_ERROR;
7561 m = gfc_match (" %n%t", name);
7562 if (m != MATCH_YES)
7563 return m;
7565 /* Make sure the name is not the name of an intrinsic type. */
7566 if (gfc_is_intrinsic_typename (name))
7568 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7569 "type", name);
7570 return MATCH_ERROR;
7573 if (gfc_get_symbol (name, NULL, &gensym))
7574 return MATCH_ERROR;
7576 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
7578 gfc_error ("Derived type name '%s' at %C already has a basic type "
7579 "of %s", gensym->name, gfc_typename (&gensym->ts));
7580 return MATCH_ERROR;
7583 if (!gensym->attr.generic
7584 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
7585 return MATCH_ERROR;
7587 if (!gensym->attr.function
7588 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
7589 return MATCH_ERROR;
7591 sym = gfc_find_dt_in_generic (gensym);
7593 if (sym && (sym->components != NULL || sym->attr.zero_comp))
7595 gfc_error ("Derived type definition of '%s' at %C has already been "
7596 "defined", sym->name);
7597 return MATCH_ERROR;
7600 if (!sym)
7602 /* Use upper case to save the actual derived-type symbol. */
7603 gfc_get_symbol (gfc_get_string ("%c%s",
7604 (char) TOUPPER ((unsigned char) gensym->name[0]),
7605 &gensym->name[1]), NULL, &sym);
7606 sym->name = gfc_get_string (gensym->name);
7607 head = gensym->generic;
7608 intr = gfc_get_interface ();
7609 intr->sym = sym;
7610 intr->where = gfc_current_locus;
7611 intr->sym->declared_at = gfc_current_locus;
7612 intr->next = head;
7613 gensym->generic = intr;
7614 gensym->attr.if_source = IFSRC_DECL;
7617 /* The symbol may already have the derived attribute without the
7618 components. The ways this can happen is via a function
7619 definition, an INTRINSIC statement or a subtype in another
7620 derived type that is a pointer. The first part of the AND clause
7621 is true if the symbol is not the return value of a function. */
7622 if (sym->attr.flavor != FL_DERIVED
7623 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
7624 return MATCH_ERROR;
7626 if (attr.access != ACCESS_UNKNOWN
7627 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
7628 return MATCH_ERROR;
7629 else if (sym->attr.access == ACCESS_UNKNOWN
7630 && gensym->attr.access != ACCESS_UNKNOWN
7631 && !gfc_add_access (&sym->attr, gensym->attr.access,
7632 sym->name, NULL))
7633 return MATCH_ERROR;
7635 if (sym->attr.access != ACCESS_UNKNOWN
7636 && gensym->attr.access == ACCESS_UNKNOWN)
7637 gensym->attr.access = sym->attr.access;
7639 /* See if the derived type was labeled as bind(c). */
7640 if (attr.is_bind_c != 0)
7641 sym->attr.is_bind_c = attr.is_bind_c;
7643 /* Construct the f2k_derived namespace if it is not yet there. */
7644 if (!sym->f2k_derived)
7645 sym->f2k_derived = gfc_get_namespace (NULL, 0);
7647 if (extended && !sym->components)
7649 gfc_component *p;
7650 gfc_symtree *st;
7652 /* Add the extended derived type as the first component. */
7653 gfc_add_component (sym, parent, &p);
7654 extended->refs++;
7655 gfc_set_sym_referenced (extended);
7657 p->ts.type = BT_DERIVED;
7658 p->ts.u.derived = extended;
7659 p->initializer = gfc_default_initializer (&p->ts);
7661 /* Set extension level. */
7662 if (extended->attr.extension == 255)
7664 /* Since the extension field is 8 bit wide, we can only have
7665 up to 255 extension levels. */
7666 gfc_error ("Maximum extension level reached with type '%s' at %L",
7667 extended->name, &extended->declared_at);
7668 return MATCH_ERROR;
7670 sym->attr.extension = extended->attr.extension + 1;
7672 /* Provide the links between the extended type and its extension. */
7673 if (!extended->f2k_derived)
7674 extended->f2k_derived = gfc_get_namespace (NULL, 0);
7675 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7676 st->n.sym = sym;
7679 if (!sym->hash_value)
7680 /* Set the hash for the compound name for this type. */
7681 sym->hash_value = gfc_hash_value (sym);
7683 /* Take over the ABSTRACT attribute. */
7684 sym->attr.abstract = attr.abstract;
7686 gfc_new_block = sym;
7688 return MATCH_YES;
7692 /* Cray Pointees can be declared as:
7693 pointer (ipt, a (n,m,...,*)) */
7695 match
7696 gfc_mod_pointee_as (gfc_array_spec *as)
7698 as->cray_pointee = true; /* This will be useful to know later. */
7699 if (as->type == AS_ASSUMED_SIZE)
7700 as->cp_was_assumed = true;
7701 else if (as->type == AS_ASSUMED_SHAPE)
7703 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7704 return MATCH_ERROR;
7706 return MATCH_YES;
7710 /* Match the enum definition statement, here we are trying to match
7711 the first line of enum definition statement.
7712 Returns MATCH_YES if match is found. */
7714 match
7715 gfc_match_enum (void)
7717 match m;
7719 m = gfc_match_eos ();
7720 if (m != MATCH_YES)
7721 return m;
7723 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
7724 return MATCH_ERROR;
7726 return MATCH_YES;
7730 /* Returns an initializer whose value is one higher than the value of the
7731 LAST_INITIALIZER argument. If the argument is NULL, the
7732 initializers value will be set to zero. The initializer's kind
7733 will be set to gfc_c_int_kind.
7735 If -fshort-enums is given, the appropriate kind will be selected
7736 later after all enumerators have been parsed. A warning is issued
7737 here if an initializer exceeds gfc_c_int_kind. */
7739 static gfc_expr *
7740 enum_initializer (gfc_expr *last_initializer, locus where)
7742 gfc_expr *result;
7743 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7745 mpz_init (result->value.integer);
7747 if (last_initializer != NULL)
7749 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7750 result->where = last_initializer->where;
7752 if (gfc_check_integer_range (result->value.integer,
7753 gfc_c_int_kind) != ARITH_OK)
7755 gfc_error ("Enumerator exceeds the C integer type at %C");
7756 return NULL;
7759 else
7761 /* Control comes here, if it's the very first enumerator and no
7762 initializer has been given. It will be initialized to zero. */
7763 mpz_set_si (result->value.integer, 0);
7766 return result;
7770 /* Match a variable name with an optional initializer. When this
7771 subroutine is called, a variable is expected to be parsed next.
7772 Depending on what is happening at the moment, updates either the
7773 symbol table or the current interface. */
7775 static match
7776 enumerator_decl (void)
7778 char name[GFC_MAX_SYMBOL_LEN + 1];
7779 gfc_expr *initializer;
7780 gfc_array_spec *as = NULL;
7781 gfc_symbol *sym;
7782 locus var_locus;
7783 match m;
7784 bool t;
7785 locus old_locus;
7787 initializer = NULL;
7788 old_locus = gfc_current_locus;
7790 /* When we get here, we've just matched a list of attributes and
7791 maybe a type and a double colon. The next thing we expect to see
7792 is the name of the symbol. */
7793 m = gfc_match_name (name);
7794 if (m != MATCH_YES)
7795 goto cleanup;
7797 var_locus = gfc_current_locus;
7799 /* OK, we've successfully matched the declaration. Now put the
7800 symbol in the current namespace. If we fail to create the symbol,
7801 bail out. */
7802 if (!build_sym (name, NULL, false, &as, &var_locus))
7804 m = MATCH_ERROR;
7805 goto cleanup;
7808 /* The double colon must be present in order to have initializers.
7809 Otherwise the statement is ambiguous with an assignment statement. */
7810 if (colon_seen)
7812 if (gfc_match_char ('=') == MATCH_YES)
7814 m = gfc_match_init_expr (&initializer);
7815 if (m == MATCH_NO)
7817 gfc_error ("Expected an initialization expression at %C");
7818 m = MATCH_ERROR;
7821 if (m != MATCH_YES)
7822 goto cleanup;
7826 /* If we do not have an initializer, the initialization value of the
7827 previous enumerator (stored in last_initializer) is incremented
7828 by 1 and is used to initialize the current enumerator. */
7829 if (initializer == NULL)
7830 initializer = enum_initializer (last_initializer, old_locus);
7832 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7834 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7835 &var_locus);
7836 m = MATCH_ERROR;
7837 goto cleanup;
7840 /* Store this current initializer, for the next enumerator variable
7841 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7842 use last_initializer below. */
7843 last_initializer = initializer;
7844 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7846 /* Maintain enumerator history. */
7847 gfc_find_symbol (name, NULL, 0, &sym);
7848 create_enum_history (sym, last_initializer);
7850 return (t) ? MATCH_YES : MATCH_ERROR;
7852 cleanup:
7853 /* Free stuff up and return. */
7854 gfc_free_expr (initializer);
7856 return m;
7860 /* Match the enumerator definition statement. */
7862 match
7863 gfc_match_enumerator_def (void)
7865 match m;
7866 bool t;
7868 gfc_clear_ts (&current_ts);
7870 m = gfc_match (" enumerator");
7871 if (m != MATCH_YES)
7872 return m;
7874 m = gfc_match (" :: ");
7875 if (m == MATCH_ERROR)
7876 return m;
7878 colon_seen = (m == MATCH_YES);
7880 if (gfc_current_state () != COMP_ENUM)
7882 gfc_error ("ENUM definition statement expected before %C");
7883 gfc_free_enum_history ();
7884 return MATCH_ERROR;
7887 (&current_ts)->type = BT_INTEGER;
7888 (&current_ts)->kind = gfc_c_int_kind;
7890 gfc_clear_attr (&current_attr);
7891 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
7892 if (!t)
7894 m = MATCH_ERROR;
7895 goto cleanup;
7898 for (;;)
7900 m = enumerator_decl ();
7901 if (m == MATCH_ERROR)
7903 gfc_free_enum_history ();
7904 goto cleanup;
7906 if (m == MATCH_NO)
7907 break;
7909 if (gfc_match_eos () == MATCH_YES)
7910 goto cleanup;
7911 if (gfc_match_char (',') != MATCH_YES)
7912 break;
7915 if (gfc_current_state () == COMP_ENUM)
7917 gfc_free_enum_history ();
7918 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7919 m = MATCH_ERROR;
7922 cleanup:
7923 gfc_free_array_spec (current_as);
7924 current_as = NULL;
7925 return m;
7930 /* Match binding attributes. */
7932 static match
7933 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7935 bool found_passing = false;
7936 bool seen_ptr = false;
7937 match m = MATCH_YES;
7939 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
7940 this case the defaults are in there. */
7941 ba->access = ACCESS_UNKNOWN;
7942 ba->pass_arg = NULL;
7943 ba->pass_arg_num = 0;
7944 ba->nopass = 0;
7945 ba->non_overridable = 0;
7946 ba->deferred = 0;
7947 ba->ppc = ppc;
7949 /* If we find a comma, we believe there are binding attributes. */
7950 m = gfc_match_char (',');
7951 if (m == MATCH_NO)
7952 goto done;
7956 /* Access specifier. */
7958 m = gfc_match (" public");
7959 if (m == MATCH_ERROR)
7960 goto error;
7961 if (m == MATCH_YES)
7963 if (ba->access != ACCESS_UNKNOWN)
7965 gfc_error ("Duplicate access-specifier at %C");
7966 goto error;
7969 ba->access = ACCESS_PUBLIC;
7970 continue;
7973 m = gfc_match (" private");
7974 if (m == MATCH_ERROR)
7975 goto error;
7976 if (m == MATCH_YES)
7978 if (ba->access != ACCESS_UNKNOWN)
7980 gfc_error ("Duplicate access-specifier at %C");
7981 goto error;
7984 ba->access = ACCESS_PRIVATE;
7985 continue;
7988 /* If inside GENERIC, the following is not allowed. */
7989 if (!generic)
7992 /* NOPASS flag. */
7993 m = gfc_match (" nopass");
7994 if (m == MATCH_ERROR)
7995 goto error;
7996 if (m == MATCH_YES)
7998 if (found_passing)
8000 gfc_error ("Binding attributes already specify passing,"
8001 " illegal NOPASS at %C");
8002 goto error;
8005 found_passing = true;
8006 ba->nopass = 1;
8007 continue;
8010 /* PASS possibly including argument. */
8011 m = gfc_match (" pass");
8012 if (m == MATCH_ERROR)
8013 goto error;
8014 if (m == MATCH_YES)
8016 char arg[GFC_MAX_SYMBOL_LEN + 1];
8018 if (found_passing)
8020 gfc_error ("Binding attributes already specify passing,"
8021 " illegal PASS at %C");
8022 goto error;
8025 m = gfc_match (" ( %n )", arg);
8026 if (m == MATCH_ERROR)
8027 goto error;
8028 if (m == MATCH_YES)
8029 ba->pass_arg = gfc_get_string (arg);
8030 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
8032 found_passing = true;
8033 ba->nopass = 0;
8034 continue;
8037 if (ppc)
8039 /* POINTER flag. */
8040 m = gfc_match (" pointer");
8041 if (m == MATCH_ERROR)
8042 goto error;
8043 if (m == MATCH_YES)
8045 if (seen_ptr)
8047 gfc_error ("Duplicate POINTER attribute at %C");
8048 goto error;
8051 seen_ptr = true;
8052 continue;
8055 else
8057 /* NON_OVERRIDABLE flag. */
8058 m = gfc_match (" non_overridable");
8059 if (m == MATCH_ERROR)
8060 goto error;
8061 if (m == MATCH_YES)
8063 if (ba->non_overridable)
8065 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8066 goto error;
8069 ba->non_overridable = 1;
8070 continue;
8073 /* DEFERRED flag. */
8074 m = gfc_match (" deferred");
8075 if (m == MATCH_ERROR)
8076 goto error;
8077 if (m == MATCH_YES)
8079 if (ba->deferred)
8081 gfc_error ("Duplicate DEFERRED at %C");
8082 goto error;
8085 ba->deferred = 1;
8086 continue;
8092 /* Nothing matching found. */
8093 if (generic)
8094 gfc_error ("Expected access-specifier at %C");
8095 else
8096 gfc_error ("Expected binding attribute at %C");
8097 goto error;
8099 while (gfc_match_char (',') == MATCH_YES);
8101 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8102 if (ba->non_overridable && ba->deferred)
8104 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8105 goto error;
8108 m = MATCH_YES;
8110 done:
8111 if (ba->access == ACCESS_UNKNOWN)
8112 ba->access = gfc_typebound_default_access;
8114 if (ppc && !seen_ptr)
8116 gfc_error ("POINTER attribute is required for procedure pointer component"
8117 " at %C");
8118 goto error;
8121 return m;
8123 error:
8124 return MATCH_ERROR;
8128 /* Match a PROCEDURE specific binding inside a derived type. */
8130 static match
8131 match_procedure_in_type (void)
8133 char name[GFC_MAX_SYMBOL_LEN + 1];
8134 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
8135 char* target = NULL, *ifc = NULL;
8136 gfc_typebound_proc tb;
8137 bool seen_colons;
8138 bool seen_attrs;
8139 match m;
8140 gfc_symtree* stree;
8141 gfc_namespace* ns;
8142 gfc_symbol* block;
8143 int num;
8145 /* Check current state. */
8146 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8147 block = gfc_state_stack->previous->sym;
8148 gcc_assert (block);
8150 /* Try to match PROCEDURE(interface). */
8151 if (gfc_match (" (") == MATCH_YES)
8153 m = gfc_match_name (target_buf);
8154 if (m == MATCH_ERROR)
8155 return m;
8156 if (m != MATCH_YES)
8158 gfc_error ("Interface-name expected after '(' at %C");
8159 return MATCH_ERROR;
8162 if (gfc_match (" )") != MATCH_YES)
8164 gfc_error ("')' expected at %C");
8165 return MATCH_ERROR;
8168 ifc = target_buf;
8171 /* Construct the data structure. */
8172 memset (&tb, 0, sizeof (tb));
8173 tb.where = gfc_current_locus;
8175 /* Match binding attributes. */
8176 m = match_binding_attributes (&tb, false, false);
8177 if (m == MATCH_ERROR)
8178 return m;
8179 seen_attrs = (m == MATCH_YES);
8181 /* Check that attribute DEFERRED is given if an interface is specified. */
8182 if (tb.deferred && !ifc)
8184 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8185 return MATCH_ERROR;
8187 if (ifc && !tb.deferred)
8189 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8190 return MATCH_ERROR;
8193 /* Match the colons. */
8194 m = gfc_match (" ::");
8195 if (m == MATCH_ERROR)
8196 return m;
8197 seen_colons = (m == MATCH_YES);
8198 if (seen_attrs && !seen_colons)
8200 gfc_error ("Expected '::' after binding-attributes at %C");
8201 return MATCH_ERROR;
8204 /* Match the binding names. */
8205 for(num=1;;num++)
8207 m = gfc_match_name (name);
8208 if (m == MATCH_ERROR)
8209 return m;
8210 if (m == MATCH_NO)
8212 gfc_error ("Expected binding name at %C");
8213 return MATCH_ERROR;
8216 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
8217 return MATCH_ERROR;
8219 /* Try to match the '=> target', if it's there. */
8220 target = ifc;
8221 m = gfc_match (" =>");
8222 if (m == MATCH_ERROR)
8223 return m;
8224 if (m == MATCH_YES)
8226 if (tb.deferred)
8228 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
8229 return MATCH_ERROR;
8232 if (!seen_colons)
8234 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
8235 " at %C");
8236 return MATCH_ERROR;
8239 m = gfc_match_name (target_buf);
8240 if (m == MATCH_ERROR)
8241 return m;
8242 if (m == MATCH_NO)
8244 gfc_error ("Expected binding target after '=>' at %C");
8245 return MATCH_ERROR;
8247 target = target_buf;
8250 /* If no target was found, it has the same name as the binding. */
8251 if (!target)
8252 target = name;
8254 /* Get the namespace to insert the symbols into. */
8255 ns = block->f2k_derived;
8256 gcc_assert (ns);
8258 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8259 if (tb.deferred && !block->attr.abstract)
8261 gfc_error ("Type '%s' containing DEFERRED binding at %C "
8262 "is not ABSTRACT", block->name);
8263 return MATCH_ERROR;
8266 /* See if we already have a binding with this name in the symtree which
8267 would be an error. If a GENERIC already targeted this binding, it may
8268 be already there but then typebound is still NULL. */
8269 stree = gfc_find_symtree (ns->tb_sym_root, name);
8270 if (stree && stree->n.tb)
8272 gfc_error ("There is already a procedure with binding name '%s' for "
8273 "the derived type '%s' at %C", name, block->name);
8274 return MATCH_ERROR;
8277 /* Insert it and set attributes. */
8279 if (!stree)
8281 stree = gfc_new_symtree (&ns->tb_sym_root, name);
8282 gcc_assert (stree);
8284 stree->n.tb = gfc_get_typebound_proc (&tb);
8286 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8287 false))
8288 return MATCH_ERROR;
8289 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
8291 if (gfc_match_eos () == MATCH_YES)
8292 return MATCH_YES;
8293 if (gfc_match_char (',') != MATCH_YES)
8294 goto syntax;
8297 syntax:
8298 gfc_error ("Syntax error in PROCEDURE statement at %C");
8299 return MATCH_ERROR;
8303 /* Match a GENERIC procedure binding inside a derived type. */
8305 match
8306 gfc_match_generic (void)
8308 char name[GFC_MAX_SYMBOL_LEN + 1];
8309 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
8310 gfc_symbol* block;
8311 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8312 gfc_typebound_proc* tb;
8313 gfc_namespace* ns;
8314 interface_type op_type;
8315 gfc_intrinsic_op op;
8316 match m;
8318 /* Check current state. */
8319 if (gfc_current_state () == COMP_DERIVED)
8321 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8322 return MATCH_ERROR;
8324 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8325 return MATCH_NO;
8326 block = gfc_state_stack->previous->sym;
8327 ns = block->f2k_derived;
8328 gcc_assert (block && ns);
8330 memset (&tbattr, 0, sizeof (tbattr));
8331 tbattr.where = gfc_current_locus;
8333 /* See if we get an access-specifier. */
8334 m = match_binding_attributes (&tbattr, true, false);
8335 if (m == MATCH_ERROR)
8336 goto error;
8338 /* Now the colons, those are required. */
8339 if (gfc_match (" ::") != MATCH_YES)
8341 gfc_error ("Expected '::' at %C");
8342 goto error;
8345 /* Match the binding name; depending on type (operator / generic) format
8346 it for future error messages into bind_name. */
8348 m = gfc_match_generic_spec (&op_type, name, &op);
8349 if (m == MATCH_ERROR)
8350 return MATCH_ERROR;
8351 if (m == MATCH_NO)
8353 gfc_error ("Expected generic name or operator descriptor at %C");
8354 goto error;
8357 switch (op_type)
8359 case INTERFACE_GENERIC:
8360 snprintf (bind_name, sizeof (bind_name), "%s", name);
8361 break;
8363 case INTERFACE_USER_OP:
8364 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8365 break;
8367 case INTERFACE_INTRINSIC_OP:
8368 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8369 gfc_op2string (op));
8370 break;
8372 default:
8373 gcc_unreachable ();
8376 /* Match the required =>. */
8377 if (gfc_match (" =>") != MATCH_YES)
8379 gfc_error ("Expected '=>' at %C");
8380 goto error;
8383 /* Try to find existing GENERIC binding with this name / for this operator;
8384 if there is something, check that it is another GENERIC and then extend
8385 it rather than building a new node. Otherwise, create it and put it
8386 at the right position. */
8388 switch (op_type)
8390 case INTERFACE_USER_OP:
8391 case INTERFACE_GENERIC:
8393 const bool is_op = (op_type == INTERFACE_USER_OP);
8394 gfc_symtree* st;
8396 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8397 if (st)
8399 tb = st->n.tb;
8400 gcc_assert (tb);
8402 else
8403 tb = NULL;
8405 break;
8408 case INTERFACE_INTRINSIC_OP:
8409 tb = ns->tb_op[op];
8410 break;
8412 default:
8413 gcc_unreachable ();
8416 if (tb)
8418 if (!tb->is_generic)
8420 gcc_assert (op_type == INTERFACE_GENERIC);
8421 gfc_error ("There's already a non-generic procedure with binding name"
8422 " '%s' for the derived type '%s' at %C",
8423 bind_name, block->name);
8424 goto error;
8427 if (tb->access != tbattr.access)
8429 gfc_error ("Binding at %C must have the same access as already"
8430 " defined binding '%s'", bind_name);
8431 goto error;
8434 else
8436 tb = gfc_get_typebound_proc (NULL);
8437 tb->where = gfc_current_locus;
8438 tb->access = tbattr.access;
8439 tb->is_generic = 1;
8440 tb->u.generic = NULL;
8442 switch (op_type)
8444 case INTERFACE_GENERIC:
8445 case INTERFACE_USER_OP:
8447 const bool is_op = (op_type == INTERFACE_USER_OP);
8448 gfc_symtree* st;
8450 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8451 name);
8452 gcc_assert (st);
8453 st->n.tb = tb;
8455 break;
8458 case INTERFACE_INTRINSIC_OP:
8459 ns->tb_op[op] = tb;
8460 break;
8462 default:
8463 gcc_unreachable ();
8467 /* Now, match all following names as specific targets. */
8470 gfc_symtree* target_st;
8471 gfc_tbp_generic* target;
8473 m = gfc_match_name (name);
8474 if (m == MATCH_ERROR)
8475 goto error;
8476 if (m == MATCH_NO)
8478 gfc_error ("Expected specific binding name at %C");
8479 goto error;
8482 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8484 /* See if this is a duplicate specification. */
8485 for (target = tb->u.generic; target; target = target->next)
8486 if (target_st == target->specific_st)
8488 gfc_error ("'%s' already defined as specific binding for the"
8489 " generic '%s' at %C", name, bind_name);
8490 goto error;
8493 target = gfc_get_tbp_generic ();
8494 target->specific_st = target_st;
8495 target->specific = NULL;
8496 target->next = tb->u.generic;
8497 target->is_operator = ((op_type == INTERFACE_USER_OP)
8498 || (op_type == INTERFACE_INTRINSIC_OP));
8499 tb->u.generic = target;
8501 while (gfc_match (" ,") == MATCH_YES);
8503 /* Here should be the end. */
8504 if (gfc_match_eos () != MATCH_YES)
8506 gfc_error ("Junk after GENERIC binding at %C");
8507 goto error;
8510 return MATCH_YES;
8512 error:
8513 return MATCH_ERROR;
8517 /* Match a FINAL declaration inside a derived type. */
8519 match
8520 gfc_match_final_decl (void)
8522 char name[GFC_MAX_SYMBOL_LEN + 1];
8523 gfc_symbol* sym;
8524 match m;
8525 gfc_namespace* module_ns;
8526 bool first, last;
8527 gfc_symbol* block;
8529 if (gfc_current_form == FORM_FREE)
8531 char c = gfc_peek_ascii_char ();
8532 if (!gfc_is_whitespace (c) && c != ':')
8533 return MATCH_NO;
8536 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8538 if (gfc_current_form == FORM_FIXED)
8539 return MATCH_NO;
8541 gfc_error ("FINAL declaration at %C must be inside a derived type "
8542 "CONTAINS section");
8543 return MATCH_ERROR;
8546 block = gfc_state_stack->previous->sym;
8547 gcc_assert (block);
8549 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8550 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8552 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8553 " specification part of a MODULE");
8554 return MATCH_ERROR;
8557 module_ns = gfc_current_ns;
8558 gcc_assert (module_ns);
8559 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8561 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8562 if (gfc_match (" ::") == MATCH_ERROR)
8563 return MATCH_ERROR;
8565 /* Match the sequence of procedure names. */
8566 first = true;
8567 last = false;
8570 gfc_finalizer* f;
8572 if (first && gfc_match_eos () == MATCH_YES)
8574 gfc_error ("Empty FINAL at %C");
8575 return MATCH_ERROR;
8578 m = gfc_match_name (name);
8579 if (m == MATCH_NO)
8581 gfc_error ("Expected module procedure name at %C");
8582 return MATCH_ERROR;
8584 else if (m != MATCH_YES)
8585 return MATCH_ERROR;
8587 if (gfc_match_eos () == MATCH_YES)
8588 last = true;
8589 if (!last && gfc_match_char (',') != MATCH_YES)
8591 gfc_error ("Expected ',' at %C");
8592 return MATCH_ERROR;
8595 if (gfc_get_symbol (name, module_ns, &sym))
8597 gfc_error ("Unknown procedure name \"%s\" at %C", name);
8598 return MATCH_ERROR;
8601 /* Mark the symbol as module procedure. */
8602 if (sym->attr.proc != PROC_MODULE
8603 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
8604 return MATCH_ERROR;
8606 /* Check if we already have this symbol in the list, this is an error. */
8607 for (f = block->f2k_derived->finalizers; f; f = f->next)
8608 if (f->proc_sym == sym)
8610 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8611 name);
8612 return MATCH_ERROR;
8615 /* Add this symbol to the list of finalizers. */
8616 gcc_assert (block->f2k_derived);
8617 ++sym->refs;
8618 f = XCNEW (gfc_finalizer);
8619 f->proc_sym = sym;
8620 f->proc_tree = NULL;
8621 f->where = gfc_current_locus;
8622 f->next = block->f2k_derived->finalizers;
8623 block->f2k_derived->finalizers = f;
8625 first = false;
8627 while (!last);
8629 return MATCH_YES;
8633 const ext_attr_t ext_attr_list[] = {
8634 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8635 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8636 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
8637 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
8638 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
8639 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
8640 { NULL, EXT_ATTR_LAST, NULL }
8643 /* Match a !GCC$ ATTRIBUTES statement of the form:
8644 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8645 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8647 TODO: We should support all GCC attributes using the same syntax for
8648 the attribute list, i.e. the list in C
8649 __attributes(( attribute-list ))
8650 matches then
8651 !GCC$ ATTRIBUTES attribute-list ::
8652 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8653 saved into a TREE.
8655 As there is absolutely no risk of confusion, we should never return
8656 MATCH_NO. */
8657 match
8658 gfc_match_gcc_attributes (void)
8660 symbol_attribute attr;
8661 char name[GFC_MAX_SYMBOL_LEN + 1];
8662 unsigned id;
8663 gfc_symbol *sym;
8664 match m;
8666 gfc_clear_attr (&attr);
8667 for(;;)
8669 char ch;
8671 if (gfc_match_name (name) != MATCH_YES)
8672 return MATCH_ERROR;
8674 for (id = 0; id < EXT_ATTR_LAST; id++)
8675 if (strcmp (name, ext_attr_list[id].name) == 0)
8676 break;
8678 if (id == EXT_ATTR_LAST)
8680 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8681 return MATCH_ERROR;
8684 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
8685 return MATCH_ERROR;
8687 gfc_gobble_whitespace ();
8688 ch = gfc_next_ascii_char ();
8689 if (ch == ':')
8691 /* This is the successful exit condition for the loop. */
8692 if (gfc_next_ascii_char () == ':')
8693 break;
8696 if (ch == ',')
8697 continue;
8699 goto syntax;
8702 if (gfc_match_eos () == MATCH_YES)
8703 goto syntax;
8705 for(;;)
8707 m = gfc_match_name (name);
8708 if (m != MATCH_YES)
8709 return m;
8711 if (find_special (name, &sym, true))
8712 return MATCH_ERROR;
8714 sym->attr.ext_attr |= attr.ext_attr;
8716 if (gfc_match_eos () == MATCH_YES)
8717 break;
8719 if (gfc_match_char (',') != MATCH_YES)
8720 goto syntax;
8723 return MATCH_YES;
8725 syntax:
8726 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
8727 return MATCH_ERROR;