Daily bump.
[official-gcc.git] / gcc / fortran / decl.c
blobd8988fd201534950a3b5aeb91fafaf047f5afc53
1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "gfortran.h"
26 #include "match.h"
27 #include "parse.h"
30 /* This flag is set if an old-style length selector is matched
31 during a type-declaration statement. */
33 static int old_char_selector;
35 /* When variables acquire types and attributes from a declaration
36 statement, they get them from the following static variables. The
37 first part of a declaration sets these variables and the second
38 part copies these into symbol structures. */
40 static gfc_typespec current_ts;
42 static symbol_attribute current_attr;
43 static gfc_array_spec *current_as;
44 static int colon_seen;
46 /* Initializer of the previous enumerator. */
48 static gfc_expr *last_initializer;
50 /* History of all the enumerators is maintained, so that
51 kind values of all the enumerators could be updated depending
52 upon the maximum initialized value. */
54 typedef struct enumerator_history
56 gfc_symbol *sym;
57 gfc_expr *initializer;
58 struct enumerator_history *next;
60 enumerator_history;
62 /* Header of enum history chain. */
64 static enumerator_history *enum_history = NULL;
66 /* Pointer of enum history node containing largest initializer. */
68 static enumerator_history *max_enum = NULL;
70 /* gfc_new_block points to the symbol of a newly matched block. */
72 gfc_symbol *gfc_new_block;
75 /********************* DATA statement subroutines *********************/
77 /* Free a gfc_data_variable structure and everything beneath it. */
79 static void
80 free_variable (gfc_data_variable * p)
82 gfc_data_variable *q;
84 for (; p; p = q)
86 q = p->next;
87 gfc_free_expr (p->expr);
88 gfc_free_iterator (&p->iter, 0);
89 free_variable (p->list);
91 gfc_free (p);
96 /* Free a gfc_data_value structure and everything beneath it. */
98 static void
99 free_value (gfc_data_value * p)
101 gfc_data_value *q;
103 for (; p; p = q)
105 q = p->next;
106 gfc_free_expr (p->expr);
107 gfc_free (p);
112 /* Free a list of gfc_data structures. */
114 void
115 gfc_free_data (gfc_data * p)
117 gfc_data *q;
119 for (; p; p = q)
121 q = p->next;
123 free_variable (p->var);
124 free_value (p->value);
126 gfc_free (p);
131 /* Free all data in a namespace. */
132 static void
133 gfc_free_data_all (gfc_namespace * ns)
135 gfc_data *d;
137 for (;ns->data;)
139 d = ns->data->next;
140 gfc_free (ns->data);
141 ns->data = d;
146 static match var_element (gfc_data_variable *);
148 /* Match a list of variables terminated by an iterator and a right
149 parenthesis. */
151 static match
152 var_list (gfc_data_variable * parent)
154 gfc_data_variable *tail, var;
155 match m;
157 m = var_element (&var);
158 if (m == MATCH_ERROR)
159 return MATCH_ERROR;
160 if (m == MATCH_NO)
161 goto syntax;
163 tail = gfc_get_data_variable ();
164 *tail = var;
166 parent->list = tail;
168 for (;;)
170 if (gfc_match_char (',') != MATCH_YES)
171 goto syntax;
173 m = gfc_match_iterator (&parent->iter, 1);
174 if (m == MATCH_YES)
175 break;
176 if (m == MATCH_ERROR)
177 return MATCH_ERROR;
179 m = var_element (&var);
180 if (m == MATCH_ERROR)
181 return MATCH_ERROR;
182 if (m == MATCH_NO)
183 goto syntax;
185 tail->next = gfc_get_data_variable ();
186 tail = tail->next;
188 *tail = var;
191 if (gfc_match_char (')') != MATCH_YES)
192 goto syntax;
193 return MATCH_YES;
195 syntax:
196 gfc_syntax_error (ST_DATA);
197 return MATCH_ERROR;
201 /* Match a single element in a data variable list, which can be a
202 variable-iterator list. */
204 static match
205 var_element (gfc_data_variable * new)
207 match m;
208 gfc_symbol *sym;
210 memset (new, 0, sizeof (gfc_data_variable));
212 if (gfc_match_char ('(') == MATCH_YES)
213 return var_list (new);
215 m = gfc_match_variable (&new->expr, 0);
216 if (m != MATCH_YES)
217 return m;
219 sym = new->expr->symtree->n.sym;
221 if (!sym->attr.function && gfc_current_ns->parent && gfc_current_ns->parent == sym->ns)
223 gfc_error ("Host associated variable '%s' may not be in the DATA "
224 "statement at %C", sym->name);
225 return MATCH_ERROR;
228 if (gfc_current_state () != COMP_BLOCK_DATA
229 && sym->attr.in_common
230 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
231 "common block variable '%s' in DATA statement at %C",
232 sym->name) == FAILURE)
233 return MATCH_ERROR;
235 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
236 return MATCH_ERROR;
238 return MATCH_YES;
242 /* Match the top-level list of data variables. */
244 static match
245 top_var_list (gfc_data * d)
247 gfc_data_variable var, *tail, *new;
248 match m;
250 tail = NULL;
252 for (;;)
254 m = var_element (&var);
255 if (m == MATCH_NO)
256 goto syntax;
257 if (m == MATCH_ERROR)
258 return MATCH_ERROR;
260 new = gfc_get_data_variable ();
261 *new = var;
263 if (tail == NULL)
264 d->var = new;
265 else
266 tail->next = new;
268 tail = new;
270 if (gfc_match_char ('/') == MATCH_YES)
271 break;
272 if (gfc_match_char (',') != MATCH_YES)
273 goto syntax;
276 return MATCH_YES;
278 syntax:
279 gfc_syntax_error (ST_DATA);
280 gfc_free_data_all (gfc_current_ns);
281 return MATCH_ERROR;
285 static match
286 match_data_constant (gfc_expr ** result)
288 char name[GFC_MAX_SYMBOL_LEN + 1];
289 gfc_symbol *sym;
290 gfc_expr *expr;
291 match m;
293 m = gfc_match_literal_constant (&expr, 1);
294 if (m == MATCH_YES)
296 *result = expr;
297 return MATCH_YES;
300 if (m == MATCH_ERROR)
301 return MATCH_ERROR;
303 m = gfc_match_null (result);
304 if (m != MATCH_NO)
305 return m;
307 m = gfc_match_name (name);
308 if (m != MATCH_YES)
309 return m;
311 if (gfc_find_symbol (name, NULL, 1, &sym))
312 return MATCH_ERROR;
314 if (sym == NULL
315 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
317 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
318 name);
319 return MATCH_ERROR;
321 else if (sym->attr.flavor == FL_DERIVED)
322 return gfc_match_structure_constructor (sym, result);
324 *result = gfc_copy_expr (sym->value);
325 return MATCH_YES;
329 /* Match a list of values in a DATA statement. The leading '/' has
330 already been seen at this point. */
332 static match
333 top_val_list (gfc_data * data)
335 gfc_data_value *new, *tail;
336 gfc_expr *expr;
337 const char *msg;
338 match m;
340 tail = NULL;
342 for (;;)
344 m = match_data_constant (&expr);
345 if (m == MATCH_NO)
346 goto syntax;
347 if (m == MATCH_ERROR)
348 return MATCH_ERROR;
350 new = gfc_get_data_value ();
352 if (tail == NULL)
353 data->value = new;
354 else
355 tail->next = new;
357 tail = new;
359 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
361 tail->expr = expr;
362 tail->repeat = 1;
364 else
366 signed int tmp;
367 msg = gfc_extract_int (expr, &tmp);
368 gfc_free_expr (expr);
369 if (msg != NULL)
371 gfc_error (msg);
372 return MATCH_ERROR;
374 tail->repeat = tmp;
376 m = match_data_constant (&tail->expr);
377 if (m == MATCH_NO)
378 goto syntax;
379 if (m == MATCH_ERROR)
380 return MATCH_ERROR;
383 if (gfc_match_char ('/') == MATCH_YES)
384 break;
385 if (gfc_match_char (',') == MATCH_NO)
386 goto syntax;
389 return MATCH_YES;
391 syntax:
392 gfc_syntax_error (ST_DATA);
393 gfc_free_data_all (gfc_current_ns);
394 return MATCH_ERROR;
398 /* Matches an old style initialization. */
400 static match
401 match_old_style_init (const char *name)
403 match m;
404 gfc_symtree *st;
405 gfc_symbol *sym;
406 gfc_data *newdata;
408 /* Set up data structure to hold initializers. */
409 gfc_find_sym_tree (name, NULL, 0, &st);
410 sym = st->n.sym;
412 newdata = gfc_get_data ();
413 newdata->var = gfc_get_data_variable ();
414 newdata->var->expr = gfc_get_variable_expr (st);
415 newdata->where = gfc_current_locus;
417 /* Match initial value list. This also eats the terminal
418 '/'. */
419 m = top_val_list (newdata);
420 if (m != MATCH_YES)
422 gfc_free (newdata);
423 return m;
426 if (gfc_pure (NULL))
428 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
429 gfc_free (newdata);
430 return MATCH_ERROR;
433 /* Mark the variable as having appeared in a data statement. */
434 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
436 gfc_free (newdata);
437 return MATCH_ERROR;
440 /* Chain in namespace list of DATA initializers. */
441 newdata->next = gfc_current_ns->data;
442 gfc_current_ns->data = newdata;
444 return m;
447 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
448 we are matching a DATA statement and are therefore issuing an error
449 if we encounter something unexpected, if not, we're trying to match
450 an old-style initialization expression of the form INTEGER I /2/. */
452 match
453 gfc_match_data (void)
455 gfc_data *new;
456 match m;
458 for (;;)
460 new = gfc_get_data ();
461 new->where = gfc_current_locus;
463 m = top_var_list (new);
464 if (m != MATCH_YES)
465 goto cleanup;
467 m = top_val_list (new);
468 if (m != MATCH_YES)
469 goto cleanup;
471 new->next = gfc_current_ns->data;
472 gfc_current_ns->data = new;
474 if (gfc_match_eos () == MATCH_YES)
475 break;
477 gfc_match_char (','); /* Optional comma */
480 if (gfc_pure (NULL))
482 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
483 return MATCH_ERROR;
486 return MATCH_YES;
488 cleanup:
489 gfc_free_data (new);
490 return MATCH_ERROR;
494 /************************ Declaration statements *********************/
496 /* Match an intent specification. Since this can only happen after an
497 INTENT word, a legal intent-spec must follow. */
499 static sym_intent
500 match_intent_spec (void)
503 if (gfc_match (" ( in out )") == MATCH_YES)
504 return INTENT_INOUT;
505 if (gfc_match (" ( in )") == MATCH_YES)
506 return INTENT_IN;
507 if (gfc_match (" ( out )") == MATCH_YES)
508 return INTENT_OUT;
510 gfc_error ("Bad INTENT specification at %C");
511 return INTENT_UNKNOWN;
515 /* Matches a character length specification, which is either a
516 specification expression or a '*'. */
518 static match
519 char_len_param_value (gfc_expr ** expr)
522 if (gfc_match_char ('*') == MATCH_YES)
524 *expr = NULL;
525 return MATCH_YES;
528 return gfc_match_expr (expr);
532 /* A character length is a '*' followed by a literal integer or a
533 char_len_param_value in parenthesis. */
535 static match
536 match_char_length (gfc_expr ** expr)
538 int length;
539 match m;
541 m = gfc_match_char ('*');
542 if (m != MATCH_YES)
543 return m;
545 m = gfc_match_small_literal_int (&length, NULL);
546 if (m == MATCH_ERROR)
547 return m;
549 if (m == MATCH_YES)
551 *expr = gfc_int_expr (length);
552 return m;
555 if (gfc_match_char ('(') == MATCH_NO)
556 goto syntax;
558 m = char_len_param_value (expr);
559 if (m == MATCH_ERROR)
560 return m;
561 if (m == MATCH_NO)
562 goto syntax;
564 if (gfc_match_char (')') == MATCH_NO)
566 gfc_free_expr (*expr);
567 *expr = NULL;
568 goto syntax;
571 return MATCH_YES;
573 syntax:
574 gfc_error ("Syntax error in character length specification at %C");
575 return MATCH_ERROR;
579 /* Special subroutine for finding a symbol. Check if the name is found
580 in the current name space. If not, and we're compiling a function or
581 subroutine and the parent compilation unit is an interface, then check
582 to see if the name we've been given is the name of the interface
583 (located in another namespace). */
585 static int
586 find_special (const char *name, gfc_symbol ** result)
588 gfc_state_data *s;
589 int i;
591 i = gfc_get_symbol (name, NULL, result);
592 if (i==0)
593 goto end;
595 if (gfc_current_state () != COMP_SUBROUTINE
596 && gfc_current_state () != COMP_FUNCTION)
597 goto end;
599 s = gfc_state_stack->previous;
600 if (s == NULL)
601 goto end;
603 if (s->state != COMP_INTERFACE)
604 goto end;
605 if (s->sym == NULL)
606 goto end; /* Nameless interface */
608 if (strcmp (name, s->sym->name) == 0)
610 *result = s->sym;
611 return 0;
614 end:
615 return i;
619 /* Special subroutine for getting a symbol node associated with a
620 procedure name, used in SUBROUTINE and FUNCTION statements. The
621 symbol is created in the parent using with symtree node in the
622 child unit pointing to the symbol. If the current namespace has no
623 parent, then the symbol is just created in the current unit. */
625 static int
626 get_proc_name (const char *name, gfc_symbol ** result,
627 bool module_fcn_entry)
629 gfc_symtree *st;
630 gfc_symbol *sym;
631 int rc;
633 /* Module functions have to be left in their own namespace because
634 they have potentially (almost certainly!) already been referenced.
635 In this sense, they are rather like external functions. This is
636 fixed up in resolve.c(resolve_entries), where the symbol name-
637 space is set to point to the master function, so that the fake
638 result mechanism can work. */
639 if (module_fcn_entry)
640 rc = gfc_get_symbol (name, NULL, result);
641 else
642 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
644 sym = *result;
645 gfc_current_ns->refs++;
647 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
649 /* Trap another encompassed procedure with the same name. All
650 these conditions are necessary to avoid picking up an entry
651 whose name clashes with that of the encompassing procedure;
652 this is handled using gsymbols to register unique,globally
653 accessible names. */
654 if (sym->attr.flavor != 0
655 && sym->attr.proc != 0
656 && (sym->attr.subroutine || sym->attr.function)
657 && sym->attr.if_source != IFSRC_UNKNOWN)
658 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
659 name, &sym->declared_at);
661 /* Trap declarations of attributes in encompassing scope. The
662 signature for this is that ts.kind is set. Legitimate
663 references only set ts.type. */
664 if (sym->ts.kind != 0
665 && !sym->attr.implicit_type
666 && sym->attr.proc == 0
667 && gfc_current_ns->parent != NULL
668 && sym->attr.access == 0
669 && !module_fcn_entry)
670 gfc_error_now ("Procedure '%s' at %C has an explicit interface"
671 " and must not have attributes declared at %L",
672 name, &sym->declared_at);
675 if (gfc_current_ns->parent == NULL || *result == NULL)
676 return rc;
678 /* Module function entries will already have a symtree in
679 the current namespace but will need one at module level. */
680 if (module_fcn_entry)
681 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
682 else
683 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
685 st->n.sym = sym;
686 sym->refs++;
688 /* See if the procedure should be a module procedure */
690 if (((sym->ns->proc_name != NULL
691 && sym->ns->proc_name->attr.flavor == FL_MODULE
692 && sym->attr.proc != PROC_MODULE) || module_fcn_entry)
693 && gfc_add_procedure (&sym->attr, PROC_MODULE,
694 sym->name, NULL) == FAILURE)
695 rc = 2;
697 return rc;
701 /* Function called by variable_decl() that adds a name to the symbol
702 table. */
704 static try
705 build_sym (const char *name, gfc_charlen * cl,
706 gfc_array_spec ** as, locus * var_locus)
708 symbol_attribute attr;
709 gfc_symbol *sym;
711 /* if (find_special (name, &sym)) */
712 if (gfc_get_symbol (name, NULL, &sym))
713 return FAILURE;
715 /* Start updating the symbol table. Add basic type attribute
716 if present. */
717 if (current_ts.type != BT_UNKNOWN
718 &&(sym->attr.implicit_type == 0
719 || !gfc_compare_types (&sym->ts, &current_ts))
720 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
721 return FAILURE;
723 if (sym->ts.type == BT_CHARACTER)
724 sym->ts.cl = cl;
726 /* Add dimension attribute if present. */
727 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
728 return FAILURE;
729 *as = NULL;
731 /* Add attribute to symbol. The copy is so that we can reset the
732 dimension attribute. */
733 attr = current_attr;
734 attr.dimension = 0;
736 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
737 return FAILURE;
739 return SUCCESS;
742 /* Set character constant to the given length. The constant will be padded or
743 truncated. */
745 void
746 gfc_set_constant_character_len (int len, gfc_expr * expr)
748 char * s;
749 int slen;
751 gcc_assert (expr->expr_type == EXPR_CONSTANT);
752 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
754 slen = expr->value.character.length;
755 if (len != slen)
757 s = gfc_getmem (len + 1);
758 memcpy (s, expr->value.character.string, MIN (len, slen));
759 if (len > slen)
760 memset (&s[slen], ' ', len - slen);
761 s[len] = '\0';
762 gfc_free (expr->value.character.string);
763 expr->value.character.string = s;
764 expr->value.character.length = len;
769 /* Function to create and update the enumerator history
770 using the information passed as arguments.
771 Pointer "max_enum" is also updated, to point to
772 enum history node containing largest initializer.
774 SYM points to the symbol node of enumerator.
775 INIT points to its enumerator value. */
777 static void
778 create_enum_history(gfc_symbol *sym, gfc_expr *init)
780 enumerator_history *new_enum_history;
781 gcc_assert (sym != NULL && init != NULL);
783 new_enum_history = gfc_getmem (sizeof (enumerator_history));
785 new_enum_history->sym = sym;
786 new_enum_history->initializer = init;
787 new_enum_history->next = NULL;
789 if (enum_history == NULL)
791 enum_history = new_enum_history;
792 max_enum = enum_history;
794 else
796 new_enum_history->next = enum_history;
797 enum_history = new_enum_history;
799 if (mpz_cmp (max_enum->initializer->value.integer,
800 new_enum_history->initializer->value.integer) < 0)
801 max_enum = new_enum_history;
806 /* Function to free enum kind history. */
808 void
809 gfc_free_enum_history(void)
811 enumerator_history *current = enum_history;
812 enumerator_history *next;
814 while (current != NULL)
816 next = current->next;
817 gfc_free (current);
818 current = next;
820 max_enum = NULL;
821 enum_history = NULL;
825 /* Function called by variable_decl() that adds an initialization
826 expression to a symbol. */
828 static try
829 add_init_expr_to_sym (const char *name, gfc_expr ** initp,
830 locus * var_locus)
832 symbol_attribute attr;
833 gfc_symbol *sym;
834 gfc_expr *init;
836 init = *initp;
837 if (find_special (name, &sym))
838 return FAILURE;
840 attr = sym->attr;
842 /* If this symbol is confirming an implicit parameter type,
843 then an initialization expression is not allowed. */
844 if (attr.flavor == FL_PARAMETER
845 && sym->value != NULL
846 && *initp != NULL)
848 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
849 sym->name);
850 return FAILURE;
853 if (attr.in_common
854 && !attr.data
855 && *initp != NULL)
857 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
858 sym->name);
859 return FAILURE;
862 if (init == NULL)
864 /* An initializer is required for PARAMETER declarations. */
865 if (attr.flavor == FL_PARAMETER)
867 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
868 return FAILURE;
871 else
873 /* If a variable appears in a DATA block, it cannot have an
874 initializer. */
875 if (sym->attr.data)
877 gfc_error
878 ("Variable '%s' at %C with an initializer already appears "
879 "in a DATA statement", sym->name);
880 return FAILURE;
883 /* Check if the assignment can happen. This has to be put off
884 until later for a derived type variable. */
885 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
886 && gfc_check_assign_symbol (sym, init) == FAILURE)
887 return FAILURE;
889 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
891 /* Update symbol character length according initializer. */
892 if (sym->ts.cl->length == NULL)
894 /* If there are multiple CHARACTER variables declared on
895 the same line, we don't want them to share the same
896 length. */
897 sym->ts.cl = gfc_get_charlen ();
898 sym->ts.cl->next = gfc_current_ns->cl_list;
899 gfc_current_ns->cl_list = sym->ts.cl;
901 if (sym->attr.flavor == FL_PARAMETER
902 && init->expr_type == EXPR_ARRAY)
903 sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
905 /* Update initializer character length according symbol. */
906 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
908 int len = mpz_get_si (sym->ts.cl->length->value.integer);
909 gfc_constructor * p;
911 if (init->expr_type == EXPR_CONSTANT)
912 gfc_set_constant_character_len (len, init);
913 else if (init->expr_type == EXPR_ARRAY)
915 gfc_free_expr (init->ts.cl->length);
916 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
917 for (p = init->value.constructor; p; p = p->next)
918 gfc_set_constant_character_len (len, p->expr);
923 /* Add initializer. Make sure we keep the ranks sane. */
924 if (sym->attr.dimension && init->rank == 0)
925 init->rank = sym->as->rank;
927 sym->value = init;
928 *initp = NULL;
931 /* Maintain enumerator history. */
932 if (gfc_current_state () == COMP_ENUM)
933 create_enum_history (sym, init);
935 return SUCCESS;
939 /* Function called by variable_decl() that adds a name to a structure
940 being built. */
942 static try
943 build_struct (const char *name, gfc_charlen * cl, gfc_expr ** init,
944 gfc_array_spec ** as)
946 gfc_component *c;
948 /* If the current symbol is of the same derived type that we're
949 constructing, it must have the pointer attribute. */
950 if (current_ts.type == BT_DERIVED
951 && current_ts.derived == gfc_current_block ()
952 && current_attr.pointer == 0)
954 gfc_error ("Component at %C must have the POINTER attribute");
955 return FAILURE;
958 if (gfc_current_block ()->attr.pointer
959 && (*as)->rank != 0)
961 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
963 gfc_error ("Array component of structure at %C must have explicit "
964 "or deferred shape");
965 return FAILURE;
969 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
970 return FAILURE;
972 c->ts = current_ts;
973 c->ts.cl = cl;
974 gfc_set_component_attr (c, &current_attr);
976 c->initializer = *init;
977 *init = NULL;
979 c->as = *as;
980 if (c->as != NULL)
981 c->dimension = 1;
982 *as = NULL;
984 /* Check array components. */
985 if (!c->dimension)
987 if (c->allocatable)
989 gfc_error ("Allocatable component at %C must be an array");
990 return FAILURE;
992 else
993 return SUCCESS;
996 if (c->pointer)
998 if (c->as->type != AS_DEFERRED)
1000 gfc_error ("Pointer array component of structure at %C must have a "
1001 "deferred shape");
1002 return FAILURE;
1005 else if (c->allocatable)
1007 if (c->as->type != AS_DEFERRED)
1009 gfc_error ("Allocatable component of structure at %C must have a "
1010 "deferred shape");
1011 return FAILURE;
1014 else
1016 if (c->as->type != AS_EXPLICIT)
1018 gfc_error
1019 ("Array component of structure at %C must have an explicit "
1020 "shape");
1021 return FAILURE;
1025 return SUCCESS;
1029 /* Match a 'NULL()', and possibly take care of some side effects. */
1031 match
1032 gfc_match_null (gfc_expr ** result)
1034 gfc_symbol *sym;
1035 gfc_expr *e;
1036 match m;
1038 m = gfc_match (" null ( )");
1039 if (m != MATCH_YES)
1040 return m;
1042 /* The NULL symbol now has to be/become an intrinsic function. */
1043 if (gfc_get_symbol ("null", NULL, &sym))
1045 gfc_error ("NULL() initialization at %C is ambiguous");
1046 return MATCH_ERROR;
1049 gfc_intrinsic_symbol (sym);
1051 if (sym->attr.proc != PROC_INTRINSIC
1052 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1053 sym->name, NULL) == FAILURE
1054 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1055 return MATCH_ERROR;
1057 e = gfc_get_expr ();
1058 e->where = gfc_current_locus;
1059 e->expr_type = EXPR_NULL;
1060 e->ts.type = BT_UNKNOWN;
1062 *result = e;
1064 return MATCH_YES;
1068 /* Match a variable name with an optional initializer. When this
1069 subroutine is called, a variable is expected to be parsed next.
1070 Depending on what is happening at the moment, updates either the
1071 symbol table or the current interface. */
1073 static match
1074 variable_decl (int elem)
1076 char name[GFC_MAX_SYMBOL_LEN + 1];
1077 gfc_expr *initializer, *char_len;
1078 gfc_array_spec *as;
1079 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1080 gfc_charlen *cl;
1081 locus var_locus;
1082 match m;
1083 try t;
1084 gfc_symbol *sym;
1085 locus old_locus;
1087 initializer = NULL;
1088 as = NULL;
1089 cp_as = NULL;
1090 old_locus = gfc_current_locus;
1092 /* When we get here, we've just matched a list of attributes and
1093 maybe a type and a double colon. The next thing we expect to see
1094 is the name of the symbol. */
1095 m = gfc_match_name (name);
1096 if (m != MATCH_YES)
1097 goto cleanup;
1099 var_locus = gfc_current_locus;
1101 /* Now we could see the optional array spec. or character length. */
1102 m = gfc_match_array_spec (&as);
1103 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1104 cp_as = gfc_copy_array_spec (as);
1105 else if (m == MATCH_ERROR)
1106 goto cleanup;
1108 if (m == MATCH_NO)
1109 as = gfc_copy_array_spec (current_as);
1110 else if (gfc_current_state () == COMP_ENUM)
1112 gfc_error ("Enumerator cannot be array at %C");
1113 gfc_free_enum_history ();
1114 m = MATCH_ERROR;
1115 goto cleanup;
1119 char_len = NULL;
1120 cl = NULL;
1122 if (current_ts.type == BT_CHARACTER)
1124 switch (match_char_length (&char_len))
1126 case MATCH_YES:
1127 cl = gfc_get_charlen ();
1128 cl->next = gfc_current_ns->cl_list;
1129 gfc_current_ns->cl_list = cl;
1131 cl->length = char_len;
1132 break;
1134 /* Non-constant lengths need to be copied after the first
1135 element. */
1136 case MATCH_NO:
1137 if (elem > 1 && current_ts.cl->length
1138 && current_ts.cl->length->expr_type != EXPR_CONSTANT)
1140 cl = gfc_get_charlen ();
1141 cl->next = gfc_current_ns->cl_list;
1142 gfc_current_ns->cl_list = cl;
1143 cl->length = gfc_copy_expr (current_ts.cl->length);
1145 else
1146 cl = current_ts.cl;
1148 break;
1150 case MATCH_ERROR:
1151 goto cleanup;
1155 /* If this symbol has already shown up in a Cray Pointer declaration,
1156 then we want to set the type & bail out. */
1157 if (gfc_option.flag_cray_pointer)
1159 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1160 if (sym != NULL && sym->attr.cray_pointee)
1162 sym->ts.type = current_ts.type;
1163 sym->ts.kind = current_ts.kind;
1164 sym->ts.cl = cl;
1165 sym->ts.derived = current_ts.derived;
1166 m = MATCH_YES;
1168 /* Check to see if we have an array specification. */
1169 if (cp_as != NULL)
1171 if (sym->as != NULL)
1173 gfc_error ("Duplicate array spec for Cray pointee at %C");
1174 gfc_free_array_spec (cp_as);
1175 m = MATCH_ERROR;
1176 goto cleanup;
1178 else
1180 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1181 gfc_internal_error ("Couldn't set pointee array spec.");
1183 /* Fix the array spec. */
1184 m = gfc_mod_pointee_as (sym->as);
1185 if (m == MATCH_ERROR)
1186 goto cleanup;
1189 goto cleanup;
1191 else
1193 gfc_free_array_spec (cp_as);
1198 /* OK, we've successfully matched the declaration. Now put the
1199 symbol in the current namespace, because it might be used in the
1200 optional initialization expression for this symbol, e.g. this is
1201 perfectly legal:
1203 integer, parameter :: i = huge(i)
1205 This is only true for parameters or variables of a basic type.
1206 For components of derived types, it is not true, so we don't
1207 create a symbol for those yet. If we fail to create the symbol,
1208 bail out. */
1209 if (gfc_current_state () != COMP_DERIVED
1210 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1212 m = MATCH_ERROR;
1213 goto cleanup;
1216 /* An interface body specifies all of the procedure's characteristics and these
1217 shall be consistent with those specified in the procedure definition, except
1218 that the interface may specify a procedure that is not pure if the procedure
1219 is defined to be pure(12.3.2). */
1220 if (current_ts.type == BT_DERIVED
1221 && gfc_current_ns->proc_name
1222 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1223 && current_ts.derived->ns != gfc_current_ns
1224 && !gfc_current_ns->has_import_set)
1226 gfc_error ("the type of '%s' at %C has not been declared within the "
1227 "interface", name);
1228 m = MATCH_ERROR;
1229 goto cleanup;
1232 /* In functions that have a RESULT variable defined, the function
1233 name always refers to function calls. Therefore, the name is
1234 not allowed to appear in specification statements. */
1235 if (gfc_current_state () == COMP_FUNCTION
1236 && gfc_current_block () != NULL
1237 && gfc_current_block ()->result != NULL
1238 && gfc_current_block ()->result != gfc_current_block ()
1239 && strcmp (gfc_current_block ()->name, name) == 0)
1241 gfc_error ("Function name '%s' not allowed at %C", name);
1242 m = MATCH_ERROR;
1243 goto cleanup;
1246 /* We allow old-style initializations of the form
1247 integer i /2/, j(4) /3*3, 1/
1248 (if no colon has been seen). These are different from data
1249 statements in that initializers are only allowed to apply to the
1250 variable immediately preceding, i.e.
1251 integer i, j /1, 2/
1252 is not allowed. Therefore we have to do some work manually, that
1253 could otherwise be left to the matchers for DATA statements. */
1255 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1257 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1258 "initialization at %C") == FAILURE)
1259 return MATCH_ERROR;
1261 return match_old_style_init (name);
1264 /* The double colon must be present in order to have initializers.
1265 Otherwise the statement is ambiguous with an assignment statement. */
1266 if (colon_seen)
1268 if (gfc_match (" =>") == MATCH_YES)
1271 if (!current_attr.pointer)
1273 gfc_error ("Initialization at %C isn't for a pointer variable");
1274 m = MATCH_ERROR;
1275 goto cleanup;
1278 m = gfc_match_null (&initializer);
1279 if (m == MATCH_NO)
1281 gfc_error ("Pointer initialization requires a NULL() at %C");
1282 m = MATCH_ERROR;
1285 if (gfc_pure (NULL))
1287 gfc_error
1288 ("Initialization of pointer at %C is not allowed in a "
1289 "PURE procedure");
1290 m = MATCH_ERROR;
1293 if (m != MATCH_YES)
1294 goto cleanup;
1297 else if (gfc_match_char ('=') == MATCH_YES)
1299 if (current_attr.pointer)
1301 gfc_error
1302 ("Pointer initialization at %C requires '=>', not '='");
1303 m = MATCH_ERROR;
1304 goto cleanup;
1307 m = gfc_match_init_expr (&initializer);
1308 if (m == MATCH_NO)
1310 gfc_error ("Expected an initialization expression at %C");
1311 m = MATCH_ERROR;
1314 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1316 gfc_error
1317 ("Initialization of variable at %C is not allowed in a "
1318 "PURE procedure");
1319 m = MATCH_ERROR;
1322 if (m != MATCH_YES)
1323 goto cleanup;
1327 if (initializer != NULL && current_attr.allocatable
1328 && gfc_current_state () == COMP_DERIVED)
1330 gfc_error ("Initialization of allocatable component at %C is not allowed");
1331 m = MATCH_ERROR;
1332 goto cleanup;
1335 /* Check if we are parsing an enumeration and if the current enumerator
1336 variable has an initializer or not. If it does not have an
1337 initializer, the initialization value of the previous enumerator
1338 (stored in last_initializer) is incremented by 1 and is used to
1339 initialize the current enumerator. */
1340 if (gfc_current_state () == COMP_ENUM)
1342 if (initializer == NULL)
1343 initializer = gfc_enum_initializer (last_initializer, old_locus);
1345 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
1347 gfc_error("ENUMERATOR %L not initialized with integer expression",
1348 &var_locus);
1349 m = MATCH_ERROR;
1350 gfc_free_enum_history ();
1351 goto cleanup;
1354 /* Store this current initializer, for the next enumerator
1355 variable to be parsed. */
1356 last_initializer = initializer;
1359 /* Add the initializer. Note that it is fine if initializer is
1360 NULL here, because we sometimes also need to check if a
1361 declaration *must* have an initialization expression. */
1362 if (gfc_current_state () != COMP_DERIVED)
1363 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1364 else
1366 if (current_ts.type == BT_DERIVED
1367 && !current_attr.pointer
1368 && !initializer)
1369 initializer = gfc_default_initializer (&current_ts);
1370 t = build_struct (name, cl, &initializer, &as);
1373 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1375 cleanup:
1376 /* Free stuff up and return. */
1377 gfc_free_expr (initializer);
1378 gfc_free_array_spec (as);
1380 return m;
1384 /* Match an extended-f77 kind specification. */
1386 match
1387 gfc_match_old_kind_spec (gfc_typespec * ts)
1389 match m;
1390 int original_kind;
1392 if (gfc_match_char ('*') != MATCH_YES)
1393 return MATCH_NO;
1395 m = gfc_match_small_literal_int (&ts->kind, NULL);
1396 if (m != MATCH_YES)
1397 return MATCH_ERROR;
1399 original_kind = ts->kind;
1401 /* Massage the kind numbers for complex types. */
1402 if (ts->type == BT_COMPLEX)
1404 if (ts->kind % 2)
1406 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1407 gfc_basic_typename (ts->type), original_kind);
1408 return MATCH_ERROR;
1410 ts->kind /= 2;
1413 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1415 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1416 gfc_basic_typename (ts->type), original_kind);
1417 return MATCH_ERROR;
1420 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1421 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1422 return MATCH_ERROR;
1424 return MATCH_YES;
1428 /* Match a kind specification. Since kinds are generally optional, we
1429 usually return MATCH_NO if something goes wrong. If a "kind="
1430 string is found, then we know we have an error. */
1432 match
1433 gfc_match_kind_spec (gfc_typespec * ts)
1435 locus where;
1436 gfc_expr *e;
1437 match m, n;
1438 const char *msg;
1440 m = MATCH_NO;
1441 e = NULL;
1443 where = gfc_current_locus;
1445 if (gfc_match_char ('(') == MATCH_NO)
1446 return MATCH_NO;
1448 /* Also gobbles optional text. */
1449 if (gfc_match (" kind = ") == MATCH_YES)
1450 m = MATCH_ERROR;
1452 n = gfc_match_init_expr (&e);
1453 if (n == MATCH_NO)
1454 gfc_error ("Expected initialization expression at %C");
1455 if (n != MATCH_YES)
1456 return MATCH_ERROR;
1458 if (e->rank != 0)
1460 gfc_error ("Expected scalar initialization expression at %C");
1461 m = MATCH_ERROR;
1462 goto no_match;
1465 msg = gfc_extract_int (e, &ts->kind);
1466 if (msg != NULL)
1468 gfc_error (msg);
1469 m = MATCH_ERROR;
1470 goto no_match;
1473 gfc_free_expr (e);
1474 e = NULL;
1476 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1478 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1479 gfc_basic_typename (ts->type));
1481 m = MATCH_ERROR;
1482 goto no_match;
1485 if (gfc_match_char (')') != MATCH_YES)
1487 gfc_error ("Missing right parenthesis at %C");
1488 goto no_match;
1491 return MATCH_YES;
1493 no_match:
1494 gfc_free_expr (e);
1495 gfc_current_locus = where;
1496 return m;
1500 /* Match the various kind/length specifications in a CHARACTER
1501 declaration. We don't return MATCH_NO. */
1503 static match
1504 match_char_spec (gfc_typespec * ts)
1506 int i, kind, seen_length;
1507 gfc_charlen *cl;
1508 gfc_expr *len;
1509 match m;
1511 kind = gfc_default_character_kind;
1512 len = NULL;
1513 seen_length = 0;
1515 /* Try the old-style specification first. */
1516 old_char_selector = 0;
1518 m = match_char_length (&len);
1519 if (m != MATCH_NO)
1521 if (m == MATCH_YES)
1522 old_char_selector = 1;
1523 seen_length = 1;
1524 goto done;
1527 m = gfc_match_char ('(');
1528 if (m != MATCH_YES)
1530 m = MATCH_YES; /* character without length is a single char */
1531 goto done;
1534 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ) */
1535 if (gfc_match (" kind =") == MATCH_YES)
1537 m = gfc_match_small_int (&kind);
1538 if (m == MATCH_ERROR)
1539 goto done;
1540 if (m == MATCH_NO)
1541 goto syntax;
1543 if (gfc_match (" , len =") == MATCH_NO)
1544 goto rparen;
1546 m = char_len_param_value (&len);
1547 if (m == MATCH_NO)
1548 goto syntax;
1549 if (m == MATCH_ERROR)
1550 goto done;
1551 seen_length = 1;
1553 goto rparen;
1556 /* Try to match ( LEN = <len-param> ) or ( LEN = <len-param>, KIND = <int> ) */
1557 if (gfc_match (" len =") == MATCH_YES)
1559 m = char_len_param_value (&len);
1560 if (m == MATCH_NO)
1561 goto syntax;
1562 if (m == MATCH_ERROR)
1563 goto done;
1564 seen_length = 1;
1566 if (gfc_match_char (')') == MATCH_YES)
1567 goto done;
1569 if (gfc_match (" , kind =") != MATCH_YES)
1570 goto syntax;
1572 gfc_match_small_int (&kind);
1574 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1576 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1577 return MATCH_YES;
1580 goto rparen;
1583 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ) */
1584 m = char_len_param_value (&len);
1585 if (m == MATCH_NO)
1586 goto syntax;
1587 if (m == MATCH_ERROR)
1588 goto done;
1589 seen_length = 1;
1591 m = gfc_match_char (')');
1592 if (m == MATCH_YES)
1593 goto done;
1595 if (gfc_match_char (',') != MATCH_YES)
1596 goto syntax;
1598 gfc_match (" kind ="); /* Gobble optional text */
1600 m = gfc_match_small_int (&kind);
1601 if (m == MATCH_ERROR)
1602 goto done;
1603 if (m == MATCH_NO)
1604 goto syntax;
1606 rparen:
1607 /* Require a right-paren at this point. */
1608 m = gfc_match_char (')');
1609 if (m == MATCH_YES)
1610 goto done;
1612 syntax:
1613 gfc_error ("Syntax error in CHARACTER declaration at %C");
1614 m = MATCH_ERROR;
1616 done:
1617 if (m == MATCH_YES && gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1619 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1620 m = MATCH_ERROR;
1623 if (m != MATCH_YES)
1625 gfc_free_expr (len);
1626 return m;
1629 /* Do some final massaging of the length values. */
1630 cl = gfc_get_charlen ();
1631 cl->next = gfc_current_ns->cl_list;
1632 gfc_current_ns->cl_list = cl;
1634 if (seen_length == 0)
1635 cl->length = gfc_int_expr (1);
1636 else
1638 if (len == NULL || gfc_extract_int (len, &i) != NULL || i >= 0)
1639 cl->length = len;
1640 else
1642 gfc_free_expr (len);
1643 cl->length = gfc_int_expr (0);
1647 ts->cl = cl;
1648 ts->kind = kind;
1650 return MATCH_YES;
1654 /* Matches a type specification. If successful, sets the ts structure
1655 to the matched specification. This is necessary for FUNCTION and
1656 IMPLICIT statements.
1658 If implicit_flag is nonzero, then we don't check for the optional
1659 kind specification. Not doing so is needed for matching an IMPLICIT
1660 statement correctly. */
1662 static match
1663 match_type_spec (gfc_typespec * ts, int implicit_flag)
1665 char name[GFC_MAX_SYMBOL_LEN + 1];
1666 gfc_symbol *sym;
1667 match m;
1668 int c;
1670 gfc_clear_ts (ts);
1672 if (gfc_match (" byte") == MATCH_YES)
1674 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
1675 == FAILURE)
1676 return MATCH_ERROR;
1678 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
1680 gfc_error ("BYTE type used at %C "
1681 "is not available on the target machine");
1682 return MATCH_ERROR;
1685 ts->type = BT_INTEGER;
1686 ts->kind = 1;
1687 return MATCH_YES;
1690 if (gfc_match (" integer") == MATCH_YES)
1692 ts->type = BT_INTEGER;
1693 ts->kind = gfc_default_integer_kind;
1694 goto get_kind;
1697 if (gfc_match (" character") == MATCH_YES)
1699 ts->type = BT_CHARACTER;
1700 if (implicit_flag == 0)
1701 return match_char_spec (ts);
1702 else
1703 return MATCH_YES;
1706 if (gfc_match (" real") == MATCH_YES)
1708 ts->type = BT_REAL;
1709 ts->kind = gfc_default_real_kind;
1710 goto get_kind;
1713 if (gfc_match (" double precision") == MATCH_YES)
1715 ts->type = BT_REAL;
1716 ts->kind = gfc_default_double_kind;
1717 return MATCH_YES;
1720 if (gfc_match (" complex") == MATCH_YES)
1722 ts->type = BT_COMPLEX;
1723 ts->kind = gfc_default_complex_kind;
1724 goto get_kind;
1727 if (gfc_match (" double complex") == MATCH_YES)
1729 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
1730 "conform to the Fortran 95 standard") == FAILURE)
1731 return MATCH_ERROR;
1733 ts->type = BT_COMPLEX;
1734 ts->kind = gfc_default_double_kind;
1735 return MATCH_YES;
1738 if (gfc_match (" logical") == MATCH_YES)
1740 ts->type = BT_LOGICAL;
1741 ts->kind = gfc_default_logical_kind;
1742 goto get_kind;
1745 m = gfc_match (" type ( %n )", name);
1746 if (m != MATCH_YES)
1747 return m;
1749 /* Search for the name but allow the components to be defined later. */
1750 if (gfc_get_ha_symbol (name, &sym))
1752 gfc_error ("Type name '%s' at %C is ambiguous", name);
1753 return MATCH_ERROR;
1756 if (sym->attr.flavor != FL_DERIVED
1757 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
1758 return MATCH_ERROR;
1760 ts->type = BT_DERIVED;
1761 ts->kind = 0;
1762 ts->derived = sym;
1764 return MATCH_YES;
1766 get_kind:
1767 /* For all types except double, derived and character, look for an
1768 optional kind specifier. MATCH_NO is actually OK at this point. */
1769 if (implicit_flag == 1)
1770 return MATCH_YES;
1772 if (gfc_current_form == FORM_FREE)
1774 c = gfc_peek_char();
1775 if (!gfc_is_whitespace(c) && c != '*' && c != '('
1776 && c != ':' && c != ',')
1777 return MATCH_NO;
1780 m = gfc_match_kind_spec (ts);
1781 if (m == MATCH_NO && ts->type != BT_CHARACTER)
1782 m = gfc_match_old_kind_spec (ts);
1784 if (m == MATCH_NO)
1785 m = MATCH_YES; /* No kind specifier found. */
1787 return m;
1791 /* Match an IMPLICIT NONE statement. Actually, this statement is
1792 already matched in parse.c, or we would not end up here in the
1793 first place. So the only thing we need to check, is if there is
1794 trailing garbage. If not, the match is successful. */
1796 match
1797 gfc_match_implicit_none (void)
1800 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
1804 /* Match the letter range(s) of an IMPLICIT statement. */
1806 static match
1807 match_implicit_range (void)
1809 int c, c1, c2, inner;
1810 locus cur_loc;
1812 cur_loc = gfc_current_locus;
1814 gfc_gobble_whitespace ();
1815 c = gfc_next_char ();
1816 if (c != '(')
1818 gfc_error ("Missing character range in IMPLICIT at %C");
1819 goto bad;
1822 inner = 1;
1823 while (inner)
1825 gfc_gobble_whitespace ();
1826 c1 = gfc_next_char ();
1827 if (!ISALPHA (c1))
1828 goto bad;
1830 gfc_gobble_whitespace ();
1831 c = gfc_next_char ();
1833 switch (c)
1835 case ')':
1836 inner = 0; /* Fall through */
1838 case ',':
1839 c2 = c1;
1840 break;
1842 case '-':
1843 gfc_gobble_whitespace ();
1844 c2 = gfc_next_char ();
1845 if (!ISALPHA (c2))
1846 goto bad;
1848 gfc_gobble_whitespace ();
1849 c = gfc_next_char ();
1851 if ((c != ',') && (c != ')'))
1852 goto bad;
1853 if (c == ')')
1854 inner = 0;
1856 break;
1858 default:
1859 goto bad;
1862 if (c1 > c2)
1864 gfc_error ("Letters must be in alphabetic order in "
1865 "IMPLICIT statement at %C");
1866 goto bad;
1869 /* See if we can add the newly matched range to the pending
1870 implicits from this IMPLICIT statement. We do not check for
1871 conflicts with whatever earlier IMPLICIT statements may have
1872 set. This is done when we've successfully finished matching
1873 the current one. */
1874 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
1875 goto bad;
1878 return MATCH_YES;
1880 bad:
1881 gfc_syntax_error (ST_IMPLICIT);
1883 gfc_current_locus = cur_loc;
1884 return MATCH_ERROR;
1888 /* Match an IMPLICIT statement, storing the types for
1889 gfc_set_implicit() if the statement is accepted by the parser.
1890 There is a strange looking, but legal syntactic construction
1891 possible. It looks like:
1893 IMPLICIT INTEGER (a-b) (c-d)
1895 This is legal if "a-b" is a constant expression that happens to
1896 equal one of the legal kinds for integers. The real problem
1897 happens with an implicit specification that looks like:
1899 IMPLICIT INTEGER (a-b)
1901 In this case, a typespec matcher that is "greedy" (as most of the
1902 matchers are) gobbles the character range as a kindspec, leaving
1903 nothing left. We therefore have to go a bit more slowly in the
1904 matching process by inhibiting the kindspec checking during
1905 typespec matching and checking for a kind later. */
1907 match
1908 gfc_match_implicit (void)
1910 gfc_typespec ts;
1911 locus cur_loc;
1912 int c;
1913 match m;
1915 /* We don't allow empty implicit statements. */
1916 if (gfc_match_eos () == MATCH_YES)
1918 gfc_error ("Empty IMPLICIT statement at %C");
1919 return MATCH_ERROR;
1924 /* First cleanup. */
1925 gfc_clear_new_implicit ();
1927 /* A basic type is mandatory here. */
1928 m = match_type_spec (&ts, 1);
1929 if (m == MATCH_ERROR)
1930 goto error;
1931 if (m == MATCH_NO)
1932 goto syntax;
1934 cur_loc = gfc_current_locus;
1935 m = match_implicit_range ();
1937 if (m == MATCH_YES)
1939 /* We may have <TYPE> (<RANGE>). */
1940 gfc_gobble_whitespace ();
1941 c = gfc_next_char ();
1942 if ((c == '\n') || (c == ','))
1944 /* Check for CHARACTER with no length parameter. */
1945 if (ts.type == BT_CHARACTER && !ts.cl)
1947 ts.kind = gfc_default_character_kind;
1948 ts.cl = gfc_get_charlen ();
1949 ts.cl->next = gfc_current_ns->cl_list;
1950 gfc_current_ns->cl_list = ts.cl;
1951 ts.cl->length = gfc_int_expr (1);
1954 /* Record the Successful match. */
1955 if (gfc_merge_new_implicit (&ts) != SUCCESS)
1956 return MATCH_ERROR;
1957 continue;
1960 gfc_current_locus = cur_loc;
1963 /* Discard the (incorrectly) matched range. */
1964 gfc_clear_new_implicit ();
1966 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
1967 if (ts.type == BT_CHARACTER)
1968 m = match_char_spec (&ts);
1969 else
1971 m = gfc_match_kind_spec (&ts);
1972 if (m == MATCH_NO)
1974 m = gfc_match_old_kind_spec (&ts);
1975 if (m == MATCH_ERROR)
1976 goto error;
1977 if (m == MATCH_NO)
1978 goto syntax;
1981 if (m == MATCH_ERROR)
1982 goto error;
1984 m = match_implicit_range ();
1985 if (m == MATCH_ERROR)
1986 goto error;
1987 if (m == MATCH_NO)
1988 goto syntax;
1990 gfc_gobble_whitespace ();
1991 c = gfc_next_char ();
1992 if ((c != '\n') && (c != ','))
1993 goto syntax;
1995 if (gfc_merge_new_implicit (&ts) != SUCCESS)
1996 return MATCH_ERROR;
1998 while (c == ',');
2000 return MATCH_YES;
2002 syntax:
2003 gfc_syntax_error (ST_IMPLICIT);
2005 error:
2006 return MATCH_ERROR;
2009 match
2010 gfc_match_import (void)
2012 char name[GFC_MAX_SYMBOL_LEN + 1];
2013 match m;
2014 gfc_symbol *sym;
2015 gfc_symtree *st;
2017 if (gfc_current_ns->proc_name == NULL ||
2018 gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2020 gfc_error ("IMPORT statement at %C only permitted in "
2021 "an INTERFACE body");
2022 return MATCH_ERROR;
2025 if (gfc_notify_std (GFC_STD_F2003,
2026 "Fortran 2003: IMPORT statement at %C")
2027 == FAILURE)
2028 return MATCH_ERROR;
2030 if (gfc_match_eos () == MATCH_YES)
2032 /* All host variables should be imported. */
2033 gfc_current_ns->has_import_set = 1;
2034 return MATCH_YES;
2037 if (gfc_match (" ::") == MATCH_YES)
2039 if (gfc_match_eos () == MATCH_YES)
2041 gfc_error ("Expecting list of named entities at %C");
2042 return MATCH_ERROR;
2046 for(;;)
2048 m = gfc_match (" %n", name);
2049 switch (m)
2051 case MATCH_YES:
2052 if (gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2054 gfc_error ("Type name '%s' at %C is ambiguous", name);
2055 return MATCH_ERROR;
2058 if (sym == NULL)
2060 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2061 "at %C - does not exist.", name);
2062 return MATCH_ERROR;
2065 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2067 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2068 "at %C.", name);
2069 goto next_item;
2072 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2073 st->n.sym = sym;
2074 sym->refs++;
2075 sym->ns = gfc_current_ns;
2077 goto next_item;
2079 case MATCH_NO:
2080 break;
2082 case MATCH_ERROR:
2083 return MATCH_ERROR;
2086 next_item:
2087 if (gfc_match_eos () == MATCH_YES)
2088 break;
2089 if (gfc_match_char (',') != MATCH_YES)
2090 goto syntax;
2093 return MATCH_YES;
2095 syntax:
2096 gfc_error ("Syntax error in IMPORT statement at %C");
2097 return MATCH_ERROR;
2100 /* Matches an attribute specification including array specs. If
2101 successful, leaves the variables current_attr and current_as
2102 holding the specification. Also sets the colon_seen variable for
2103 later use by matchers associated with initializations.
2105 This subroutine is a little tricky in the sense that we don't know
2106 if we really have an attr-spec until we hit the double colon.
2107 Until that time, we can only return MATCH_NO. This forces us to
2108 check for duplicate specification at this level. */
2110 static match
2111 match_attr_spec (void)
2114 /* Modifiers that can exist in a type statement. */
2115 typedef enum
2116 { GFC_DECL_BEGIN = 0,
2117 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2118 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2119 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2120 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2121 DECL_COLON, DECL_NONE,
2122 GFC_DECL_END /* Sentinel */
2124 decl_types;
2126 /* GFC_DECL_END is the sentinel, index starts at 0. */
2127 #define NUM_DECL GFC_DECL_END
2129 static mstring decls[] = {
2130 minit (", allocatable", DECL_ALLOCATABLE),
2131 minit (", dimension", DECL_DIMENSION),
2132 minit (", external", DECL_EXTERNAL),
2133 minit (", intent ( in )", DECL_IN),
2134 minit (", intent ( out )", DECL_OUT),
2135 minit (", intent ( in out )", DECL_INOUT),
2136 minit (", intrinsic", DECL_INTRINSIC),
2137 minit (", optional", DECL_OPTIONAL),
2138 minit (", parameter", DECL_PARAMETER),
2139 minit (", pointer", DECL_POINTER),
2140 minit (", protected", DECL_PROTECTED),
2141 minit (", private", DECL_PRIVATE),
2142 minit (", public", DECL_PUBLIC),
2143 minit (", save", DECL_SAVE),
2144 minit (", target", DECL_TARGET),
2145 minit (", value", DECL_VALUE),
2146 minit (", volatile", DECL_VOLATILE),
2147 minit ("::", DECL_COLON),
2148 minit (NULL, DECL_NONE)
2151 locus start, seen_at[NUM_DECL];
2152 int seen[NUM_DECL];
2153 decl_types d;
2154 const char *attr;
2155 match m;
2156 try t;
2158 gfc_clear_attr (&current_attr);
2159 start = gfc_current_locus;
2161 current_as = NULL;
2162 colon_seen = 0;
2164 /* See if we get all of the keywords up to the final double colon. */
2165 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2166 seen[d] = 0;
2168 for (;;)
2170 d = (decl_types) gfc_match_strings (decls);
2171 if (d == DECL_NONE || d == DECL_COLON)
2172 break;
2174 if (gfc_current_state () == COMP_ENUM)
2176 gfc_error ("Enumerator cannot have attributes %C");
2177 return MATCH_ERROR;
2180 seen[d]++;
2181 seen_at[d] = gfc_current_locus;
2183 if (d == DECL_DIMENSION)
2185 m = gfc_match_array_spec (&current_as);
2187 if (m == MATCH_NO)
2189 gfc_error ("Missing dimension specification at %C");
2190 m = MATCH_ERROR;
2193 if (m == MATCH_ERROR)
2194 goto cleanup;
2198 /* If we are parsing an enumeration and have ensured that no other
2199 attributes are present we can now set the parameter attribute. */
2200 if (gfc_current_state () == COMP_ENUM)
2202 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
2203 if (t == FAILURE)
2205 m = MATCH_ERROR;
2206 goto cleanup;
2210 /* No double colon, so assume that we've been looking at something
2211 else the whole time. */
2212 if (d == DECL_NONE)
2214 m = MATCH_NO;
2215 goto cleanup;
2218 /* Since we've seen a double colon, we have to be looking at an
2219 attr-spec. This means that we can now issue errors. */
2220 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2221 if (seen[d] > 1)
2223 switch (d)
2225 case DECL_ALLOCATABLE:
2226 attr = "ALLOCATABLE";
2227 break;
2228 case DECL_DIMENSION:
2229 attr = "DIMENSION";
2230 break;
2231 case DECL_EXTERNAL:
2232 attr = "EXTERNAL";
2233 break;
2234 case DECL_IN:
2235 attr = "INTENT (IN)";
2236 break;
2237 case DECL_OUT:
2238 attr = "INTENT (OUT)";
2239 break;
2240 case DECL_INOUT:
2241 attr = "INTENT (IN OUT)";
2242 break;
2243 case DECL_INTRINSIC:
2244 attr = "INTRINSIC";
2245 break;
2246 case DECL_OPTIONAL:
2247 attr = "OPTIONAL";
2248 break;
2249 case DECL_PARAMETER:
2250 attr = "PARAMETER";
2251 break;
2252 case DECL_POINTER:
2253 attr = "POINTER";
2254 break;
2255 case DECL_PROTECTED:
2256 attr = "PROTECTED";
2257 break;
2258 case DECL_PRIVATE:
2259 attr = "PRIVATE";
2260 break;
2261 case DECL_PUBLIC:
2262 attr = "PUBLIC";
2263 break;
2264 case DECL_SAVE:
2265 attr = "SAVE";
2266 break;
2267 case DECL_TARGET:
2268 attr = "TARGET";
2269 break;
2270 case DECL_VALUE:
2271 attr = "VALUE";
2272 break;
2273 case DECL_VOLATILE:
2274 attr = "VOLATILE";
2275 break;
2276 default:
2277 attr = NULL; /* This shouldn't happen */
2280 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2281 m = MATCH_ERROR;
2282 goto cleanup;
2285 /* Now that we've dealt with duplicate attributes, add the attributes
2286 to the current attribute. */
2287 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2289 if (seen[d] == 0)
2290 continue;
2292 if (gfc_current_state () == COMP_DERIVED
2293 && d != DECL_DIMENSION && d != DECL_POINTER
2294 && d != DECL_COLON && d != DECL_NONE)
2296 if (d == DECL_ALLOCATABLE)
2298 if (gfc_notify_std (GFC_STD_F2003,
2299 "Fortran 2003: ALLOCATABLE "
2300 "attribute at %C in a TYPE "
2301 "definition") == FAILURE)
2303 m = MATCH_ERROR;
2304 goto cleanup;
2307 else
2309 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2310 &seen_at[d]);
2311 m = MATCH_ERROR;
2312 goto cleanup;
2316 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2317 && gfc_current_state () != COMP_MODULE)
2319 if (d == DECL_PRIVATE)
2320 attr = "PRIVATE";
2321 else
2322 attr = "PUBLIC";
2324 gfc_error ("%s attribute at %L is not allowed outside of a MODULE",
2325 attr, &seen_at[d]);
2326 m = MATCH_ERROR;
2327 goto cleanup;
2330 switch (d)
2332 case DECL_ALLOCATABLE:
2333 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
2334 break;
2336 case DECL_DIMENSION:
2337 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
2338 break;
2340 case DECL_EXTERNAL:
2341 t = gfc_add_external (&current_attr, &seen_at[d]);
2342 break;
2344 case DECL_IN:
2345 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
2346 break;
2348 case DECL_OUT:
2349 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
2350 break;
2352 case DECL_INOUT:
2353 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
2354 break;
2356 case DECL_INTRINSIC:
2357 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
2358 break;
2360 case DECL_OPTIONAL:
2361 t = gfc_add_optional (&current_attr, &seen_at[d]);
2362 break;
2364 case DECL_PARAMETER:
2365 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
2366 break;
2368 case DECL_POINTER:
2369 t = gfc_add_pointer (&current_attr, &seen_at[d]);
2370 break;
2372 case DECL_PROTECTED:
2373 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
2375 gfc_error ("PROTECTED at %C only allowed in specification "
2376 "part of a module");
2377 t = FAILURE;
2378 break;
2381 if (gfc_notify_std (GFC_STD_F2003,
2382 "Fortran 2003: PROTECTED attribute at %C")
2383 == FAILURE)
2384 t = FAILURE;
2385 else
2386 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
2387 break;
2389 case DECL_PRIVATE:
2390 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
2391 &seen_at[d]);
2392 break;
2394 case DECL_PUBLIC:
2395 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
2396 &seen_at[d]);
2397 break;
2399 case DECL_SAVE:
2400 t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
2401 break;
2403 case DECL_TARGET:
2404 t = gfc_add_target (&current_attr, &seen_at[d]);
2405 break;
2407 case DECL_VALUE:
2408 if (gfc_notify_std (GFC_STD_F2003,
2409 "Fortran 2003: VALUE attribute at %C")
2410 == FAILURE)
2411 t = FAILURE;
2412 else
2413 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
2414 break;
2416 case DECL_VOLATILE:
2417 if (gfc_notify_std (GFC_STD_F2003,
2418 "Fortran 2003: VOLATILE attribute at %C")
2419 == FAILURE)
2420 t = FAILURE;
2421 else
2422 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
2423 break;
2425 default:
2426 gfc_internal_error ("match_attr_spec(): Bad attribute");
2429 if (t == FAILURE)
2431 m = MATCH_ERROR;
2432 goto cleanup;
2436 colon_seen = 1;
2437 return MATCH_YES;
2439 cleanup:
2440 gfc_current_locus = start;
2441 gfc_free_array_spec (current_as);
2442 current_as = NULL;
2443 return m;
2447 /* Match a data declaration statement. */
2449 match
2450 gfc_match_data_decl (void)
2452 gfc_symbol *sym;
2453 match m;
2454 int elem;
2456 m = match_type_spec (&current_ts, 0);
2457 if (m != MATCH_YES)
2458 return m;
2460 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
2462 sym = gfc_use_derived (current_ts.derived);
2464 if (sym == NULL)
2466 m = MATCH_ERROR;
2467 goto cleanup;
2470 current_ts.derived = sym;
2473 m = match_attr_spec ();
2474 if (m == MATCH_ERROR)
2476 m = MATCH_NO;
2477 goto cleanup;
2480 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
2483 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
2484 goto ok;
2486 gfc_find_symbol (current_ts.derived->name,
2487 current_ts.derived->ns->parent, 1, &sym);
2489 /* Any symbol that we find had better be a type definition
2490 which has its components defined. */
2491 if (sym != NULL && sym->attr.flavor == FL_DERIVED
2492 && current_ts.derived->components != NULL)
2493 goto ok;
2495 /* Now we have an error, which we signal, and then fix up
2496 because the knock-on is plain and simple confusing. */
2497 gfc_error_now ("Derived type at %C has not been previously defined "
2498 "and so cannot appear in a derived type definition");
2499 current_attr.pointer = 1;
2500 goto ok;
2504 /* If we have an old-style character declaration, and no new-style
2505 attribute specifications, then there a comma is optional between
2506 the type specification and the variable list. */
2507 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
2508 gfc_match_char (',');
2510 /* Give the types/attributes to symbols that follow. Give the element
2511 a number so that repeat character length expressions can be copied. */
2512 elem = 1;
2513 for (;;)
2515 m = variable_decl (elem++);
2516 if (m == MATCH_ERROR)
2517 goto cleanup;
2518 if (m == MATCH_NO)
2519 break;
2521 if (gfc_match_eos () == MATCH_YES)
2522 goto cleanup;
2523 if (gfc_match_char (',') != MATCH_YES)
2524 break;
2527 if (gfc_error_flag_test () == 0)
2528 gfc_error ("Syntax error in data declaration at %C");
2529 m = MATCH_ERROR;
2531 gfc_free_data_all (gfc_current_ns);
2533 cleanup:
2534 gfc_free_array_spec (current_as);
2535 current_as = NULL;
2536 return m;
2540 /* Match a prefix associated with a function or subroutine
2541 declaration. If the typespec pointer is nonnull, then a typespec
2542 can be matched. Note that if nothing matches, MATCH_YES is
2543 returned (the null string was matched). */
2545 static match
2546 match_prefix (gfc_typespec * ts)
2548 int seen_type;
2550 gfc_clear_attr (&current_attr);
2551 seen_type = 0;
2553 loop:
2554 if (!seen_type && ts != NULL
2555 && match_type_spec (ts, 0) == MATCH_YES
2556 && gfc_match_space () == MATCH_YES)
2559 seen_type = 1;
2560 goto loop;
2563 if (gfc_match ("elemental% ") == MATCH_YES)
2565 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
2566 return MATCH_ERROR;
2568 goto loop;
2571 if (gfc_match ("pure% ") == MATCH_YES)
2573 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
2574 return MATCH_ERROR;
2576 goto loop;
2579 if (gfc_match ("recursive% ") == MATCH_YES)
2581 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
2582 return MATCH_ERROR;
2584 goto loop;
2587 /* At this point, the next item is not a prefix. */
2588 return MATCH_YES;
2592 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
2594 static try
2595 copy_prefix (symbol_attribute * dest, locus * where)
2598 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
2599 return FAILURE;
2601 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
2602 return FAILURE;
2604 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
2605 return FAILURE;
2607 return SUCCESS;
2611 /* Match a formal argument list. */
2613 match
2614 gfc_match_formal_arglist (gfc_symbol * progname, int st_flag, int null_flag)
2616 gfc_formal_arglist *head, *tail, *p, *q;
2617 char name[GFC_MAX_SYMBOL_LEN + 1];
2618 gfc_symbol *sym;
2619 match m;
2621 head = tail = NULL;
2623 if (gfc_match_char ('(') != MATCH_YES)
2625 if (null_flag)
2626 goto ok;
2627 return MATCH_NO;
2630 if (gfc_match_char (')') == MATCH_YES)
2631 goto ok;
2633 for (;;)
2635 if (gfc_match_char ('*') == MATCH_YES)
2636 sym = NULL;
2637 else
2639 m = gfc_match_name (name);
2640 if (m != MATCH_YES)
2641 goto cleanup;
2643 if (gfc_get_symbol (name, NULL, &sym))
2644 goto cleanup;
2647 p = gfc_get_formal_arglist ();
2649 if (head == NULL)
2650 head = tail = p;
2651 else
2653 tail->next = p;
2654 tail = p;
2657 tail->sym = sym;
2659 /* We don't add the VARIABLE flavor because the name could be a
2660 dummy procedure. We don't apply these attributes to formal
2661 arguments of statement functions. */
2662 if (sym != NULL && !st_flag
2663 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
2664 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
2666 m = MATCH_ERROR;
2667 goto cleanup;
2670 /* The name of a program unit can be in a different namespace,
2671 so check for it explicitly. After the statement is accepted,
2672 the name is checked for especially in gfc_get_symbol(). */
2673 if (gfc_new_block != NULL && sym != NULL
2674 && strcmp (sym->name, gfc_new_block->name) == 0)
2676 gfc_error ("Name '%s' at %C is the name of the procedure",
2677 sym->name);
2678 m = MATCH_ERROR;
2679 goto cleanup;
2682 if (gfc_match_char (')') == MATCH_YES)
2683 goto ok;
2685 m = gfc_match_char (',');
2686 if (m != MATCH_YES)
2688 gfc_error ("Unexpected junk in formal argument list at %C");
2689 goto cleanup;
2694 /* Check for duplicate symbols in the formal argument list. */
2695 if (head != NULL)
2697 for (p = head; p->next; p = p->next)
2699 if (p->sym == NULL)
2700 continue;
2702 for (q = p->next; q; q = q->next)
2703 if (p->sym == q->sym)
2705 gfc_error
2706 ("Duplicate symbol '%s' in formal argument list at %C",
2707 p->sym->name);
2709 m = MATCH_ERROR;
2710 goto cleanup;
2715 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL) ==
2716 FAILURE)
2718 m = MATCH_ERROR;
2719 goto cleanup;
2722 return MATCH_YES;
2724 cleanup:
2725 gfc_free_formal_arglist (head);
2726 return m;
2730 /* Match a RESULT specification following a function declaration or
2731 ENTRY statement. Also matches the end-of-statement. */
2733 static match
2734 match_result (gfc_symbol * function, gfc_symbol ** result)
2736 char name[GFC_MAX_SYMBOL_LEN + 1];
2737 gfc_symbol *r;
2738 match m;
2740 if (gfc_match (" result (") != MATCH_YES)
2741 return MATCH_NO;
2743 m = gfc_match_name (name);
2744 if (m != MATCH_YES)
2745 return m;
2747 if (gfc_match (" )%t") != MATCH_YES)
2749 gfc_error ("Unexpected junk following RESULT variable at %C");
2750 return MATCH_ERROR;
2753 if (strcmp (function->name, name) == 0)
2755 gfc_error
2756 ("RESULT variable at %C must be different than function name");
2757 return MATCH_ERROR;
2760 if (gfc_get_symbol (name, NULL, &r))
2761 return MATCH_ERROR;
2763 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
2764 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
2765 return MATCH_ERROR;
2767 *result = r;
2769 return MATCH_YES;
2773 /* Match a function declaration. */
2775 match
2776 gfc_match_function_decl (void)
2778 char name[GFC_MAX_SYMBOL_LEN + 1];
2779 gfc_symbol *sym, *result;
2780 locus old_loc;
2781 match m;
2783 if (gfc_current_state () != COMP_NONE
2784 && gfc_current_state () != COMP_INTERFACE
2785 && gfc_current_state () != COMP_CONTAINS)
2786 return MATCH_NO;
2788 gfc_clear_ts (&current_ts);
2790 old_loc = gfc_current_locus;
2792 m = match_prefix (&current_ts);
2793 if (m != MATCH_YES)
2795 gfc_current_locus = old_loc;
2796 return m;
2799 if (gfc_match ("function% %n", name) != MATCH_YES)
2801 gfc_current_locus = old_loc;
2802 return MATCH_NO;
2805 if (get_proc_name (name, &sym, false))
2806 return MATCH_ERROR;
2807 gfc_new_block = sym;
2809 m = gfc_match_formal_arglist (sym, 0, 0);
2810 if (m == MATCH_NO)
2812 gfc_error ("Expected formal argument list in function "
2813 "definition at %C");
2814 m = MATCH_ERROR;
2815 goto cleanup;
2817 else if (m == MATCH_ERROR)
2818 goto cleanup;
2820 result = NULL;
2822 if (gfc_match_eos () != MATCH_YES)
2824 /* See if a result variable is present. */
2825 m = match_result (sym, &result);
2826 if (m == MATCH_NO)
2827 gfc_error ("Unexpected junk after function declaration at %C");
2829 if (m != MATCH_YES)
2831 m = MATCH_ERROR;
2832 goto cleanup;
2836 /* Make changes to the symbol. */
2837 m = MATCH_ERROR;
2839 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2840 goto cleanup;
2842 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
2843 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2844 goto cleanup;
2846 if (current_ts.type != BT_UNKNOWN
2847 && sym->ts.type != BT_UNKNOWN
2848 && !sym->attr.implicit_type)
2850 gfc_error ("Function '%s' at %C already has a type of %s", name,
2851 gfc_basic_typename (sym->ts.type));
2852 goto cleanup;
2855 if (result == NULL)
2857 sym->ts = current_ts;
2858 sym->result = sym;
2860 else
2862 result->ts = current_ts;
2863 sym->result = result;
2866 return MATCH_YES;
2868 cleanup:
2869 gfc_current_locus = old_loc;
2870 return m;
2873 /* This is mostly a copy of parse.c(add_global_procedure) but modified to pass the
2874 name of the entry, rather than the gfc_current_block name, and to return false
2875 upon finding an existing global entry. */
2877 static bool
2878 add_global_entry (const char * name, int sub)
2880 gfc_gsymbol *s;
2882 s = gfc_get_gsymbol(name);
2884 if (s->defined
2885 || (s->type != GSYM_UNKNOWN && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
2886 global_used(s, NULL);
2887 else
2889 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2890 s->where = gfc_current_locus;
2891 s->defined = 1;
2892 return true;
2894 return false;
2897 /* Match an ENTRY statement. */
2899 match
2900 gfc_match_entry (void)
2902 gfc_symbol *proc;
2903 gfc_symbol *result;
2904 gfc_symbol *entry;
2905 char name[GFC_MAX_SYMBOL_LEN + 1];
2906 gfc_compile_state state;
2907 match m;
2908 gfc_entry_list *el;
2909 locus old_loc;
2910 bool module_procedure;
2912 m = gfc_match_name (name);
2913 if (m != MATCH_YES)
2914 return m;
2916 state = gfc_current_state ();
2917 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
2919 switch (state)
2921 case COMP_PROGRAM:
2922 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
2923 break;
2924 case COMP_MODULE:
2925 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
2926 break;
2927 case COMP_BLOCK_DATA:
2928 gfc_error
2929 ("ENTRY statement at %C cannot appear within a BLOCK DATA");
2930 break;
2931 case COMP_INTERFACE:
2932 gfc_error
2933 ("ENTRY statement at %C cannot appear within an INTERFACE");
2934 break;
2935 case COMP_DERIVED:
2936 gfc_error
2937 ("ENTRY statement at %C cannot appear "
2938 "within a DERIVED TYPE block");
2939 break;
2940 case COMP_IF:
2941 gfc_error
2942 ("ENTRY statement at %C cannot appear within an IF-THEN block");
2943 break;
2944 case COMP_DO:
2945 gfc_error
2946 ("ENTRY statement at %C cannot appear within a DO block");
2947 break;
2948 case COMP_SELECT:
2949 gfc_error
2950 ("ENTRY statement at %C cannot appear within a SELECT block");
2951 break;
2952 case COMP_FORALL:
2953 gfc_error
2954 ("ENTRY statement at %C cannot appear within a FORALL block");
2955 break;
2956 case COMP_WHERE:
2957 gfc_error
2958 ("ENTRY statement at %C cannot appear within a WHERE block");
2959 break;
2960 case COMP_CONTAINS:
2961 gfc_error
2962 ("ENTRY statement at %C cannot appear "
2963 "within a contained subprogram");
2964 break;
2965 default:
2966 gfc_internal_error ("gfc_match_entry(): Bad state");
2968 return MATCH_ERROR;
2971 module_procedure = gfc_current_ns->parent != NULL
2972 && gfc_current_ns->parent->proc_name
2973 && gfc_current_ns->parent->proc_name->attr.flavor == FL_MODULE;
2975 if (gfc_current_ns->parent != NULL
2976 && gfc_current_ns->parent->proc_name
2977 && !module_procedure)
2979 gfc_error("ENTRY statement at %C cannot appear in a "
2980 "contained procedure");
2981 return MATCH_ERROR;
2984 /* Module function entries need special care in get_proc_name
2985 because previous references within the function will have
2986 created symbols attached to the current namespace. */
2987 if (get_proc_name (name, &entry,
2988 gfc_current_ns->parent != NULL
2989 && module_procedure
2990 && gfc_current_ns->proc_name->attr.function))
2991 return MATCH_ERROR;
2993 proc = gfc_current_block ();
2995 if (state == COMP_SUBROUTINE)
2997 /* An entry in a subroutine. */
2998 if (!add_global_entry (name, 1))
2999 return MATCH_ERROR;
3001 m = gfc_match_formal_arglist (entry, 0, 1);
3002 if (m != MATCH_YES)
3003 return MATCH_ERROR;
3005 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3006 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
3007 return MATCH_ERROR;
3009 else
3011 /* An entry in a function.
3012 We need to take special care because writing
3013 ENTRY f()
3015 ENTRY f
3016 is allowed, whereas
3017 ENTRY f() RESULT (r)
3018 can't be written as
3019 ENTRY f RESULT (r). */
3020 if (!add_global_entry (name, 0))
3021 return MATCH_ERROR;
3023 old_loc = gfc_current_locus;
3024 if (gfc_match_eos () == MATCH_YES)
3026 gfc_current_locus = old_loc;
3027 /* Match the empty argument list, and add the interface to
3028 the symbol. */
3029 m = gfc_match_formal_arglist (entry, 0, 1);
3031 else
3032 m = gfc_match_formal_arglist (entry, 0, 0);
3034 if (m != MATCH_YES)
3035 return MATCH_ERROR;
3037 result = NULL;
3039 if (gfc_match_eos () == MATCH_YES)
3041 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3042 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
3043 return MATCH_ERROR;
3045 entry->result = entry;
3047 else
3049 m = match_result (proc, &result);
3050 if (m == MATCH_NO)
3051 gfc_syntax_error (ST_ENTRY);
3052 if (m != MATCH_YES)
3053 return MATCH_ERROR;
3055 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
3056 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
3057 || gfc_add_function (&entry->attr, result->name,
3058 NULL) == FAILURE)
3059 return MATCH_ERROR;
3061 entry->result = result;
3064 if (proc->attr.recursive && result == NULL)
3066 gfc_error ("RESULT attribute required in ENTRY statement at %C");
3067 return MATCH_ERROR;
3071 if (gfc_match_eos () != MATCH_YES)
3073 gfc_syntax_error (ST_ENTRY);
3074 return MATCH_ERROR;
3077 entry->attr.recursive = proc->attr.recursive;
3078 entry->attr.elemental = proc->attr.elemental;
3079 entry->attr.pure = proc->attr.pure;
3081 el = gfc_get_entry_list ();
3082 el->sym = entry;
3083 el->next = gfc_current_ns->entries;
3084 gfc_current_ns->entries = el;
3085 if (el->next)
3086 el->id = el->next->id + 1;
3087 else
3088 el->id = 1;
3090 new_st.op = EXEC_ENTRY;
3091 new_st.ext.entry = el;
3093 return MATCH_YES;
3097 /* Match a subroutine statement, including optional prefixes. */
3099 match
3100 gfc_match_subroutine (void)
3102 char name[GFC_MAX_SYMBOL_LEN + 1];
3103 gfc_symbol *sym;
3104 match m;
3106 if (gfc_current_state () != COMP_NONE
3107 && gfc_current_state () != COMP_INTERFACE
3108 && gfc_current_state () != COMP_CONTAINS)
3109 return MATCH_NO;
3111 m = match_prefix (NULL);
3112 if (m != MATCH_YES)
3113 return m;
3115 m = gfc_match ("subroutine% %n", name);
3116 if (m != MATCH_YES)
3117 return m;
3119 if (get_proc_name (name, &sym, false))
3120 return MATCH_ERROR;
3121 gfc_new_block = sym;
3123 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
3124 return MATCH_ERROR;
3126 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
3127 return MATCH_ERROR;
3129 if (gfc_match_eos () != MATCH_YES)
3131 gfc_syntax_error (ST_SUBROUTINE);
3132 return MATCH_ERROR;
3135 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
3136 return MATCH_ERROR;
3138 return MATCH_YES;
3142 /* Return nonzero if we're currently compiling a contained procedure. */
3144 static int
3145 contained_procedure (void)
3147 gfc_state_data *s;
3149 for (s=gfc_state_stack; s; s=s->previous)
3150 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
3151 && s->previous != NULL
3152 && s->previous->state == COMP_CONTAINS)
3153 return 1;
3155 return 0;
3158 /* Set the kind of each enumerator. The kind is selected such that it is
3159 interoperable with the corresponding C enumeration type, making
3160 sure that -fshort-enums is honored. */
3162 static void
3163 set_enum_kind(void)
3165 enumerator_history *current_history = NULL;
3166 int kind;
3167 int i;
3169 if (max_enum == NULL || enum_history == NULL)
3170 return;
3172 if (!gfc_option.fshort_enums)
3173 return;
3175 i = 0;
3178 kind = gfc_integer_kinds[i++].kind;
3180 while (kind < gfc_c_int_kind
3181 && gfc_check_integer_range (max_enum->initializer->value.integer,
3182 kind) != ARITH_OK);
3184 current_history = enum_history;
3185 while (current_history != NULL)
3187 current_history->sym->ts.kind = kind;
3188 current_history = current_history->next;
3192 /* Match any of the various end-block statements. Returns the type of
3193 END to the caller. The END INTERFACE, END IF, END DO and END
3194 SELECT statements cannot be replaced by a single END statement. */
3196 match
3197 gfc_match_end (gfc_statement * st)
3199 char name[GFC_MAX_SYMBOL_LEN + 1];
3200 gfc_compile_state state;
3201 locus old_loc;
3202 const char *block_name;
3203 const char *target;
3204 int eos_ok;
3205 match m;
3207 old_loc = gfc_current_locus;
3208 if (gfc_match ("end") != MATCH_YES)
3209 return MATCH_NO;
3211 state = gfc_current_state ();
3212 block_name =
3213 gfc_current_block () == NULL ? NULL : gfc_current_block ()->name;
3215 if (state == COMP_CONTAINS)
3217 state = gfc_state_stack->previous->state;
3218 block_name = gfc_state_stack->previous->sym == NULL ? NULL
3219 : gfc_state_stack->previous->sym->name;
3222 switch (state)
3224 case COMP_NONE:
3225 case COMP_PROGRAM:
3226 *st = ST_END_PROGRAM;
3227 target = " program";
3228 eos_ok = 1;
3229 break;
3231 case COMP_SUBROUTINE:
3232 *st = ST_END_SUBROUTINE;
3233 target = " subroutine";
3234 eos_ok = !contained_procedure ();
3235 break;
3237 case COMP_FUNCTION:
3238 *st = ST_END_FUNCTION;
3239 target = " function";
3240 eos_ok = !contained_procedure ();
3241 break;
3243 case COMP_BLOCK_DATA:
3244 *st = ST_END_BLOCK_DATA;
3245 target = " block data";
3246 eos_ok = 1;
3247 break;
3249 case COMP_MODULE:
3250 *st = ST_END_MODULE;
3251 target = " module";
3252 eos_ok = 1;
3253 break;
3255 case COMP_INTERFACE:
3256 *st = ST_END_INTERFACE;
3257 target = " interface";
3258 eos_ok = 0;
3259 break;
3261 case COMP_DERIVED:
3262 *st = ST_END_TYPE;
3263 target = " type";
3264 eos_ok = 0;
3265 break;
3267 case COMP_IF:
3268 *st = ST_ENDIF;
3269 target = " if";
3270 eos_ok = 0;
3271 break;
3273 case COMP_DO:
3274 *st = ST_ENDDO;
3275 target = " do";
3276 eos_ok = 0;
3277 break;
3279 case COMP_SELECT:
3280 *st = ST_END_SELECT;
3281 target = " select";
3282 eos_ok = 0;
3283 break;
3285 case COMP_FORALL:
3286 *st = ST_END_FORALL;
3287 target = " forall";
3288 eos_ok = 0;
3289 break;
3291 case COMP_WHERE:
3292 *st = ST_END_WHERE;
3293 target = " where";
3294 eos_ok = 0;
3295 break;
3297 case COMP_ENUM:
3298 *st = ST_END_ENUM;
3299 target = " enum";
3300 eos_ok = 0;
3301 last_initializer = NULL;
3302 set_enum_kind ();
3303 gfc_free_enum_history ();
3304 break;
3306 default:
3307 gfc_error ("Unexpected END statement at %C");
3308 goto cleanup;
3311 if (gfc_match_eos () == MATCH_YES)
3313 if (!eos_ok)
3315 /* We would have required END [something] */
3316 gfc_error ("%s statement expected at %L",
3317 gfc_ascii_statement (*st), &old_loc);
3318 goto cleanup;
3321 return MATCH_YES;
3324 /* Verify that we've got the sort of end-block that we're expecting. */
3325 if (gfc_match (target) != MATCH_YES)
3327 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
3328 goto cleanup;
3331 /* If we're at the end, make sure a block name wasn't required. */
3332 if (gfc_match_eos () == MATCH_YES)
3335 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT)
3336 return MATCH_YES;
3338 if (gfc_current_block () == NULL)
3339 return MATCH_YES;
3341 gfc_error ("Expected block name of '%s' in %s statement at %C",
3342 block_name, gfc_ascii_statement (*st));
3344 return MATCH_ERROR;
3347 /* END INTERFACE has a special handler for its several possible endings. */
3348 if (*st == ST_END_INTERFACE)
3349 return gfc_match_end_interface ();
3351 /* We haven't hit the end of statement, so what is left must be an end-name. */
3352 m = gfc_match_space ();
3353 if (m == MATCH_YES)
3354 m = gfc_match_name (name);
3356 if (m == MATCH_NO)
3357 gfc_error ("Expected terminating name at %C");
3358 if (m != MATCH_YES)
3359 goto cleanup;
3361 if (block_name == NULL)
3362 goto syntax;
3364 if (strcmp (name, block_name) != 0)
3366 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
3367 gfc_ascii_statement (*st));
3368 goto cleanup;
3371 if (gfc_match_eos () == MATCH_YES)
3372 return MATCH_YES;
3374 syntax:
3375 gfc_syntax_error (*st);
3377 cleanup:
3378 gfc_current_locus = old_loc;
3379 return MATCH_ERROR;
3384 /***************** Attribute declaration statements ****************/
3386 /* Set the attribute of a single variable. */
3388 static match
3389 attr_decl1 (void)
3391 char name[GFC_MAX_SYMBOL_LEN + 1];
3392 gfc_array_spec *as;
3393 gfc_symbol *sym;
3394 locus var_locus;
3395 match m;
3397 as = NULL;
3399 m = gfc_match_name (name);
3400 if (m != MATCH_YES)
3401 goto cleanup;
3403 if (find_special (name, &sym))
3404 return MATCH_ERROR;
3406 var_locus = gfc_current_locus;
3408 /* Deal with possible array specification for certain attributes. */
3409 if (current_attr.dimension
3410 || current_attr.allocatable
3411 || current_attr.pointer
3412 || current_attr.target)
3414 m = gfc_match_array_spec (&as);
3415 if (m == MATCH_ERROR)
3416 goto cleanup;
3418 if (current_attr.dimension && m == MATCH_NO)
3420 gfc_error
3421 ("Missing array specification at %L in DIMENSION statement",
3422 &var_locus);
3423 m = MATCH_ERROR;
3424 goto cleanup;
3427 if ((current_attr.allocatable || current_attr.pointer)
3428 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
3430 gfc_error ("Array specification must be deferred at %L",
3431 &var_locus);
3432 m = MATCH_ERROR;
3433 goto cleanup;
3437 /* Update symbol table. DIMENSION attribute is set in gfc_set_array_spec(). */
3438 if (current_attr.dimension == 0
3439 && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
3441 m = MATCH_ERROR;
3442 goto cleanup;
3445 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
3447 m = MATCH_ERROR;
3448 goto cleanup;
3451 if (sym->attr.cray_pointee && sym->as != NULL)
3453 /* Fix the array spec. */
3454 m = gfc_mod_pointee_as (sym->as);
3455 if (m == MATCH_ERROR)
3456 goto cleanup;
3459 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
3461 m = MATCH_ERROR;
3462 goto cleanup;
3465 if ((current_attr.external || current_attr.intrinsic)
3466 && sym->attr.flavor != FL_PROCEDURE
3467 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
3469 m = MATCH_ERROR;
3470 goto cleanup;
3473 return MATCH_YES;
3475 cleanup:
3476 gfc_free_array_spec (as);
3477 return m;
3481 /* Generic attribute declaration subroutine. Used for attributes that
3482 just have a list of names. */
3484 static match
3485 attr_decl (void)
3487 match m;
3489 /* Gobble the optional double colon, by simply ignoring the result
3490 of gfc_match(). */
3491 gfc_match (" ::");
3493 for (;;)
3495 m = attr_decl1 ();
3496 if (m != MATCH_YES)
3497 break;
3499 if (gfc_match_eos () == MATCH_YES)
3501 m = MATCH_YES;
3502 break;
3505 if (gfc_match_char (',') != MATCH_YES)
3507 gfc_error ("Unexpected character in variable list at %C");
3508 m = MATCH_ERROR;
3509 break;
3513 return m;
3517 /* This routine matches Cray Pointer declarations of the form:
3518 pointer ( <pointer>, <pointee> )
3520 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
3521 The pointer, if already declared, should be an integer. Otherwise, we
3522 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
3523 be either a scalar, or an array declaration. No space is allocated for
3524 the pointee. For the statement
3525 pointer (ipt, ar(10))
3526 any subsequent uses of ar will be translated (in C-notation) as
3527 ar(i) => ((<type> *) ipt)(i)
3528 After gimplification, pointee variable will disappear in the code. */
3530 static match
3531 cray_pointer_decl (void)
3533 match m;
3534 gfc_array_spec *as;
3535 gfc_symbol *cptr; /* Pointer symbol. */
3536 gfc_symbol *cpte; /* Pointee symbol. */
3537 locus var_locus;
3538 bool done = false;
3540 while (!done)
3542 if (gfc_match_char ('(') != MATCH_YES)
3544 gfc_error ("Expected '(' at %C");
3545 return MATCH_ERROR;
3548 /* Match pointer. */
3549 var_locus = gfc_current_locus;
3550 gfc_clear_attr (&current_attr);
3551 gfc_add_cray_pointer (&current_attr, &var_locus);
3552 current_ts.type = BT_INTEGER;
3553 current_ts.kind = gfc_index_integer_kind;
3555 m = gfc_match_symbol (&cptr, 0);
3556 if (m != MATCH_YES)
3558 gfc_error ("Expected variable name at %C");
3559 return m;
3562 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
3563 return MATCH_ERROR;
3565 gfc_set_sym_referenced (cptr);
3567 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
3569 cptr->ts.type = BT_INTEGER;
3570 cptr->ts.kind = gfc_index_integer_kind;
3572 else if (cptr->ts.type != BT_INTEGER)
3574 gfc_error ("Cray pointer at %C must be an integer");
3575 return MATCH_ERROR;
3577 else if (cptr->ts.kind < gfc_index_integer_kind)
3578 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
3579 " memory addresses require %d bytes",
3580 cptr->ts.kind,
3581 gfc_index_integer_kind);
3583 if (gfc_match_char (',') != MATCH_YES)
3585 gfc_error ("Expected \",\" at %C");
3586 return MATCH_ERROR;
3589 /* Match Pointee. */
3590 var_locus = gfc_current_locus;
3591 gfc_clear_attr (&current_attr);
3592 gfc_add_cray_pointee (&current_attr, &var_locus);
3593 current_ts.type = BT_UNKNOWN;
3594 current_ts.kind = 0;
3596 m = gfc_match_symbol (&cpte, 0);
3597 if (m != MATCH_YES)
3599 gfc_error ("Expected variable name at %C");
3600 return m;
3603 /* Check for an optional array spec. */
3604 m = gfc_match_array_spec (&as);
3605 if (m == MATCH_ERROR)
3607 gfc_free_array_spec (as);
3608 return m;
3610 else if (m == MATCH_NO)
3612 gfc_free_array_spec (as);
3613 as = NULL;
3616 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
3617 return MATCH_ERROR;
3619 gfc_set_sym_referenced (cpte);
3621 if (cpte->as == NULL)
3623 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
3624 gfc_internal_error ("Couldn't set Cray pointee array spec.");
3626 else if (as != NULL)
3628 gfc_error ("Duplicate array spec for Cray pointee at %C");
3629 gfc_free_array_spec (as);
3630 return MATCH_ERROR;
3633 as = NULL;
3635 if (cpte->as != NULL)
3637 /* Fix array spec. */
3638 m = gfc_mod_pointee_as (cpte->as);
3639 if (m == MATCH_ERROR)
3640 return m;
3643 /* Point the Pointee at the Pointer. */
3644 cpte->cp_pointer = cptr;
3646 if (gfc_match_char (')') != MATCH_YES)
3648 gfc_error ("Expected \")\" at %C");
3649 return MATCH_ERROR;
3651 m = gfc_match_char (',');
3652 if (m != MATCH_YES)
3653 done = true; /* Stop searching for more declarations. */
3657 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
3658 || gfc_match_eos () != MATCH_YES)
3660 gfc_error ("Expected \",\" or end of statement at %C");
3661 return MATCH_ERROR;
3663 return MATCH_YES;
3667 match
3668 gfc_match_external (void)
3671 gfc_clear_attr (&current_attr);
3672 current_attr.external = 1;
3674 return attr_decl ();
3679 match
3680 gfc_match_intent (void)
3682 sym_intent intent;
3684 intent = match_intent_spec ();
3685 if (intent == INTENT_UNKNOWN)
3686 return MATCH_ERROR;
3688 gfc_clear_attr (&current_attr);
3689 current_attr.intent = intent;
3691 return attr_decl ();
3695 match
3696 gfc_match_intrinsic (void)
3699 gfc_clear_attr (&current_attr);
3700 current_attr.intrinsic = 1;
3702 return attr_decl ();
3706 match
3707 gfc_match_optional (void)
3710 gfc_clear_attr (&current_attr);
3711 current_attr.optional = 1;
3713 return attr_decl ();
3717 match
3718 gfc_match_pointer (void)
3720 gfc_gobble_whitespace ();
3721 if (gfc_peek_char () == '(')
3723 if (!gfc_option.flag_cray_pointer)
3725 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer"
3726 " flag");
3727 return MATCH_ERROR;
3729 return cray_pointer_decl ();
3731 else
3733 gfc_clear_attr (&current_attr);
3734 current_attr.pointer = 1;
3736 return attr_decl ();
3741 match
3742 gfc_match_allocatable (void)
3745 gfc_clear_attr (&current_attr);
3746 current_attr.allocatable = 1;
3748 return attr_decl ();
3752 match
3753 gfc_match_dimension (void)
3756 gfc_clear_attr (&current_attr);
3757 current_attr.dimension = 1;
3759 return attr_decl ();
3763 match
3764 gfc_match_target (void)
3767 gfc_clear_attr (&current_attr);
3768 current_attr.target = 1;
3770 return attr_decl ();
3774 /* Match the list of entities being specified in a PUBLIC or PRIVATE
3775 statement. */
3777 static match
3778 access_attr_decl (gfc_statement st)
3780 char name[GFC_MAX_SYMBOL_LEN + 1];
3781 interface_type type;
3782 gfc_user_op *uop;
3783 gfc_symbol *sym;
3784 gfc_intrinsic_op operator;
3785 match m;
3787 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
3788 goto done;
3790 for (;;)
3792 m = gfc_match_generic_spec (&type, name, &operator);
3793 if (m == MATCH_NO)
3794 goto syntax;
3795 if (m == MATCH_ERROR)
3796 return MATCH_ERROR;
3798 switch (type)
3800 case INTERFACE_NAMELESS:
3801 goto syntax;
3803 case INTERFACE_GENERIC:
3804 if (gfc_get_symbol (name, NULL, &sym))
3805 goto done;
3807 if (gfc_add_access (&sym->attr,
3808 (st ==
3809 ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE,
3810 sym->name, NULL) == FAILURE)
3811 return MATCH_ERROR;
3813 break;
3815 case INTERFACE_INTRINSIC_OP:
3816 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
3818 gfc_current_ns->operator_access[operator] =
3819 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3821 else
3823 gfc_error ("Access specification of the %s operator at %C has "
3824 "already been specified", gfc_op2string (operator));
3825 goto done;
3828 break;
3830 case INTERFACE_USER_OP:
3831 uop = gfc_get_uop (name);
3833 if (uop->access == ACCESS_UNKNOWN)
3835 uop->access =
3836 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3838 else
3840 gfc_error
3841 ("Access specification of the .%s. operator at %C has "
3842 "already been specified", sym->name);
3843 goto done;
3846 break;
3849 if (gfc_match_char (',') == MATCH_NO)
3850 break;
3853 if (gfc_match_eos () != MATCH_YES)
3854 goto syntax;
3855 return MATCH_YES;
3857 syntax:
3858 gfc_syntax_error (st);
3860 done:
3861 return MATCH_ERROR;
3865 match
3866 gfc_match_protected (void)
3868 gfc_symbol *sym;
3869 match m;
3871 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3873 gfc_error ("PROTECTED at %C only allowed in specification "
3874 "part of a module");
3875 return MATCH_ERROR;
3879 if (gfc_notify_std (GFC_STD_F2003,
3880 "Fortran 2003: PROTECTED statement at %C")
3881 == FAILURE)
3882 return MATCH_ERROR;
3884 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
3886 return MATCH_ERROR;
3889 if (gfc_match_eos () == MATCH_YES)
3890 goto syntax;
3892 for(;;)
3894 m = gfc_match_symbol (&sym, 0);
3895 switch (m)
3897 case MATCH_YES:
3898 if (gfc_add_protected (&sym->attr, sym->name,
3899 &gfc_current_locus) == FAILURE)
3900 return MATCH_ERROR;
3901 goto next_item;
3903 case MATCH_NO:
3904 break;
3906 case MATCH_ERROR:
3907 return MATCH_ERROR;
3910 next_item:
3911 if (gfc_match_eos () == MATCH_YES)
3912 break;
3913 if (gfc_match_char (',') != MATCH_YES)
3914 goto syntax;
3917 return MATCH_YES;
3919 syntax:
3920 gfc_error ("Syntax error in PROTECTED statement at %C");
3921 return MATCH_ERROR;
3926 /* The PRIVATE statement is a bit weird in that it can be a attribute
3927 declaration, but also works as a standlone statement inside of a
3928 type declaration or a module. */
3930 match
3931 gfc_match_private (gfc_statement * st)
3934 if (gfc_match ("private") != MATCH_YES)
3935 return MATCH_NO;
3937 if (gfc_current_state () == COMP_DERIVED)
3939 if (gfc_match_eos () == MATCH_YES)
3941 *st = ST_PRIVATE;
3942 return MATCH_YES;
3945 gfc_syntax_error (ST_PRIVATE);
3946 return MATCH_ERROR;
3949 if (gfc_match_eos () == MATCH_YES)
3951 *st = ST_PRIVATE;
3952 return MATCH_YES;
3955 *st = ST_ATTR_DECL;
3956 return access_attr_decl (ST_PRIVATE);
3960 match
3961 gfc_match_public (gfc_statement * st)
3964 if (gfc_match ("public") != MATCH_YES)
3965 return MATCH_NO;
3967 if (gfc_match_eos () == MATCH_YES)
3969 *st = ST_PUBLIC;
3970 return MATCH_YES;
3973 *st = ST_ATTR_DECL;
3974 return access_attr_decl (ST_PUBLIC);
3978 /* Workhorse for gfc_match_parameter. */
3980 static match
3981 do_parm (void)
3983 gfc_symbol *sym;
3984 gfc_expr *init;
3985 match m;
3987 m = gfc_match_symbol (&sym, 0);
3988 if (m == MATCH_NO)
3989 gfc_error ("Expected variable name at %C in PARAMETER statement");
3991 if (m != MATCH_YES)
3992 return m;
3994 if (gfc_match_char ('=') == MATCH_NO)
3996 gfc_error ("Expected = sign in PARAMETER statement at %C");
3997 return MATCH_ERROR;
4000 m = gfc_match_init_expr (&init);
4001 if (m == MATCH_NO)
4002 gfc_error ("Expected expression at %C in PARAMETER statement");
4003 if (m != MATCH_YES)
4004 return m;
4006 if (sym->ts.type == BT_UNKNOWN
4007 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
4009 m = MATCH_ERROR;
4010 goto cleanup;
4013 if (gfc_check_assign_symbol (sym, init) == FAILURE
4014 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
4016 m = MATCH_ERROR;
4017 goto cleanup;
4020 if (sym->ts.type == BT_CHARACTER
4021 && sym->ts.cl != NULL
4022 && sym->ts.cl->length != NULL
4023 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
4024 && init->expr_type == EXPR_CONSTANT
4025 && init->ts.type == BT_CHARACTER
4026 && init->ts.kind == 1)
4027 gfc_set_constant_character_len (
4028 mpz_get_si (sym->ts.cl->length->value.integer), init);
4030 sym->value = init;
4031 return MATCH_YES;
4033 cleanup:
4034 gfc_free_expr (init);
4035 return m;
4039 /* Match a parameter statement, with the weird syntax that these have. */
4041 match
4042 gfc_match_parameter (void)
4044 match m;
4046 if (gfc_match_char ('(') == MATCH_NO)
4047 return MATCH_NO;
4049 for (;;)
4051 m = do_parm ();
4052 if (m != MATCH_YES)
4053 break;
4055 if (gfc_match (" )%t") == MATCH_YES)
4056 break;
4058 if (gfc_match_char (',') != MATCH_YES)
4060 gfc_error ("Unexpected characters in PARAMETER statement at %C");
4061 m = MATCH_ERROR;
4062 break;
4066 return m;
4070 /* Save statements have a special syntax. */
4072 match
4073 gfc_match_save (void)
4075 char n[GFC_MAX_SYMBOL_LEN+1];
4076 gfc_common_head *c;
4077 gfc_symbol *sym;
4078 match m;
4080 if (gfc_match_eos () == MATCH_YES)
4082 if (gfc_current_ns->seen_save)
4084 if (gfc_notify_std (GFC_STD_LEGACY,
4085 "Blanket SAVE statement at %C follows previous "
4086 "SAVE statement")
4087 == FAILURE)
4088 return MATCH_ERROR;
4091 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
4092 return MATCH_YES;
4095 if (gfc_current_ns->save_all)
4097 if (gfc_notify_std (GFC_STD_LEGACY,
4098 "SAVE statement at %C follows blanket SAVE statement")
4099 == FAILURE)
4100 return MATCH_ERROR;
4103 gfc_match (" ::");
4105 for (;;)
4107 m = gfc_match_symbol (&sym, 0);
4108 switch (m)
4110 case MATCH_YES:
4111 if (gfc_add_save (&sym->attr, sym->name,
4112 &gfc_current_locus) == FAILURE)
4113 return MATCH_ERROR;
4114 goto next_item;
4116 case MATCH_NO:
4117 break;
4119 case MATCH_ERROR:
4120 return MATCH_ERROR;
4123 m = gfc_match (" / %n /", &n);
4124 if (m == MATCH_ERROR)
4125 return MATCH_ERROR;
4126 if (m == MATCH_NO)
4127 goto syntax;
4129 c = gfc_get_common (n, 0);
4130 c->saved = 1;
4132 gfc_current_ns->seen_save = 1;
4134 next_item:
4135 if (gfc_match_eos () == MATCH_YES)
4136 break;
4137 if (gfc_match_char (',') != MATCH_YES)
4138 goto syntax;
4141 return MATCH_YES;
4143 syntax:
4144 gfc_error ("Syntax error in SAVE statement at %C");
4145 return MATCH_ERROR;
4149 match
4150 gfc_match_value (void)
4152 gfc_symbol *sym;
4153 match m;
4155 if (gfc_notify_std (GFC_STD_F2003,
4156 "Fortran 2003: VALUE statement at %C")
4157 == FAILURE)
4158 return MATCH_ERROR;
4160 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4162 return MATCH_ERROR;
4165 if (gfc_match_eos () == MATCH_YES)
4166 goto syntax;
4168 for(;;)
4170 m = gfc_match_symbol (&sym, 0);
4171 switch (m)
4173 case MATCH_YES:
4174 if (gfc_add_value (&sym->attr, sym->name,
4175 &gfc_current_locus) == FAILURE)
4176 return MATCH_ERROR;
4177 goto next_item;
4179 case MATCH_NO:
4180 break;
4182 case MATCH_ERROR:
4183 return MATCH_ERROR;
4186 next_item:
4187 if (gfc_match_eos () == MATCH_YES)
4188 break;
4189 if (gfc_match_char (',') != MATCH_YES)
4190 goto syntax;
4193 return MATCH_YES;
4195 syntax:
4196 gfc_error ("Syntax error in VALUE statement at %C");
4197 return MATCH_ERROR;
4200 match
4201 gfc_match_volatile (void)
4203 gfc_symbol *sym;
4204 match m;
4206 if (gfc_notify_std (GFC_STD_F2003,
4207 "Fortran 2003: VOLATILE statement at %C")
4208 == FAILURE)
4209 return MATCH_ERROR;
4211 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4213 return MATCH_ERROR;
4216 if (gfc_match_eos () == MATCH_YES)
4217 goto syntax;
4219 for(;;)
4221 m = gfc_match_symbol (&sym, 0);
4222 switch (m)
4224 case MATCH_YES:
4225 if (gfc_add_volatile (&sym->attr, sym->name,
4226 &gfc_current_locus) == FAILURE)
4227 return MATCH_ERROR;
4228 goto next_item;
4230 case MATCH_NO:
4231 break;
4233 case MATCH_ERROR:
4234 return MATCH_ERROR;
4237 next_item:
4238 if (gfc_match_eos () == MATCH_YES)
4239 break;
4240 if (gfc_match_char (',') != MATCH_YES)
4241 goto syntax;
4244 return MATCH_YES;
4246 syntax:
4247 gfc_error ("Syntax error in VOLATILE statement at %C");
4248 return MATCH_ERROR;
4253 /* Match a module procedure statement. Note that we have to modify
4254 symbols in the parent's namespace because the current one was there
4255 to receive symbols that are in an interface's formal argument list. */
4257 match
4258 gfc_match_modproc (void)
4260 char name[GFC_MAX_SYMBOL_LEN + 1];
4261 gfc_symbol *sym;
4262 match m;
4264 if (gfc_state_stack->state != COMP_INTERFACE
4265 || gfc_state_stack->previous == NULL
4266 || current_interface.type == INTERFACE_NAMELESS)
4268 gfc_error
4269 ("MODULE PROCEDURE at %C must be in a generic module interface");
4270 return MATCH_ERROR;
4273 for (;;)
4275 m = gfc_match_name (name);
4276 if (m == MATCH_NO)
4277 goto syntax;
4278 if (m != MATCH_YES)
4279 return MATCH_ERROR;
4281 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4282 return MATCH_ERROR;
4284 if (sym->attr.proc != PROC_MODULE
4285 && gfc_add_procedure (&sym->attr, PROC_MODULE,
4286 sym->name, NULL) == FAILURE)
4287 return MATCH_ERROR;
4289 if (gfc_add_interface (sym) == FAILURE)
4290 return MATCH_ERROR;
4292 sym->attr.mod_proc = 1;
4294 if (gfc_match_eos () == MATCH_YES)
4295 break;
4296 if (gfc_match_char (',') != MATCH_YES)
4297 goto syntax;
4300 return MATCH_YES;
4302 syntax:
4303 gfc_syntax_error (ST_MODULE_PROC);
4304 return MATCH_ERROR;
4308 /* Match the beginning of a derived type declaration. If a type name
4309 was the result of a function, then it is possible to have a symbol
4310 already to be known as a derived type yet have no components. */
4312 match
4313 gfc_match_derived_decl (void)
4315 char name[GFC_MAX_SYMBOL_LEN + 1];
4316 symbol_attribute attr;
4317 gfc_symbol *sym;
4318 match m;
4320 if (gfc_current_state () == COMP_DERIVED)
4321 return MATCH_NO;
4323 gfc_clear_attr (&attr);
4325 loop:
4326 if (gfc_match (" , private") == MATCH_YES)
4328 if (gfc_find_state (COMP_MODULE) == FAILURE)
4330 gfc_error
4331 ("Derived type at %C can only be PRIVATE within a MODULE");
4332 return MATCH_ERROR;
4335 if (gfc_add_access (&attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
4336 return MATCH_ERROR;
4337 goto loop;
4340 if (gfc_match (" , public") == MATCH_YES)
4342 if (gfc_find_state (COMP_MODULE) == FAILURE)
4344 gfc_error ("Derived type at %C can only be PUBLIC within a MODULE");
4345 return MATCH_ERROR;
4348 if (gfc_add_access (&attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
4349 return MATCH_ERROR;
4350 goto loop;
4353 if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
4355 gfc_error ("Expected :: in TYPE definition at %C");
4356 return MATCH_ERROR;
4359 m = gfc_match (" %n%t", name);
4360 if (m != MATCH_YES)
4361 return m;
4363 /* Make sure the name isn't the name of an intrinsic type. The
4364 'double precision' type doesn't get past the name matcher. */
4365 if (strcmp (name, "integer") == 0
4366 || strcmp (name, "real") == 0
4367 || strcmp (name, "character") == 0
4368 || strcmp (name, "logical") == 0
4369 || strcmp (name, "complex") == 0)
4371 gfc_error
4372 ("Type name '%s' at %C cannot be the same as an intrinsic type",
4373 name);
4374 return MATCH_ERROR;
4377 if (gfc_get_symbol (name, NULL, &sym))
4378 return MATCH_ERROR;
4380 if (sym->ts.type != BT_UNKNOWN)
4382 gfc_error ("Derived type name '%s' at %C already has a basic type "
4383 "of %s", sym->name, gfc_typename (&sym->ts));
4384 return MATCH_ERROR;
4387 /* The symbol may already have the derived attribute without the
4388 components. The ways this can happen is via a function
4389 definition, an INTRINSIC statement or a subtype in another
4390 derived type that is a pointer. The first part of the AND clause
4391 is true if a the symbol is not the return value of a function. */
4392 if (sym->attr.flavor != FL_DERIVED
4393 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
4394 return MATCH_ERROR;
4396 if (sym->components != NULL)
4398 gfc_error
4399 ("Derived type definition of '%s' at %C has already been defined",
4400 sym->name);
4401 return MATCH_ERROR;
4404 if (attr.access != ACCESS_UNKNOWN
4405 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
4406 return MATCH_ERROR;
4408 gfc_new_block = sym;
4410 return MATCH_YES;
4414 /* Cray Pointees can be declared as:
4415 pointer (ipt, a (n,m,...,*))
4416 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
4417 cheat and set a constant bound of 1 for the last dimension, if this
4418 is the case. Since there is no bounds-checking for Cray Pointees,
4419 this will be okay. */
4422 gfc_mod_pointee_as (gfc_array_spec *as)
4424 as->cray_pointee = true; /* This will be useful to know later. */
4425 if (as->type == AS_ASSUMED_SIZE)
4427 as->type = AS_EXPLICIT;
4428 as->upper[as->rank - 1] = gfc_int_expr (1);
4429 as->cp_was_assumed = true;
4431 else if (as->type == AS_ASSUMED_SHAPE)
4433 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
4434 return MATCH_ERROR;
4436 return MATCH_YES;
4440 /* Match the enum definition statement, here we are trying to match
4441 the first line of enum definition statement.
4442 Returns MATCH_YES if match is found. */
4444 match
4445 gfc_match_enum (void)
4447 match m;
4449 m = gfc_match_eos ();
4450 if (m != MATCH_YES)
4451 return m;
4453 if (gfc_notify_std (GFC_STD_F2003,
4454 "Fortran 2003: ENUM AND ENUMERATOR at %C")
4455 == FAILURE)
4456 return MATCH_ERROR;
4458 return MATCH_YES;
4462 /* Match the enumerator definition statement. */
4464 match
4465 gfc_match_enumerator_def (void)
4467 match m;
4468 int elem;
4470 gfc_clear_ts (&current_ts);
4472 m = gfc_match (" enumerator");
4473 if (m != MATCH_YES)
4474 return m;
4476 if (gfc_current_state () != COMP_ENUM)
4478 gfc_error ("ENUM definition statement expected before %C");
4479 gfc_free_enum_history ();
4480 return MATCH_ERROR;
4483 (&current_ts)->type = BT_INTEGER;
4484 (&current_ts)->kind = gfc_c_int_kind;
4486 m = match_attr_spec ();
4487 if (m == MATCH_ERROR)
4489 m = MATCH_NO;
4490 goto cleanup;
4493 elem = 1;
4494 for (;;)
4496 m = variable_decl (elem++);
4497 if (m == MATCH_ERROR)
4498 goto cleanup;
4499 if (m == MATCH_NO)
4500 break;
4502 if (gfc_match_eos () == MATCH_YES)
4503 goto cleanup;
4504 if (gfc_match_char (',') != MATCH_YES)
4505 break;
4508 if (gfc_current_state () == COMP_ENUM)
4510 gfc_free_enum_history ();
4511 gfc_error ("Syntax error in ENUMERATOR definition at %C");
4512 m = MATCH_ERROR;
4515 cleanup:
4516 gfc_free_array_spec (current_as);
4517 current_as = NULL;
4518 return m;