2011-04-21 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / fortran / decl.c
blob9efe01aac2c1c084b3b6fe172339b5948b1dfdfc
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)
1065 gfc_error ("Variable '%s' at %L cannot have the "
1066 "OPTIONAL attribute because procedure '%s'"
1067 " is BIND(C)", sym->name, &(sym->declared_at),
1068 sym->ns->proc_name->name);
1069 retval = FAILURE;
1072 /* Make sure that if it has the dimension attribute, that it is
1073 either assumed size or explicit shape. */
1074 if (sym->as != NULL)
1076 if (sym->as->type == AS_ASSUMED_SHAPE)
1078 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1079 "argument to the procedure '%s' at %L because "
1080 "the procedure is BIND(C)", sym->name,
1081 &(sym->declared_at), sym->ns->proc_name->name,
1082 &(sym->ns->proc_name->declared_at));
1083 retval = FAILURE;
1086 if (sym->as->type == AS_DEFERRED)
1088 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1089 "argument to the procedure '%s' at %L because "
1090 "the procedure is BIND(C)", sym->name,
1091 &(sym->declared_at), sym->ns->proc_name->name,
1092 &(sym->ns->proc_name->declared_at));
1093 retval = FAILURE;
1099 return retval;
1104 /* Function called by variable_decl() that adds a name to the symbol table. */
1106 static gfc_try
1107 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1108 gfc_array_spec **as, locus *var_locus)
1110 symbol_attribute attr;
1111 gfc_symbol *sym;
1113 if (gfc_get_symbol (name, NULL, &sym))
1114 return FAILURE;
1116 /* Start updating the symbol table. Add basic type attribute if present. */
1117 if (current_ts.type != BT_UNKNOWN
1118 && (sym->attr.implicit_type == 0
1119 || !gfc_compare_types (&sym->ts, &current_ts))
1120 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
1121 return FAILURE;
1123 if (sym->ts.type == BT_CHARACTER)
1125 sym->ts.u.cl = cl;
1126 sym->ts.deferred = cl_deferred;
1129 /* Add dimension attribute if present. */
1130 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1131 return FAILURE;
1132 *as = NULL;
1134 /* Add attribute to symbol. The copy is so that we can reset the
1135 dimension attribute. */
1136 attr = current_attr;
1137 attr.dimension = 0;
1138 attr.codimension = 0;
1140 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1141 return FAILURE;
1143 /* Finish any work that may need to be done for the binding label,
1144 if it's a bind(c). The bind(c) attr is found before the symbol
1145 is made, and before the symbol name (for data decls), so the
1146 current_ts is holding the binding label, or nothing if the
1147 name= attr wasn't given. Therefore, test here if we're dealing
1148 with a bind(c) and make sure the binding label is set correctly. */
1149 if (sym->attr.is_bind_c == 1)
1151 if (sym->binding_label[0] == '\0')
1153 /* Set the binding label and verify that if a NAME= was specified
1154 then only one identifier was in the entity-decl-list. */
1155 if (set_binding_label (sym->binding_label, sym->name,
1156 num_idents_on_line) == FAILURE)
1157 return FAILURE;
1161 /* See if we know we're in a common block, and if it's a bind(c)
1162 common then we need to make sure we're an interoperable type. */
1163 if (sym->attr.in_common == 1)
1165 /* Test the common block object. */
1166 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1167 && sym->ts.is_c_interop != 1)
1169 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1170 "must be declared with a C interoperable "
1171 "kind since common block '%s' is BIND(C)",
1172 sym->name, sym->common_block->name,
1173 sym->common_block->name);
1174 gfc_clear_error ();
1178 sym->attr.implied_index = 0;
1180 if (sym->ts.type == BT_CLASS)
1181 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1183 return SUCCESS;
1187 /* Set character constant to the given length. The constant will be padded or
1188 truncated. If we're inside an array constructor without a typespec, we
1189 additionally check that all elements have the same length; check_len -1
1190 means no checking. */
1192 void
1193 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1195 gfc_char_t *s;
1196 int slen;
1198 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1199 gcc_assert (expr->ts.type == BT_CHARACTER);
1201 slen = expr->value.character.length;
1202 if (len != slen)
1204 s = gfc_get_wide_string (len + 1);
1205 memcpy (s, expr->value.character.string,
1206 MIN (len, slen) * sizeof (gfc_char_t));
1207 if (len > slen)
1208 gfc_wide_memset (&s[slen], ' ', len - slen);
1210 if (gfc_option.warn_character_truncation && slen > len)
1211 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1212 "(%d/%d)", &expr->where, slen, len);
1214 /* Apply the standard by 'hand' otherwise it gets cleared for
1215 initializers. */
1216 if (check_len != -1 && slen != check_len
1217 && !(gfc_option.allow_std & GFC_STD_GNU))
1218 gfc_error_now ("The CHARACTER elements of the array constructor "
1219 "at %L must have the same length (%d/%d)",
1220 &expr->where, slen, check_len);
1222 s[len] = '\0';
1223 free (expr->value.character.string);
1224 expr->value.character.string = s;
1225 expr->value.character.length = len;
1230 /* Function to create and update the enumerator history
1231 using the information passed as arguments.
1232 Pointer "max_enum" is also updated, to point to
1233 enum history node containing largest initializer.
1235 SYM points to the symbol node of enumerator.
1236 INIT points to its enumerator value. */
1238 static void
1239 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1241 enumerator_history *new_enum_history;
1242 gcc_assert (sym != NULL && init != NULL);
1244 new_enum_history = XCNEW (enumerator_history);
1246 new_enum_history->sym = sym;
1247 new_enum_history->initializer = init;
1248 new_enum_history->next = NULL;
1250 if (enum_history == NULL)
1252 enum_history = new_enum_history;
1253 max_enum = enum_history;
1255 else
1257 new_enum_history->next = enum_history;
1258 enum_history = new_enum_history;
1260 if (mpz_cmp (max_enum->initializer->value.integer,
1261 new_enum_history->initializer->value.integer) < 0)
1262 max_enum = new_enum_history;
1267 /* Function to free enum kind history. */
1269 void
1270 gfc_free_enum_history (void)
1272 enumerator_history *current = enum_history;
1273 enumerator_history *next;
1275 while (current != NULL)
1277 next = current->next;
1278 free (current);
1279 current = next;
1281 max_enum = NULL;
1282 enum_history = NULL;
1286 /* Function called by variable_decl() that adds an initialization
1287 expression to a symbol. */
1289 static gfc_try
1290 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1292 symbol_attribute attr;
1293 gfc_symbol *sym;
1294 gfc_expr *init;
1296 init = *initp;
1297 if (find_special (name, &sym, false))
1298 return FAILURE;
1300 attr = sym->attr;
1302 /* If this symbol is confirming an implicit parameter type,
1303 then an initialization expression is not allowed. */
1304 if (attr.flavor == FL_PARAMETER
1305 && sym->value != NULL
1306 && *initp != NULL)
1308 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1309 sym->name);
1310 return FAILURE;
1313 if (init == NULL)
1315 /* An initializer is required for PARAMETER declarations. */
1316 if (attr.flavor == FL_PARAMETER)
1318 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1319 return FAILURE;
1322 else
1324 /* If a variable appears in a DATA block, it cannot have an
1325 initializer. */
1326 if (sym->attr.data)
1328 gfc_error ("Variable '%s' at %C with an initializer already "
1329 "appears in a DATA statement", sym->name);
1330 return FAILURE;
1333 /* Check if the assignment can happen. This has to be put off
1334 until later for derived type variables and procedure pointers. */
1335 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1336 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1337 && !sym->attr.proc_pointer
1338 && gfc_check_assign_symbol (sym, init) == FAILURE)
1339 return FAILURE;
1341 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1342 && init->ts.type == BT_CHARACTER)
1344 /* Update symbol character length according initializer. */
1345 if (gfc_check_assign_symbol (sym, init) == FAILURE)
1346 return FAILURE;
1348 if (sym->ts.u.cl->length == NULL)
1350 int clen;
1351 /* If there are multiple CHARACTER variables declared on the
1352 same line, we don't want them to share the same length. */
1353 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1355 if (sym->attr.flavor == FL_PARAMETER)
1357 if (init->expr_type == EXPR_CONSTANT)
1359 clen = init->value.character.length;
1360 sym->ts.u.cl->length
1361 = gfc_get_int_expr (gfc_default_integer_kind,
1362 NULL, clen);
1364 else if (init->expr_type == EXPR_ARRAY)
1366 gfc_constructor *c;
1367 c = gfc_constructor_first (init->value.constructor);
1368 clen = c->expr->value.character.length;
1369 sym->ts.u.cl->length
1370 = gfc_get_int_expr (gfc_default_integer_kind,
1371 NULL, clen);
1373 else if (init->ts.u.cl && init->ts.u.cl->length)
1374 sym->ts.u.cl->length =
1375 gfc_copy_expr (sym->value->ts.u.cl->length);
1378 /* Update initializer character length according symbol. */
1379 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1381 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1383 if (init->expr_type == EXPR_CONSTANT)
1384 gfc_set_constant_character_len (len, init, -1);
1385 else if (init->expr_type == EXPR_ARRAY)
1387 gfc_constructor *c;
1389 /* Build a new charlen to prevent simplification from
1390 deleting the length before it is resolved. */
1391 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1392 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1394 for (c = gfc_constructor_first (init->value.constructor);
1395 c; c = gfc_constructor_next (c))
1396 gfc_set_constant_character_len (len, c->expr, -1);
1401 /* If sym is implied-shape, set its upper bounds from init. */
1402 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1403 && sym->as->type == AS_IMPLIED_SHAPE)
1405 int dim;
1407 if (init->rank == 0)
1409 gfc_error ("Can't initialize implied-shape array at %L"
1410 " with scalar", &sym->declared_at);
1411 return FAILURE;
1413 gcc_assert (sym->as->rank == init->rank);
1415 /* Shape should be present, we get an initialization expression. */
1416 gcc_assert (init->shape);
1418 for (dim = 0; dim < sym->as->rank; ++dim)
1420 int k;
1421 gfc_expr* lower;
1422 gfc_expr* e;
1424 lower = sym->as->lower[dim];
1425 if (lower->expr_type != EXPR_CONSTANT)
1427 gfc_error ("Non-constant lower bound in implied-shape"
1428 " declaration at %L", &lower->where);
1429 return FAILURE;
1432 /* All dimensions must be without upper bound. */
1433 gcc_assert (!sym->as->upper[dim]);
1435 k = lower->ts.kind;
1436 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1437 mpz_add (e->value.integer,
1438 lower->value.integer, init->shape[dim]);
1439 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1440 sym->as->upper[dim] = e;
1443 sym->as->type = AS_EXPLICIT;
1446 /* Need to check if the expression we initialized this
1447 to was one of the iso_c_binding named constants. If so,
1448 and we're a parameter (constant), let it be iso_c.
1449 For example:
1450 integer(c_int), parameter :: my_int = c_int
1451 integer(my_int) :: my_int_2
1452 If we mark my_int as iso_c (since we can see it's value
1453 is equal to one of the named constants), then my_int_2
1454 will be considered C interoperable. */
1455 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1457 sym->ts.is_iso_c |= init->ts.is_iso_c;
1458 sym->ts.is_c_interop |= init->ts.is_c_interop;
1459 /* attr bits needed for module files. */
1460 sym->attr.is_iso_c |= init->ts.is_iso_c;
1461 sym->attr.is_c_interop |= init->ts.is_c_interop;
1462 if (init->ts.is_iso_c)
1463 sym->ts.f90_type = init->ts.f90_type;
1466 /* Add initializer. Make sure we keep the ranks sane. */
1467 if (sym->attr.dimension && init->rank == 0)
1469 mpz_t size;
1470 gfc_expr *array;
1471 int n;
1472 if (sym->attr.flavor == FL_PARAMETER
1473 && init->expr_type == EXPR_CONSTANT
1474 && spec_size (sym->as, &size) == SUCCESS
1475 && mpz_cmp_si (size, 0) > 0)
1477 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1478 &init->where);
1479 for (n = 0; n < (int)mpz_get_si (size); n++)
1480 gfc_constructor_append_expr (&array->value.constructor,
1481 n == 0
1482 ? init
1483 : gfc_copy_expr (init),
1484 &init->where);
1486 array->shape = gfc_get_shape (sym->as->rank);
1487 for (n = 0; n < sym->as->rank; n++)
1488 spec_dimen_size (sym->as, n, &array->shape[n]);
1490 init = array;
1491 mpz_clear (size);
1493 init->rank = sym->as->rank;
1496 sym->value = init;
1497 if (sym->attr.save == SAVE_NONE)
1498 sym->attr.save = SAVE_IMPLICIT;
1499 *initp = NULL;
1502 return SUCCESS;
1506 /* Function called by variable_decl() that adds a name to a structure
1507 being built. */
1509 static gfc_try
1510 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1511 gfc_array_spec **as)
1513 gfc_component *c;
1514 gfc_try t = SUCCESS;
1516 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1517 constructing, it must have the pointer attribute. */
1518 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1519 && current_ts.u.derived == gfc_current_block ()
1520 && current_attr.pointer == 0)
1522 gfc_error ("Component at %C must have the POINTER attribute");
1523 return FAILURE;
1526 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1528 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1530 gfc_error ("Array component of structure at %C must have explicit "
1531 "or deferred shape");
1532 return FAILURE;
1536 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1537 return FAILURE;
1539 c->ts = current_ts;
1540 if (c->ts.type == BT_CHARACTER)
1541 c->ts.u.cl = cl;
1542 c->attr = current_attr;
1544 c->initializer = *init;
1545 *init = NULL;
1547 c->as = *as;
1548 if (c->as != NULL)
1550 if (c->as->corank)
1551 c->attr.codimension = 1;
1552 if (c->as->rank)
1553 c->attr.dimension = 1;
1555 *as = NULL;
1557 /* Should this ever get more complicated, combine with similar section
1558 in add_init_expr_to_sym into a separate function. */
1559 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer && c->ts.u.cl
1560 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1562 int len;
1564 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1565 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1566 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1568 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1570 if (c->initializer->expr_type == EXPR_CONSTANT)
1571 gfc_set_constant_character_len (len, c->initializer, -1);
1572 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1573 c->initializer->ts.u.cl->length->value.integer))
1575 gfc_constructor *ctor;
1576 ctor = gfc_constructor_first (c->initializer->value.constructor);
1578 if (ctor)
1580 int first_len;
1581 bool has_ts = (c->initializer->ts.u.cl
1582 && c->initializer->ts.u.cl->length_from_typespec);
1584 /* Remember the length of the first element for checking
1585 that all elements *in the constructor* have the same
1586 length. This need not be the length of the LHS! */
1587 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1588 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1589 first_len = ctor->expr->value.character.length;
1591 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1592 if (ctor->expr->expr_type == EXPR_CONSTANT)
1594 gfc_set_constant_character_len (len, ctor->expr,
1595 has_ts ? -1 : first_len);
1596 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1602 /* Check array components. */
1603 if (!c->attr.dimension)
1604 goto scalar;
1606 if (c->attr.pointer)
1608 if (c->as->type != AS_DEFERRED)
1610 gfc_error ("Pointer array component of structure at %C must have a "
1611 "deferred shape");
1612 t = FAILURE;
1615 else if (c->attr.allocatable)
1617 if (c->as->type != AS_DEFERRED)
1619 gfc_error ("Allocatable component of structure at %C must have a "
1620 "deferred shape");
1621 t = FAILURE;
1624 else
1626 if (c->as->type != AS_EXPLICIT)
1628 gfc_error ("Array component of structure at %C must have an "
1629 "explicit shape");
1630 t = FAILURE;
1634 scalar:
1635 if (c->ts.type == BT_CLASS)
1637 bool delayed = (gfc_state_stack->sym == c->ts.u.derived)
1638 || (!c->ts.u.derived->components
1639 && !c->ts.u.derived->attr.zero_comp);
1640 return gfc_build_class_symbol (&c->ts, &c->attr, &c->as, delayed);
1643 return t;
1647 /* Match a 'NULL()', and possibly take care of some side effects. */
1649 match
1650 gfc_match_null (gfc_expr **result)
1652 gfc_symbol *sym;
1653 match m;
1655 m = gfc_match (" null ( )");
1656 if (m != MATCH_YES)
1657 return m;
1659 /* The NULL symbol now has to be/become an intrinsic function. */
1660 if (gfc_get_symbol ("null", NULL, &sym))
1662 gfc_error ("NULL() initialization at %C is ambiguous");
1663 return MATCH_ERROR;
1666 gfc_intrinsic_symbol (sym);
1668 if (sym->attr.proc != PROC_INTRINSIC
1669 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1670 sym->name, NULL) == FAILURE
1671 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1672 return MATCH_ERROR;
1674 *result = gfc_get_null_expr (&gfc_current_locus);
1676 return MATCH_YES;
1680 /* Match the initialization expr for a data pointer or procedure pointer. */
1682 static match
1683 match_pointer_init (gfc_expr **init, int procptr)
1685 match m;
1687 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1689 gfc_error ("Initialization of pointer at %C is not allowed in "
1690 "a PURE procedure");
1691 return MATCH_ERROR;
1694 /* Match NULL() initilization. */
1695 m = gfc_match_null (init);
1696 if (m != MATCH_NO)
1697 return m;
1699 /* Match non-NULL initialization. */
1700 gfc_matching_ptr_assignment = !procptr;
1701 gfc_matching_procptr_assignment = procptr;
1702 m = gfc_match_rvalue (init);
1703 gfc_matching_ptr_assignment = 0;
1704 gfc_matching_procptr_assignment = 0;
1705 if (m == MATCH_ERROR)
1706 return MATCH_ERROR;
1707 else if (m == MATCH_NO)
1709 gfc_error ("Error in pointer initialization at %C");
1710 return MATCH_ERROR;
1713 if (!procptr)
1714 gfc_resolve_expr (*init);
1716 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: non-NULL pointer "
1717 "initialization at %C") == FAILURE)
1718 return MATCH_ERROR;
1720 return MATCH_YES;
1724 /* Match a variable name with an optional initializer. When this
1725 subroutine is called, a variable is expected to be parsed next.
1726 Depending on what is happening at the moment, updates either the
1727 symbol table or the current interface. */
1729 static match
1730 variable_decl (int elem)
1732 char name[GFC_MAX_SYMBOL_LEN + 1];
1733 gfc_expr *initializer, *char_len;
1734 gfc_array_spec *as;
1735 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1736 gfc_charlen *cl;
1737 bool cl_deferred;
1738 locus var_locus;
1739 match m;
1740 gfc_try t;
1741 gfc_symbol *sym;
1743 initializer = NULL;
1744 as = NULL;
1745 cp_as = NULL;
1747 /* When we get here, we've just matched a list of attributes and
1748 maybe a type and a double colon. The next thing we expect to see
1749 is the name of the symbol. */
1750 m = gfc_match_name (name);
1751 if (m != MATCH_YES)
1752 goto cleanup;
1754 var_locus = gfc_current_locus;
1756 /* Now we could see the optional array spec. or character length. */
1757 m = gfc_match_array_spec (&as, true, true);
1758 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1759 cp_as = gfc_copy_array_spec (as);
1760 else if (m == MATCH_ERROR)
1761 goto cleanup;
1763 if (m == MATCH_NO)
1764 as = gfc_copy_array_spec (current_as);
1765 else if (current_as)
1766 merge_array_spec (current_as, as, true);
1768 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1769 determine (and check) whether it can be implied-shape. If it
1770 was parsed as assumed-size, change it because PARAMETERs can not
1771 be assumed-size. */
1772 if (as)
1774 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1776 m = MATCH_ERROR;
1777 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1778 name, &var_locus);
1779 goto cleanup;
1782 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1783 && current_attr.flavor == FL_PARAMETER)
1784 as->type = AS_IMPLIED_SHAPE;
1786 if (as->type == AS_IMPLIED_SHAPE
1787 && gfc_notify_std (GFC_STD_F2008,
1788 "Fortran 2008: Implied-shape array at %L",
1789 &var_locus) == FAILURE)
1791 m = MATCH_ERROR;
1792 goto cleanup;
1796 char_len = NULL;
1797 cl = NULL;
1798 cl_deferred = false;
1800 if (current_ts.type == BT_CHARACTER)
1802 switch (match_char_length (&char_len, &cl_deferred))
1804 case MATCH_YES:
1805 cl = gfc_new_charlen (gfc_current_ns, NULL);
1807 cl->length = char_len;
1808 break;
1810 /* Non-constant lengths need to be copied after the first
1811 element. Also copy assumed lengths. */
1812 case MATCH_NO:
1813 if (elem > 1
1814 && (current_ts.u.cl->length == NULL
1815 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1817 cl = gfc_new_charlen (gfc_current_ns, NULL);
1818 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1820 else
1821 cl = current_ts.u.cl;
1823 cl_deferred = current_ts.deferred;
1825 break;
1827 case MATCH_ERROR:
1828 goto cleanup;
1832 /* If this symbol has already shown up in a Cray Pointer declaration,
1833 then we want to set the type & bail out. */
1834 if (gfc_option.flag_cray_pointer)
1836 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1837 if (sym != NULL && sym->attr.cray_pointee)
1839 sym->ts.type = current_ts.type;
1840 sym->ts.kind = current_ts.kind;
1841 sym->ts.u.cl = cl;
1842 sym->ts.u.derived = current_ts.u.derived;
1843 sym->ts.is_c_interop = current_ts.is_c_interop;
1844 sym->ts.is_iso_c = current_ts.is_iso_c;
1845 m = MATCH_YES;
1847 /* Check to see if we have an array specification. */
1848 if (cp_as != NULL)
1850 if (sym->as != NULL)
1852 gfc_error ("Duplicate array spec for Cray pointee at %C");
1853 gfc_free_array_spec (cp_as);
1854 m = MATCH_ERROR;
1855 goto cleanup;
1857 else
1859 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1860 gfc_internal_error ("Couldn't set pointee array spec.");
1862 /* Fix the array spec. */
1863 m = gfc_mod_pointee_as (sym->as);
1864 if (m == MATCH_ERROR)
1865 goto cleanup;
1868 goto cleanup;
1870 else
1872 gfc_free_array_spec (cp_as);
1876 /* Procedure pointer as function result. */
1877 if (gfc_current_state () == COMP_FUNCTION
1878 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1879 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1880 strcpy (name, "ppr@");
1882 if (gfc_current_state () == COMP_FUNCTION
1883 && strcmp (name, gfc_current_block ()->name) == 0
1884 && gfc_current_block ()->result
1885 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1886 strcpy (name, "ppr@");
1888 /* OK, we've successfully matched the declaration. Now put the
1889 symbol in the current namespace, because it might be used in the
1890 optional initialization expression for this symbol, e.g. this is
1891 perfectly legal:
1893 integer, parameter :: i = huge(i)
1895 This is only true for parameters or variables of a basic type.
1896 For components of derived types, it is not true, so we don't
1897 create a symbol for those yet. If we fail to create the symbol,
1898 bail out. */
1899 if (gfc_current_state () != COMP_DERIVED
1900 && build_sym (name, cl, cl_deferred, &as, &var_locus) == FAILURE)
1902 m = MATCH_ERROR;
1903 goto cleanup;
1906 /* An interface body specifies all of the procedure's
1907 characteristics and these shall be consistent with those
1908 specified in the procedure definition, except that the interface
1909 may specify a procedure that is not pure if the procedure is
1910 defined to be pure(12.3.2). */
1911 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1912 && gfc_current_ns->proc_name
1913 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1914 && current_ts.u.derived->ns != gfc_current_ns)
1916 gfc_symtree *st;
1917 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.u.derived->name);
1918 if (!(current_ts.u.derived->attr.imported
1919 && st != NULL
1920 && st->n.sym == current_ts.u.derived)
1921 && !gfc_current_ns->has_import_set)
1923 gfc_error ("the type of '%s' at %C has not been declared within the "
1924 "interface", name);
1925 m = MATCH_ERROR;
1926 goto cleanup;
1930 /* In functions that have a RESULT variable defined, the function
1931 name always refers to function calls. Therefore, the name is
1932 not allowed to appear in specification statements. */
1933 if (gfc_current_state () == COMP_FUNCTION
1934 && gfc_current_block () != NULL
1935 && gfc_current_block ()->result != NULL
1936 && gfc_current_block ()->result != gfc_current_block ()
1937 && strcmp (gfc_current_block ()->name, name) == 0)
1939 gfc_error ("Function name '%s' not allowed at %C", name);
1940 m = MATCH_ERROR;
1941 goto cleanup;
1944 /* We allow old-style initializations of the form
1945 integer i /2/, j(4) /3*3, 1/
1946 (if no colon has been seen). These are different from data
1947 statements in that initializers are only allowed to apply to the
1948 variable immediately preceding, i.e.
1949 integer i, j /1, 2/
1950 is not allowed. Therefore we have to do some work manually, that
1951 could otherwise be left to the matchers for DATA statements. */
1953 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1955 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1956 "initialization at %C") == FAILURE)
1957 return MATCH_ERROR;
1959 return match_old_style_init (name);
1962 /* The double colon must be present in order to have initializers.
1963 Otherwise the statement is ambiguous with an assignment statement. */
1964 if (colon_seen)
1966 if (gfc_match (" =>") == MATCH_YES)
1968 if (!current_attr.pointer)
1970 gfc_error ("Initialization at %C isn't for a pointer variable");
1971 m = MATCH_ERROR;
1972 goto cleanup;
1975 m = match_pointer_init (&initializer, 0);
1976 if (m != MATCH_YES)
1977 goto cleanup;
1979 else if (gfc_match_char ('=') == MATCH_YES)
1981 if (current_attr.pointer)
1983 gfc_error ("Pointer initialization at %C requires '=>', "
1984 "not '='");
1985 m = MATCH_ERROR;
1986 goto cleanup;
1989 m = gfc_match_init_expr (&initializer);
1990 if (m == MATCH_NO)
1992 gfc_error ("Expected an initialization expression at %C");
1993 m = MATCH_ERROR;
1996 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
1997 && gfc_state_stack->state != COMP_DERIVED)
1999 gfc_error ("Initialization of variable at %C is not allowed in "
2000 "a PURE procedure");
2001 m = MATCH_ERROR;
2004 if (m != MATCH_YES)
2005 goto cleanup;
2009 if (initializer != NULL && current_attr.allocatable
2010 && gfc_current_state () == COMP_DERIVED)
2012 gfc_error ("Initialization of allocatable component at %C is not "
2013 "allowed");
2014 m = MATCH_ERROR;
2015 goto cleanup;
2018 /* Add the initializer. Note that it is fine if initializer is
2019 NULL here, because we sometimes also need to check if a
2020 declaration *must* have an initialization expression. */
2021 if (gfc_current_state () != COMP_DERIVED)
2022 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2023 else
2025 if (current_ts.type == BT_DERIVED
2026 && !current_attr.pointer && !initializer)
2027 initializer = gfc_default_initializer (&current_ts);
2028 t = build_struct (name, cl, &initializer, &as);
2031 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
2033 cleanup:
2034 /* Free stuff up and return. */
2035 gfc_free_expr (initializer);
2036 gfc_free_array_spec (as);
2038 return m;
2042 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2043 This assumes that the byte size is equal to the kind number for
2044 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2046 match
2047 gfc_match_old_kind_spec (gfc_typespec *ts)
2049 match m;
2050 int original_kind;
2052 if (gfc_match_char ('*') != MATCH_YES)
2053 return MATCH_NO;
2055 m = gfc_match_small_literal_int (&ts->kind, NULL);
2056 if (m != MATCH_YES)
2057 return MATCH_ERROR;
2059 original_kind = ts->kind;
2061 /* Massage the kind numbers for complex types. */
2062 if (ts->type == BT_COMPLEX)
2064 if (ts->kind % 2)
2066 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2067 gfc_basic_typename (ts->type), original_kind);
2068 return MATCH_ERROR;
2070 ts->kind /= 2;
2073 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
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;
2080 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2081 gfc_basic_typename (ts->type), original_kind) == FAILURE)
2082 return MATCH_ERROR;
2084 return MATCH_YES;
2088 /* Match a kind specification. Since kinds are generally optional, we
2089 usually return MATCH_NO if something goes wrong. If a "kind="
2090 string is found, then we know we have an error. */
2092 match
2093 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2095 locus where, loc;
2096 gfc_expr *e;
2097 match m, n;
2098 char c;
2099 const char *msg;
2101 m = MATCH_NO;
2102 n = MATCH_YES;
2103 e = NULL;
2105 where = loc = gfc_current_locus;
2107 if (kind_expr_only)
2108 goto kind_expr;
2110 if (gfc_match_char ('(') == MATCH_NO)
2111 return MATCH_NO;
2113 /* Also gobbles optional text. */
2114 if (gfc_match (" kind = ") == MATCH_YES)
2115 m = MATCH_ERROR;
2117 loc = gfc_current_locus;
2119 kind_expr:
2120 n = gfc_match_init_expr (&e);
2122 if (n != MATCH_YES)
2124 if (gfc_matching_function)
2126 /* The function kind expression might include use associated or
2127 imported parameters and try again after the specification
2128 expressions..... */
2129 if (gfc_match_char (')') != MATCH_YES)
2131 gfc_error ("Missing right parenthesis at %C");
2132 m = MATCH_ERROR;
2133 goto no_match;
2136 gfc_free_expr (e);
2137 gfc_undo_symbols ();
2138 return MATCH_YES;
2140 else
2142 /* ....or else, the match is real. */
2143 if (n == MATCH_NO)
2144 gfc_error ("Expected initialization expression at %C");
2145 if (n != MATCH_YES)
2146 return MATCH_ERROR;
2150 if (e->rank != 0)
2152 gfc_error ("Expected scalar initialization expression at %C");
2153 m = MATCH_ERROR;
2154 goto no_match;
2157 msg = gfc_extract_int (e, &ts->kind);
2159 if (msg != NULL)
2161 gfc_error (msg);
2162 m = MATCH_ERROR;
2163 goto no_match;
2166 /* Before throwing away the expression, let's see if we had a
2167 C interoperable kind (and store the fact). */
2168 if (e->ts.is_c_interop == 1)
2170 /* Mark this as c interoperable if being declared with one
2171 of the named constants from iso_c_binding. */
2172 ts->is_c_interop = e->ts.is_iso_c;
2173 ts->f90_type = e->ts.f90_type;
2176 gfc_free_expr (e);
2177 e = NULL;
2179 /* Ignore errors to this point, if we've gotten here. This means
2180 we ignore the m=MATCH_ERROR from above. */
2181 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2183 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2184 gfc_basic_typename (ts->type));
2185 gfc_current_locus = where;
2186 return MATCH_ERROR;
2189 /* Warn if, e.g., c_int is used for a REAL variable, but not
2190 if, e.g., c_double is used for COMPLEX as the standard
2191 explicitly says that the kind type parameter for complex and real
2192 variable is the same, i.e. c_float == c_float_complex. */
2193 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2194 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2195 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2196 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2197 "is %s", gfc_basic_typename (ts->f90_type), &where,
2198 gfc_basic_typename (ts->type));
2200 gfc_gobble_whitespace ();
2201 if ((c = gfc_next_ascii_char ()) != ')'
2202 && (ts->type != BT_CHARACTER || c != ','))
2204 if (ts->type == BT_CHARACTER)
2205 gfc_error ("Missing right parenthesis or comma at %C");
2206 else
2207 gfc_error ("Missing right parenthesis at %C");
2208 m = MATCH_ERROR;
2210 else
2211 /* All tests passed. */
2212 m = MATCH_YES;
2214 if(m == MATCH_ERROR)
2215 gfc_current_locus = where;
2217 /* Return what we know from the test(s). */
2218 return m;
2220 no_match:
2221 gfc_free_expr (e);
2222 gfc_current_locus = where;
2223 return m;
2227 static match
2228 match_char_kind (int * kind, int * is_iso_c)
2230 locus where;
2231 gfc_expr *e;
2232 match m, n;
2233 const char *msg;
2235 m = MATCH_NO;
2236 e = NULL;
2237 where = gfc_current_locus;
2239 n = gfc_match_init_expr (&e);
2241 if (n != MATCH_YES && gfc_matching_function)
2243 /* The expression might include use-associated or imported
2244 parameters and try again after the specification
2245 expressions. */
2246 gfc_free_expr (e);
2247 gfc_undo_symbols ();
2248 return MATCH_YES;
2251 if (n == MATCH_NO)
2252 gfc_error ("Expected initialization expression at %C");
2253 if (n != MATCH_YES)
2254 return MATCH_ERROR;
2256 if (e->rank != 0)
2258 gfc_error ("Expected scalar initialization expression at %C");
2259 m = MATCH_ERROR;
2260 goto no_match;
2263 msg = gfc_extract_int (e, kind);
2264 *is_iso_c = e->ts.is_iso_c;
2265 if (msg != NULL)
2267 gfc_error (msg);
2268 m = MATCH_ERROR;
2269 goto no_match;
2272 gfc_free_expr (e);
2274 /* Ignore errors to this point, if we've gotten here. This means
2275 we ignore the m=MATCH_ERROR from above. */
2276 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2278 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2279 m = MATCH_ERROR;
2281 else
2282 /* All tests passed. */
2283 m = MATCH_YES;
2285 if (m == MATCH_ERROR)
2286 gfc_current_locus = where;
2288 /* Return what we know from the test(s). */
2289 return m;
2291 no_match:
2292 gfc_free_expr (e);
2293 gfc_current_locus = where;
2294 return m;
2298 /* Match the various kind/length specifications in a CHARACTER
2299 declaration. We don't return MATCH_NO. */
2301 match
2302 gfc_match_char_spec (gfc_typespec *ts)
2304 int kind, seen_length, is_iso_c;
2305 gfc_charlen *cl;
2306 gfc_expr *len;
2307 match m;
2308 bool deferred;
2310 len = NULL;
2311 seen_length = 0;
2312 kind = 0;
2313 is_iso_c = 0;
2314 deferred = false;
2316 /* Try the old-style specification first. */
2317 old_char_selector = 0;
2319 m = match_char_length (&len, &deferred);
2320 if (m != MATCH_NO)
2322 if (m == MATCH_YES)
2323 old_char_selector = 1;
2324 seen_length = 1;
2325 goto done;
2328 m = gfc_match_char ('(');
2329 if (m != MATCH_YES)
2331 m = MATCH_YES; /* Character without length is a single char. */
2332 goto done;
2335 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2336 if (gfc_match (" kind =") == MATCH_YES)
2338 m = match_char_kind (&kind, &is_iso_c);
2340 if (m == MATCH_ERROR)
2341 goto done;
2342 if (m == MATCH_NO)
2343 goto syntax;
2345 if (gfc_match (" , len =") == MATCH_NO)
2346 goto rparen;
2348 m = char_len_param_value (&len, &deferred);
2349 if (m == MATCH_NO)
2350 goto syntax;
2351 if (m == MATCH_ERROR)
2352 goto done;
2353 seen_length = 1;
2355 goto rparen;
2358 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2359 if (gfc_match (" len =") == MATCH_YES)
2361 m = char_len_param_value (&len, &deferred);
2362 if (m == MATCH_NO)
2363 goto syntax;
2364 if (m == MATCH_ERROR)
2365 goto done;
2366 seen_length = 1;
2368 if (gfc_match_char (')') == MATCH_YES)
2369 goto done;
2371 if (gfc_match (" , kind =") != MATCH_YES)
2372 goto syntax;
2374 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2375 goto done;
2377 goto rparen;
2380 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2381 m = char_len_param_value (&len, &deferred);
2382 if (m == MATCH_NO)
2383 goto syntax;
2384 if (m == MATCH_ERROR)
2385 goto done;
2386 seen_length = 1;
2388 m = gfc_match_char (')');
2389 if (m == MATCH_YES)
2390 goto done;
2392 if (gfc_match_char (',') != MATCH_YES)
2393 goto syntax;
2395 gfc_match (" kind ="); /* Gobble optional text. */
2397 m = match_char_kind (&kind, &is_iso_c);
2398 if (m == MATCH_ERROR)
2399 goto done;
2400 if (m == MATCH_NO)
2401 goto syntax;
2403 rparen:
2404 /* Require a right-paren at this point. */
2405 m = gfc_match_char (')');
2406 if (m == MATCH_YES)
2407 goto done;
2409 syntax:
2410 gfc_error ("Syntax error in CHARACTER declaration at %C");
2411 m = MATCH_ERROR;
2412 gfc_free_expr (len);
2413 return m;
2415 done:
2416 /* Deal with character functions after USE and IMPORT statements. */
2417 if (gfc_matching_function)
2419 gfc_free_expr (len);
2420 gfc_undo_symbols ();
2421 return MATCH_YES;
2424 if (m != MATCH_YES)
2426 gfc_free_expr (len);
2427 return m;
2430 /* Do some final massaging of the length values. */
2431 cl = gfc_new_charlen (gfc_current_ns, NULL);
2433 if (seen_length == 0)
2434 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2435 else
2436 cl->length = len;
2438 ts->u.cl = cl;
2439 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2440 ts->deferred = deferred;
2442 /* We have to know if it was a c interoperable kind so we can
2443 do accurate type checking of bind(c) procs, etc. */
2444 if (kind != 0)
2445 /* Mark this as c interoperable if being declared with one
2446 of the named constants from iso_c_binding. */
2447 ts->is_c_interop = is_iso_c;
2448 else if (len != NULL)
2449 /* Here, we might have parsed something such as: character(c_char)
2450 In this case, the parsing code above grabs the c_char when
2451 looking for the length (line 1690, roughly). it's the last
2452 testcase for parsing the kind params of a character variable.
2453 However, it's not actually the length. this seems like it
2454 could be an error.
2455 To see if the user used a C interop kind, test the expr
2456 of the so called length, and see if it's C interoperable. */
2457 ts->is_c_interop = len->ts.is_iso_c;
2459 return MATCH_YES;
2463 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2464 structure to the matched specification. This is necessary for FUNCTION and
2465 IMPLICIT statements.
2467 If implicit_flag is nonzero, then we don't check for the optional
2468 kind specification. Not doing so is needed for matching an IMPLICIT
2469 statement correctly. */
2471 match
2472 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2474 char name[GFC_MAX_SYMBOL_LEN + 1];
2475 gfc_symbol *sym;
2476 match m;
2477 char c;
2478 bool seen_deferred_kind, matched_type;
2480 /* A belt and braces check that the typespec is correctly being treated
2481 as a deferred characteristic association. */
2482 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2483 && (gfc_current_block ()->result->ts.kind == -1)
2484 && (ts->kind == -1);
2485 gfc_clear_ts (ts);
2486 if (seen_deferred_kind)
2487 ts->kind = -1;
2489 /* Clear the current binding label, in case one is given. */
2490 curr_binding_label[0] = '\0';
2492 if (gfc_match (" byte") == MATCH_YES)
2494 if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2495 == FAILURE)
2496 return MATCH_ERROR;
2498 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2500 gfc_error ("BYTE type used at %C "
2501 "is not available on the target machine");
2502 return MATCH_ERROR;
2505 ts->type = BT_INTEGER;
2506 ts->kind = 1;
2507 return MATCH_YES;
2511 m = gfc_match (" type ( %n", name);
2512 matched_type = (m == MATCH_YES);
2514 if ((matched_type && strcmp ("integer", name) == 0)
2515 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2517 ts->type = BT_INTEGER;
2518 ts->kind = gfc_default_integer_kind;
2519 goto get_kind;
2522 if ((matched_type && strcmp ("character", name) == 0)
2523 || (!matched_type && gfc_match (" character") == MATCH_YES))
2525 if (matched_type
2526 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2527 "intrinsic-type-spec at %C") == FAILURE)
2528 return MATCH_ERROR;
2530 ts->type = BT_CHARACTER;
2531 if (implicit_flag == 0)
2532 m = gfc_match_char_spec (ts);
2533 else
2534 m = MATCH_YES;
2536 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2537 m = MATCH_ERROR;
2539 return m;
2542 if ((matched_type && strcmp ("real", name) == 0)
2543 || (!matched_type && gfc_match (" real") == MATCH_YES))
2545 ts->type = BT_REAL;
2546 ts->kind = gfc_default_real_kind;
2547 goto get_kind;
2550 if ((matched_type
2551 && (strcmp ("doubleprecision", name) == 0
2552 || (strcmp ("double", name) == 0
2553 && gfc_match (" precision") == MATCH_YES)))
2554 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2556 if (matched_type
2557 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2558 "intrinsic-type-spec at %C") == FAILURE)
2559 return MATCH_ERROR;
2560 if (matched_type && gfc_match_char (')') != MATCH_YES)
2561 return MATCH_ERROR;
2563 ts->type = BT_REAL;
2564 ts->kind = gfc_default_double_kind;
2565 return MATCH_YES;
2568 if ((matched_type && strcmp ("complex", name) == 0)
2569 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2571 ts->type = BT_COMPLEX;
2572 ts->kind = gfc_default_complex_kind;
2573 goto get_kind;
2576 if ((matched_type
2577 && (strcmp ("doublecomplex", name) == 0
2578 || (strcmp ("double", name) == 0
2579 && gfc_match (" complex") == MATCH_YES)))
2580 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2582 if (gfc_notify_std (GFC_STD_GNU, "Extension: DOUBLE COMPLEX at %C")
2583 == FAILURE)
2584 return MATCH_ERROR;
2586 if (matched_type
2587 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2588 "intrinsic-type-spec at %C") == FAILURE)
2589 return MATCH_ERROR;
2591 if (matched_type && gfc_match_char (')') != MATCH_YES)
2592 return MATCH_ERROR;
2594 ts->type = BT_COMPLEX;
2595 ts->kind = gfc_default_double_kind;
2596 return MATCH_YES;
2599 if ((matched_type && strcmp ("logical", name) == 0)
2600 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2602 ts->type = BT_LOGICAL;
2603 ts->kind = gfc_default_logical_kind;
2604 goto get_kind;
2607 if (matched_type)
2608 m = gfc_match_char (')');
2610 if (m == MATCH_YES)
2611 ts->type = BT_DERIVED;
2612 else
2614 /* Match CLASS declarations. */
2615 m = gfc_match (" class ( * )");
2616 if (m == MATCH_ERROR)
2617 return MATCH_ERROR;
2618 else if (m == MATCH_YES)
2620 gfc_fatal_error ("Unlimited polymorphism at %C not yet supported");
2621 return MATCH_ERROR;
2624 m = gfc_match (" class ( %n )", name);
2625 if (m != MATCH_YES)
2626 return m;
2627 ts->type = BT_CLASS;
2629 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2630 == FAILURE)
2631 return MATCH_ERROR;
2634 /* Defer association of the derived type until the end of the
2635 specification block. However, if the derived type can be
2636 found, add it to the typespec. */
2637 if (gfc_matching_function)
2639 ts->u.derived = NULL;
2640 if (gfc_current_state () != COMP_INTERFACE
2641 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2642 ts->u.derived = sym;
2643 return MATCH_YES;
2646 /* Search for the name but allow the components to be defined later. If
2647 type = -1, this typespec has been seen in a function declaration but
2648 the type could not be accessed at that point. */
2649 sym = NULL;
2650 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2652 gfc_error ("Type name '%s' at %C is ambiguous", name);
2653 return MATCH_ERROR;
2655 else if (ts->kind == -1)
2657 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2658 || gfc_current_ns->has_import_set;
2659 if (gfc_find_symbol (name, NULL, iface, &sym))
2661 gfc_error ("Type name '%s' at %C is ambiguous", name);
2662 return MATCH_ERROR;
2665 ts->kind = 0;
2666 if (sym == NULL)
2667 return MATCH_NO;
2670 if (sym->attr.flavor != FL_DERIVED
2671 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2672 return MATCH_ERROR;
2674 gfc_set_sym_referenced (sym);
2675 ts->u.derived = sym;
2677 return MATCH_YES;
2679 get_kind:
2680 if (matched_type
2681 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2682 "intrinsic-type-spec at %C") == FAILURE)
2683 return MATCH_ERROR;
2685 /* For all types except double, derived and character, look for an
2686 optional kind specifier. MATCH_NO is actually OK at this point. */
2687 if (implicit_flag == 1)
2689 if (matched_type && gfc_match_char (')') != MATCH_YES)
2690 return MATCH_ERROR;
2692 return MATCH_YES;
2695 if (gfc_current_form == FORM_FREE)
2697 c = gfc_peek_ascii_char ();
2698 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2699 && c != ':' && c != ',')
2701 if (matched_type && c == ')')
2703 gfc_next_ascii_char ();
2704 return MATCH_YES;
2706 return MATCH_NO;
2710 m = gfc_match_kind_spec (ts, false);
2711 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2712 m = gfc_match_old_kind_spec (ts);
2714 if (matched_type && gfc_match_char (')') != MATCH_YES)
2715 return MATCH_ERROR;
2717 /* Defer association of the KIND expression of function results
2718 until after USE and IMPORT statements. */
2719 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2720 || gfc_matching_function)
2721 return MATCH_YES;
2723 if (m == MATCH_NO)
2724 m = MATCH_YES; /* No kind specifier found. */
2726 return m;
2730 /* Match an IMPLICIT NONE statement. Actually, this statement is
2731 already matched in parse.c, or we would not end up here in the
2732 first place. So the only thing we need to check, is if there is
2733 trailing garbage. If not, the match is successful. */
2735 match
2736 gfc_match_implicit_none (void)
2738 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2742 /* Match the letter range(s) of an IMPLICIT statement. */
2744 static match
2745 match_implicit_range (void)
2747 char c, c1, c2;
2748 int inner;
2749 locus cur_loc;
2751 cur_loc = gfc_current_locus;
2753 gfc_gobble_whitespace ();
2754 c = gfc_next_ascii_char ();
2755 if (c != '(')
2757 gfc_error ("Missing character range in IMPLICIT at %C");
2758 goto bad;
2761 inner = 1;
2762 while (inner)
2764 gfc_gobble_whitespace ();
2765 c1 = gfc_next_ascii_char ();
2766 if (!ISALPHA (c1))
2767 goto bad;
2769 gfc_gobble_whitespace ();
2770 c = gfc_next_ascii_char ();
2772 switch (c)
2774 case ')':
2775 inner = 0; /* Fall through. */
2777 case ',':
2778 c2 = c1;
2779 break;
2781 case '-':
2782 gfc_gobble_whitespace ();
2783 c2 = gfc_next_ascii_char ();
2784 if (!ISALPHA (c2))
2785 goto bad;
2787 gfc_gobble_whitespace ();
2788 c = gfc_next_ascii_char ();
2790 if ((c != ',') && (c != ')'))
2791 goto bad;
2792 if (c == ')')
2793 inner = 0;
2795 break;
2797 default:
2798 goto bad;
2801 if (c1 > c2)
2803 gfc_error ("Letters must be in alphabetic order in "
2804 "IMPLICIT statement at %C");
2805 goto bad;
2808 /* See if we can add the newly matched range to the pending
2809 implicits from this IMPLICIT statement. We do not check for
2810 conflicts with whatever earlier IMPLICIT statements may have
2811 set. This is done when we've successfully finished matching
2812 the current one. */
2813 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2814 goto bad;
2817 return MATCH_YES;
2819 bad:
2820 gfc_syntax_error (ST_IMPLICIT);
2822 gfc_current_locus = cur_loc;
2823 return MATCH_ERROR;
2827 /* Match an IMPLICIT statement, storing the types for
2828 gfc_set_implicit() if the statement is accepted by the parser.
2829 There is a strange looking, but legal syntactic construction
2830 possible. It looks like:
2832 IMPLICIT INTEGER (a-b) (c-d)
2834 This is legal if "a-b" is a constant expression that happens to
2835 equal one of the legal kinds for integers. The real problem
2836 happens with an implicit specification that looks like:
2838 IMPLICIT INTEGER (a-b)
2840 In this case, a typespec matcher that is "greedy" (as most of the
2841 matchers are) gobbles the character range as a kindspec, leaving
2842 nothing left. We therefore have to go a bit more slowly in the
2843 matching process by inhibiting the kindspec checking during
2844 typespec matching and checking for a kind later. */
2846 match
2847 gfc_match_implicit (void)
2849 gfc_typespec ts;
2850 locus cur_loc;
2851 char c;
2852 match m;
2854 gfc_clear_ts (&ts);
2856 /* We don't allow empty implicit statements. */
2857 if (gfc_match_eos () == MATCH_YES)
2859 gfc_error ("Empty IMPLICIT statement at %C");
2860 return MATCH_ERROR;
2865 /* First cleanup. */
2866 gfc_clear_new_implicit ();
2868 /* A basic type is mandatory here. */
2869 m = gfc_match_decl_type_spec (&ts, 1);
2870 if (m == MATCH_ERROR)
2871 goto error;
2872 if (m == MATCH_NO)
2873 goto syntax;
2875 cur_loc = gfc_current_locus;
2876 m = match_implicit_range ();
2878 if (m == MATCH_YES)
2880 /* We may have <TYPE> (<RANGE>). */
2881 gfc_gobble_whitespace ();
2882 c = gfc_next_ascii_char ();
2883 if ((c == '\n') || (c == ','))
2885 /* Check for CHARACTER with no length parameter. */
2886 if (ts.type == BT_CHARACTER && !ts.u.cl)
2888 ts.kind = gfc_default_character_kind;
2889 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2890 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
2891 NULL, 1);
2894 /* Record the Successful match. */
2895 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2896 return MATCH_ERROR;
2897 continue;
2900 gfc_current_locus = cur_loc;
2903 /* Discard the (incorrectly) matched range. */
2904 gfc_clear_new_implicit ();
2906 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2907 if (ts.type == BT_CHARACTER)
2908 m = gfc_match_char_spec (&ts);
2909 else
2911 m = gfc_match_kind_spec (&ts, false);
2912 if (m == MATCH_NO)
2914 m = gfc_match_old_kind_spec (&ts);
2915 if (m == MATCH_ERROR)
2916 goto error;
2917 if (m == MATCH_NO)
2918 goto syntax;
2921 if (m == MATCH_ERROR)
2922 goto error;
2924 m = match_implicit_range ();
2925 if (m == MATCH_ERROR)
2926 goto error;
2927 if (m == MATCH_NO)
2928 goto syntax;
2930 gfc_gobble_whitespace ();
2931 c = gfc_next_ascii_char ();
2932 if ((c != '\n') && (c != ','))
2933 goto syntax;
2935 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2936 return MATCH_ERROR;
2938 while (c == ',');
2940 return MATCH_YES;
2942 syntax:
2943 gfc_syntax_error (ST_IMPLICIT);
2945 error:
2946 return MATCH_ERROR;
2950 match
2951 gfc_match_import (void)
2953 char name[GFC_MAX_SYMBOL_LEN + 1];
2954 match m;
2955 gfc_symbol *sym;
2956 gfc_symtree *st;
2958 if (gfc_current_ns->proc_name == NULL
2959 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2961 gfc_error ("IMPORT statement at %C only permitted in "
2962 "an INTERFACE body");
2963 return MATCH_ERROR;
2966 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2967 == FAILURE)
2968 return MATCH_ERROR;
2970 if (gfc_match_eos () == MATCH_YES)
2972 /* All host variables should be imported. */
2973 gfc_current_ns->has_import_set = 1;
2974 return MATCH_YES;
2977 if (gfc_match (" ::") == MATCH_YES)
2979 if (gfc_match_eos () == MATCH_YES)
2981 gfc_error ("Expecting list of named entities at %C");
2982 return MATCH_ERROR;
2986 for(;;)
2988 m = gfc_match (" %n", name);
2989 switch (m)
2991 case MATCH_YES:
2992 if (gfc_current_ns->parent != NULL
2993 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2995 gfc_error ("Type name '%s' at %C is ambiguous", name);
2996 return MATCH_ERROR;
2998 else if (gfc_current_ns->proc_name->ns->parent != NULL
2999 && gfc_find_symbol (name,
3000 gfc_current_ns->proc_name->ns->parent,
3001 1, &sym))
3003 gfc_error ("Type name '%s' at %C is ambiguous", name);
3004 return MATCH_ERROR;
3007 if (sym == NULL)
3009 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3010 "at %C - does not exist.", name);
3011 return MATCH_ERROR;
3014 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
3016 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3017 "at %C.", name);
3018 goto next_item;
3021 st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
3022 st->n.sym = sym;
3023 sym->refs++;
3024 sym->attr.imported = 1;
3026 goto next_item;
3028 case MATCH_NO:
3029 break;
3031 case MATCH_ERROR:
3032 return MATCH_ERROR;
3035 next_item:
3036 if (gfc_match_eos () == MATCH_YES)
3037 break;
3038 if (gfc_match_char (',') != MATCH_YES)
3039 goto syntax;
3042 return MATCH_YES;
3044 syntax:
3045 gfc_error ("Syntax error in IMPORT statement at %C");
3046 return MATCH_ERROR;
3050 /* A minimal implementation of gfc_match without whitespace, escape
3051 characters or variable arguments. Returns true if the next
3052 characters match the TARGET template exactly. */
3054 static bool
3055 match_string_p (const char *target)
3057 const char *p;
3059 for (p = target; *p; p++)
3060 if ((char) gfc_next_ascii_char () != *p)
3061 return false;
3062 return true;
3065 /* Matches an attribute specification including array specs. If
3066 successful, leaves the variables current_attr and current_as
3067 holding the specification. Also sets the colon_seen variable for
3068 later use by matchers associated with initializations.
3070 This subroutine is a little tricky in the sense that we don't know
3071 if we really have an attr-spec until we hit the double colon.
3072 Until that time, we can only return MATCH_NO. This forces us to
3073 check for duplicate specification at this level. */
3075 static match
3076 match_attr_spec (void)
3078 /* Modifiers that can exist in a type statement. */
3079 typedef enum
3080 { GFC_DECL_BEGIN = 0,
3081 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3082 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3083 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3084 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3085 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3086 DECL_NONE, GFC_DECL_END /* Sentinel */
3088 decl_types;
3090 /* GFC_DECL_END is the sentinel, index starts at 0. */
3091 #define NUM_DECL GFC_DECL_END
3093 locus start, seen_at[NUM_DECL];
3094 int seen[NUM_DECL];
3095 unsigned int d;
3096 const char *attr;
3097 match m;
3098 gfc_try t;
3100 gfc_clear_attr (&current_attr);
3101 start = gfc_current_locus;
3103 current_as = NULL;
3104 colon_seen = 0;
3106 /* See if we get all of the keywords up to the final double colon. */
3107 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3108 seen[d] = 0;
3110 for (;;)
3112 char ch;
3114 d = DECL_NONE;
3115 gfc_gobble_whitespace ();
3117 ch = gfc_next_ascii_char ();
3118 if (ch == ':')
3120 /* This is the successful exit condition for the loop. */
3121 if (gfc_next_ascii_char () == ':')
3122 break;
3124 else if (ch == ',')
3126 gfc_gobble_whitespace ();
3127 switch (gfc_peek_ascii_char ())
3129 case 'a':
3130 gfc_next_ascii_char ();
3131 switch (gfc_next_ascii_char ())
3133 case 'l':
3134 if (match_string_p ("locatable"))
3136 /* Matched "allocatable". */
3137 d = DECL_ALLOCATABLE;
3139 break;
3141 case 's':
3142 if (match_string_p ("ynchronous"))
3144 /* Matched "asynchronous". */
3145 d = DECL_ASYNCHRONOUS;
3147 break;
3149 break;
3151 case 'b':
3152 /* Try and match the bind(c). */
3153 m = gfc_match_bind_c (NULL, true);
3154 if (m == MATCH_YES)
3155 d = DECL_IS_BIND_C;
3156 else if (m == MATCH_ERROR)
3157 goto cleanup;
3158 break;
3160 case 'c':
3161 gfc_next_ascii_char ();
3162 if ('o' != gfc_next_ascii_char ())
3163 break;
3164 switch (gfc_next_ascii_char ())
3166 case 'd':
3167 if (match_string_p ("imension"))
3169 d = DECL_CODIMENSION;
3170 break;
3172 case 'n':
3173 if (match_string_p ("tiguous"))
3175 d = DECL_CONTIGUOUS;
3176 break;
3179 break;
3181 case 'd':
3182 if (match_string_p ("dimension"))
3183 d = DECL_DIMENSION;
3184 break;
3186 case 'e':
3187 if (match_string_p ("external"))
3188 d = DECL_EXTERNAL;
3189 break;
3191 case 'i':
3192 if (match_string_p ("int"))
3194 ch = gfc_next_ascii_char ();
3195 if (ch == 'e')
3197 if (match_string_p ("nt"))
3199 /* Matched "intent". */
3200 /* TODO: Call match_intent_spec from here. */
3201 if (gfc_match (" ( in out )") == MATCH_YES)
3202 d = DECL_INOUT;
3203 else if (gfc_match (" ( in )") == MATCH_YES)
3204 d = DECL_IN;
3205 else if (gfc_match (" ( out )") == MATCH_YES)
3206 d = DECL_OUT;
3209 else if (ch == 'r')
3211 if (match_string_p ("insic"))
3213 /* Matched "intrinsic". */
3214 d = DECL_INTRINSIC;
3218 break;
3220 case 'o':
3221 if (match_string_p ("optional"))
3222 d = DECL_OPTIONAL;
3223 break;
3225 case 'p':
3226 gfc_next_ascii_char ();
3227 switch (gfc_next_ascii_char ())
3229 case 'a':
3230 if (match_string_p ("rameter"))
3232 /* Matched "parameter". */
3233 d = DECL_PARAMETER;
3235 break;
3237 case 'o':
3238 if (match_string_p ("inter"))
3240 /* Matched "pointer". */
3241 d = DECL_POINTER;
3243 break;
3245 case 'r':
3246 ch = gfc_next_ascii_char ();
3247 if (ch == 'i')
3249 if (match_string_p ("vate"))
3251 /* Matched "private". */
3252 d = DECL_PRIVATE;
3255 else if (ch == 'o')
3257 if (match_string_p ("tected"))
3259 /* Matched "protected". */
3260 d = DECL_PROTECTED;
3263 break;
3265 case 'u':
3266 if (match_string_p ("blic"))
3268 /* Matched "public". */
3269 d = DECL_PUBLIC;
3271 break;
3273 break;
3275 case 's':
3276 if (match_string_p ("save"))
3277 d = DECL_SAVE;
3278 break;
3280 case 't':
3281 if (match_string_p ("target"))
3282 d = DECL_TARGET;
3283 break;
3285 case 'v':
3286 gfc_next_ascii_char ();
3287 ch = gfc_next_ascii_char ();
3288 if (ch == 'a')
3290 if (match_string_p ("lue"))
3292 /* Matched "value". */
3293 d = DECL_VALUE;
3296 else if (ch == 'o')
3298 if (match_string_p ("latile"))
3300 /* Matched "volatile". */
3301 d = DECL_VOLATILE;
3304 break;
3308 /* No double colon and no recognizable decl_type, so assume that
3309 we've been looking at something else the whole time. */
3310 if (d == DECL_NONE)
3312 m = MATCH_NO;
3313 goto cleanup;
3316 /* Check to make sure any parens are paired up correctly. */
3317 if (gfc_match_parens () == MATCH_ERROR)
3319 m = MATCH_ERROR;
3320 goto cleanup;
3323 seen[d]++;
3324 seen_at[d] = gfc_current_locus;
3326 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3328 gfc_array_spec *as = NULL;
3330 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3331 d == DECL_CODIMENSION);
3333 if (current_as == NULL)
3334 current_as = as;
3335 else if (m == MATCH_YES)
3337 merge_array_spec (as, current_as, false);
3338 free (as);
3341 if (m == MATCH_NO)
3343 if (d == DECL_CODIMENSION)
3344 gfc_error ("Missing codimension specification at %C");
3345 else
3346 gfc_error ("Missing dimension specification at %C");
3347 m = MATCH_ERROR;
3350 if (m == MATCH_ERROR)
3351 goto cleanup;
3355 /* Since we've seen a double colon, we have to be looking at an
3356 attr-spec. This means that we can now issue errors. */
3357 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3358 if (seen[d] > 1)
3360 switch (d)
3362 case DECL_ALLOCATABLE:
3363 attr = "ALLOCATABLE";
3364 break;
3365 case DECL_ASYNCHRONOUS:
3366 attr = "ASYNCHRONOUS";
3367 break;
3368 case DECL_CODIMENSION:
3369 attr = "CODIMENSION";
3370 break;
3371 case DECL_CONTIGUOUS:
3372 attr = "CONTIGUOUS";
3373 break;
3374 case DECL_DIMENSION:
3375 attr = "DIMENSION";
3376 break;
3377 case DECL_EXTERNAL:
3378 attr = "EXTERNAL";
3379 break;
3380 case DECL_IN:
3381 attr = "INTENT (IN)";
3382 break;
3383 case DECL_OUT:
3384 attr = "INTENT (OUT)";
3385 break;
3386 case DECL_INOUT:
3387 attr = "INTENT (IN OUT)";
3388 break;
3389 case DECL_INTRINSIC:
3390 attr = "INTRINSIC";
3391 break;
3392 case DECL_OPTIONAL:
3393 attr = "OPTIONAL";
3394 break;
3395 case DECL_PARAMETER:
3396 attr = "PARAMETER";
3397 break;
3398 case DECL_POINTER:
3399 attr = "POINTER";
3400 break;
3401 case DECL_PROTECTED:
3402 attr = "PROTECTED";
3403 break;
3404 case DECL_PRIVATE:
3405 attr = "PRIVATE";
3406 break;
3407 case DECL_PUBLIC:
3408 attr = "PUBLIC";
3409 break;
3410 case DECL_SAVE:
3411 attr = "SAVE";
3412 break;
3413 case DECL_TARGET:
3414 attr = "TARGET";
3415 break;
3416 case DECL_IS_BIND_C:
3417 attr = "IS_BIND_C";
3418 break;
3419 case DECL_VALUE:
3420 attr = "VALUE";
3421 break;
3422 case DECL_VOLATILE:
3423 attr = "VOLATILE";
3424 break;
3425 default:
3426 attr = NULL; /* This shouldn't happen. */
3429 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3430 m = MATCH_ERROR;
3431 goto cleanup;
3434 /* Now that we've dealt with duplicate attributes, add the attributes
3435 to the current attribute. */
3436 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3438 if (seen[d] == 0)
3439 continue;
3441 if (gfc_current_state () == COMP_DERIVED
3442 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3443 && d != DECL_POINTER && d != DECL_PRIVATE
3444 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3446 if (d == DECL_ALLOCATABLE)
3448 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3449 "attribute at %C in a TYPE definition")
3450 == FAILURE)
3452 m = MATCH_ERROR;
3453 goto cleanup;
3456 else
3458 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3459 &seen_at[d]);
3460 m = MATCH_ERROR;
3461 goto cleanup;
3465 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3466 && gfc_current_state () != COMP_MODULE)
3468 if (d == DECL_PRIVATE)
3469 attr = "PRIVATE";
3470 else
3471 attr = "PUBLIC";
3472 if (gfc_current_state () == COMP_DERIVED
3473 && gfc_state_stack->previous
3474 && gfc_state_stack->previous->state == COMP_MODULE)
3476 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3477 "at %L in a TYPE definition", attr,
3478 &seen_at[d])
3479 == FAILURE)
3481 m = MATCH_ERROR;
3482 goto cleanup;
3485 else
3487 gfc_error ("%s attribute at %L is not allowed outside of the "
3488 "specification part of a module", attr, &seen_at[d]);
3489 m = MATCH_ERROR;
3490 goto cleanup;
3494 switch (d)
3496 case DECL_ALLOCATABLE:
3497 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3498 break;
3500 case DECL_ASYNCHRONOUS:
3501 if (gfc_notify_std (GFC_STD_F2003,
3502 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3503 == FAILURE)
3504 t = FAILURE;
3505 else
3506 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3507 break;
3509 case DECL_CODIMENSION:
3510 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3511 break;
3513 case DECL_CONTIGUOUS:
3514 if (gfc_notify_std (GFC_STD_F2008,
3515 "Fortran 2008: CONTIGUOUS attribute at %C")
3516 == FAILURE)
3517 t = FAILURE;
3518 else
3519 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3520 break;
3522 case DECL_DIMENSION:
3523 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3524 break;
3526 case DECL_EXTERNAL:
3527 t = gfc_add_external (&current_attr, &seen_at[d]);
3528 break;
3530 case DECL_IN:
3531 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3532 break;
3534 case DECL_OUT:
3535 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3536 break;
3538 case DECL_INOUT:
3539 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3540 break;
3542 case DECL_INTRINSIC:
3543 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3544 break;
3546 case DECL_OPTIONAL:
3547 t = gfc_add_optional (&current_attr, &seen_at[d]);
3548 break;
3550 case DECL_PARAMETER:
3551 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3552 break;
3554 case DECL_POINTER:
3555 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3556 break;
3558 case DECL_PROTECTED:
3559 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3561 gfc_error ("PROTECTED at %C only allowed in specification "
3562 "part of a module");
3563 t = FAILURE;
3564 break;
3567 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3568 "attribute at %C")
3569 == FAILURE)
3570 t = FAILURE;
3571 else
3572 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3573 break;
3575 case DECL_PRIVATE:
3576 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3577 &seen_at[d]);
3578 break;
3580 case DECL_PUBLIC:
3581 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3582 &seen_at[d]);
3583 break;
3585 case DECL_SAVE:
3586 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3587 break;
3589 case DECL_TARGET:
3590 t = gfc_add_target (&current_attr, &seen_at[d]);
3591 break;
3593 case DECL_IS_BIND_C:
3594 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3595 break;
3597 case DECL_VALUE:
3598 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3599 "at %C")
3600 == FAILURE)
3601 t = FAILURE;
3602 else
3603 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3604 break;
3606 case DECL_VOLATILE:
3607 if (gfc_notify_std (GFC_STD_F2003,
3608 "Fortran 2003: VOLATILE attribute at %C")
3609 == FAILURE)
3610 t = FAILURE;
3611 else
3612 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3613 break;
3615 default:
3616 gfc_internal_error ("match_attr_spec(): Bad attribute");
3619 if (t == FAILURE)
3621 m = MATCH_ERROR;
3622 goto cleanup;
3626 /* Module variables implicitly have the SAVE attribute. */
3627 if (gfc_current_state () == COMP_MODULE && !current_attr.save)
3628 current_attr.save = SAVE_IMPLICIT;
3630 colon_seen = 1;
3631 return MATCH_YES;
3633 cleanup:
3634 gfc_current_locus = start;
3635 gfc_free_array_spec (current_as);
3636 current_as = NULL;
3637 return m;
3641 /* Set the binding label, dest_label, either with the binding label
3642 stored in the given gfc_typespec, ts, or if none was provided, it
3643 will be the symbol name in all lower case, as required by the draft
3644 (J3/04-007, section 15.4.1). If a binding label was given and
3645 there is more than one argument (num_idents), it is an error. */
3647 gfc_try
3648 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3650 if (num_idents > 1 && has_name_equals)
3652 gfc_error ("Multiple identifiers provided with "
3653 "single NAME= specifier at %C");
3654 return FAILURE;
3657 if (curr_binding_label[0] != '\0')
3659 /* Binding label given; store in temp holder til have sym. */
3660 strcpy (dest_label, curr_binding_label);
3662 else
3664 /* No binding label given, and the NAME= specifier did not exist,
3665 which means there was no NAME="". */
3666 if (sym_name != NULL && has_name_equals == 0)
3667 strcpy (dest_label, sym_name);
3670 return SUCCESS;
3674 /* Set the status of the given common block as being BIND(C) or not,
3675 depending on the given parameter, is_bind_c. */
3677 void
3678 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3680 com_block->is_bind_c = is_bind_c;
3681 return;
3685 /* Verify that the given gfc_typespec is for a C interoperable type. */
3687 gfc_try
3688 verify_c_interop (gfc_typespec *ts)
3690 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3691 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3692 ? SUCCESS : FAILURE;
3693 else if (ts->is_c_interop != 1)
3694 return FAILURE;
3696 return SUCCESS;
3700 /* Verify that the variables of a given common block, which has been
3701 defined with the attribute specifier bind(c), to be of a C
3702 interoperable type. Errors will be reported here, if
3703 encountered. */
3705 gfc_try
3706 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3708 gfc_symbol *curr_sym = NULL;
3709 gfc_try retval = SUCCESS;
3711 curr_sym = com_block->head;
3713 /* Make sure we have at least one symbol. */
3714 if (curr_sym == NULL)
3715 return retval;
3717 /* Here we know we have a symbol, so we'll execute this loop
3718 at least once. */
3721 /* The second to last param, 1, says this is in a common block. */
3722 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3723 curr_sym = curr_sym->common_next;
3724 } while (curr_sym != NULL);
3726 return retval;
3730 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3731 an appropriate error message is reported. */
3733 gfc_try
3734 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3735 int is_in_common, gfc_common_head *com_block)
3737 bool bind_c_function = false;
3738 gfc_try retval = SUCCESS;
3740 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3741 bind_c_function = true;
3743 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3745 tmp_sym = tmp_sym->result;
3746 /* Make sure it wasn't an implicitly typed result. */
3747 if (tmp_sym->attr.implicit_type)
3749 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3750 "%L may not be C interoperable", tmp_sym->name,
3751 &tmp_sym->declared_at);
3752 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3753 /* Mark it as C interoperable to prevent duplicate warnings. */
3754 tmp_sym->ts.is_c_interop = 1;
3755 tmp_sym->attr.is_c_interop = 1;
3759 /* Here, we know we have the bind(c) attribute, so if we have
3760 enough type info, then verify that it's a C interop kind.
3761 The info could be in the symbol already, or possibly still in
3762 the given ts (current_ts), so look in both. */
3763 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3765 if (verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3767 /* See if we're dealing with a sym in a common block or not. */
3768 if (is_in_common == 1)
3770 gfc_warning ("Variable '%s' in common block '%s' at %L "
3771 "may not be a C interoperable "
3772 "kind though common block '%s' is BIND(C)",
3773 tmp_sym->name, com_block->name,
3774 &(tmp_sym->declared_at), com_block->name);
3776 else
3778 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3779 gfc_error ("Type declaration '%s' at %L is not C "
3780 "interoperable but it is BIND(C)",
3781 tmp_sym->name, &(tmp_sym->declared_at));
3782 else
3783 gfc_warning ("Variable '%s' at %L "
3784 "may not be a C interoperable "
3785 "kind but it is bind(c)",
3786 tmp_sym->name, &(tmp_sym->declared_at));
3790 /* Variables declared w/in a common block can't be bind(c)
3791 since there's no way for C to see these variables, so there's
3792 semantically no reason for the attribute. */
3793 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3795 gfc_error ("Variable '%s' in common block '%s' at "
3796 "%L cannot be declared with BIND(C) "
3797 "since it is not a global",
3798 tmp_sym->name, com_block->name,
3799 &(tmp_sym->declared_at));
3800 retval = FAILURE;
3803 /* Scalar variables that are bind(c) can not have the pointer
3804 or allocatable attributes. */
3805 if (tmp_sym->attr.is_bind_c == 1)
3807 if (tmp_sym->attr.pointer == 1)
3809 gfc_error ("Variable '%s' at %L cannot have both the "
3810 "POINTER and BIND(C) attributes",
3811 tmp_sym->name, &(tmp_sym->declared_at));
3812 retval = FAILURE;
3815 if (tmp_sym->attr.allocatable == 1)
3817 gfc_error ("Variable '%s' at %L cannot have both the "
3818 "ALLOCATABLE and BIND(C) attributes",
3819 tmp_sym->name, &(tmp_sym->declared_at));
3820 retval = FAILURE;
3825 /* If it is a BIND(C) function, make sure the return value is a
3826 scalar value. The previous tests in this function made sure
3827 the type is interoperable. */
3828 if (bind_c_function && tmp_sym->as != NULL)
3829 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3830 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3832 /* BIND(C) functions can not return a character string. */
3833 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3834 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
3835 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
3836 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
3837 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3838 "be a character string", tmp_sym->name,
3839 &(tmp_sym->declared_at));
3842 /* See if the symbol has been marked as private. If it has, make sure
3843 there is no binding label and warn the user if there is one. */
3844 if (tmp_sym->attr.access == ACCESS_PRIVATE
3845 && tmp_sym->binding_label[0] != '\0')
3846 /* Use gfc_warning_now because we won't say that the symbol fails
3847 just because of this. */
3848 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3849 "given the binding label '%s'", tmp_sym->name,
3850 &(tmp_sym->declared_at), tmp_sym->binding_label);
3852 return retval;
3856 /* Set the appropriate fields for a symbol that's been declared as
3857 BIND(C) (the is_bind_c flag and the binding label), and verify that
3858 the type is C interoperable. Errors are reported by the functions
3859 used to set/test these fields. */
3861 gfc_try
3862 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3864 gfc_try retval = SUCCESS;
3866 /* TODO: Do we need to make sure the vars aren't marked private? */
3868 /* Set the is_bind_c bit in symbol_attribute. */
3869 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3871 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3872 num_idents) != SUCCESS)
3873 return FAILURE;
3875 return retval;
3879 /* Set the fields marking the given common block as BIND(C), including
3880 a binding label, and report any errors encountered. */
3882 gfc_try
3883 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3885 gfc_try retval = SUCCESS;
3887 /* destLabel, common name, typespec (which may have binding label). */
3888 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3889 != SUCCESS)
3890 return FAILURE;
3892 /* Set the given common block (com_block) to being bind(c) (1). */
3893 set_com_block_bind_c (com_block, 1);
3895 return retval;
3899 /* Retrieve the list of one or more identifiers that the given bind(c)
3900 attribute applies to. */
3902 gfc_try
3903 get_bind_c_idents (void)
3905 char name[GFC_MAX_SYMBOL_LEN + 1];
3906 int num_idents = 0;
3907 gfc_symbol *tmp_sym = NULL;
3908 match found_id;
3909 gfc_common_head *com_block = NULL;
3911 if (gfc_match_name (name) == MATCH_YES)
3913 found_id = MATCH_YES;
3914 gfc_get_ha_symbol (name, &tmp_sym);
3916 else if (match_common_name (name) == MATCH_YES)
3918 found_id = MATCH_YES;
3919 com_block = gfc_get_common (name, 0);
3921 else
3923 gfc_error ("Need either entity or common block name for "
3924 "attribute specification statement at %C");
3925 return FAILURE;
3928 /* Save the current identifier and look for more. */
3931 /* Increment the number of identifiers found for this spec stmt. */
3932 num_idents++;
3934 /* Make sure we have a sym or com block, and verify that it can
3935 be bind(c). Set the appropriate field(s) and look for more
3936 identifiers. */
3937 if (tmp_sym != NULL || com_block != NULL)
3939 if (tmp_sym != NULL)
3941 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3942 != SUCCESS)
3943 return FAILURE;
3945 else
3947 if (set_verify_bind_c_com_block(com_block, num_idents)
3948 != SUCCESS)
3949 return FAILURE;
3952 /* Look to see if we have another identifier. */
3953 tmp_sym = NULL;
3954 if (gfc_match_eos () == MATCH_YES)
3955 found_id = MATCH_NO;
3956 else if (gfc_match_char (',') != MATCH_YES)
3957 found_id = MATCH_NO;
3958 else if (gfc_match_name (name) == MATCH_YES)
3960 found_id = MATCH_YES;
3961 gfc_get_ha_symbol (name, &tmp_sym);
3963 else if (match_common_name (name) == MATCH_YES)
3965 found_id = MATCH_YES;
3966 com_block = gfc_get_common (name, 0);
3968 else
3970 gfc_error ("Missing entity or common block name for "
3971 "attribute specification statement at %C");
3972 return FAILURE;
3975 else
3977 gfc_internal_error ("Missing symbol");
3979 } while (found_id == MATCH_YES);
3981 /* if we get here we were successful */
3982 return SUCCESS;
3986 /* Try and match a BIND(C) attribute specification statement. */
3988 match
3989 gfc_match_bind_c_stmt (void)
3991 match found_match = MATCH_NO;
3992 gfc_typespec *ts;
3994 ts = &current_ts;
3996 /* This may not be necessary. */
3997 gfc_clear_ts (ts);
3998 /* Clear the temporary binding label holder. */
3999 curr_binding_label[0] = '\0';
4001 /* Look for the bind(c). */
4002 found_match = gfc_match_bind_c (NULL, true);
4004 if (found_match == MATCH_YES)
4006 /* Look for the :: now, but it is not required. */
4007 gfc_match (" :: ");
4009 /* Get the identifier(s) that needs to be updated. This may need to
4010 change to hand the flag(s) for the attr specified so all identifiers
4011 found can have all appropriate parts updated (assuming that the same
4012 spec stmt can have multiple attrs, such as both bind(c) and
4013 allocatable...). */
4014 if (get_bind_c_idents () != SUCCESS)
4015 /* Error message should have printed already. */
4016 return MATCH_ERROR;
4019 return found_match;
4023 /* Match a data declaration statement. */
4025 match
4026 gfc_match_data_decl (void)
4028 gfc_symbol *sym;
4029 match m;
4030 int elem;
4032 num_idents_on_line = 0;
4034 m = gfc_match_decl_type_spec (&current_ts, 0);
4035 if (m != MATCH_YES)
4036 return m;
4038 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4039 && gfc_current_state () != COMP_DERIVED)
4041 sym = gfc_use_derived (current_ts.u.derived);
4043 if (sym == NULL)
4045 m = MATCH_ERROR;
4046 goto cleanup;
4049 current_ts.u.derived = sym;
4052 m = match_attr_spec ();
4053 if (m == MATCH_ERROR)
4055 m = MATCH_NO;
4056 goto cleanup;
4059 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4060 && current_ts.u.derived->components == NULL
4061 && !current_ts.u.derived->attr.zero_comp)
4064 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4065 goto ok;
4067 gfc_find_symbol (current_ts.u.derived->name,
4068 current_ts.u.derived->ns->parent, 1, &sym);
4070 /* Any symbol that we find had better be a type definition
4071 which has its components defined. */
4072 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4073 && (current_ts.u.derived->components != NULL
4074 || current_ts.u.derived->attr.zero_comp))
4075 goto ok;
4077 /* Now we have an error, which we signal, and then fix up
4078 because the knock-on is plain and simple confusing. */
4079 gfc_error_now ("Derived type at %C has not been previously defined "
4080 "and so cannot appear in a derived type definition");
4081 current_attr.pointer = 1;
4082 goto ok;
4086 /* If we have an old-style character declaration, and no new-style
4087 attribute specifications, then there a comma is optional between
4088 the type specification and the variable list. */
4089 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4090 gfc_match_char (',');
4092 /* Give the types/attributes to symbols that follow. Give the element
4093 a number so that repeat character length expressions can be copied. */
4094 elem = 1;
4095 for (;;)
4097 num_idents_on_line++;
4098 m = variable_decl (elem++);
4099 if (m == MATCH_ERROR)
4100 goto cleanup;
4101 if (m == MATCH_NO)
4102 break;
4104 if (gfc_match_eos () == MATCH_YES)
4105 goto cleanup;
4106 if (gfc_match_char (',') != MATCH_YES)
4107 break;
4110 if (gfc_error_flag_test () == 0)
4111 gfc_error ("Syntax error in data declaration at %C");
4112 m = MATCH_ERROR;
4114 gfc_free_data_all (gfc_current_ns);
4116 cleanup:
4117 gfc_free_array_spec (current_as);
4118 current_as = NULL;
4119 return m;
4123 /* Match a prefix associated with a function or subroutine
4124 declaration. If the typespec pointer is nonnull, then a typespec
4125 can be matched. Note that if nothing matches, MATCH_YES is
4126 returned (the null string was matched). */
4128 match
4129 gfc_match_prefix (gfc_typespec *ts)
4131 bool seen_type;
4132 bool seen_impure;
4133 bool found_prefix;
4135 gfc_clear_attr (&current_attr);
4136 seen_type = false;
4137 seen_impure = false;
4139 gcc_assert (!gfc_matching_prefix);
4140 gfc_matching_prefix = true;
4144 found_prefix = false;
4146 if (!seen_type && ts != NULL
4147 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4148 && gfc_match_space () == MATCH_YES)
4151 seen_type = true;
4152 found_prefix = true;
4155 if (gfc_match ("elemental% ") == MATCH_YES)
4157 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
4158 goto error;
4160 found_prefix = true;
4163 if (gfc_match ("pure% ") == MATCH_YES)
4165 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
4166 goto error;
4168 found_prefix = true;
4171 if (gfc_match ("recursive% ") == MATCH_YES)
4173 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
4174 goto error;
4176 found_prefix = true;
4179 /* IMPURE is a somewhat special case, as it needs not set an actual
4180 attribute but rather only prevents ELEMENTAL routines from being
4181 automatically PURE. */
4182 if (gfc_match ("impure% ") == MATCH_YES)
4184 if (gfc_notify_std (GFC_STD_F2008,
4185 "Fortran 2008: IMPURE procedure at %C")
4186 == FAILURE)
4187 goto error;
4189 seen_impure = true;
4190 found_prefix = true;
4193 while (found_prefix);
4195 /* IMPURE and PURE must not both appear, of course. */
4196 if (seen_impure && current_attr.pure)
4198 gfc_error ("PURE and IMPURE must not appear both at %C");
4199 goto error;
4202 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4203 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4205 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
4206 goto error;
4209 /* At this point, the next item is not a prefix. */
4210 gcc_assert (gfc_matching_prefix);
4211 gfc_matching_prefix = false;
4212 return MATCH_YES;
4214 error:
4215 gcc_assert (gfc_matching_prefix);
4216 gfc_matching_prefix = false;
4217 return MATCH_ERROR;
4221 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4223 static gfc_try
4224 copy_prefix (symbol_attribute *dest, locus *where)
4226 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
4227 return FAILURE;
4229 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
4230 return FAILURE;
4232 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
4233 return FAILURE;
4235 return SUCCESS;
4239 /* Match a formal argument list. */
4241 match
4242 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4244 gfc_formal_arglist *head, *tail, *p, *q;
4245 char name[GFC_MAX_SYMBOL_LEN + 1];
4246 gfc_symbol *sym;
4247 match m;
4249 head = tail = NULL;
4251 if (gfc_match_char ('(') != MATCH_YES)
4253 if (null_flag)
4254 goto ok;
4255 return MATCH_NO;
4258 if (gfc_match_char (')') == MATCH_YES)
4259 goto ok;
4261 for (;;)
4263 if (gfc_match_char ('*') == MATCH_YES)
4264 sym = NULL;
4265 else
4267 m = gfc_match_name (name);
4268 if (m != MATCH_YES)
4269 goto cleanup;
4271 if (gfc_get_symbol (name, NULL, &sym))
4272 goto cleanup;
4275 p = gfc_get_formal_arglist ();
4277 if (head == NULL)
4278 head = tail = p;
4279 else
4281 tail->next = p;
4282 tail = p;
4285 tail->sym = sym;
4287 /* We don't add the VARIABLE flavor because the name could be a
4288 dummy procedure. We don't apply these attributes to formal
4289 arguments of statement functions. */
4290 if (sym != NULL && !st_flag
4291 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
4292 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
4294 m = MATCH_ERROR;
4295 goto cleanup;
4298 /* The name of a program unit can be in a different namespace,
4299 so check for it explicitly. After the statement is accepted,
4300 the name is checked for especially in gfc_get_symbol(). */
4301 if (gfc_new_block != NULL && sym != NULL
4302 && strcmp (sym->name, gfc_new_block->name) == 0)
4304 gfc_error ("Name '%s' at %C is the name of the procedure",
4305 sym->name);
4306 m = MATCH_ERROR;
4307 goto cleanup;
4310 if (gfc_match_char (')') == MATCH_YES)
4311 goto ok;
4313 m = gfc_match_char (',');
4314 if (m != MATCH_YES)
4316 gfc_error ("Unexpected junk in formal argument list at %C");
4317 goto cleanup;
4322 /* Check for duplicate symbols in the formal argument list. */
4323 if (head != NULL)
4325 for (p = head; p->next; p = p->next)
4327 if (p->sym == NULL)
4328 continue;
4330 for (q = p->next; q; q = q->next)
4331 if (p->sym == q->sym)
4333 gfc_error ("Duplicate symbol '%s' in formal argument list "
4334 "at %C", p->sym->name);
4336 m = MATCH_ERROR;
4337 goto cleanup;
4342 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
4343 == FAILURE)
4345 m = MATCH_ERROR;
4346 goto cleanup;
4349 return MATCH_YES;
4351 cleanup:
4352 gfc_free_formal_arglist (head);
4353 return m;
4357 /* Match a RESULT specification following a function declaration or
4358 ENTRY statement. Also matches the end-of-statement. */
4360 static match
4361 match_result (gfc_symbol *function, gfc_symbol **result)
4363 char name[GFC_MAX_SYMBOL_LEN + 1];
4364 gfc_symbol *r;
4365 match m;
4367 if (gfc_match (" result (") != MATCH_YES)
4368 return MATCH_NO;
4370 m = gfc_match_name (name);
4371 if (m != MATCH_YES)
4372 return m;
4374 /* Get the right paren, and that's it because there could be the
4375 bind(c) attribute after the result clause. */
4376 if (gfc_match_char(')') != MATCH_YES)
4378 /* TODO: should report the missing right paren here. */
4379 return MATCH_ERROR;
4382 if (strcmp (function->name, name) == 0)
4384 gfc_error ("RESULT variable at %C must be different than function name");
4385 return MATCH_ERROR;
4388 if (gfc_get_symbol (name, NULL, &r))
4389 return MATCH_ERROR;
4391 if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4392 return MATCH_ERROR;
4394 *result = r;
4396 return MATCH_YES;
4400 /* Match a function suffix, which could be a combination of a result
4401 clause and BIND(C), either one, or neither. The draft does not
4402 require them to come in a specific order. */
4404 match
4405 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4407 match is_bind_c; /* Found bind(c). */
4408 match is_result; /* Found result clause. */
4409 match found_match; /* Status of whether we've found a good match. */
4410 char peek_char; /* Character we're going to peek at. */
4411 bool allow_binding_name;
4413 /* Initialize to having found nothing. */
4414 found_match = MATCH_NO;
4415 is_bind_c = MATCH_NO;
4416 is_result = MATCH_NO;
4418 /* Get the next char to narrow between result and bind(c). */
4419 gfc_gobble_whitespace ();
4420 peek_char = gfc_peek_ascii_char ();
4422 /* C binding names are not allowed for internal procedures. */
4423 if (gfc_current_state () == COMP_CONTAINS
4424 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4425 allow_binding_name = false;
4426 else
4427 allow_binding_name = true;
4429 switch (peek_char)
4431 case 'r':
4432 /* Look for result clause. */
4433 is_result = match_result (sym, result);
4434 if (is_result == MATCH_YES)
4436 /* Now see if there is a bind(c) after it. */
4437 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4438 /* We've found the result clause and possibly bind(c). */
4439 found_match = MATCH_YES;
4441 else
4442 /* This should only be MATCH_ERROR. */
4443 found_match = is_result;
4444 break;
4445 case 'b':
4446 /* Look for bind(c) first. */
4447 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4448 if (is_bind_c == MATCH_YES)
4450 /* Now see if a result clause followed it. */
4451 is_result = match_result (sym, result);
4452 found_match = MATCH_YES;
4454 else
4456 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4457 found_match = MATCH_ERROR;
4459 break;
4460 default:
4461 gfc_error ("Unexpected junk after function declaration at %C");
4462 found_match = MATCH_ERROR;
4463 break;
4466 if (is_bind_c == MATCH_YES)
4468 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4469 if (gfc_current_state () == COMP_CONTAINS
4470 && sym->ns->proc_name->attr.flavor != FL_MODULE
4471 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4472 "at %L may not be specified for an internal "
4473 "procedure", &gfc_current_locus)
4474 == FAILURE)
4475 return MATCH_ERROR;
4477 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4478 == FAILURE)
4479 return MATCH_ERROR;
4482 return found_match;
4486 /* Procedure pointer return value without RESULT statement:
4487 Add "hidden" result variable named "ppr@". */
4489 static gfc_try
4490 add_hidden_procptr_result (gfc_symbol *sym)
4492 bool case1,case2;
4494 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4495 return FAILURE;
4497 /* First usage case: PROCEDURE and EXTERNAL statements. */
4498 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4499 && strcmp (gfc_current_block ()->name, sym->name) == 0
4500 && sym->attr.external;
4501 /* Second usage case: INTERFACE statements. */
4502 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4503 && gfc_state_stack->previous->state == COMP_FUNCTION
4504 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4506 if (case1 || case2)
4508 gfc_symtree *stree;
4509 if (case1)
4510 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4511 else if (case2)
4513 gfc_symtree *st2;
4514 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4515 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4516 st2->n.sym = stree->n.sym;
4518 sym->result = stree->n.sym;
4520 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4521 sym->result->attr.pointer = sym->attr.pointer;
4522 sym->result->attr.external = sym->attr.external;
4523 sym->result->attr.referenced = sym->attr.referenced;
4524 sym->result->ts = sym->ts;
4525 sym->attr.proc_pointer = 0;
4526 sym->attr.pointer = 0;
4527 sym->attr.external = 0;
4528 if (sym->result->attr.external && sym->result->attr.pointer)
4530 sym->result->attr.pointer = 0;
4531 sym->result->attr.proc_pointer = 1;
4534 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4536 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4537 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4538 && sym->result && sym->result != sym && sym->result->attr.external
4539 && sym == gfc_current_ns->proc_name
4540 && sym == sym->result->ns->proc_name
4541 && strcmp ("ppr@", sym->result->name) == 0)
4543 sym->result->attr.proc_pointer = 1;
4544 sym->attr.pointer = 0;
4545 return SUCCESS;
4547 else
4548 return FAILURE;
4552 /* Match the interface for a PROCEDURE declaration,
4553 including brackets (R1212). */
4555 static match
4556 match_procedure_interface (gfc_symbol **proc_if)
4558 match m;
4559 gfc_symtree *st;
4560 locus old_loc, entry_loc;
4561 gfc_namespace *old_ns = gfc_current_ns;
4562 char name[GFC_MAX_SYMBOL_LEN + 1];
4564 old_loc = entry_loc = gfc_current_locus;
4565 gfc_clear_ts (&current_ts);
4567 if (gfc_match (" (") != MATCH_YES)
4569 gfc_current_locus = entry_loc;
4570 return MATCH_NO;
4573 /* Get the type spec. for the procedure interface. */
4574 old_loc = gfc_current_locus;
4575 m = gfc_match_decl_type_spec (&current_ts, 0);
4576 gfc_gobble_whitespace ();
4577 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4578 goto got_ts;
4580 if (m == MATCH_ERROR)
4581 return m;
4583 /* Procedure interface is itself a procedure. */
4584 gfc_current_locus = old_loc;
4585 m = gfc_match_name (name);
4587 /* First look to see if it is already accessible in the current
4588 namespace because it is use associated or contained. */
4589 st = NULL;
4590 if (gfc_find_sym_tree (name, NULL, 0, &st))
4591 return MATCH_ERROR;
4593 /* If it is still not found, then try the parent namespace, if it
4594 exists and create the symbol there if it is still not found. */
4595 if (gfc_current_ns->parent)
4596 gfc_current_ns = gfc_current_ns->parent;
4597 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4598 return MATCH_ERROR;
4600 gfc_current_ns = old_ns;
4601 *proc_if = st->n.sym;
4603 /* Various interface checks. */
4604 if (*proc_if)
4606 (*proc_if)->refs++;
4607 /* Resolve interface if possible. That way, attr.procedure is only set
4608 if it is declared by a later procedure-declaration-stmt, which is
4609 invalid per C1212. */
4610 while ((*proc_if)->ts.interface)
4611 *proc_if = (*proc_if)->ts.interface;
4613 if ((*proc_if)->generic)
4615 gfc_error ("Interface '%s' at %C may not be generic",
4616 (*proc_if)->name);
4617 return MATCH_ERROR;
4619 if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
4621 gfc_error ("Interface '%s' at %C may not be a statement function",
4622 (*proc_if)->name);
4623 return MATCH_ERROR;
4625 /* Handle intrinsic procedures. */
4626 if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4627 || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4628 && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4629 || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4630 (*proc_if)->attr.intrinsic = 1;
4631 if ((*proc_if)->attr.intrinsic
4632 && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
4634 gfc_error ("Intrinsic procedure '%s' not allowed "
4635 "in PROCEDURE statement at %C", (*proc_if)->name);
4636 return MATCH_ERROR;
4640 got_ts:
4641 if (gfc_match (" )") != MATCH_YES)
4643 gfc_current_locus = entry_loc;
4644 return MATCH_NO;
4647 return MATCH_YES;
4651 /* Match a PROCEDURE declaration (R1211). */
4653 static match
4654 match_procedure_decl (void)
4656 match m;
4657 gfc_symbol *sym, *proc_if = NULL;
4658 int num;
4659 gfc_expr *initializer = NULL;
4661 /* Parse interface (with brackets). */
4662 m = match_procedure_interface (&proc_if);
4663 if (m != MATCH_YES)
4664 return m;
4666 /* Parse attributes (with colons). */
4667 m = match_attr_spec();
4668 if (m == MATCH_ERROR)
4669 return MATCH_ERROR;
4671 /* Get procedure symbols. */
4672 for(num=1;;num++)
4674 m = gfc_match_symbol (&sym, 0);
4675 if (m == MATCH_NO)
4676 goto syntax;
4677 else if (m == MATCH_ERROR)
4678 return m;
4680 /* Add current_attr to the symbol attributes. */
4681 if (gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4682 return MATCH_ERROR;
4684 if (sym->attr.is_bind_c)
4686 /* Check for C1218. */
4687 if (!proc_if || !proc_if->attr.is_bind_c)
4689 gfc_error ("BIND(C) attribute at %C requires "
4690 "an interface with BIND(C)");
4691 return MATCH_ERROR;
4693 /* Check for C1217. */
4694 if (has_name_equals && sym->attr.pointer)
4696 gfc_error ("BIND(C) procedure with NAME may not have "
4697 "POINTER attribute at %C");
4698 return MATCH_ERROR;
4700 if (has_name_equals && sym->attr.dummy)
4702 gfc_error ("Dummy procedure at %C may not have "
4703 "BIND(C) attribute with NAME");
4704 return MATCH_ERROR;
4706 /* Set binding label for BIND(C). */
4707 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4708 return MATCH_ERROR;
4711 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4712 return MATCH_ERROR;
4714 if (add_hidden_procptr_result (sym) == SUCCESS)
4715 sym = sym->result;
4717 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4718 return MATCH_ERROR;
4720 /* Set interface. */
4721 if (proc_if != NULL)
4723 if (sym->ts.type != BT_UNKNOWN)
4725 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4726 sym->name, &gfc_current_locus,
4727 gfc_basic_typename (sym->ts.type));
4728 return MATCH_ERROR;
4730 sym->ts.interface = proc_if;
4731 sym->attr.untyped = 1;
4732 sym->attr.if_source = IFSRC_IFBODY;
4734 else if (current_ts.type != BT_UNKNOWN)
4736 if (gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
4737 return MATCH_ERROR;
4738 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4739 sym->ts.interface->ts = current_ts;
4740 sym->ts.interface->attr.flavor = FL_PROCEDURE;
4741 sym->ts.interface->attr.function = 1;
4742 sym->attr.function = 1;
4743 sym->attr.if_source = IFSRC_UNKNOWN;
4746 if (gfc_match (" =>") == MATCH_YES)
4748 if (!current_attr.pointer)
4750 gfc_error ("Initialization at %C isn't for a pointer variable");
4751 m = MATCH_ERROR;
4752 goto cleanup;
4755 m = match_pointer_init (&initializer, 1);
4756 if (m != MATCH_YES)
4757 goto cleanup;
4759 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4760 != SUCCESS)
4761 goto cleanup;
4765 gfc_set_sym_referenced (sym);
4767 if (gfc_match_eos () == MATCH_YES)
4768 return MATCH_YES;
4769 if (gfc_match_char (',') != MATCH_YES)
4770 goto syntax;
4773 syntax:
4774 gfc_error ("Syntax error in PROCEDURE statement at %C");
4775 return MATCH_ERROR;
4777 cleanup:
4778 /* Free stuff up and return. */
4779 gfc_free_expr (initializer);
4780 return m;
4784 static match
4785 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4788 /* Match a procedure pointer component declaration (R445). */
4790 static match
4791 match_ppc_decl (void)
4793 match m;
4794 gfc_symbol *proc_if = NULL;
4795 gfc_typespec ts;
4796 int num;
4797 gfc_component *c;
4798 gfc_expr *initializer = NULL;
4799 gfc_typebound_proc* tb;
4800 char name[GFC_MAX_SYMBOL_LEN + 1];
4802 /* Parse interface (with brackets). */
4803 m = match_procedure_interface (&proc_if);
4804 if (m != MATCH_YES)
4805 goto syntax;
4807 /* Parse attributes. */
4808 tb = XCNEW (gfc_typebound_proc);
4809 tb->where = gfc_current_locus;
4810 m = match_binding_attributes (tb, false, true);
4811 if (m == MATCH_ERROR)
4812 return m;
4814 gfc_clear_attr (&current_attr);
4815 current_attr.procedure = 1;
4816 current_attr.proc_pointer = 1;
4817 current_attr.access = tb->access;
4818 current_attr.flavor = FL_PROCEDURE;
4820 /* Match the colons (required). */
4821 if (gfc_match (" ::") != MATCH_YES)
4823 gfc_error ("Expected '::' after binding-attributes at %C");
4824 return MATCH_ERROR;
4827 /* Check for C450. */
4828 if (!tb->nopass && proc_if == NULL)
4830 gfc_error("NOPASS or explicit interface required at %C");
4831 return MATCH_ERROR;
4834 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure pointer "
4835 "component at %C") == FAILURE)
4836 return MATCH_ERROR;
4838 /* Match PPC names. */
4839 ts = current_ts;
4840 for(num=1;;num++)
4842 m = gfc_match_name (name);
4843 if (m == MATCH_NO)
4844 goto syntax;
4845 else if (m == MATCH_ERROR)
4846 return m;
4848 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
4849 return MATCH_ERROR;
4851 /* Add current_attr to the symbol attributes. */
4852 if (gfc_copy_attr (&c->attr, &current_attr, NULL) == FAILURE)
4853 return MATCH_ERROR;
4855 if (gfc_add_external (&c->attr, NULL) == FAILURE)
4856 return MATCH_ERROR;
4858 if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
4859 return MATCH_ERROR;
4861 c->tb = tb;
4863 /* Set interface. */
4864 if (proc_if != NULL)
4866 c->ts.interface = proc_if;
4867 c->attr.untyped = 1;
4868 c->attr.if_source = IFSRC_IFBODY;
4870 else if (ts.type != BT_UNKNOWN)
4872 c->ts = ts;
4873 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4874 c->ts.interface->ts = ts;
4875 c->ts.interface->attr.flavor = FL_PROCEDURE;
4876 c->ts.interface->attr.function = 1;
4877 c->attr.function = 1;
4878 c->attr.if_source = IFSRC_UNKNOWN;
4881 if (gfc_match (" =>") == MATCH_YES)
4883 m = match_pointer_init (&initializer, 1);
4884 if (m != MATCH_YES)
4886 gfc_free_expr (initializer);
4887 return m;
4889 c->initializer = initializer;
4892 if (gfc_match_eos () == MATCH_YES)
4893 return MATCH_YES;
4894 if (gfc_match_char (',') != MATCH_YES)
4895 goto syntax;
4898 syntax:
4899 gfc_error ("Syntax error in procedure pointer component at %C");
4900 return MATCH_ERROR;
4904 /* Match a PROCEDURE declaration inside an interface (R1206). */
4906 static match
4907 match_procedure_in_interface (void)
4909 match m;
4910 gfc_symbol *sym;
4911 char name[GFC_MAX_SYMBOL_LEN + 1];
4913 if (current_interface.type == INTERFACE_NAMELESS
4914 || current_interface.type == INTERFACE_ABSTRACT)
4916 gfc_error ("PROCEDURE at %C must be in a generic interface");
4917 return MATCH_ERROR;
4920 for(;;)
4922 m = gfc_match_name (name);
4923 if (m == MATCH_NO)
4924 goto syntax;
4925 else if (m == MATCH_ERROR)
4926 return m;
4927 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4928 return MATCH_ERROR;
4930 if (gfc_add_interface (sym) == FAILURE)
4931 return MATCH_ERROR;
4933 if (gfc_match_eos () == MATCH_YES)
4934 break;
4935 if (gfc_match_char (',') != MATCH_YES)
4936 goto syntax;
4939 return MATCH_YES;
4941 syntax:
4942 gfc_error ("Syntax error in PROCEDURE statement at %C");
4943 return MATCH_ERROR;
4947 /* General matcher for PROCEDURE declarations. */
4949 static match match_procedure_in_type (void);
4951 match
4952 gfc_match_procedure (void)
4954 match m;
4956 switch (gfc_current_state ())
4958 case COMP_NONE:
4959 case COMP_PROGRAM:
4960 case COMP_MODULE:
4961 case COMP_SUBROUTINE:
4962 case COMP_FUNCTION:
4963 m = match_procedure_decl ();
4964 break;
4965 case COMP_INTERFACE:
4966 m = match_procedure_in_interface ();
4967 break;
4968 case COMP_DERIVED:
4969 m = match_ppc_decl ();
4970 break;
4971 case COMP_DERIVED_CONTAINS:
4972 m = match_procedure_in_type ();
4973 break;
4974 default:
4975 return MATCH_NO;
4978 if (m != MATCH_YES)
4979 return m;
4981 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4982 == FAILURE)
4983 return MATCH_ERROR;
4985 return m;
4989 /* Warn if a matched procedure has the same name as an intrinsic; this is
4990 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4991 parser-state-stack to find out whether we're in a module. */
4993 static void
4994 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
4996 bool in_module;
4998 in_module = (gfc_state_stack->previous
4999 && gfc_state_stack->previous->state == COMP_MODULE);
5001 gfc_warn_intrinsic_shadow (sym, in_module, func);
5005 /* Match a function declaration. */
5007 match
5008 gfc_match_function_decl (void)
5010 char name[GFC_MAX_SYMBOL_LEN + 1];
5011 gfc_symbol *sym, *result;
5012 locus old_loc;
5013 match m;
5014 match suffix_match;
5015 match found_match; /* Status returned by match func. */
5017 if (gfc_current_state () != COMP_NONE
5018 && gfc_current_state () != COMP_INTERFACE
5019 && gfc_current_state () != COMP_CONTAINS)
5020 return MATCH_NO;
5022 gfc_clear_ts (&current_ts);
5024 old_loc = gfc_current_locus;
5026 m = gfc_match_prefix (&current_ts);
5027 if (m != MATCH_YES)
5029 gfc_current_locus = old_loc;
5030 return m;
5033 if (gfc_match ("function% %n", name) != MATCH_YES)
5035 gfc_current_locus = old_loc;
5036 return MATCH_NO;
5038 if (get_proc_name (name, &sym, false))
5039 return MATCH_ERROR;
5041 if (add_hidden_procptr_result (sym) == SUCCESS)
5042 sym = sym->result;
5044 gfc_new_block = sym;
5046 m = gfc_match_formal_arglist (sym, 0, 0);
5047 if (m == MATCH_NO)
5049 gfc_error ("Expected formal argument list in function "
5050 "definition at %C");
5051 m = MATCH_ERROR;
5052 goto cleanup;
5054 else if (m == MATCH_ERROR)
5055 goto cleanup;
5057 result = NULL;
5059 /* According to the draft, the bind(c) and result clause can
5060 come in either order after the formal_arg_list (i.e., either
5061 can be first, both can exist together or by themselves or neither
5062 one). Therefore, the match_result can't match the end of the
5063 string, and check for the bind(c) or result clause in either order. */
5064 found_match = gfc_match_eos ();
5066 /* Make sure that it isn't already declared as BIND(C). If it is, it
5067 must have been marked BIND(C) with a BIND(C) attribute and that is
5068 not allowed for procedures. */
5069 if (sym->attr.is_bind_c == 1)
5071 sym->attr.is_bind_c = 0;
5072 if (sym->old_symbol != NULL)
5073 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5074 "variables or common blocks",
5075 &(sym->old_symbol->declared_at));
5076 else
5077 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5078 "variables or common blocks", &gfc_current_locus);
5081 if (found_match != MATCH_YES)
5083 /* If we haven't found the end-of-statement, look for a suffix. */
5084 suffix_match = gfc_match_suffix (sym, &result);
5085 if (suffix_match == MATCH_YES)
5086 /* Need to get the eos now. */
5087 found_match = gfc_match_eos ();
5088 else
5089 found_match = suffix_match;
5092 if(found_match != MATCH_YES)
5093 m = MATCH_ERROR;
5094 else
5096 /* Make changes to the symbol. */
5097 m = MATCH_ERROR;
5099 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
5100 goto cleanup;
5102 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
5103 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5104 goto cleanup;
5106 /* Delay matching the function characteristics until after the
5107 specification block by signalling kind=-1. */
5108 sym->declared_at = old_loc;
5109 if (current_ts.type != BT_UNKNOWN)
5110 current_ts.kind = -1;
5111 else
5112 current_ts.kind = 0;
5114 if (result == NULL)
5116 if (current_ts.type != BT_UNKNOWN
5117 && gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
5118 goto cleanup;
5119 sym->result = sym;
5121 else
5123 if (current_ts.type != BT_UNKNOWN
5124 && gfc_add_type (result, &current_ts, &gfc_current_locus)
5125 == FAILURE)
5126 goto cleanup;
5127 sym->result = result;
5130 /* Warn if this procedure has the same name as an intrinsic. */
5131 warn_intrinsic_shadow (sym, true);
5133 return MATCH_YES;
5136 cleanup:
5137 gfc_current_locus = old_loc;
5138 return m;
5142 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5143 pass the name of the entry, rather than the gfc_current_block name, and
5144 to return false upon finding an existing global entry. */
5146 static bool
5147 add_global_entry (const char *name, int sub)
5149 gfc_gsymbol *s;
5150 enum gfc_symbol_type type;
5152 s = gfc_get_gsymbol(name);
5153 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5155 if (s->defined
5156 || (s->type != GSYM_UNKNOWN
5157 && s->type != type))
5158 gfc_global_used(s, NULL);
5159 else
5161 s->type = type;
5162 s->where = gfc_current_locus;
5163 s->defined = 1;
5164 s->ns = gfc_current_ns;
5165 return true;
5167 return false;
5171 /* Match an ENTRY statement. */
5173 match
5174 gfc_match_entry (void)
5176 gfc_symbol *proc;
5177 gfc_symbol *result;
5178 gfc_symbol *entry;
5179 char name[GFC_MAX_SYMBOL_LEN + 1];
5180 gfc_compile_state state;
5181 match m;
5182 gfc_entry_list *el;
5183 locus old_loc;
5184 bool module_procedure;
5185 char peek_char;
5186 match is_bind_c;
5188 m = gfc_match_name (name);
5189 if (m != MATCH_YES)
5190 return m;
5192 if (gfc_notify_std (GFC_STD_F2008_OBS, "Fortran 2008 obsolescent feature: "
5193 "ENTRY statement at %C") == FAILURE)
5194 return MATCH_ERROR;
5196 state = gfc_current_state ();
5197 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5199 switch (state)
5201 case COMP_PROGRAM:
5202 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5203 break;
5204 case COMP_MODULE:
5205 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5206 break;
5207 case COMP_BLOCK_DATA:
5208 gfc_error ("ENTRY statement at %C cannot appear within "
5209 "a BLOCK DATA");
5210 break;
5211 case COMP_INTERFACE:
5212 gfc_error ("ENTRY statement at %C cannot appear within "
5213 "an INTERFACE");
5214 break;
5215 case COMP_DERIVED:
5216 gfc_error ("ENTRY statement at %C cannot appear within "
5217 "a DERIVED TYPE block");
5218 break;
5219 case COMP_IF:
5220 gfc_error ("ENTRY statement at %C cannot appear within "
5221 "an IF-THEN block");
5222 break;
5223 case COMP_DO:
5224 gfc_error ("ENTRY statement at %C cannot appear within "
5225 "a DO block");
5226 break;
5227 case COMP_SELECT:
5228 gfc_error ("ENTRY statement at %C cannot appear within "
5229 "a SELECT block");
5230 break;
5231 case COMP_FORALL:
5232 gfc_error ("ENTRY statement at %C cannot appear within "
5233 "a FORALL block");
5234 break;
5235 case COMP_WHERE:
5236 gfc_error ("ENTRY statement at %C cannot appear within "
5237 "a WHERE block");
5238 break;
5239 case COMP_CONTAINS:
5240 gfc_error ("ENTRY statement at %C cannot appear within "
5241 "a contained subprogram");
5242 break;
5243 default:
5244 gfc_internal_error ("gfc_match_entry(): Bad state");
5246 return MATCH_ERROR;
5249 module_procedure = gfc_current_ns->parent != NULL
5250 && gfc_current_ns->parent->proc_name
5251 && gfc_current_ns->parent->proc_name->attr.flavor
5252 == FL_MODULE;
5254 if (gfc_current_ns->parent != NULL
5255 && gfc_current_ns->parent->proc_name
5256 && !module_procedure)
5258 gfc_error("ENTRY statement at %C cannot appear in a "
5259 "contained procedure");
5260 return MATCH_ERROR;
5263 /* Module function entries need special care in get_proc_name
5264 because previous references within the function will have
5265 created symbols attached to the current namespace. */
5266 if (get_proc_name (name, &entry,
5267 gfc_current_ns->parent != NULL
5268 && module_procedure))
5269 return MATCH_ERROR;
5271 proc = gfc_current_block ();
5273 /* Make sure that it isn't already declared as BIND(C). If it is, it
5274 must have been marked BIND(C) with a BIND(C) attribute and that is
5275 not allowed for procedures. */
5276 if (entry->attr.is_bind_c == 1)
5278 entry->attr.is_bind_c = 0;
5279 if (entry->old_symbol != NULL)
5280 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5281 "variables or common blocks",
5282 &(entry->old_symbol->declared_at));
5283 else
5284 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5285 "variables or common blocks", &gfc_current_locus);
5288 /* Check what next non-whitespace character is so we can tell if there
5289 is the required parens if we have a BIND(C). */
5290 gfc_gobble_whitespace ();
5291 peek_char = gfc_peek_ascii_char ();
5293 if (state == COMP_SUBROUTINE)
5295 /* An entry in a subroutine. */
5296 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
5297 return MATCH_ERROR;
5299 m = gfc_match_formal_arglist (entry, 0, 1);
5300 if (m != MATCH_YES)
5301 return MATCH_ERROR;
5303 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5304 never be an internal procedure. */
5305 is_bind_c = gfc_match_bind_c (entry, true);
5306 if (is_bind_c == MATCH_ERROR)
5307 return MATCH_ERROR;
5308 if (is_bind_c == MATCH_YES)
5310 if (peek_char != '(')
5312 gfc_error ("Missing required parentheses before BIND(C) at %C");
5313 return MATCH_ERROR;
5315 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
5316 == FAILURE)
5317 return MATCH_ERROR;
5320 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5321 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
5322 return MATCH_ERROR;
5324 else
5326 /* An entry in a function.
5327 We need to take special care because writing
5328 ENTRY f()
5330 ENTRY f
5331 is allowed, whereas
5332 ENTRY f() RESULT (r)
5333 can't be written as
5334 ENTRY f RESULT (r). */
5335 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
5336 return MATCH_ERROR;
5338 old_loc = gfc_current_locus;
5339 if (gfc_match_eos () == MATCH_YES)
5341 gfc_current_locus = old_loc;
5342 /* Match the empty argument list, and add the interface to
5343 the symbol. */
5344 m = gfc_match_formal_arglist (entry, 0, 1);
5346 else
5347 m = gfc_match_formal_arglist (entry, 0, 0);
5349 if (m != MATCH_YES)
5350 return MATCH_ERROR;
5352 result = NULL;
5354 if (gfc_match_eos () == MATCH_YES)
5356 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5357 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5358 return MATCH_ERROR;
5360 entry->result = entry;
5362 else
5364 m = gfc_match_suffix (entry, &result);
5365 if (m == MATCH_NO)
5366 gfc_syntax_error (ST_ENTRY);
5367 if (m != MATCH_YES)
5368 return MATCH_ERROR;
5370 if (result)
5372 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
5373 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
5374 || gfc_add_function (&entry->attr, result->name, NULL)
5375 == FAILURE)
5376 return MATCH_ERROR;
5377 entry->result = result;
5379 else
5381 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5382 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5383 return MATCH_ERROR;
5384 entry->result = entry;
5389 if (gfc_match_eos () != MATCH_YES)
5391 gfc_syntax_error (ST_ENTRY);
5392 return MATCH_ERROR;
5395 entry->attr.recursive = proc->attr.recursive;
5396 entry->attr.elemental = proc->attr.elemental;
5397 entry->attr.pure = proc->attr.pure;
5399 el = gfc_get_entry_list ();
5400 el->sym = entry;
5401 el->next = gfc_current_ns->entries;
5402 gfc_current_ns->entries = el;
5403 if (el->next)
5404 el->id = el->next->id + 1;
5405 else
5406 el->id = 1;
5408 new_st.op = EXEC_ENTRY;
5409 new_st.ext.entry = el;
5411 return MATCH_YES;
5415 /* Match a subroutine statement, including optional prefixes. */
5417 match
5418 gfc_match_subroutine (void)
5420 char name[GFC_MAX_SYMBOL_LEN + 1];
5421 gfc_symbol *sym;
5422 match m;
5423 match is_bind_c;
5424 char peek_char;
5425 bool allow_binding_name;
5427 if (gfc_current_state () != COMP_NONE
5428 && gfc_current_state () != COMP_INTERFACE
5429 && gfc_current_state () != COMP_CONTAINS)
5430 return MATCH_NO;
5432 m = gfc_match_prefix (NULL);
5433 if (m != MATCH_YES)
5434 return m;
5436 m = gfc_match ("subroutine% %n", name);
5437 if (m != MATCH_YES)
5438 return m;
5440 if (get_proc_name (name, &sym, false))
5441 return MATCH_ERROR;
5443 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5444 the symbol existed before. */
5445 sym->declared_at = gfc_current_locus;
5447 if (add_hidden_procptr_result (sym) == SUCCESS)
5448 sym = sym->result;
5450 gfc_new_block = sym;
5452 /* Check what next non-whitespace character is so we can tell if there
5453 is the required parens if we have a BIND(C). */
5454 gfc_gobble_whitespace ();
5455 peek_char = gfc_peek_ascii_char ();
5457 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5458 return MATCH_ERROR;
5460 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5461 return MATCH_ERROR;
5463 /* Make sure that it isn't already declared as BIND(C). If it is, it
5464 must have been marked BIND(C) with a BIND(C) attribute and that is
5465 not allowed for procedures. */
5466 if (sym->attr.is_bind_c == 1)
5468 sym->attr.is_bind_c = 0;
5469 if (sym->old_symbol != NULL)
5470 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5471 "variables or common blocks",
5472 &(sym->old_symbol->declared_at));
5473 else
5474 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5475 "variables or common blocks", &gfc_current_locus);
5478 /* C binding names are not allowed for internal procedures. */
5479 if (gfc_current_state () == COMP_CONTAINS
5480 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5481 allow_binding_name = false;
5482 else
5483 allow_binding_name = true;
5485 /* Here, we are just checking if it has the bind(c) attribute, and if
5486 so, then we need to make sure it's all correct. If it doesn't,
5487 we still need to continue matching the rest of the subroutine line. */
5488 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5489 if (is_bind_c == MATCH_ERROR)
5491 /* There was an attempt at the bind(c), but it was wrong. An
5492 error message should have been printed w/in the gfc_match_bind_c
5493 so here we'll just return the MATCH_ERROR. */
5494 return MATCH_ERROR;
5497 if (is_bind_c == MATCH_YES)
5499 /* The following is allowed in the Fortran 2008 draft. */
5500 if (gfc_current_state () == COMP_CONTAINS
5501 && sym->ns->proc_name->attr.flavor != FL_MODULE
5502 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5503 "at %L may not be specified for an internal "
5504 "procedure", &gfc_current_locus)
5505 == FAILURE)
5506 return MATCH_ERROR;
5508 if (peek_char != '(')
5510 gfc_error ("Missing required parentheses before BIND(C) at %C");
5511 return MATCH_ERROR;
5513 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5514 == FAILURE)
5515 return MATCH_ERROR;
5518 if (gfc_match_eos () != MATCH_YES)
5520 gfc_syntax_error (ST_SUBROUTINE);
5521 return MATCH_ERROR;
5524 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5525 return MATCH_ERROR;
5527 /* Warn if it has the same name as an intrinsic. */
5528 warn_intrinsic_shadow (sym, false);
5530 return MATCH_YES;
5534 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5535 given, and set the binding label in either the given symbol (if not
5536 NULL), or in the current_ts. The symbol may be NULL because we may
5537 encounter the BIND(C) before the declaration itself. Return
5538 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5539 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5540 or MATCH_YES if the specifier was correct and the binding label and
5541 bind(c) fields were set correctly for the given symbol or the
5542 current_ts. If allow_binding_name is false, no binding name may be
5543 given. */
5545 match
5546 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5548 /* binding label, if exists */
5549 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
5550 match double_quote;
5551 match single_quote;
5553 /* Initialize the flag that specifies whether we encountered a NAME=
5554 specifier or not. */
5555 has_name_equals = 0;
5557 /* Init the first char to nil so we can catch if we don't have
5558 the label (name attr) or the symbol name yet. */
5559 binding_label[0] = '\0';
5561 /* This much we have to be able to match, in this order, if
5562 there is a bind(c) label. */
5563 if (gfc_match (" bind ( c ") != MATCH_YES)
5564 return MATCH_NO;
5566 /* Now see if there is a binding label, or if we've reached the
5567 end of the bind(c) attribute without one. */
5568 if (gfc_match_char (',') == MATCH_YES)
5570 if (gfc_match (" name = ") != MATCH_YES)
5572 gfc_error ("Syntax error in NAME= specifier for binding label "
5573 "at %C");
5574 /* should give an error message here */
5575 return MATCH_ERROR;
5578 has_name_equals = 1;
5580 /* Get the opening quote. */
5581 double_quote = MATCH_YES;
5582 single_quote = MATCH_YES;
5583 double_quote = gfc_match_char ('"');
5584 if (double_quote != MATCH_YES)
5585 single_quote = gfc_match_char ('\'');
5586 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5588 gfc_error ("Syntax error in NAME= specifier for binding label "
5589 "at %C");
5590 return MATCH_ERROR;
5593 /* Grab the binding label, using functions that will not lower
5594 case the names automatically. */
5595 if (gfc_match_name_C (binding_label) != MATCH_YES)
5596 return MATCH_ERROR;
5598 /* Get the closing quotation. */
5599 if (double_quote == MATCH_YES)
5601 if (gfc_match_char ('"') != MATCH_YES)
5603 gfc_error ("Missing closing quote '\"' for binding label at %C");
5604 /* User started string with '"' so looked to match it. */
5605 return MATCH_ERROR;
5608 else
5610 if (gfc_match_char ('\'') != MATCH_YES)
5612 gfc_error ("Missing closing quote '\'' for binding label at %C");
5613 /* User started string with "'" char. */
5614 return MATCH_ERROR;
5619 /* Get the required right paren. */
5620 if (gfc_match_char (')') != MATCH_YES)
5622 gfc_error ("Missing closing paren for binding label at %C");
5623 return MATCH_ERROR;
5626 if (has_name_equals && !allow_binding_name)
5628 gfc_error ("No binding name is allowed in BIND(C) at %C");
5629 return MATCH_ERROR;
5632 if (has_name_equals && sym != NULL && sym->attr.dummy)
5634 gfc_error ("For dummy procedure %s, no binding name is "
5635 "allowed in BIND(C) at %C", sym->name);
5636 return MATCH_ERROR;
5640 /* Save the binding label to the symbol. If sym is null, we're
5641 probably matching the typespec attributes of a declaration and
5642 haven't gotten the name yet, and therefore, no symbol yet. */
5643 if (binding_label[0] != '\0')
5645 if (sym != NULL)
5647 strcpy (sym->binding_label, binding_label);
5649 else
5650 strcpy (curr_binding_label, binding_label);
5652 else if (allow_binding_name)
5654 /* No binding label, but if symbol isn't null, we
5655 can set the label for it here.
5656 If name="" or allow_binding_name is false, no C binding name is
5657 created. */
5658 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5659 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
5662 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5663 && current_interface.type == INTERFACE_ABSTRACT)
5665 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5666 return MATCH_ERROR;
5669 return MATCH_YES;
5673 /* Return nonzero if we're currently compiling a contained procedure. */
5675 static int
5676 contained_procedure (void)
5678 gfc_state_data *s = gfc_state_stack;
5680 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5681 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5682 return 1;
5684 return 0;
5687 /* Set the kind of each enumerator. The kind is selected such that it is
5688 interoperable with the corresponding C enumeration type, making
5689 sure that -fshort-enums is honored. */
5691 static void
5692 set_enum_kind(void)
5694 enumerator_history *current_history = NULL;
5695 int kind;
5696 int i;
5698 if (max_enum == NULL || enum_history == NULL)
5699 return;
5701 if (!flag_short_enums)
5702 return;
5704 i = 0;
5707 kind = gfc_integer_kinds[i++].kind;
5709 while (kind < gfc_c_int_kind
5710 && gfc_check_integer_range (max_enum->initializer->value.integer,
5711 kind) != ARITH_OK);
5713 current_history = enum_history;
5714 while (current_history != NULL)
5716 current_history->sym->ts.kind = kind;
5717 current_history = current_history->next;
5722 /* Match any of the various end-block statements. Returns the type of
5723 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5724 and END BLOCK statements cannot be replaced by a single END statement. */
5726 match
5727 gfc_match_end (gfc_statement *st)
5729 char name[GFC_MAX_SYMBOL_LEN + 1];
5730 gfc_compile_state state;
5731 locus old_loc;
5732 const char *block_name;
5733 const char *target;
5734 int eos_ok;
5735 match m;
5737 old_loc = gfc_current_locus;
5738 if (gfc_match ("end") != MATCH_YES)
5739 return MATCH_NO;
5741 state = gfc_current_state ();
5742 block_name = gfc_current_block () == NULL
5743 ? NULL : gfc_current_block ()->name;
5745 switch (state)
5747 case COMP_ASSOCIATE:
5748 case COMP_BLOCK:
5749 if (!strcmp (block_name, "block@"))
5750 block_name = NULL;
5751 break;
5753 case COMP_CONTAINS:
5754 case COMP_DERIVED_CONTAINS:
5755 state = gfc_state_stack->previous->state;
5756 block_name = gfc_state_stack->previous->sym == NULL
5757 ? NULL : gfc_state_stack->previous->sym->name;
5758 break;
5760 default:
5761 break;
5764 switch (state)
5766 case COMP_NONE:
5767 case COMP_PROGRAM:
5768 *st = ST_END_PROGRAM;
5769 target = " program";
5770 eos_ok = 1;
5771 break;
5773 case COMP_SUBROUTINE:
5774 *st = ST_END_SUBROUTINE;
5775 target = " subroutine";
5776 eos_ok = !contained_procedure ();
5777 break;
5779 case COMP_FUNCTION:
5780 *st = ST_END_FUNCTION;
5781 target = " function";
5782 eos_ok = !contained_procedure ();
5783 break;
5785 case COMP_BLOCK_DATA:
5786 *st = ST_END_BLOCK_DATA;
5787 target = " block data";
5788 eos_ok = 1;
5789 break;
5791 case COMP_MODULE:
5792 *st = ST_END_MODULE;
5793 target = " module";
5794 eos_ok = 1;
5795 break;
5797 case COMP_INTERFACE:
5798 *st = ST_END_INTERFACE;
5799 target = " interface";
5800 eos_ok = 0;
5801 break;
5803 case COMP_DERIVED:
5804 case COMP_DERIVED_CONTAINS:
5805 *st = ST_END_TYPE;
5806 target = " type";
5807 eos_ok = 0;
5808 break;
5810 case COMP_ASSOCIATE:
5811 *st = ST_END_ASSOCIATE;
5812 target = " associate";
5813 eos_ok = 0;
5814 break;
5816 case COMP_BLOCK:
5817 *st = ST_END_BLOCK;
5818 target = " block";
5819 eos_ok = 0;
5820 break;
5822 case COMP_IF:
5823 *st = ST_ENDIF;
5824 target = " if";
5825 eos_ok = 0;
5826 break;
5828 case COMP_DO:
5829 *st = ST_ENDDO;
5830 target = " do";
5831 eos_ok = 0;
5832 break;
5834 case COMP_CRITICAL:
5835 *st = ST_END_CRITICAL;
5836 target = " critical";
5837 eos_ok = 0;
5838 break;
5840 case COMP_SELECT:
5841 case COMP_SELECT_TYPE:
5842 *st = ST_END_SELECT;
5843 target = " select";
5844 eos_ok = 0;
5845 break;
5847 case COMP_FORALL:
5848 *st = ST_END_FORALL;
5849 target = " forall";
5850 eos_ok = 0;
5851 break;
5853 case COMP_WHERE:
5854 *st = ST_END_WHERE;
5855 target = " where";
5856 eos_ok = 0;
5857 break;
5859 case COMP_ENUM:
5860 *st = ST_END_ENUM;
5861 target = " enum";
5862 eos_ok = 0;
5863 last_initializer = NULL;
5864 set_enum_kind ();
5865 gfc_free_enum_history ();
5866 break;
5868 default:
5869 gfc_error ("Unexpected END statement at %C");
5870 goto cleanup;
5873 if (gfc_match_eos () == MATCH_YES)
5875 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
5877 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: END statement "
5878 "instead of %s statement at %L",
5879 gfc_ascii_statement (*st), &old_loc) == FAILURE)
5880 goto cleanup;
5882 else if (!eos_ok)
5884 /* We would have required END [something]. */
5885 gfc_error ("%s statement expected at %L",
5886 gfc_ascii_statement (*st), &old_loc);
5887 goto cleanup;
5890 return MATCH_YES;
5893 /* Verify that we've got the sort of end-block that we're expecting. */
5894 if (gfc_match (target) != MATCH_YES)
5896 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5897 goto cleanup;
5900 /* If we're at the end, make sure a block name wasn't required. */
5901 if (gfc_match_eos () == MATCH_YES)
5904 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5905 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
5906 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
5907 return MATCH_YES;
5909 if (!block_name)
5910 return MATCH_YES;
5912 gfc_error ("Expected block name of '%s' in %s statement at %C",
5913 block_name, gfc_ascii_statement (*st));
5915 return MATCH_ERROR;
5918 /* END INTERFACE has a special handler for its several possible endings. */
5919 if (*st == ST_END_INTERFACE)
5920 return gfc_match_end_interface ();
5922 /* We haven't hit the end of statement, so what is left must be an
5923 end-name. */
5924 m = gfc_match_space ();
5925 if (m == MATCH_YES)
5926 m = gfc_match_name (name);
5928 if (m == MATCH_NO)
5929 gfc_error ("Expected terminating name at %C");
5930 if (m != MATCH_YES)
5931 goto cleanup;
5933 if (block_name == NULL)
5934 goto syntax;
5936 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
5938 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5939 gfc_ascii_statement (*st));
5940 goto cleanup;
5942 /* Procedure pointer as function result. */
5943 else if (strcmp (block_name, "ppr@") == 0
5944 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
5946 gfc_error ("Expected label '%s' for %s statement at %C",
5947 gfc_current_block ()->ns->proc_name->name,
5948 gfc_ascii_statement (*st));
5949 goto cleanup;
5952 if (gfc_match_eos () == MATCH_YES)
5953 return MATCH_YES;
5955 syntax:
5956 gfc_syntax_error (*st);
5958 cleanup:
5959 gfc_current_locus = old_loc;
5960 return MATCH_ERROR;
5965 /***************** Attribute declaration statements ****************/
5967 /* Set the attribute of a single variable. */
5969 static match
5970 attr_decl1 (void)
5972 char name[GFC_MAX_SYMBOL_LEN + 1];
5973 gfc_array_spec *as;
5974 gfc_symbol *sym;
5975 locus var_locus;
5976 match m;
5978 as = NULL;
5980 m = gfc_match_name (name);
5981 if (m != MATCH_YES)
5982 goto cleanup;
5984 if (find_special (name, &sym, false))
5985 return MATCH_ERROR;
5987 var_locus = gfc_current_locus;
5989 /* Deal with possible array specification for certain attributes. */
5990 if (current_attr.dimension
5991 || current_attr.codimension
5992 || current_attr.allocatable
5993 || current_attr.pointer
5994 || current_attr.target)
5996 m = gfc_match_array_spec (&as, !current_attr.codimension,
5997 !current_attr.dimension
5998 && !current_attr.pointer
5999 && !current_attr.target);
6000 if (m == MATCH_ERROR)
6001 goto cleanup;
6003 if (current_attr.dimension && m == MATCH_NO)
6005 gfc_error ("Missing array specification at %L in DIMENSION "
6006 "statement", &var_locus);
6007 m = MATCH_ERROR;
6008 goto cleanup;
6011 if (current_attr.dimension && sym->value)
6013 gfc_error ("Dimensions specified for %s at %L after its "
6014 "initialisation", sym->name, &var_locus);
6015 m = MATCH_ERROR;
6016 goto cleanup;
6019 if (current_attr.codimension && m == MATCH_NO)
6021 gfc_error ("Missing array specification at %L in CODIMENSION "
6022 "statement", &var_locus);
6023 m = MATCH_ERROR;
6024 goto cleanup;
6027 if ((current_attr.allocatable || current_attr.pointer)
6028 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6030 gfc_error ("Array specification must be deferred at %L", &var_locus);
6031 m = MATCH_ERROR;
6032 goto cleanup;
6036 /* Update symbol table. DIMENSION attribute is set in
6037 gfc_set_array_spec(). For CLASS variables, this must be applied
6038 to the first component, or '_data' field. */
6039 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6041 if (gfc_copy_attr (&CLASS_DATA (sym)->attr, &current_attr, &var_locus)
6042 == FAILURE)
6044 m = MATCH_ERROR;
6045 goto cleanup;
6048 else
6050 if (current_attr.dimension == 0 && current_attr.codimension == 0
6051 && gfc_copy_attr (&sym->attr, &current_attr, &var_locus) == FAILURE)
6053 m = MATCH_ERROR;
6054 goto cleanup;
6058 if (sym->ts.type == BT_CLASS
6059 && gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false) == FAILURE)
6061 m = MATCH_ERROR;
6062 goto cleanup;
6065 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
6067 m = MATCH_ERROR;
6068 goto cleanup;
6071 if (sym->attr.cray_pointee && sym->as != NULL)
6073 /* Fix the array spec. */
6074 m = gfc_mod_pointee_as (sym->as);
6075 if (m == MATCH_ERROR)
6076 goto cleanup;
6079 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
6081 m = MATCH_ERROR;
6082 goto cleanup;
6085 if ((current_attr.external || current_attr.intrinsic)
6086 && sym->attr.flavor != FL_PROCEDURE
6087 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6089 m = MATCH_ERROR;
6090 goto cleanup;
6093 add_hidden_procptr_result (sym);
6095 return MATCH_YES;
6097 cleanup:
6098 gfc_free_array_spec (as);
6099 return m;
6103 /* Generic attribute declaration subroutine. Used for attributes that
6104 just have a list of names. */
6106 static match
6107 attr_decl (void)
6109 match m;
6111 /* Gobble the optional double colon, by simply ignoring the result
6112 of gfc_match(). */
6113 gfc_match (" ::");
6115 for (;;)
6117 m = attr_decl1 ();
6118 if (m != MATCH_YES)
6119 break;
6121 if (gfc_match_eos () == MATCH_YES)
6123 m = MATCH_YES;
6124 break;
6127 if (gfc_match_char (',') != MATCH_YES)
6129 gfc_error ("Unexpected character in variable list at %C");
6130 m = MATCH_ERROR;
6131 break;
6135 return m;
6139 /* This routine matches Cray Pointer declarations of the form:
6140 pointer ( <pointer>, <pointee> )
6142 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6143 The pointer, if already declared, should be an integer. Otherwise, we
6144 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6145 be either a scalar, or an array declaration. No space is allocated for
6146 the pointee. For the statement
6147 pointer (ipt, ar(10))
6148 any subsequent uses of ar will be translated (in C-notation) as
6149 ar(i) => ((<type> *) ipt)(i)
6150 After gimplification, pointee variable will disappear in the code. */
6152 static match
6153 cray_pointer_decl (void)
6155 match m;
6156 gfc_array_spec *as = NULL;
6157 gfc_symbol *cptr; /* Pointer symbol. */
6158 gfc_symbol *cpte; /* Pointee symbol. */
6159 locus var_locus;
6160 bool done = false;
6162 while (!done)
6164 if (gfc_match_char ('(') != MATCH_YES)
6166 gfc_error ("Expected '(' at %C");
6167 return MATCH_ERROR;
6170 /* Match pointer. */
6171 var_locus = gfc_current_locus;
6172 gfc_clear_attr (&current_attr);
6173 gfc_add_cray_pointer (&current_attr, &var_locus);
6174 current_ts.type = BT_INTEGER;
6175 current_ts.kind = gfc_index_integer_kind;
6177 m = gfc_match_symbol (&cptr, 0);
6178 if (m != MATCH_YES)
6180 gfc_error ("Expected variable name at %C");
6181 return m;
6184 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
6185 return MATCH_ERROR;
6187 gfc_set_sym_referenced (cptr);
6189 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6191 cptr->ts.type = BT_INTEGER;
6192 cptr->ts.kind = gfc_index_integer_kind;
6194 else if (cptr->ts.type != BT_INTEGER)
6196 gfc_error ("Cray pointer at %C must be an integer");
6197 return MATCH_ERROR;
6199 else if (cptr->ts.kind < gfc_index_integer_kind)
6200 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6201 " memory addresses require %d bytes",
6202 cptr->ts.kind, gfc_index_integer_kind);
6204 if (gfc_match_char (',') != MATCH_YES)
6206 gfc_error ("Expected \",\" at %C");
6207 return MATCH_ERROR;
6210 /* Match Pointee. */
6211 var_locus = gfc_current_locus;
6212 gfc_clear_attr (&current_attr);
6213 gfc_add_cray_pointee (&current_attr, &var_locus);
6214 current_ts.type = BT_UNKNOWN;
6215 current_ts.kind = 0;
6217 m = gfc_match_symbol (&cpte, 0);
6218 if (m != MATCH_YES)
6220 gfc_error ("Expected variable name at %C");
6221 return m;
6224 /* Check for an optional array spec. */
6225 m = gfc_match_array_spec (&as, true, false);
6226 if (m == MATCH_ERROR)
6228 gfc_free_array_spec (as);
6229 return m;
6231 else if (m == MATCH_NO)
6233 gfc_free_array_spec (as);
6234 as = NULL;
6237 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
6238 return MATCH_ERROR;
6240 gfc_set_sym_referenced (cpte);
6242 if (cpte->as == NULL)
6244 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
6245 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6247 else if (as != NULL)
6249 gfc_error ("Duplicate array spec for Cray pointee at %C");
6250 gfc_free_array_spec (as);
6251 return MATCH_ERROR;
6254 as = NULL;
6256 if (cpte->as != NULL)
6258 /* Fix array spec. */
6259 m = gfc_mod_pointee_as (cpte->as);
6260 if (m == MATCH_ERROR)
6261 return m;
6264 /* Point the Pointee at the Pointer. */
6265 cpte->cp_pointer = cptr;
6267 if (gfc_match_char (')') != MATCH_YES)
6269 gfc_error ("Expected \")\" at %C");
6270 return MATCH_ERROR;
6272 m = gfc_match_char (',');
6273 if (m != MATCH_YES)
6274 done = true; /* Stop searching for more declarations. */
6278 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6279 || gfc_match_eos () != MATCH_YES)
6281 gfc_error ("Expected \",\" or end of statement at %C");
6282 return MATCH_ERROR;
6284 return MATCH_YES;
6288 match
6289 gfc_match_external (void)
6292 gfc_clear_attr (&current_attr);
6293 current_attr.external = 1;
6295 return attr_decl ();
6299 match
6300 gfc_match_intent (void)
6302 sym_intent intent;
6304 /* This is not allowed within a BLOCK construct! */
6305 if (gfc_current_state () == COMP_BLOCK)
6307 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6308 return MATCH_ERROR;
6311 intent = match_intent_spec ();
6312 if (intent == INTENT_UNKNOWN)
6313 return MATCH_ERROR;
6315 gfc_clear_attr (&current_attr);
6316 current_attr.intent = intent;
6318 return attr_decl ();
6322 match
6323 gfc_match_intrinsic (void)
6326 gfc_clear_attr (&current_attr);
6327 current_attr.intrinsic = 1;
6329 return attr_decl ();
6333 match
6334 gfc_match_optional (void)
6336 /* This is not allowed within a BLOCK construct! */
6337 if (gfc_current_state () == COMP_BLOCK)
6339 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6340 return MATCH_ERROR;
6343 gfc_clear_attr (&current_attr);
6344 current_attr.optional = 1;
6346 return attr_decl ();
6350 match
6351 gfc_match_pointer (void)
6353 gfc_gobble_whitespace ();
6354 if (gfc_peek_ascii_char () == '(')
6356 if (!gfc_option.flag_cray_pointer)
6358 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6359 "flag");
6360 return MATCH_ERROR;
6362 return cray_pointer_decl ();
6364 else
6366 gfc_clear_attr (&current_attr);
6367 current_attr.pointer = 1;
6369 return attr_decl ();
6374 match
6375 gfc_match_allocatable (void)
6377 gfc_clear_attr (&current_attr);
6378 current_attr.allocatable = 1;
6380 return attr_decl ();
6384 match
6385 gfc_match_codimension (void)
6387 gfc_clear_attr (&current_attr);
6388 current_attr.codimension = 1;
6390 return attr_decl ();
6394 match
6395 gfc_match_contiguous (void)
6397 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTIGUOUS statement at %C")
6398 == FAILURE)
6399 return MATCH_ERROR;
6401 gfc_clear_attr (&current_attr);
6402 current_attr.contiguous = 1;
6404 return attr_decl ();
6408 match
6409 gfc_match_dimension (void)
6411 gfc_clear_attr (&current_attr);
6412 current_attr.dimension = 1;
6414 return attr_decl ();
6418 match
6419 gfc_match_target (void)
6421 gfc_clear_attr (&current_attr);
6422 current_attr.target = 1;
6424 return attr_decl ();
6428 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6429 statement. */
6431 static match
6432 access_attr_decl (gfc_statement st)
6434 char name[GFC_MAX_SYMBOL_LEN + 1];
6435 interface_type type;
6436 gfc_user_op *uop;
6437 gfc_symbol *sym;
6438 gfc_intrinsic_op op;
6439 match m;
6441 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6442 goto done;
6444 for (;;)
6446 m = gfc_match_generic_spec (&type, name, &op);
6447 if (m == MATCH_NO)
6448 goto syntax;
6449 if (m == MATCH_ERROR)
6450 return MATCH_ERROR;
6452 switch (type)
6454 case INTERFACE_NAMELESS:
6455 case INTERFACE_ABSTRACT:
6456 goto syntax;
6458 case INTERFACE_GENERIC:
6459 if (gfc_get_symbol (name, NULL, &sym))
6460 goto done;
6462 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
6463 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6464 sym->name, NULL) == FAILURE)
6465 return MATCH_ERROR;
6467 break;
6469 case INTERFACE_INTRINSIC_OP:
6470 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6472 gfc_current_ns->operator_access[op] =
6473 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6475 else
6477 gfc_error ("Access specification of the %s operator at %C has "
6478 "already been specified", gfc_op2string (op));
6479 goto done;
6482 break;
6484 case INTERFACE_USER_OP:
6485 uop = gfc_get_uop (name);
6487 if (uop->access == ACCESS_UNKNOWN)
6489 uop->access = (st == ST_PUBLIC)
6490 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6492 else
6494 gfc_error ("Access specification of the .%s. operator at %C "
6495 "has already been specified", sym->name);
6496 goto done;
6499 break;
6502 if (gfc_match_char (',') == MATCH_NO)
6503 break;
6506 if (gfc_match_eos () != MATCH_YES)
6507 goto syntax;
6508 return MATCH_YES;
6510 syntax:
6511 gfc_syntax_error (st);
6513 done:
6514 return MATCH_ERROR;
6518 match
6519 gfc_match_protected (void)
6521 gfc_symbol *sym;
6522 match m;
6524 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6526 gfc_error ("PROTECTED at %C only allowed in specification "
6527 "part of a module");
6528 return MATCH_ERROR;
6532 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
6533 == FAILURE)
6534 return MATCH_ERROR;
6536 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6538 return MATCH_ERROR;
6541 if (gfc_match_eos () == MATCH_YES)
6542 goto syntax;
6544 for(;;)
6546 m = gfc_match_symbol (&sym, 0);
6547 switch (m)
6549 case MATCH_YES:
6550 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6551 == FAILURE)
6552 return MATCH_ERROR;
6553 goto next_item;
6555 case MATCH_NO:
6556 break;
6558 case MATCH_ERROR:
6559 return MATCH_ERROR;
6562 next_item:
6563 if (gfc_match_eos () == MATCH_YES)
6564 break;
6565 if (gfc_match_char (',') != MATCH_YES)
6566 goto syntax;
6569 return MATCH_YES;
6571 syntax:
6572 gfc_error ("Syntax error in PROTECTED statement at %C");
6573 return MATCH_ERROR;
6577 /* The PRIVATE statement is a bit weird in that it can be an attribute
6578 declaration, but also works as a standalone statement inside of a
6579 type declaration or a module. */
6581 match
6582 gfc_match_private (gfc_statement *st)
6585 if (gfc_match ("private") != MATCH_YES)
6586 return MATCH_NO;
6588 if (gfc_current_state () != COMP_MODULE
6589 && !(gfc_current_state () == COMP_DERIVED
6590 && gfc_state_stack->previous
6591 && gfc_state_stack->previous->state == COMP_MODULE)
6592 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6593 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6594 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6596 gfc_error ("PRIVATE statement at %C is only allowed in the "
6597 "specification part of a module");
6598 return MATCH_ERROR;
6601 if (gfc_current_state () == COMP_DERIVED)
6603 if (gfc_match_eos () == MATCH_YES)
6605 *st = ST_PRIVATE;
6606 return MATCH_YES;
6609 gfc_syntax_error (ST_PRIVATE);
6610 return MATCH_ERROR;
6613 if (gfc_match_eos () == MATCH_YES)
6615 *st = ST_PRIVATE;
6616 return MATCH_YES;
6619 *st = ST_ATTR_DECL;
6620 return access_attr_decl (ST_PRIVATE);
6624 match
6625 gfc_match_public (gfc_statement *st)
6628 if (gfc_match ("public") != MATCH_YES)
6629 return MATCH_NO;
6631 if (gfc_current_state () != COMP_MODULE)
6633 gfc_error ("PUBLIC statement at %C is only allowed in the "
6634 "specification part of a module");
6635 return MATCH_ERROR;
6638 if (gfc_match_eos () == MATCH_YES)
6640 *st = ST_PUBLIC;
6641 return MATCH_YES;
6644 *st = ST_ATTR_DECL;
6645 return access_attr_decl (ST_PUBLIC);
6649 /* Workhorse for gfc_match_parameter. */
6651 static match
6652 do_parm (void)
6654 gfc_symbol *sym;
6655 gfc_expr *init;
6656 match m;
6657 gfc_try t;
6659 m = gfc_match_symbol (&sym, 0);
6660 if (m == MATCH_NO)
6661 gfc_error ("Expected variable name at %C in PARAMETER statement");
6663 if (m != MATCH_YES)
6664 return m;
6666 if (gfc_match_char ('=') == MATCH_NO)
6668 gfc_error ("Expected = sign in PARAMETER statement at %C");
6669 return MATCH_ERROR;
6672 m = gfc_match_init_expr (&init);
6673 if (m == MATCH_NO)
6674 gfc_error ("Expected expression at %C in PARAMETER statement");
6675 if (m != MATCH_YES)
6676 return m;
6678 if (sym->ts.type == BT_UNKNOWN
6679 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6681 m = MATCH_ERROR;
6682 goto cleanup;
6685 if (gfc_check_assign_symbol (sym, init) == FAILURE
6686 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6688 m = MATCH_ERROR;
6689 goto cleanup;
6692 if (sym->value)
6694 gfc_error ("Initializing already initialized variable at %C");
6695 m = MATCH_ERROR;
6696 goto cleanup;
6699 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
6700 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6702 cleanup:
6703 gfc_free_expr (init);
6704 return m;
6708 /* Match a parameter statement, with the weird syntax that these have. */
6710 match
6711 gfc_match_parameter (void)
6713 match m;
6715 if (gfc_match_char ('(') == MATCH_NO)
6716 return MATCH_NO;
6718 for (;;)
6720 m = do_parm ();
6721 if (m != MATCH_YES)
6722 break;
6724 if (gfc_match (" )%t") == MATCH_YES)
6725 break;
6727 if (gfc_match_char (',') != MATCH_YES)
6729 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6730 m = MATCH_ERROR;
6731 break;
6735 return m;
6739 /* Save statements have a special syntax. */
6741 match
6742 gfc_match_save (void)
6744 char n[GFC_MAX_SYMBOL_LEN+1];
6745 gfc_common_head *c;
6746 gfc_symbol *sym;
6747 match m;
6749 if (gfc_match_eos () == MATCH_YES)
6751 if (gfc_current_ns->seen_save)
6753 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6754 "follows previous SAVE statement")
6755 == FAILURE)
6756 return MATCH_ERROR;
6759 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6760 return MATCH_YES;
6763 if (gfc_current_ns->save_all)
6765 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6766 "blanket SAVE statement")
6767 == FAILURE)
6768 return MATCH_ERROR;
6771 gfc_match (" ::");
6773 for (;;)
6775 m = gfc_match_symbol (&sym, 0);
6776 switch (m)
6778 case MATCH_YES:
6779 if (gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
6780 &gfc_current_locus) == FAILURE)
6781 return MATCH_ERROR;
6782 goto next_item;
6784 case MATCH_NO:
6785 break;
6787 case MATCH_ERROR:
6788 return MATCH_ERROR;
6791 m = gfc_match (" / %n /", &n);
6792 if (m == MATCH_ERROR)
6793 return MATCH_ERROR;
6794 if (m == MATCH_NO)
6795 goto syntax;
6797 c = gfc_get_common (n, 0);
6798 c->saved = 1;
6800 gfc_current_ns->seen_save = 1;
6802 next_item:
6803 if (gfc_match_eos () == MATCH_YES)
6804 break;
6805 if (gfc_match_char (',') != MATCH_YES)
6806 goto syntax;
6809 return MATCH_YES;
6811 syntax:
6812 gfc_error ("Syntax error in SAVE statement at %C");
6813 return MATCH_ERROR;
6817 match
6818 gfc_match_value (void)
6820 gfc_symbol *sym;
6821 match m;
6823 /* This is not allowed within a BLOCK construct! */
6824 if (gfc_current_state () == COMP_BLOCK)
6826 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
6827 return MATCH_ERROR;
6830 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
6831 == FAILURE)
6832 return MATCH_ERROR;
6834 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6836 return MATCH_ERROR;
6839 if (gfc_match_eos () == MATCH_YES)
6840 goto syntax;
6842 for(;;)
6844 m = gfc_match_symbol (&sym, 0);
6845 switch (m)
6847 case MATCH_YES:
6848 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
6849 == FAILURE)
6850 return MATCH_ERROR;
6851 goto next_item;
6853 case MATCH_NO:
6854 break;
6856 case MATCH_ERROR:
6857 return MATCH_ERROR;
6860 next_item:
6861 if (gfc_match_eos () == MATCH_YES)
6862 break;
6863 if (gfc_match_char (',') != MATCH_YES)
6864 goto syntax;
6867 return MATCH_YES;
6869 syntax:
6870 gfc_error ("Syntax error in VALUE statement at %C");
6871 return MATCH_ERROR;
6875 match
6876 gfc_match_volatile (void)
6878 gfc_symbol *sym;
6879 match m;
6881 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
6882 == FAILURE)
6883 return MATCH_ERROR;
6885 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6887 return MATCH_ERROR;
6890 if (gfc_match_eos () == MATCH_YES)
6891 goto syntax;
6893 for(;;)
6895 /* VOLATILE is special because it can be added to host-associated
6896 symbols locally. Except for coarrays. */
6897 m = gfc_match_symbol (&sym, 1);
6898 switch (m)
6900 case MATCH_YES:
6901 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
6902 for variable in a BLOCK which is defined outside of the BLOCK. */
6903 if (sym->ns != gfc_current_ns && sym->attr.codimension)
6905 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
6906 "%C, which is use-/host-associated", sym->name);
6907 return MATCH_ERROR;
6909 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6910 == FAILURE)
6911 return MATCH_ERROR;
6912 goto next_item;
6914 case MATCH_NO:
6915 break;
6917 case MATCH_ERROR:
6918 return MATCH_ERROR;
6921 next_item:
6922 if (gfc_match_eos () == MATCH_YES)
6923 break;
6924 if (gfc_match_char (',') != MATCH_YES)
6925 goto syntax;
6928 return MATCH_YES;
6930 syntax:
6931 gfc_error ("Syntax error in VOLATILE statement at %C");
6932 return MATCH_ERROR;
6936 match
6937 gfc_match_asynchronous (void)
6939 gfc_symbol *sym;
6940 match m;
6942 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS statement at %C")
6943 == FAILURE)
6944 return MATCH_ERROR;
6946 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6948 return MATCH_ERROR;
6951 if (gfc_match_eos () == MATCH_YES)
6952 goto syntax;
6954 for(;;)
6956 /* ASYNCHRONOUS is special because it can be added to host-associated
6957 symbols locally. */
6958 m = gfc_match_symbol (&sym, 1);
6959 switch (m)
6961 case MATCH_YES:
6962 if (gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus)
6963 == FAILURE)
6964 return MATCH_ERROR;
6965 goto next_item;
6967 case MATCH_NO:
6968 break;
6970 case MATCH_ERROR:
6971 return MATCH_ERROR;
6974 next_item:
6975 if (gfc_match_eos () == MATCH_YES)
6976 break;
6977 if (gfc_match_char (',') != MATCH_YES)
6978 goto syntax;
6981 return MATCH_YES;
6983 syntax:
6984 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
6985 return MATCH_ERROR;
6989 /* Match a module procedure statement. Note that we have to modify
6990 symbols in the parent's namespace because the current one was there
6991 to receive symbols that are in an interface's formal argument list. */
6993 match
6994 gfc_match_modproc (void)
6996 char name[GFC_MAX_SYMBOL_LEN + 1];
6997 gfc_symbol *sym;
6998 match m;
6999 gfc_namespace *module_ns;
7000 gfc_interface *old_interface_head, *interface;
7002 if (gfc_state_stack->state != COMP_INTERFACE
7003 || gfc_state_stack->previous == NULL
7004 || current_interface.type == INTERFACE_NAMELESS
7005 || current_interface.type == INTERFACE_ABSTRACT)
7007 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7008 "interface");
7009 return MATCH_ERROR;
7012 module_ns = gfc_current_ns->parent;
7013 for (; module_ns; module_ns = module_ns->parent)
7014 if (module_ns->proc_name->attr.flavor == FL_MODULE
7015 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7016 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7017 && !module_ns->proc_name->attr.contained))
7018 break;
7020 if (module_ns == NULL)
7021 return MATCH_ERROR;
7023 /* Store the current state of the interface. We will need it if we
7024 end up with a syntax error and need to recover. */
7025 old_interface_head = gfc_current_interface_head ();
7027 for (;;)
7029 locus old_locus = gfc_current_locus;
7030 bool last = false;
7032 m = gfc_match_name (name);
7033 if (m == MATCH_NO)
7034 goto syntax;
7035 if (m != MATCH_YES)
7036 return MATCH_ERROR;
7038 /* Check for syntax error before starting to add symbols to the
7039 current namespace. */
7040 if (gfc_match_eos () == MATCH_YES)
7041 last = true;
7042 if (!last && gfc_match_char (',') != MATCH_YES)
7043 goto syntax;
7045 /* Now we're sure the syntax is valid, we process this item
7046 further. */
7047 if (gfc_get_symbol (name, module_ns, &sym))
7048 return MATCH_ERROR;
7050 if (sym->attr.intrinsic)
7052 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7053 "PROCEDURE", &old_locus);
7054 return MATCH_ERROR;
7057 if (sym->attr.proc != PROC_MODULE
7058 && gfc_add_procedure (&sym->attr, PROC_MODULE,
7059 sym->name, NULL) == FAILURE)
7060 return MATCH_ERROR;
7062 if (gfc_add_interface (sym) == FAILURE)
7063 return MATCH_ERROR;
7065 sym->attr.mod_proc = 1;
7066 sym->declared_at = old_locus;
7068 if (last)
7069 break;
7072 return MATCH_YES;
7074 syntax:
7075 /* Restore the previous state of the interface. */
7076 interface = gfc_current_interface_head ();
7077 gfc_set_current_interface_head (old_interface_head);
7079 /* Free the new interfaces. */
7080 while (interface != old_interface_head)
7082 gfc_interface *i = interface->next;
7083 free (interface);
7084 interface = i;
7087 /* And issue a syntax error. */
7088 gfc_syntax_error (ST_MODULE_PROC);
7089 return MATCH_ERROR;
7093 /* Check a derived type that is being extended. */
7094 static gfc_symbol*
7095 check_extended_derived_type (char *name)
7097 gfc_symbol *extended;
7099 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7101 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7102 return NULL;
7105 if (!extended)
7107 gfc_error ("No such symbol in TYPE definition at %C");
7108 return NULL;
7111 if (extended->attr.flavor != FL_DERIVED)
7113 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7114 "derived type", name);
7115 return NULL;
7118 if (extended->attr.is_bind_c)
7120 gfc_error ("'%s' cannot be extended at %C because it "
7121 "is BIND(C)", extended->name);
7122 return NULL;
7125 if (extended->attr.sequence)
7127 gfc_error ("'%s' cannot be extended at %C because it "
7128 "is a SEQUENCE type", extended->name);
7129 return NULL;
7132 return extended;
7136 /* Match the optional attribute specifiers for a type declaration.
7137 Return MATCH_ERROR if an error is encountered in one of the handled
7138 attributes (public, private, bind(c)), MATCH_NO if what's found is
7139 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7140 checking on attribute conflicts needs to be done. */
7142 match
7143 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7145 /* See if the derived type is marked as private. */
7146 if (gfc_match (" , private") == MATCH_YES)
7148 if (gfc_current_state () != COMP_MODULE)
7150 gfc_error ("Derived type at %C can only be PRIVATE in the "
7151 "specification part of a module");
7152 return MATCH_ERROR;
7155 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
7156 return MATCH_ERROR;
7158 else if (gfc_match (" , public") == MATCH_YES)
7160 if (gfc_current_state () != COMP_MODULE)
7162 gfc_error ("Derived type at %C can only be PUBLIC in the "
7163 "specification part of a module");
7164 return MATCH_ERROR;
7167 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
7168 return MATCH_ERROR;
7170 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7172 /* If the type is defined to be bind(c) it then needs to make
7173 sure that all fields are interoperable. This will
7174 need to be a semantic check on the finished derived type.
7175 See 15.2.3 (lines 9-12) of F2003 draft. */
7176 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
7177 return MATCH_ERROR;
7179 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7181 else if (gfc_match (" , abstract") == MATCH_YES)
7183 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
7184 == FAILURE)
7185 return MATCH_ERROR;
7187 if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
7188 return MATCH_ERROR;
7190 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
7192 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
7193 return MATCH_ERROR;
7195 else
7196 return MATCH_NO;
7198 /* If we get here, something matched. */
7199 return MATCH_YES;
7203 /* Match the beginning of a derived type declaration. If a type name
7204 was the result of a function, then it is possible to have a symbol
7205 already to be known as a derived type yet have no components. */
7207 match
7208 gfc_match_derived_decl (void)
7210 char name[GFC_MAX_SYMBOL_LEN + 1];
7211 char parent[GFC_MAX_SYMBOL_LEN + 1];
7212 symbol_attribute attr;
7213 gfc_symbol *sym;
7214 gfc_symbol *extended;
7215 match m;
7216 match is_type_attr_spec = MATCH_NO;
7217 bool seen_attr = false;
7219 if (gfc_current_state () == COMP_DERIVED)
7220 return MATCH_NO;
7222 name[0] = '\0';
7223 parent[0] = '\0';
7224 gfc_clear_attr (&attr);
7225 extended = NULL;
7229 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7230 if (is_type_attr_spec == MATCH_ERROR)
7231 return MATCH_ERROR;
7232 if (is_type_attr_spec == MATCH_YES)
7233 seen_attr = true;
7234 } while (is_type_attr_spec == MATCH_YES);
7236 /* Deal with derived type extensions. The extension attribute has
7237 been added to 'attr' but now the parent type must be found and
7238 checked. */
7239 if (parent[0])
7240 extended = check_extended_derived_type (parent);
7242 if (parent[0] && !extended)
7243 return MATCH_ERROR;
7245 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7247 gfc_error ("Expected :: in TYPE definition at %C");
7248 return MATCH_ERROR;
7251 m = gfc_match (" %n%t", name);
7252 if (m != MATCH_YES)
7253 return m;
7255 /* Make sure the name is not the name of an intrinsic type. */
7256 if (gfc_is_intrinsic_typename (name))
7258 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7259 "type", name);
7260 return MATCH_ERROR;
7263 if (gfc_get_symbol (name, NULL, &sym))
7264 return MATCH_ERROR;
7266 if (sym->ts.type != BT_UNKNOWN)
7268 gfc_error ("Derived type name '%s' at %C already has a basic type "
7269 "of %s", sym->name, gfc_typename (&sym->ts));
7270 return MATCH_ERROR;
7273 /* The symbol may already have the derived attribute without the
7274 components. The ways this can happen is via a function
7275 definition, an INTRINSIC statement or a subtype in another
7276 derived type that is a pointer. The first part of the AND clause
7277 is true if the symbol is not the return value of a function. */
7278 if (sym->attr.flavor != FL_DERIVED
7279 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
7280 return MATCH_ERROR;
7282 if (sym->components != NULL || sym->attr.zero_comp)
7284 gfc_error ("Derived type definition of '%s' at %C has already been "
7285 "defined", sym->name);
7286 return MATCH_ERROR;
7289 if (attr.access != ACCESS_UNKNOWN
7290 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
7291 return MATCH_ERROR;
7293 /* See if the derived type was labeled as bind(c). */
7294 if (attr.is_bind_c != 0)
7295 sym->attr.is_bind_c = attr.is_bind_c;
7297 /* Construct the f2k_derived namespace if it is not yet there. */
7298 if (!sym->f2k_derived)
7299 sym->f2k_derived = gfc_get_namespace (NULL, 0);
7301 if (extended && !sym->components)
7303 gfc_component *p;
7304 gfc_symtree *st;
7306 /* Add the extended derived type as the first component. */
7307 gfc_add_component (sym, parent, &p);
7308 extended->refs++;
7309 gfc_set_sym_referenced (extended);
7311 p->ts.type = BT_DERIVED;
7312 p->ts.u.derived = extended;
7313 p->initializer = gfc_default_initializer (&p->ts);
7315 /* Set extension level. */
7316 if (extended->attr.extension == 255)
7318 /* Since the extension field is 8 bit wide, we can only have
7319 up to 255 extension levels. */
7320 gfc_error ("Maximum extension level reached with type '%s' at %L",
7321 extended->name, &extended->declared_at);
7322 return MATCH_ERROR;
7324 sym->attr.extension = extended->attr.extension + 1;
7326 /* Provide the links between the extended type and its extension. */
7327 if (!extended->f2k_derived)
7328 extended->f2k_derived = gfc_get_namespace (NULL, 0);
7329 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7330 st->n.sym = sym;
7333 if (!sym->hash_value)
7334 /* Set the hash for the compound name for this type. */
7335 sym->hash_value = gfc_hash_value (sym);
7337 /* Take over the ABSTRACT attribute. */
7338 sym->attr.abstract = attr.abstract;
7340 gfc_new_block = sym;
7342 return MATCH_YES;
7346 /* Cray Pointees can be declared as:
7347 pointer (ipt, a (n,m,...,*)) */
7349 match
7350 gfc_mod_pointee_as (gfc_array_spec *as)
7352 as->cray_pointee = true; /* This will be useful to know later. */
7353 if (as->type == AS_ASSUMED_SIZE)
7354 as->cp_was_assumed = true;
7355 else if (as->type == AS_ASSUMED_SHAPE)
7357 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7358 return MATCH_ERROR;
7360 return MATCH_YES;
7364 /* Match the enum definition statement, here we are trying to match
7365 the first line of enum definition statement.
7366 Returns MATCH_YES if match is found. */
7368 match
7369 gfc_match_enum (void)
7371 match m;
7373 m = gfc_match_eos ();
7374 if (m != MATCH_YES)
7375 return m;
7377 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
7378 == FAILURE)
7379 return MATCH_ERROR;
7381 return MATCH_YES;
7385 /* Returns an initializer whose value is one higher than the value of the
7386 LAST_INITIALIZER argument. If the argument is NULL, the
7387 initializers value will be set to zero. The initializer's kind
7388 will be set to gfc_c_int_kind.
7390 If -fshort-enums is given, the appropriate kind will be selected
7391 later after all enumerators have been parsed. A warning is issued
7392 here if an initializer exceeds gfc_c_int_kind. */
7394 static gfc_expr *
7395 enum_initializer (gfc_expr *last_initializer, locus where)
7397 gfc_expr *result;
7398 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7400 mpz_init (result->value.integer);
7402 if (last_initializer != NULL)
7404 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7405 result->where = last_initializer->where;
7407 if (gfc_check_integer_range (result->value.integer,
7408 gfc_c_int_kind) != ARITH_OK)
7410 gfc_error ("Enumerator exceeds the C integer type at %C");
7411 return NULL;
7414 else
7416 /* Control comes here, if it's the very first enumerator and no
7417 initializer has been given. It will be initialized to zero. */
7418 mpz_set_si (result->value.integer, 0);
7421 return result;
7425 /* Match a variable name with an optional initializer. When this
7426 subroutine is called, a variable is expected to be parsed next.
7427 Depending on what is happening at the moment, updates either the
7428 symbol table or the current interface. */
7430 static match
7431 enumerator_decl (void)
7433 char name[GFC_MAX_SYMBOL_LEN + 1];
7434 gfc_expr *initializer;
7435 gfc_array_spec *as = NULL;
7436 gfc_symbol *sym;
7437 locus var_locus;
7438 match m;
7439 gfc_try t;
7440 locus old_locus;
7442 initializer = NULL;
7443 old_locus = gfc_current_locus;
7445 /* When we get here, we've just matched a list of attributes and
7446 maybe a type and a double colon. The next thing we expect to see
7447 is the name of the symbol. */
7448 m = gfc_match_name (name);
7449 if (m != MATCH_YES)
7450 goto cleanup;
7452 var_locus = gfc_current_locus;
7454 /* OK, we've successfully matched the declaration. Now put the
7455 symbol in the current namespace. If we fail to create the symbol,
7456 bail out. */
7457 if (build_sym (name, NULL, false, &as, &var_locus) == FAILURE)
7459 m = MATCH_ERROR;
7460 goto cleanup;
7463 /* The double colon must be present in order to have initializers.
7464 Otherwise the statement is ambiguous with an assignment statement. */
7465 if (colon_seen)
7467 if (gfc_match_char ('=') == MATCH_YES)
7469 m = gfc_match_init_expr (&initializer);
7470 if (m == MATCH_NO)
7472 gfc_error ("Expected an initialization expression at %C");
7473 m = MATCH_ERROR;
7476 if (m != MATCH_YES)
7477 goto cleanup;
7481 /* If we do not have an initializer, the initialization value of the
7482 previous enumerator (stored in last_initializer) is incremented
7483 by 1 and is used to initialize the current enumerator. */
7484 if (initializer == NULL)
7485 initializer = enum_initializer (last_initializer, old_locus);
7487 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7489 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7490 &var_locus);
7491 m = MATCH_ERROR;
7492 goto cleanup;
7495 /* Store this current initializer, for the next enumerator variable
7496 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7497 use last_initializer below. */
7498 last_initializer = initializer;
7499 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7501 /* Maintain enumerator history. */
7502 gfc_find_symbol (name, NULL, 0, &sym);
7503 create_enum_history (sym, last_initializer);
7505 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
7507 cleanup:
7508 /* Free stuff up and return. */
7509 gfc_free_expr (initializer);
7511 return m;
7515 /* Match the enumerator definition statement. */
7517 match
7518 gfc_match_enumerator_def (void)
7520 match m;
7521 gfc_try t;
7523 gfc_clear_ts (&current_ts);
7525 m = gfc_match (" enumerator");
7526 if (m != MATCH_YES)
7527 return m;
7529 m = gfc_match (" :: ");
7530 if (m == MATCH_ERROR)
7531 return m;
7533 colon_seen = (m == MATCH_YES);
7535 if (gfc_current_state () != COMP_ENUM)
7537 gfc_error ("ENUM definition statement expected before %C");
7538 gfc_free_enum_history ();
7539 return MATCH_ERROR;
7542 (&current_ts)->type = BT_INTEGER;
7543 (&current_ts)->kind = gfc_c_int_kind;
7545 gfc_clear_attr (&current_attr);
7546 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
7547 if (t == FAILURE)
7549 m = MATCH_ERROR;
7550 goto cleanup;
7553 for (;;)
7555 m = enumerator_decl ();
7556 if (m == MATCH_ERROR)
7558 gfc_free_enum_history ();
7559 goto cleanup;
7561 if (m == MATCH_NO)
7562 break;
7564 if (gfc_match_eos () == MATCH_YES)
7565 goto cleanup;
7566 if (gfc_match_char (',') != MATCH_YES)
7567 break;
7570 if (gfc_current_state () == COMP_ENUM)
7572 gfc_free_enum_history ();
7573 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7574 m = MATCH_ERROR;
7577 cleanup:
7578 gfc_free_array_spec (current_as);
7579 current_as = NULL;
7580 return m;
7585 /* Match binding attributes. */
7587 static match
7588 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7590 bool found_passing = false;
7591 bool seen_ptr = false;
7592 match m = MATCH_YES;
7594 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7595 this case the defaults are in there. */
7596 ba->access = ACCESS_UNKNOWN;
7597 ba->pass_arg = NULL;
7598 ba->pass_arg_num = 0;
7599 ba->nopass = 0;
7600 ba->non_overridable = 0;
7601 ba->deferred = 0;
7602 ba->ppc = ppc;
7604 /* If we find a comma, we believe there are binding attributes. */
7605 m = gfc_match_char (',');
7606 if (m == MATCH_NO)
7607 goto done;
7611 /* Access specifier. */
7613 m = gfc_match (" public");
7614 if (m == MATCH_ERROR)
7615 goto error;
7616 if (m == MATCH_YES)
7618 if (ba->access != ACCESS_UNKNOWN)
7620 gfc_error ("Duplicate access-specifier at %C");
7621 goto error;
7624 ba->access = ACCESS_PUBLIC;
7625 continue;
7628 m = gfc_match (" private");
7629 if (m == MATCH_ERROR)
7630 goto error;
7631 if (m == MATCH_YES)
7633 if (ba->access != ACCESS_UNKNOWN)
7635 gfc_error ("Duplicate access-specifier at %C");
7636 goto error;
7639 ba->access = ACCESS_PRIVATE;
7640 continue;
7643 /* If inside GENERIC, the following is not allowed. */
7644 if (!generic)
7647 /* NOPASS flag. */
7648 m = gfc_match (" nopass");
7649 if (m == MATCH_ERROR)
7650 goto error;
7651 if (m == MATCH_YES)
7653 if (found_passing)
7655 gfc_error ("Binding attributes already specify passing,"
7656 " illegal NOPASS at %C");
7657 goto error;
7660 found_passing = true;
7661 ba->nopass = 1;
7662 continue;
7665 /* PASS possibly including argument. */
7666 m = gfc_match (" pass");
7667 if (m == MATCH_ERROR)
7668 goto error;
7669 if (m == MATCH_YES)
7671 char arg[GFC_MAX_SYMBOL_LEN + 1];
7673 if (found_passing)
7675 gfc_error ("Binding attributes already specify passing,"
7676 " illegal PASS at %C");
7677 goto error;
7680 m = gfc_match (" ( %n )", arg);
7681 if (m == MATCH_ERROR)
7682 goto error;
7683 if (m == MATCH_YES)
7684 ba->pass_arg = gfc_get_string (arg);
7685 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7687 found_passing = true;
7688 ba->nopass = 0;
7689 continue;
7692 if (ppc)
7694 /* POINTER flag. */
7695 m = gfc_match (" pointer");
7696 if (m == MATCH_ERROR)
7697 goto error;
7698 if (m == MATCH_YES)
7700 if (seen_ptr)
7702 gfc_error ("Duplicate POINTER attribute at %C");
7703 goto error;
7706 seen_ptr = true;
7707 continue;
7710 else
7712 /* NON_OVERRIDABLE flag. */
7713 m = gfc_match (" non_overridable");
7714 if (m == MATCH_ERROR)
7715 goto error;
7716 if (m == MATCH_YES)
7718 if (ba->non_overridable)
7720 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7721 goto error;
7724 ba->non_overridable = 1;
7725 continue;
7728 /* DEFERRED flag. */
7729 m = gfc_match (" deferred");
7730 if (m == MATCH_ERROR)
7731 goto error;
7732 if (m == MATCH_YES)
7734 if (ba->deferred)
7736 gfc_error ("Duplicate DEFERRED at %C");
7737 goto error;
7740 ba->deferred = 1;
7741 continue;
7747 /* Nothing matching found. */
7748 if (generic)
7749 gfc_error ("Expected access-specifier at %C");
7750 else
7751 gfc_error ("Expected binding attribute at %C");
7752 goto error;
7754 while (gfc_match_char (',') == MATCH_YES);
7756 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
7757 if (ba->non_overridable && ba->deferred)
7759 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7760 goto error;
7763 m = MATCH_YES;
7765 done:
7766 if (ba->access == ACCESS_UNKNOWN)
7767 ba->access = gfc_typebound_default_access;
7769 if (ppc && !seen_ptr)
7771 gfc_error ("POINTER attribute is required for procedure pointer component"
7772 " at %C");
7773 goto error;
7776 return m;
7778 error:
7779 return MATCH_ERROR;
7783 /* Match a PROCEDURE specific binding inside a derived type. */
7785 static match
7786 match_procedure_in_type (void)
7788 char name[GFC_MAX_SYMBOL_LEN + 1];
7789 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
7790 char* target = NULL, *ifc = NULL;
7791 gfc_typebound_proc tb;
7792 bool seen_colons;
7793 bool seen_attrs;
7794 match m;
7795 gfc_symtree* stree;
7796 gfc_namespace* ns;
7797 gfc_symbol* block;
7798 int num;
7800 /* Check current state. */
7801 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
7802 block = gfc_state_stack->previous->sym;
7803 gcc_assert (block);
7805 /* Try to match PROCEDURE(interface). */
7806 if (gfc_match (" (") == MATCH_YES)
7808 m = gfc_match_name (target_buf);
7809 if (m == MATCH_ERROR)
7810 return m;
7811 if (m != MATCH_YES)
7813 gfc_error ("Interface-name expected after '(' at %C");
7814 return MATCH_ERROR;
7817 if (gfc_match (" )") != MATCH_YES)
7819 gfc_error ("')' expected at %C");
7820 return MATCH_ERROR;
7823 ifc = target_buf;
7826 /* Construct the data structure. */
7827 memset (&tb, 0, sizeof (tb));
7828 tb.where = gfc_current_locus;
7830 /* Match binding attributes. */
7831 m = match_binding_attributes (&tb, false, false);
7832 if (m == MATCH_ERROR)
7833 return m;
7834 seen_attrs = (m == MATCH_YES);
7836 /* Check that attribute DEFERRED is given if an interface is specified. */
7837 if (tb.deferred && !ifc)
7839 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7840 return MATCH_ERROR;
7842 if (ifc && !tb.deferred)
7844 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7845 return MATCH_ERROR;
7848 /* Match the colons. */
7849 m = gfc_match (" ::");
7850 if (m == MATCH_ERROR)
7851 return m;
7852 seen_colons = (m == MATCH_YES);
7853 if (seen_attrs && !seen_colons)
7855 gfc_error ("Expected '::' after binding-attributes at %C");
7856 return MATCH_ERROR;
7859 /* Match the binding names. */
7860 for(num=1;;num++)
7862 m = gfc_match_name (name);
7863 if (m == MATCH_ERROR)
7864 return m;
7865 if (m == MATCH_NO)
7867 gfc_error ("Expected binding name at %C");
7868 return MATCH_ERROR;
7871 if (num>1 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: PROCEDURE list"
7872 " at %C") == FAILURE)
7873 return MATCH_ERROR;
7875 /* Try to match the '=> target', if it's there. */
7876 target = ifc;
7877 m = gfc_match (" =>");
7878 if (m == MATCH_ERROR)
7879 return m;
7880 if (m == MATCH_YES)
7882 if (tb.deferred)
7884 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7885 return MATCH_ERROR;
7888 if (!seen_colons)
7890 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7891 " at %C");
7892 return MATCH_ERROR;
7895 m = gfc_match_name (target_buf);
7896 if (m == MATCH_ERROR)
7897 return m;
7898 if (m == MATCH_NO)
7900 gfc_error ("Expected binding target after '=>' at %C");
7901 return MATCH_ERROR;
7903 target = target_buf;
7906 /* If no target was found, it has the same name as the binding. */
7907 if (!target)
7908 target = name;
7910 /* Get the namespace to insert the symbols into. */
7911 ns = block->f2k_derived;
7912 gcc_assert (ns);
7914 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7915 if (tb.deferred && !block->attr.abstract)
7917 gfc_error ("Type '%s' containing DEFERRED binding at %C "
7918 "is not ABSTRACT", block->name);
7919 return MATCH_ERROR;
7922 /* See if we already have a binding with this name in the symtree which
7923 would be an error. If a GENERIC already targetted this binding, it may
7924 be already there but then typebound is still NULL. */
7925 stree = gfc_find_symtree (ns->tb_sym_root, name);
7926 if (stree && stree->n.tb)
7928 gfc_error ("There is already a procedure with binding name '%s' for "
7929 "the derived type '%s' at %C", name, block->name);
7930 return MATCH_ERROR;
7933 /* Insert it and set attributes. */
7935 if (!stree)
7937 stree = gfc_new_symtree (&ns->tb_sym_root, name);
7938 gcc_assert (stree);
7940 stree->n.tb = gfc_get_typebound_proc (&tb);
7942 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
7943 false))
7944 return MATCH_ERROR;
7945 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
7947 if (gfc_match_eos () == MATCH_YES)
7948 return MATCH_YES;
7949 if (gfc_match_char (',') != MATCH_YES)
7950 goto syntax;
7953 syntax:
7954 gfc_error ("Syntax error in PROCEDURE statement at %C");
7955 return MATCH_ERROR;
7959 /* Match a GENERIC procedure binding inside a derived type. */
7961 match
7962 gfc_match_generic (void)
7964 char name[GFC_MAX_SYMBOL_LEN + 1];
7965 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
7966 gfc_symbol* block;
7967 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
7968 gfc_typebound_proc* tb;
7969 gfc_namespace* ns;
7970 interface_type op_type;
7971 gfc_intrinsic_op op;
7972 match m;
7974 /* Check current state. */
7975 if (gfc_current_state () == COMP_DERIVED)
7977 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
7978 return MATCH_ERROR;
7980 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
7981 return MATCH_NO;
7982 block = gfc_state_stack->previous->sym;
7983 ns = block->f2k_derived;
7984 gcc_assert (block && ns);
7986 memset (&tbattr, 0, sizeof (tbattr));
7987 tbattr.where = gfc_current_locus;
7989 /* See if we get an access-specifier. */
7990 m = match_binding_attributes (&tbattr, true, false);
7991 if (m == MATCH_ERROR)
7992 goto error;
7994 /* Now the colons, those are required. */
7995 if (gfc_match (" ::") != MATCH_YES)
7997 gfc_error ("Expected '::' at %C");
7998 goto error;
8001 /* Match the binding name; depending on type (operator / generic) format
8002 it for future error messages into bind_name. */
8004 m = gfc_match_generic_spec (&op_type, name, &op);
8005 if (m == MATCH_ERROR)
8006 return MATCH_ERROR;
8007 if (m == MATCH_NO)
8009 gfc_error ("Expected generic name or operator descriptor at %C");
8010 goto error;
8013 switch (op_type)
8015 case INTERFACE_GENERIC:
8016 snprintf (bind_name, sizeof (bind_name), "%s", name);
8017 break;
8019 case INTERFACE_USER_OP:
8020 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8021 break;
8023 case INTERFACE_INTRINSIC_OP:
8024 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8025 gfc_op2string (op));
8026 break;
8028 default:
8029 gcc_unreachable ();
8032 /* Match the required =>. */
8033 if (gfc_match (" =>") != MATCH_YES)
8035 gfc_error ("Expected '=>' at %C");
8036 goto error;
8039 /* Try to find existing GENERIC binding with this name / for this operator;
8040 if there is something, check that it is another GENERIC and then extend
8041 it rather than building a new node. Otherwise, create it and put it
8042 at the right position. */
8044 switch (op_type)
8046 case INTERFACE_USER_OP:
8047 case INTERFACE_GENERIC:
8049 const bool is_op = (op_type == INTERFACE_USER_OP);
8050 gfc_symtree* st;
8052 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8053 if (st)
8055 tb = st->n.tb;
8056 gcc_assert (tb);
8058 else
8059 tb = NULL;
8061 break;
8064 case INTERFACE_INTRINSIC_OP:
8065 tb = ns->tb_op[op];
8066 break;
8068 default:
8069 gcc_unreachable ();
8072 if (tb)
8074 if (!tb->is_generic)
8076 gcc_assert (op_type == INTERFACE_GENERIC);
8077 gfc_error ("There's already a non-generic procedure with binding name"
8078 " '%s' for the derived type '%s' at %C",
8079 bind_name, block->name);
8080 goto error;
8083 if (tb->access != tbattr.access)
8085 gfc_error ("Binding at %C must have the same access as already"
8086 " defined binding '%s'", bind_name);
8087 goto error;
8090 else
8092 tb = gfc_get_typebound_proc (NULL);
8093 tb->where = gfc_current_locus;
8094 tb->access = tbattr.access;
8095 tb->is_generic = 1;
8096 tb->u.generic = NULL;
8098 switch (op_type)
8100 case INTERFACE_GENERIC:
8101 case INTERFACE_USER_OP:
8103 const bool is_op = (op_type == INTERFACE_USER_OP);
8104 gfc_symtree* st;
8106 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8107 name);
8108 gcc_assert (st);
8109 st->n.tb = tb;
8111 break;
8114 case INTERFACE_INTRINSIC_OP:
8115 ns->tb_op[op] = tb;
8116 break;
8118 default:
8119 gcc_unreachable ();
8123 /* Now, match all following names as specific targets. */
8126 gfc_symtree* target_st;
8127 gfc_tbp_generic* target;
8129 m = gfc_match_name (name);
8130 if (m == MATCH_ERROR)
8131 goto error;
8132 if (m == MATCH_NO)
8134 gfc_error ("Expected specific binding name at %C");
8135 goto error;
8138 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8140 /* See if this is a duplicate specification. */
8141 for (target = tb->u.generic; target; target = target->next)
8142 if (target_st == target->specific_st)
8144 gfc_error ("'%s' already defined as specific binding for the"
8145 " generic '%s' at %C", name, bind_name);
8146 goto error;
8149 target = gfc_get_tbp_generic ();
8150 target->specific_st = target_st;
8151 target->specific = NULL;
8152 target->next = tb->u.generic;
8153 tb->u.generic = target;
8155 while (gfc_match (" ,") == MATCH_YES);
8157 /* Here should be the end. */
8158 if (gfc_match_eos () != MATCH_YES)
8160 gfc_error ("Junk after GENERIC binding at %C");
8161 goto error;
8164 return MATCH_YES;
8166 error:
8167 return MATCH_ERROR;
8171 /* Match a FINAL declaration inside a derived type. */
8173 match
8174 gfc_match_final_decl (void)
8176 char name[GFC_MAX_SYMBOL_LEN + 1];
8177 gfc_symbol* sym;
8178 match m;
8179 gfc_namespace* module_ns;
8180 bool first, last;
8181 gfc_symbol* block;
8183 if (gfc_current_form == FORM_FREE)
8185 char c = gfc_peek_ascii_char ();
8186 if (!gfc_is_whitespace (c) && c != ':')
8187 return MATCH_NO;
8190 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8192 if (gfc_current_form == FORM_FIXED)
8193 return MATCH_NO;
8195 gfc_error ("FINAL declaration at %C must be inside a derived type "
8196 "CONTAINS section");
8197 return MATCH_ERROR;
8200 block = gfc_state_stack->previous->sym;
8201 gcc_assert (block);
8203 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8204 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8206 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8207 " specification part of a MODULE");
8208 return MATCH_ERROR;
8211 module_ns = gfc_current_ns;
8212 gcc_assert (module_ns);
8213 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8215 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8216 if (gfc_match (" ::") == MATCH_ERROR)
8217 return MATCH_ERROR;
8219 /* Match the sequence of procedure names. */
8220 first = true;
8221 last = false;
8224 gfc_finalizer* f;
8226 if (first && gfc_match_eos () == MATCH_YES)
8228 gfc_error ("Empty FINAL at %C");
8229 return MATCH_ERROR;
8232 m = gfc_match_name (name);
8233 if (m == MATCH_NO)
8235 gfc_error ("Expected module procedure name at %C");
8236 return MATCH_ERROR;
8238 else if (m != MATCH_YES)
8239 return MATCH_ERROR;
8241 if (gfc_match_eos () == MATCH_YES)
8242 last = true;
8243 if (!last && gfc_match_char (',') != MATCH_YES)
8245 gfc_error ("Expected ',' at %C");
8246 return MATCH_ERROR;
8249 if (gfc_get_symbol (name, module_ns, &sym))
8251 gfc_error ("Unknown procedure name \"%s\" at %C", name);
8252 return MATCH_ERROR;
8255 /* Mark the symbol as module procedure. */
8256 if (sym->attr.proc != PROC_MODULE
8257 && gfc_add_procedure (&sym->attr, PROC_MODULE,
8258 sym->name, NULL) == FAILURE)
8259 return MATCH_ERROR;
8261 /* Check if we already have this symbol in the list, this is an error. */
8262 for (f = block->f2k_derived->finalizers; f; f = f->next)
8263 if (f->proc_sym == sym)
8265 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8266 name);
8267 return MATCH_ERROR;
8270 /* Add this symbol to the list of finalizers. */
8271 gcc_assert (block->f2k_derived);
8272 ++sym->refs;
8273 f = XCNEW (gfc_finalizer);
8274 f->proc_sym = sym;
8275 f->proc_tree = NULL;
8276 f->where = gfc_current_locus;
8277 f->next = block->f2k_derived->finalizers;
8278 block->f2k_derived->finalizers = f;
8280 first = false;
8282 while (!last);
8284 return MATCH_YES;
8288 const ext_attr_t ext_attr_list[] = {
8289 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8290 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8291 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
8292 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
8293 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
8294 { NULL, EXT_ATTR_LAST, NULL }
8297 /* Match a !GCC$ ATTRIBUTES statement of the form:
8298 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8299 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8301 TODO: We should support all GCC attributes using the same syntax for
8302 the attribute list, i.e. the list in C
8303 __attributes(( attribute-list ))
8304 matches then
8305 !GCC$ ATTRIBUTES attribute-list ::
8306 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8307 saved into a TREE.
8309 As there is absolutely no risk of confusion, we should never return
8310 MATCH_NO. */
8311 match
8312 gfc_match_gcc_attributes (void)
8314 symbol_attribute attr;
8315 char name[GFC_MAX_SYMBOL_LEN + 1];
8316 unsigned id;
8317 gfc_symbol *sym;
8318 match m;
8320 gfc_clear_attr (&attr);
8321 for(;;)
8323 char ch;
8325 if (gfc_match_name (name) != MATCH_YES)
8326 return MATCH_ERROR;
8328 for (id = 0; id < EXT_ATTR_LAST; id++)
8329 if (strcmp (name, ext_attr_list[id].name) == 0)
8330 break;
8332 if (id == EXT_ATTR_LAST)
8334 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8335 return MATCH_ERROR;
8338 if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
8339 == FAILURE)
8340 return MATCH_ERROR;
8342 gfc_gobble_whitespace ();
8343 ch = gfc_next_ascii_char ();
8344 if (ch == ':')
8346 /* This is the successful exit condition for the loop. */
8347 if (gfc_next_ascii_char () == ':')
8348 break;
8351 if (ch == ',')
8352 continue;
8354 goto syntax;
8357 if (gfc_match_eos () == MATCH_YES)
8358 goto syntax;
8360 for(;;)
8362 m = gfc_match_name (name);
8363 if (m != MATCH_YES)
8364 return m;
8366 if (find_special (name, &sym, true))
8367 return MATCH_ERROR;
8369 sym->attr.ext_attr |= attr.ext_attr;
8371 if (gfc_match_eos () == MATCH_YES)
8372 break;
8374 if (gfc_match_char (',') != MATCH_YES)
8375 goto syntax;
8378 return MATCH_YES;
8380 syntax:
8381 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
8382 return MATCH_ERROR;