2011-06-14 Zdenek Dvorak <ook@ucw.cz>
[official-gcc.git] / gcc / fortran / decl.c
blob7098368e56efab9511f13de86055fb46926885a2
1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "gfortran.h"
25 #include "match.h"
26 #include "parse.h"
27 #include "flags.h"
28 #include "constructor.h"
30 /* Macros to access allocate memory for gfc_data_variable,
31 gfc_data_value and gfc_data. */
32 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
33 #define gfc_get_data_value() XCNEW (gfc_data_value)
34 #define gfc_get_data() XCNEW (gfc_data)
37 /* This flag is set if an old-style length selector is matched
38 during a type-declaration statement. */
40 static int old_char_selector;
42 /* When variables acquire types and attributes from a declaration
43 statement, they get them from the following static variables. The
44 first part of a declaration sets these variables and the second
45 part copies these into symbol structures. */
47 static gfc_typespec current_ts;
49 static symbol_attribute current_attr;
50 static gfc_array_spec *current_as;
51 static int colon_seen;
53 /* The current binding label (if any). */
54 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
55 /* Need to know how many identifiers are on the current data declaration
56 line in case we're given the BIND(C) attribute with a NAME= specifier. */
57 static int num_idents_on_line;
58 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
59 can supply a name if the curr_binding_label is nil and NAME= was not. */
60 static int has_name_equals = 0;
62 /* Initializer of the previous enumerator. */
64 static gfc_expr *last_initializer;
66 /* History of all the enumerators is maintained, so that
67 kind values of all the enumerators could be updated depending
68 upon the maximum initialized value. */
70 typedef struct enumerator_history
72 gfc_symbol *sym;
73 gfc_expr *initializer;
74 struct enumerator_history *next;
76 enumerator_history;
78 /* Header of enum history chain. */
80 static enumerator_history *enum_history = NULL;
82 /* Pointer of enum history node containing largest initializer. */
84 static enumerator_history *max_enum = NULL;
86 /* gfc_new_block points to the symbol of a newly matched block. */
88 gfc_symbol *gfc_new_block;
90 bool gfc_matching_function;
93 /********************* DATA statement subroutines *********************/
95 static bool in_match_data = false;
97 bool
98 gfc_in_match_data (void)
100 return in_match_data;
103 static void
104 set_in_match_data (bool set_value)
106 in_match_data = set_value;
109 /* Free a gfc_data_variable structure and everything beneath it. */
111 static void
112 free_variable (gfc_data_variable *p)
114 gfc_data_variable *q;
116 for (; p; p = q)
118 q = p->next;
119 gfc_free_expr (p->expr);
120 gfc_free_iterator (&p->iter, 0);
121 free_variable (p->list);
122 free (p);
127 /* Free a gfc_data_value structure and everything beneath it. */
129 static void
130 free_value (gfc_data_value *p)
132 gfc_data_value *q;
134 for (; p; p = q)
136 q = p->next;
137 mpz_clear (p->repeat);
138 gfc_free_expr (p->expr);
139 free (p);
144 /* Free a list of gfc_data structures. */
146 void
147 gfc_free_data (gfc_data *p)
149 gfc_data *q;
151 for (; p; p = q)
153 q = p->next;
154 free_variable (p->var);
155 free_value (p->value);
156 free (p);
161 /* Free all data in a namespace. */
163 static void
164 gfc_free_data_all (gfc_namespace *ns)
166 gfc_data *d;
168 for (;ns->data;)
170 d = ns->data->next;
171 free (ns->data);
172 ns->data = d;
177 static match var_element (gfc_data_variable *);
179 /* Match a list of variables terminated by an iterator and a right
180 parenthesis. */
182 static match
183 var_list (gfc_data_variable *parent)
185 gfc_data_variable *tail, var;
186 match m;
188 m = var_element (&var);
189 if (m == MATCH_ERROR)
190 return MATCH_ERROR;
191 if (m == MATCH_NO)
192 goto syntax;
194 tail = gfc_get_data_variable ();
195 *tail = var;
197 parent->list = tail;
199 for (;;)
201 if (gfc_match_char (',') != MATCH_YES)
202 goto syntax;
204 m = gfc_match_iterator (&parent->iter, 1);
205 if (m == MATCH_YES)
206 break;
207 if (m == MATCH_ERROR)
208 return MATCH_ERROR;
210 m = var_element (&var);
211 if (m == MATCH_ERROR)
212 return MATCH_ERROR;
213 if (m == MATCH_NO)
214 goto syntax;
216 tail->next = gfc_get_data_variable ();
217 tail = tail->next;
219 *tail = var;
222 if (gfc_match_char (')') != MATCH_YES)
223 goto syntax;
224 return MATCH_YES;
226 syntax:
227 gfc_syntax_error (ST_DATA);
228 return MATCH_ERROR;
232 /* Match a single element in a data variable list, which can be a
233 variable-iterator list. */
235 static match
236 var_element (gfc_data_variable *new_var)
238 match m;
239 gfc_symbol *sym;
241 memset (new_var, 0, sizeof (gfc_data_variable));
243 if (gfc_match_char ('(') == MATCH_YES)
244 return var_list (new_var);
246 m = gfc_match_variable (&new_var->expr, 0);
247 if (m != MATCH_YES)
248 return m;
250 sym = new_var->expr->symtree->n.sym;
252 /* Symbol should already have an associated type. */
253 if (gfc_check_symbol_typed (sym, gfc_current_ns,
254 false, gfc_current_locus) == FAILURE)
255 return MATCH_ERROR;
257 if (!sym->attr.function && gfc_current_ns->parent
258 && gfc_current_ns->parent == sym->ns)
260 gfc_error ("Host associated variable '%s' may not be in the DATA "
261 "statement at %C", sym->name);
262 return MATCH_ERROR;
265 if (gfc_current_state () != COMP_BLOCK_DATA
266 && sym->attr.in_common
267 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
268 "common block variable '%s' in DATA statement at %C",
269 sym->name) == FAILURE)
270 return MATCH_ERROR;
272 if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
273 return MATCH_ERROR;
275 return MATCH_YES;
279 /* Match the top-level list of data variables. */
281 static match
282 top_var_list (gfc_data *d)
284 gfc_data_variable var, *tail, *new_var;
285 match m;
287 tail = NULL;
289 for (;;)
291 m = var_element (&var);
292 if (m == MATCH_NO)
293 goto syntax;
294 if (m == MATCH_ERROR)
295 return MATCH_ERROR;
297 new_var = gfc_get_data_variable ();
298 *new_var = var;
300 if (tail == NULL)
301 d->var = new_var;
302 else
303 tail->next = new_var;
305 tail = new_var;
307 if (gfc_match_char ('/') == MATCH_YES)
308 break;
309 if (gfc_match_char (',') != MATCH_YES)
310 goto syntax;
313 return MATCH_YES;
315 syntax:
316 gfc_syntax_error (ST_DATA);
317 gfc_free_data_all (gfc_current_ns);
318 return MATCH_ERROR;
322 static match
323 match_data_constant (gfc_expr **result)
325 char name[GFC_MAX_SYMBOL_LEN + 1];
326 gfc_symbol *sym;
327 gfc_expr *expr;
328 match m;
329 locus old_loc;
331 m = gfc_match_literal_constant (&expr, 1);
332 if (m == MATCH_YES)
334 *result = expr;
335 return MATCH_YES;
338 if (m == MATCH_ERROR)
339 return MATCH_ERROR;
341 m = gfc_match_null (result);
342 if (m != MATCH_NO)
343 return m;
345 old_loc = gfc_current_locus;
347 /* Should this be a structure component, try to match it
348 before matching a name. */
349 m = gfc_match_rvalue (result);
350 if (m == MATCH_ERROR)
351 return m;
353 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
355 if (gfc_simplify_expr (*result, 0) == FAILURE)
356 m = MATCH_ERROR;
357 return m;
360 gfc_current_locus = old_loc;
362 m = gfc_match_name (name);
363 if (m != MATCH_YES)
364 return m;
366 if (gfc_find_symbol (name, NULL, 1, &sym))
367 return MATCH_ERROR;
369 if (sym == NULL
370 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
372 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
373 name);
374 return MATCH_ERROR;
376 else if (sym->attr.flavor == FL_DERIVED)
377 return gfc_match_structure_constructor (sym, result, false);
379 /* Check to see if the value is an initialization array expression. */
380 if (sym->value->expr_type == EXPR_ARRAY)
382 gfc_current_locus = old_loc;
384 m = gfc_match_init_expr (result);
385 if (m == MATCH_ERROR)
386 return m;
388 if (m == MATCH_YES)
390 if (gfc_simplify_expr (*result, 0) == FAILURE)
391 m = MATCH_ERROR;
393 if ((*result)->expr_type == EXPR_CONSTANT)
394 return m;
395 else
397 gfc_error ("Invalid initializer %s in Data statement at %C", name);
398 return MATCH_ERROR;
403 *result = gfc_copy_expr (sym->value);
404 return MATCH_YES;
408 /* Match a list of values in a DATA statement. The leading '/' has
409 already been seen at this point. */
411 static match
412 top_val_list (gfc_data *data)
414 gfc_data_value *new_val, *tail;
415 gfc_expr *expr;
416 match m;
418 tail = NULL;
420 for (;;)
422 m = match_data_constant (&expr);
423 if (m == MATCH_NO)
424 goto syntax;
425 if (m == MATCH_ERROR)
426 return MATCH_ERROR;
428 new_val = gfc_get_data_value ();
429 mpz_init (new_val->repeat);
431 if (tail == NULL)
432 data->value = new_val;
433 else
434 tail->next = new_val;
436 tail = new_val;
438 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
440 tail->expr = expr;
441 mpz_set_ui (tail->repeat, 1);
443 else
445 if (expr->ts.type == BT_INTEGER)
446 mpz_set (tail->repeat, expr->value.integer);
447 gfc_free_expr (expr);
449 m = match_data_constant (&tail->expr);
450 if (m == MATCH_NO)
451 goto syntax;
452 if (m == MATCH_ERROR)
453 return MATCH_ERROR;
456 if (gfc_match_char ('/') == MATCH_YES)
457 break;
458 if (gfc_match_char (',') == MATCH_NO)
459 goto syntax;
462 return MATCH_YES;
464 syntax:
465 gfc_syntax_error (ST_DATA);
466 gfc_free_data_all (gfc_current_ns);
467 return MATCH_ERROR;
471 /* Matches an old style initialization. */
473 static match
474 match_old_style_init (const char *name)
476 match m;
477 gfc_symtree *st;
478 gfc_symbol *sym;
479 gfc_data *newdata;
481 /* Set up data structure to hold initializers. */
482 gfc_find_sym_tree (name, NULL, 0, &st);
483 sym = st->n.sym;
485 newdata = gfc_get_data ();
486 newdata->var = gfc_get_data_variable ();
487 newdata->var->expr = gfc_get_variable_expr (st);
488 newdata->where = gfc_current_locus;
490 /* Match initial value list. This also eats the terminal '/'. */
491 m = top_val_list (newdata);
492 if (m != MATCH_YES)
494 free (newdata);
495 return m;
498 if (gfc_pure (NULL))
500 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
501 free (newdata);
502 return MATCH_ERROR;
505 if (gfc_implicit_pure (NULL))
506 gfc_current_ns->proc_name->attr.implicit_pure = 0;
508 /* Mark the variable as having appeared in a data statement. */
509 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
511 free (newdata);
512 return MATCH_ERROR;
515 /* Chain in namespace list of DATA initializers. */
516 newdata->next = gfc_current_ns->data;
517 gfc_current_ns->data = newdata;
519 return m;
523 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
524 we are matching a DATA statement and are therefore issuing an error
525 if we encounter something unexpected, if not, we're trying to match
526 an old-style initialization expression of the form INTEGER I /2/. */
528 match
529 gfc_match_data (void)
531 gfc_data *new_data;
532 match m;
534 set_in_match_data (true);
536 for (;;)
538 new_data = gfc_get_data ();
539 new_data->where = gfc_current_locus;
541 m = top_var_list (new_data);
542 if (m != MATCH_YES)
543 goto cleanup;
545 m = top_val_list (new_data);
546 if (m != MATCH_YES)
547 goto cleanup;
549 new_data->next = gfc_current_ns->data;
550 gfc_current_ns->data = new_data;
552 if (gfc_match_eos () == MATCH_YES)
553 break;
555 gfc_match_char (','); /* Optional comma */
558 set_in_match_data (false);
560 if (gfc_pure (NULL))
562 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
563 return MATCH_ERROR;
566 if (gfc_implicit_pure (NULL))
567 gfc_current_ns->proc_name->attr.implicit_pure = 0;
569 return MATCH_YES;
571 cleanup:
572 set_in_match_data (false);
573 gfc_free_data (new_data);
574 return MATCH_ERROR;
578 /************************ Declaration statements *********************/
581 /* Auxilliary function to merge DIMENSION and CODIMENSION array specs. */
583 static void
584 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
586 int i;
588 if (to->rank == 0 && from->rank > 0)
590 to->rank = from->rank;
591 to->type = from->type;
592 to->cray_pointee = from->cray_pointee;
593 to->cp_was_assumed = from->cp_was_assumed;
595 for (i = 0; i < to->corank; i++)
597 to->lower[from->rank + i] = to->lower[i];
598 to->upper[from->rank + i] = to->upper[i];
600 for (i = 0; i < from->rank; i++)
602 if (copy)
604 to->lower[i] = gfc_copy_expr (from->lower[i]);
605 to->upper[i] = gfc_copy_expr (from->upper[i]);
607 else
609 to->lower[i] = from->lower[i];
610 to->upper[i] = from->upper[i];
614 else if (to->corank == 0 && from->corank > 0)
616 to->corank = from->corank;
617 to->cotype = from->cotype;
619 for (i = 0; i < from->corank; i++)
621 if (copy)
623 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
624 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
626 else
628 to->lower[to->rank + i] = from->lower[i];
629 to->upper[to->rank + i] = from->upper[i];
636 /* Match an intent specification. Since this can only happen after an
637 INTENT word, a legal intent-spec must follow. */
639 static sym_intent
640 match_intent_spec (void)
643 if (gfc_match (" ( in out )") == MATCH_YES)
644 return INTENT_INOUT;
645 if (gfc_match (" ( in )") == MATCH_YES)
646 return INTENT_IN;
647 if (gfc_match (" ( out )") == MATCH_YES)
648 return INTENT_OUT;
650 gfc_error ("Bad INTENT specification at %C");
651 return INTENT_UNKNOWN;
655 /* Matches a character length specification, which is either a
656 specification expression, '*', or ':'. */
658 static match
659 char_len_param_value (gfc_expr **expr, bool *deferred)
661 match m;
663 *expr = NULL;
664 *deferred = false;
666 if (gfc_match_char ('*') == MATCH_YES)
667 return MATCH_YES;
669 if (gfc_match_char (':') == MATCH_YES)
671 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: deferred type "
672 "parameter at %C") == FAILURE)
673 return MATCH_ERROR;
675 *deferred = true;
677 return MATCH_YES;
680 m = gfc_match_expr (expr);
682 if (m == MATCH_YES
683 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
684 return MATCH_ERROR;
686 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
688 if ((*expr)->value.function.actual
689 && (*expr)->value.function.actual->expr->symtree)
691 gfc_expr *e;
692 e = (*expr)->value.function.actual->expr;
693 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
694 && e->expr_type == EXPR_VARIABLE)
696 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
697 goto syntax;
698 if (e->symtree->n.sym->ts.type == BT_CHARACTER
699 && e->symtree->n.sym->ts.u.cl
700 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
701 goto syntax;
705 return m;
707 syntax:
708 gfc_error ("Conflict in attributes of function argument at %C");
709 return MATCH_ERROR;
713 /* A character length is a '*' followed by a literal integer or a
714 char_len_param_value in parenthesis. */
716 static match
717 match_char_length (gfc_expr **expr, bool *deferred)
719 int length;
720 match m;
722 *deferred = false;
723 m = gfc_match_char ('*');
724 if (m != MATCH_YES)
725 return m;
727 m = gfc_match_small_literal_int (&length, NULL);
728 if (m == MATCH_ERROR)
729 return m;
731 if (m == MATCH_YES)
733 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
734 "Old-style character length at %C") == FAILURE)
735 return MATCH_ERROR;
736 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
737 return m;
740 if (gfc_match_char ('(') == MATCH_NO)
741 goto syntax;
743 m = char_len_param_value (expr, deferred);
744 if (m != MATCH_YES && gfc_matching_function)
746 gfc_undo_symbols ();
747 m = MATCH_YES;
750 if (m == MATCH_ERROR)
751 return m;
752 if (m == MATCH_NO)
753 goto syntax;
755 if (gfc_match_char (')') == MATCH_NO)
757 gfc_free_expr (*expr);
758 *expr = NULL;
759 goto syntax;
762 return MATCH_YES;
764 syntax:
765 gfc_error ("Syntax error in character length specification at %C");
766 return MATCH_ERROR;
770 /* Special subroutine for finding a symbol. Check if the name is found
771 in the current name space. If not, and we're compiling a function or
772 subroutine and the parent compilation unit is an interface, then check
773 to see if the name we've been given is the name of the interface
774 (located in another namespace). */
776 static int
777 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
779 gfc_state_data *s;
780 gfc_symtree *st;
781 int i;
783 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
784 if (i == 0)
786 *result = st ? st->n.sym : NULL;
787 goto end;
790 if (gfc_current_state () != COMP_SUBROUTINE
791 && gfc_current_state () != COMP_FUNCTION)
792 goto end;
794 s = gfc_state_stack->previous;
795 if (s == NULL)
796 goto end;
798 if (s->state != COMP_INTERFACE)
799 goto end;
800 if (s->sym == NULL)
801 goto end; /* Nameless interface. */
803 if (strcmp (name, s->sym->name) == 0)
805 *result = s->sym;
806 return 0;
809 end:
810 return i;
814 /* Special subroutine for getting a symbol node associated with a
815 procedure name, used in SUBROUTINE and FUNCTION statements. The
816 symbol is created in the parent using with symtree node in the
817 child unit pointing to the symbol. If the current namespace has no
818 parent, then the symbol is just created in the current unit. */
820 static int
821 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
823 gfc_symtree *st;
824 gfc_symbol *sym;
825 int rc = 0;
827 /* Module functions have to be left in their own namespace because
828 they have potentially (almost certainly!) already been referenced.
829 In this sense, they are rather like external functions. This is
830 fixed up in resolve.c(resolve_entries), where the symbol name-
831 space is set to point to the master function, so that the fake
832 result mechanism can work. */
833 if (module_fcn_entry)
835 /* Present if entry is declared to be a module procedure. */
836 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
838 if (*result == NULL)
839 rc = gfc_get_symbol (name, NULL, result);
840 else if (!gfc_get_symbol (name, NULL, &sym) && sym
841 && (*result)->ts.type == BT_UNKNOWN
842 && sym->attr.flavor == FL_UNKNOWN)
843 /* Pick up the typespec for the entry, if declared in the function
844 body. Note that this symbol is FL_UNKNOWN because it will
845 only have appeared in a type declaration. The local symtree
846 is set to point to the module symbol and a unique symtree
847 to the local version. This latter ensures a correct clearing
848 of the symbols. */
850 /* If the ENTRY proceeds its specification, we need to ensure
851 that this does not raise a "has no IMPLICIT type" error. */
852 if (sym->ts.type == BT_UNKNOWN)
853 sym->attr.untyped = 1;
855 (*result)->ts = sym->ts;
857 /* Put the symbol in the procedure namespace so that, should
858 the ENTRY precede its specification, the specification
859 can be applied. */
860 (*result)->ns = gfc_current_ns;
862 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
863 st->n.sym = *result;
864 st = gfc_get_unique_symtree (gfc_current_ns);
865 st->n.sym = sym;
868 else
869 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
871 if (rc)
872 return rc;
874 sym = *result;
875 gfc_current_ns->refs++;
877 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
879 /* Trap another encompassed procedure with the same name. All
880 these conditions are necessary to avoid picking up an entry
881 whose name clashes with that of the encompassing procedure;
882 this is handled using gsymbols to register unique,globally
883 accessible names. */
884 if (sym->attr.flavor != 0
885 && sym->attr.proc != 0
886 && (sym->attr.subroutine || sym->attr.function)
887 && sym->attr.if_source != IFSRC_UNKNOWN)
888 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
889 name, &sym->declared_at);
891 /* Trap a procedure with a name the same as interface in the
892 encompassing scope. */
893 if (sym->attr.generic != 0
894 && (sym->attr.subroutine || sym->attr.function)
895 && !sym->attr.mod_proc)
896 gfc_error_now ("Name '%s' at %C is already defined"
897 " as a generic interface at %L",
898 name, &sym->declared_at);
900 /* Trap declarations of attributes in encompassing scope. The
901 signature for this is that ts.kind is set. Legitimate
902 references only set ts.type. */
903 if (sym->ts.kind != 0
904 && !sym->attr.implicit_type
905 && sym->attr.proc == 0
906 && gfc_current_ns->parent != NULL
907 && sym->attr.access == 0
908 && !module_fcn_entry)
909 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
910 "and must not have attributes declared at %L",
911 name, &sym->declared_at);
914 if (gfc_current_ns->parent == NULL || *result == NULL)
915 return rc;
917 /* Module function entries will already have a symtree in
918 the current namespace but will need one at module level. */
919 if (module_fcn_entry)
921 /* Present if entry is declared to be a module procedure. */
922 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
923 if (st == NULL)
924 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
926 else
927 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
929 st->n.sym = sym;
930 sym->refs++;
932 /* See if the procedure should be a module procedure. */
934 if (((sym->ns->proc_name != NULL
935 && sym->ns->proc_name->attr.flavor == FL_MODULE
936 && sym->attr.proc != PROC_MODULE)
937 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
938 && gfc_add_procedure (&sym->attr, PROC_MODULE,
939 sym->name, NULL) == FAILURE)
940 rc = 2;
942 return rc;
946 /* Verify that the given symbol representing a parameter is C
947 interoperable, by checking to see if it was marked as such after
948 its declaration. If the given symbol is not interoperable, a
949 warning is reported, thus removing the need to return the status to
950 the calling function. The standard does not require the user use
951 one of the iso_c_binding named constants to declare an
952 interoperable parameter, but we can't be sure if the param is C
953 interop or not if the user doesn't. For example, integer(4) may be
954 legal Fortran, but doesn't have meaning in C. It may interop with
955 a number of the C types, which causes a problem because the
956 compiler can't know which one. This code is almost certainly not
957 portable, and the user will get what they deserve if the C type
958 across platforms isn't always interoperable with integer(4). If
959 the user had used something like integer(c_int) or integer(c_long),
960 the compiler could have automatically handled the varying sizes
961 across platforms. */
963 gfc_try
964 verify_c_interop_param (gfc_symbol *sym)
966 int is_c_interop = 0;
967 gfc_try retval = SUCCESS;
969 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
970 Don't repeat the checks here. */
971 if (sym->attr.implicit_type)
972 return SUCCESS;
974 /* For subroutines or functions that are passed to a BIND(C) procedure,
975 they're interoperable if they're BIND(C) and their params are all
976 interoperable. */
977 if (sym->attr.flavor == FL_PROCEDURE)
979 if (sym->attr.is_bind_c == 0)
981 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
982 "attribute to be C interoperable", sym->name,
983 &(sym->declared_at));
985 return FAILURE;
987 else
989 if (sym->attr.is_c_interop == 1)
990 /* We've already checked this procedure; don't check it again. */
991 return SUCCESS;
992 else
993 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
994 sym->common_block);
998 /* See if we've stored a reference to a procedure that owns sym. */
999 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1001 if (sym->ns->proc_name->attr.is_bind_c == 1)
1003 is_c_interop =
1004 (verify_c_interop (&(sym->ts))
1005 == SUCCESS ? 1 : 0);
1007 if (is_c_interop != 1)
1009 /* Make personalized messages to give better feedback. */
1010 if (sym->ts.type == BT_DERIVED)
1011 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
1012 "procedure '%s' but is not C interoperable "
1013 "because derived type '%s' is not C interoperable",
1014 sym->name, &(sym->declared_at),
1015 sym->ns->proc_name->name,
1016 sym->ts.u.derived->name);
1017 else
1018 gfc_warning ("Variable '%s' at %L is a parameter to the "
1019 "BIND(C) procedure '%s' but may not be C "
1020 "interoperable",
1021 sym->name, &(sym->declared_at),
1022 sym->ns->proc_name->name);
1025 /* Character strings are only C interoperable if they have a
1026 length of 1. */
1027 if (sym->ts.type == BT_CHARACTER)
1029 gfc_charlen *cl = sym->ts.u.cl;
1030 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1031 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1033 gfc_error ("Character argument '%s' at %L "
1034 "must be length 1 because "
1035 "procedure '%s' is BIND(C)",
1036 sym->name, &sym->declared_at,
1037 sym->ns->proc_name->name);
1038 retval = FAILURE;
1042 /* We have to make sure that any param to a bind(c) routine does
1043 not have the allocatable, pointer, or optional attributes,
1044 according to J3/04-007, section 5.1. */
1045 if (sym->attr.allocatable == 1)
1047 gfc_error ("Variable '%s' at %L cannot have the "
1048 "ALLOCATABLE attribute because procedure '%s'"
1049 " is BIND(C)", sym->name, &(sym->declared_at),
1050 sym->ns->proc_name->name);
1051 retval = FAILURE;
1054 if (sym->attr.pointer == 1)
1056 gfc_error ("Variable '%s' at %L cannot have the "
1057 "POINTER attribute because procedure '%s'"
1058 " is BIND(C)", sym->name, &(sym->declared_at),
1059 sym->ns->proc_name->name);
1060 retval = FAILURE;
1063 if (sym->attr.optional == 1 && sym->attr.value)
1065 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1066 "and the VALUE attribute because procedure '%s' "
1067 "is BIND(C)", sym->name, &(sym->declared_at),
1068 sym->ns->proc_name->name);
1069 retval = FAILURE;
1071 else if (sym->attr.optional == 1
1072 && gfc_notify_std (GFC_STD_F2008_TR, "TR29113: Variable '%s' "
1073 "at %L with OPTIONAL attribute in "
1074 "procedure '%s' which is BIND(C)",
1075 sym->name, &(sym->declared_at),
1076 sym->ns->proc_name->name)
1077 == FAILURE)
1078 retval = FAILURE;
1080 /* Make sure that if it has the dimension attribute, that it is
1081 either assumed size or explicit shape. */
1082 if (sym->as != NULL)
1084 if (sym->as->type == AS_ASSUMED_SHAPE)
1086 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1087 "argument to the procedure '%s' at %L because "
1088 "the procedure is BIND(C)", sym->name,
1089 &(sym->declared_at), sym->ns->proc_name->name,
1090 &(sym->ns->proc_name->declared_at));
1091 retval = FAILURE;
1094 if (sym->as->type == AS_DEFERRED)
1096 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1097 "argument to the procedure '%s' at %L because "
1098 "the procedure is BIND(C)", sym->name,
1099 &(sym->declared_at), sym->ns->proc_name->name,
1100 &(sym->ns->proc_name->declared_at));
1101 retval = FAILURE;
1107 return retval;
1112 /* Function called by variable_decl() that adds a name to the symbol table. */
1114 static gfc_try
1115 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1116 gfc_array_spec **as, locus *var_locus)
1118 symbol_attribute attr;
1119 gfc_symbol *sym;
1121 if (gfc_get_symbol (name, NULL, &sym))
1122 return FAILURE;
1124 /* Start updating the symbol table. Add basic type attribute if present. */
1125 if (current_ts.type != BT_UNKNOWN
1126 && (sym->attr.implicit_type == 0
1127 || !gfc_compare_types (&sym->ts, &current_ts))
1128 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
1129 return FAILURE;
1131 if (sym->ts.type == BT_CHARACTER)
1133 sym->ts.u.cl = cl;
1134 sym->ts.deferred = cl_deferred;
1137 /* Add dimension attribute if present. */
1138 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1139 return FAILURE;
1140 *as = NULL;
1142 /* Add attribute to symbol. The copy is so that we can reset the
1143 dimension attribute. */
1144 attr = current_attr;
1145 attr.dimension = 0;
1146 attr.codimension = 0;
1148 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1149 return FAILURE;
1151 /* Finish any work that may need to be done for the binding label,
1152 if it's a bind(c). The bind(c) attr is found before the symbol
1153 is made, and before the symbol name (for data decls), so the
1154 current_ts is holding the binding label, or nothing if the
1155 name= attr wasn't given. Therefore, test here if we're dealing
1156 with a bind(c) and make sure the binding label is set correctly. */
1157 if (sym->attr.is_bind_c == 1)
1159 if (sym->binding_label[0] == '\0')
1161 /* Set the binding label and verify that if a NAME= was specified
1162 then only one identifier was in the entity-decl-list. */
1163 if (set_binding_label (sym->binding_label, sym->name,
1164 num_idents_on_line) == FAILURE)
1165 return FAILURE;
1169 /* See if we know we're in a common block, and if it's a bind(c)
1170 common then we need to make sure we're an interoperable type. */
1171 if (sym->attr.in_common == 1)
1173 /* Test the common block object. */
1174 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1175 && sym->ts.is_c_interop != 1)
1177 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1178 "must be declared with a C interoperable "
1179 "kind since common block '%s' is BIND(C)",
1180 sym->name, sym->common_block->name,
1181 sym->common_block->name);
1182 gfc_clear_error ();
1186 sym->attr.implied_index = 0;
1188 if (sym->ts.type == BT_CLASS)
1189 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1191 return SUCCESS;
1195 /* Set character constant to the given length. The constant will be padded or
1196 truncated. If we're inside an array constructor without a typespec, we
1197 additionally check that all elements have the same length; check_len -1
1198 means no checking. */
1200 void
1201 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1203 gfc_char_t *s;
1204 int slen;
1206 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1207 gcc_assert (expr->ts.type == BT_CHARACTER);
1209 slen = expr->value.character.length;
1210 if (len != slen)
1212 s = gfc_get_wide_string (len + 1);
1213 memcpy (s, expr->value.character.string,
1214 MIN (len, slen) * sizeof (gfc_char_t));
1215 if (len > slen)
1216 gfc_wide_memset (&s[slen], ' ', len - slen);
1218 if (gfc_option.warn_character_truncation && slen > len)
1219 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1220 "(%d/%d)", &expr->where, slen, len);
1222 /* Apply the standard by 'hand' otherwise it gets cleared for
1223 initializers. */
1224 if (check_len != -1 && slen != check_len
1225 && !(gfc_option.allow_std & GFC_STD_GNU))
1226 gfc_error_now ("The CHARACTER elements of the array constructor "
1227 "at %L must have the same length (%d/%d)",
1228 &expr->where, slen, check_len);
1230 s[len] = '\0';
1231 free (expr->value.character.string);
1232 expr->value.character.string = s;
1233 expr->value.character.length = len;
1238 /* Function to create and update the enumerator history
1239 using the information passed as arguments.
1240 Pointer "max_enum" is also updated, to point to
1241 enum history node containing largest initializer.
1243 SYM points to the symbol node of enumerator.
1244 INIT points to its enumerator value. */
1246 static void
1247 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1249 enumerator_history *new_enum_history;
1250 gcc_assert (sym != NULL && init != NULL);
1252 new_enum_history = XCNEW (enumerator_history);
1254 new_enum_history->sym = sym;
1255 new_enum_history->initializer = init;
1256 new_enum_history->next = NULL;
1258 if (enum_history == NULL)
1260 enum_history = new_enum_history;
1261 max_enum = enum_history;
1263 else
1265 new_enum_history->next = enum_history;
1266 enum_history = new_enum_history;
1268 if (mpz_cmp (max_enum->initializer->value.integer,
1269 new_enum_history->initializer->value.integer) < 0)
1270 max_enum = new_enum_history;
1275 /* Function to free enum kind history. */
1277 void
1278 gfc_free_enum_history (void)
1280 enumerator_history *current = enum_history;
1281 enumerator_history *next;
1283 while (current != NULL)
1285 next = current->next;
1286 free (current);
1287 current = next;
1289 max_enum = NULL;
1290 enum_history = NULL;
1294 /* Function called by variable_decl() that adds an initialization
1295 expression to a symbol. */
1297 static gfc_try
1298 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1300 symbol_attribute attr;
1301 gfc_symbol *sym;
1302 gfc_expr *init;
1304 init = *initp;
1305 if (find_special (name, &sym, false))
1306 return FAILURE;
1308 attr = sym->attr;
1310 /* If this symbol is confirming an implicit parameter type,
1311 then an initialization expression is not allowed. */
1312 if (attr.flavor == FL_PARAMETER
1313 && sym->value != NULL
1314 && *initp != NULL)
1316 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1317 sym->name);
1318 return FAILURE;
1321 if (init == NULL)
1323 /* An initializer is required for PARAMETER declarations. */
1324 if (attr.flavor == FL_PARAMETER)
1326 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1327 return FAILURE;
1330 else
1332 /* If a variable appears in a DATA block, it cannot have an
1333 initializer. */
1334 if (sym->attr.data)
1336 gfc_error ("Variable '%s' at %C with an initializer already "
1337 "appears in a DATA statement", sym->name);
1338 return FAILURE;
1341 /* Check if the assignment can happen. This has to be put off
1342 until later for derived type variables and procedure pointers. */
1343 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1344 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1345 && !sym->attr.proc_pointer
1346 && gfc_check_assign_symbol (sym, init) == FAILURE)
1347 return FAILURE;
1349 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1350 && init->ts.type == BT_CHARACTER)
1352 /* Update symbol character length according initializer. */
1353 if (gfc_check_assign_symbol (sym, init) == FAILURE)
1354 return FAILURE;
1356 if (sym->ts.u.cl->length == NULL)
1358 int clen;
1359 /* If there are multiple CHARACTER variables declared on the
1360 same line, we don't want them to share the same length. */
1361 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1363 if (sym->attr.flavor == FL_PARAMETER)
1365 if (init->expr_type == EXPR_CONSTANT)
1367 clen = init->value.character.length;
1368 sym->ts.u.cl->length
1369 = gfc_get_int_expr (gfc_default_integer_kind,
1370 NULL, clen);
1372 else if (init->expr_type == EXPR_ARRAY)
1374 gfc_constructor *c;
1375 c = gfc_constructor_first (init->value.constructor);
1376 clen = c->expr->value.character.length;
1377 sym->ts.u.cl->length
1378 = gfc_get_int_expr (gfc_default_integer_kind,
1379 NULL, clen);
1381 else if (init->ts.u.cl && init->ts.u.cl->length)
1382 sym->ts.u.cl->length =
1383 gfc_copy_expr (sym->value->ts.u.cl->length);
1386 /* Update initializer character length according symbol. */
1387 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1389 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1391 if (init->expr_type == EXPR_CONSTANT)
1392 gfc_set_constant_character_len (len, init, -1);
1393 else if (init->expr_type == EXPR_ARRAY)
1395 gfc_constructor *c;
1397 /* Build a new charlen to prevent simplification from
1398 deleting the length before it is resolved. */
1399 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1400 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1402 for (c = gfc_constructor_first (init->value.constructor);
1403 c; c = gfc_constructor_next (c))
1404 gfc_set_constant_character_len (len, c->expr, -1);
1409 /* If sym is implied-shape, set its upper bounds from init. */
1410 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1411 && sym->as->type == AS_IMPLIED_SHAPE)
1413 int dim;
1415 if (init->rank == 0)
1417 gfc_error ("Can't initialize implied-shape array at %L"
1418 " with scalar", &sym->declared_at);
1419 return FAILURE;
1421 gcc_assert (sym->as->rank == init->rank);
1423 /* Shape should be present, we get an initialization expression. */
1424 gcc_assert (init->shape);
1426 for (dim = 0; dim < sym->as->rank; ++dim)
1428 int k;
1429 gfc_expr* lower;
1430 gfc_expr* e;
1432 lower = sym->as->lower[dim];
1433 if (lower->expr_type != EXPR_CONSTANT)
1435 gfc_error ("Non-constant lower bound in implied-shape"
1436 " declaration at %L", &lower->where);
1437 return FAILURE;
1440 /* All dimensions must be without upper bound. */
1441 gcc_assert (!sym->as->upper[dim]);
1443 k = lower->ts.kind;
1444 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1445 mpz_add (e->value.integer,
1446 lower->value.integer, init->shape[dim]);
1447 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1448 sym->as->upper[dim] = e;
1451 sym->as->type = AS_EXPLICIT;
1454 /* Need to check if the expression we initialized this
1455 to was one of the iso_c_binding named constants. If so,
1456 and we're a parameter (constant), let it be iso_c.
1457 For example:
1458 integer(c_int), parameter :: my_int = c_int
1459 integer(my_int) :: my_int_2
1460 If we mark my_int as iso_c (since we can see it's value
1461 is equal to one of the named constants), then my_int_2
1462 will be considered C interoperable. */
1463 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1465 sym->ts.is_iso_c |= init->ts.is_iso_c;
1466 sym->ts.is_c_interop |= init->ts.is_c_interop;
1467 /* attr bits needed for module files. */
1468 sym->attr.is_iso_c |= init->ts.is_iso_c;
1469 sym->attr.is_c_interop |= init->ts.is_c_interop;
1470 if (init->ts.is_iso_c)
1471 sym->ts.f90_type = init->ts.f90_type;
1474 /* Add initializer. Make sure we keep the ranks sane. */
1475 if (sym->attr.dimension && init->rank == 0)
1477 mpz_t size;
1478 gfc_expr *array;
1479 int n;
1480 if (sym->attr.flavor == FL_PARAMETER
1481 && init->expr_type == EXPR_CONSTANT
1482 && spec_size (sym->as, &size) == SUCCESS
1483 && mpz_cmp_si (size, 0) > 0)
1485 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1486 &init->where);
1487 for (n = 0; n < (int)mpz_get_si (size); n++)
1488 gfc_constructor_append_expr (&array->value.constructor,
1489 n == 0
1490 ? init
1491 : gfc_copy_expr (init),
1492 &init->where);
1494 array->shape = gfc_get_shape (sym->as->rank);
1495 for (n = 0; n < sym->as->rank; n++)
1496 spec_dimen_size (sym->as, n, &array->shape[n]);
1498 init = array;
1499 mpz_clear (size);
1501 init->rank = sym->as->rank;
1504 sym->value = init;
1505 if (sym->attr.save == SAVE_NONE)
1506 sym->attr.save = SAVE_IMPLICIT;
1507 *initp = NULL;
1510 return SUCCESS;
1514 /* Function called by variable_decl() that adds a name to a structure
1515 being built. */
1517 static gfc_try
1518 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1519 gfc_array_spec **as)
1521 gfc_component *c;
1522 gfc_try t = SUCCESS;
1524 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1525 constructing, it must have the pointer attribute. */
1526 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1527 && current_ts.u.derived == gfc_current_block ()
1528 && current_attr.pointer == 0)
1530 gfc_error ("Component at %C must have the POINTER attribute");
1531 return FAILURE;
1534 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1536 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1538 gfc_error ("Array component of structure at %C must have explicit "
1539 "or deferred shape");
1540 return FAILURE;
1544 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1545 return FAILURE;
1547 c->ts = current_ts;
1548 if (c->ts.type == BT_CHARACTER)
1549 c->ts.u.cl = cl;
1550 c->attr = current_attr;
1552 c->initializer = *init;
1553 *init = NULL;
1555 c->as = *as;
1556 if (c->as != NULL)
1558 if (c->as->corank)
1559 c->attr.codimension = 1;
1560 if (c->as->rank)
1561 c->attr.dimension = 1;
1563 *as = NULL;
1565 /* Should this ever get more complicated, combine with similar section
1566 in add_init_expr_to_sym into a separate function. */
1567 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer && c->ts.u.cl
1568 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1570 int len;
1572 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1573 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1574 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1576 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1578 if (c->initializer->expr_type == EXPR_CONSTANT)
1579 gfc_set_constant_character_len (len, c->initializer, -1);
1580 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1581 c->initializer->ts.u.cl->length->value.integer))
1583 gfc_constructor *ctor;
1584 ctor = gfc_constructor_first (c->initializer->value.constructor);
1586 if (ctor)
1588 int first_len;
1589 bool has_ts = (c->initializer->ts.u.cl
1590 && c->initializer->ts.u.cl->length_from_typespec);
1592 /* Remember the length of the first element for checking
1593 that all elements *in the constructor* have the same
1594 length. This need not be the length of the LHS! */
1595 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1596 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1597 first_len = ctor->expr->value.character.length;
1599 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1600 if (ctor->expr->expr_type == EXPR_CONSTANT)
1602 gfc_set_constant_character_len (len, ctor->expr,
1603 has_ts ? -1 : first_len);
1604 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1610 /* Check array components. */
1611 if (!c->attr.dimension)
1612 goto scalar;
1614 if (c->attr.pointer)
1616 if (c->as->type != AS_DEFERRED)
1618 gfc_error ("Pointer array component of structure at %C must have a "
1619 "deferred shape");
1620 t = FAILURE;
1623 else if (c->attr.allocatable)
1625 if (c->as->type != AS_DEFERRED)
1627 gfc_error ("Allocatable component of structure at %C must have a "
1628 "deferred shape");
1629 t = FAILURE;
1632 else
1634 if (c->as->type != AS_EXPLICIT)
1636 gfc_error ("Array component of structure at %C must have an "
1637 "explicit shape");
1638 t = FAILURE;
1642 scalar:
1643 if (c->ts.type == BT_CLASS)
1645 bool delayed = (gfc_state_stack->sym == c->ts.u.derived)
1646 || (!c->ts.u.derived->components
1647 && !c->ts.u.derived->attr.zero_comp);
1648 return gfc_build_class_symbol (&c->ts, &c->attr, &c->as, delayed);
1651 return t;
1655 /* Match a 'NULL()', and possibly take care of some side effects. */
1657 match
1658 gfc_match_null (gfc_expr **result)
1660 gfc_symbol *sym;
1661 match m;
1663 m = gfc_match (" null ( )");
1664 if (m != MATCH_YES)
1665 return m;
1667 /* The NULL symbol now has to be/become an intrinsic function. */
1668 if (gfc_get_symbol ("null", NULL, &sym))
1670 gfc_error ("NULL() initialization at %C is ambiguous");
1671 return MATCH_ERROR;
1674 gfc_intrinsic_symbol (sym);
1676 if (sym->attr.proc != PROC_INTRINSIC
1677 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1678 sym->name, NULL) == FAILURE
1679 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1680 return MATCH_ERROR;
1682 *result = gfc_get_null_expr (&gfc_current_locus);
1684 return MATCH_YES;
1688 /* Match the initialization expr for a data pointer or procedure pointer. */
1690 static match
1691 match_pointer_init (gfc_expr **init, int procptr)
1693 match m;
1695 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1697 gfc_error ("Initialization of pointer at %C is not allowed in "
1698 "a PURE procedure");
1699 return MATCH_ERROR;
1702 /* Match NULL() initilization. */
1703 m = gfc_match_null (init);
1704 if (m != MATCH_NO)
1705 return m;
1707 /* Match non-NULL initialization. */
1708 gfc_matching_ptr_assignment = !procptr;
1709 gfc_matching_procptr_assignment = procptr;
1710 m = gfc_match_rvalue (init);
1711 gfc_matching_ptr_assignment = 0;
1712 gfc_matching_procptr_assignment = 0;
1713 if (m == MATCH_ERROR)
1714 return MATCH_ERROR;
1715 else if (m == MATCH_NO)
1717 gfc_error ("Error in pointer initialization at %C");
1718 return MATCH_ERROR;
1721 if (!procptr)
1722 gfc_resolve_expr (*init);
1724 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: non-NULL pointer "
1725 "initialization at %C") == FAILURE)
1726 return MATCH_ERROR;
1728 return MATCH_YES;
1732 /* Match a variable name with an optional initializer. When this
1733 subroutine is called, a variable is expected to be parsed next.
1734 Depending on what is happening at the moment, updates either the
1735 symbol table or the current interface. */
1737 static match
1738 variable_decl (int elem)
1740 char name[GFC_MAX_SYMBOL_LEN + 1];
1741 gfc_expr *initializer, *char_len;
1742 gfc_array_spec *as;
1743 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1744 gfc_charlen *cl;
1745 bool cl_deferred;
1746 locus var_locus;
1747 match m;
1748 gfc_try t;
1749 gfc_symbol *sym;
1751 initializer = NULL;
1752 as = NULL;
1753 cp_as = NULL;
1755 /* When we get here, we've just matched a list of attributes and
1756 maybe a type and a double colon. The next thing we expect to see
1757 is the name of the symbol. */
1758 m = gfc_match_name (name);
1759 if (m != MATCH_YES)
1760 goto cleanup;
1762 var_locus = gfc_current_locus;
1764 /* Now we could see the optional array spec. or character length. */
1765 m = gfc_match_array_spec (&as, true, true);
1766 if (m == MATCH_ERROR)
1767 goto cleanup;
1769 if (m == MATCH_NO)
1770 as = gfc_copy_array_spec (current_as);
1771 else if (current_as)
1772 merge_array_spec (current_as, as, true);
1774 if (gfc_option.flag_cray_pointer)
1775 cp_as = gfc_copy_array_spec (as);
1777 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1778 determine (and check) whether it can be implied-shape. If it
1779 was parsed as assumed-size, change it because PARAMETERs can not
1780 be assumed-size. */
1781 if (as)
1783 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1785 m = MATCH_ERROR;
1786 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1787 name, &var_locus);
1788 goto cleanup;
1791 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1792 && current_attr.flavor == FL_PARAMETER)
1793 as->type = AS_IMPLIED_SHAPE;
1795 if (as->type == AS_IMPLIED_SHAPE
1796 && gfc_notify_std (GFC_STD_F2008,
1797 "Fortran 2008: Implied-shape array at %L",
1798 &var_locus) == FAILURE)
1800 m = MATCH_ERROR;
1801 goto cleanup;
1805 char_len = NULL;
1806 cl = NULL;
1807 cl_deferred = false;
1809 if (current_ts.type == BT_CHARACTER)
1811 switch (match_char_length (&char_len, &cl_deferred))
1813 case MATCH_YES:
1814 cl = gfc_new_charlen (gfc_current_ns, NULL);
1816 cl->length = char_len;
1817 break;
1819 /* Non-constant lengths need to be copied after the first
1820 element. Also copy assumed lengths. */
1821 case MATCH_NO:
1822 if (elem > 1
1823 && (current_ts.u.cl->length == NULL
1824 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1826 cl = gfc_new_charlen (gfc_current_ns, NULL);
1827 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1829 else
1830 cl = current_ts.u.cl;
1832 cl_deferred = current_ts.deferred;
1834 break;
1836 case MATCH_ERROR:
1837 goto cleanup;
1841 /* If this symbol has already shown up in a Cray Pointer declaration,
1842 then we want to set the type & bail out. */
1843 if (gfc_option.flag_cray_pointer)
1845 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1846 if (sym != NULL && sym->attr.cray_pointee)
1848 sym->ts.type = current_ts.type;
1849 sym->ts.kind = current_ts.kind;
1850 sym->ts.u.cl = cl;
1851 sym->ts.u.derived = current_ts.u.derived;
1852 sym->ts.is_c_interop = current_ts.is_c_interop;
1853 sym->ts.is_iso_c = current_ts.is_iso_c;
1854 m = MATCH_YES;
1856 /* Check to see if we have an array specification. */
1857 if (cp_as != NULL)
1859 if (sym->as != NULL)
1861 gfc_error ("Duplicate array spec for Cray pointee at %C");
1862 gfc_free_array_spec (cp_as);
1863 m = MATCH_ERROR;
1864 goto cleanup;
1866 else
1868 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1869 gfc_internal_error ("Couldn't set pointee array spec.");
1871 /* Fix the array spec. */
1872 m = gfc_mod_pointee_as (sym->as);
1873 if (m == MATCH_ERROR)
1874 goto cleanup;
1877 goto cleanup;
1879 else
1881 gfc_free_array_spec (cp_as);
1885 /* Procedure pointer as function result. */
1886 if (gfc_current_state () == COMP_FUNCTION
1887 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1888 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1889 strcpy (name, "ppr@");
1891 if (gfc_current_state () == COMP_FUNCTION
1892 && strcmp (name, gfc_current_block ()->name) == 0
1893 && gfc_current_block ()->result
1894 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1895 strcpy (name, "ppr@");
1897 /* OK, we've successfully matched the declaration. Now put the
1898 symbol in the current namespace, because it might be used in the
1899 optional initialization expression for this symbol, e.g. this is
1900 perfectly legal:
1902 integer, parameter :: i = huge(i)
1904 This is only true for parameters or variables of a basic type.
1905 For components of derived types, it is not true, so we don't
1906 create a symbol for those yet. If we fail to create the symbol,
1907 bail out. */
1908 if (gfc_current_state () != COMP_DERIVED
1909 && build_sym (name, cl, cl_deferred, &as, &var_locus) == FAILURE)
1911 m = MATCH_ERROR;
1912 goto cleanup;
1915 /* An interface body specifies all of the procedure's
1916 characteristics and these shall be consistent with those
1917 specified in the procedure definition, except that the interface
1918 may specify a procedure that is not pure if the procedure is
1919 defined to be pure(12.3.2). */
1920 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1921 && gfc_current_ns->proc_name
1922 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1923 && current_ts.u.derived->ns != gfc_current_ns)
1925 gfc_symtree *st;
1926 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.u.derived->name);
1927 if (!(current_ts.u.derived->attr.imported
1928 && st != NULL
1929 && st->n.sym == current_ts.u.derived)
1930 && !gfc_current_ns->has_import_set)
1932 gfc_error ("the type of '%s' at %C has not been declared within the "
1933 "interface", name);
1934 m = MATCH_ERROR;
1935 goto cleanup;
1939 /* In functions that have a RESULT variable defined, the function
1940 name always refers to function calls. Therefore, the name is
1941 not allowed to appear in specification statements. */
1942 if (gfc_current_state () == COMP_FUNCTION
1943 && gfc_current_block () != NULL
1944 && gfc_current_block ()->result != NULL
1945 && gfc_current_block ()->result != gfc_current_block ()
1946 && strcmp (gfc_current_block ()->name, name) == 0)
1948 gfc_error ("Function name '%s' not allowed at %C", name);
1949 m = MATCH_ERROR;
1950 goto cleanup;
1953 /* We allow old-style initializations of the form
1954 integer i /2/, j(4) /3*3, 1/
1955 (if no colon has been seen). These are different from data
1956 statements in that initializers are only allowed to apply to the
1957 variable immediately preceding, i.e.
1958 integer i, j /1, 2/
1959 is not allowed. Therefore we have to do some work manually, that
1960 could otherwise be left to the matchers for DATA statements. */
1962 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1964 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1965 "initialization at %C") == FAILURE)
1966 return MATCH_ERROR;
1968 return match_old_style_init (name);
1971 /* The double colon must be present in order to have initializers.
1972 Otherwise the statement is ambiguous with an assignment statement. */
1973 if (colon_seen)
1975 if (gfc_match (" =>") == MATCH_YES)
1977 if (!current_attr.pointer)
1979 gfc_error ("Initialization at %C isn't for a pointer variable");
1980 m = MATCH_ERROR;
1981 goto cleanup;
1984 m = match_pointer_init (&initializer, 0);
1985 if (m != MATCH_YES)
1986 goto cleanup;
1988 else if (gfc_match_char ('=') == MATCH_YES)
1990 if (current_attr.pointer)
1992 gfc_error ("Pointer initialization at %C requires '=>', "
1993 "not '='");
1994 m = MATCH_ERROR;
1995 goto cleanup;
1998 m = gfc_match_init_expr (&initializer);
1999 if (m == MATCH_NO)
2001 gfc_error ("Expected an initialization expression at %C");
2002 m = MATCH_ERROR;
2005 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2006 && gfc_state_stack->state != COMP_DERIVED)
2008 gfc_error ("Initialization of variable at %C is not allowed in "
2009 "a PURE procedure");
2010 m = MATCH_ERROR;
2013 if (m != MATCH_YES)
2014 goto cleanup;
2018 if (initializer != NULL && current_attr.allocatable
2019 && gfc_current_state () == COMP_DERIVED)
2021 gfc_error ("Initialization of allocatable component at %C is not "
2022 "allowed");
2023 m = MATCH_ERROR;
2024 goto cleanup;
2027 /* Add the initializer. Note that it is fine if initializer is
2028 NULL here, because we sometimes also need to check if a
2029 declaration *must* have an initialization expression. */
2030 if (gfc_current_state () != COMP_DERIVED)
2031 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2032 else
2034 if (current_ts.type == BT_DERIVED
2035 && !current_attr.pointer && !initializer)
2036 initializer = gfc_default_initializer (&current_ts);
2037 t = build_struct (name, cl, &initializer, &as);
2040 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
2042 cleanup:
2043 /* Free stuff up and return. */
2044 gfc_free_expr (initializer);
2045 gfc_free_array_spec (as);
2047 return m;
2051 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2052 This assumes that the byte size is equal to the kind number for
2053 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2055 match
2056 gfc_match_old_kind_spec (gfc_typespec *ts)
2058 match m;
2059 int original_kind;
2061 if (gfc_match_char ('*') != MATCH_YES)
2062 return MATCH_NO;
2064 m = gfc_match_small_literal_int (&ts->kind, NULL);
2065 if (m != MATCH_YES)
2066 return MATCH_ERROR;
2068 original_kind = ts->kind;
2070 /* Massage the kind numbers for complex types. */
2071 if (ts->type == BT_COMPLEX)
2073 if (ts->kind % 2)
2075 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2076 gfc_basic_typename (ts->type), original_kind);
2077 return MATCH_ERROR;
2079 ts->kind /= 2;
2082 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2084 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2085 gfc_basic_typename (ts->type), original_kind);
2086 return MATCH_ERROR;
2089 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2090 gfc_basic_typename (ts->type), original_kind) == FAILURE)
2091 return MATCH_ERROR;
2093 return MATCH_YES;
2097 /* Match a kind specification. Since kinds are generally optional, we
2098 usually return MATCH_NO if something goes wrong. If a "kind="
2099 string is found, then we know we have an error. */
2101 match
2102 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2104 locus where, loc;
2105 gfc_expr *e;
2106 match m, n;
2107 char c;
2108 const char *msg;
2110 m = MATCH_NO;
2111 n = MATCH_YES;
2112 e = NULL;
2114 where = loc = gfc_current_locus;
2116 if (kind_expr_only)
2117 goto kind_expr;
2119 if (gfc_match_char ('(') == MATCH_NO)
2120 return MATCH_NO;
2122 /* Also gobbles optional text. */
2123 if (gfc_match (" kind = ") == MATCH_YES)
2124 m = MATCH_ERROR;
2126 loc = gfc_current_locus;
2128 kind_expr:
2129 n = gfc_match_init_expr (&e);
2131 if (n != MATCH_YES)
2133 if (gfc_matching_function)
2135 /* The function kind expression might include use associated or
2136 imported parameters and try again after the specification
2137 expressions..... */
2138 if (gfc_match_char (')') != MATCH_YES)
2140 gfc_error ("Missing right parenthesis at %C");
2141 m = MATCH_ERROR;
2142 goto no_match;
2145 gfc_free_expr (e);
2146 gfc_undo_symbols ();
2147 return MATCH_YES;
2149 else
2151 /* ....or else, the match is real. */
2152 if (n == MATCH_NO)
2153 gfc_error ("Expected initialization expression at %C");
2154 if (n != MATCH_YES)
2155 return MATCH_ERROR;
2159 if (e->rank != 0)
2161 gfc_error ("Expected scalar initialization expression at %C");
2162 m = MATCH_ERROR;
2163 goto no_match;
2166 msg = gfc_extract_int (e, &ts->kind);
2168 if (msg != NULL)
2170 gfc_error (msg);
2171 m = MATCH_ERROR;
2172 goto no_match;
2175 /* Before throwing away the expression, let's see if we had a
2176 C interoperable kind (and store the fact). */
2177 if (e->ts.is_c_interop == 1)
2179 /* Mark this as c interoperable if being declared with one
2180 of the named constants from iso_c_binding. */
2181 ts->is_c_interop = e->ts.is_iso_c;
2182 ts->f90_type = e->ts.f90_type;
2185 gfc_free_expr (e);
2186 e = NULL;
2188 /* Ignore errors to this point, if we've gotten here. This means
2189 we ignore the m=MATCH_ERROR from above. */
2190 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2192 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2193 gfc_basic_typename (ts->type));
2194 gfc_current_locus = where;
2195 return MATCH_ERROR;
2198 /* Warn if, e.g., c_int is used for a REAL variable, but not
2199 if, e.g., c_double is used for COMPLEX as the standard
2200 explicitly says that the kind type parameter for complex and real
2201 variable is the same, i.e. c_float == c_float_complex. */
2202 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2203 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2204 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2205 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2206 "is %s", gfc_basic_typename (ts->f90_type), &where,
2207 gfc_basic_typename (ts->type));
2209 gfc_gobble_whitespace ();
2210 if ((c = gfc_next_ascii_char ()) != ')'
2211 && (ts->type != BT_CHARACTER || c != ','))
2213 if (ts->type == BT_CHARACTER)
2214 gfc_error ("Missing right parenthesis or comma at %C");
2215 else
2216 gfc_error ("Missing right parenthesis at %C");
2217 m = MATCH_ERROR;
2219 else
2220 /* All tests passed. */
2221 m = MATCH_YES;
2223 if(m == MATCH_ERROR)
2224 gfc_current_locus = where;
2226 /* Return what we know from the test(s). */
2227 return m;
2229 no_match:
2230 gfc_free_expr (e);
2231 gfc_current_locus = where;
2232 return m;
2236 static match
2237 match_char_kind (int * kind, int * is_iso_c)
2239 locus where;
2240 gfc_expr *e;
2241 match m, n;
2242 const char *msg;
2244 m = MATCH_NO;
2245 e = NULL;
2246 where = gfc_current_locus;
2248 n = gfc_match_init_expr (&e);
2250 if (n != MATCH_YES && gfc_matching_function)
2252 /* The expression might include use-associated or imported
2253 parameters and try again after the specification
2254 expressions. */
2255 gfc_free_expr (e);
2256 gfc_undo_symbols ();
2257 return MATCH_YES;
2260 if (n == MATCH_NO)
2261 gfc_error ("Expected initialization expression at %C");
2262 if (n != MATCH_YES)
2263 return MATCH_ERROR;
2265 if (e->rank != 0)
2267 gfc_error ("Expected scalar initialization expression at %C");
2268 m = MATCH_ERROR;
2269 goto no_match;
2272 msg = gfc_extract_int (e, kind);
2273 *is_iso_c = e->ts.is_iso_c;
2274 if (msg != NULL)
2276 gfc_error (msg);
2277 m = MATCH_ERROR;
2278 goto no_match;
2281 gfc_free_expr (e);
2283 /* Ignore errors to this point, if we've gotten here. This means
2284 we ignore the m=MATCH_ERROR from above. */
2285 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2287 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2288 m = MATCH_ERROR;
2290 else
2291 /* All tests passed. */
2292 m = MATCH_YES;
2294 if (m == MATCH_ERROR)
2295 gfc_current_locus = where;
2297 /* Return what we know from the test(s). */
2298 return m;
2300 no_match:
2301 gfc_free_expr (e);
2302 gfc_current_locus = where;
2303 return m;
2307 /* Match the various kind/length specifications in a CHARACTER
2308 declaration. We don't return MATCH_NO. */
2310 match
2311 gfc_match_char_spec (gfc_typespec *ts)
2313 int kind, seen_length, is_iso_c;
2314 gfc_charlen *cl;
2315 gfc_expr *len;
2316 match m;
2317 bool deferred;
2319 len = NULL;
2320 seen_length = 0;
2321 kind = 0;
2322 is_iso_c = 0;
2323 deferred = false;
2325 /* Try the old-style specification first. */
2326 old_char_selector = 0;
2328 m = match_char_length (&len, &deferred);
2329 if (m != MATCH_NO)
2331 if (m == MATCH_YES)
2332 old_char_selector = 1;
2333 seen_length = 1;
2334 goto done;
2337 m = gfc_match_char ('(');
2338 if (m != MATCH_YES)
2340 m = MATCH_YES; /* Character without length is a single char. */
2341 goto done;
2344 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2345 if (gfc_match (" kind =") == MATCH_YES)
2347 m = match_char_kind (&kind, &is_iso_c);
2349 if (m == MATCH_ERROR)
2350 goto done;
2351 if (m == MATCH_NO)
2352 goto syntax;
2354 if (gfc_match (" , len =") == MATCH_NO)
2355 goto rparen;
2357 m = char_len_param_value (&len, &deferred);
2358 if (m == MATCH_NO)
2359 goto syntax;
2360 if (m == MATCH_ERROR)
2361 goto done;
2362 seen_length = 1;
2364 goto rparen;
2367 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2368 if (gfc_match (" len =") == MATCH_YES)
2370 m = char_len_param_value (&len, &deferred);
2371 if (m == MATCH_NO)
2372 goto syntax;
2373 if (m == MATCH_ERROR)
2374 goto done;
2375 seen_length = 1;
2377 if (gfc_match_char (')') == MATCH_YES)
2378 goto done;
2380 if (gfc_match (" , kind =") != MATCH_YES)
2381 goto syntax;
2383 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2384 goto done;
2386 goto rparen;
2389 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2390 m = char_len_param_value (&len, &deferred);
2391 if (m == MATCH_NO)
2392 goto syntax;
2393 if (m == MATCH_ERROR)
2394 goto done;
2395 seen_length = 1;
2397 m = gfc_match_char (')');
2398 if (m == MATCH_YES)
2399 goto done;
2401 if (gfc_match_char (',') != MATCH_YES)
2402 goto syntax;
2404 gfc_match (" kind ="); /* Gobble optional text. */
2406 m = match_char_kind (&kind, &is_iso_c);
2407 if (m == MATCH_ERROR)
2408 goto done;
2409 if (m == MATCH_NO)
2410 goto syntax;
2412 rparen:
2413 /* Require a right-paren at this point. */
2414 m = gfc_match_char (')');
2415 if (m == MATCH_YES)
2416 goto done;
2418 syntax:
2419 gfc_error ("Syntax error in CHARACTER declaration at %C");
2420 m = MATCH_ERROR;
2421 gfc_free_expr (len);
2422 return m;
2424 done:
2425 /* Deal with character functions after USE and IMPORT statements. */
2426 if (gfc_matching_function)
2428 gfc_free_expr (len);
2429 gfc_undo_symbols ();
2430 return MATCH_YES;
2433 if (m != MATCH_YES)
2435 gfc_free_expr (len);
2436 return m;
2439 /* Do some final massaging of the length values. */
2440 cl = gfc_new_charlen (gfc_current_ns, NULL);
2442 if (seen_length == 0)
2443 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2444 else
2445 cl->length = len;
2447 ts->u.cl = cl;
2448 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2449 ts->deferred = deferred;
2451 /* We have to know if it was a c interoperable kind so we can
2452 do accurate type checking of bind(c) procs, etc. */
2453 if (kind != 0)
2454 /* Mark this as c interoperable if being declared with one
2455 of the named constants from iso_c_binding. */
2456 ts->is_c_interop = is_iso_c;
2457 else if (len != NULL)
2458 /* Here, we might have parsed something such as: character(c_char)
2459 In this case, the parsing code above grabs the c_char when
2460 looking for the length (line 1690, roughly). it's the last
2461 testcase for parsing the kind params of a character variable.
2462 However, it's not actually the length. this seems like it
2463 could be an error.
2464 To see if the user used a C interop kind, test the expr
2465 of the so called length, and see if it's C interoperable. */
2466 ts->is_c_interop = len->ts.is_iso_c;
2468 return MATCH_YES;
2472 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2473 structure to the matched specification. This is necessary for FUNCTION and
2474 IMPLICIT statements.
2476 If implicit_flag is nonzero, then we don't check for the optional
2477 kind specification. Not doing so is needed for matching an IMPLICIT
2478 statement correctly. */
2480 match
2481 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2483 char name[GFC_MAX_SYMBOL_LEN + 1];
2484 gfc_symbol *sym;
2485 match m;
2486 char c;
2487 bool seen_deferred_kind, matched_type;
2489 /* A belt and braces check that the typespec is correctly being treated
2490 as a deferred characteristic association. */
2491 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2492 && (gfc_current_block ()->result->ts.kind == -1)
2493 && (ts->kind == -1);
2494 gfc_clear_ts (ts);
2495 if (seen_deferred_kind)
2496 ts->kind = -1;
2498 /* Clear the current binding label, in case one is given. */
2499 curr_binding_label[0] = '\0';
2501 if (gfc_match (" byte") == MATCH_YES)
2503 if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2504 == FAILURE)
2505 return MATCH_ERROR;
2507 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2509 gfc_error ("BYTE type used at %C "
2510 "is not available on the target machine");
2511 return MATCH_ERROR;
2514 ts->type = BT_INTEGER;
2515 ts->kind = 1;
2516 return MATCH_YES;
2520 m = gfc_match (" type ( %n", name);
2521 matched_type = (m == MATCH_YES);
2523 if ((matched_type && strcmp ("integer", name) == 0)
2524 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2526 ts->type = BT_INTEGER;
2527 ts->kind = gfc_default_integer_kind;
2528 goto get_kind;
2531 if ((matched_type && strcmp ("character", name) == 0)
2532 || (!matched_type && gfc_match (" character") == MATCH_YES))
2534 if (matched_type
2535 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2536 "intrinsic-type-spec at %C") == FAILURE)
2537 return MATCH_ERROR;
2539 ts->type = BT_CHARACTER;
2540 if (implicit_flag == 0)
2541 m = gfc_match_char_spec (ts);
2542 else
2543 m = MATCH_YES;
2545 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2546 m = MATCH_ERROR;
2548 return m;
2551 if ((matched_type && strcmp ("real", name) == 0)
2552 || (!matched_type && gfc_match (" real") == MATCH_YES))
2554 ts->type = BT_REAL;
2555 ts->kind = gfc_default_real_kind;
2556 goto get_kind;
2559 if ((matched_type
2560 && (strcmp ("doubleprecision", name) == 0
2561 || (strcmp ("double", name) == 0
2562 && gfc_match (" precision") == MATCH_YES)))
2563 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2565 if (matched_type
2566 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2567 "intrinsic-type-spec at %C") == FAILURE)
2568 return MATCH_ERROR;
2569 if (matched_type && gfc_match_char (')') != MATCH_YES)
2570 return MATCH_ERROR;
2572 ts->type = BT_REAL;
2573 ts->kind = gfc_default_double_kind;
2574 return MATCH_YES;
2577 if ((matched_type && strcmp ("complex", name) == 0)
2578 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2580 ts->type = BT_COMPLEX;
2581 ts->kind = gfc_default_complex_kind;
2582 goto get_kind;
2585 if ((matched_type
2586 && (strcmp ("doublecomplex", name) == 0
2587 || (strcmp ("double", name) == 0
2588 && gfc_match (" complex") == MATCH_YES)))
2589 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2591 if (gfc_notify_std (GFC_STD_GNU, "Extension: DOUBLE COMPLEX at %C")
2592 == FAILURE)
2593 return MATCH_ERROR;
2595 if (matched_type
2596 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2597 "intrinsic-type-spec at %C") == FAILURE)
2598 return MATCH_ERROR;
2600 if (matched_type && gfc_match_char (')') != MATCH_YES)
2601 return MATCH_ERROR;
2603 ts->type = BT_COMPLEX;
2604 ts->kind = gfc_default_double_kind;
2605 return MATCH_YES;
2608 if ((matched_type && strcmp ("logical", name) == 0)
2609 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2611 ts->type = BT_LOGICAL;
2612 ts->kind = gfc_default_logical_kind;
2613 goto get_kind;
2616 if (matched_type)
2617 m = gfc_match_char (')');
2619 if (m == MATCH_YES)
2620 ts->type = BT_DERIVED;
2621 else
2623 /* Match CLASS declarations. */
2624 m = gfc_match (" class ( * )");
2625 if (m == MATCH_ERROR)
2626 return MATCH_ERROR;
2627 else if (m == MATCH_YES)
2629 gfc_fatal_error ("Unlimited polymorphism at %C not yet supported");
2630 return MATCH_ERROR;
2633 m = gfc_match (" class ( %n )", name);
2634 if (m != MATCH_YES)
2635 return m;
2636 ts->type = BT_CLASS;
2638 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2639 == FAILURE)
2640 return MATCH_ERROR;
2643 /* Defer association of the derived type until the end of the
2644 specification block. However, if the derived type can be
2645 found, add it to the typespec. */
2646 if (gfc_matching_function)
2648 ts->u.derived = NULL;
2649 if (gfc_current_state () != COMP_INTERFACE
2650 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2651 ts->u.derived = sym;
2652 return MATCH_YES;
2655 /* Search for the name but allow the components to be defined later. If
2656 type = -1, this typespec has been seen in a function declaration but
2657 the type could not be accessed at that point. */
2658 sym = NULL;
2659 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2661 gfc_error ("Type name '%s' at %C is ambiguous", name);
2662 return MATCH_ERROR;
2664 else if (ts->kind == -1)
2666 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2667 || gfc_current_ns->has_import_set;
2668 if (gfc_find_symbol (name, NULL, iface, &sym))
2670 gfc_error ("Type name '%s' at %C is ambiguous", name);
2671 return MATCH_ERROR;
2674 ts->kind = 0;
2675 if (sym == NULL)
2676 return MATCH_NO;
2679 if (sym->attr.flavor != FL_DERIVED
2680 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2681 return MATCH_ERROR;
2683 gfc_set_sym_referenced (sym);
2684 ts->u.derived = sym;
2686 return MATCH_YES;
2688 get_kind:
2689 if (matched_type
2690 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2691 "intrinsic-type-spec at %C") == FAILURE)
2692 return MATCH_ERROR;
2694 /* For all types except double, derived and character, look for an
2695 optional kind specifier. MATCH_NO is actually OK at this point. */
2696 if (implicit_flag == 1)
2698 if (matched_type && gfc_match_char (')') != MATCH_YES)
2699 return MATCH_ERROR;
2701 return MATCH_YES;
2704 if (gfc_current_form == FORM_FREE)
2706 c = gfc_peek_ascii_char ();
2707 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2708 && c != ':' && c != ',')
2710 if (matched_type && c == ')')
2712 gfc_next_ascii_char ();
2713 return MATCH_YES;
2715 return MATCH_NO;
2719 m = gfc_match_kind_spec (ts, false);
2720 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2721 m = gfc_match_old_kind_spec (ts);
2723 if (matched_type && gfc_match_char (')') != MATCH_YES)
2724 return MATCH_ERROR;
2726 /* Defer association of the KIND expression of function results
2727 until after USE and IMPORT statements. */
2728 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2729 || gfc_matching_function)
2730 return MATCH_YES;
2732 if (m == MATCH_NO)
2733 m = MATCH_YES; /* No kind specifier found. */
2735 return m;
2739 /* Match an IMPLICIT NONE statement. Actually, this statement is
2740 already matched in parse.c, or we would not end up here in the
2741 first place. So the only thing we need to check, is if there is
2742 trailing garbage. If not, the match is successful. */
2744 match
2745 gfc_match_implicit_none (void)
2747 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2751 /* Match the letter range(s) of an IMPLICIT statement. */
2753 static match
2754 match_implicit_range (void)
2756 char c, c1, c2;
2757 int inner;
2758 locus cur_loc;
2760 cur_loc = gfc_current_locus;
2762 gfc_gobble_whitespace ();
2763 c = gfc_next_ascii_char ();
2764 if (c != '(')
2766 gfc_error ("Missing character range in IMPLICIT at %C");
2767 goto bad;
2770 inner = 1;
2771 while (inner)
2773 gfc_gobble_whitespace ();
2774 c1 = gfc_next_ascii_char ();
2775 if (!ISALPHA (c1))
2776 goto bad;
2778 gfc_gobble_whitespace ();
2779 c = gfc_next_ascii_char ();
2781 switch (c)
2783 case ')':
2784 inner = 0; /* Fall through. */
2786 case ',':
2787 c2 = c1;
2788 break;
2790 case '-':
2791 gfc_gobble_whitespace ();
2792 c2 = gfc_next_ascii_char ();
2793 if (!ISALPHA (c2))
2794 goto bad;
2796 gfc_gobble_whitespace ();
2797 c = gfc_next_ascii_char ();
2799 if ((c != ',') && (c != ')'))
2800 goto bad;
2801 if (c == ')')
2802 inner = 0;
2804 break;
2806 default:
2807 goto bad;
2810 if (c1 > c2)
2812 gfc_error ("Letters must be in alphabetic order in "
2813 "IMPLICIT statement at %C");
2814 goto bad;
2817 /* See if we can add the newly matched range to the pending
2818 implicits from this IMPLICIT statement. We do not check for
2819 conflicts with whatever earlier IMPLICIT statements may have
2820 set. This is done when we've successfully finished matching
2821 the current one. */
2822 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2823 goto bad;
2826 return MATCH_YES;
2828 bad:
2829 gfc_syntax_error (ST_IMPLICIT);
2831 gfc_current_locus = cur_loc;
2832 return MATCH_ERROR;
2836 /* Match an IMPLICIT statement, storing the types for
2837 gfc_set_implicit() if the statement is accepted by the parser.
2838 There is a strange looking, but legal syntactic construction
2839 possible. It looks like:
2841 IMPLICIT INTEGER (a-b) (c-d)
2843 This is legal if "a-b" is a constant expression that happens to
2844 equal one of the legal kinds for integers. The real problem
2845 happens with an implicit specification that looks like:
2847 IMPLICIT INTEGER (a-b)
2849 In this case, a typespec matcher that is "greedy" (as most of the
2850 matchers are) gobbles the character range as a kindspec, leaving
2851 nothing left. We therefore have to go a bit more slowly in the
2852 matching process by inhibiting the kindspec checking during
2853 typespec matching and checking for a kind later. */
2855 match
2856 gfc_match_implicit (void)
2858 gfc_typespec ts;
2859 locus cur_loc;
2860 char c;
2861 match m;
2863 gfc_clear_ts (&ts);
2865 /* We don't allow empty implicit statements. */
2866 if (gfc_match_eos () == MATCH_YES)
2868 gfc_error ("Empty IMPLICIT statement at %C");
2869 return MATCH_ERROR;
2874 /* First cleanup. */
2875 gfc_clear_new_implicit ();
2877 /* A basic type is mandatory here. */
2878 m = gfc_match_decl_type_spec (&ts, 1);
2879 if (m == MATCH_ERROR)
2880 goto error;
2881 if (m == MATCH_NO)
2882 goto syntax;
2884 cur_loc = gfc_current_locus;
2885 m = match_implicit_range ();
2887 if (m == MATCH_YES)
2889 /* We may have <TYPE> (<RANGE>). */
2890 gfc_gobble_whitespace ();
2891 c = gfc_next_ascii_char ();
2892 if ((c == '\n') || (c == ','))
2894 /* Check for CHARACTER with no length parameter. */
2895 if (ts.type == BT_CHARACTER && !ts.u.cl)
2897 ts.kind = gfc_default_character_kind;
2898 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2899 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
2900 NULL, 1);
2903 /* Record the Successful match. */
2904 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2905 return MATCH_ERROR;
2906 continue;
2909 gfc_current_locus = cur_loc;
2912 /* Discard the (incorrectly) matched range. */
2913 gfc_clear_new_implicit ();
2915 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2916 if (ts.type == BT_CHARACTER)
2917 m = gfc_match_char_spec (&ts);
2918 else
2920 m = gfc_match_kind_spec (&ts, false);
2921 if (m == MATCH_NO)
2923 m = gfc_match_old_kind_spec (&ts);
2924 if (m == MATCH_ERROR)
2925 goto error;
2926 if (m == MATCH_NO)
2927 goto syntax;
2930 if (m == MATCH_ERROR)
2931 goto error;
2933 m = match_implicit_range ();
2934 if (m == MATCH_ERROR)
2935 goto error;
2936 if (m == MATCH_NO)
2937 goto syntax;
2939 gfc_gobble_whitespace ();
2940 c = gfc_next_ascii_char ();
2941 if ((c != '\n') && (c != ','))
2942 goto syntax;
2944 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2945 return MATCH_ERROR;
2947 while (c == ',');
2949 return MATCH_YES;
2951 syntax:
2952 gfc_syntax_error (ST_IMPLICIT);
2954 error:
2955 return MATCH_ERROR;
2959 match
2960 gfc_match_import (void)
2962 char name[GFC_MAX_SYMBOL_LEN + 1];
2963 match m;
2964 gfc_symbol *sym;
2965 gfc_symtree *st;
2967 if (gfc_current_ns->proc_name == NULL
2968 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2970 gfc_error ("IMPORT statement at %C only permitted in "
2971 "an INTERFACE body");
2972 return MATCH_ERROR;
2975 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2976 == FAILURE)
2977 return MATCH_ERROR;
2979 if (gfc_match_eos () == MATCH_YES)
2981 /* All host variables should be imported. */
2982 gfc_current_ns->has_import_set = 1;
2983 return MATCH_YES;
2986 if (gfc_match (" ::") == MATCH_YES)
2988 if (gfc_match_eos () == MATCH_YES)
2990 gfc_error ("Expecting list of named entities at %C");
2991 return MATCH_ERROR;
2995 for(;;)
2997 sym = NULL;
2998 m = gfc_match (" %n", name);
2999 switch (m)
3001 case MATCH_YES:
3002 if (gfc_current_ns->parent != NULL
3003 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3005 gfc_error ("Type name '%s' at %C is ambiguous", name);
3006 return MATCH_ERROR;
3008 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3009 && gfc_find_symbol (name,
3010 gfc_current_ns->proc_name->ns->parent,
3011 1, &sym))
3013 gfc_error ("Type name '%s' at %C is ambiguous", name);
3014 return MATCH_ERROR;
3017 if (sym == NULL)
3019 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3020 "at %C - does not exist.", name);
3021 return MATCH_ERROR;
3024 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
3026 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3027 "at %C.", name);
3028 goto next_item;
3031 st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
3032 st->n.sym = sym;
3033 sym->refs++;
3034 sym->attr.imported = 1;
3036 goto next_item;
3038 case MATCH_NO:
3039 break;
3041 case MATCH_ERROR:
3042 return MATCH_ERROR;
3045 next_item:
3046 if (gfc_match_eos () == MATCH_YES)
3047 break;
3048 if (gfc_match_char (',') != MATCH_YES)
3049 goto syntax;
3052 return MATCH_YES;
3054 syntax:
3055 gfc_error ("Syntax error in IMPORT statement at %C");
3056 return MATCH_ERROR;
3060 /* A minimal implementation of gfc_match without whitespace, escape
3061 characters or variable arguments. Returns true if the next
3062 characters match the TARGET template exactly. */
3064 static bool
3065 match_string_p (const char *target)
3067 const char *p;
3069 for (p = target; *p; p++)
3070 if ((char) gfc_next_ascii_char () != *p)
3071 return false;
3072 return true;
3075 /* Matches an attribute specification including array specs. If
3076 successful, leaves the variables current_attr and current_as
3077 holding the specification. Also sets the colon_seen variable for
3078 later use by matchers associated with initializations.
3080 This subroutine is a little tricky in the sense that we don't know
3081 if we really have an attr-spec until we hit the double colon.
3082 Until that time, we can only return MATCH_NO. This forces us to
3083 check for duplicate specification at this level. */
3085 static match
3086 match_attr_spec (void)
3088 /* Modifiers that can exist in a type statement. */
3089 typedef enum
3090 { GFC_DECL_BEGIN = 0,
3091 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3092 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3093 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3094 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3095 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3096 DECL_NONE, GFC_DECL_END /* Sentinel */
3098 decl_types;
3100 /* GFC_DECL_END is the sentinel, index starts at 0. */
3101 #define NUM_DECL GFC_DECL_END
3103 locus start, seen_at[NUM_DECL];
3104 int seen[NUM_DECL];
3105 unsigned int d;
3106 const char *attr;
3107 match m;
3108 gfc_try t;
3110 gfc_clear_attr (&current_attr);
3111 start = gfc_current_locus;
3113 current_as = NULL;
3114 colon_seen = 0;
3116 /* See if we get all of the keywords up to the final double colon. */
3117 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3118 seen[d] = 0;
3120 for (;;)
3122 char ch;
3124 d = DECL_NONE;
3125 gfc_gobble_whitespace ();
3127 ch = gfc_next_ascii_char ();
3128 if (ch == ':')
3130 /* This is the successful exit condition for the loop. */
3131 if (gfc_next_ascii_char () == ':')
3132 break;
3134 else if (ch == ',')
3136 gfc_gobble_whitespace ();
3137 switch (gfc_peek_ascii_char ())
3139 case 'a':
3140 gfc_next_ascii_char ();
3141 switch (gfc_next_ascii_char ())
3143 case 'l':
3144 if (match_string_p ("locatable"))
3146 /* Matched "allocatable". */
3147 d = DECL_ALLOCATABLE;
3149 break;
3151 case 's':
3152 if (match_string_p ("ynchronous"))
3154 /* Matched "asynchronous". */
3155 d = DECL_ASYNCHRONOUS;
3157 break;
3159 break;
3161 case 'b':
3162 /* Try and match the bind(c). */
3163 m = gfc_match_bind_c (NULL, true);
3164 if (m == MATCH_YES)
3165 d = DECL_IS_BIND_C;
3166 else if (m == MATCH_ERROR)
3167 goto cleanup;
3168 break;
3170 case 'c':
3171 gfc_next_ascii_char ();
3172 if ('o' != gfc_next_ascii_char ())
3173 break;
3174 switch (gfc_next_ascii_char ())
3176 case 'd':
3177 if (match_string_p ("imension"))
3179 d = DECL_CODIMENSION;
3180 break;
3182 case 'n':
3183 if (match_string_p ("tiguous"))
3185 d = DECL_CONTIGUOUS;
3186 break;
3189 break;
3191 case 'd':
3192 if (match_string_p ("dimension"))
3193 d = DECL_DIMENSION;
3194 break;
3196 case 'e':
3197 if (match_string_p ("external"))
3198 d = DECL_EXTERNAL;
3199 break;
3201 case 'i':
3202 if (match_string_p ("int"))
3204 ch = gfc_next_ascii_char ();
3205 if (ch == 'e')
3207 if (match_string_p ("nt"))
3209 /* Matched "intent". */
3210 /* TODO: Call match_intent_spec from here. */
3211 if (gfc_match (" ( in out )") == MATCH_YES)
3212 d = DECL_INOUT;
3213 else if (gfc_match (" ( in )") == MATCH_YES)
3214 d = DECL_IN;
3215 else if (gfc_match (" ( out )") == MATCH_YES)
3216 d = DECL_OUT;
3219 else if (ch == 'r')
3221 if (match_string_p ("insic"))
3223 /* Matched "intrinsic". */
3224 d = DECL_INTRINSIC;
3228 break;
3230 case 'o':
3231 if (match_string_p ("optional"))
3232 d = DECL_OPTIONAL;
3233 break;
3235 case 'p':
3236 gfc_next_ascii_char ();
3237 switch (gfc_next_ascii_char ())
3239 case 'a':
3240 if (match_string_p ("rameter"))
3242 /* Matched "parameter". */
3243 d = DECL_PARAMETER;
3245 break;
3247 case 'o':
3248 if (match_string_p ("inter"))
3250 /* Matched "pointer". */
3251 d = DECL_POINTER;
3253 break;
3255 case 'r':
3256 ch = gfc_next_ascii_char ();
3257 if (ch == 'i')
3259 if (match_string_p ("vate"))
3261 /* Matched "private". */
3262 d = DECL_PRIVATE;
3265 else if (ch == 'o')
3267 if (match_string_p ("tected"))
3269 /* Matched "protected". */
3270 d = DECL_PROTECTED;
3273 break;
3275 case 'u':
3276 if (match_string_p ("blic"))
3278 /* Matched "public". */
3279 d = DECL_PUBLIC;
3281 break;
3283 break;
3285 case 's':
3286 if (match_string_p ("save"))
3287 d = DECL_SAVE;
3288 break;
3290 case 't':
3291 if (match_string_p ("target"))
3292 d = DECL_TARGET;
3293 break;
3295 case 'v':
3296 gfc_next_ascii_char ();
3297 ch = gfc_next_ascii_char ();
3298 if (ch == 'a')
3300 if (match_string_p ("lue"))
3302 /* Matched "value". */
3303 d = DECL_VALUE;
3306 else if (ch == 'o')
3308 if (match_string_p ("latile"))
3310 /* Matched "volatile". */
3311 d = DECL_VOLATILE;
3314 break;
3318 /* No double colon and no recognizable decl_type, so assume that
3319 we've been looking at something else the whole time. */
3320 if (d == DECL_NONE)
3322 m = MATCH_NO;
3323 goto cleanup;
3326 /* Check to make sure any parens are paired up correctly. */
3327 if (gfc_match_parens () == MATCH_ERROR)
3329 m = MATCH_ERROR;
3330 goto cleanup;
3333 seen[d]++;
3334 seen_at[d] = gfc_current_locus;
3336 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3338 gfc_array_spec *as = NULL;
3340 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3341 d == DECL_CODIMENSION);
3343 if (current_as == NULL)
3344 current_as = as;
3345 else if (m == MATCH_YES)
3347 merge_array_spec (as, current_as, false);
3348 free (as);
3351 if (m == MATCH_NO)
3353 if (d == DECL_CODIMENSION)
3354 gfc_error ("Missing codimension specification at %C");
3355 else
3356 gfc_error ("Missing dimension specification at %C");
3357 m = MATCH_ERROR;
3360 if (m == MATCH_ERROR)
3361 goto cleanup;
3365 /* Since we've seen a double colon, we have to be looking at an
3366 attr-spec. This means that we can now issue errors. */
3367 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3368 if (seen[d] > 1)
3370 switch (d)
3372 case DECL_ALLOCATABLE:
3373 attr = "ALLOCATABLE";
3374 break;
3375 case DECL_ASYNCHRONOUS:
3376 attr = "ASYNCHRONOUS";
3377 break;
3378 case DECL_CODIMENSION:
3379 attr = "CODIMENSION";
3380 break;
3381 case DECL_CONTIGUOUS:
3382 attr = "CONTIGUOUS";
3383 break;
3384 case DECL_DIMENSION:
3385 attr = "DIMENSION";
3386 break;
3387 case DECL_EXTERNAL:
3388 attr = "EXTERNAL";
3389 break;
3390 case DECL_IN:
3391 attr = "INTENT (IN)";
3392 break;
3393 case DECL_OUT:
3394 attr = "INTENT (OUT)";
3395 break;
3396 case DECL_INOUT:
3397 attr = "INTENT (IN OUT)";
3398 break;
3399 case DECL_INTRINSIC:
3400 attr = "INTRINSIC";
3401 break;
3402 case DECL_OPTIONAL:
3403 attr = "OPTIONAL";
3404 break;
3405 case DECL_PARAMETER:
3406 attr = "PARAMETER";
3407 break;
3408 case DECL_POINTER:
3409 attr = "POINTER";
3410 break;
3411 case DECL_PROTECTED:
3412 attr = "PROTECTED";
3413 break;
3414 case DECL_PRIVATE:
3415 attr = "PRIVATE";
3416 break;
3417 case DECL_PUBLIC:
3418 attr = "PUBLIC";
3419 break;
3420 case DECL_SAVE:
3421 attr = "SAVE";
3422 break;
3423 case DECL_TARGET:
3424 attr = "TARGET";
3425 break;
3426 case DECL_IS_BIND_C:
3427 attr = "IS_BIND_C";
3428 break;
3429 case DECL_VALUE:
3430 attr = "VALUE";
3431 break;
3432 case DECL_VOLATILE:
3433 attr = "VOLATILE";
3434 break;
3435 default:
3436 attr = NULL; /* This shouldn't happen. */
3439 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3440 m = MATCH_ERROR;
3441 goto cleanup;
3444 /* Now that we've dealt with duplicate attributes, add the attributes
3445 to the current attribute. */
3446 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3448 if (seen[d] == 0)
3449 continue;
3451 if (gfc_current_state () == COMP_DERIVED
3452 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3453 && d != DECL_POINTER && d != DECL_PRIVATE
3454 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3456 if (d == DECL_ALLOCATABLE)
3458 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3459 "attribute at %C in a TYPE definition")
3460 == FAILURE)
3462 m = MATCH_ERROR;
3463 goto cleanup;
3466 else
3468 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3469 &seen_at[d]);
3470 m = MATCH_ERROR;
3471 goto cleanup;
3475 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3476 && gfc_current_state () != COMP_MODULE)
3478 if (d == DECL_PRIVATE)
3479 attr = "PRIVATE";
3480 else
3481 attr = "PUBLIC";
3482 if (gfc_current_state () == COMP_DERIVED
3483 && gfc_state_stack->previous
3484 && gfc_state_stack->previous->state == COMP_MODULE)
3486 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3487 "at %L in a TYPE definition", attr,
3488 &seen_at[d])
3489 == FAILURE)
3491 m = MATCH_ERROR;
3492 goto cleanup;
3495 else
3497 gfc_error ("%s attribute at %L is not allowed outside of the "
3498 "specification part of a module", attr, &seen_at[d]);
3499 m = MATCH_ERROR;
3500 goto cleanup;
3504 switch (d)
3506 case DECL_ALLOCATABLE:
3507 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3508 break;
3510 case DECL_ASYNCHRONOUS:
3511 if (gfc_notify_std (GFC_STD_F2003,
3512 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3513 == FAILURE)
3514 t = FAILURE;
3515 else
3516 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3517 break;
3519 case DECL_CODIMENSION:
3520 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3521 break;
3523 case DECL_CONTIGUOUS:
3524 if (gfc_notify_std (GFC_STD_F2008,
3525 "Fortran 2008: CONTIGUOUS attribute at %C")
3526 == FAILURE)
3527 t = FAILURE;
3528 else
3529 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3530 break;
3532 case DECL_DIMENSION:
3533 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3534 break;
3536 case DECL_EXTERNAL:
3537 t = gfc_add_external (&current_attr, &seen_at[d]);
3538 break;
3540 case DECL_IN:
3541 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3542 break;
3544 case DECL_OUT:
3545 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3546 break;
3548 case DECL_INOUT:
3549 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3550 break;
3552 case DECL_INTRINSIC:
3553 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3554 break;
3556 case DECL_OPTIONAL:
3557 t = gfc_add_optional (&current_attr, &seen_at[d]);
3558 break;
3560 case DECL_PARAMETER:
3561 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3562 break;
3564 case DECL_POINTER:
3565 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3566 break;
3568 case DECL_PROTECTED:
3569 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3571 gfc_error ("PROTECTED at %C only allowed in specification "
3572 "part of a module");
3573 t = FAILURE;
3574 break;
3577 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3578 "attribute at %C")
3579 == FAILURE)
3580 t = FAILURE;
3581 else
3582 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3583 break;
3585 case DECL_PRIVATE:
3586 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3587 &seen_at[d]);
3588 break;
3590 case DECL_PUBLIC:
3591 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3592 &seen_at[d]);
3593 break;
3595 case DECL_SAVE:
3596 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3597 break;
3599 case DECL_TARGET:
3600 t = gfc_add_target (&current_attr, &seen_at[d]);
3601 break;
3603 case DECL_IS_BIND_C:
3604 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3605 break;
3607 case DECL_VALUE:
3608 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3609 "at %C")
3610 == FAILURE)
3611 t = FAILURE;
3612 else
3613 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3614 break;
3616 case DECL_VOLATILE:
3617 if (gfc_notify_std (GFC_STD_F2003,
3618 "Fortran 2003: VOLATILE attribute at %C")
3619 == FAILURE)
3620 t = FAILURE;
3621 else
3622 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3623 break;
3625 default:
3626 gfc_internal_error ("match_attr_spec(): Bad attribute");
3629 if (t == FAILURE)
3631 m = MATCH_ERROR;
3632 goto cleanup;
3636 /* Module variables implicitly have the SAVE attribute. */
3637 if (gfc_current_state () == COMP_MODULE && !current_attr.save)
3638 current_attr.save = SAVE_IMPLICIT;
3640 colon_seen = 1;
3641 return MATCH_YES;
3643 cleanup:
3644 gfc_current_locus = start;
3645 gfc_free_array_spec (current_as);
3646 current_as = NULL;
3647 return m;
3651 /* Set the binding label, dest_label, either with the binding label
3652 stored in the given gfc_typespec, ts, or if none was provided, it
3653 will be the symbol name in all lower case, as required by the draft
3654 (J3/04-007, section 15.4.1). If a binding label was given and
3655 there is more than one argument (num_idents), it is an error. */
3657 gfc_try
3658 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3660 if (num_idents > 1 && has_name_equals)
3662 gfc_error ("Multiple identifiers provided with "
3663 "single NAME= specifier at %C");
3664 return FAILURE;
3667 if (curr_binding_label[0] != '\0')
3669 /* Binding label given; store in temp holder til have sym. */
3670 strcpy (dest_label, curr_binding_label);
3672 else
3674 /* No binding label given, and the NAME= specifier did not exist,
3675 which means there was no NAME="". */
3676 if (sym_name != NULL && has_name_equals == 0)
3677 strcpy (dest_label, sym_name);
3680 return SUCCESS;
3684 /* Set the status of the given common block as being BIND(C) or not,
3685 depending on the given parameter, is_bind_c. */
3687 void
3688 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3690 com_block->is_bind_c = is_bind_c;
3691 return;
3695 /* Verify that the given gfc_typespec is for a C interoperable type. */
3697 gfc_try
3698 verify_c_interop (gfc_typespec *ts)
3700 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3701 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3702 ? SUCCESS : FAILURE;
3703 else if (ts->is_c_interop != 1)
3704 return FAILURE;
3706 return SUCCESS;
3710 /* Verify that the variables of a given common block, which has been
3711 defined with the attribute specifier bind(c), to be of a C
3712 interoperable type. Errors will be reported here, if
3713 encountered. */
3715 gfc_try
3716 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3718 gfc_symbol *curr_sym = NULL;
3719 gfc_try retval = SUCCESS;
3721 curr_sym = com_block->head;
3723 /* Make sure we have at least one symbol. */
3724 if (curr_sym == NULL)
3725 return retval;
3727 /* Here we know we have a symbol, so we'll execute this loop
3728 at least once. */
3731 /* The second to last param, 1, says this is in a common block. */
3732 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3733 curr_sym = curr_sym->common_next;
3734 } while (curr_sym != NULL);
3736 return retval;
3740 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3741 an appropriate error message is reported. */
3743 gfc_try
3744 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3745 int is_in_common, gfc_common_head *com_block)
3747 bool bind_c_function = false;
3748 gfc_try retval = SUCCESS;
3750 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3751 bind_c_function = true;
3753 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3755 tmp_sym = tmp_sym->result;
3756 /* Make sure it wasn't an implicitly typed result. */
3757 if (tmp_sym->attr.implicit_type)
3759 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3760 "%L may not be C interoperable", tmp_sym->name,
3761 &tmp_sym->declared_at);
3762 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3763 /* Mark it as C interoperable to prevent duplicate warnings. */
3764 tmp_sym->ts.is_c_interop = 1;
3765 tmp_sym->attr.is_c_interop = 1;
3769 /* Here, we know we have the bind(c) attribute, so if we have
3770 enough type info, then verify that it's a C interop kind.
3771 The info could be in the symbol already, or possibly still in
3772 the given ts (current_ts), so look in both. */
3773 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3775 if (verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3777 /* See if we're dealing with a sym in a common block or not. */
3778 if (is_in_common == 1)
3780 gfc_warning ("Variable '%s' in common block '%s' at %L "
3781 "may not be a C interoperable "
3782 "kind though common block '%s' is BIND(C)",
3783 tmp_sym->name, com_block->name,
3784 &(tmp_sym->declared_at), com_block->name);
3786 else
3788 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3789 gfc_error ("Type declaration '%s' at %L is not C "
3790 "interoperable but it is BIND(C)",
3791 tmp_sym->name, &(tmp_sym->declared_at));
3792 else
3793 gfc_warning ("Variable '%s' at %L "
3794 "may not be a C interoperable "
3795 "kind but it is bind(c)",
3796 tmp_sym->name, &(tmp_sym->declared_at));
3800 /* Variables declared w/in a common block can't be bind(c)
3801 since there's no way for C to see these variables, so there's
3802 semantically no reason for the attribute. */
3803 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3805 gfc_error ("Variable '%s' in common block '%s' at "
3806 "%L cannot be declared with BIND(C) "
3807 "since it is not a global",
3808 tmp_sym->name, com_block->name,
3809 &(tmp_sym->declared_at));
3810 retval = FAILURE;
3813 /* Scalar variables that are bind(c) can not have the pointer
3814 or allocatable attributes. */
3815 if (tmp_sym->attr.is_bind_c == 1)
3817 if (tmp_sym->attr.pointer == 1)
3819 gfc_error ("Variable '%s' at %L cannot have both the "
3820 "POINTER and BIND(C) attributes",
3821 tmp_sym->name, &(tmp_sym->declared_at));
3822 retval = FAILURE;
3825 if (tmp_sym->attr.allocatable == 1)
3827 gfc_error ("Variable '%s' at %L cannot have both the "
3828 "ALLOCATABLE and BIND(C) attributes",
3829 tmp_sym->name, &(tmp_sym->declared_at));
3830 retval = FAILURE;
3835 /* If it is a BIND(C) function, make sure the return value is a
3836 scalar value. The previous tests in this function made sure
3837 the type is interoperable. */
3838 if (bind_c_function && tmp_sym->as != NULL)
3839 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3840 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3842 /* BIND(C) functions can not return a character string. */
3843 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3844 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
3845 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
3846 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
3847 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3848 "be a character string", tmp_sym->name,
3849 &(tmp_sym->declared_at));
3852 /* See if the symbol has been marked as private. If it has, make sure
3853 there is no binding label and warn the user if there is one. */
3854 if (tmp_sym->attr.access == ACCESS_PRIVATE
3855 && tmp_sym->binding_label[0] != '\0')
3856 /* Use gfc_warning_now because we won't say that the symbol fails
3857 just because of this. */
3858 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3859 "given the binding label '%s'", tmp_sym->name,
3860 &(tmp_sym->declared_at), tmp_sym->binding_label);
3862 return retval;
3866 /* Set the appropriate fields for a symbol that's been declared as
3867 BIND(C) (the is_bind_c flag and the binding label), and verify that
3868 the type is C interoperable. Errors are reported by the functions
3869 used to set/test these fields. */
3871 gfc_try
3872 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3874 gfc_try retval = SUCCESS;
3876 /* TODO: Do we need to make sure the vars aren't marked private? */
3878 /* Set the is_bind_c bit in symbol_attribute. */
3879 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3881 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3882 num_idents) != SUCCESS)
3883 return FAILURE;
3885 return retval;
3889 /* Set the fields marking the given common block as BIND(C), including
3890 a binding label, and report any errors encountered. */
3892 gfc_try
3893 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3895 gfc_try retval = SUCCESS;
3897 /* destLabel, common name, typespec (which may have binding label). */
3898 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3899 != SUCCESS)
3900 return FAILURE;
3902 /* Set the given common block (com_block) to being bind(c) (1). */
3903 set_com_block_bind_c (com_block, 1);
3905 return retval;
3909 /* Retrieve the list of one or more identifiers that the given bind(c)
3910 attribute applies to. */
3912 gfc_try
3913 get_bind_c_idents (void)
3915 char name[GFC_MAX_SYMBOL_LEN + 1];
3916 int num_idents = 0;
3917 gfc_symbol *tmp_sym = NULL;
3918 match found_id;
3919 gfc_common_head *com_block = NULL;
3921 if (gfc_match_name (name) == MATCH_YES)
3923 found_id = MATCH_YES;
3924 gfc_get_ha_symbol (name, &tmp_sym);
3926 else if (match_common_name (name) == MATCH_YES)
3928 found_id = MATCH_YES;
3929 com_block = gfc_get_common (name, 0);
3931 else
3933 gfc_error ("Need either entity or common block name for "
3934 "attribute specification statement at %C");
3935 return FAILURE;
3938 /* Save the current identifier and look for more. */
3941 /* Increment the number of identifiers found for this spec stmt. */
3942 num_idents++;
3944 /* Make sure we have a sym or com block, and verify that it can
3945 be bind(c). Set the appropriate field(s) and look for more
3946 identifiers. */
3947 if (tmp_sym != NULL || com_block != NULL)
3949 if (tmp_sym != NULL)
3951 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3952 != SUCCESS)
3953 return FAILURE;
3955 else
3957 if (set_verify_bind_c_com_block(com_block, num_idents)
3958 != SUCCESS)
3959 return FAILURE;
3962 /* Look to see if we have another identifier. */
3963 tmp_sym = NULL;
3964 if (gfc_match_eos () == MATCH_YES)
3965 found_id = MATCH_NO;
3966 else if (gfc_match_char (',') != MATCH_YES)
3967 found_id = MATCH_NO;
3968 else if (gfc_match_name (name) == MATCH_YES)
3970 found_id = MATCH_YES;
3971 gfc_get_ha_symbol (name, &tmp_sym);
3973 else if (match_common_name (name) == MATCH_YES)
3975 found_id = MATCH_YES;
3976 com_block = gfc_get_common (name, 0);
3978 else
3980 gfc_error ("Missing entity or common block name for "
3981 "attribute specification statement at %C");
3982 return FAILURE;
3985 else
3987 gfc_internal_error ("Missing symbol");
3989 } while (found_id == MATCH_YES);
3991 /* if we get here we were successful */
3992 return SUCCESS;
3996 /* Try and match a BIND(C) attribute specification statement. */
3998 match
3999 gfc_match_bind_c_stmt (void)
4001 match found_match = MATCH_NO;
4002 gfc_typespec *ts;
4004 ts = &current_ts;
4006 /* This may not be necessary. */
4007 gfc_clear_ts (ts);
4008 /* Clear the temporary binding label holder. */
4009 curr_binding_label[0] = '\0';
4011 /* Look for the bind(c). */
4012 found_match = gfc_match_bind_c (NULL, true);
4014 if (found_match == MATCH_YES)
4016 /* Look for the :: now, but it is not required. */
4017 gfc_match (" :: ");
4019 /* Get the identifier(s) that needs to be updated. This may need to
4020 change to hand the flag(s) for the attr specified so all identifiers
4021 found can have all appropriate parts updated (assuming that the same
4022 spec stmt can have multiple attrs, such as both bind(c) and
4023 allocatable...). */
4024 if (get_bind_c_idents () != SUCCESS)
4025 /* Error message should have printed already. */
4026 return MATCH_ERROR;
4029 return found_match;
4033 /* Match a data declaration statement. */
4035 match
4036 gfc_match_data_decl (void)
4038 gfc_symbol *sym;
4039 match m;
4040 int elem;
4042 num_idents_on_line = 0;
4044 m = gfc_match_decl_type_spec (&current_ts, 0);
4045 if (m != MATCH_YES)
4046 return m;
4048 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4049 && gfc_current_state () != COMP_DERIVED)
4051 sym = gfc_use_derived (current_ts.u.derived);
4053 if (sym == NULL)
4055 m = MATCH_ERROR;
4056 goto cleanup;
4059 current_ts.u.derived = sym;
4062 m = match_attr_spec ();
4063 if (m == MATCH_ERROR)
4065 m = MATCH_NO;
4066 goto cleanup;
4069 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4070 && current_ts.u.derived->components == NULL
4071 && !current_ts.u.derived->attr.zero_comp)
4074 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4075 goto ok;
4077 gfc_find_symbol (current_ts.u.derived->name,
4078 current_ts.u.derived->ns->parent, 1, &sym);
4080 /* Any symbol that we find had better be a type definition
4081 which has its components defined. */
4082 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4083 && (current_ts.u.derived->components != NULL
4084 || current_ts.u.derived->attr.zero_comp))
4085 goto ok;
4087 /* Now we have an error, which we signal, and then fix up
4088 because the knock-on is plain and simple confusing. */
4089 gfc_error_now ("Derived type at %C has not been previously defined "
4090 "and so cannot appear in a derived type definition");
4091 current_attr.pointer = 1;
4092 goto ok;
4096 /* If we have an old-style character declaration, and no new-style
4097 attribute specifications, then there a comma is optional between
4098 the type specification and the variable list. */
4099 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4100 gfc_match_char (',');
4102 /* Give the types/attributes to symbols that follow. Give the element
4103 a number so that repeat character length expressions can be copied. */
4104 elem = 1;
4105 for (;;)
4107 num_idents_on_line++;
4108 m = variable_decl (elem++);
4109 if (m == MATCH_ERROR)
4110 goto cleanup;
4111 if (m == MATCH_NO)
4112 break;
4114 if (gfc_match_eos () == MATCH_YES)
4115 goto cleanup;
4116 if (gfc_match_char (',') != MATCH_YES)
4117 break;
4120 if (gfc_error_flag_test () == 0)
4121 gfc_error ("Syntax error in data declaration at %C");
4122 m = MATCH_ERROR;
4124 gfc_free_data_all (gfc_current_ns);
4126 cleanup:
4127 gfc_free_array_spec (current_as);
4128 current_as = NULL;
4129 return m;
4133 /* Match a prefix associated with a function or subroutine
4134 declaration. If the typespec pointer is nonnull, then a typespec
4135 can be matched. Note that if nothing matches, MATCH_YES is
4136 returned (the null string was matched). */
4138 match
4139 gfc_match_prefix (gfc_typespec *ts)
4141 bool seen_type;
4142 bool seen_impure;
4143 bool found_prefix;
4145 gfc_clear_attr (&current_attr);
4146 seen_type = false;
4147 seen_impure = false;
4149 gcc_assert (!gfc_matching_prefix);
4150 gfc_matching_prefix = true;
4154 found_prefix = false;
4156 if (!seen_type && ts != NULL
4157 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4158 && gfc_match_space () == MATCH_YES)
4161 seen_type = true;
4162 found_prefix = true;
4165 if (gfc_match ("elemental% ") == MATCH_YES)
4167 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
4168 goto error;
4170 found_prefix = true;
4173 if (gfc_match ("pure% ") == MATCH_YES)
4175 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
4176 goto error;
4178 found_prefix = true;
4181 if (gfc_match ("recursive% ") == MATCH_YES)
4183 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
4184 goto error;
4186 found_prefix = true;
4189 /* IMPURE is a somewhat special case, as it needs not set an actual
4190 attribute but rather only prevents ELEMENTAL routines from being
4191 automatically PURE. */
4192 if (gfc_match ("impure% ") == MATCH_YES)
4194 if (gfc_notify_std (GFC_STD_F2008,
4195 "Fortran 2008: IMPURE procedure at %C")
4196 == FAILURE)
4197 goto error;
4199 seen_impure = true;
4200 found_prefix = true;
4203 while (found_prefix);
4205 /* IMPURE and PURE must not both appear, of course. */
4206 if (seen_impure && current_attr.pure)
4208 gfc_error ("PURE and IMPURE must not appear both at %C");
4209 goto error;
4212 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4213 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4215 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
4216 goto error;
4219 /* At this point, the next item is not a prefix. */
4220 gcc_assert (gfc_matching_prefix);
4221 gfc_matching_prefix = false;
4222 return MATCH_YES;
4224 error:
4225 gcc_assert (gfc_matching_prefix);
4226 gfc_matching_prefix = false;
4227 return MATCH_ERROR;
4231 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4233 static gfc_try
4234 copy_prefix (symbol_attribute *dest, locus *where)
4236 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
4237 return FAILURE;
4239 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
4240 return FAILURE;
4242 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
4243 return FAILURE;
4245 return SUCCESS;
4249 /* Match a formal argument list. */
4251 match
4252 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4254 gfc_formal_arglist *head, *tail, *p, *q;
4255 char name[GFC_MAX_SYMBOL_LEN + 1];
4256 gfc_symbol *sym;
4257 match m;
4259 head = tail = NULL;
4261 if (gfc_match_char ('(') != MATCH_YES)
4263 if (null_flag)
4264 goto ok;
4265 return MATCH_NO;
4268 if (gfc_match_char (')') == MATCH_YES)
4269 goto ok;
4271 for (;;)
4273 if (gfc_match_char ('*') == MATCH_YES)
4274 sym = NULL;
4275 else
4277 m = gfc_match_name (name);
4278 if (m != MATCH_YES)
4279 goto cleanup;
4281 if (gfc_get_symbol (name, NULL, &sym))
4282 goto cleanup;
4285 p = gfc_get_formal_arglist ();
4287 if (head == NULL)
4288 head = tail = p;
4289 else
4291 tail->next = p;
4292 tail = p;
4295 tail->sym = sym;
4297 /* We don't add the VARIABLE flavor because the name could be a
4298 dummy procedure. We don't apply these attributes to formal
4299 arguments of statement functions. */
4300 if (sym != NULL && !st_flag
4301 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
4302 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
4304 m = MATCH_ERROR;
4305 goto cleanup;
4308 /* The name of a program unit can be in a different namespace,
4309 so check for it explicitly. After the statement is accepted,
4310 the name is checked for especially in gfc_get_symbol(). */
4311 if (gfc_new_block != NULL && sym != NULL
4312 && strcmp (sym->name, gfc_new_block->name) == 0)
4314 gfc_error ("Name '%s' at %C is the name of the procedure",
4315 sym->name);
4316 m = MATCH_ERROR;
4317 goto cleanup;
4320 if (gfc_match_char (')') == MATCH_YES)
4321 goto ok;
4323 m = gfc_match_char (',');
4324 if (m != MATCH_YES)
4326 gfc_error ("Unexpected junk in formal argument list at %C");
4327 goto cleanup;
4332 /* Check for duplicate symbols in the formal argument list. */
4333 if (head != NULL)
4335 for (p = head; p->next; p = p->next)
4337 if (p->sym == NULL)
4338 continue;
4340 for (q = p->next; q; q = q->next)
4341 if (p->sym == q->sym)
4343 gfc_error ("Duplicate symbol '%s' in formal argument list "
4344 "at %C", p->sym->name);
4346 m = MATCH_ERROR;
4347 goto cleanup;
4352 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
4353 == FAILURE)
4355 m = MATCH_ERROR;
4356 goto cleanup;
4359 return MATCH_YES;
4361 cleanup:
4362 gfc_free_formal_arglist (head);
4363 return m;
4367 /* Match a RESULT specification following a function declaration or
4368 ENTRY statement. Also matches the end-of-statement. */
4370 static match
4371 match_result (gfc_symbol *function, gfc_symbol **result)
4373 char name[GFC_MAX_SYMBOL_LEN + 1];
4374 gfc_symbol *r;
4375 match m;
4377 if (gfc_match (" result (") != MATCH_YES)
4378 return MATCH_NO;
4380 m = gfc_match_name (name);
4381 if (m != MATCH_YES)
4382 return m;
4384 /* Get the right paren, and that's it because there could be the
4385 bind(c) attribute after the result clause. */
4386 if (gfc_match_char(')') != MATCH_YES)
4388 /* TODO: should report the missing right paren here. */
4389 return MATCH_ERROR;
4392 if (strcmp (function->name, name) == 0)
4394 gfc_error ("RESULT variable at %C must be different than function name");
4395 return MATCH_ERROR;
4398 if (gfc_get_symbol (name, NULL, &r))
4399 return MATCH_ERROR;
4401 if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4402 return MATCH_ERROR;
4404 *result = r;
4406 return MATCH_YES;
4410 /* Match a function suffix, which could be a combination of a result
4411 clause and BIND(C), either one, or neither. The draft does not
4412 require them to come in a specific order. */
4414 match
4415 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4417 match is_bind_c; /* Found bind(c). */
4418 match is_result; /* Found result clause. */
4419 match found_match; /* Status of whether we've found a good match. */
4420 char peek_char; /* Character we're going to peek at. */
4421 bool allow_binding_name;
4423 /* Initialize to having found nothing. */
4424 found_match = MATCH_NO;
4425 is_bind_c = MATCH_NO;
4426 is_result = MATCH_NO;
4428 /* Get the next char to narrow between result and bind(c). */
4429 gfc_gobble_whitespace ();
4430 peek_char = gfc_peek_ascii_char ();
4432 /* C binding names are not allowed for internal procedures. */
4433 if (gfc_current_state () == COMP_CONTAINS
4434 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4435 allow_binding_name = false;
4436 else
4437 allow_binding_name = true;
4439 switch (peek_char)
4441 case 'r':
4442 /* Look for result clause. */
4443 is_result = match_result (sym, result);
4444 if (is_result == MATCH_YES)
4446 /* Now see if there is a bind(c) after it. */
4447 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4448 /* We've found the result clause and possibly bind(c). */
4449 found_match = MATCH_YES;
4451 else
4452 /* This should only be MATCH_ERROR. */
4453 found_match = is_result;
4454 break;
4455 case 'b':
4456 /* Look for bind(c) first. */
4457 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4458 if (is_bind_c == MATCH_YES)
4460 /* Now see if a result clause followed it. */
4461 is_result = match_result (sym, result);
4462 found_match = MATCH_YES;
4464 else
4466 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4467 found_match = MATCH_ERROR;
4469 break;
4470 default:
4471 gfc_error ("Unexpected junk after function declaration at %C");
4472 found_match = MATCH_ERROR;
4473 break;
4476 if (is_bind_c == MATCH_YES)
4478 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4479 if (gfc_current_state () == COMP_CONTAINS
4480 && sym->ns->proc_name->attr.flavor != FL_MODULE
4481 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4482 "at %L may not be specified for an internal "
4483 "procedure", &gfc_current_locus)
4484 == FAILURE)
4485 return MATCH_ERROR;
4487 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4488 == FAILURE)
4489 return MATCH_ERROR;
4492 return found_match;
4496 /* Procedure pointer return value without RESULT statement:
4497 Add "hidden" result variable named "ppr@". */
4499 static gfc_try
4500 add_hidden_procptr_result (gfc_symbol *sym)
4502 bool case1,case2;
4504 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4505 return FAILURE;
4507 /* First usage case: PROCEDURE and EXTERNAL statements. */
4508 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4509 && strcmp (gfc_current_block ()->name, sym->name) == 0
4510 && sym->attr.external;
4511 /* Second usage case: INTERFACE statements. */
4512 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4513 && gfc_state_stack->previous->state == COMP_FUNCTION
4514 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4516 if (case1 || case2)
4518 gfc_symtree *stree;
4519 if (case1)
4520 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4521 else if (case2)
4523 gfc_symtree *st2;
4524 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4525 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4526 st2->n.sym = stree->n.sym;
4528 sym->result = stree->n.sym;
4530 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4531 sym->result->attr.pointer = sym->attr.pointer;
4532 sym->result->attr.external = sym->attr.external;
4533 sym->result->attr.referenced = sym->attr.referenced;
4534 sym->result->ts = sym->ts;
4535 sym->attr.proc_pointer = 0;
4536 sym->attr.pointer = 0;
4537 sym->attr.external = 0;
4538 if (sym->result->attr.external && sym->result->attr.pointer)
4540 sym->result->attr.pointer = 0;
4541 sym->result->attr.proc_pointer = 1;
4544 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4546 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4547 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4548 && sym->result && sym->result != sym && sym->result->attr.external
4549 && sym == gfc_current_ns->proc_name
4550 && sym == sym->result->ns->proc_name
4551 && strcmp ("ppr@", sym->result->name) == 0)
4553 sym->result->attr.proc_pointer = 1;
4554 sym->attr.pointer = 0;
4555 return SUCCESS;
4557 else
4558 return FAILURE;
4562 /* Match the interface for a PROCEDURE declaration,
4563 including brackets (R1212). */
4565 static match
4566 match_procedure_interface (gfc_symbol **proc_if)
4568 match m;
4569 gfc_symtree *st;
4570 locus old_loc, entry_loc;
4571 gfc_namespace *old_ns = gfc_current_ns;
4572 char name[GFC_MAX_SYMBOL_LEN + 1];
4574 old_loc = entry_loc = gfc_current_locus;
4575 gfc_clear_ts (&current_ts);
4577 if (gfc_match (" (") != MATCH_YES)
4579 gfc_current_locus = entry_loc;
4580 return MATCH_NO;
4583 /* Get the type spec. for the procedure interface. */
4584 old_loc = gfc_current_locus;
4585 m = gfc_match_decl_type_spec (&current_ts, 0);
4586 gfc_gobble_whitespace ();
4587 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4588 goto got_ts;
4590 if (m == MATCH_ERROR)
4591 return m;
4593 /* Procedure interface is itself a procedure. */
4594 gfc_current_locus = old_loc;
4595 m = gfc_match_name (name);
4597 /* First look to see if it is already accessible in the current
4598 namespace because it is use associated or contained. */
4599 st = NULL;
4600 if (gfc_find_sym_tree (name, NULL, 0, &st))
4601 return MATCH_ERROR;
4603 /* If it is still not found, then try the parent namespace, if it
4604 exists and create the symbol there if it is still not found. */
4605 if (gfc_current_ns->parent)
4606 gfc_current_ns = gfc_current_ns->parent;
4607 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4608 return MATCH_ERROR;
4610 gfc_current_ns = old_ns;
4611 *proc_if = st->n.sym;
4613 /* Various interface checks. */
4614 if (*proc_if)
4616 (*proc_if)->refs++;
4617 /* Resolve interface if possible. That way, attr.procedure is only set
4618 if it is declared by a later procedure-declaration-stmt, which is
4619 invalid per C1212. */
4620 while ((*proc_if)->ts.interface)
4621 *proc_if = (*proc_if)->ts.interface;
4623 if ((*proc_if)->generic)
4625 gfc_error ("Interface '%s' at %C may not be generic",
4626 (*proc_if)->name);
4627 return MATCH_ERROR;
4629 if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
4631 gfc_error ("Interface '%s' at %C may not be a statement function",
4632 (*proc_if)->name);
4633 return MATCH_ERROR;
4635 /* Handle intrinsic procedures. */
4636 if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4637 || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4638 && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4639 || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4640 (*proc_if)->attr.intrinsic = 1;
4641 if ((*proc_if)->attr.intrinsic
4642 && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
4644 gfc_error ("Intrinsic procedure '%s' not allowed "
4645 "in PROCEDURE statement at %C", (*proc_if)->name);
4646 return MATCH_ERROR;
4650 got_ts:
4651 if (gfc_match (" )") != MATCH_YES)
4653 gfc_current_locus = entry_loc;
4654 return MATCH_NO;
4657 return MATCH_YES;
4661 /* Match a PROCEDURE declaration (R1211). */
4663 static match
4664 match_procedure_decl (void)
4666 match m;
4667 gfc_symbol *sym, *proc_if = NULL;
4668 int num;
4669 gfc_expr *initializer = NULL;
4671 /* Parse interface (with brackets). */
4672 m = match_procedure_interface (&proc_if);
4673 if (m != MATCH_YES)
4674 return m;
4676 /* Parse attributes (with colons). */
4677 m = match_attr_spec();
4678 if (m == MATCH_ERROR)
4679 return MATCH_ERROR;
4681 /* Get procedure symbols. */
4682 for(num=1;;num++)
4684 m = gfc_match_symbol (&sym, 0);
4685 if (m == MATCH_NO)
4686 goto syntax;
4687 else if (m == MATCH_ERROR)
4688 return m;
4690 /* Add current_attr to the symbol attributes. */
4691 if (gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4692 return MATCH_ERROR;
4694 if (sym->attr.is_bind_c)
4696 /* Check for C1218. */
4697 if (!proc_if || !proc_if->attr.is_bind_c)
4699 gfc_error ("BIND(C) attribute at %C requires "
4700 "an interface with BIND(C)");
4701 return MATCH_ERROR;
4703 /* Check for C1217. */
4704 if (has_name_equals && sym->attr.pointer)
4706 gfc_error ("BIND(C) procedure with NAME may not have "
4707 "POINTER attribute at %C");
4708 return MATCH_ERROR;
4710 if (has_name_equals && sym->attr.dummy)
4712 gfc_error ("Dummy procedure at %C may not have "
4713 "BIND(C) attribute with NAME");
4714 return MATCH_ERROR;
4716 /* Set binding label for BIND(C). */
4717 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4718 return MATCH_ERROR;
4721 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4722 return MATCH_ERROR;
4724 if (add_hidden_procptr_result (sym) == SUCCESS)
4725 sym = sym->result;
4727 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4728 return MATCH_ERROR;
4730 /* Set interface. */
4731 if (proc_if != NULL)
4733 if (sym->ts.type != BT_UNKNOWN)
4735 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4736 sym->name, &gfc_current_locus,
4737 gfc_basic_typename (sym->ts.type));
4738 return MATCH_ERROR;
4740 sym->ts.interface = proc_if;
4741 sym->attr.untyped = 1;
4742 sym->attr.if_source = IFSRC_IFBODY;
4744 else if (current_ts.type != BT_UNKNOWN)
4746 if (gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
4747 return MATCH_ERROR;
4748 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4749 sym->ts.interface->ts = current_ts;
4750 sym->ts.interface->attr.flavor = FL_PROCEDURE;
4751 sym->ts.interface->attr.function = 1;
4752 sym->attr.function = 1;
4753 sym->attr.if_source = IFSRC_UNKNOWN;
4756 if (gfc_match (" =>") == MATCH_YES)
4758 if (!current_attr.pointer)
4760 gfc_error ("Initialization at %C isn't for a pointer variable");
4761 m = MATCH_ERROR;
4762 goto cleanup;
4765 m = match_pointer_init (&initializer, 1);
4766 if (m != MATCH_YES)
4767 goto cleanup;
4769 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4770 != SUCCESS)
4771 goto cleanup;
4775 gfc_set_sym_referenced (sym);
4777 if (gfc_match_eos () == MATCH_YES)
4778 return MATCH_YES;
4779 if (gfc_match_char (',') != MATCH_YES)
4780 goto syntax;
4783 syntax:
4784 gfc_error ("Syntax error in PROCEDURE statement at %C");
4785 return MATCH_ERROR;
4787 cleanup:
4788 /* Free stuff up and return. */
4789 gfc_free_expr (initializer);
4790 return m;
4794 static match
4795 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4798 /* Match a procedure pointer component declaration (R445). */
4800 static match
4801 match_ppc_decl (void)
4803 match m;
4804 gfc_symbol *proc_if = NULL;
4805 gfc_typespec ts;
4806 int num;
4807 gfc_component *c;
4808 gfc_expr *initializer = NULL;
4809 gfc_typebound_proc* tb;
4810 char name[GFC_MAX_SYMBOL_LEN + 1];
4812 /* Parse interface (with brackets). */
4813 m = match_procedure_interface (&proc_if);
4814 if (m != MATCH_YES)
4815 goto syntax;
4817 /* Parse attributes. */
4818 tb = XCNEW (gfc_typebound_proc);
4819 tb->where = gfc_current_locus;
4820 m = match_binding_attributes (tb, false, true);
4821 if (m == MATCH_ERROR)
4822 return m;
4824 gfc_clear_attr (&current_attr);
4825 current_attr.procedure = 1;
4826 current_attr.proc_pointer = 1;
4827 current_attr.access = tb->access;
4828 current_attr.flavor = FL_PROCEDURE;
4830 /* Match the colons (required). */
4831 if (gfc_match (" ::") != MATCH_YES)
4833 gfc_error ("Expected '::' after binding-attributes at %C");
4834 return MATCH_ERROR;
4837 /* Check for C450. */
4838 if (!tb->nopass && proc_if == NULL)
4840 gfc_error("NOPASS or explicit interface required at %C");
4841 return MATCH_ERROR;
4844 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure pointer "
4845 "component at %C") == FAILURE)
4846 return MATCH_ERROR;
4848 /* Match PPC names. */
4849 ts = current_ts;
4850 for(num=1;;num++)
4852 m = gfc_match_name (name);
4853 if (m == MATCH_NO)
4854 goto syntax;
4855 else if (m == MATCH_ERROR)
4856 return m;
4858 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
4859 return MATCH_ERROR;
4861 /* Add current_attr to the symbol attributes. */
4862 if (gfc_copy_attr (&c->attr, &current_attr, NULL) == FAILURE)
4863 return MATCH_ERROR;
4865 if (gfc_add_external (&c->attr, NULL) == FAILURE)
4866 return MATCH_ERROR;
4868 if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
4869 return MATCH_ERROR;
4871 c->tb = tb;
4873 /* Set interface. */
4874 if (proc_if != NULL)
4876 c->ts.interface = proc_if;
4877 c->attr.untyped = 1;
4878 c->attr.if_source = IFSRC_IFBODY;
4880 else if (ts.type != BT_UNKNOWN)
4882 c->ts = ts;
4883 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4884 c->ts.interface->ts = ts;
4885 c->ts.interface->attr.flavor = FL_PROCEDURE;
4886 c->ts.interface->attr.function = 1;
4887 c->attr.function = 1;
4888 c->attr.if_source = IFSRC_UNKNOWN;
4891 if (gfc_match (" =>") == MATCH_YES)
4893 m = match_pointer_init (&initializer, 1);
4894 if (m != MATCH_YES)
4896 gfc_free_expr (initializer);
4897 return m;
4899 c->initializer = initializer;
4902 if (gfc_match_eos () == MATCH_YES)
4903 return MATCH_YES;
4904 if (gfc_match_char (',') != MATCH_YES)
4905 goto syntax;
4908 syntax:
4909 gfc_error ("Syntax error in procedure pointer component at %C");
4910 return MATCH_ERROR;
4914 /* Match a PROCEDURE declaration inside an interface (R1206). */
4916 static match
4917 match_procedure_in_interface (void)
4919 match m;
4920 gfc_symbol *sym;
4921 char name[GFC_MAX_SYMBOL_LEN + 1];
4923 if (current_interface.type == INTERFACE_NAMELESS
4924 || current_interface.type == INTERFACE_ABSTRACT)
4926 gfc_error ("PROCEDURE at %C must be in a generic interface");
4927 return MATCH_ERROR;
4930 for(;;)
4932 m = gfc_match_name (name);
4933 if (m == MATCH_NO)
4934 goto syntax;
4935 else if (m == MATCH_ERROR)
4936 return m;
4937 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4938 return MATCH_ERROR;
4940 if (gfc_add_interface (sym) == FAILURE)
4941 return MATCH_ERROR;
4943 if (gfc_match_eos () == MATCH_YES)
4944 break;
4945 if (gfc_match_char (',') != MATCH_YES)
4946 goto syntax;
4949 return MATCH_YES;
4951 syntax:
4952 gfc_error ("Syntax error in PROCEDURE statement at %C");
4953 return MATCH_ERROR;
4957 /* General matcher for PROCEDURE declarations. */
4959 static match match_procedure_in_type (void);
4961 match
4962 gfc_match_procedure (void)
4964 match m;
4966 switch (gfc_current_state ())
4968 case COMP_NONE:
4969 case COMP_PROGRAM:
4970 case COMP_MODULE:
4971 case COMP_SUBROUTINE:
4972 case COMP_FUNCTION:
4973 m = match_procedure_decl ();
4974 break;
4975 case COMP_INTERFACE:
4976 m = match_procedure_in_interface ();
4977 break;
4978 case COMP_DERIVED:
4979 m = match_ppc_decl ();
4980 break;
4981 case COMP_DERIVED_CONTAINS:
4982 m = match_procedure_in_type ();
4983 break;
4984 default:
4985 return MATCH_NO;
4988 if (m != MATCH_YES)
4989 return m;
4991 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4992 == FAILURE)
4993 return MATCH_ERROR;
4995 return m;
4999 /* Warn if a matched procedure has the same name as an intrinsic; this is
5000 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5001 parser-state-stack to find out whether we're in a module. */
5003 static void
5004 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5006 bool in_module;
5008 in_module = (gfc_state_stack->previous
5009 && gfc_state_stack->previous->state == COMP_MODULE);
5011 gfc_warn_intrinsic_shadow (sym, in_module, func);
5015 /* Match a function declaration. */
5017 match
5018 gfc_match_function_decl (void)
5020 char name[GFC_MAX_SYMBOL_LEN + 1];
5021 gfc_symbol *sym, *result;
5022 locus old_loc;
5023 match m;
5024 match suffix_match;
5025 match found_match; /* Status returned by match func. */
5027 if (gfc_current_state () != COMP_NONE
5028 && gfc_current_state () != COMP_INTERFACE
5029 && gfc_current_state () != COMP_CONTAINS)
5030 return MATCH_NO;
5032 gfc_clear_ts (&current_ts);
5034 old_loc = gfc_current_locus;
5036 m = gfc_match_prefix (&current_ts);
5037 if (m != MATCH_YES)
5039 gfc_current_locus = old_loc;
5040 return m;
5043 if (gfc_match ("function% %n", name) != MATCH_YES)
5045 gfc_current_locus = old_loc;
5046 return MATCH_NO;
5048 if (get_proc_name (name, &sym, false))
5049 return MATCH_ERROR;
5051 if (add_hidden_procptr_result (sym) == SUCCESS)
5052 sym = sym->result;
5054 gfc_new_block = sym;
5056 m = gfc_match_formal_arglist (sym, 0, 0);
5057 if (m == MATCH_NO)
5059 gfc_error ("Expected formal argument list in function "
5060 "definition at %C");
5061 m = MATCH_ERROR;
5062 goto cleanup;
5064 else if (m == MATCH_ERROR)
5065 goto cleanup;
5067 result = NULL;
5069 /* According to the draft, the bind(c) and result clause can
5070 come in either order after the formal_arg_list (i.e., either
5071 can be first, both can exist together or by themselves or neither
5072 one). Therefore, the match_result can't match the end of the
5073 string, and check for the bind(c) or result clause in either order. */
5074 found_match = gfc_match_eos ();
5076 /* Make sure that it isn't already declared as BIND(C). If it is, it
5077 must have been marked BIND(C) with a BIND(C) attribute and that is
5078 not allowed for procedures. */
5079 if (sym->attr.is_bind_c == 1)
5081 sym->attr.is_bind_c = 0;
5082 if (sym->old_symbol != NULL)
5083 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5084 "variables or common blocks",
5085 &(sym->old_symbol->declared_at));
5086 else
5087 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5088 "variables or common blocks", &gfc_current_locus);
5091 if (found_match != MATCH_YES)
5093 /* If we haven't found the end-of-statement, look for a suffix. */
5094 suffix_match = gfc_match_suffix (sym, &result);
5095 if (suffix_match == MATCH_YES)
5096 /* Need to get the eos now. */
5097 found_match = gfc_match_eos ();
5098 else
5099 found_match = suffix_match;
5102 if(found_match != MATCH_YES)
5103 m = MATCH_ERROR;
5104 else
5106 /* Make changes to the symbol. */
5107 m = MATCH_ERROR;
5109 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
5110 goto cleanup;
5112 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
5113 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5114 goto cleanup;
5116 /* Delay matching the function characteristics until after the
5117 specification block by signalling kind=-1. */
5118 sym->declared_at = old_loc;
5119 if (current_ts.type != BT_UNKNOWN)
5120 current_ts.kind = -1;
5121 else
5122 current_ts.kind = 0;
5124 if (result == NULL)
5126 if (current_ts.type != BT_UNKNOWN
5127 && gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
5128 goto cleanup;
5129 sym->result = sym;
5131 else
5133 if (current_ts.type != BT_UNKNOWN
5134 && gfc_add_type (result, &current_ts, &gfc_current_locus)
5135 == FAILURE)
5136 goto cleanup;
5137 sym->result = result;
5140 /* Warn if this procedure has the same name as an intrinsic. */
5141 warn_intrinsic_shadow (sym, true);
5143 return MATCH_YES;
5146 cleanup:
5147 gfc_current_locus = old_loc;
5148 return m;
5152 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5153 pass the name of the entry, rather than the gfc_current_block name, and
5154 to return false upon finding an existing global entry. */
5156 static bool
5157 add_global_entry (const char *name, int sub)
5159 gfc_gsymbol *s;
5160 enum gfc_symbol_type type;
5162 s = gfc_get_gsymbol(name);
5163 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5165 if (s->defined
5166 || (s->type != GSYM_UNKNOWN
5167 && s->type != type))
5168 gfc_global_used(s, NULL);
5169 else
5171 s->type = type;
5172 s->where = gfc_current_locus;
5173 s->defined = 1;
5174 s->ns = gfc_current_ns;
5175 return true;
5177 return false;
5181 /* Match an ENTRY statement. */
5183 match
5184 gfc_match_entry (void)
5186 gfc_symbol *proc;
5187 gfc_symbol *result;
5188 gfc_symbol *entry;
5189 char name[GFC_MAX_SYMBOL_LEN + 1];
5190 gfc_compile_state state;
5191 match m;
5192 gfc_entry_list *el;
5193 locus old_loc;
5194 bool module_procedure;
5195 char peek_char;
5196 match is_bind_c;
5198 m = gfc_match_name (name);
5199 if (m != MATCH_YES)
5200 return m;
5202 if (gfc_notify_std (GFC_STD_F2008_OBS, "Fortran 2008 obsolescent feature: "
5203 "ENTRY statement at %C") == FAILURE)
5204 return MATCH_ERROR;
5206 state = gfc_current_state ();
5207 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5209 switch (state)
5211 case COMP_PROGRAM:
5212 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5213 break;
5214 case COMP_MODULE:
5215 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5216 break;
5217 case COMP_BLOCK_DATA:
5218 gfc_error ("ENTRY statement at %C cannot appear within "
5219 "a BLOCK DATA");
5220 break;
5221 case COMP_INTERFACE:
5222 gfc_error ("ENTRY statement at %C cannot appear within "
5223 "an INTERFACE");
5224 break;
5225 case COMP_DERIVED:
5226 gfc_error ("ENTRY statement at %C cannot appear within "
5227 "a DERIVED TYPE block");
5228 break;
5229 case COMP_IF:
5230 gfc_error ("ENTRY statement at %C cannot appear within "
5231 "an IF-THEN block");
5232 break;
5233 case COMP_DO:
5234 gfc_error ("ENTRY statement at %C cannot appear within "
5235 "a DO block");
5236 break;
5237 case COMP_SELECT:
5238 gfc_error ("ENTRY statement at %C cannot appear within "
5239 "a SELECT block");
5240 break;
5241 case COMP_FORALL:
5242 gfc_error ("ENTRY statement at %C cannot appear within "
5243 "a FORALL block");
5244 break;
5245 case COMP_WHERE:
5246 gfc_error ("ENTRY statement at %C cannot appear within "
5247 "a WHERE block");
5248 break;
5249 case COMP_CONTAINS:
5250 gfc_error ("ENTRY statement at %C cannot appear within "
5251 "a contained subprogram");
5252 break;
5253 default:
5254 gfc_internal_error ("gfc_match_entry(): Bad state");
5256 return MATCH_ERROR;
5259 module_procedure = gfc_current_ns->parent != NULL
5260 && gfc_current_ns->parent->proc_name
5261 && gfc_current_ns->parent->proc_name->attr.flavor
5262 == FL_MODULE;
5264 if (gfc_current_ns->parent != NULL
5265 && gfc_current_ns->parent->proc_name
5266 && !module_procedure)
5268 gfc_error("ENTRY statement at %C cannot appear in a "
5269 "contained procedure");
5270 return MATCH_ERROR;
5273 /* Module function entries need special care in get_proc_name
5274 because previous references within the function will have
5275 created symbols attached to the current namespace. */
5276 if (get_proc_name (name, &entry,
5277 gfc_current_ns->parent != NULL
5278 && module_procedure))
5279 return MATCH_ERROR;
5281 proc = gfc_current_block ();
5283 /* Make sure that it isn't already declared as BIND(C). If it is, it
5284 must have been marked BIND(C) with a BIND(C) attribute and that is
5285 not allowed for procedures. */
5286 if (entry->attr.is_bind_c == 1)
5288 entry->attr.is_bind_c = 0;
5289 if (entry->old_symbol != NULL)
5290 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5291 "variables or common blocks",
5292 &(entry->old_symbol->declared_at));
5293 else
5294 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5295 "variables or common blocks", &gfc_current_locus);
5298 /* Check what next non-whitespace character is so we can tell if there
5299 is the required parens if we have a BIND(C). */
5300 gfc_gobble_whitespace ();
5301 peek_char = gfc_peek_ascii_char ();
5303 if (state == COMP_SUBROUTINE)
5305 /* An entry in a subroutine. */
5306 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
5307 return MATCH_ERROR;
5309 m = gfc_match_formal_arglist (entry, 0, 1);
5310 if (m != MATCH_YES)
5311 return MATCH_ERROR;
5313 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5314 never be an internal procedure. */
5315 is_bind_c = gfc_match_bind_c (entry, true);
5316 if (is_bind_c == MATCH_ERROR)
5317 return MATCH_ERROR;
5318 if (is_bind_c == MATCH_YES)
5320 if (peek_char != '(')
5322 gfc_error ("Missing required parentheses before BIND(C) at %C");
5323 return MATCH_ERROR;
5325 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
5326 == FAILURE)
5327 return MATCH_ERROR;
5330 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5331 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
5332 return MATCH_ERROR;
5334 else
5336 /* An entry in a function.
5337 We need to take special care because writing
5338 ENTRY f()
5340 ENTRY f
5341 is allowed, whereas
5342 ENTRY f() RESULT (r)
5343 can't be written as
5344 ENTRY f RESULT (r). */
5345 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
5346 return MATCH_ERROR;
5348 old_loc = gfc_current_locus;
5349 if (gfc_match_eos () == MATCH_YES)
5351 gfc_current_locus = old_loc;
5352 /* Match the empty argument list, and add the interface to
5353 the symbol. */
5354 m = gfc_match_formal_arglist (entry, 0, 1);
5356 else
5357 m = gfc_match_formal_arglist (entry, 0, 0);
5359 if (m != MATCH_YES)
5360 return MATCH_ERROR;
5362 result = NULL;
5364 if (gfc_match_eos () == MATCH_YES)
5366 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5367 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5368 return MATCH_ERROR;
5370 entry->result = entry;
5372 else
5374 m = gfc_match_suffix (entry, &result);
5375 if (m == MATCH_NO)
5376 gfc_syntax_error (ST_ENTRY);
5377 if (m != MATCH_YES)
5378 return MATCH_ERROR;
5380 if (result)
5382 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
5383 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
5384 || gfc_add_function (&entry->attr, result->name, NULL)
5385 == FAILURE)
5386 return MATCH_ERROR;
5387 entry->result = result;
5389 else
5391 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5392 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5393 return MATCH_ERROR;
5394 entry->result = entry;
5399 if (gfc_match_eos () != MATCH_YES)
5401 gfc_syntax_error (ST_ENTRY);
5402 return MATCH_ERROR;
5405 entry->attr.recursive = proc->attr.recursive;
5406 entry->attr.elemental = proc->attr.elemental;
5407 entry->attr.pure = proc->attr.pure;
5409 el = gfc_get_entry_list ();
5410 el->sym = entry;
5411 el->next = gfc_current_ns->entries;
5412 gfc_current_ns->entries = el;
5413 if (el->next)
5414 el->id = el->next->id + 1;
5415 else
5416 el->id = 1;
5418 new_st.op = EXEC_ENTRY;
5419 new_st.ext.entry = el;
5421 return MATCH_YES;
5425 /* Match a subroutine statement, including optional prefixes. */
5427 match
5428 gfc_match_subroutine (void)
5430 char name[GFC_MAX_SYMBOL_LEN + 1];
5431 gfc_symbol *sym;
5432 match m;
5433 match is_bind_c;
5434 char peek_char;
5435 bool allow_binding_name;
5437 if (gfc_current_state () != COMP_NONE
5438 && gfc_current_state () != COMP_INTERFACE
5439 && gfc_current_state () != COMP_CONTAINS)
5440 return MATCH_NO;
5442 m = gfc_match_prefix (NULL);
5443 if (m != MATCH_YES)
5444 return m;
5446 m = gfc_match ("subroutine% %n", name);
5447 if (m != MATCH_YES)
5448 return m;
5450 if (get_proc_name (name, &sym, false))
5451 return MATCH_ERROR;
5453 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5454 the symbol existed before. */
5455 sym->declared_at = gfc_current_locus;
5457 if (add_hidden_procptr_result (sym) == SUCCESS)
5458 sym = sym->result;
5460 gfc_new_block = sym;
5462 /* Check what next non-whitespace character is so we can tell if there
5463 is the required parens if we have a BIND(C). */
5464 gfc_gobble_whitespace ();
5465 peek_char = gfc_peek_ascii_char ();
5467 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5468 return MATCH_ERROR;
5470 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5471 return MATCH_ERROR;
5473 /* Make sure that it isn't already declared as BIND(C). If it is, it
5474 must have been marked BIND(C) with a BIND(C) attribute and that is
5475 not allowed for procedures. */
5476 if (sym->attr.is_bind_c == 1)
5478 sym->attr.is_bind_c = 0;
5479 if (sym->old_symbol != NULL)
5480 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5481 "variables or common blocks",
5482 &(sym->old_symbol->declared_at));
5483 else
5484 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5485 "variables or common blocks", &gfc_current_locus);
5488 /* C binding names are not allowed for internal procedures. */
5489 if (gfc_current_state () == COMP_CONTAINS
5490 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5491 allow_binding_name = false;
5492 else
5493 allow_binding_name = true;
5495 /* Here, we are just checking if it has the bind(c) attribute, and if
5496 so, then we need to make sure it's all correct. If it doesn't,
5497 we still need to continue matching the rest of the subroutine line. */
5498 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5499 if (is_bind_c == MATCH_ERROR)
5501 /* There was an attempt at the bind(c), but it was wrong. An
5502 error message should have been printed w/in the gfc_match_bind_c
5503 so here we'll just return the MATCH_ERROR. */
5504 return MATCH_ERROR;
5507 if (is_bind_c == MATCH_YES)
5509 /* The following is allowed in the Fortran 2008 draft. */
5510 if (gfc_current_state () == COMP_CONTAINS
5511 && sym->ns->proc_name->attr.flavor != FL_MODULE
5512 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5513 "at %L may not be specified for an internal "
5514 "procedure", &gfc_current_locus)
5515 == FAILURE)
5516 return MATCH_ERROR;
5518 if (peek_char != '(')
5520 gfc_error ("Missing required parentheses before BIND(C) at %C");
5521 return MATCH_ERROR;
5523 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5524 == FAILURE)
5525 return MATCH_ERROR;
5528 if (gfc_match_eos () != MATCH_YES)
5530 gfc_syntax_error (ST_SUBROUTINE);
5531 return MATCH_ERROR;
5534 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5535 return MATCH_ERROR;
5537 /* Warn if it has the same name as an intrinsic. */
5538 warn_intrinsic_shadow (sym, false);
5540 return MATCH_YES;
5544 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5545 given, and set the binding label in either the given symbol (if not
5546 NULL), or in the current_ts. The symbol may be NULL because we may
5547 encounter the BIND(C) before the declaration itself. Return
5548 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5549 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5550 or MATCH_YES if the specifier was correct and the binding label and
5551 bind(c) fields were set correctly for the given symbol or the
5552 current_ts. If allow_binding_name is false, no binding name may be
5553 given. */
5555 match
5556 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5558 /* binding label, if exists */
5559 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
5560 match double_quote;
5561 match single_quote;
5563 /* Initialize the flag that specifies whether we encountered a NAME=
5564 specifier or not. */
5565 has_name_equals = 0;
5567 /* Init the first char to nil so we can catch if we don't have
5568 the label (name attr) or the symbol name yet. */
5569 binding_label[0] = '\0';
5571 /* This much we have to be able to match, in this order, if
5572 there is a bind(c) label. */
5573 if (gfc_match (" bind ( c ") != MATCH_YES)
5574 return MATCH_NO;
5576 /* Now see if there is a binding label, or if we've reached the
5577 end of the bind(c) attribute without one. */
5578 if (gfc_match_char (',') == MATCH_YES)
5580 if (gfc_match (" name = ") != MATCH_YES)
5582 gfc_error ("Syntax error in NAME= specifier for binding label "
5583 "at %C");
5584 /* should give an error message here */
5585 return MATCH_ERROR;
5588 has_name_equals = 1;
5590 /* Get the opening quote. */
5591 double_quote = MATCH_YES;
5592 single_quote = MATCH_YES;
5593 double_quote = gfc_match_char ('"');
5594 if (double_quote != MATCH_YES)
5595 single_quote = gfc_match_char ('\'');
5596 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5598 gfc_error ("Syntax error in NAME= specifier for binding label "
5599 "at %C");
5600 return MATCH_ERROR;
5603 /* Grab the binding label, using functions that will not lower
5604 case the names automatically. */
5605 if (gfc_match_name_C (binding_label) != MATCH_YES)
5606 return MATCH_ERROR;
5608 /* Get the closing quotation. */
5609 if (double_quote == MATCH_YES)
5611 if (gfc_match_char ('"') != MATCH_YES)
5613 gfc_error ("Missing closing quote '\"' for binding label at %C");
5614 /* User started string with '"' so looked to match it. */
5615 return MATCH_ERROR;
5618 else
5620 if (gfc_match_char ('\'') != MATCH_YES)
5622 gfc_error ("Missing closing quote '\'' for binding label at %C");
5623 /* User started string with "'" char. */
5624 return MATCH_ERROR;
5629 /* Get the required right paren. */
5630 if (gfc_match_char (')') != MATCH_YES)
5632 gfc_error ("Missing closing paren for binding label at %C");
5633 return MATCH_ERROR;
5636 if (has_name_equals && !allow_binding_name)
5638 gfc_error ("No binding name is allowed in BIND(C) at %C");
5639 return MATCH_ERROR;
5642 if (has_name_equals && sym != NULL && sym->attr.dummy)
5644 gfc_error ("For dummy procedure %s, no binding name is "
5645 "allowed in BIND(C) at %C", sym->name);
5646 return MATCH_ERROR;
5650 /* Save the binding label to the symbol. If sym is null, we're
5651 probably matching the typespec attributes of a declaration and
5652 haven't gotten the name yet, and therefore, no symbol yet. */
5653 if (binding_label[0] != '\0')
5655 if (sym != NULL)
5657 strcpy (sym->binding_label, binding_label);
5659 else
5660 strcpy (curr_binding_label, binding_label);
5662 else if (allow_binding_name)
5664 /* No binding label, but if symbol isn't null, we
5665 can set the label for it here.
5666 If name="" or allow_binding_name is false, no C binding name is
5667 created. */
5668 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5669 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
5672 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5673 && current_interface.type == INTERFACE_ABSTRACT)
5675 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5676 return MATCH_ERROR;
5679 return MATCH_YES;
5683 /* Return nonzero if we're currently compiling a contained procedure. */
5685 static int
5686 contained_procedure (void)
5688 gfc_state_data *s = gfc_state_stack;
5690 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5691 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5692 return 1;
5694 return 0;
5697 /* Set the kind of each enumerator. The kind is selected such that it is
5698 interoperable with the corresponding C enumeration type, making
5699 sure that -fshort-enums is honored. */
5701 static void
5702 set_enum_kind(void)
5704 enumerator_history *current_history = NULL;
5705 int kind;
5706 int i;
5708 if (max_enum == NULL || enum_history == NULL)
5709 return;
5711 if (!flag_short_enums)
5712 return;
5714 i = 0;
5717 kind = gfc_integer_kinds[i++].kind;
5719 while (kind < gfc_c_int_kind
5720 && gfc_check_integer_range (max_enum->initializer->value.integer,
5721 kind) != ARITH_OK);
5723 current_history = enum_history;
5724 while (current_history != NULL)
5726 current_history->sym->ts.kind = kind;
5727 current_history = current_history->next;
5732 /* Match any of the various end-block statements. Returns the type of
5733 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5734 and END BLOCK statements cannot be replaced by a single END statement. */
5736 match
5737 gfc_match_end (gfc_statement *st)
5739 char name[GFC_MAX_SYMBOL_LEN + 1];
5740 gfc_compile_state state;
5741 locus old_loc;
5742 const char *block_name;
5743 const char *target;
5744 int eos_ok;
5745 match m;
5747 old_loc = gfc_current_locus;
5748 if (gfc_match ("end") != MATCH_YES)
5749 return MATCH_NO;
5751 state = gfc_current_state ();
5752 block_name = gfc_current_block () == NULL
5753 ? NULL : gfc_current_block ()->name;
5755 switch (state)
5757 case COMP_ASSOCIATE:
5758 case COMP_BLOCK:
5759 if (!strncmp (block_name, "block@", strlen("block@")))
5760 block_name = NULL;
5761 break;
5763 case COMP_CONTAINS:
5764 case COMP_DERIVED_CONTAINS:
5765 state = gfc_state_stack->previous->state;
5766 block_name = gfc_state_stack->previous->sym == NULL
5767 ? NULL : gfc_state_stack->previous->sym->name;
5768 break;
5770 default:
5771 break;
5774 switch (state)
5776 case COMP_NONE:
5777 case COMP_PROGRAM:
5778 *st = ST_END_PROGRAM;
5779 target = " program";
5780 eos_ok = 1;
5781 break;
5783 case COMP_SUBROUTINE:
5784 *st = ST_END_SUBROUTINE;
5785 target = " subroutine";
5786 eos_ok = !contained_procedure ();
5787 break;
5789 case COMP_FUNCTION:
5790 *st = ST_END_FUNCTION;
5791 target = " function";
5792 eos_ok = !contained_procedure ();
5793 break;
5795 case COMP_BLOCK_DATA:
5796 *st = ST_END_BLOCK_DATA;
5797 target = " block data";
5798 eos_ok = 1;
5799 break;
5801 case COMP_MODULE:
5802 *st = ST_END_MODULE;
5803 target = " module";
5804 eos_ok = 1;
5805 break;
5807 case COMP_INTERFACE:
5808 *st = ST_END_INTERFACE;
5809 target = " interface";
5810 eos_ok = 0;
5811 break;
5813 case COMP_DERIVED:
5814 case COMP_DERIVED_CONTAINS:
5815 *st = ST_END_TYPE;
5816 target = " type";
5817 eos_ok = 0;
5818 break;
5820 case COMP_ASSOCIATE:
5821 *st = ST_END_ASSOCIATE;
5822 target = " associate";
5823 eos_ok = 0;
5824 break;
5826 case COMP_BLOCK:
5827 *st = ST_END_BLOCK;
5828 target = " block";
5829 eos_ok = 0;
5830 break;
5832 case COMP_IF:
5833 *st = ST_ENDIF;
5834 target = " if";
5835 eos_ok = 0;
5836 break;
5838 case COMP_DO:
5839 *st = ST_ENDDO;
5840 target = " do";
5841 eos_ok = 0;
5842 break;
5844 case COMP_CRITICAL:
5845 *st = ST_END_CRITICAL;
5846 target = " critical";
5847 eos_ok = 0;
5848 break;
5850 case COMP_SELECT:
5851 case COMP_SELECT_TYPE:
5852 *st = ST_END_SELECT;
5853 target = " select";
5854 eos_ok = 0;
5855 break;
5857 case COMP_FORALL:
5858 *st = ST_END_FORALL;
5859 target = " forall";
5860 eos_ok = 0;
5861 break;
5863 case COMP_WHERE:
5864 *st = ST_END_WHERE;
5865 target = " where";
5866 eos_ok = 0;
5867 break;
5869 case COMP_ENUM:
5870 *st = ST_END_ENUM;
5871 target = " enum";
5872 eos_ok = 0;
5873 last_initializer = NULL;
5874 set_enum_kind ();
5875 gfc_free_enum_history ();
5876 break;
5878 default:
5879 gfc_error ("Unexpected END statement at %C");
5880 goto cleanup;
5883 if (gfc_match_eos () == MATCH_YES)
5885 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
5887 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: END statement "
5888 "instead of %s statement at %L",
5889 gfc_ascii_statement (*st), &old_loc) == FAILURE)
5890 goto cleanup;
5892 else if (!eos_ok)
5894 /* We would have required END [something]. */
5895 gfc_error ("%s statement expected at %L",
5896 gfc_ascii_statement (*st), &old_loc);
5897 goto cleanup;
5900 return MATCH_YES;
5903 /* Verify that we've got the sort of end-block that we're expecting. */
5904 if (gfc_match (target) != MATCH_YES)
5906 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5907 goto cleanup;
5910 /* If we're at the end, make sure a block name wasn't required. */
5911 if (gfc_match_eos () == MATCH_YES)
5914 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5915 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
5916 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
5917 return MATCH_YES;
5919 if (!block_name)
5920 return MATCH_YES;
5922 gfc_error ("Expected block name of '%s' in %s statement at %C",
5923 block_name, gfc_ascii_statement (*st));
5925 return MATCH_ERROR;
5928 /* END INTERFACE has a special handler for its several possible endings. */
5929 if (*st == ST_END_INTERFACE)
5930 return gfc_match_end_interface ();
5932 /* We haven't hit the end of statement, so what is left must be an
5933 end-name. */
5934 m = gfc_match_space ();
5935 if (m == MATCH_YES)
5936 m = gfc_match_name (name);
5938 if (m == MATCH_NO)
5939 gfc_error ("Expected terminating name at %C");
5940 if (m != MATCH_YES)
5941 goto cleanup;
5943 if (block_name == NULL)
5944 goto syntax;
5946 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
5948 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5949 gfc_ascii_statement (*st));
5950 goto cleanup;
5952 /* Procedure pointer as function result. */
5953 else if (strcmp (block_name, "ppr@") == 0
5954 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
5956 gfc_error ("Expected label '%s' for %s statement at %C",
5957 gfc_current_block ()->ns->proc_name->name,
5958 gfc_ascii_statement (*st));
5959 goto cleanup;
5962 if (gfc_match_eos () == MATCH_YES)
5963 return MATCH_YES;
5965 syntax:
5966 gfc_syntax_error (*st);
5968 cleanup:
5969 gfc_current_locus = old_loc;
5970 return MATCH_ERROR;
5975 /***************** Attribute declaration statements ****************/
5977 /* Set the attribute of a single variable. */
5979 static match
5980 attr_decl1 (void)
5982 char name[GFC_MAX_SYMBOL_LEN + 1];
5983 gfc_array_spec *as;
5984 gfc_symbol *sym;
5985 locus var_locus;
5986 match m;
5988 as = NULL;
5990 m = gfc_match_name (name);
5991 if (m != MATCH_YES)
5992 goto cleanup;
5994 if (find_special (name, &sym, false))
5995 return MATCH_ERROR;
5997 var_locus = gfc_current_locus;
5999 /* Deal with possible array specification for certain attributes. */
6000 if (current_attr.dimension
6001 || current_attr.codimension
6002 || current_attr.allocatable
6003 || current_attr.pointer
6004 || current_attr.target)
6006 m = gfc_match_array_spec (&as, !current_attr.codimension,
6007 !current_attr.dimension
6008 && !current_attr.pointer
6009 && !current_attr.target);
6010 if (m == MATCH_ERROR)
6011 goto cleanup;
6013 if (current_attr.dimension && m == MATCH_NO)
6015 gfc_error ("Missing array specification at %L in DIMENSION "
6016 "statement", &var_locus);
6017 m = MATCH_ERROR;
6018 goto cleanup;
6021 if (current_attr.dimension && sym->value)
6023 gfc_error ("Dimensions specified for %s at %L after its "
6024 "initialisation", sym->name, &var_locus);
6025 m = MATCH_ERROR;
6026 goto cleanup;
6029 if (current_attr.codimension && m == MATCH_NO)
6031 gfc_error ("Missing array specification at %L in CODIMENSION "
6032 "statement", &var_locus);
6033 m = MATCH_ERROR;
6034 goto cleanup;
6037 if ((current_attr.allocatable || current_attr.pointer)
6038 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6040 gfc_error ("Array specification must be deferred at %L", &var_locus);
6041 m = MATCH_ERROR;
6042 goto cleanup;
6046 /* Update symbol table. DIMENSION attribute is set in
6047 gfc_set_array_spec(). For CLASS variables, this must be applied
6048 to the first component, or '_data' field. */
6049 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6051 if (gfc_copy_attr (&CLASS_DATA (sym)->attr, &current_attr, &var_locus)
6052 == FAILURE)
6054 m = MATCH_ERROR;
6055 goto cleanup;
6058 else
6060 if (current_attr.dimension == 0 && current_attr.codimension == 0
6061 && gfc_copy_attr (&sym->attr, &current_attr, &var_locus) == FAILURE)
6063 m = MATCH_ERROR;
6064 goto cleanup;
6068 if (sym->ts.type == BT_CLASS
6069 && gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false) == FAILURE)
6071 m = MATCH_ERROR;
6072 goto cleanup;
6075 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
6077 m = MATCH_ERROR;
6078 goto cleanup;
6081 if (sym->attr.cray_pointee && sym->as != NULL)
6083 /* Fix the array spec. */
6084 m = gfc_mod_pointee_as (sym->as);
6085 if (m == MATCH_ERROR)
6086 goto cleanup;
6089 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
6091 m = MATCH_ERROR;
6092 goto cleanup;
6095 if ((current_attr.external || current_attr.intrinsic)
6096 && sym->attr.flavor != FL_PROCEDURE
6097 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6099 m = MATCH_ERROR;
6100 goto cleanup;
6103 add_hidden_procptr_result (sym);
6105 return MATCH_YES;
6107 cleanup:
6108 gfc_free_array_spec (as);
6109 return m;
6113 /* Generic attribute declaration subroutine. Used for attributes that
6114 just have a list of names. */
6116 static match
6117 attr_decl (void)
6119 match m;
6121 /* Gobble the optional double colon, by simply ignoring the result
6122 of gfc_match(). */
6123 gfc_match (" ::");
6125 for (;;)
6127 m = attr_decl1 ();
6128 if (m != MATCH_YES)
6129 break;
6131 if (gfc_match_eos () == MATCH_YES)
6133 m = MATCH_YES;
6134 break;
6137 if (gfc_match_char (',') != MATCH_YES)
6139 gfc_error ("Unexpected character in variable list at %C");
6140 m = MATCH_ERROR;
6141 break;
6145 return m;
6149 /* This routine matches Cray Pointer declarations of the form:
6150 pointer ( <pointer>, <pointee> )
6152 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6153 The pointer, if already declared, should be an integer. Otherwise, we
6154 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6155 be either a scalar, or an array declaration. No space is allocated for
6156 the pointee. For the statement
6157 pointer (ipt, ar(10))
6158 any subsequent uses of ar will be translated (in C-notation) as
6159 ar(i) => ((<type> *) ipt)(i)
6160 After gimplification, pointee variable will disappear in the code. */
6162 static match
6163 cray_pointer_decl (void)
6165 match m;
6166 gfc_array_spec *as = NULL;
6167 gfc_symbol *cptr; /* Pointer symbol. */
6168 gfc_symbol *cpte; /* Pointee symbol. */
6169 locus var_locus;
6170 bool done = false;
6172 while (!done)
6174 if (gfc_match_char ('(') != MATCH_YES)
6176 gfc_error ("Expected '(' at %C");
6177 return MATCH_ERROR;
6180 /* Match pointer. */
6181 var_locus = gfc_current_locus;
6182 gfc_clear_attr (&current_attr);
6183 gfc_add_cray_pointer (&current_attr, &var_locus);
6184 current_ts.type = BT_INTEGER;
6185 current_ts.kind = gfc_index_integer_kind;
6187 m = gfc_match_symbol (&cptr, 0);
6188 if (m != MATCH_YES)
6190 gfc_error ("Expected variable name at %C");
6191 return m;
6194 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
6195 return MATCH_ERROR;
6197 gfc_set_sym_referenced (cptr);
6199 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6201 cptr->ts.type = BT_INTEGER;
6202 cptr->ts.kind = gfc_index_integer_kind;
6204 else if (cptr->ts.type != BT_INTEGER)
6206 gfc_error ("Cray pointer at %C must be an integer");
6207 return MATCH_ERROR;
6209 else if (cptr->ts.kind < gfc_index_integer_kind)
6210 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6211 " memory addresses require %d bytes",
6212 cptr->ts.kind, gfc_index_integer_kind);
6214 if (gfc_match_char (',') != MATCH_YES)
6216 gfc_error ("Expected \",\" at %C");
6217 return MATCH_ERROR;
6220 /* Match Pointee. */
6221 var_locus = gfc_current_locus;
6222 gfc_clear_attr (&current_attr);
6223 gfc_add_cray_pointee (&current_attr, &var_locus);
6224 current_ts.type = BT_UNKNOWN;
6225 current_ts.kind = 0;
6227 m = gfc_match_symbol (&cpte, 0);
6228 if (m != MATCH_YES)
6230 gfc_error ("Expected variable name at %C");
6231 return m;
6234 /* Check for an optional array spec. */
6235 m = gfc_match_array_spec (&as, true, false);
6236 if (m == MATCH_ERROR)
6238 gfc_free_array_spec (as);
6239 return m;
6241 else if (m == MATCH_NO)
6243 gfc_free_array_spec (as);
6244 as = NULL;
6247 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
6248 return MATCH_ERROR;
6250 gfc_set_sym_referenced (cpte);
6252 if (cpte->as == NULL)
6254 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
6255 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6257 else if (as != NULL)
6259 gfc_error ("Duplicate array spec for Cray pointee at %C");
6260 gfc_free_array_spec (as);
6261 return MATCH_ERROR;
6264 as = NULL;
6266 if (cpte->as != NULL)
6268 /* Fix array spec. */
6269 m = gfc_mod_pointee_as (cpte->as);
6270 if (m == MATCH_ERROR)
6271 return m;
6274 /* Point the Pointee at the Pointer. */
6275 cpte->cp_pointer = cptr;
6277 if (gfc_match_char (')') != MATCH_YES)
6279 gfc_error ("Expected \")\" at %C");
6280 return MATCH_ERROR;
6282 m = gfc_match_char (',');
6283 if (m != MATCH_YES)
6284 done = true; /* Stop searching for more declarations. */
6288 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6289 || gfc_match_eos () != MATCH_YES)
6291 gfc_error ("Expected \",\" or end of statement at %C");
6292 return MATCH_ERROR;
6294 return MATCH_YES;
6298 match
6299 gfc_match_external (void)
6302 gfc_clear_attr (&current_attr);
6303 current_attr.external = 1;
6305 return attr_decl ();
6309 match
6310 gfc_match_intent (void)
6312 sym_intent intent;
6314 /* This is not allowed within a BLOCK construct! */
6315 if (gfc_current_state () == COMP_BLOCK)
6317 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6318 return MATCH_ERROR;
6321 intent = match_intent_spec ();
6322 if (intent == INTENT_UNKNOWN)
6323 return MATCH_ERROR;
6325 gfc_clear_attr (&current_attr);
6326 current_attr.intent = intent;
6328 return attr_decl ();
6332 match
6333 gfc_match_intrinsic (void)
6336 gfc_clear_attr (&current_attr);
6337 current_attr.intrinsic = 1;
6339 return attr_decl ();
6343 match
6344 gfc_match_optional (void)
6346 /* This is not allowed within a BLOCK construct! */
6347 if (gfc_current_state () == COMP_BLOCK)
6349 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6350 return MATCH_ERROR;
6353 gfc_clear_attr (&current_attr);
6354 current_attr.optional = 1;
6356 return attr_decl ();
6360 match
6361 gfc_match_pointer (void)
6363 gfc_gobble_whitespace ();
6364 if (gfc_peek_ascii_char () == '(')
6366 if (!gfc_option.flag_cray_pointer)
6368 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6369 "flag");
6370 return MATCH_ERROR;
6372 return cray_pointer_decl ();
6374 else
6376 gfc_clear_attr (&current_attr);
6377 current_attr.pointer = 1;
6379 return attr_decl ();
6384 match
6385 gfc_match_allocatable (void)
6387 gfc_clear_attr (&current_attr);
6388 current_attr.allocatable = 1;
6390 return attr_decl ();
6394 match
6395 gfc_match_codimension (void)
6397 gfc_clear_attr (&current_attr);
6398 current_attr.codimension = 1;
6400 return attr_decl ();
6404 match
6405 gfc_match_contiguous (void)
6407 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTIGUOUS statement at %C")
6408 == FAILURE)
6409 return MATCH_ERROR;
6411 gfc_clear_attr (&current_attr);
6412 current_attr.contiguous = 1;
6414 return attr_decl ();
6418 match
6419 gfc_match_dimension (void)
6421 gfc_clear_attr (&current_attr);
6422 current_attr.dimension = 1;
6424 return attr_decl ();
6428 match
6429 gfc_match_target (void)
6431 gfc_clear_attr (&current_attr);
6432 current_attr.target = 1;
6434 return attr_decl ();
6438 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6439 statement. */
6441 static match
6442 access_attr_decl (gfc_statement st)
6444 char name[GFC_MAX_SYMBOL_LEN + 1];
6445 interface_type type;
6446 gfc_user_op *uop;
6447 gfc_symbol *sym;
6448 gfc_intrinsic_op op;
6449 match m;
6451 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6452 goto done;
6454 for (;;)
6456 m = gfc_match_generic_spec (&type, name, &op);
6457 if (m == MATCH_NO)
6458 goto syntax;
6459 if (m == MATCH_ERROR)
6460 return MATCH_ERROR;
6462 switch (type)
6464 case INTERFACE_NAMELESS:
6465 case INTERFACE_ABSTRACT:
6466 goto syntax;
6468 case INTERFACE_GENERIC:
6469 if (gfc_get_symbol (name, NULL, &sym))
6470 goto done;
6472 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
6473 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6474 sym->name, NULL) == FAILURE)
6475 return MATCH_ERROR;
6477 break;
6479 case INTERFACE_INTRINSIC_OP:
6480 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6482 gfc_intrinsic_op other_op;
6484 gfc_current_ns->operator_access[op] =
6485 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6487 /* Handle the case if there is another op with the same
6488 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6489 other_op = gfc_equivalent_op (op);
6491 if (other_op != INTRINSIC_NONE)
6492 gfc_current_ns->operator_access[other_op] =
6493 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6496 else
6498 gfc_error ("Access specification of the %s operator at %C has "
6499 "already been specified", gfc_op2string (op));
6500 goto done;
6503 break;
6505 case INTERFACE_USER_OP:
6506 uop = gfc_get_uop (name);
6508 if (uop->access == ACCESS_UNKNOWN)
6510 uop->access = (st == ST_PUBLIC)
6511 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6513 else
6515 gfc_error ("Access specification of the .%s. operator at %C "
6516 "has already been specified", sym->name);
6517 goto done;
6520 break;
6523 if (gfc_match_char (',') == MATCH_NO)
6524 break;
6527 if (gfc_match_eos () != MATCH_YES)
6528 goto syntax;
6529 return MATCH_YES;
6531 syntax:
6532 gfc_syntax_error (st);
6534 done:
6535 return MATCH_ERROR;
6539 match
6540 gfc_match_protected (void)
6542 gfc_symbol *sym;
6543 match m;
6545 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6547 gfc_error ("PROTECTED at %C only allowed in specification "
6548 "part of a module");
6549 return MATCH_ERROR;
6553 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
6554 == FAILURE)
6555 return MATCH_ERROR;
6557 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6559 return MATCH_ERROR;
6562 if (gfc_match_eos () == MATCH_YES)
6563 goto syntax;
6565 for(;;)
6567 m = gfc_match_symbol (&sym, 0);
6568 switch (m)
6570 case MATCH_YES:
6571 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6572 == FAILURE)
6573 return MATCH_ERROR;
6574 goto next_item;
6576 case MATCH_NO:
6577 break;
6579 case MATCH_ERROR:
6580 return MATCH_ERROR;
6583 next_item:
6584 if (gfc_match_eos () == MATCH_YES)
6585 break;
6586 if (gfc_match_char (',') != MATCH_YES)
6587 goto syntax;
6590 return MATCH_YES;
6592 syntax:
6593 gfc_error ("Syntax error in PROTECTED statement at %C");
6594 return MATCH_ERROR;
6598 /* The PRIVATE statement is a bit weird in that it can be an attribute
6599 declaration, but also works as a standalone statement inside of a
6600 type declaration or a module. */
6602 match
6603 gfc_match_private (gfc_statement *st)
6606 if (gfc_match ("private") != MATCH_YES)
6607 return MATCH_NO;
6609 if (gfc_current_state () != COMP_MODULE
6610 && !(gfc_current_state () == COMP_DERIVED
6611 && gfc_state_stack->previous
6612 && gfc_state_stack->previous->state == COMP_MODULE)
6613 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6614 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6615 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6617 gfc_error ("PRIVATE statement at %C is only allowed in the "
6618 "specification part of a module");
6619 return MATCH_ERROR;
6622 if (gfc_current_state () == COMP_DERIVED)
6624 if (gfc_match_eos () == MATCH_YES)
6626 *st = ST_PRIVATE;
6627 return MATCH_YES;
6630 gfc_syntax_error (ST_PRIVATE);
6631 return MATCH_ERROR;
6634 if (gfc_match_eos () == MATCH_YES)
6636 *st = ST_PRIVATE;
6637 return MATCH_YES;
6640 *st = ST_ATTR_DECL;
6641 return access_attr_decl (ST_PRIVATE);
6645 match
6646 gfc_match_public (gfc_statement *st)
6649 if (gfc_match ("public") != MATCH_YES)
6650 return MATCH_NO;
6652 if (gfc_current_state () != COMP_MODULE)
6654 gfc_error ("PUBLIC statement at %C is only allowed in the "
6655 "specification part of a module");
6656 return MATCH_ERROR;
6659 if (gfc_match_eos () == MATCH_YES)
6661 *st = ST_PUBLIC;
6662 return MATCH_YES;
6665 *st = ST_ATTR_DECL;
6666 return access_attr_decl (ST_PUBLIC);
6670 /* Workhorse for gfc_match_parameter. */
6672 static match
6673 do_parm (void)
6675 gfc_symbol *sym;
6676 gfc_expr *init;
6677 match m;
6678 gfc_try t;
6680 m = gfc_match_symbol (&sym, 0);
6681 if (m == MATCH_NO)
6682 gfc_error ("Expected variable name at %C in PARAMETER statement");
6684 if (m != MATCH_YES)
6685 return m;
6687 if (gfc_match_char ('=') == MATCH_NO)
6689 gfc_error ("Expected = sign in PARAMETER statement at %C");
6690 return MATCH_ERROR;
6693 m = gfc_match_init_expr (&init);
6694 if (m == MATCH_NO)
6695 gfc_error ("Expected expression at %C in PARAMETER statement");
6696 if (m != MATCH_YES)
6697 return m;
6699 if (sym->ts.type == BT_UNKNOWN
6700 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6702 m = MATCH_ERROR;
6703 goto cleanup;
6706 if (gfc_check_assign_symbol (sym, init) == FAILURE
6707 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6709 m = MATCH_ERROR;
6710 goto cleanup;
6713 if (sym->value)
6715 gfc_error ("Initializing already initialized variable at %C");
6716 m = MATCH_ERROR;
6717 goto cleanup;
6720 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
6721 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6723 cleanup:
6724 gfc_free_expr (init);
6725 return m;
6729 /* Match a parameter statement, with the weird syntax that these have. */
6731 match
6732 gfc_match_parameter (void)
6734 match m;
6736 if (gfc_match_char ('(') == MATCH_NO)
6737 return MATCH_NO;
6739 for (;;)
6741 m = do_parm ();
6742 if (m != MATCH_YES)
6743 break;
6745 if (gfc_match (" )%t") == MATCH_YES)
6746 break;
6748 if (gfc_match_char (',') != MATCH_YES)
6750 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6751 m = MATCH_ERROR;
6752 break;
6756 return m;
6760 /* Save statements have a special syntax. */
6762 match
6763 gfc_match_save (void)
6765 char n[GFC_MAX_SYMBOL_LEN+1];
6766 gfc_common_head *c;
6767 gfc_symbol *sym;
6768 match m;
6770 if (gfc_match_eos () == MATCH_YES)
6772 if (gfc_current_ns->seen_save)
6774 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6775 "follows previous SAVE statement")
6776 == FAILURE)
6777 return MATCH_ERROR;
6780 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6781 return MATCH_YES;
6784 if (gfc_current_ns->save_all)
6786 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6787 "blanket SAVE statement")
6788 == FAILURE)
6789 return MATCH_ERROR;
6792 gfc_match (" ::");
6794 for (;;)
6796 m = gfc_match_symbol (&sym, 0);
6797 switch (m)
6799 case MATCH_YES:
6800 if (gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
6801 &gfc_current_locus) == FAILURE)
6802 return MATCH_ERROR;
6803 goto next_item;
6805 case MATCH_NO:
6806 break;
6808 case MATCH_ERROR:
6809 return MATCH_ERROR;
6812 m = gfc_match (" / %n /", &n);
6813 if (m == MATCH_ERROR)
6814 return MATCH_ERROR;
6815 if (m == MATCH_NO)
6816 goto syntax;
6818 c = gfc_get_common (n, 0);
6819 c->saved = 1;
6821 gfc_current_ns->seen_save = 1;
6823 next_item:
6824 if (gfc_match_eos () == MATCH_YES)
6825 break;
6826 if (gfc_match_char (',') != MATCH_YES)
6827 goto syntax;
6830 return MATCH_YES;
6832 syntax:
6833 gfc_error ("Syntax error in SAVE statement at %C");
6834 return MATCH_ERROR;
6838 match
6839 gfc_match_value (void)
6841 gfc_symbol *sym;
6842 match m;
6844 /* This is not allowed within a BLOCK construct! */
6845 if (gfc_current_state () == COMP_BLOCK)
6847 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
6848 return MATCH_ERROR;
6851 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
6852 == FAILURE)
6853 return MATCH_ERROR;
6855 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6857 return MATCH_ERROR;
6860 if (gfc_match_eos () == MATCH_YES)
6861 goto syntax;
6863 for(;;)
6865 m = gfc_match_symbol (&sym, 0);
6866 switch (m)
6868 case MATCH_YES:
6869 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
6870 == FAILURE)
6871 return MATCH_ERROR;
6872 goto next_item;
6874 case MATCH_NO:
6875 break;
6877 case MATCH_ERROR:
6878 return MATCH_ERROR;
6881 next_item:
6882 if (gfc_match_eos () == MATCH_YES)
6883 break;
6884 if (gfc_match_char (',') != MATCH_YES)
6885 goto syntax;
6888 return MATCH_YES;
6890 syntax:
6891 gfc_error ("Syntax error in VALUE statement at %C");
6892 return MATCH_ERROR;
6896 match
6897 gfc_match_volatile (void)
6899 gfc_symbol *sym;
6900 match m;
6902 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
6903 == FAILURE)
6904 return MATCH_ERROR;
6906 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6908 return MATCH_ERROR;
6911 if (gfc_match_eos () == MATCH_YES)
6912 goto syntax;
6914 for(;;)
6916 /* VOLATILE is special because it can be added to host-associated
6917 symbols locally. Except for coarrays. */
6918 m = gfc_match_symbol (&sym, 1);
6919 switch (m)
6921 case MATCH_YES:
6922 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
6923 for variable in a BLOCK which is defined outside of the BLOCK. */
6924 if (sym->ns != gfc_current_ns && sym->attr.codimension)
6926 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
6927 "%C, which is use-/host-associated", sym->name);
6928 return MATCH_ERROR;
6930 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6931 == FAILURE)
6932 return MATCH_ERROR;
6933 goto next_item;
6935 case MATCH_NO:
6936 break;
6938 case MATCH_ERROR:
6939 return MATCH_ERROR;
6942 next_item:
6943 if (gfc_match_eos () == MATCH_YES)
6944 break;
6945 if (gfc_match_char (',') != MATCH_YES)
6946 goto syntax;
6949 return MATCH_YES;
6951 syntax:
6952 gfc_error ("Syntax error in VOLATILE statement at %C");
6953 return MATCH_ERROR;
6957 match
6958 gfc_match_asynchronous (void)
6960 gfc_symbol *sym;
6961 match m;
6963 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS statement at %C")
6964 == FAILURE)
6965 return MATCH_ERROR;
6967 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6969 return MATCH_ERROR;
6972 if (gfc_match_eos () == MATCH_YES)
6973 goto syntax;
6975 for(;;)
6977 /* ASYNCHRONOUS is special because it can be added to host-associated
6978 symbols locally. */
6979 m = gfc_match_symbol (&sym, 1);
6980 switch (m)
6982 case MATCH_YES:
6983 if (gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus)
6984 == FAILURE)
6985 return MATCH_ERROR;
6986 goto next_item;
6988 case MATCH_NO:
6989 break;
6991 case MATCH_ERROR:
6992 return MATCH_ERROR;
6995 next_item:
6996 if (gfc_match_eos () == MATCH_YES)
6997 break;
6998 if (gfc_match_char (',') != MATCH_YES)
6999 goto syntax;
7002 return MATCH_YES;
7004 syntax:
7005 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7006 return MATCH_ERROR;
7010 /* Match a module procedure statement. Note that we have to modify
7011 symbols in the parent's namespace because the current one was there
7012 to receive symbols that are in an interface's formal argument list. */
7014 match
7015 gfc_match_modproc (void)
7017 char name[GFC_MAX_SYMBOL_LEN + 1];
7018 gfc_symbol *sym;
7019 match m;
7020 locus old_locus;
7021 gfc_namespace *module_ns;
7022 gfc_interface *old_interface_head, *interface;
7024 if (gfc_state_stack->state != COMP_INTERFACE
7025 || gfc_state_stack->previous == NULL
7026 || current_interface.type == INTERFACE_NAMELESS
7027 || current_interface.type == INTERFACE_ABSTRACT)
7029 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7030 "interface");
7031 return MATCH_ERROR;
7034 module_ns = gfc_current_ns->parent;
7035 for (; module_ns; module_ns = module_ns->parent)
7036 if (module_ns->proc_name->attr.flavor == FL_MODULE
7037 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7038 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7039 && !module_ns->proc_name->attr.contained))
7040 break;
7042 if (module_ns == NULL)
7043 return MATCH_ERROR;
7045 /* Store the current state of the interface. We will need it if we
7046 end up with a syntax error and need to recover. */
7047 old_interface_head = gfc_current_interface_head ();
7049 /* Check if the F2008 optional double colon appears. */
7050 gfc_gobble_whitespace ();
7051 old_locus = gfc_current_locus;
7052 if (gfc_match ("::") == MATCH_YES)
7054 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: double colon in "
7055 "MODULE PROCEDURE statement at %L", &old_locus)
7056 == FAILURE)
7057 return MATCH_ERROR;
7059 else
7060 gfc_current_locus = old_locus;
7062 for (;;)
7064 bool last = false;
7065 old_locus = gfc_current_locus;
7067 m = gfc_match_name (name);
7068 if (m == MATCH_NO)
7069 goto syntax;
7070 if (m != MATCH_YES)
7071 return MATCH_ERROR;
7073 /* Check for syntax error before starting to add symbols to the
7074 current namespace. */
7075 if (gfc_match_eos () == MATCH_YES)
7076 last = true;
7078 if (!last && gfc_match_char (',') != MATCH_YES)
7079 goto syntax;
7081 /* Now we're sure the syntax is valid, we process this item
7082 further. */
7083 if (gfc_get_symbol (name, module_ns, &sym))
7084 return MATCH_ERROR;
7086 if (sym->attr.intrinsic)
7088 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7089 "PROCEDURE", &old_locus);
7090 return MATCH_ERROR;
7093 if (sym->attr.proc != PROC_MODULE
7094 && gfc_add_procedure (&sym->attr, PROC_MODULE,
7095 sym->name, NULL) == FAILURE)
7096 return MATCH_ERROR;
7098 if (gfc_add_interface (sym) == FAILURE)
7099 return MATCH_ERROR;
7101 sym->attr.mod_proc = 1;
7102 sym->declared_at = old_locus;
7104 if (last)
7105 break;
7108 return MATCH_YES;
7110 syntax:
7111 /* Restore the previous state of the interface. */
7112 interface = gfc_current_interface_head ();
7113 gfc_set_current_interface_head (old_interface_head);
7115 /* Free the new interfaces. */
7116 while (interface != old_interface_head)
7118 gfc_interface *i = interface->next;
7119 free (interface);
7120 interface = i;
7123 /* And issue a syntax error. */
7124 gfc_syntax_error (ST_MODULE_PROC);
7125 return MATCH_ERROR;
7129 /* Check a derived type that is being extended. */
7130 static gfc_symbol*
7131 check_extended_derived_type (char *name)
7133 gfc_symbol *extended;
7135 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7137 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7138 return NULL;
7141 if (!extended)
7143 gfc_error ("No such symbol in TYPE definition at %C");
7144 return NULL;
7147 if (extended->attr.flavor != FL_DERIVED)
7149 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7150 "derived type", name);
7151 return NULL;
7154 if (extended->attr.is_bind_c)
7156 gfc_error ("'%s' cannot be extended at %C because it "
7157 "is BIND(C)", extended->name);
7158 return NULL;
7161 if (extended->attr.sequence)
7163 gfc_error ("'%s' cannot be extended at %C because it "
7164 "is a SEQUENCE type", extended->name);
7165 return NULL;
7168 return extended;
7172 /* Match the optional attribute specifiers for a type declaration.
7173 Return MATCH_ERROR if an error is encountered in one of the handled
7174 attributes (public, private, bind(c)), MATCH_NO if what's found is
7175 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7176 checking on attribute conflicts needs to be done. */
7178 match
7179 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7181 /* See if the derived type is marked as private. */
7182 if (gfc_match (" , private") == MATCH_YES)
7184 if (gfc_current_state () != COMP_MODULE)
7186 gfc_error ("Derived type at %C can only be PRIVATE in the "
7187 "specification part of a module");
7188 return MATCH_ERROR;
7191 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
7192 return MATCH_ERROR;
7194 else if (gfc_match (" , public") == MATCH_YES)
7196 if (gfc_current_state () != COMP_MODULE)
7198 gfc_error ("Derived type at %C can only be PUBLIC in the "
7199 "specification part of a module");
7200 return MATCH_ERROR;
7203 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
7204 return MATCH_ERROR;
7206 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7208 /* If the type is defined to be bind(c) it then needs to make
7209 sure that all fields are interoperable. This will
7210 need to be a semantic check on the finished derived type.
7211 See 15.2.3 (lines 9-12) of F2003 draft. */
7212 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
7213 return MATCH_ERROR;
7215 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7217 else if (gfc_match (" , abstract") == MATCH_YES)
7219 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
7220 == FAILURE)
7221 return MATCH_ERROR;
7223 if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
7224 return MATCH_ERROR;
7226 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
7228 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
7229 return MATCH_ERROR;
7231 else
7232 return MATCH_NO;
7234 /* If we get here, something matched. */
7235 return MATCH_YES;
7239 /* Match the beginning of a derived type declaration. If a type name
7240 was the result of a function, then it is possible to have a symbol
7241 already to be known as a derived type yet have no components. */
7243 match
7244 gfc_match_derived_decl (void)
7246 char name[GFC_MAX_SYMBOL_LEN + 1];
7247 char parent[GFC_MAX_SYMBOL_LEN + 1];
7248 symbol_attribute attr;
7249 gfc_symbol *sym;
7250 gfc_symbol *extended;
7251 match m;
7252 match is_type_attr_spec = MATCH_NO;
7253 bool seen_attr = false;
7255 if (gfc_current_state () == COMP_DERIVED)
7256 return MATCH_NO;
7258 name[0] = '\0';
7259 parent[0] = '\0';
7260 gfc_clear_attr (&attr);
7261 extended = NULL;
7265 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7266 if (is_type_attr_spec == MATCH_ERROR)
7267 return MATCH_ERROR;
7268 if (is_type_attr_spec == MATCH_YES)
7269 seen_attr = true;
7270 } while (is_type_attr_spec == MATCH_YES);
7272 /* Deal with derived type extensions. The extension attribute has
7273 been added to 'attr' but now the parent type must be found and
7274 checked. */
7275 if (parent[0])
7276 extended = check_extended_derived_type (parent);
7278 if (parent[0] && !extended)
7279 return MATCH_ERROR;
7281 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7283 gfc_error ("Expected :: in TYPE definition at %C");
7284 return MATCH_ERROR;
7287 m = gfc_match (" %n%t", name);
7288 if (m != MATCH_YES)
7289 return m;
7291 /* Make sure the name is not the name of an intrinsic type. */
7292 if (gfc_is_intrinsic_typename (name))
7294 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7295 "type", name);
7296 return MATCH_ERROR;
7299 if (gfc_get_symbol (name, NULL, &sym))
7300 return MATCH_ERROR;
7302 if (sym->ts.type != BT_UNKNOWN)
7304 gfc_error ("Derived type name '%s' at %C already has a basic type "
7305 "of %s", sym->name, gfc_typename (&sym->ts));
7306 return MATCH_ERROR;
7309 /* The symbol may already have the derived attribute without the
7310 components. The ways this can happen is via a function
7311 definition, an INTRINSIC statement or a subtype in another
7312 derived type that is a pointer. The first part of the AND clause
7313 is true if the symbol is not the return value of a function. */
7314 if (sym->attr.flavor != FL_DERIVED
7315 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
7316 return MATCH_ERROR;
7318 if (sym->components != NULL || sym->attr.zero_comp)
7320 gfc_error ("Derived type definition of '%s' at %C has already been "
7321 "defined", sym->name);
7322 return MATCH_ERROR;
7325 if (attr.access != ACCESS_UNKNOWN
7326 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
7327 return MATCH_ERROR;
7329 /* See if the derived type was labeled as bind(c). */
7330 if (attr.is_bind_c != 0)
7331 sym->attr.is_bind_c = attr.is_bind_c;
7333 /* Construct the f2k_derived namespace if it is not yet there. */
7334 if (!sym->f2k_derived)
7335 sym->f2k_derived = gfc_get_namespace (NULL, 0);
7337 if (extended && !sym->components)
7339 gfc_component *p;
7340 gfc_symtree *st;
7342 /* Add the extended derived type as the first component. */
7343 gfc_add_component (sym, parent, &p);
7344 extended->refs++;
7345 gfc_set_sym_referenced (extended);
7347 p->ts.type = BT_DERIVED;
7348 p->ts.u.derived = extended;
7349 p->initializer = gfc_default_initializer (&p->ts);
7351 /* Set extension level. */
7352 if (extended->attr.extension == 255)
7354 /* Since the extension field is 8 bit wide, we can only have
7355 up to 255 extension levels. */
7356 gfc_error ("Maximum extension level reached with type '%s' at %L",
7357 extended->name, &extended->declared_at);
7358 return MATCH_ERROR;
7360 sym->attr.extension = extended->attr.extension + 1;
7362 /* Provide the links between the extended type and its extension. */
7363 if (!extended->f2k_derived)
7364 extended->f2k_derived = gfc_get_namespace (NULL, 0);
7365 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7366 st->n.sym = sym;
7369 if (!sym->hash_value)
7370 /* Set the hash for the compound name for this type. */
7371 sym->hash_value = gfc_hash_value (sym);
7373 /* Take over the ABSTRACT attribute. */
7374 sym->attr.abstract = attr.abstract;
7376 gfc_new_block = sym;
7378 return MATCH_YES;
7382 /* Cray Pointees can be declared as:
7383 pointer (ipt, a (n,m,...,*)) */
7385 match
7386 gfc_mod_pointee_as (gfc_array_spec *as)
7388 as->cray_pointee = true; /* This will be useful to know later. */
7389 if (as->type == AS_ASSUMED_SIZE)
7390 as->cp_was_assumed = true;
7391 else if (as->type == AS_ASSUMED_SHAPE)
7393 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7394 return MATCH_ERROR;
7396 return MATCH_YES;
7400 /* Match the enum definition statement, here we are trying to match
7401 the first line of enum definition statement.
7402 Returns MATCH_YES if match is found. */
7404 match
7405 gfc_match_enum (void)
7407 match m;
7409 m = gfc_match_eos ();
7410 if (m != MATCH_YES)
7411 return m;
7413 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
7414 == FAILURE)
7415 return MATCH_ERROR;
7417 return MATCH_YES;
7421 /* Returns an initializer whose value is one higher than the value of the
7422 LAST_INITIALIZER argument. If the argument is NULL, the
7423 initializers value will be set to zero. The initializer's kind
7424 will be set to gfc_c_int_kind.
7426 If -fshort-enums is given, the appropriate kind will be selected
7427 later after all enumerators have been parsed. A warning is issued
7428 here if an initializer exceeds gfc_c_int_kind. */
7430 static gfc_expr *
7431 enum_initializer (gfc_expr *last_initializer, locus where)
7433 gfc_expr *result;
7434 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7436 mpz_init (result->value.integer);
7438 if (last_initializer != NULL)
7440 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7441 result->where = last_initializer->where;
7443 if (gfc_check_integer_range (result->value.integer,
7444 gfc_c_int_kind) != ARITH_OK)
7446 gfc_error ("Enumerator exceeds the C integer type at %C");
7447 return NULL;
7450 else
7452 /* Control comes here, if it's the very first enumerator and no
7453 initializer has been given. It will be initialized to zero. */
7454 mpz_set_si (result->value.integer, 0);
7457 return result;
7461 /* Match a variable name with an optional initializer. When this
7462 subroutine is called, a variable is expected to be parsed next.
7463 Depending on what is happening at the moment, updates either the
7464 symbol table or the current interface. */
7466 static match
7467 enumerator_decl (void)
7469 char name[GFC_MAX_SYMBOL_LEN + 1];
7470 gfc_expr *initializer;
7471 gfc_array_spec *as = NULL;
7472 gfc_symbol *sym;
7473 locus var_locus;
7474 match m;
7475 gfc_try t;
7476 locus old_locus;
7478 initializer = NULL;
7479 old_locus = gfc_current_locus;
7481 /* When we get here, we've just matched a list of attributes and
7482 maybe a type and a double colon. The next thing we expect to see
7483 is the name of the symbol. */
7484 m = gfc_match_name (name);
7485 if (m != MATCH_YES)
7486 goto cleanup;
7488 var_locus = gfc_current_locus;
7490 /* OK, we've successfully matched the declaration. Now put the
7491 symbol in the current namespace. If we fail to create the symbol,
7492 bail out. */
7493 if (build_sym (name, NULL, false, &as, &var_locus) == FAILURE)
7495 m = MATCH_ERROR;
7496 goto cleanup;
7499 /* The double colon must be present in order to have initializers.
7500 Otherwise the statement is ambiguous with an assignment statement. */
7501 if (colon_seen)
7503 if (gfc_match_char ('=') == MATCH_YES)
7505 m = gfc_match_init_expr (&initializer);
7506 if (m == MATCH_NO)
7508 gfc_error ("Expected an initialization expression at %C");
7509 m = MATCH_ERROR;
7512 if (m != MATCH_YES)
7513 goto cleanup;
7517 /* If we do not have an initializer, the initialization value of the
7518 previous enumerator (stored in last_initializer) is incremented
7519 by 1 and is used to initialize the current enumerator. */
7520 if (initializer == NULL)
7521 initializer = enum_initializer (last_initializer, old_locus);
7523 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7525 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7526 &var_locus);
7527 m = MATCH_ERROR;
7528 goto cleanup;
7531 /* Store this current initializer, for the next enumerator variable
7532 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7533 use last_initializer below. */
7534 last_initializer = initializer;
7535 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7537 /* Maintain enumerator history. */
7538 gfc_find_symbol (name, NULL, 0, &sym);
7539 create_enum_history (sym, last_initializer);
7541 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
7543 cleanup:
7544 /* Free stuff up and return. */
7545 gfc_free_expr (initializer);
7547 return m;
7551 /* Match the enumerator definition statement. */
7553 match
7554 gfc_match_enumerator_def (void)
7556 match m;
7557 gfc_try t;
7559 gfc_clear_ts (&current_ts);
7561 m = gfc_match (" enumerator");
7562 if (m != MATCH_YES)
7563 return m;
7565 m = gfc_match (" :: ");
7566 if (m == MATCH_ERROR)
7567 return m;
7569 colon_seen = (m == MATCH_YES);
7571 if (gfc_current_state () != COMP_ENUM)
7573 gfc_error ("ENUM definition statement expected before %C");
7574 gfc_free_enum_history ();
7575 return MATCH_ERROR;
7578 (&current_ts)->type = BT_INTEGER;
7579 (&current_ts)->kind = gfc_c_int_kind;
7581 gfc_clear_attr (&current_attr);
7582 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
7583 if (t == FAILURE)
7585 m = MATCH_ERROR;
7586 goto cleanup;
7589 for (;;)
7591 m = enumerator_decl ();
7592 if (m == MATCH_ERROR)
7594 gfc_free_enum_history ();
7595 goto cleanup;
7597 if (m == MATCH_NO)
7598 break;
7600 if (gfc_match_eos () == MATCH_YES)
7601 goto cleanup;
7602 if (gfc_match_char (',') != MATCH_YES)
7603 break;
7606 if (gfc_current_state () == COMP_ENUM)
7608 gfc_free_enum_history ();
7609 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7610 m = MATCH_ERROR;
7613 cleanup:
7614 gfc_free_array_spec (current_as);
7615 current_as = NULL;
7616 return m;
7621 /* Match binding attributes. */
7623 static match
7624 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7626 bool found_passing = false;
7627 bool seen_ptr = false;
7628 match m = MATCH_YES;
7630 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7631 this case the defaults are in there. */
7632 ba->access = ACCESS_UNKNOWN;
7633 ba->pass_arg = NULL;
7634 ba->pass_arg_num = 0;
7635 ba->nopass = 0;
7636 ba->non_overridable = 0;
7637 ba->deferred = 0;
7638 ba->ppc = ppc;
7640 /* If we find a comma, we believe there are binding attributes. */
7641 m = gfc_match_char (',');
7642 if (m == MATCH_NO)
7643 goto done;
7647 /* Access specifier. */
7649 m = gfc_match (" public");
7650 if (m == MATCH_ERROR)
7651 goto error;
7652 if (m == MATCH_YES)
7654 if (ba->access != ACCESS_UNKNOWN)
7656 gfc_error ("Duplicate access-specifier at %C");
7657 goto error;
7660 ba->access = ACCESS_PUBLIC;
7661 continue;
7664 m = gfc_match (" private");
7665 if (m == MATCH_ERROR)
7666 goto error;
7667 if (m == MATCH_YES)
7669 if (ba->access != ACCESS_UNKNOWN)
7671 gfc_error ("Duplicate access-specifier at %C");
7672 goto error;
7675 ba->access = ACCESS_PRIVATE;
7676 continue;
7679 /* If inside GENERIC, the following is not allowed. */
7680 if (!generic)
7683 /* NOPASS flag. */
7684 m = gfc_match (" nopass");
7685 if (m == MATCH_ERROR)
7686 goto error;
7687 if (m == MATCH_YES)
7689 if (found_passing)
7691 gfc_error ("Binding attributes already specify passing,"
7692 " illegal NOPASS at %C");
7693 goto error;
7696 found_passing = true;
7697 ba->nopass = 1;
7698 continue;
7701 /* PASS possibly including argument. */
7702 m = gfc_match (" pass");
7703 if (m == MATCH_ERROR)
7704 goto error;
7705 if (m == MATCH_YES)
7707 char arg[GFC_MAX_SYMBOL_LEN + 1];
7709 if (found_passing)
7711 gfc_error ("Binding attributes already specify passing,"
7712 " illegal PASS at %C");
7713 goto error;
7716 m = gfc_match (" ( %n )", arg);
7717 if (m == MATCH_ERROR)
7718 goto error;
7719 if (m == MATCH_YES)
7720 ba->pass_arg = gfc_get_string (arg);
7721 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7723 found_passing = true;
7724 ba->nopass = 0;
7725 continue;
7728 if (ppc)
7730 /* POINTER flag. */
7731 m = gfc_match (" pointer");
7732 if (m == MATCH_ERROR)
7733 goto error;
7734 if (m == MATCH_YES)
7736 if (seen_ptr)
7738 gfc_error ("Duplicate POINTER attribute at %C");
7739 goto error;
7742 seen_ptr = true;
7743 continue;
7746 else
7748 /* NON_OVERRIDABLE flag. */
7749 m = gfc_match (" non_overridable");
7750 if (m == MATCH_ERROR)
7751 goto error;
7752 if (m == MATCH_YES)
7754 if (ba->non_overridable)
7756 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7757 goto error;
7760 ba->non_overridable = 1;
7761 continue;
7764 /* DEFERRED flag. */
7765 m = gfc_match (" deferred");
7766 if (m == MATCH_ERROR)
7767 goto error;
7768 if (m == MATCH_YES)
7770 if (ba->deferred)
7772 gfc_error ("Duplicate DEFERRED at %C");
7773 goto error;
7776 ba->deferred = 1;
7777 continue;
7783 /* Nothing matching found. */
7784 if (generic)
7785 gfc_error ("Expected access-specifier at %C");
7786 else
7787 gfc_error ("Expected binding attribute at %C");
7788 goto error;
7790 while (gfc_match_char (',') == MATCH_YES);
7792 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
7793 if (ba->non_overridable && ba->deferred)
7795 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7796 goto error;
7799 m = MATCH_YES;
7801 done:
7802 if (ba->access == ACCESS_UNKNOWN)
7803 ba->access = gfc_typebound_default_access;
7805 if (ppc && !seen_ptr)
7807 gfc_error ("POINTER attribute is required for procedure pointer component"
7808 " at %C");
7809 goto error;
7812 return m;
7814 error:
7815 return MATCH_ERROR;
7819 /* Match a PROCEDURE specific binding inside a derived type. */
7821 static match
7822 match_procedure_in_type (void)
7824 char name[GFC_MAX_SYMBOL_LEN + 1];
7825 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
7826 char* target = NULL, *ifc = NULL;
7827 gfc_typebound_proc tb;
7828 bool seen_colons;
7829 bool seen_attrs;
7830 match m;
7831 gfc_symtree* stree;
7832 gfc_namespace* ns;
7833 gfc_symbol* block;
7834 int num;
7836 /* Check current state. */
7837 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
7838 block = gfc_state_stack->previous->sym;
7839 gcc_assert (block);
7841 /* Try to match PROCEDURE(interface). */
7842 if (gfc_match (" (") == MATCH_YES)
7844 m = gfc_match_name (target_buf);
7845 if (m == MATCH_ERROR)
7846 return m;
7847 if (m != MATCH_YES)
7849 gfc_error ("Interface-name expected after '(' at %C");
7850 return MATCH_ERROR;
7853 if (gfc_match (" )") != MATCH_YES)
7855 gfc_error ("')' expected at %C");
7856 return MATCH_ERROR;
7859 ifc = target_buf;
7862 /* Construct the data structure. */
7863 memset (&tb, 0, sizeof (tb));
7864 tb.where = gfc_current_locus;
7866 /* Match binding attributes. */
7867 m = match_binding_attributes (&tb, false, false);
7868 if (m == MATCH_ERROR)
7869 return m;
7870 seen_attrs = (m == MATCH_YES);
7872 /* Check that attribute DEFERRED is given if an interface is specified. */
7873 if (tb.deferred && !ifc)
7875 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7876 return MATCH_ERROR;
7878 if (ifc && !tb.deferred)
7880 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7881 return MATCH_ERROR;
7884 /* Match the colons. */
7885 m = gfc_match (" ::");
7886 if (m == MATCH_ERROR)
7887 return m;
7888 seen_colons = (m == MATCH_YES);
7889 if (seen_attrs && !seen_colons)
7891 gfc_error ("Expected '::' after binding-attributes at %C");
7892 return MATCH_ERROR;
7895 /* Match the binding names. */
7896 for(num=1;;num++)
7898 m = gfc_match_name (name);
7899 if (m == MATCH_ERROR)
7900 return m;
7901 if (m == MATCH_NO)
7903 gfc_error ("Expected binding name at %C");
7904 return MATCH_ERROR;
7907 if (num>1 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: PROCEDURE list"
7908 " at %C") == FAILURE)
7909 return MATCH_ERROR;
7911 /* Try to match the '=> target', if it's there. */
7912 target = ifc;
7913 m = gfc_match (" =>");
7914 if (m == MATCH_ERROR)
7915 return m;
7916 if (m == MATCH_YES)
7918 if (tb.deferred)
7920 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7921 return MATCH_ERROR;
7924 if (!seen_colons)
7926 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7927 " at %C");
7928 return MATCH_ERROR;
7931 m = gfc_match_name (target_buf);
7932 if (m == MATCH_ERROR)
7933 return m;
7934 if (m == MATCH_NO)
7936 gfc_error ("Expected binding target after '=>' at %C");
7937 return MATCH_ERROR;
7939 target = target_buf;
7942 /* If no target was found, it has the same name as the binding. */
7943 if (!target)
7944 target = name;
7946 /* Get the namespace to insert the symbols into. */
7947 ns = block->f2k_derived;
7948 gcc_assert (ns);
7950 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7951 if (tb.deferred && !block->attr.abstract)
7953 gfc_error ("Type '%s' containing DEFERRED binding at %C "
7954 "is not ABSTRACT", block->name);
7955 return MATCH_ERROR;
7958 /* See if we already have a binding with this name in the symtree which
7959 would be an error. If a GENERIC already targetted this binding, it may
7960 be already there but then typebound is still NULL. */
7961 stree = gfc_find_symtree (ns->tb_sym_root, name);
7962 if (stree && stree->n.tb)
7964 gfc_error ("There is already a procedure with binding name '%s' for "
7965 "the derived type '%s' at %C", name, block->name);
7966 return MATCH_ERROR;
7969 /* Insert it and set attributes. */
7971 if (!stree)
7973 stree = gfc_new_symtree (&ns->tb_sym_root, name);
7974 gcc_assert (stree);
7976 stree->n.tb = gfc_get_typebound_proc (&tb);
7978 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
7979 false))
7980 return MATCH_ERROR;
7981 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
7983 if (gfc_match_eos () == MATCH_YES)
7984 return MATCH_YES;
7985 if (gfc_match_char (',') != MATCH_YES)
7986 goto syntax;
7989 syntax:
7990 gfc_error ("Syntax error in PROCEDURE statement at %C");
7991 return MATCH_ERROR;
7995 /* Match a GENERIC procedure binding inside a derived type. */
7997 match
7998 gfc_match_generic (void)
8000 char name[GFC_MAX_SYMBOL_LEN + 1];
8001 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
8002 gfc_symbol* block;
8003 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8004 gfc_typebound_proc* tb;
8005 gfc_namespace* ns;
8006 interface_type op_type;
8007 gfc_intrinsic_op op;
8008 match m;
8010 /* Check current state. */
8011 if (gfc_current_state () == COMP_DERIVED)
8013 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8014 return MATCH_ERROR;
8016 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8017 return MATCH_NO;
8018 block = gfc_state_stack->previous->sym;
8019 ns = block->f2k_derived;
8020 gcc_assert (block && ns);
8022 memset (&tbattr, 0, sizeof (tbattr));
8023 tbattr.where = gfc_current_locus;
8025 /* See if we get an access-specifier. */
8026 m = match_binding_attributes (&tbattr, true, false);
8027 if (m == MATCH_ERROR)
8028 goto error;
8030 /* Now the colons, those are required. */
8031 if (gfc_match (" ::") != MATCH_YES)
8033 gfc_error ("Expected '::' at %C");
8034 goto error;
8037 /* Match the binding name; depending on type (operator / generic) format
8038 it for future error messages into bind_name. */
8040 m = gfc_match_generic_spec (&op_type, name, &op);
8041 if (m == MATCH_ERROR)
8042 return MATCH_ERROR;
8043 if (m == MATCH_NO)
8045 gfc_error ("Expected generic name or operator descriptor at %C");
8046 goto error;
8049 switch (op_type)
8051 case INTERFACE_GENERIC:
8052 snprintf (bind_name, sizeof (bind_name), "%s", name);
8053 break;
8055 case INTERFACE_USER_OP:
8056 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8057 break;
8059 case INTERFACE_INTRINSIC_OP:
8060 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8061 gfc_op2string (op));
8062 break;
8064 default:
8065 gcc_unreachable ();
8068 /* Match the required =>. */
8069 if (gfc_match (" =>") != MATCH_YES)
8071 gfc_error ("Expected '=>' at %C");
8072 goto error;
8075 /* Try to find existing GENERIC binding with this name / for this operator;
8076 if there is something, check that it is another GENERIC and then extend
8077 it rather than building a new node. Otherwise, create it and put it
8078 at the right position. */
8080 switch (op_type)
8082 case INTERFACE_USER_OP:
8083 case INTERFACE_GENERIC:
8085 const bool is_op = (op_type == INTERFACE_USER_OP);
8086 gfc_symtree* st;
8088 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8089 if (st)
8091 tb = st->n.tb;
8092 gcc_assert (tb);
8094 else
8095 tb = NULL;
8097 break;
8100 case INTERFACE_INTRINSIC_OP:
8101 tb = ns->tb_op[op];
8102 break;
8104 default:
8105 gcc_unreachable ();
8108 if (tb)
8110 if (!tb->is_generic)
8112 gcc_assert (op_type == INTERFACE_GENERIC);
8113 gfc_error ("There's already a non-generic procedure with binding name"
8114 " '%s' for the derived type '%s' at %C",
8115 bind_name, block->name);
8116 goto error;
8119 if (tb->access != tbattr.access)
8121 gfc_error ("Binding at %C must have the same access as already"
8122 " defined binding '%s'", bind_name);
8123 goto error;
8126 else
8128 tb = gfc_get_typebound_proc (NULL);
8129 tb->where = gfc_current_locus;
8130 tb->access = tbattr.access;
8131 tb->is_generic = 1;
8132 tb->u.generic = NULL;
8134 switch (op_type)
8136 case INTERFACE_GENERIC:
8137 case INTERFACE_USER_OP:
8139 const bool is_op = (op_type == INTERFACE_USER_OP);
8140 gfc_symtree* st;
8142 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8143 name);
8144 gcc_assert (st);
8145 st->n.tb = tb;
8147 break;
8150 case INTERFACE_INTRINSIC_OP:
8151 ns->tb_op[op] = tb;
8152 break;
8154 default:
8155 gcc_unreachable ();
8159 /* Now, match all following names as specific targets. */
8162 gfc_symtree* target_st;
8163 gfc_tbp_generic* target;
8165 m = gfc_match_name (name);
8166 if (m == MATCH_ERROR)
8167 goto error;
8168 if (m == MATCH_NO)
8170 gfc_error ("Expected specific binding name at %C");
8171 goto error;
8174 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8176 /* See if this is a duplicate specification. */
8177 for (target = tb->u.generic; target; target = target->next)
8178 if (target_st == target->specific_st)
8180 gfc_error ("'%s' already defined as specific binding for the"
8181 " generic '%s' at %C", name, bind_name);
8182 goto error;
8185 target = gfc_get_tbp_generic ();
8186 target->specific_st = target_st;
8187 target->specific = NULL;
8188 target->next = tb->u.generic;
8189 tb->u.generic = target;
8191 while (gfc_match (" ,") == MATCH_YES);
8193 /* Here should be the end. */
8194 if (gfc_match_eos () != MATCH_YES)
8196 gfc_error ("Junk after GENERIC binding at %C");
8197 goto error;
8200 return MATCH_YES;
8202 error:
8203 return MATCH_ERROR;
8207 /* Match a FINAL declaration inside a derived type. */
8209 match
8210 gfc_match_final_decl (void)
8212 char name[GFC_MAX_SYMBOL_LEN + 1];
8213 gfc_symbol* sym;
8214 match m;
8215 gfc_namespace* module_ns;
8216 bool first, last;
8217 gfc_symbol* block;
8219 if (gfc_current_form == FORM_FREE)
8221 char c = gfc_peek_ascii_char ();
8222 if (!gfc_is_whitespace (c) && c != ':')
8223 return MATCH_NO;
8226 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8228 if (gfc_current_form == FORM_FIXED)
8229 return MATCH_NO;
8231 gfc_error ("FINAL declaration at %C must be inside a derived type "
8232 "CONTAINS section");
8233 return MATCH_ERROR;
8236 block = gfc_state_stack->previous->sym;
8237 gcc_assert (block);
8239 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8240 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8242 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8243 " specification part of a MODULE");
8244 return MATCH_ERROR;
8247 module_ns = gfc_current_ns;
8248 gcc_assert (module_ns);
8249 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8251 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8252 if (gfc_match (" ::") == MATCH_ERROR)
8253 return MATCH_ERROR;
8255 /* Match the sequence of procedure names. */
8256 first = true;
8257 last = false;
8260 gfc_finalizer* f;
8262 if (first && gfc_match_eos () == MATCH_YES)
8264 gfc_error ("Empty FINAL at %C");
8265 return MATCH_ERROR;
8268 m = gfc_match_name (name);
8269 if (m == MATCH_NO)
8271 gfc_error ("Expected module procedure name at %C");
8272 return MATCH_ERROR;
8274 else if (m != MATCH_YES)
8275 return MATCH_ERROR;
8277 if (gfc_match_eos () == MATCH_YES)
8278 last = true;
8279 if (!last && gfc_match_char (',') != MATCH_YES)
8281 gfc_error ("Expected ',' at %C");
8282 return MATCH_ERROR;
8285 if (gfc_get_symbol (name, module_ns, &sym))
8287 gfc_error ("Unknown procedure name \"%s\" at %C", name);
8288 return MATCH_ERROR;
8291 /* Mark the symbol as module procedure. */
8292 if (sym->attr.proc != PROC_MODULE
8293 && gfc_add_procedure (&sym->attr, PROC_MODULE,
8294 sym->name, NULL) == FAILURE)
8295 return MATCH_ERROR;
8297 /* Check if we already have this symbol in the list, this is an error. */
8298 for (f = block->f2k_derived->finalizers; f; f = f->next)
8299 if (f->proc_sym == sym)
8301 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8302 name);
8303 return MATCH_ERROR;
8306 /* Add this symbol to the list of finalizers. */
8307 gcc_assert (block->f2k_derived);
8308 ++sym->refs;
8309 f = XCNEW (gfc_finalizer);
8310 f->proc_sym = sym;
8311 f->proc_tree = NULL;
8312 f->where = gfc_current_locus;
8313 f->next = block->f2k_derived->finalizers;
8314 block->f2k_derived->finalizers = f;
8316 first = false;
8318 while (!last);
8320 return MATCH_YES;
8324 const ext_attr_t ext_attr_list[] = {
8325 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8326 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8327 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
8328 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
8329 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
8330 { NULL, EXT_ATTR_LAST, NULL }
8333 /* Match a !GCC$ ATTRIBUTES statement of the form:
8334 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8335 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8337 TODO: We should support all GCC attributes using the same syntax for
8338 the attribute list, i.e. the list in C
8339 __attributes(( attribute-list ))
8340 matches then
8341 !GCC$ ATTRIBUTES attribute-list ::
8342 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8343 saved into a TREE.
8345 As there is absolutely no risk of confusion, we should never return
8346 MATCH_NO. */
8347 match
8348 gfc_match_gcc_attributes (void)
8350 symbol_attribute attr;
8351 char name[GFC_MAX_SYMBOL_LEN + 1];
8352 unsigned id;
8353 gfc_symbol *sym;
8354 match m;
8356 gfc_clear_attr (&attr);
8357 for(;;)
8359 char ch;
8361 if (gfc_match_name (name) != MATCH_YES)
8362 return MATCH_ERROR;
8364 for (id = 0; id < EXT_ATTR_LAST; id++)
8365 if (strcmp (name, ext_attr_list[id].name) == 0)
8366 break;
8368 if (id == EXT_ATTR_LAST)
8370 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8371 return MATCH_ERROR;
8374 if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
8375 == FAILURE)
8376 return MATCH_ERROR;
8378 gfc_gobble_whitespace ();
8379 ch = gfc_next_ascii_char ();
8380 if (ch == ':')
8382 /* This is the successful exit condition for the loop. */
8383 if (gfc_next_ascii_char () == ':')
8384 break;
8387 if (ch == ',')
8388 continue;
8390 goto syntax;
8393 if (gfc_match_eos () == MATCH_YES)
8394 goto syntax;
8396 for(;;)
8398 m = gfc_match_name (name);
8399 if (m != MATCH_YES)
8400 return m;
8402 if (find_special (name, &sym, true))
8403 return MATCH_ERROR;
8405 sym->attr.ext_attr |= attr.ext_attr;
8407 if (gfc_match_eos () == MATCH_YES)
8408 break;
8410 if (gfc_match_char (',') != MATCH_YES)
8411 goto syntax;
8414 return MATCH_YES;
8416 syntax:
8417 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
8418 return MATCH_ERROR;