* Make-lang.in (GFORTRAN_TARGET_INSTALL_NAME): Define.
[official-gcc.git] / gcc / fortran / decl.c
blob7516057bfeecef96b99e3b6425557e2703c6cb70
1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005 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 static match var_element (gfc_data_variable *);
133 /* Match a list of variables terminated by an iterator and a right
134 parenthesis. */
136 static match
137 var_list (gfc_data_variable * parent)
139 gfc_data_variable *tail, var;
140 match m;
142 m = var_element (&var);
143 if (m == MATCH_ERROR)
144 return MATCH_ERROR;
145 if (m == MATCH_NO)
146 goto syntax;
148 tail = gfc_get_data_variable ();
149 *tail = var;
151 parent->list = tail;
153 for (;;)
155 if (gfc_match_char (',') != MATCH_YES)
156 goto syntax;
158 m = gfc_match_iterator (&parent->iter, 1);
159 if (m == MATCH_YES)
160 break;
161 if (m == MATCH_ERROR)
162 return MATCH_ERROR;
164 m = var_element (&var);
165 if (m == MATCH_ERROR)
166 return MATCH_ERROR;
167 if (m == MATCH_NO)
168 goto syntax;
170 tail->next = gfc_get_data_variable ();
171 tail = tail->next;
173 *tail = var;
176 if (gfc_match_char (')') != MATCH_YES)
177 goto syntax;
178 return MATCH_YES;
180 syntax:
181 gfc_syntax_error (ST_DATA);
182 return MATCH_ERROR;
186 /* Match a single element in a data variable list, which can be a
187 variable-iterator list. */
189 static match
190 var_element (gfc_data_variable * new)
192 match m;
193 gfc_symbol *sym;
195 memset (new, 0, sizeof (gfc_data_variable));
197 if (gfc_match_char ('(') == MATCH_YES)
198 return var_list (new);
200 m = gfc_match_variable (&new->expr, 0);
201 if (m != MATCH_YES)
202 return m;
204 sym = new->expr->symtree->n.sym;
206 if(sym->value != NULL)
208 gfc_error ("Variable '%s' at %C already has an initialization",
209 sym->name);
210 return MATCH_ERROR;
213 #if 0 /* TODO: Find out where to move this message */
214 if (sym->attr.in_common)
215 /* See if sym is in the blank common block. */
216 for (t = &sym->ns->blank_common; t; t = t->common_next)
217 if (sym == t->head)
219 gfc_error ("DATA statement at %C may not initialize variable "
220 "'%s' from blank COMMON", sym->name);
221 return MATCH_ERROR;
223 #endif
225 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
226 return MATCH_ERROR;
228 return MATCH_YES;
232 /* Match the top-level list of data variables. */
234 static match
235 top_var_list (gfc_data * d)
237 gfc_data_variable var, *tail, *new;
238 match m;
240 tail = NULL;
242 for (;;)
244 m = var_element (&var);
245 if (m == MATCH_NO)
246 goto syntax;
247 if (m == MATCH_ERROR)
248 return MATCH_ERROR;
250 new = gfc_get_data_variable ();
251 *new = var;
253 if (tail == NULL)
254 d->var = new;
255 else
256 tail->next = new;
258 tail = new;
260 if (gfc_match_char ('/') == MATCH_YES)
261 break;
262 if (gfc_match_char (',') != MATCH_YES)
263 goto syntax;
266 return MATCH_YES;
268 syntax:
269 gfc_syntax_error (ST_DATA);
270 return MATCH_ERROR;
274 static match
275 match_data_constant (gfc_expr ** result)
277 char name[GFC_MAX_SYMBOL_LEN + 1];
278 gfc_symbol *sym;
279 gfc_expr *expr;
280 match m;
282 m = gfc_match_literal_constant (&expr, 1);
283 if (m == MATCH_YES)
285 *result = expr;
286 return MATCH_YES;
289 if (m == MATCH_ERROR)
290 return MATCH_ERROR;
292 m = gfc_match_null (result);
293 if (m != MATCH_NO)
294 return m;
296 m = gfc_match_name (name);
297 if (m != MATCH_YES)
298 return m;
300 if (gfc_find_symbol (name, NULL, 1, &sym))
301 return MATCH_ERROR;
303 if (sym == NULL
304 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
306 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
307 name);
308 return MATCH_ERROR;
310 else if (sym->attr.flavor == FL_DERIVED)
311 return gfc_match_structure_constructor (sym, result);
313 *result = gfc_copy_expr (sym->value);
314 return MATCH_YES;
318 /* Match a list of values in a DATA statement. The leading '/' has
319 already been seen at this point. */
321 static match
322 top_val_list (gfc_data * data)
324 gfc_data_value *new, *tail;
325 gfc_expr *expr;
326 const char *msg;
327 match m;
329 tail = NULL;
331 for (;;)
333 m = match_data_constant (&expr);
334 if (m == MATCH_NO)
335 goto syntax;
336 if (m == MATCH_ERROR)
337 return MATCH_ERROR;
339 new = gfc_get_data_value ();
341 if (tail == NULL)
342 data->value = new;
343 else
344 tail->next = new;
346 tail = new;
348 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
350 tail->expr = expr;
351 tail->repeat = 1;
353 else
355 signed int tmp;
356 msg = gfc_extract_int (expr, &tmp);
357 gfc_free_expr (expr);
358 if (msg != NULL)
360 gfc_error (msg);
361 return MATCH_ERROR;
363 tail->repeat = tmp;
365 m = match_data_constant (&tail->expr);
366 if (m == MATCH_NO)
367 goto syntax;
368 if (m == MATCH_ERROR)
369 return MATCH_ERROR;
372 if (gfc_match_char ('/') == MATCH_YES)
373 break;
374 if (gfc_match_char (',') == MATCH_NO)
375 goto syntax;
378 return MATCH_YES;
380 syntax:
381 gfc_syntax_error (ST_DATA);
382 return MATCH_ERROR;
386 /* Matches an old style initialization. */
388 static match
389 match_old_style_init (const char *name)
391 match m;
392 gfc_symtree *st;
393 gfc_data *newdata;
395 /* Set up data structure to hold initializers. */
396 gfc_find_sym_tree (name, NULL, 0, &st);
398 newdata = gfc_get_data ();
399 newdata->var = gfc_get_data_variable ();
400 newdata->var->expr = gfc_get_variable_expr (st);
402 /* Match initial value list. This also eats the terminal
403 '/'. */
404 m = top_val_list (newdata);
405 if (m != MATCH_YES)
407 gfc_free (newdata);
408 return m;
411 if (gfc_pure (NULL))
413 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
414 gfc_free (newdata);
415 return MATCH_ERROR;
418 /* Chain in namespace list of DATA initializers. */
419 newdata->next = gfc_current_ns->data;
420 gfc_current_ns->data = newdata;
422 return m;
425 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
426 we are matching a DATA statement and are therefore issuing an error
427 if we encounter something unexpected, if not, we're trying to match
428 an old-style initialization expression of the form INTEGER I /2/. */
430 match
431 gfc_match_data (void)
433 gfc_data *new;
434 match m;
436 for (;;)
438 new = gfc_get_data ();
439 new->where = gfc_current_locus;
441 m = top_var_list (new);
442 if (m != MATCH_YES)
443 goto cleanup;
445 m = top_val_list (new);
446 if (m != MATCH_YES)
447 goto cleanup;
449 new->next = gfc_current_ns->data;
450 gfc_current_ns->data = new;
452 if (gfc_match_eos () == MATCH_YES)
453 break;
455 gfc_match_char (','); /* Optional comma */
458 if (gfc_pure (NULL))
460 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
461 return MATCH_ERROR;
464 return MATCH_YES;
466 cleanup:
467 gfc_free_data (new);
468 return MATCH_ERROR;
472 /************************ Declaration statements *********************/
474 /* Match an intent specification. Since this can only happen after an
475 INTENT word, a legal intent-spec must follow. */
477 static sym_intent
478 match_intent_spec (void)
481 if (gfc_match (" ( in out )") == MATCH_YES)
482 return INTENT_INOUT;
483 if (gfc_match (" ( in )") == MATCH_YES)
484 return INTENT_IN;
485 if (gfc_match (" ( out )") == MATCH_YES)
486 return INTENT_OUT;
488 gfc_error ("Bad INTENT specification at %C");
489 return INTENT_UNKNOWN;
493 /* Matches a character length specification, which is either a
494 specification expression or a '*'. */
496 static match
497 char_len_param_value (gfc_expr ** expr)
500 if (gfc_match_char ('*') == MATCH_YES)
502 *expr = NULL;
503 return MATCH_YES;
506 return gfc_match_expr (expr);
510 /* A character length is a '*' followed by a literal integer or a
511 char_len_param_value in parenthesis. */
513 static match
514 match_char_length (gfc_expr ** expr)
516 int length;
517 match m;
519 m = gfc_match_char ('*');
520 if (m != MATCH_YES)
521 return m;
523 m = gfc_match_small_literal_int (&length);
524 if (m == MATCH_ERROR)
525 return m;
527 if (m == MATCH_YES)
529 *expr = gfc_int_expr (length);
530 return m;
533 if (gfc_match_char ('(') == MATCH_NO)
534 goto syntax;
536 m = char_len_param_value (expr);
537 if (m == MATCH_ERROR)
538 return m;
539 if (m == MATCH_NO)
540 goto syntax;
542 if (gfc_match_char (')') == MATCH_NO)
544 gfc_free_expr (*expr);
545 *expr = NULL;
546 goto syntax;
549 return MATCH_YES;
551 syntax:
552 gfc_error ("Syntax error in character length specification at %C");
553 return MATCH_ERROR;
557 /* Special subroutine for finding a symbol. Check if the name is found
558 in the current name space. If not, and we're compiling a function or
559 subroutine and the parent compilation unit is an interface, then check
560 to see if the name we've been given is the name of the interface
561 (located in another namespace). */
563 static int
564 find_special (const char *name, gfc_symbol ** result)
566 gfc_state_data *s;
567 int i;
569 i = gfc_get_symbol (name, NULL, result);
570 if (i==0)
571 goto end;
573 if (gfc_current_state () != COMP_SUBROUTINE
574 && gfc_current_state () != COMP_FUNCTION)
575 goto end;
577 s = gfc_state_stack->previous;
578 if (s == NULL)
579 goto end;
581 if (s->state != COMP_INTERFACE)
582 goto end;
583 if (s->sym == NULL)
584 goto end; /* Nameless interface */
586 if (strcmp (name, s->sym->name) == 0)
588 *result = s->sym;
589 return 0;
592 end:
593 return i;
597 /* Special subroutine for getting a symbol node associated with a
598 procedure name, used in SUBROUTINE and FUNCTION statements. The
599 symbol is created in the parent using with symtree node in the
600 child unit pointing to the symbol. If the current namespace has no
601 parent, then the symbol is just created in the current unit. */
603 static int
604 get_proc_name (const char *name, gfc_symbol ** result)
606 gfc_symtree *st;
607 gfc_symbol *sym;
608 int rc;
610 if (gfc_current_ns->parent == NULL)
611 return gfc_get_symbol (name, NULL, result);
613 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
614 if (*result == NULL)
615 return rc;
617 /* ??? Deal with ENTRY problem */
619 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
621 sym = *result;
622 st->n.sym = sym;
623 sym->refs++;
625 /* See if the procedure should be a module procedure */
627 if (sym->ns->proc_name != NULL
628 && sym->ns->proc_name->attr.flavor == FL_MODULE
629 && sym->attr.proc != PROC_MODULE
630 && gfc_add_procedure (&sym->attr, PROC_MODULE,
631 sym->name, NULL) == FAILURE)
632 rc = 2;
634 return rc;
638 /* Function called by variable_decl() that adds a name to the symbol
639 table. */
641 static try
642 build_sym (const char *name, gfc_charlen * cl,
643 gfc_array_spec ** as, locus * var_locus)
645 symbol_attribute attr;
646 gfc_symbol *sym;
648 /* if (find_special (name, &sym)) */
649 if (gfc_get_symbol (name, NULL, &sym))
650 return FAILURE;
652 /* Start updating the symbol table. Add basic type attribute
653 if present. */
654 if (current_ts.type != BT_UNKNOWN
655 &&(sym->attr.implicit_type == 0
656 || !gfc_compare_types (&sym->ts, &current_ts))
657 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
658 return FAILURE;
660 if (sym->ts.type == BT_CHARACTER)
661 sym->ts.cl = cl;
663 /* Add dimension attribute if present. */
664 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
665 return FAILURE;
666 *as = NULL;
668 /* Add attribute to symbol. The copy is so that we can reset the
669 dimension attribute. */
670 attr = current_attr;
671 attr.dimension = 0;
673 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
674 return FAILURE;
676 return SUCCESS;
679 /* Set character constant to the given length. The constant will be padded or
680 truncated. */
682 void
683 gfc_set_constant_character_len (int len, gfc_expr * expr)
685 char * s;
686 int slen;
688 gcc_assert (expr->expr_type == EXPR_CONSTANT);
689 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
691 slen = expr->value.character.length;
692 if (len != slen)
694 s = gfc_getmem (len);
695 memcpy (s, expr->value.character.string, MIN (len, slen));
696 if (len > slen)
697 memset (&s[slen], ' ', len - slen);
698 gfc_free (expr->value.character.string);
699 expr->value.character.string = s;
700 expr->value.character.length = len;
705 /* Function to create and update the enumumerator history
706 using the information passed as arguments.
707 Pointer "max_enum" is also updated, to point to
708 enum history node containing largest initializer.
710 SYM points to the symbol node of enumerator.
711 INIT points to its enumerator value. */
713 static void
714 create_enum_history(gfc_symbol *sym, gfc_expr *init)
716 enumerator_history *new_enum_history;
717 gcc_assert (sym != NULL && init != NULL);
719 new_enum_history = gfc_getmem (sizeof (enumerator_history));
721 new_enum_history->sym = sym;
722 new_enum_history->initializer = init;
723 new_enum_history->next = NULL;
725 if (enum_history == NULL)
727 enum_history = new_enum_history;
728 max_enum = enum_history;
730 else
732 new_enum_history->next = enum_history;
733 enum_history = new_enum_history;
735 if (mpz_cmp (max_enum->initializer->value.integer,
736 new_enum_history->initializer->value.integer) < 0)
737 max_enum = new_enum_history;
742 /* Function to free enum kind history. */
744 void
745 gfc_free_enum_history(void)
747 enumerator_history *current = enum_history;
748 enumerator_history *next;
750 while (current != NULL)
752 next = current->next;
753 gfc_free (current);
754 current = next;
756 max_enum = NULL;
757 enum_history = NULL;
761 /* Function called by variable_decl() that adds an initialization
762 expression to a symbol. */
764 static try
765 add_init_expr_to_sym (const char *name, gfc_expr ** initp,
766 locus * var_locus)
768 symbol_attribute attr;
769 gfc_symbol *sym;
770 gfc_expr *init;
772 init = *initp;
773 if (find_special (name, &sym))
774 return FAILURE;
776 attr = sym->attr;
778 /* If this symbol is confirming an implicit parameter type,
779 then an initialization expression is not allowed. */
780 if (attr.flavor == FL_PARAMETER
781 && sym->value != NULL
782 && *initp != NULL)
784 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
785 sym->name);
786 return FAILURE;
789 if (attr.in_common
790 && !attr.data
791 && *initp != NULL)
793 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
794 sym->name);
795 return FAILURE;
798 if (init == NULL)
800 /* An initializer is required for PARAMETER declarations. */
801 if (attr.flavor == FL_PARAMETER)
803 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
804 return FAILURE;
807 else
809 /* If a variable appears in a DATA block, it cannot have an
810 initializer. */
811 if (sym->attr.data)
813 gfc_error
814 ("Variable '%s' at %C with an initializer already appears "
815 "in a DATA statement", sym->name);
816 return FAILURE;
819 /* Check if the assignment can happen. This has to be put off
820 until later for a derived type variable. */
821 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
822 && gfc_check_assign_symbol (sym, init) == FAILURE)
823 return FAILURE;
825 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
827 /* Update symbol character length according initializer. */
828 if (sym->ts.cl->length == NULL)
830 /* If there are multiple CHARACTER variables declared on
831 the same line, we don't want them to share the same
832 length. */
833 sym->ts.cl = gfc_get_charlen ();
834 sym->ts.cl->next = gfc_current_ns->cl_list;
835 gfc_current_ns->cl_list = sym->ts.cl;
837 if (init->expr_type == EXPR_CONSTANT)
838 sym->ts.cl->length =
839 gfc_int_expr (init->value.character.length);
840 else if (init->expr_type == EXPR_ARRAY)
841 sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
843 /* Update initializer character length according symbol. */
844 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
846 int len = mpz_get_si (sym->ts.cl->length->value.integer);
847 gfc_constructor * p;
849 if (init->expr_type == EXPR_CONSTANT)
850 gfc_set_constant_character_len (len, init);
851 else if (init->expr_type == EXPR_ARRAY)
853 gfc_free_expr (init->ts.cl->length);
854 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
855 for (p = init->value.constructor; p; p = p->next)
856 gfc_set_constant_character_len (len, p->expr);
861 /* Add initializer. Make sure we keep the ranks sane. */
862 if (sym->attr.dimension && init->rank == 0)
863 init->rank = sym->as->rank;
865 sym->value = init;
866 *initp = NULL;
869 /* Maintain enumerator history. */
870 if (gfc_current_state () == COMP_ENUM)
871 create_enum_history (sym, init);
873 return SUCCESS;
877 /* Function called by variable_decl() that adds a name to a structure
878 being built. */
880 static try
881 build_struct (const char *name, gfc_charlen * cl, gfc_expr ** init,
882 gfc_array_spec ** as)
884 gfc_component *c;
886 /* If the current symbol is of the same derived type that we're
887 constructing, it must have the pointer attribute. */
888 if (current_ts.type == BT_DERIVED
889 && current_ts.derived == gfc_current_block ()
890 && current_attr.pointer == 0)
892 gfc_error ("Component at %C must have the POINTER attribute");
893 return FAILURE;
896 if (gfc_current_block ()->attr.pointer
897 && (*as)->rank != 0)
899 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
901 gfc_error ("Array component of structure at %C must have explicit "
902 "or deferred shape");
903 return FAILURE;
907 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
908 return FAILURE;
910 c->ts = current_ts;
911 c->ts.cl = cl;
912 gfc_set_component_attr (c, &current_attr);
914 c->initializer = *init;
915 *init = NULL;
917 c->as = *as;
918 if (c->as != NULL)
919 c->dimension = 1;
920 *as = NULL;
922 /* Check array components. */
923 if (!c->dimension)
924 return SUCCESS;
926 if (c->pointer)
928 if (c->as->type != AS_DEFERRED)
930 gfc_error ("Pointer array component of structure at %C "
931 "must have a deferred shape");
932 return FAILURE;
935 else
937 if (c->as->type != AS_EXPLICIT)
939 gfc_error
940 ("Array component of structure at %C must have an explicit "
941 "shape");
942 return FAILURE;
946 return SUCCESS;
950 /* Match a 'NULL()', and possibly take care of some side effects. */
952 match
953 gfc_match_null (gfc_expr ** result)
955 gfc_symbol *sym;
956 gfc_expr *e;
957 match m;
959 m = gfc_match (" null ( )");
960 if (m != MATCH_YES)
961 return m;
963 /* The NULL symbol now has to be/become an intrinsic function. */
964 if (gfc_get_symbol ("null", NULL, &sym))
966 gfc_error ("NULL() initialization at %C is ambiguous");
967 return MATCH_ERROR;
970 gfc_intrinsic_symbol (sym);
972 if (sym->attr.proc != PROC_INTRINSIC
973 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
974 sym->name, NULL) == FAILURE
975 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
976 return MATCH_ERROR;
978 e = gfc_get_expr ();
979 e->where = gfc_current_locus;
980 e->expr_type = EXPR_NULL;
981 e->ts.type = BT_UNKNOWN;
983 *result = e;
985 return MATCH_YES;
989 /* Match a variable name with an optional initializer. When this
990 subroutine is called, a variable is expected to be parsed next.
991 Depending on what is happening at the moment, updates either the
992 symbol table or the current interface. */
994 static match
995 variable_decl (int elem)
997 char name[GFC_MAX_SYMBOL_LEN + 1];
998 gfc_expr *initializer, *char_len;
999 gfc_array_spec *as;
1000 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1001 gfc_charlen *cl;
1002 locus var_locus;
1003 match m;
1004 try t;
1005 gfc_symbol *sym;
1006 locus old_locus;
1008 initializer = NULL;
1009 as = NULL;
1010 cp_as = NULL;
1011 old_locus = gfc_current_locus;
1013 /* When we get here, we've just matched a list of attributes and
1014 maybe a type and a double colon. The next thing we expect to see
1015 is the name of the symbol. */
1016 m = gfc_match_name (name);
1017 if (m != MATCH_YES)
1018 goto cleanup;
1020 var_locus = gfc_current_locus;
1022 /* Now we could see the optional array spec. or character length. */
1023 m = gfc_match_array_spec (&as);
1024 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1025 cp_as = gfc_copy_array_spec (as);
1026 else if (m == MATCH_ERROR)
1027 goto cleanup;
1029 if (m == MATCH_NO)
1030 as = gfc_copy_array_spec (current_as);
1031 else if (gfc_current_state () == COMP_ENUM)
1033 gfc_error ("Enumerator cannot be array at %C");
1034 gfc_free_enum_history ();
1035 m = MATCH_ERROR;
1036 goto cleanup;
1040 char_len = NULL;
1041 cl = NULL;
1043 if (current_ts.type == BT_CHARACTER)
1045 switch (match_char_length (&char_len))
1047 case MATCH_YES:
1048 cl = gfc_get_charlen ();
1049 cl->next = gfc_current_ns->cl_list;
1050 gfc_current_ns->cl_list = cl;
1052 cl->length = char_len;
1053 break;
1055 /* Non-constant lengths need to be copied after the first
1056 element. */
1057 case MATCH_NO:
1058 if (elem > 1 && current_ts.cl->length
1059 && current_ts.cl->length->expr_type != EXPR_CONSTANT)
1061 cl = gfc_get_charlen ();
1062 cl->next = gfc_current_ns->cl_list;
1063 gfc_current_ns->cl_list = cl;
1064 cl->length = gfc_copy_expr (current_ts.cl->length);
1066 else
1067 cl = current_ts.cl;
1069 break;
1071 case MATCH_ERROR:
1072 goto cleanup;
1076 /* If this symbol has already shown up in a Cray Pointer declaration,
1077 then we want to set the type & bail out. */
1078 if (gfc_option.flag_cray_pointer)
1080 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1081 if (sym != NULL && sym->attr.cray_pointee)
1083 sym->ts.type = current_ts.type;
1084 sym->ts.kind = current_ts.kind;
1085 sym->ts.cl = cl;
1086 sym->ts.derived = current_ts.derived;
1087 m = MATCH_YES;
1089 /* Check to see if we have an array specification. */
1090 if (cp_as != NULL)
1092 if (sym->as != NULL)
1094 gfc_error ("Duplicate array spec for Cray pointee at %C.");
1095 gfc_free_array_spec (cp_as);
1096 m = MATCH_ERROR;
1097 goto cleanup;
1099 else
1101 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1102 gfc_internal_error ("Couldn't set pointee array spec.");
1104 /* Fix the array spec. */
1105 m = gfc_mod_pointee_as (sym->as);
1106 if (m == MATCH_ERROR)
1107 goto cleanup;
1110 goto cleanup;
1112 else
1114 gfc_free_array_spec (cp_as);
1119 /* OK, we've successfully matched the declaration. Now put the
1120 symbol in the current namespace, because it might be used in the
1121 optional initialization expression for this symbol, e.g. this is
1122 perfectly legal:
1124 integer, parameter :: i = huge(i)
1126 This is only true for parameters or variables of a basic type.
1127 For components of derived types, it is not true, so we don't
1128 create a symbol for those yet. If we fail to create the symbol,
1129 bail out. */
1130 if (gfc_current_state () != COMP_DERIVED
1131 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1133 m = MATCH_ERROR;
1134 goto cleanup;
1137 /* In functions that have a RESULT variable defined, the function
1138 name always refers to function calls. Therefore, the name is
1139 not allowed to appear in specification statements. */
1140 if (gfc_current_state () == COMP_FUNCTION
1141 && gfc_current_block () != NULL
1142 && gfc_current_block ()->result != NULL
1143 && gfc_current_block ()->result != gfc_current_block ()
1144 && strcmp (gfc_current_block ()->name, name) == 0)
1146 gfc_error ("Function name '%s' not allowed at %C", name);
1147 m = MATCH_ERROR;
1148 goto cleanup;
1151 /* We allow old-style initializations of the form
1152 integer i /2/, j(4) /3*3, 1/
1153 (if no colon has been seen). These are different from data
1154 statements in that initializers are only allowed to apply to the
1155 variable immediately preceding, i.e.
1156 integer i, j /1, 2/
1157 is not allowed. Therefore we have to do some work manually, that
1158 could otherwise be left to the matchers for DATA statements. */
1160 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1162 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1163 "initialization at %C") == FAILURE)
1164 return MATCH_ERROR;
1166 return match_old_style_init (name);
1169 /* The double colon must be present in order to have initializers.
1170 Otherwise the statement is ambiguous with an assignment statement. */
1171 if (colon_seen)
1173 if (gfc_match (" =>") == MATCH_YES)
1176 if (!current_attr.pointer)
1178 gfc_error ("Initialization at %C isn't for a pointer variable");
1179 m = MATCH_ERROR;
1180 goto cleanup;
1183 m = gfc_match_null (&initializer);
1184 if (m == MATCH_NO)
1186 gfc_error ("Pointer initialization requires a NULL at %C");
1187 m = MATCH_ERROR;
1190 if (gfc_pure (NULL))
1192 gfc_error
1193 ("Initialization of pointer at %C is not allowed in a "
1194 "PURE procedure");
1195 m = MATCH_ERROR;
1198 if (m != MATCH_YES)
1199 goto cleanup;
1201 initializer->ts = current_ts;
1204 else if (gfc_match_char ('=') == MATCH_YES)
1206 if (current_attr.pointer)
1208 gfc_error
1209 ("Pointer initialization at %C requires '=>', not '='");
1210 m = MATCH_ERROR;
1211 goto cleanup;
1214 m = gfc_match_init_expr (&initializer);
1215 if (m == MATCH_NO)
1217 gfc_error ("Expected an initialization expression at %C");
1218 m = MATCH_ERROR;
1221 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1223 gfc_error
1224 ("Initialization of variable at %C is not allowed in a "
1225 "PURE procedure");
1226 m = MATCH_ERROR;
1229 if (m != MATCH_YES)
1230 goto cleanup;
1234 /* Check if we are parsing an enumeration and if the current enumerator
1235 variable has an initializer or not. If it does not have an
1236 initializer, the initialization value of the previous enumerator
1237 (stored in last_initializer) is incremented by 1 and is used to
1238 initialize the current enumerator. */
1239 if (gfc_current_state () == COMP_ENUM)
1241 if (initializer == NULL)
1242 initializer = gfc_enum_initializer (last_initializer, old_locus);
1244 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
1246 gfc_error("ENUMERATOR %L not initialized with integer expression",
1247 &var_locus);
1248 m = MATCH_ERROR;
1249 gfc_free_enum_history ();
1250 goto cleanup;
1253 /* Store this current initializer, for the next enumerator
1254 variable to be parsed. */
1255 last_initializer = initializer;
1258 /* Add the initializer. Note that it is fine if initializer is
1259 NULL here, because we sometimes also need to check if a
1260 declaration *must* have an initialization expression. */
1261 if (gfc_current_state () != COMP_DERIVED)
1262 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1263 else
1265 if (current_ts.type == BT_DERIVED && !current_attr.pointer && !initializer)
1266 initializer = gfc_default_initializer (&current_ts);
1267 t = build_struct (name, cl, &initializer, &as);
1270 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1272 cleanup:
1273 /* Free stuff up and return. */
1274 gfc_free_expr (initializer);
1275 gfc_free_array_spec (as);
1277 return m;
1281 /* Match an extended-f77 kind specification. */
1283 match
1284 gfc_match_old_kind_spec (gfc_typespec * ts)
1286 match m;
1288 if (gfc_match_char ('*') != MATCH_YES)
1289 return MATCH_NO;
1291 m = gfc_match_small_literal_int (&ts->kind);
1292 if (m != MATCH_YES)
1293 return MATCH_ERROR;
1295 /* Massage the kind numbers for complex types. */
1296 if (ts->type == BT_COMPLEX && ts->kind == 8)
1297 ts->kind = 4;
1298 if (ts->type == BT_COMPLEX && ts->kind == 16)
1299 ts->kind = 8;
1301 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1303 gfc_error ("Old-style kind %d not supported for type %s at %C",
1304 ts->kind, gfc_basic_typename (ts->type));
1306 return MATCH_ERROR;
1309 return MATCH_YES;
1313 /* Match a kind specification. Since kinds are generally optional, we
1314 usually return MATCH_NO if something goes wrong. If a "kind="
1315 string is found, then we know we have an error. */
1317 match
1318 gfc_match_kind_spec (gfc_typespec * ts)
1320 locus where;
1321 gfc_expr *e;
1322 match m, n;
1323 const char *msg;
1325 m = MATCH_NO;
1326 e = NULL;
1328 where = gfc_current_locus;
1330 if (gfc_match_char ('(') == MATCH_NO)
1331 return MATCH_NO;
1333 /* Also gobbles optional text. */
1334 if (gfc_match (" kind = ") == MATCH_YES)
1335 m = MATCH_ERROR;
1337 n = gfc_match_init_expr (&e);
1338 if (n == MATCH_NO)
1339 gfc_error ("Expected initialization expression at %C");
1340 if (n != MATCH_YES)
1341 return MATCH_ERROR;
1343 if (e->rank != 0)
1345 gfc_error ("Expected scalar initialization expression at %C");
1346 m = MATCH_ERROR;
1347 goto no_match;
1350 msg = gfc_extract_int (e, &ts->kind);
1351 if (msg != NULL)
1353 gfc_error (msg);
1354 m = MATCH_ERROR;
1355 goto no_match;
1358 gfc_free_expr (e);
1359 e = NULL;
1361 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1363 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1364 gfc_basic_typename (ts->type));
1366 m = MATCH_ERROR;
1367 goto no_match;
1370 if (gfc_match_char (')') != MATCH_YES)
1372 gfc_error ("Missing right paren at %C");
1373 goto no_match;
1376 return MATCH_YES;
1378 no_match:
1379 gfc_free_expr (e);
1380 gfc_current_locus = where;
1381 return m;
1385 /* Match the various kind/length specifications in a CHARACTER
1386 declaration. We don't return MATCH_NO. */
1388 static match
1389 match_char_spec (gfc_typespec * ts)
1391 int i, kind, seen_length;
1392 gfc_charlen *cl;
1393 gfc_expr *len;
1394 match m;
1396 kind = gfc_default_character_kind;
1397 len = NULL;
1398 seen_length = 0;
1400 /* Try the old-style specification first. */
1401 old_char_selector = 0;
1403 m = match_char_length (&len);
1404 if (m != MATCH_NO)
1406 if (m == MATCH_YES)
1407 old_char_selector = 1;
1408 seen_length = 1;
1409 goto done;
1412 m = gfc_match_char ('(');
1413 if (m != MATCH_YES)
1415 m = MATCH_YES; /* character without length is a single char */
1416 goto done;
1419 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ) */
1420 if (gfc_match (" kind =") == MATCH_YES)
1422 m = gfc_match_small_int (&kind);
1423 if (m == MATCH_ERROR)
1424 goto done;
1425 if (m == MATCH_NO)
1426 goto syntax;
1428 if (gfc_match (" , len =") == MATCH_NO)
1429 goto rparen;
1431 m = char_len_param_value (&len);
1432 if (m == MATCH_NO)
1433 goto syntax;
1434 if (m == MATCH_ERROR)
1435 goto done;
1436 seen_length = 1;
1438 goto rparen;
1441 /* Try to match ( LEN = <len-param> ) or ( LEN = <len-param>, KIND = <int> ) */
1442 if (gfc_match (" len =") == MATCH_YES)
1444 m = char_len_param_value (&len);
1445 if (m == MATCH_NO)
1446 goto syntax;
1447 if (m == MATCH_ERROR)
1448 goto done;
1449 seen_length = 1;
1451 if (gfc_match_char (')') == MATCH_YES)
1452 goto done;
1454 if (gfc_match (" , kind =") != MATCH_YES)
1455 goto syntax;
1457 gfc_match_small_int (&kind);
1459 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1461 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1462 return MATCH_YES;
1465 goto rparen;
1468 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ) */
1469 m = char_len_param_value (&len);
1470 if (m == MATCH_NO)
1471 goto syntax;
1472 if (m == MATCH_ERROR)
1473 goto done;
1474 seen_length = 1;
1476 m = gfc_match_char (')');
1477 if (m == MATCH_YES)
1478 goto done;
1480 if (gfc_match_char (',') != MATCH_YES)
1481 goto syntax;
1483 gfc_match (" kind ="); /* Gobble optional text */
1485 m = gfc_match_small_int (&kind);
1486 if (m == MATCH_ERROR)
1487 goto done;
1488 if (m == MATCH_NO)
1489 goto syntax;
1491 rparen:
1492 /* Require a right-paren at this point. */
1493 m = gfc_match_char (')');
1494 if (m == MATCH_YES)
1495 goto done;
1497 syntax:
1498 gfc_error ("Syntax error in CHARACTER declaration at %C");
1499 m = MATCH_ERROR;
1501 done:
1502 if (m == MATCH_YES && gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1504 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1505 m = MATCH_ERROR;
1508 if (m != MATCH_YES)
1510 gfc_free_expr (len);
1511 return m;
1514 /* Do some final massaging of the length values. */
1515 cl = gfc_get_charlen ();
1516 cl->next = gfc_current_ns->cl_list;
1517 gfc_current_ns->cl_list = cl;
1519 if (seen_length == 0)
1520 cl->length = gfc_int_expr (1);
1521 else
1523 if (len == NULL || gfc_extract_int (len, &i) != NULL || i >= 0)
1524 cl->length = len;
1525 else
1527 gfc_free_expr (len);
1528 cl->length = gfc_int_expr (0);
1532 ts->cl = cl;
1533 ts->kind = kind;
1535 return MATCH_YES;
1539 /* Matches a type specification. If successful, sets the ts structure
1540 to the matched specification. This is necessary for FUNCTION and
1541 IMPLICIT statements.
1543 If implicit_flag is nonzero, then we don't check for the optional
1544 kind specification. Not doing so is needed for matching an IMPLICIT
1545 statement correctly. */
1547 static match
1548 match_type_spec (gfc_typespec * ts, int implicit_flag)
1550 char name[GFC_MAX_SYMBOL_LEN + 1];
1551 gfc_symbol *sym;
1552 match m;
1553 int c;
1555 gfc_clear_ts (ts);
1557 if (gfc_match (" byte") == MATCH_YES)
1559 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
1560 == FAILURE)
1561 return MATCH_ERROR;
1563 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
1565 gfc_error ("BYTE type used at %C "
1566 "is not available on the target machine");
1567 return MATCH_ERROR;
1570 ts->type = BT_INTEGER;
1571 ts->kind = 1;
1572 return MATCH_YES;
1575 if (gfc_match (" integer") == MATCH_YES)
1577 ts->type = BT_INTEGER;
1578 ts->kind = gfc_default_integer_kind;
1579 goto get_kind;
1582 if (gfc_match (" character") == MATCH_YES)
1584 ts->type = BT_CHARACTER;
1585 if (implicit_flag == 0)
1586 return match_char_spec (ts);
1587 else
1588 return MATCH_YES;
1591 if (gfc_match (" real") == MATCH_YES)
1593 ts->type = BT_REAL;
1594 ts->kind = gfc_default_real_kind;
1595 goto get_kind;
1598 if (gfc_match (" double precision") == MATCH_YES)
1600 ts->type = BT_REAL;
1601 ts->kind = gfc_default_double_kind;
1602 return MATCH_YES;
1605 if (gfc_match (" complex") == MATCH_YES)
1607 ts->type = BT_COMPLEX;
1608 ts->kind = gfc_default_complex_kind;
1609 goto get_kind;
1612 if (gfc_match (" double complex") == MATCH_YES)
1614 ts->type = BT_COMPLEX;
1615 ts->kind = gfc_default_double_kind;
1616 return MATCH_YES;
1619 if (gfc_match (" logical") == MATCH_YES)
1621 ts->type = BT_LOGICAL;
1622 ts->kind = gfc_default_logical_kind;
1623 goto get_kind;
1626 m = gfc_match (" type ( %n )", name);
1627 if (m != MATCH_YES)
1628 return m;
1630 /* Search for the name but allow the components to be defined later. */
1631 if (gfc_get_ha_symbol (name, &sym))
1633 gfc_error ("Type name '%s' at %C is ambiguous", name);
1634 return MATCH_ERROR;
1637 if (sym->attr.flavor != FL_DERIVED
1638 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
1639 return MATCH_ERROR;
1641 ts->type = BT_DERIVED;
1642 ts->kind = 0;
1643 ts->derived = sym;
1645 return MATCH_YES;
1647 get_kind:
1648 /* For all types except double, derived and character, look for an
1649 optional kind specifier. MATCH_NO is actually OK at this point. */
1650 if (implicit_flag == 1)
1651 return MATCH_YES;
1653 if (gfc_current_form == FORM_FREE)
1655 c = gfc_peek_char();
1656 if (!gfc_is_whitespace(c) && c != '*' && c != '('
1657 && c != ':' && c != ',')
1658 return MATCH_NO;
1661 m = gfc_match_kind_spec (ts);
1662 if (m == MATCH_NO && ts->type != BT_CHARACTER)
1663 m = gfc_match_old_kind_spec (ts);
1665 if (m == MATCH_NO)
1666 m = MATCH_YES; /* No kind specifier found. */
1668 return m;
1672 /* Match an IMPLICIT NONE statement. Actually, this statement is
1673 already matched in parse.c, or we would not end up here in the
1674 first place. So the only thing we need to check, is if there is
1675 trailing garbage. If not, the match is successful. */
1677 match
1678 gfc_match_implicit_none (void)
1681 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
1685 /* Match the letter range(s) of an IMPLICIT statement. */
1687 static match
1688 match_implicit_range (void)
1690 int c, c1, c2, inner;
1691 locus cur_loc;
1693 cur_loc = gfc_current_locus;
1695 gfc_gobble_whitespace ();
1696 c = gfc_next_char ();
1697 if (c != '(')
1699 gfc_error ("Missing character range in IMPLICIT at %C");
1700 goto bad;
1703 inner = 1;
1704 while (inner)
1706 gfc_gobble_whitespace ();
1707 c1 = gfc_next_char ();
1708 if (!ISALPHA (c1))
1709 goto bad;
1711 gfc_gobble_whitespace ();
1712 c = gfc_next_char ();
1714 switch (c)
1716 case ')':
1717 inner = 0; /* Fall through */
1719 case ',':
1720 c2 = c1;
1721 break;
1723 case '-':
1724 gfc_gobble_whitespace ();
1725 c2 = gfc_next_char ();
1726 if (!ISALPHA (c2))
1727 goto bad;
1729 gfc_gobble_whitespace ();
1730 c = gfc_next_char ();
1732 if ((c != ',') && (c != ')'))
1733 goto bad;
1734 if (c == ')')
1735 inner = 0;
1737 break;
1739 default:
1740 goto bad;
1743 if (c1 > c2)
1745 gfc_error ("Letters must be in alphabetic order in "
1746 "IMPLICIT statement at %C");
1747 goto bad;
1750 /* See if we can add the newly matched range to the pending
1751 implicits from this IMPLICIT statement. We do not check for
1752 conflicts with whatever earlier IMPLICIT statements may have
1753 set. This is done when we've successfully finished matching
1754 the current one. */
1755 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
1756 goto bad;
1759 return MATCH_YES;
1761 bad:
1762 gfc_syntax_error (ST_IMPLICIT);
1764 gfc_current_locus = cur_loc;
1765 return MATCH_ERROR;
1769 /* Match an IMPLICIT statement, storing the types for
1770 gfc_set_implicit() if the statement is accepted by the parser.
1771 There is a strange looking, but legal syntactic construction
1772 possible. It looks like:
1774 IMPLICIT INTEGER (a-b) (c-d)
1776 This is legal if "a-b" is a constant expression that happens to
1777 equal one of the legal kinds for integers. The real problem
1778 happens with an implicit specification that looks like:
1780 IMPLICIT INTEGER (a-b)
1782 In this case, a typespec matcher that is "greedy" (as most of the
1783 matchers are) gobbles the character range as a kindspec, leaving
1784 nothing left. We therefore have to go a bit more slowly in the
1785 matching process by inhibiting the kindspec checking during
1786 typespec matching and checking for a kind later. */
1788 match
1789 gfc_match_implicit (void)
1791 gfc_typespec ts;
1792 locus cur_loc;
1793 int c;
1794 match m;
1796 /* We don't allow empty implicit statements. */
1797 if (gfc_match_eos () == MATCH_YES)
1799 gfc_error ("Empty IMPLICIT statement at %C");
1800 return MATCH_ERROR;
1805 /* First cleanup. */
1806 gfc_clear_new_implicit ();
1808 /* A basic type is mandatory here. */
1809 m = match_type_spec (&ts, 1);
1810 if (m == MATCH_ERROR)
1811 goto error;
1812 if (m == MATCH_NO)
1813 goto syntax;
1815 cur_loc = gfc_current_locus;
1816 m = match_implicit_range ();
1818 if (m == MATCH_YES)
1820 /* We may have <TYPE> (<RANGE>). */
1821 gfc_gobble_whitespace ();
1822 c = gfc_next_char ();
1823 if ((c == '\n') || (c == ','))
1825 /* Check for CHARACTER with no length parameter. */
1826 if (ts.type == BT_CHARACTER && !ts.cl)
1828 ts.kind = gfc_default_character_kind;
1829 ts.cl = gfc_get_charlen ();
1830 ts.cl->next = gfc_current_ns->cl_list;
1831 gfc_current_ns->cl_list = ts.cl;
1832 ts.cl->length = gfc_int_expr (1);
1835 /* Record the Successful match. */
1836 if (gfc_merge_new_implicit (&ts) != SUCCESS)
1837 return MATCH_ERROR;
1838 continue;
1841 gfc_current_locus = cur_loc;
1844 /* Discard the (incorrectly) matched range. */
1845 gfc_clear_new_implicit ();
1847 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
1848 if (ts.type == BT_CHARACTER)
1849 m = match_char_spec (&ts);
1850 else
1852 m = gfc_match_kind_spec (&ts);
1853 if (m == MATCH_NO)
1855 m = gfc_match_old_kind_spec (&ts);
1856 if (m == MATCH_ERROR)
1857 goto error;
1858 if (m == MATCH_NO)
1859 goto syntax;
1862 if (m == MATCH_ERROR)
1863 goto error;
1865 m = match_implicit_range ();
1866 if (m == MATCH_ERROR)
1867 goto error;
1868 if (m == MATCH_NO)
1869 goto syntax;
1871 gfc_gobble_whitespace ();
1872 c = gfc_next_char ();
1873 if ((c != '\n') && (c != ','))
1874 goto syntax;
1876 if (gfc_merge_new_implicit (&ts) != SUCCESS)
1877 return MATCH_ERROR;
1879 while (c == ',');
1881 return MATCH_YES;
1883 syntax:
1884 gfc_syntax_error (ST_IMPLICIT);
1886 error:
1887 return MATCH_ERROR;
1891 /* Matches an attribute specification including array specs. If
1892 successful, leaves the variables current_attr and current_as
1893 holding the specification. Also sets the colon_seen variable for
1894 later use by matchers associated with initializations.
1896 This subroutine is a little tricky in the sense that we don't know
1897 if we really have an attr-spec until we hit the double colon.
1898 Until that time, we can only return MATCH_NO. This forces us to
1899 check for duplicate specification at this level. */
1901 static match
1902 match_attr_spec (void)
1905 /* Modifiers that can exist in a type statement. */
1906 typedef enum
1907 { GFC_DECL_BEGIN = 0,
1908 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
1909 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
1910 DECL_PARAMETER, DECL_POINTER, DECL_PRIVATE, DECL_PUBLIC, DECL_SAVE,
1911 DECL_TARGET, DECL_COLON, DECL_NONE,
1912 GFC_DECL_END /* Sentinel */
1914 decl_types;
1916 /* GFC_DECL_END is the sentinel, index starts at 0. */
1917 #define NUM_DECL GFC_DECL_END
1919 static mstring decls[] = {
1920 minit (", allocatable", DECL_ALLOCATABLE),
1921 minit (", dimension", DECL_DIMENSION),
1922 minit (", external", DECL_EXTERNAL),
1923 minit (", intent ( in )", DECL_IN),
1924 minit (", intent ( out )", DECL_OUT),
1925 minit (", intent ( in out )", DECL_INOUT),
1926 minit (", intrinsic", DECL_INTRINSIC),
1927 minit (", optional", DECL_OPTIONAL),
1928 minit (", parameter", DECL_PARAMETER),
1929 minit (", pointer", DECL_POINTER),
1930 minit (", private", DECL_PRIVATE),
1931 minit (", public", DECL_PUBLIC),
1932 minit (", save", DECL_SAVE),
1933 minit (", target", DECL_TARGET),
1934 minit ("::", DECL_COLON),
1935 minit (NULL, DECL_NONE)
1938 locus start, seen_at[NUM_DECL];
1939 int seen[NUM_DECL];
1940 decl_types d;
1941 const char *attr;
1942 match m;
1943 try t;
1945 gfc_clear_attr (&current_attr);
1946 start = gfc_current_locus;
1948 current_as = NULL;
1949 colon_seen = 0;
1951 /* See if we get all of the keywords up to the final double colon. */
1952 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1953 seen[d] = 0;
1955 for (;;)
1957 d = (decl_types) gfc_match_strings (decls);
1958 if (d == DECL_NONE || d == DECL_COLON)
1959 break;
1961 if (gfc_current_state () == COMP_ENUM)
1963 gfc_error ("Enumerator cannot have attributes %C");
1964 return MATCH_ERROR;
1967 seen[d]++;
1968 seen_at[d] = gfc_current_locus;
1970 if (d == DECL_DIMENSION)
1972 m = gfc_match_array_spec (&current_as);
1974 if (m == MATCH_NO)
1976 gfc_error ("Missing dimension specification at %C");
1977 m = MATCH_ERROR;
1980 if (m == MATCH_ERROR)
1981 goto cleanup;
1985 /* If we are parsing an enumeration and have enusured that no other
1986 attributes are present we can now set the parameter attribute. */
1987 if (gfc_current_state () == COMP_ENUM)
1989 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
1990 if (t == FAILURE)
1992 m = MATCH_ERROR;
1993 goto cleanup;
1997 /* No double colon, so assume that we've been looking at something
1998 else the whole time. */
1999 if (d == DECL_NONE)
2001 m = MATCH_NO;
2002 goto cleanup;
2005 /* Since we've seen a double colon, we have to be looking at an
2006 attr-spec. This means that we can now issue errors. */
2007 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2008 if (seen[d] > 1)
2010 switch (d)
2012 case DECL_ALLOCATABLE:
2013 attr = "ALLOCATABLE";
2014 break;
2015 case DECL_DIMENSION:
2016 attr = "DIMENSION";
2017 break;
2018 case DECL_EXTERNAL:
2019 attr = "EXTERNAL";
2020 break;
2021 case DECL_IN:
2022 attr = "INTENT (IN)";
2023 break;
2024 case DECL_OUT:
2025 attr = "INTENT (OUT)";
2026 break;
2027 case DECL_INOUT:
2028 attr = "INTENT (IN OUT)";
2029 break;
2030 case DECL_INTRINSIC:
2031 attr = "INTRINSIC";
2032 break;
2033 case DECL_OPTIONAL:
2034 attr = "OPTIONAL";
2035 break;
2036 case DECL_PARAMETER:
2037 attr = "PARAMETER";
2038 break;
2039 case DECL_POINTER:
2040 attr = "POINTER";
2041 break;
2042 case DECL_PRIVATE:
2043 attr = "PRIVATE";
2044 break;
2045 case DECL_PUBLIC:
2046 attr = "PUBLIC";
2047 break;
2048 case DECL_SAVE:
2049 attr = "SAVE";
2050 break;
2051 case DECL_TARGET:
2052 attr = "TARGET";
2053 break;
2054 default:
2055 attr = NULL; /* This shouldn't happen */
2058 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2059 m = MATCH_ERROR;
2060 goto cleanup;
2063 /* Now that we've dealt with duplicate attributes, add the attributes
2064 to the current attribute. */
2065 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2067 if (seen[d] == 0)
2068 continue;
2070 if (gfc_current_state () == COMP_DERIVED
2071 && d != DECL_DIMENSION && d != DECL_POINTER
2072 && d != DECL_COLON && d != DECL_NONE)
2075 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2076 &seen_at[d]);
2077 m = MATCH_ERROR;
2078 goto cleanup;
2081 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2082 && gfc_current_state () != COMP_MODULE)
2084 if (d == DECL_PRIVATE)
2085 attr = "PRIVATE";
2086 else
2087 attr = "PUBLIC";
2089 gfc_error ("%s attribute at %L is not allowed outside of a MODULE",
2090 attr, &seen_at[d]);
2091 m = MATCH_ERROR;
2092 goto cleanup;
2095 switch (d)
2097 case DECL_ALLOCATABLE:
2098 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
2099 break;
2101 case DECL_DIMENSION:
2102 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
2103 break;
2105 case DECL_EXTERNAL:
2106 t = gfc_add_external (&current_attr, &seen_at[d]);
2107 break;
2109 case DECL_IN:
2110 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
2111 break;
2113 case DECL_OUT:
2114 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
2115 break;
2117 case DECL_INOUT:
2118 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
2119 break;
2121 case DECL_INTRINSIC:
2122 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
2123 break;
2125 case DECL_OPTIONAL:
2126 t = gfc_add_optional (&current_attr, &seen_at[d]);
2127 break;
2129 case DECL_PARAMETER:
2130 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
2131 break;
2133 case DECL_POINTER:
2134 t = gfc_add_pointer (&current_attr, &seen_at[d]);
2135 break;
2137 case DECL_PRIVATE:
2138 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
2139 &seen_at[d]);
2140 break;
2142 case DECL_PUBLIC:
2143 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
2144 &seen_at[d]);
2145 break;
2147 case DECL_SAVE:
2148 t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
2149 break;
2151 case DECL_TARGET:
2152 t = gfc_add_target (&current_attr, &seen_at[d]);
2153 break;
2155 default:
2156 gfc_internal_error ("match_attr_spec(): Bad attribute");
2159 if (t == FAILURE)
2161 m = MATCH_ERROR;
2162 goto cleanup;
2166 colon_seen = 1;
2167 return MATCH_YES;
2169 cleanup:
2170 gfc_current_locus = start;
2171 gfc_free_array_spec (current_as);
2172 current_as = NULL;
2173 return m;
2177 /* Match a data declaration statement. */
2179 match
2180 gfc_match_data_decl (void)
2182 gfc_symbol *sym;
2183 match m;
2184 int elem;
2186 m = match_type_spec (&current_ts, 0);
2187 if (m != MATCH_YES)
2188 return m;
2190 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
2192 sym = gfc_use_derived (current_ts.derived);
2194 if (sym == NULL)
2196 m = MATCH_ERROR;
2197 goto cleanup;
2200 current_ts.derived = sym;
2203 m = match_attr_spec ();
2204 if (m == MATCH_ERROR)
2206 m = MATCH_NO;
2207 goto cleanup;
2210 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
2213 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
2214 goto ok;
2216 gfc_find_symbol (current_ts.derived->name,
2217 current_ts.derived->ns->parent, 1, &sym);
2219 /* Any symbol that we find had better be a type definition
2220 which has its components defined. */
2221 if (sym != NULL && sym->attr.flavor == FL_DERIVED
2222 && current_ts.derived->components != NULL)
2223 goto ok;
2225 /* Now we have an error, which we signal, and then fix up
2226 because the knock-on is plain and simple confusing. */
2227 gfc_error_now ("Derived type at %C has not been previously defined "
2228 "and so cannot appear in a derived type definition.");
2229 current_attr.pointer = 1;
2230 goto ok;
2234 /* If we have an old-style character declaration, and no new-style
2235 attribute specifications, then there a comma is optional between
2236 the type specification and the variable list. */
2237 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
2238 gfc_match_char (',');
2240 /* Give the types/attributes to symbols that follow. Give the element
2241 a number so that repeat character length expressions can be copied. */
2242 elem = 1;
2243 for (;;)
2245 m = variable_decl (elem++);
2246 if (m == MATCH_ERROR)
2247 goto cleanup;
2248 if (m == MATCH_NO)
2249 break;
2251 if (gfc_match_eos () == MATCH_YES)
2252 goto cleanup;
2253 if (gfc_match_char (',') != MATCH_YES)
2254 break;
2257 gfc_error ("Syntax error in data declaration at %C");
2258 m = MATCH_ERROR;
2260 cleanup:
2261 gfc_free_array_spec (current_as);
2262 current_as = NULL;
2263 return m;
2267 /* Match a prefix associated with a function or subroutine
2268 declaration. If the typespec pointer is nonnull, then a typespec
2269 can be matched. Note that if nothing matches, MATCH_YES is
2270 returned (the null string was matched). */
2272 static match
2273 match_prefix (gfc_typespec * ts)
2275 int seen_type;
2277 gfc_clear_attr (&current_attr);
2278 seen_type = 0;
2280 loop:
2281 if (!seen_type && ts != NULL
2282 && match_type_spec (ts, 0) == MATCH_YES
2283 && gfc_match_space () == MATCH_YES)
2286 seen_type = 1;
2287 goto loop;
2290 if (gfc_match ("elemental% ") == MATCH_YES)
2292 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
2293 return MATCH_ERROR;
2295 goto loop;
2298 if (gfc_match ("pure% ") == MATCH_YES)
2300 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
2301 return MATCH_ERROR;
2303 goto loop;
2306 if (gfc_match ("recursive% ") == MATCH_YES)
2308 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
2309 return MATCH_ERROR;
2311 goto loop;
2314 /* At this point, the next item is not a prefix. */
2315 return MATCH_YES;
2319 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
2321 static try
2322 copy_prefix (symbol_attribute * dest, locus * where)
2325 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
2326 return FAILURE;
2328 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
2329 return FAILURE;
2331 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
2332 return FAILURE;
2334 return SUCCESS;
2338 /* Match a formal argument list. */
2340 match
2341 gfc_match_formal_arglist (gfc_symbol * progname, int st_flag, int null_flag)
2343 gfc_formal_arglist *head, *tail, *p, *q;
2344 char name[GFC_MAX_SYMBOL_LEN + 1];
2345 gfc_symbol *sym;
2346 match m;
2348 head = tail = NULL;
2350 if (gfc_match_char ('(') != MATCH_YES)
2352 if (null_flag)
2353 goto ok;
2354 return MATCH_NO;
2357 if (gfc_match_char (')') == MATCH_YES)
2358 goto ok;
2360 for (;;)
2362 if (gfc_match_char ('*') == MATCH_YES)
2363 sym = NULL;
2364 else
2366 m = gfc_match_name (name);
2367 if (m != MATCH_YES)
2368 goto cleanup;
2370 if (gfc_get_symbol (name, NULL, &sym))
2371 goto cleanup;
2374 p = gfc_get_formal_arglist ();
2376 if (head == NULL)
2377 head = tail = p;
2378 else
2380 tail->next = p;
2381 tail = p;
2384 tail->sym = sym;
2386 /* We don't add the VARIABLE flavor because the name could be a
2387 dummy procedure. We don't apply these attributes to formal
2388 arguments of statement functions. */
2389 if (sym != NULL && !st_flag
2390 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
2391 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
2393 m = MATCH_ERROR;
2394 goto cleanup;
2397 /* The name of a program unit can be in a different namespace,
2398 so check for it explicitly. After the statement is accepted,
2399 the name is checked for especially in gfc_get_symbol(). */
2400 if (gfc_new_block != NULL && sym != NULL
2401 && strcmp (sym->name, gfc_new_block->name) == 0)
2403 gfc_error ("Name '%s' at %C is the name of the procedure",
2404 sym->name);
2405 m = MATCH_ERROR;
2406 goto cleanup;
2409 if (gfc_match_char (')') == MATCH_YES)
2410 goto ok;
2412 m = gfc_match_char (',');
2413 if (m != MATCH_YES)
2415 gfc_error ("Unexpected junk in formal argument list at %C");
2416 goto cleanup;
2421 /* Check for duplicate symbols in the formal argument list. */
2422 if (head != NULL)
2424 for (p = head; p->next; p = p->next)
2426 if (p->sym == NULL)
2427 continue;
2429 for (q = p->next; q; q = q->next)
2430 if (p->sym == q->sym)
2432 gfc_error
2433 ("Duplicate symbol '%s' in formal argument list at %C",
2434 p->sym->name);
2436 m = MATCH_ERROR;
2437 goto cleanup;
2442 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL) ==
2443 FAILURE)
2445 m = MATCH_ERROR;
2446 goto cleanup;
2449 return MATCH_YES;
2451 cleanup:
2452 gfc_free_formal_arglist (head);
2453 return m;
2457 /* Match a RESULT specification following a function declaration or
2458 ENTRY statement. Also matches the end-of-statement. */
2460 static match
2461 match_result (gfc_symbol * function, gfc_symbol ** result)
2463 char name[GFC_MAX_SYMBOL_LEN + 1];
2464 gfc_symbol *r;
2465 match m;
2467 if (gfc_match (" result (") != MATCH_YES)
2468 return MATCH_NO;
2470 m = gfc_match_name (name);
2471 if (m != MATCH_YES)
2472 return m;
2474 if (gfc_match (" )%t") != MATCH_YES)
2476 gfc_error ("Unexpected junk following RESULT variable at %C");
2477 return MATCH_ERROR;
2480 if (strcmp (function->name, name) == 0)
2482 gfc_error
2483 ("RESULT variable at %C must be different than function name");
2484 return MATCH_ERROR;
2487 if (gfc_get_symbol (name, NULL, &r))
2488 return MATCH_ERROR;
2490 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
2491 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
2492 return MATCH_ERROR;
2494 *result = r;
2496 return MATCH_YES;
2500 /* Match a function declaration. */
2502 match
2503 gfc_match_function_decl (void)
2505 char name[GFC_MAX_SYMBOL_LEN + 1];
2506 gfc_symbol *sym, *result;
2507 locus old_loc;
2508 match m;
2510 if (gfc_current_state () != COMP_NONE
2511 && gfc_current_state () != COMP_INTERFACE
2512 && gfc_current_state () != COMP_CONTAINS)
2513 return MATCH_NO;
2515 gfc_clear_ts (&current_ts);
2517 old_loc = gfc_current_locus;
2519 m = match_prefix (&current_ts);
2520 if (m != MATCH_YES)
2522 gfc_current_locus = old_loc;
2523 return m;
2526 if (gfc_match ("function% %n", name) != MATCH_YES)
2528 gfc_current_locus = old_loc;
2529 return MATCH_NO;
2532 if (get_proc_name (name, &sym))
2533 return MATCH_ERROR;
2534 gfc_new_block = sym;
2536 m = gfc_match_formal_arglist (sym, 0, 0);
2537 if (m == MATCH_NO)
2538 gfc_error ("Expected formal argument list in function definition at %C");
2539 else if (m == MATCH_ERROR)
2540 goto cleanup;
2542 result = NULL;
2544 if (gfc_match_eos () != MATCH_YES)
2546 /* See if a result variable is present. */
2547 m = match_result (sym, &result);
2548 if (m == MATCH_NO)
2549 gfc_error ("Unexpected junk after function declaration at %C");
2551 if (m != MATCH_YES)
2553 m = MATCH_ERROR;
2554 goto cleanup;
2558 /* Make changes to the symbol. */
2559 m = MATCH_ERROR;
2561 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2562 goto cleanup;
2564 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
2565 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2566 goto cleanup;
2568 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN)
2570 gfc_error ("Function '%s' at %C already has a type of %s", name,
2571 gfc_basic_typename (sym->ts.type));
2572 goto cleanup;
2575 if (result == NULL)
2577 sym->ts = current_ts;
2578 sym->result = sym;
2580 else
2582 result->ts = current_ts;
2583 sym->result = result;
2586 return MATCH_YES;
2588 cleanup:
2589 gfc_current_locus = old_loc;
2590 return m;
2594 /* Match an ENTRY statement. */
2596 match
2597 gfc_match_entry (void)
2599 gfc_symbol *proc;
2600 gfc_symbol *result;
2601 gfc_symbol *entry;
2602 char name[GFC_MAX_SYMBOL_LEN + 1];
2603 gfc_compile_state state;
2604 match m;
2605 gfc_entry_list *el;
2607 m = gfc_match_name (name);
2608 if (m != MATCH_YES)
2609 return m;
2611 state = gfc_current_state ();
2612 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
2614 switch (state)
2616 case COMP_PROGRAM:
2617 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
2618 break;
2619 case COMP_MODULE:
2620 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
2621 break;
2622 case COMP_BLOCK_DATA:
2623 gfc_error
2624 ("ENTRY statement at %C cannot appear within a BLOCK DATA");
2625 break;
2626 case COMP_INTERFACE:
2627 gfc_error
2628 ("ENTRY statement at %C cannot appear within an INTERFACE");
2629 break;
2630 case COMP_DERIVED:
2631 gfc_error
2632 ("ENTRY statement at %C cannot appear "
2633 "within a DERIVED TYPE block");
2634 break;
2635 case COMP_IF:
2636 gfc_error
2637 ("ENTRY statement at %C cannot appear within an IF-THEN block");
2638 break;
2639 case COMP_DO:
2640 gfc_error
2641 ("ENTRY statement at %C cannot appear within a DO block");
2642 break;
2643 case COMP_SELECT:
2644 gfc_error
2645 ("ENTRY statement at %C cannot appear within a SELECT block");
2646 break;
2647 case COMP_FORALL:
2648 gfc_error
2649 ("ENTRY statement at %C cannot appear within a FORALL block");
2650 break;
2651 case COMP_WHERE:
2652 gfc_error
2653 ("ENTRY statement at %C cannot appear within a WHERE block");
2654 break;
2655 case COMP_CONTAINS:
2656 gfc_error
2657 ("ENTRY statement at %C cannot appear "
2658 "within a contained subprogram");
2659 break;
2660 default:
2661 gfc_internal_error ("gfc_match_entry(): Bad state");
2663 return MATCH_ERROR;
2666 if (gfc_current_ns->parent != NULL
2667 && gfc_current_ns->parent->proc_name
2668 && gfc_current_ns->parent->proc_name->attr.flavor != FL_MODULE)
2670 gfc_error("ENTRY statement at %C cannot appear in a "
2671 "contained procedure");
2672 return MATCH_ERROR;
2675 if (get_proc_name (name, &entry))
2676 return MATCH_ERROR;
2678 proc = gfc_current_block ();
2680 if (state == COMP_SUBROUTINE)
2682 /* An entry in a subroutine. */
2683 m = gfc_match_formal_arglist (entry, 0, 1);
2684 if (m != MATCH_YES)
2685 return MATCH_ERROR;
2687 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
2688 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
2689 return MATCH_ERROR;
2691 else
2693 /* An entry in a function. */
2694 m = gfc_match_formal_arglist (entry, 0, 1);
2695 if (m != MATCH_YES)
2696 return MATCH_ERROR;
2698 result = NULL;
2700 if (gfc_match_eos () == MATCH_YES)
2702 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
2703 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
2704 return MATCH_ERROR;
2706 entry->result = entry;
2708 else
2710 m = match_result (proc, &result);
2711 if (m == MATCH_NO)
2712 gfc_syntax_error (ST_ENTRY);
2713 if (m != MATCH_YES)
2714 return MATCH_ERROR;
2716 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
2717 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
2718 || gfc_add_function (&entry->attr, result->name,
2719 NULL) == FAILURE)
2720 return MATCH_ERROR;
2722 entry->result = result;
2725 if (proc->attr.recursive && result == NULL)
2727 gfc_error ("RESULT attribute required in ENTRY statement at %C");
2728 return MATCH_ERROR;
2732 if (gfc_match_eos () != MATCH_YES)
2734 gfc_syntax_error (ST_ENTRY);
2735 return MATCH_ERROR;
2738 entry->attr.recursive = proc->attr.recursive;
2739 entry->attr.elemental = proc->attr.elemental;
2740 entry->attr.pure = proc->attr.pure;
2742 el = gfc_get_entry_list ();
2743 el->sym = entry;
2744 el->next = gfc_current_ns->entries;
2745 gfc_current_ns->entries = el;
2746 if (el->next)
2747 el->id = el->next->id + 1;
2748 else
2749 el->id = 1;
2751 new_st.op = EXEC_ENTRY;
2752 new_st.ext.entry = el;
2754 return MATCH_YES;
2758 /* Match a subroutine statement, including optional prefixes. */
2760 match
2761 gfc_match_subroutine (void)
2763 char name[GFC_MAX_SYMBOL_LEN + 1];
2764 gfc_symbol *sym;
2765 match m;
2767 if (gfc_current_state () != COMP_NONE
2768 && gfc_current_state () != COMP_INTERFACE
2769 && gfc_current_state () != COMP_CONTAINS)
2770 return MATCH_NO;
2772 m = match_prefix (NULL);
2773 if (m != MATCH_YES)
2774 return m;
2776 m = gfc_match ("subroutine% %n", name);
2777 if (m != MATCH_YES)
2778 return m;
2780 if (get_proc_name (name, &sym))
2781 return MATCH_ERROR;
2782 gfc_new_block = sym;
2784 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2785 return MATCH_ERROR;
2787 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
2788 return MATCH_ERROR;
2790 if (gfc_match_eos () != MATCH_YES)
2792 gfc_syntax_error (ST_SUBROUTINE);
2793 return MATCH_ERROR;
2796 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2797 return MATCH_ERROR;
2799 return MATCH_YES;
2803 /* Return nonzero if we're currently compiling a contained procedure. */
2805 static int
2806 contained_procedure (void)
2808 gfc_state_data *s;
2810 for (s=gfc_state_stack; s; s=s->previous)
2811 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
2812 && s->previous != NULL
2813 && s->previous->state == COMP_CONTAINS)
2814 return 1;
2816 return 0;
2819 /* Set the kind of each enumerator. The kind is selected such that it is
2820 interoperable with the corresponding C enumeration type, making
2821 sure that -fshort-enums is honored. */
2823 static void
2824 set_enum_kind(void)
2826 enumerator_history *current_history = NULL;
2827 int kind;
2828 int i;
2830 if (max_enum == NULL || enum_history == NULL)
2831 return;
2833 if (!gfc_option.fshort_enums)
2834 return;
2836 i = 0;
2839 kind = gfc_integer_kinds[i++].kind;
2841 while (kind < gfc_c_int_kind
2842 && gfc_check_integer_range (max_enum->initializer->value.integer,
2843 kind) != ARITH_OK);
2845 current_history = enum_history;
2846 while (current_history != NULL)
2848 current_history->sym->ts.kind = kind;
2849 current_history = current_history->next;
2853 /* Match any of the various end-block statements. Returns the type of
2854 END to the caller. The END INTERFACE, END IF, END DO and END
2855 SELECT statements cannot be replaced by a single END statement. */
2857 match
2858 gfc_match_end (gfc_statement * st)
2860 char name[GFC_MAX_SYMBOL_LEN + 1];
2861 gfc_compile_state state;
2862 locus old_loc;
2863 const char *block_name;
2864 const char *target;
2865 int eos_ok;
2866 match m;
2868 old_loc = gfc_current_locus;
2869 if (gfc_match ("end") != MATCH_YES)
2870 return MATCH_NO;
2872 state = gfc_current_state ();
2873 block_name =
2874 gfc_current_block () == NULL ? NULL : gfc_current_block ()->name;
2876 if (state == COMP_CONTAINS)
2878 state = gfc_state_stack->previous->state;
2879 block_name = gfc_state_stack->previous->sym == NULL ? NULL
2880 : gfc_state_stack->previous->sym->name;
2883 switch (state)
2885 case COMP_NONE:
2886 case COMP_PROGRAM:
2887 *st = ST_END_PROGRAM;
2888 target = " program";
2889 eos_ok = 1;
2890 break;
2892 case COMP_SUBROUTINE:
2893 *st = ST_END_SUBROUTINE;
2894 target = " subroutine";
2895 eos_ok = !contained_procedure ();
2896 break;
2898 case COMP_FUNCTION:
2899 *st = ST_END_FUNCTION;
2900 target = " function";
2901 eos_ok = !contained_procedure ();
2902 break;
2904 case COMP_BLOCK_DATA:
2905 *st = ST_END_BLOCK_DATA;
2906 target = " block data";
2907 eos_ok = 1;
2908 break;
2910 case COMP_MODULE:
2911 *st = ST_END_MODULE;
2912 target = " module";
2913 eos_ok = 1;
2914 break;
2916 case COMP_INTERFACE:
2917 *st = ST_END_INTERFACE;
2918 target = " interface";
2919 eos_ok = 0;
2920 break;
2922 case COMP_DERIVED:
2923 *st = ST_END_TYPE;
2924 target = " type";
2925 eos_ok = 0;
2926 break;
2928 case COMP_IF:
2929 *st = ST_ENDIF;
2930 target = " if";
2931 eos_ok = 0;
2932 break;
2934 case COMP_DO:
2935 *st = ST_ENDDO;
2936 target = " do";
2937 eos_ok = 0;
2938 break;
2940 case COMP_SELECT:
2941 *st = ST_END_SELECT;
2942 target = " select";
2943 eos_ok = 0;
2944 break;
2946 case COMP_FORALL:
2947 *st = ST_END_FORALL;
2948 target = " forall";
2949 eos_ok = 0;
2950 break;
2952 case COMP_WHERE:
2953 *st = ST_END_WHERE;
2954 target = " where";
2955 eos_ok = 0;
2956 break;
2958 case COMP_ENUM:
2959 *st = ST_END_ENUM;
2960 target = " enum";
2961 eos_ok = 0;
2962 last_initializer = NULL;
2963 set_enum_kind ();
2964 gfc_free_enum_history ();
2965 break;
2967 default:
2968 gfc_error ("Unexpected END statement at %C");
2969 goto cleanup;
2972 if (gfc_match_eos () == MATCH_YES)
2974 if (!eos_ok)
2976 /* We would have required END [something] */
2977 gfc_error ("%s statement expected at %L",
2978 gfc_ascii_statement (*st), &old_loc);
2979 goto cleanup;
2982 return MATCH_YES;
2985 /* Verify that we've got the sort of end-block that we're expecting. */
2986 if (gfc_match (target) != MATCH_YES)
2988 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
2989 goto cleanup;
2992 /* If we're at the end, make sure a block name wasn't required. */
2993 if (gfc_match_eos () == MATCH_YES)
2996 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT)
2997 return MATCH_YES;
2999 if (gfc_current_block () == NULL)
3000 return MATCH_YES;
3002 gfc_error ("Expected block name of '%s' in %s statement at %C",
3003 block_name, gfc_ascii_statement (*st));
3005 return MATCH_ERROR;
3008 /* END INTERFACE has a special handler for its several possible endings. */
3009 if (*st == ST_END_INTERFACE)
3010 return gfc_match_end_interface ();
3012 /* We haven't hit the end of statement, so what is left must be an end-name. */
3013 m = gfc_match_space ();
3014 if (m == MATCH_YES)
3015 m = gfc_match_name (name);
3017 if (m == MATCH_NO)
3018 gfc_error ("Expected terminating name at %C");
3019 if (m != MATCH_YES)
3020 goto cleanup;
3022 if (block_name == NULL)
3023 goto syntax;
3025 if (strcmp (name, block_name) != 0)
3027 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
3028 gfc_ascii_statement (*st));
3029 goto cleanup;
3032 if (gfc_match_eos () == MATCH_YES)
3033 return MATCH_YES;
3035 syntax:
3036 gfc_syntax_error (*st);
3038 cleanup:
3039 gfc_current_locus = old_loc;
3040 return MATCH_ERROR;
3045 /***************** Attribute declaration statements ****************/
3047 /* Set the attribute of a single variable. */
3049 static match
3050 attr_decl1 (void)
3052 char name[GFC_MAX_SYMBOL_LEN + 1];
3053 gfc_array_spec *as;
3054 gfc_symbol *sym;
3055 locus var_locus;
3056 match m;
3058 as = NULL;
3060 m = gfc_match_name (name);
3061 if (m != MATCH_YES)
3062 goto cleanup;
3064 if (find_special (name, &sym))
3065 return MATCH_ERROR;
3067 var_locus = gfc_current_locus;
3069 /* Deal with possible array specification for certain attributes. */
3070 if (current_attr.dimension
3071 || current_attr.allocatable
3072 || current_attr.pointer
3073 || current_attr.target)
3075 m = gfc_match_array_spec (&as);
3076 if (m == MATCH_ERROR)
3077 goto cleanup;
3079 if (current_attr.dimension && m == MATCH_NO)
3081 gfc_error
3082 ("Missing array specification at %L in DIMENSION statement",
3083 &var_locus);
3084 m = MATCH_ERROR;
3085 goto cleanup;
3088 if ((current_attr.allocatable || current_attr.pointer)
3089 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
3091 gfc_error ("Array specification must be deferred at %L",
3092 &var_locus);
3093 m = MATCH_ERROR;
3094 goto cleanup;
3098 /* Update symbol table. DIMENSION attribute is set in gfc_set_array_spec(). */
3099 if (current_attr.dimension == 0
3100 && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
3102 m = MATCH_ERROR;
3103 goto cleanup;
3106 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
3108 m = MATCH_ERROR;
3109 goto cleanup;
3112 if (sym->attr.cray_pointee && sym->as != NULL)
3114 /* Fix the array spec. */
3115 m = gfc_mod_pointee_as (sym->as);
3116 if (m == MATCH_ERROR)
3117 goto cleanup;
3120 if ((current_attr.external || current_attr.intrinsic)
3121 && sym->attr.flavor != FL_PROCEDURE
3122 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
3124 m = MATCH_ERROR;
3125 goto cleanup;
3128 return MATCH_YES;
3130 cleanup:
3131 gfc_free_array_spec (as);
3132 return m;
3136 /* Generic attribute declaration subroutine. Used for attributes that
3137 just have a list of names. */
3139 static match
3140 attr_decl (void)
3142 match m;
3144 /* Gobble the optional double colon, by simply ignoring the result
3145 of gfc_match(). */
3146 gfc_match (" ::");
3148 for (;;)
3150 m = attr_decl1 ();
3151 if (m != MATCH_YES)
3152 break;
3154 if (gfc_match_eos () == MATCH_YES)
3156 m = MATCH_YES;
3157 break;
3160 if (gfc_match_char (',') != MATCH_YES)
3162 gfc_error ("Unexpected character in variable list at %C");
3163 m = MATCH_ERROR;
3164 break;
3168 return m;
3172 /* This routine matches Cray Pointer declarations of the form:
3173 pointer ( <pointer>, <pointee> )
3175 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
3176 The pointer, if already declared, should be an integer. Otherwise, we
3177 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
3178 be either a scalar, or an array declaration. No space is allocated for
3179 the pointee. For the statement
3180 pointer (ipt, ar(10))
3181 any subsequent uses of ar will be translated (in C-notation) as
3182 ar(i) => ((<type> *) ipt)(i)
3183 After gimplification, pointee variable will disappear in the code. */
3185 static match
3186 cray_pointer_decl (void)
3188 match m;
3189 gfc_array_spec *as;
3190 gfc_symbol *cptr; /* Pointer symbol. */
3191 gfc_symbol *cpte; /* Pointee symbol. */
3192 locus var_locus;
3193 bool done = false;
3195 while (!done)
3197 if (gfc_match_char ('(') != MATCH_YES)
3199 gfc_error ("Expected '(' at %C");
3200 return MATCH_ERROR;
3203 /* Match pointer. */
3204 var_locus = gfc_current_locus;
3205 gfc_clear_attr (&current_attr);
3206 gfc_add_cray_pointer (&current_attr, &var_locus);
3207 current_ts.type = BT_INTEGER;
3208 current_ts.kind = gfc_index_integer_kind;
3210 m = gfc_match_symbol (&cptr, 0);
3211 if (m != MATCH_YES)
3213 gfc_error ("Expected variable name at %C");
3214 return m;
3217 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
3218 return MATCH_ERROR;
3220 gfc_set_sym_referenced (cptr);
3222 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
3224 cptr->ts.type = BT_INTEGER;
3225 cptr->ts.kind = gfc_index_integer_kind;
3227 else if (cptr->ts.type != BT_INTEGER)
3229 gfc_error ("Cray pointer at %C must be an integer.");
3230 return MATCH_ERROR;
3232 else if (cptr->ts.kind < gfc_index_integer_kind)
3233 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
3234 " memory addresses require %d bytes.",
3235 cptr->ts.kind,
3236 gfc_index_integer_kind);
3238 if (gfc_match_char (',') != MATCH_YES)
3240 gfc_error ("Expected \",\" at %C");
3241 return MATCH_ERROR;
3244 /* Match Pointee. */
3245 var_locus = gfc_current_locus;
3246 gfc_clear_attr (&current_attr);
3247 gfc_add_cray_pointee (&current_attr, &var_locus);
3248 current_ts.type = BT_UNKNOWN;
3249 current_ts.kind = 0;
3251 m = gfc_match_symbol (&cpte, 0);
3252 if (m != MATCH_YES)
3254 gfc_error ("Expected variable name at %C");
3255 return m;
3258 /* Check for an optional array spec. */
3259 m = gfc_match_array_spec (&as);
3260 if (m == MATCH_ERROR)
3262 gfc_free_array_spec (as);
3263 return m;
3265 else if (m == MATCH_NO)
3267 gfc_free_array_spec (as);
3268 as = NULL;
3271 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
3272 return MATCH_ERROR;
3274 gfc_set_sym_referenced (cpte);
3276 if (cpte->as == NULL)
3278 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
3279 gfc_internal_error ("Couldn't set Cray pointee array spec.");
3281 else if (as != NULL)
3283 gfc_error ("Duplicate array spec for Cray pointee at %C.");
3284 gfc_free_array_spec (as);
3285 return MATCH_ERROR;
3288 as = NULL;
3290 if (cpte->as != NULL)
3292 /* Fix array spec. */
3293 m = gfc_mod_pointee_as (cpte->as);
3294 if (m == MATCH_ERROR)
3295 return m;
3298 /* Point the Pointee at the Pointer. */
3299 cpte->cp_pointer = cptr;
3301 if (gfc_match_char (')') != MATCH_YES)
3303 gfc_error ("Expected \")\" at %C");
3304 return MATCH_ERROR;
3306 m = gfc_match_char (',');
3307 if (m != MATCH_YES)
3308 done = true; /* Stop searching for more declarations. */
3312 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
3313 || gfc_match_eos () != MATCH_YES)
3315 gfc_error ("Expected \",\" or end of statement at %C");
3316 return MATCH_ERROR;
3318 return MATCH_YES;
3322 match
3323 gfc_match_external (void)
3326 gfc_clear_attr (&current_attr);
3327 gfc_add_external (&current_attr, NULL);
3329 return attr_decl ();
3334 match
3335 gfc_match_intent (void)
3337 sym_intent intent;
3339 intent = match_intent_spec ();
3340 if (intent == INTENT_UNKNOWN)
3341 return MATCH_ERROR;
3343 gfc_clear_attr (&current_attr);
3344 gfc_add_intent (&current_attr, intent, NULL); /* Can't fail */
3346 return attr_decl ();
3350 match
3351 gfc_match_intrinsic (void)
3354 gfc_clear_attr (&current_attr);
3355 gfc_add_intrinsic (&current_attr, NULL);
3357 return attr_decl ();
3361 match
3362 gfc_match_optional (void)
3365 gfc_clear_attr (&current_attr);
3366 gfc_add_optional (&current_attr, NULL);
3368 return attr_decl ();
3372 match
3373 gfc_match_pointer (void)
3375 gfc_gobble_whitespace ();
3376 if (gfc_peek_char () == '(')
3378 if (!gfc_option.flag_cray_pointer)
3380 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer"
3381 " flag.");
3382 return MATCH_ERROR;
3384 return cray_pointer_decl ();
3386 else
3388 gfc_clear_attr (&current_attr);
3389 gfc_add_pointer (&current_attr, NULL);
3391 return attr_decl ();
3396 match
3397 gfc_match_allocatable (void)
3400 gfc_clear_attr (&current_attr);
3401 gfc_add_allocatable (&current_attr, NULL);
3403 return attr_decl ();
3407 match
3408 gfc_match_dimension (void)
3411 gfc_clear_attr (&current_attr);
3412 gfc_add_dimension (&current_attr, NULL, NULL);
3414 return attr_decl ();
3418 match
3419 gfc_match_target (void)
3422 gfc_clear_attr (&current_attr);
3423 gfc_add_target (&current_attr, NULL);
3425 return attr_decl ();
3429 /* Match the list of entities being specified in a PUBLIC or PRIVATE
3430 statement. */
3432 static match
3433 access_attr_decl (gfc_statement st)
3435 char name[GFC_MAX_SYMBOL_LEN + 1];
3436 interface_type type;
3437 gfc_user_op *uop;
3438 gfc_symbol *sym;
3439 gfc_intrinsic_op operator;
3440 match m;
3442 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
3443 goto done;
3445 for (;;)
3447 m = gfc_match_generic_spec (&type, name, &operator);
3448 if (m == MATCH_NO)
3449 goto syntax;
3450 if (m == MATCH_ERROR)
3451 return MATCH_ERROR;
3453 switch (type)
3455 case INTERFACE_NAMELESS:
3456 goto syntax;
3458 case INTERFACE_GENERIC:
3459 if (gfc_get_symbol (name, NULL, &sym))
3460 goto done;
3462 if (gfc_add_access (&sym->attr,
3463 (st ==
3464 ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE,
3465 sym->name, NULL) == FAILURE)
3466 return MATCH_ERROR;
3468 break;
3470 case INTERFACE_INTRINSIC_OP:
3471 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
3473 gfc_current_ns->operator_access[operator] =
3474 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3476 else
3478 gfc_error ("Access specification of the %s operator at %C has "
3479 "already been specified", gfc_op2string (operator));
3480 goto done;
3483 break;
3485 case INTERFACE_USER_OP:
3486 uop = gfc_get_uop (name);
3488 if (uop->access == ACCESS_UNKNOWN)
3490 uop->access =
3491 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3493 else
3495 gfc_error
3496 ("Access specification of the .%s. operator at %C has "
3497 "already been specified", sym->name);
3498 goto done;
3501 break;
3504 if (gfc_match_char (',') == MATCH_NO)
3505 break;
3508 if (gfc_match_eos () != MATCH_YES)
3509 goto syntax;
3510 return MATCH_YES;
3512 syntax:
3513 gfc_syntax_error (st);
3515 done:
3516 return MATCH_ERROR;
3520 /* The PRIVATE statement is a bit weird in that it can be a attribute
3521 declaration, but also works as a standlone statement inside of a
3522 type declaration or a module. */
3524 match
3525 gfc_match_private (gfc_statement * st)
3528 if (gfc_match ("private") != MATCH_YES)
3529 return MATCH_NO;
3531 if (gfc_current_state () == COMP_DERIVED)
3533 if (gfc_match_eos () == MATCH_YES)
3535 *st = ST_PRIVATE;
3536 return MATCH_YES;
3539 gfc_syntax_error (ST_PRIVATE);
3540 return MATCH_ERROR;
3543 if (gfc_match_eos () == MATCH_YES)
3545 *st = ST_PRIVATE;
3546 return MATCH_YES;
3549 *st = ST_ATTR_DECL;
3550 return access_attr_decl (ST_PRIVATE);
3554 match
3555 gfc_match_public (gfc_statement * st)
3558 if (gfc_match ("public") != MATCH_YES)
3559 return MATCH_NO;
3561 if (gfc_match_eos () == MATCH_YES)
3563 *st = ST_PUBLIC;
3564 return MATCH_YES;
3567 *st = ST_ATTR_DECL;
3568 return access_attr_decl (ST_PUBLIC);
3572 /* Workhorse for gfc_match_parameter. */
3574 static match
3575 do_parm (void)
3577 gfc_symbol *sym;
3578 gfc_expr *init;
3579 match m;
3581 m = gfc_match_symbol (&sym, 0);
3582 if (m == MATCH_NO)
3583 gfc_error ("Expected variable name at %C in PARAMETER statement");
3585 if (m != MATCH_YES)
3586 return m;
3588 if (gfc_match_char ('=') == MATCH_NO)
3590 gfc_error ("Expected = sign in PARAMETER statement at %C");
3591 return MATCH_ERROR;
3594 m = gfc_match_init_expr (&init);
3595 if (m == MATCH_NO)
3596 gfc_error ("Expected expression at %C in PARAMETER statement");
3597 if (m != MATCH_YES)
3598 return m;
3600 if (sym->ts.type == BT_UNKNOWN
3601 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
3603 m = MATCH_ERROR;
3604 goto cleanup;
3607 if (gfc_check_assign_symbol (sym, init) == FAILURE
3608 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
3610 m = MATCH_ERROR;
3611 goto cleanup;
3614 if (sym->ts.type == BT_CHARACTER
3615 && sym->ts.cl != NULL
3616 && sym->ts.cl->length != NULL
3617 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
3618 && init->expr_type == EXPR_CONSTANT
3619 && init->ts.type == BT_CHARACTER
3620 && init->ts.kind == 1)
3621 gfc_set_constant_character_len (
3622 mpz_get_si (sym->ts.cl->length->value.integer), init);
3624 sym->value = init;
3625 return MATCH_YES;
3627 cleanup:
3628 gfc_free_expr (init);
3629 return m;
3633 /* Match a parameter statement, with the weird syntax that these have. */
3635 match
3636 gfc_match_parameter (void)
3638 match m;
3640 if (gfc_match_char ('(') == MATCH_NO)
3641 return MATCH_NO;
3643 for (;;)
3645 m = do_parm ();
3646 if (m != MATCH_YES)
3647 break;
3649 if (gfc_match (" )%t") == MATCH_YES)
3650 break;
3652 if (gfc_match_char (',') != MATCH_YES)
3654 gfc_error ("Unexpected characters in PARAMETER statement at %C");
3655 m = MATCH_ERROR;
3656 break;
3660 return m;
3664 /* Save statements have a special syntax. */
3666 match
3667 gfc_match_save (void)
3669 char n[GFC_MAX_SYMBOL_LEN+1];
3670 gfc_common_head *c;
3671 gfc_symbol *sym;
3672 match m;
3674 if (gfc_match_eos () == MATCH_YES)
3676 if (gfc_current_ns->seen_save)
3678 if (gfc_notify_std (GFC_STD_LEGACY,
3679 "Blanket SAVE statement at %C follows previous "
3680 "SAVE statement")
3681 == FAILURE)
3682 return MATCH_ERROR;
3685 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
3686 return MATCH_YES;
3689 if (gfc_current_ns->save_all)
3691 if (gfc_notify_std (GFC_STD_LEGACY,
3692 "SAVE statement at %C follows blanket SAVE statement")
3693 == FAILURE)
3694 return MATCH_ERROR;
3697 gfc_match (" ::");
3699 for (;;)
3701 m = gfc_match_symbol (&sym, 0);
3702 switch (m)
3704 case MATCH_YES:
3705 if (gfc_add_save (&sym->attr, sym->name,
3706 &gfc_current_locus) == FAILURE)
3707 return MATCH_ERROR;
3708 goto next_item;
3710 case MATCH_NO:
3711 break;
3713 case MATCH_ERROR:
3714 return MATCH_ERROR;
3717 m = gfc_match (" / %n /", &n);
3718 if (m == MATCH_ERROR)
3719 return MATCH_ERROR;
3720 if (m == MATCH_NO)
3721 goto syntax;
3723 c = gfc_get_common (n, 0);
3724 c->saved = 1;
3726 gfc_current_ns->seen_save = 1;
3728 next_item:
3729 if (gfc_match_eos () == MATCH_YES)
3730 break;
3731 if (gfc_match_char (',') != MATCH_YES)
3732 goto syntax;
3735 return MATCH_YES;
3737 syntax:
3738 gfc_error ("Syntax error in SAVE statement at %C");
3739 return MATCH_ERROR;
3743 /* Match a module procedure statement. Note that we have to modify
3744 symbols in the parent's namespace because the current one was there
3745 to receive symbols that are in an interface's formal argument list. */
3747 match
3748 gfc_match_modproc (void)
3750 char name[GFC_MAX_SYMBOL_LEN + 1];
3751 gfc_symbol *sym;
3752 match m;
3754 if (gfc_state_stack->state != COMP_INTERFACE
3755 || gfc_state_stack->previous == NULL
3756 || current_interface.type == INTERFACE_NAMELESS)
3758 gfc_error
3759 ("MODULE PROCEDURE at %C must be in a generic module interface");
3760 return MATCH_ERROR;
3763 for (;;)
3765 m = gfc_match_name (name);
3766 if (m == MATCH_NO)
3767 goto syntax;
3768 if (m != MATCH_YES)
3769 return MATCH_ERROR;
3771 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
3772 return MATCH_ERROR;
3774 if (sym->attr.proc != PROC_MODULE
3775 && gfc_add_procedure (&sym->attr, PROC_MODULE,
3776 sym->name, NULL) == FAILURE)
3777 return MATCH_ERROR;
3779 if (gfc_add_interface (sym) == FAILURE)
3780 return MATCH_ERROR;
3782 if (gfc_match_eos () == MATCH_YES)
3783 break;
3784 if (gfc_match_char (',') != MATCH_YES)
3785 goto syntax;
3788 return MATCH_YES;
3790 syntax:
3791 gfc_syntax_error (ST_MODULE_PROC);
3792 return MATCH_ERROR;
3796 /* Match the beginning of a derived type declaration. If a type name
3797 was the result of a function, then it is possible to have a symbol
3798 already to be known as a derived type yet have no components. */
3800 match
3801 gfc_match_derived_decl (void)
3803 char name[GFC_MAX_SYMBOL_LEN + 1];
3804 symbol_attribute attr;
3805 gfc_symbol *sym;
3806 match m;
3808 if (gfc_current_state () == COMP_DERIVED)
3809 return MATCH_NO;
3811 gfc_clear_attr (&attr);
3813 loop:
3814 if (gfc_match (" , private") == MATCH_YES)
3816 if (gfc_find_state (COMP_MODULE) == FAILURE)
3818 gfc_error
3819 ("Derived type at %C can only be PRIVATE within a MODULE");
3820 return MATCH_ERROR;
3823 if (gfc_add_access (&attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
3824 return MATCH_ERROR;
3825 goto loop;
3828 if (gfc_match (" , public") == MATCH_YES)
3830 if (gfc_find_state (COMP_MODULE) == FAILURE)
3832 gfc_error ("Derived type at %C can only be PUBLIC within a MODULE");
3833 return MATCH_ERROR;
3836 if (gfc_add_access (&attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
3837 return MATCH_ERROR;
3838 goto loop;
3841 if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
3843 gfc_error ("Expected :: in TYPE definition at %C");
3844 return MATCH_ERROR;
3847 m = gfc_match (" %n%t", name);
3848 if (m != MATCH_YES)
3849 return m;
3851 /* Make sure the name isn't the name of an intrinsic type. The
3852 'double precision' type doesn't get past the name matcher. */
3853 if (strcmp (name, "integer") == 0
3854 || strcmp (name, "real") == 0
3855 || strcmp (name, "character") == 0
3856 || strcmp (name, "logical") == 0
3857 || strcmp (name, "complex") == 0)
3859 gfc_error
3860 ("Type name '%s' at %C cannot be the same as an intrinsic type",
3861 name);
3862 return MATCH_ERROR;
3865 if (gfc_get_symbol (name, NULL, &sym))
3866 return MATCH_ERROR;
3868 if (sym->ts.type != BT_UNKNOWN)
3870 gfc_error ("Derived type name '%s' at %C already has a basic type "
3871 "of %s", sym->name, gfc_typename (&sym->ts));
3872 return MATCH_ERROR;
3875 /* The symbol may already have the derived attribute without the
3876 components. The ways this can happen is via a function
3877 definition, an INTRINSIC statement or a subtype in another
3878 derived type that is a pointer. The first part of the AND clause
3879 is true if a the symbol is not the return value of a function. */
3880 if (sym->attr.flavor != FL_DERIVED
3881 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
3882 return MATCH_ERROR;
3884 if (sym->components != NULL)
3886 gfc_error
3887 ("Derived type definition of '%s' at %C has already been defined",
3888 sym->name);
3889 return MATCH_ERROR;
3892 if (attr.access != ACCESS_UNKNOWN
3893 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
3894 return MATCH_ERROR;
3896 gfc_new_block = sym;
3898 return MATCH_YES;
3902 /* Cray Pointees can be declared as:
3903 pointer (ipt, a (n,m,...,*))
3904 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
3905 cheat and set a constant bound of 1 for the last dimension, if this
3906 is the case. Since there is no bounds-checking for Cray Pointees,
3907 this will be okay. */
3910 gfc_mod_pointee_as (gfc_array_spec *as)
3912 as->cray_pointee = true; /* This will be useful to know later. */
3913 if (as->type == AS_ASSUMED_SIZE)
3915 as->type = AS_EXPLICIT;
3916 as->upper[as->rank - 1] = gfc_int_expr (1);
3917 as->cp_was_assumed = true;
3919 else if (as->type == AS_ASSUMED_SHAPE)
3921 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
3922 return MATCH_ERROR;
3924 return MATCH_YES;
3928 /* Match the enum definition statement, here we are trying to match
3929 the first line of enum definition statement.
3930 Returns MATCH_YES if match is found. */
3932 match
3933 gfc_match_enum (void)
3935 match m;
3937 m = gfc_match_eos ();
3938 if (m != MATCH_YES)
3939 return m;
3941 if (gfc_notify_std (GFC_STD_F2003,
3942 "New in Fortran 2003: ENUM AND ENUMERATOR at %C")
3943 == FAILURE)
3944 return MATCH_ERROR;
3946 return MATCH_YES;
3950 /* Match the enumerator definition statement. */
3952 match
3953 gfc_match_enumerator_def (void)
3955 match m;
3956 int elem;
3958 gfc_clear_ts (&current_ts);
3960 m = gfc_match (" enumerator");
3961 if (m != MATCH_YES)
3962 return m;
3964 if (gfc_current_state () != COMP_ENUM)
3966 gfc_error ("ENUM definition statement expected before %C");
3967 gfc_free_enum_history ();
3968 return MATCH_ERROR;
3971 (&current_ts)->type = BT_INTEGER;
3972 (&current_ts)->kind = gfc_c_int_kind;
3974 m = match_attr_spec ();
3975 if (m == MATCH_ERROR)
3977 m = MATCH_NO;
3978 goto cleanup;
3981 elem = 1;
3982 for (;;)
3984 m = variable_decl (elem++);
3985 if (m == MATCH_ERROR)
3986 goto cleanup;
3987 if (m == MATCH_NO)
3988 break;
3990 if (gfc_match_eos () == MATCH_YES)
3991 goto cleanup;
3992 if (gfc_match_char (',') != MATCH_YES)
3993 break;
3996 if (gfc_current_state () == COMP_ENUM)
3998 gfc_free_enum_history ();
3999 gfc_error ("Syntax error in ENUMERATOR definition at %C");
4000 m = MATCH_ERROR;
4003 cleanup:
4004 gfc_free_array_spec (current_as);
4005 current_as = NULL;
4006 return m;