2005-10-12 Joe Buck <Joe.Buck@synopsys.com>
[official-gcc.git] / gcc / fortran / decl.c
blob20d1f8a2d20549c426b80d18e2c61b327de8add0
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 /* gfc_new_block points to the symbol of a newly matched block. */
48 gfc_symbol *gfc_new_block;
51 /********************* DATA statement subroutines *********************/
53 /* Free a gfc_data_variable structure and everything beneath it. */
55 static void
56 free_variable (gfc_data_variable * p)
58 gfc_data_variable *q;
60 for (; p; p = q)
62 q = p->next;
63 gfc_free_expr (p->expr);
64 gfc_free_iterator (&p->iter, 0);
65 free_variable (p->list);
67 gfc_free (p);
72 /* Free a gfc_data_value structure and everything beneath it. */
74 static void
75 free_value (gfc_data_value * p)
77 gfc_data_value *q;
79 for (; p; p = q)
81 q = p->next;
82 gfc_free_expr (p->expr);
83 gfc_free (p);
88 /* Free a list of gfc_data structures. */
90 void
91 gfc_free_data (gfc_data * p)
93 gfc_data *q;
95 for (; p; p = q)
97 q = p->next;
99 free_variable (p->var);
100 free_value (p->value);
102 gfc_free (p);
107 static match var_element (gfc_data_variable *);
109 /* Match a list of variables terminated by an iterator and a right
110 parenthesis. */
112 static match
113 var_list (gfc_data_variable * parent)
115 gfc_data_variable *tail, var;
116 match m;
118 m = var_element (&var);
119 if (m == MATCH_ERROR)
120 return MATCH_ERROR;
121 if (m == MATCH_NO)
122 goto syntax;
124 tail = gfc_get_data_variable ();
125 *tail = var;
127 parent->list = tail;
129 for (;;)
131 if (gfc_match_char (',') != MATCH_YES)
132 goto syntax;
134 m = gfc_match_iterator (&parent->iter, 1);
135 if (m == MATCH_YES)
136 break;
137 if (m == MATCH_ERROR)
138 return MATCH_ERROR;
140 m = var_element (&var);
141 if (m == MATCH_ERROR)
142 return MATCH_ERROR;
143 if (m == MATCH_NO)
144 goto syntax;
146 tail->next = gfc_get_data_variable ();
147 tail = tail->next;
149 *tail = var;
152 if (gfc_match_char (')') != MATCH_YES)
153 goto syntax;
154 return MATCH_YES;
156 syntax:
157 gfc_syntax_error (ST_DATA);
158 return MATCH_ERROR;
162 /* Match a single element in a data variable list, which can be a
163 variable-iterator list. */
165 static match
166 var_element (gfc_data_variable * new)
168 match m;
169 gfc_symbol *sym;
171 memset (new, 0, sizeof (gfc_data_variable));
173 if (gfc_match_char ('(') == MATCH_YES)
174 return var_list (new);
176 m = gfc_match_variable (&new->expr, 0);
177 if (m != MATCH_YES)
178 return m;
180 sym = new->expr->symtree->n.sym;
182 if(sym->value != NULL)
184 gfc_error ("Variable '%s' at %C already has an initialization",
185 sym->name);
186 return MATCH_ERROR;
189 #if 0 /* TODO: Find out where to move this message */
190 if (sym->attr.in_common)
191 /* See if sym is in the blank common block. */
192 for (t = &sym->ns->blank_common; t; t = t->common_next)
193 if (sym == t->head)
195 gfc_error ("DATA statement at %C may not initialize variable "
196 "'%s' from blank COMMON", sym->name);
197 return MATCH_ERROR;
199 #endif
201 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
202 return MATCH_ERROR;
204 return MATCH_YES;
208 /* Match the top-level list of data variables. */
210 static match
211 top_var_list (gfc_data * d)
213 gfc_data_variable var, *tail, *new;
214 match m;
216 tail = NULL;
218 for (;;)
220 m = var_element (&var);
221 if (m == MATCH_NO)
222 goto syntax;
223 if (m == MATCH_ERROR)
224 return MATCH_ERROR;
226 new = gfc_get_data_variable ();
227 *new = var;
229 if (tail == NULL)
230 d->var = new;
231 else
232 tail->next = new;
234 tail = new;
236 if (gfc_match_char ('/') == MATCH_YES)
237 break;
238 if (gfc_match_char (',') != MATCH_YES)
239 goto syntax;
242 return MATCH_YES;
244 syntax:
245 gfc_syntax_error (ST_DATA);
246 return MATCH_ERROR;
250 static match
251 match_data_constant (gfc_expr ** result)
253 char name[GFC_MAX_SYMBOL_LEN + 1];
254 gfc_symbol *sym;
255 gfc_expr *expr;
256 match m;
258 m = gfc_match_literal_constant (&expr, 1);
259 if (m == MATCH_YES)
261 *result = expr;
262 return MATCH_YES;
265 if (m == MATCH_ERROR)
266 return MATCH_ERROR;
268 m = gfc_match_null (result);
269 if (m != MATCH_NO)
270 return m;
272 m = gfc_match_name (name);
273 if (m != MATCH_YES)
274 return m;
276 if (gfc_find_symbol (name, NULL, 1, &sym))
277 return MATCH_ERROR;
279 if (sym == NULL
280 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
282 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
283 name);
284 return MATCH_ERROR;
286 else if (sym->attr.flavor == FL_DERIVED)
287 return gfc_match_structure_constructor (sym, result);
289 *result = gfc_copy_expr (sym->value);
290 return MATCH_YES;
294 /* Match a list of values in a DATA statement. The leading '/' has
295 already been seen at this point. */
297 static match
298 top_val_list (gfc_data * data)
300 gfc_data_value *new, *tail;
301 gfc_expr *expr;
302 const char *msg;
303 match m;
305 tail = NULL;
307 for (;;)
309 m = match_data_constant (&expr);
310 if (m == MATCH_NO)
311 goto syntax;
312 if (m == MATCH_ERROR)
313 return MATCH_ERROR;
315 new = gfc_get_data_value ();
317 if (tail == NULL)
318 data->value = new;
319 else
320 tail->next = new;
322 tail = new;
324 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
326 tail->expr = expr;
327 tail->repeat = 1;
329 else
331 signed int tmp;
332 msg = gfc_extract_int (expr, &tmp);
333 gfc_free_expr (expr);
334 if (msg != NULL)
336 gfc_error (msg);
337 return MATCH_ERROR;
339 tail->repeat = tmp;
341 m = match_data_constant (&tail->expr);
342 if (m == MATCH_NO)
343 goto syntax;
344 if (m == MATCH_ERROR)
345 return MATCH_ERROR;
348 if (gfc_match_char ('/') == MATCH_YES)
349 break;
350 if (gfc_match_char (',') == MATCH_NO)
351 goto syntax;
354 return MATCH_YES;
356 syntax:
357 gfc_syntax_error (ST_DATA);
358 return MATCH_ERROR;
362 /* Matches an old style initialization. */
364 static match
365 match_old_style_init (const char *name)
367 match m;
368 gfc_symtree *st;
369 gfc_data *newdata;
371 /* Set up data structure to hold initializers. */
372 gfc_find_sym_tree (name, NULL, 0, &st);
374 newdata = gfc_get_data ();
375 newdata->var = gfc_get_data_variable ();
376 newdata->var->expr = gfc_get_variable_expr (st);
378 /* Match initial value list. This also eats the terminal
379 '/'. */
380 m = top_val_list (newdata);
381 if (m != MATCH_YES)
383 gfc_free (newdata);
384 return m;
387 if (gfc_pure (NULL))
389 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
390 gfc_free (newdata);
391 return MATCH_ERROR;
394 /* Chain in namespace list of DATA initializers. */
395 newdata->next = gfc_current_ns->data;
396 gfc_current_ns->data = newdata;
398 return m;
401 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
402 we are matching a DATA statement and are therefore issuing an error
403 if we encounter something unexpected, if not, we're trying to match
404 an old-style initialization expression of the form INTEGER I /2/. */
406 match
407 gfc_match_data (void)
409 gfc_data *new;
410 match m;
412 for (;;)
414 new = gfc_get_data ();
415 new->where = gfc_current_locus;
417 m = top_var_list (new);
418 if (m != MATCH_YES)
419 goto cleanup;
421 m = top_val_list (new);
422 if (m != MATCH_YES)
423 goto cleanup;
425 new->next = gfc_current_ns->data;
426 gfc_current_ns->data = new;
428 if (gfc_match_eos () == MATCH_YES)
429 break;
431 gfc_match_char (','); /* Optional comma */
434 if (gfc_pure (NULL))
436 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
437 return MATCH_ERROR;
440 return MATCH_YES;
442 cleanup:
443 gfc_free_data (new);
444 return MATCH_ERROR;
448 /************************ Declaration statements *********************/
450 /* Match an intent specification. Since this can only happen after an
451 INTENT word, a legal intent-spec must follow. */
453 static sym_intent
454 match_intent_spec (void)
457 if (gfc_match (" ( in out )") == MATCH_YES)
458 return INTENT_INOUT;
459 if (gfc_match (" ( in )") == MATCH_YES)
460 return INTENT_IN;
461 if (gfc_match (" ( out )") == MATCH_YES)
462 return INTENT_OUT;
464 gfc_error ("Bad INTENT specification at %C");
465 return INTENT_UNKNOWN;
469 /* Matches a character length specification, which is either a
470 specification expression or a '*'. */
472 static match
473 char_len_param_value (gfc_expr ** expr)
476 if (gfc_match_char ('*') == MATCH_YES)
478 *expr = NULL;
479 return MATCH_YES;
482 return gfc_match_expr (expr);
486 /* A character length is a '*' followed by a literal integer or a
487 char_len_param_value in parenthesis. */
489 static match
490 match_char_length (gfc_expr ** expr)
492 int length;
493 match m;
495 m = gfc_match_char ('*');
496 if (m != MATCH_YES)
497 return m;
499 m = gfc_match_small_literal_int (&length);
500 if (m == MATCH_ERROR)
501 return m;
503 if (m == MATCH_YES)
505 *expr = gfc_int_expr (length);
506 return m;
509 if (gfc_match_char ('(') == MATCH_NO)
510 goto syntax;
512 m = char_len_param_value (expr);
513 if (m == MATCH_ERROR)
514 return m;
515 if (m == MATCH_NO)
516 goto syntax;
518 if (gfc_match_char (')') == MATCH_NO)
520 gfc_free_expr (*expr);
521 *expr = NULL;
522 goto syntax;
525 return MATCH_YES;
527 syntax:
528 gfc_error ("Syntax error in character length specification at %C");
529 return MATCH_ERROR;
533 /* Special subroutine for finding a symbol. Check if the name is found
534 in the current name space. If not, and we're compiling a function or
535 subroutine and the parent compilation unit is an interface, then check
536 to see if the name we've been given is the name of the interface
537 (located in another namespace). */
539 static int
540 find_special (const char *name, gfc_symbol ** result)
542 gfc_state_data *s;
543 int i;
545 i = gfc_get_symbol (name, NULL, result);
546 if (i==0)
547 goto end;
549 if (gfc_current_state () != COMP_SUBROUTINE
550 && gfc_current_state () != COMP_FUNCTION)
551 goto end;
553 s = gfc_state_stack->previous;
554 if (s == NULL)
555 goto end;
557 if (s->state != COMP_INTERFACE)
558 goto end;
559 if (s->sym == NULL)
560 goto end; /* Nameless interface */
562 if (strcmp (name, s->sym->name) == 0)
564 *result = s->sym;
565 return 0;
568 end:
569 return i;
573 /* Special subroutine for getting a symbol node associated with a
574 procedure name, used in SUBROUTINE and FUNCTION statements. The
575 symbol is created in the parent using with symtree node in the
576 child unit pointing to the symbol. If the current namespace has no
577 parent, then the symbol is just created in the current unit. */
579 static int
580 get_proc_name (const char *name, gfc_symbol ** result)
582 gfc_symtree *st;
583 gfc_symbol *sym;
584 int rc;
586 if (gfc_current_ns->parent == NULL)
587 return gfc_get_symbol (name, NULL, result);
589 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
590 if (*result == NULL)
591 return rc;
593 /* ??? Deal with ENTRY problem */
595 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
597 sym = *result;
598 st->n.sym = sym;
599 sym->refs++;
601 /* See if the procedure should be a module procedure */
603 if (sym->ns->proc_name != NULL
604 && sym->ns->proc_name->attr.flavor == FL_MODULE
605 && sym->attr.proc != PROC_MODULE
606 && gfc_add_procedure (&sym->attr, PROC_MODULE,
607 sym->name, NULL) == FAILURE)
608 rc = 2;
610 return rc;
614 /* Function called by variable_decl() that adds a name to the symbol
615 table. */
617 static try
618 build_sym (const char *name, gfc_charlen * cl,
619 gfc_array_spec ** as, locus * var_locus)
621 symbol_attribute attr;
622 gfc_symbol *sym;
624 /* if (find_special (name, &sym)) */
625 if (gfc_get_symbol (name, NULL, &sym))
626 return FAILURE;
628 /* Start updating the symbol table. Add basic type attribute
629 if present. */
630 if (current_ts.type != BT_UNKNOWN
631 &&(sym->attr.implicit_type == 0
632 || !gfc_compare_types (&sym->ts, &current_ts))
633 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
634 return FAILURE;
636 if (sym->ts.type == BT_CHARACTER)
637 sym->ts.cl = cl;
639 /* Add dimension attribute if present. */
640 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
641 return FAILURE;
642 *as = NULL;
644 /* Add attribute to symbol. The copy is so that we can reset the
645 dimension attribute. */
646 attr = current_attr;
647 attr.dimension = 0;
649 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
650 return FAILURE;
652 return SUCCESS;
655 /* Set character constant to the given length. The constant will be padded or
656 truncated. */
658 void
659 gfc_set_constant_character_len (int len, gfc_expr * expr)
661 char * s;
662 int slen;
664 gcc_assert (expr->expr_type == EXPR_CONSTANT);
665 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
667 slen = expr->value.character.length;
668 if (len != slen)
670 s = gfc_getmem (len);
671 memcpy (s, expr->value.character.string, MIN (len, slen));
672 if (len > slen)
673 memset (&s[slen], ' ', len - slen);
674 gfc_free (expr->value.character.string);
675 expr->value.character.string = s;
676 expr->value.character.length = len;
680 /* Function called by variable_decl() that adds an initialization
681 expression to a symbol. */
683 static try
684 add_init_expr_to_sym (const char *name, gfc_expr ** initp,
685 locus * var_locus)
687 symbol_attribute attr;
688 gfc_symbol *sym;
689 gfc_expr *init;
691 init = *initp;
692 if (find_special (name, &sym))
693 return FAILURE;
695 attr = sym->attr;
697 /* If this symbol is confirming an implicit parameter type,
698 then an initialization expression is not allowed. */
699 if (attr.flavor == FL_PARAMETER
700 && sym->value != NULL
701 && *initp != NULL)
703 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
704 sym->name);
705 return FAILURE;
708 if (attr.in_common
709 && !attr.data
710 && *initp != NULL)
712 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
713 sym->name);
714 return FAILURE;
717 if (init == NULL)
719 /* An initializer is required for PARAMETER declarations. */
720 if (attr.flavor == FL_PARAMETER)
722 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
723 return FAILURE;
726 else
728 /* If a variable appears in a DATA block, it cannot have an
729 initializer. */
730 if (sym->attr.data)
732 gfc_error
733 ("Variable '%s' at %C with an initializer already appears "
734 "in a DATA statement", sym->name);
735 return FAILURE;
738 /* Check if the assignment can happen. This has to be put off
739 until later for a derived type variable. */
740 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
741 && gfc_check_assign_symbol (sym, init) == FAILURE)
742 return FAILURE;
744 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
746 /* Update symbol character length according initializer. */
747 if (sym->ts.cl->length == NULL)
749 if (init->expr_type == EXPR_CONSTANT)
750 sym->ts.cl->length =
751 gfc_int_expr (init->value.character.length);
752 else if (init->expr_type == EXPR_ARRAY)
753 sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
755 /* Update initializer character length according symbol. */
756 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
758 int len = mpz_get_si (sym->ts.cl->length->value.integer);
759 gfc_constructor * p;
761 if (init->expr_type == EXPR_CONSTANT)
762 gfc_set_constant_character_len (len, init);
763 else if (init->expr_type == EXPR_ARRAY)
765 gfc_free_expr (init->ts.cl->length);
766 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
767 for (p = init->value.constructor; p; p = p->next)
768 gfc_set_constant_character_len (len, p->expr);
773 /* Add initializer. Make sure we keep the ranks sane. */
774 if (sym->attr.dimension && init->rank == 0)
775 init->rank = sym->as->rank;
777 sym->value = init;
778 *initp = NULL;
781 return SUCCESS;
785 /* Function called by variable_decl() that adds a name to a structure
786 being built. */
788 static try
789 build_struct (const char *name, gfc_charlen * cl, gfc_expr ** init,
790 gfc_array_spec ** as)
792 gfc_component *c;
794 /* If the current symbol is of the same derived type that we're
795 constructing, it must have the pointer attribute. */
796 if (current_ts.type == BT_DERIVED
797 && current_ts.derived == gfc_current_block ()
798 && current_attr.pointer == 0)
800 gfc_error ("Component at %C must have the POINTER attribute");
801 return FAILURE;
804 if (gfc_current_block ()->attr.pointer
805 && (*as)->rank != 0)
807 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
809 gfc_error ("Array component of structure at %C must have explicit "
810 "or deferred shape");
811 return FAILURE;
815 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
816 return FAILURE;
818 c->ts = current_ts;
819 c->ts.cl = cl;
820 gfc_set_component_attr (c, &current_attr);
822 c->initializer = *init;
823 *init = NULL;
825 c->as = *as;
826 if (c->as != NULL)
827 c->dimension = 1;
828 *as = NULL;
830 /* Check array components. */
831 if (!c->dimension)
832 return SUCCESS;
834 if (c->pointer)
836 if (c->as->type != AS_DEFERRED)
838 gfc_error ("Pointer array component of structure at %C "
839 "must have a deferred shape");
840 return FAILURE;
843 else
845 if (c->as->type != AS_EXPLICIT)
847 gfc_error
848 ("Array component of structure at %C must have an explicit "
849 "shape");
850 return FAILURE;
854 return SUCCESS;
858 /* Match a 'NULL()', and possibly take care of some side effects. */
860 match
861 gfc_match_null (gfc_expr ** result)
863 gfc_symbol *sym;
864 gfc_expr *e;
865 match m;
867 m = gfc_match (" null ( )");
868 if (m != MATCH_YES)
869 return m;
871 /* The NULL symbol now has to be/become an intrinsic function. */
872 if (gfc_get_symbol ("null", NULL, &sym))
874 gfc_error ("NULL() initialization at %C is ambiguous");
875 return MATCH_ERROR;
878 gfc_intrinsic_symbol (sym);
880 if (sym->attr.proc != PROC_INTRINSIC
881 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
882 sym->name, NULL) == FAILURE
883 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
884 return MATCH_ERROR;
886 e = gfc_get_expr ();
887 e->where = gfc_current_locus;
888 e->expr_type = EXPR_NULL;
889 e->ts.type = BT_UNKNOWN;
891 *result = e;
893 return MATCH_YES;
897 /* Match a variable name with an optional initializer. When this
898 subroutine is called, a variable is expected to be parsed next.
899 Depending on what is happening at the moment, updates either the
900 symbol table or the current interface. */
902 static match
903 variable_decl (int elem)
905 char name[GFC_MAX_SYMBOL_LEN + 1];
906 gfc_expr *initializer, *char_len;
907 gfc_array_spec *as;
908 gfc_charlen *cl;
909 locus var_locus;
910 match m;
911 try t;
913 initializer = NULL;
914 as = NULL;
916 /* When we get here, we've just matched a list of attributes and
917 maybe a type and a double colon. The next thing we expect to see
918 is the name of the symbol. */
919 m = gfc_match_name (name);
920 if (m != MATCH_YES)
921 goto cleanup;
923 var_locus = gfc_current_locus;
925 /* Now we could see the optional array spec. or character length. */
926 m = gfc_match_array_spec (&as);
927 if (m == MATCH_ERROR)
928 goto cleanup;
929 if (m == MATCH_NO)
930 as = gfc_copy_array_spec (current_as);
932 char_len = NULL;
933 cl = NULL;
935 if (current_ts.type == BT_CHARACTER)
937 switch (match_char_length (&char_len))
939 case MATCH_YES:
940 cl = gfc_get_charlen ();
941 cl->next = gfc_current_ns->cl_list;
942 gfc_current_ns->cl_list = cl;
944 cl->length = char_len;
945 break;
947 /* Non-constant lengths need to be copied after the first
948 element. */
949 case MATCH_NO:
950 if (elem > 1 && current_ts.cl->length
951 && current_ts.cl->length->expr_type != EXPR_CONSTANT)
953 cl = gfc_get_charlen ();
954 cl->next = gfc_current_ns->cl_list;
955 gfc_current_ns->cl_list = cl;
956 cl->length = gfc_copy_expr (current_ts.cl->length);
958 else
959 cl = current_ts.cl;
961 break;
963 case MATCH_ERROR:
964 goto cleanup;
968 /* OK, we've successfully matched the declaration. Now put the
969 symbol in the current namespace, because it might be used in the
970 optional initialization expression for this symbol, e.g. this is
971 perfectly legal:
973 integer, parameter :: i = huge(i)
975 This is only true for parameters or variables of a basic type.
976 For components of derived types, it is not true, so we don't
977 create a symbol for those yet. If we fail to create the symbol,
978 bail out. */
979 if (gfc_current_state () != COMP_DERIVED
980 && build_sym (name, cl, &as, &var_locus) == FAILURE)
982 m = MATCH_ERROR;
983 goto cleanup;
986 /* In functions that have a RESULT variable defined, the function
987 name always refers to function calls. Therefore, the name is
988 not allowed to appear in specification statements. */
989 if (gfc_current_state () == COMP_FUNCTION
990 && gfc_current_block () != NULL
991 && gfc_current_block ()->result != NULL
992 && gfc_current_block ()->result != gfc_current_block ()
993 && strcmp (gfc_current_block ()->name, name) == 0)
995 gfc_error ("Function name '%s' not allowed at %C", name);
996 m = MATCH_ERROR;
997 goto cleanup;
1000 /* We allow old-style initializations of the form
1001 integer i /2/, j(4) /3*3, 1/
1002 (if no colon has been seen). These are different from data
1003 statements in that initializers are only allowed to apply to the
1004 variable immediately preceding, i.e.
1005 integer i, j /1, 2/
1006 is not allowed. Therefore we have to do some work manually, that
1007 could otherwise be left to the matchers for DATA statements. */
1009 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1011 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1012 "initialization at %C") == FAILURE)
1013 return MATCH_ERROR;
1015 return match_old_style_init (name);
1018 /* The double colon must be present in order to have initializers.
1019 Otherwise the statement is ambiguous with an assignment statement. */
1020 if (colon_seen)
1022 if (gfc_match (" =>") == MATCH_YES)
1025 if (!current_attr.pointer)
1027 gfc_error ("Initialization at %C isn't for a pointer variable");
1028 m = MATCH_ERROR;
1029 goto cleanup;
1032 m = gfc_match_null (&initializer);
1033 if (m == MATCH_NO)
1035 gfc_error ("Pointer initialization requires a NULL at %C");
1036 m = MATCH_ERROR;
1039 if (gfc_pure (NULL))
1041 gfc_error
1042 ("Initialization of pointer at %C is not allowed in a "
1043 "PURE procedure");
1044 m = MATCH_ERROR;
1047 if (m != MATCH_YES)
1048 goto cleanup;
1050 initializer->ts = current_ts;
1053 else if (gfc_match_char ('=') == MATCH_YES)
1055 if (current_attr.pointer)
1057 gfc_error
1058 ("Pointer initialization at %C requires '=>', not '='");
1059 m = MATCH_ERROR;
1060 goto cleanup;
1063 m = gfc_match_init_expr (&initializer);
1064 if (m == MATCH_NO)
1066 gfc_error ("Expected an initialization expression at %C");
1067 m = MATCH_ERROR;
1070 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1072 gfc_error
1073 ("Initialization of variable at %C is not allowed in a "
1074 "PURE procedure");
1075 m = MATCH_ERROR;
1078 if (m != MATCH_YES)
1079 goto cleanup;
1083 /* Add the initializer. Note that it is fine if initializer is
1084 NULL here, because we sometimes also need to check if a
1085 declaration *must* have an initialization expression. */
1086 if (gfc_current_state () != COMP_DERIVED)
1087 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1088 else
1090 if (current_ts.type == BT_DERIVED && !initializer)
1091 initializer = gfc_default_initializer (&current_ts);
1092 t = build_struct (name, cl, &initializer, &as);
1095 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1097 cleanup:
1098 /* Free stuff up and return. */
1099 gfc_free_expr (initializer);
1100 gfc_free_array_spec (as);
1102 return m;
1106 /* Match an extended-f77 kind specification. */
1108 match
1109 gfc_match_old_kind_spec (gfc_typespec * ts)
1111 match m;
1113 if (gfc_match_char ('*') != MATCH_YES)
1114 return MATCH_NO;
1116 m = gfc_match_small_literal_int (&ts->kind);
1117 if (m != MATCH_YES)
1118 return MATCH_ERROR;
1120 /* Massage the kind numbers for complex types. */
1121 if (ts->type == BT_COMPLEX && ts->kind == 8)
1122 ts->kind = 4;
1123 if (ts->type == BT_COMPLEX && ts->kind == 16)
1124 ts->kind = 8;
1126 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1128 gfc_error ("Old-style kind %d not supported for type %s at %C",
1129 ts->kind, gfc_basic_typename (ts->type));
1131 return MATCH_ERROR;
1134 return MATCH_YES;
1138 /* Match a kind specification. Since kinds are generally optional, we
1139 usually return MATCH_NO if something goes wrong. If a "kind="
1140 string is found, then we know we have an error. */
1142 match
1143 gfc_match_kind_spec (gfc_typespec * ts)
1145 locus where;
1146 gfc_expr *e;
1147 match m, n;
1148 const char *msg;
1150 m = MATCH_NO;
1151 e = NULL;
1153 where = gfc_current_locus;
1155 if (gfc_match_char ('(') == MATCH_NO)
1156 return MATCH_NO;
1158 /* Also gobbles optional text. */
1159 if (gfc_match (" kind = ") == MATCH_YES)
1160 m = MATCH_ERROR;
1162 n = gfc_match_init_expr (&e);
1163 if (n == MATCH_NO)
1164 gfc_error ("Expected initialization expression at %C");
1165 if (n != MATCH_YES)
1166 return MATCH_ERROR;
1168 if (e->rank != 0)
1170 gfc_error ("Expected scalar initialization expression at %C");
1171 m = MATCH_ERROR;
1172 goto no_match;
1175 msg = gfc_extract_int (e, &ts->kind);
1176 if (msg != NULL)
1178 gfc_error (msg);
1179 m = MATCH_ERROR;
1180 goto no_match;
1183 gfc_free_expr (e);
1184 e = NULL;
1186 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1188 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1189 gfc_basic_typename (ts->type));
1191 m = MATCH_ERROR;
1192 goto no_match;
1195 if (gfc_match_char (')') != MATCH_YES)
1197 gfc_error ("Missing right paren at %C");
1198 goto no_match;
1201 return MATCH_YES;
1203 no_match:
1204 gfc_free_expr (e);
1205 gfc_current_locus = where;
1206 return m;
1210 /* Match the various kind/length specifications in a CHARACTER
1211 declaration. We don't return MATCH_NO. */
1213 static match
1214 match_char_spec (gfc_typespec * ts)
1216 int i, kind, seen_length;
1217 gfc_charlen *cl;
1218 gfc_expr *len;
1219 match m;
1221 kind = gfc_default_character_kind;
1222 len = NULL;
1223 seen_length = 0;
1225 /* Try the old-style specification first. */
1226 old_char_selector = 0;
1228 m = match_char_length (&len);
1229 if (m != MATCH_NO)
1231 if (m == MATCH_YES)
1232 old_char_selector = 1;
1233 seen_length = 1;
1234 goto done;
1237 m = gfc_match_char ('(');
1238 if (m != MATCH_YES)
1240 m = MATCH_YES; /* character without length is a single char */
1241 goto done;
1244 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ) */
1245 if (gfc_match (" kind =") == MATCH_YES)
1247 m = gfc_match_small_int (&kind);
1248 if (m == MATCH_ERROR)
1249 goto done;
1250 if (m == MATCH_NO)
1251 goto syntax;
1253 if (gfc_match (" , len =") == MATCH_NO)
1254 goto rparen;
1256 m = char_len_param_value (&len);
1257 if (m == MATCH_NO)
1258 goto syntax;
1259 if (m == MATCH_ERROR)
1260 goto done;
1261 seen_length = 1;
1263 goto rparen;
1266 /* Try to match ( LEN = <len-param> ) or ( LEN = <len-param>, KIND = <int> ) */
1267 if (gfc_match (" len =") == MATCH_YES)
1269 m = char_len_param_value (&len);
1270 if (m == MATCH_NO)
1271 goto syntax;
1272 if (m == MATCH_ERROR)
1273 goto done;
1274 seen_length = 1;
1276 if (gfc_match_char (')') == MATCH_YES)
1277 goto done;
1279 if (gfc_match (" , kind =") != MATCH_YES)
1280 goto syntax;
1282 gfc_match_small_int (&kind);
1284 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1286 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1287 return MATCH_YES;
1290 goto rparen;
1293 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ) */
1294 m = char_len_param_value (&len);
1295 if (m == MATCH_NO)
1296 goto syntax;
1297 if (m == MATCH_ERROR)
1298 goto done;
1299 seen_length = 1;
1301 m = gfc_match_char (')');
1302 if (m == MATCH_YES)
1303 goto done;
1305 if (gfc_match_char (',') != MATCH_YES)
1306 goto syntax;
1308 gfc_match (" kind ="); /* Gobble optional text */
1310 m = gfc_match_small_int (&kind);
1311 if (m == MATCH_ERROR)
1312 goto done;
1313 if (m == MATCH_NO)
1314 goto syntax;
1316 rparen:
1317 /* Require a right-paren at this point. */
1318 m = gfc_match_char (')');
1319 if (m == MATCH_YES)
1320 goto done;
1322 syntax:
1323 gfc_error ("Syntax error in CHARACTER declaration at %C");
1324 m = MATCH_ERROR;
1326 done:
1327 if (m == MATCH_YES && gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1329 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1330 m = MATCH_ERROR;
1333 if (m != MATCH_YES)
1335 gfc_free_expr (len);
1336 return m;
1339 /* Do some final massaging of the length values. */
1340 cl = gfc_get_charlen ();
1341 cl->next = gfc_current_ns->cl_list;
1342 gfc_current_ns->cl_list = cl;
1344 if (seen_length == 0)
1345 cl->length = gfc_int_expr (1);
1346 else
1348 if (len == NULL || gfc_extract_int (len, &i) != NULL || i >= 0)
1349 cl->length = len;
1350 else
1352 gfc_free_expr (len);
1353 cl->length = gfc_int_expr (0);
1357 ts->cl = cl;
1358 ts->kind = kind;
1360 return MATCH_YES;
1364 /* Matches a type specification. If successful, sets the ts structure
1365 to the matched specification. This is necessary for FUNCTION and
1366 IMPLICIT statements.
1368 If implicit_flag is nonzero, then we don't check for the optional
1369 kind specification. Not doing so is needed for matching an IMPLICIT
1370 statement correctly. */
1372 static match
1373 match_type_spec (gfc_typespec * ts, int implicit_flag)
1375 char name[GFC_MAX_SYMBOL_LEN + 1];
1376 gfc_symbol *sym;
1377 match m;
1378 int c;
1380 gfc_clear_ts (ts);
1382 if (gfc_match (" integer") == MATCH_YES)
1384 ts->type = BT_INTEGER;
1385 ts->kind = gfc_default_integer_kind;
1386 goto get_kind;
1389 if (gfc_match (" character") == MATCH_YES)
1391 ts->type = BT_CHARACTER;
1392 if (implicit_flag == 0)
1393 return match_char_spec (ts);
1394 else
1395 return MATCH_YES;
1398 if (gfc_match (" real") == MATCH_YES)
1400 ts->type = BT_REAL;
1401 ts->kind = gfc_default_real_kind;
1402 goto get_kind;
1405 if (gfc_match (" double precision") == MATCH_YES)
1407 ts->type = BT_REAL;
1408 ts->kind = gfc_default_double_kind;
1409 return MATCH_YES;
1412 if (gfc_match (" complex") == MATCH_YES)
1414 ts->type = BT_COMPLEX;
1415 ts->kind = gfc_default_complex_kind;
1416 goto get_kind;
1419 if (gfc_match (" double complex") == MATCH_YES)
1421 ts->type = BT_COMPLEX;
1422 ts->kind = gfc_default_double_kind;
1423 return MATCH_YES;
1426 if (gfc_match (" logical") == MATCH_YES)
1428 ts->type = BT_LOGICAL;
1429 ts->kind = gfc_default_logical_kind;
1430 goto get_kind;
1433 m = gfc_match (" type ( %n )", name);
1434 if (m != MATCH_YES)
1435 return m;
1437 /* Search for the name but allow the components to be defined later. */
1438 if (gfc_get_ha_symbol (name, &sym))
1440 gfc_error ("Type name '%s' at %C is ambiguous", name);
1441 return MATCH_ERROR;
1444 if (sym->attr.flavor != FL_DERIVED
1445 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
1446 return MATCH_ERROR;
1448 ts->type = BT_DERIVED;
1449 ts->kind = 0;
1450 ts->derived = sym;
1452 return MATCH_YES;
1454 get_kind:
1455 /* For all types except double, derived and character, look for an
1456 optional kind specifier. MATCH_NO is actually OK at this point. */
1457 if (implicit_flag == 1)
1458 return MATCH_YES;
1460 if (gfc_current_form == FORM_FREE)
1462 c = gfc_peek_char();
1463 if (!gfc_is_whitespace(c) && c != '*' && c != '('
1464 && c != ':' && c != ',')
1465 return MATCH_NO;
1468 m = gfc_match_kind_spec (ts);
1469 if (m == MATCH_NO && ts->type != BT_CHARACTER)
1470 m = gfc_match_old_kind_spec (ts);
1472 if (m == MATCH_NO)
1473 m = MATCH_YES; /* No kind specifier found. */
1475 return m;
1479 /* Match an IMPLICIT NONE statement. Actually, this statement is
1480 already matched in parse.c, or we would not end up here in the
1481 first place. So the only thing we need to check, is if there is
1482 trailing garbage. If not, the match is successful. */
1484 match
1485 gfc_match_implicit_none (void)
1488 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
1492 /* Match the letter range(s) of an IMPLICIT statement. */
1494 static match
1495 match_implicit_range (void)
1497 int c, c1, c2, inner;
1498 locus cur_loc;
1500 cur_loc = gfc_current_locus;
1502 gfc_gobble_whitespace ();
1503 c = gfc_next_char ();
1504 if (c != '(')
1506 gfc_error ("Missing character range in IMPLICIT at %C");
1507 goto bad;
1510 inner = 1;
1511 while (inner)
1513 gfc_gobble_whitespace ();
1514 c1 = gfc_next_char ();
1515 if (!ISALPHA (c1))
1516 goto bad;
1518 gfc_gobble_whitespace ();
1519 c = gfc_next_char ();
1521 switch (c)
1523 case ')':
1524 inner = 0; /* Fall through */
1526 case ',':
1527 c2 = c1;
1528 break;
1530 case '-':
1531 gfc_gobble_whitespace ();
1532 c2 = gfc_next_char ();
1533 if (!ISALPHA (c2))
1534 goto bad;
1536 gfc_gobble_whitespace ();
1537 c = gfc_next_char ();
1539 if ((c != ',') && (c != ')'))
1540 goto bad;
1541 if (c == ')')
1542 inner = 0;
1544 break;
1546 default:
1547 goto bad;
1550 if (c1 > c2)
1552 gfc_error ("Letters must be in alphabetic order in "
1553 "IMPLICIT statement at %C");
1554 goto bad;
1557 /* See if we can add the newly matched range to the pending
1558 implicits from this IMPLICIT statement. We do not check for
1559 conflicts with whatever earlier IMPLICIT statements may have
1560 set. This is done when we've successfully finished matching
1561 the current one. */
1562 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
1563 goto bad;
1566 return MATCH_YES;
1568 bad:
1569 gfc_syntax_error (ST_IMPLICIT);
1571 gfc_current_locus = cur_loc;
1572 return MATCH_ERROR;
1576 /* Match an IMPLICIT statement, storing the types for
1577 gfc_set_implicit() if the statement is accepted by the parser.
1578 There is a strange looking, but legal syntactic construction
1579 possible. It looks like:
1581 IMPLICIT INTEGER (a-b) (c-d)
1583 This is legal if "a-b" is a constant expression that happens to
1584 equal one of the legal kinds for integers. The real problem
1585 happens with an implicit specification that looks like:
1587 IMPLICIT INTEGER (a-b)
1589 In this case, a typespec matcher that is "greedy" (as most of the
1590 matchers are) gobbles the character range as a kindspec, leaving
1591 nothing left. We therefore have to go a bit more slowly in the
1592 matching process by inhibiting the kindspec checking during
1593 typespec matching and checking for a kind later. */
1595 match
1596 gfc_match_implicit (void)
1598 gfc_typespec ts;
1599 locus cur_loc;
1600 int c;
1601 match m;
1603 /* We don't allow empty implicit statements. */
1604 if (gfc_match_eos () == MATCH_YES)
1606 gfc_error ("Empty IMPLICIT statement at %C");
1607 return MATCH_ERROR;
1612 /* First cleanup. */
1613 gfc_clear_new_implicit ();
1615 /* A basic type is mandatory here. */
1616 m = match_type_spec (&ts, 1);
1617 if (m == MATCH_ERROR)
1618 goto error;
1619 if (m == MATCH_NO)
1620 goto syntax;
1622 cur_loc = gfc_current_locus;
1623 m = match_implicit_range ();
1625 if (m == MATCH_YES)
1627 /* We may have <TYPE> (<RANGE>). */
1628 gfc_gobble_whitespace ();
1629 c = gfc_next_char ();
1630 if ((c == '\n') || (c == ','))
1632 /* Check for CHARACTER with no length parameter. */
1633 if (ts.type == BT_CHARACTER && !ts.cl)
1635 ts.kind = gfc_default_character_kind;
1636 ts.cl = gfc_get_charlen ();
1637 ts.cl->next = gfc_current_ns->cl_list;
1638 gfc_current_ns->cl_list = ts.cl;
1639 ts.cl->length = gfc_int_expr (1);
1642 /* Record the Successful match. */
1643 if (gfc_merge_new_implicit (&ts) != SUCCESS)
1644 return MATCH_ERROR;
1645 continue;
1648 gfc_current_locus = cur_loc;
1651 /* Discard the (incorrectly) matched range. */
1652 gfc_clear_new_implicit ();
1654 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
1655 if (ts.type == BT_CHARACTER)
1656 m = match_char_spec (&ts);
1657 else
1659 m = gfc_match_kind_spec (&ts);
1660 if (m == MATCH_NO)
1662 m = gfc_match_old_kind_spec (&ts);
1663 if (m == MATCH_ERROR)
1664 goto error;
1665 if (m == MATCH_NO)
1666 goto syntax;
1669 if (m == MATCH_ERROR)
1670 goto error;
1672 m = match_implicit_range ();
1673 if (m == MATCH_ERROR)
1674 goto error;
1675 if (m == MATCH_NO)
1676 goto syntax;
1678 gfc_gobble_whitespace ();
1679 c = gfc_next_char ();
1680 if ((c != '\n') && (c != ','))
1681 goto syntax;
1683 if (gfc_merge_new_implicit (&ts) != SUCCESS)
1684 return MATCH_ERROR;
1686 while (c == ',');
1688 return MATCH_YES;
1690 syntax:
1691 gfc_syntax_error (ST_IMPLICIT);
1693 error:
1694 return MATCH_ERROR;
1698 /* Matches an attribute specification including array specs. If
1699 successful, leaves the variables current_attr and current_as
1700 holding the specification. Also sets the colon_seen variable for
1701 later use by matchers associated with initializations.
1703 This subroutine is a little tricky in the sense that we don't know
1704 if we really have an attr-spec until we hit the double colon.
1705 Until that time, we can only return MATCH_NO. This forces us to
1706 check for duplicate specification at this level. */
1708 static match
1709 match_attr_spec (void)
1712 /* Modifiers that can exist in a type statement. */
1713 typedef enum
1714 { GFC_DECL_BEGIN = 0,
1715 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
1716 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
1717 DECL_PARAMETER, DECL_POINTER, DECL_PRIVATE, DECL_PUBLIC, DECL_SAVE,
1718 DECL_TARGET, DECL_COLON, DECL_NONE,
1719 GFC_DECL_END /* Sentinel */
1721 decl_types;
1723 /* GFC_DECL_END is the sentinel, index starts at 0. */
1724 #define NUM_DECL GFC_DECL_END
1726 static mstring decls[] = {
1727 minit (", allocatable", DECL_ALLOCATABLE),
1728 minit (", dimension", DECL_DIMENSION),
1729 minit (", external", DECL_EXTERNAL),
1730 minit (", intent ( in )", DECL_IN),
1731 minit (", intent ( out )", DECL_OUT),
1732 minit (", intent ( in out )", DECL_INOUT),
1733 minit (", intrinsic", DECL_INTRINSIC),
1734 minit (", optional", DECL_OPTIONAL),
1735 minit (", parameter", DECL_PARAMETER),
1736 minit (", pointer", DECL_POINTER),
1737 minit (", private", DECL_PRIVATE),
1738 minit (", public", DECL_PUBLIC),
1739 minit (", save", DECL_SAVE),
1740 minit (", target", DECL_TARGET),
1741 minit ("::", DECL_COLON),
1742 minit (NULL, DECL_NONE)
1745 locus start, seen_at[NUM_DECL];
1746 int seen[NUM_DECL];
1747 decl_types d;
1748 const char *attr;
1749 match m;
1750 try t;
1752 gfc_clear_attr (&current_attr);
1753 start = gfc_current_locus;
1755 current_as = NULL;
1756 colon_seen = 0;
1758 /* See if we get all of the keywords up to the final double colon. */
1759 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1760 seen[d] = 0;
1762 for (;;)
1764 d = (decl_types) gfc_match_strings (decls);
1765 if (d == DECL_NONE || d == DECL_COLON)
1766 break;
1768 seen[d]++;
1769 seen_at[d] = gfc_current_locus;
1771 if (d == DECL_DIMENSION)
1773 m = gfc_match_array_spec (&current_as);
1775 if (m == MATCH_NO)
1777 gfc_error ("Missing dimension specification at %C");
1778 m = MATCH_ERROR;
1781 if (m == MATCH_ERROR)
1782 goto cleanup;
1786 /* No double colon, so assume that we've been looking at something
1787 else the whole time. */
1788 if (d == DECL_NONE)
1790 m = MATCH_NO;
1791 goto cleanup;
1794 /* Since we've seen a double colon, we have to be looking at an
1795 attr-spec. This means that we can now issue errors. */
1796 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1797 if (seen[d] > 1)
1799 switch (d)
1801 case DECL_ALLOCATABLE:
1802 attr = "ALLOCATABLE";
1803 break;
1804 case DECL_DIMENSION:
1805 attr = "DIMENSION";
1806 break;
1807 case DECL_EXTERNAL:
1808 attr = "EXTERNAL";
1809 break;
1810 case DECL_IN:
1811 attr = "INTENT (IN)";
1812 break;
1813 case DECL_OUT:
1814 attr = "INTENT (OUT)";
1815 break;
1816 case DECL_INOUT:
1817 attr = "INTENT (IN OUT)";
1818 break;
1819 case DECL_INTRINSIC:
1820 attr = "INTRINSIC";
1821 break;
1822 case DECL_OPTIONAL:
1823 attr = "OPTIONAL";
1824 break;
1825 case DECL_PARAMETER:
1826 attr = "PARAMETER";
1827 break;
1828 case DECL_POINTER:
1829 attr = "POINTER";
1830 break;
1831 case DECL_PRIVATE:
1832 attr = "PRIVATE";
1833 break;
1834 case DECL_PUBLIC:
1835 attr = "PUBLIC";
1836 break;
1837 case DECL_SAVE:
1838 attr = "SAVE";
1839 break;
1840 case DECL_TARGET:
1841 attr = "TARGET";
1842 break;
1843 default:
1844 attr = NULL; /* This shouldn't happen */
1847 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
1848 m = MATCH_ERROR;
1849 goto cleanup;
1852 /* Now that we've dealt with duplicate attributes, add the attributes
1853 to the current attribute. */
1854 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
1856 if (seen[d] == 0)
1857 continue;
1859 if (gfc_current_state () == COMP_DERIVED
1860 && d != DECL_DIMENSION && d != DECL_POINTER
1861 && d != DECL_COLON && d != DECL_NONE)
1864 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
1865 &seen_at[d]);
1866 m = MATCH_ERROR;
1867 goto cleanup;
1870 switch (d)
1872 case DECL_ALLOCATABLE:
1873 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
1874 break;
1876 case DECL_DIMENSION:
1877 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
1878 break;
1880 case DECL_EXTERNAL:
1881 t = gfc_add_external (&current_attr, &seen_at[d]);
1882 break;
1884 case DECL_IN:
1885 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
1886 break;
1888 case DECL_OUT:
1889 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
1890 break;
1892 case DECL_INOUT:
1893 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
1894 break;
1896 case DECL_INTRINSIC:
1897 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
1898 break;
1900 case DECL_OPTIONAL:
1901 t = gfc_add_optional (&current_attr, &seen_at[d]);
1902 break;
1904 case DECL_PARAMETER:
1905 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
1906 break;
1908 case DECL_POINTER:
1909 t = gfc_add_pointer (&current_attr, &seen_at[d]);
1910 break;
1912 case DECL_PRIVATE:
1913 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
1914 &seen_at[d]);
1915 break;
1917 case DECL_PUBLIC:
1918 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
1919 &seen_at[d]);
1920 break;
1922 case DECL_SAVE:
1923 t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
1924 break;
1926 case DECL_TARGET:
1927 t = gfc_add_target (&current_attr, &seen_at[d]);
1928 break;
1930 default:
1931 gfc_internal_error ("match_attr_spec(): Bad attribute");
1934 if (t == FAILURE)
1936 m = MATCH_ERROR;
1937 goto cleanup;
1941 colon_seen = 1;
1942 return MATCH_YES;
1944 cleanup:
1945 gfc_current_locus = start;
1946 gfc_free_array_spec (current_as);
1947 current_as = NULL;
1948 return m;
1952 /* Match a data declaration statement. */
1954 match
1955 gfc_match_data_decl (void)
1957 gfc_symbol *sym;
1958 match m;
1959 int elem;
1961 m = match_type_spec (&current_ts, 0);
1962 if (m != MATCH_YES)
1963 return m;
1965 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
1967 sym = gfc_use_derived (current_ts.derived);
1969 if (sym == NULL)
1971 m = MATCH_ERROR;
1972 goto cleanup;
1975 current_ts.derived = sym;
1978 m = match_attr_spec ();
1979 if (m == MATCH_ERROR)
1981 m = MATCH_NO;
1982 goto cleanup;
1985 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
1988 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
1989 goto ok;
1991 if (gfc_find_symbol (current_ts.derived->name,
1992 current_ts.derived->ns->parent, 1, &sym) == 0)
1993 goto ok;
1995 /* Hope that an ambiguous symbol is itself masked by a type definition. */
1996 if (sym != NULL && sym->attr.flavor == FL_DERIVED)
1997 goto ok;
1999 gfc_error ("Derived type at %C has not been previously defined");
2000 m = MATCH_ERROR;
2001 goto cleanup;
2005 /* If we have an old-style character declaration, and no new-style
2006 attribute specifications, then there a comma is optional between
2007 the type specification and the variable list. */
2008 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
2009 gfc_match_char (',');
2011 /* Give the types/attributes to symbols that follow. Give the element
2012 a number so that repeat character length expressions can be copied. */
2013 elem = 1;
2014 for (;;)
2016 m = variable_decl (elem++);
2017 if (m == MATCH_ERROR)
2018 goto cleanup;
2019 if (m == MATCH_NO)
2020 break;
2022 if (gfc_match_eos () == MATCH_YES)
2023 goto cleanup;
2024 if (gfc_match_char (',') != MATCH_YES)
2025 break;
2028 gfc_error ("Syntax error in data declaration at %C");
2029 m = MATCH_ERROR;
2031 cleanup:
2032 gfc_free_array_spec (current_as);
2033 current_as = NULL;
2034 return m;
2038 /* Match a prefix associated with a function or subroutine
2039 declaration. If the typespec pointer is nonnull, then a typespec
2040 can be matched. Note that if nothing matches, MATCH_YES is
2041 returned (the null string was matched). */
2043 static match
2044 match_prefix (gfc_typespec * ts)
2046 int seen_type;
2048 gfc_clear_attr (&current_attr);
2049 seen_type = 0;
2051 loop:
2052 if (!seen_type && ts != NULL
2053 && match_type_spec (ts, 0) == MATCH_YES
2054 && gfc_match_space () == MATCH_YES)
2057 seen_type = 1;
2058 goto loop;
2061 if (gfc_match ("elemental% ") == MATCH_YES)
2063 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
2064 return MATCH_ERROR;
2066 goto loop;
2069 if (gfc_match ("pure% ") == MATCH_YES)
2071 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
2072 return MATCH_ERROR;
2074 goto loop;
2077 if (gfc_match ("recursive% ") == MATCH_YES)
2079 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
2080 return MATCH_ERROR;
2082 goto loop;
2085 /* At this point, the next item is not a prefix. */
2086 return MATCH_YES;
2090 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
2092 static try
2093 copy_prefix (symbol_attribute * dest, locus * where)
2096 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
2097 return FAILURE;
2099 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
2100 return FAILURE;
2102 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
2103 return FAILURE;
2105 return SUCCESS;
2109 /* Match a formal argument list. */
2111 match
2112 gfc_match_formal_arglist (gfc_symbol * progname, int st_flag, int null_flag)
2114 gfc_formal_arglist *head, *tail, *p, *q;
2115 char name[GFC_MAX_SYMBOL_LEN + 1];
2116 gfc_symbol *sym;
2117 match m;
2119 head = tail = NULL;
2121 if (gfc_match_char ('(') != MATCH_YES)
2123 if (null_flag)
2124 goto ok;
2125 return MATCH_NO;
2128 if (gfc_match_char (')') == MATCH_YES)
2129 goto ok;
2131 for (;;)
2133 if (gfc_match_char ('*') == MATCH_YES)
2134 sym = NULL;
2135 else
2137 m = gfc_match_name (name);
2138 if (m != MATCH_YES)
2139 goto cleanup;
2141 if (gfc_get_symbol (name, NULL, &sym))
2142 goto cleanup;
2145 p = gfc_get_formal_arglist ();
2147 if (head == NULL)
2148 head = tail = p;
2149 else
2151 tail->next = p;
2152 tail = p;
2155 tail->sym = sym;
2157 /* We don't add the VARIABLE flavor because the name could be a
2158 dummy procedure. We don't apply these attributes to formal
2159 arguments of statement functions. */
2160 if (sym != NULL && !st_flag
2161 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
2162 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
2164 m = MATCH_ERROR;
2165 goto cleanup;
2168 /* The name of a program unit can be in a different namespace,
2169 so check for it explicitly. After the statement is accepted,
2170 the name is checked for especially in gfc_get_symbol(). */
2171 if (gfc_new_block != NULL && sym != NULL
2172 && strcmp (sym->name, gfc_new_block->name) == 0)
2174 gfc_error ("Name '%s' at %C is the name of the procedure",
2175 sym->name);
2176 m = MATCH_ERROR;
2177 goto cleanup;
2180 if (gfc_match_char (')') == MATCH_YES)
2181 goto ok;
2183 m = gfc_match_char (',');
2184 if (m != MATCH_YES)
2186 gfc_error ("Unexpected junk in formal argument list at %C");
2187 goto cleanup;
2192 /* Check for duplicate symbols in the formal argument list. */
2193 if (head != NULL)
2195 for (p = head; p->next; p = p->next)
2197 if (p->sym == NULL)
2198 continue;
2200 for (q = p->next; q; q = q->next)
2201 if (p->sym == q->sym)
2203 gfc_error
2204 ("Duplicate symbol '%s' in formal argument list at %C",
2205 p->sym->name);
2207 m = MATCH_ERROR;
2208 goto cleanup;
2213 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL) ==
2214 FAILURE)
2216 m = MATCH_ERROR;
2217 goto cleanup;
2220 return MATCH_YES;
2222 cleanup:
2223 gfc_free_formal_arglist (head);
2224 return m;
2228 /* Match a RESULT specification following a function declaration or
2229 ENTRY statement. Also matches the end-of-statement. */
2231 static match
2232 match_result (gfc_symbol * function, gfc_symbol ** result)
2234 char name[GFC_MAX_SYMBOL_LEN + 1];
2235 gfc_symbol *r;
2236 match m;
2238 if (gfc_match (" result (") != MATCH_YES)
2239 return MATCH_NO;
2241 m = gfc_match_name (name);
2242 if (m != MATCH_YES)
2243 return m;
2245 if (gfc_match (" )%t") != MATCH_YES)
2247 gfc_error ("Unexpected junk following RESULT variable at %C");
2248 return MATCH_ERROR;
2251 if (strcmp (function->name, name) == 0)
2253 gfc_error
2254 ("RESULT variable at %C must be different than function name");
2255 return MATCH_ERROR;
2258 if (gfc_get_symbol (name, NULL, &r))
2259 return MATCH_ERROR;
2261 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
2262 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
2263 return MATCH_ERROR;
2265 *result = r;
2267 return MATCH_YES;
2271 /* Match a function declaration. */
2273 match
2274 gfc_match_function_decl (void)
2276 char name[GFC_MAX_SYMBOL_LEN + 1];
2277 gfc_symbol *sym, *result;
2278 locus old_loc;
2279 match m;
2281 if (gfc_current_state () != COMP_NONE
2282 && gfc_current_state () != COMP_INTERFACE
2283 && gfc_current_state () != COMP_CONTAINS)
2284 return MATCH_NO;
2286 gfc_clear_ts (&current_ts);
2288 old_loc = gfc_current_locus;
2290 m = match_prefix (&current_ts);
2291 if (m != MATCH_YES)
2293 gfc_current_locus = old_loc;
2294 return m;
2297 if (gfc_match ("function% %n", name) != MATCH_YES)
2299 gfc_current_locus = old_loc;
2300 return MATCH_NO;
2303 if (get_proc_name (name, &sym))
2304 return MATCH_ERROR;
2305 gfc_new_block = sym;
2307 m = gfc_match_formal_arglist (sym, 0, 0);
2308 if (m == MATCH_NO)
2309 gfc_error ("Expected formal argument list in function definition at %C");
2310 else if (m == MATCH_ERROR)
2311 goto cleanup;
2313 result = NULL;
2315 if (gfc_match_eos () != MATCH_YES)
2317 /* See if a result variable is present. */
2318 m = match_result (sym, &result);
2319 if (m == MATCH_NO)
2320 gfc_error ("Unexpected junk after function declaration at %C");
2322 if (m != MATCH_YES)
2324 m = MATCH_ERROR;
2325 goto cleanup;
2329 /* Make changes to the symbol. */
2330 m = MATCH_ERROR;
2332 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2333 goto cleanup;
2335 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
2336 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2337 goto cleanup;
2339 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN)
2341 gfc_error ("Function '%s' at %C already has a type of %s", name,
2342 gfc_basic_typename (sym->ts.type));
2343 goto cleanup;
2346 if (result == NULL)
2348 sym->ts = current_ts;
2349 sym->result = sym;
2351 else
2353 result->ts = current_ts;
2354 sym->result = result;
2357 return MATCH_YES;
2359 cleanup:
2360 gfc_current_locus = old_loc;
2361 return m;
2365 /* Match an ENTRY statement. */
2367 match
2368 gfc_match_entry (void)
2370 gfc_symbol *proc;
2371 gfc_symbol *result;
2372 gfc_symbol *entry;
2373 char name[GFC_MAX_SYMBOL_LEN + 1];
2374 gfc_compile_state state;
2375 match m;
2376 gfc_entry_list *el;
2378 m = gfc_match_name (name);
2379 if (m != MATCH_YES)
2380 return m;
2382 state = gfc_current_state ();
2383 if (state != COMP_SUBROUTINE
2384 && state != COMP_FUNCTION)
2386 gfc_error ("ENTRY statement at %C cannot appear within %s",
2387 gfc_state_name (gfc_current_state ()));
2388 return MATCH_ERROR;
2391 if (gfc_current_ns->parent != NULL
2392 && gfc_current_ns->parent->proc_name
2393 && gfc_current_ns->parent->proc_name->attr.flavor != FL_MODULE)
2395 gfc_error("ENTRY statement at %C cannot appear in a "
2396 "contained procedure");
2397 return MATCH_ERROR;
2400 if (get_proc_name (name, &entry))
2401 return MATCH_ERROR;
2403 proc = gfc_current_block ();
2405 if (state == COMP_SUBROUTINE)
2407 /* An entry in a subroutine. */
2408 m = gfc_match_formal_arglist (entry, 0, 1);
2409 if (m != MATCH_YES)
2410 return MATCH_ERROR;
2412 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
2413 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
2414 return MATCH_ERROR;
2416 else
2418 /* An entry in a function. */
2419 m = gfc_match_formal_arglist (entry, 0, 1);
2420 if (m != MATCH_YES)
2421 return MATCH_ERROR;
2423 result = NULL;
2425 if (gfc_match_eos () == MATCH_YES)
2427 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
2428 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
2429 return MATCH_ERROR;
2431 entry->result = entry;
2433 else
2435 m = match_result (proc, &result);
2436 if (m == MATCH_NO)
2437 gfc_syntax_error (ST_ENTRY);
2438 if (m != MATCH_YES)
2439 return MATCH_ERROR;
2441 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
2442 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
2443 || gfc_add_function (&entry->attr, result->name,
2444 NULL) == FAILURE)
2445 return MATCH_ERROR;
2447 entry->result = result;
2450 if (proc->attr.recursive && result == NULL)
2452 gfc_error ("RESULT attribute required in ENTRY statement at %C");
2453 return MATCH_ERROR;
2457 if (gfc_match_eos () != MATCH_YES)
2459 gfc_syntax_error (ST_ENTRY);
2460 return MATCH_ERROR;
2463 entry->attr.recursive = proc->attr.recursive;
2464 entry->attr.elemental = proc->attr.elemental;
2465 entry->attr.pure = proc->attr.pure;
2467 el = gfc_get_entry_list ();
2468 el->sym = entry;
2469 el->next = gfc_current_ns->entries;
2470 gfc_current_ns->entries = el;
2471 if (el->next)
2472 el->id = el->next->id + 1;
2473 else
2474 el->id = 1;
2476 new_st.op = EXEC_ENTRY;
2477 new_st.ext.entry = el;
2479 return MATCH_YES;
2483 /* Match a subroutine statement, including optional prefixes. */
2485 match
2486 gfc_match_subroutine (void)
2488 char name[GFC_MAX_SYMBOL_LEN + 1];
2489 gfc_symbol *sym;
2490 match m;
2492 if (gfc_current_state () != COMP_NONE
2493 && gfc_current_state () != COMP_INTERFACE
2494 && gfc_current_state () != COMP_CONTAINS)
2495 return MATCH_NO;
2497 m = match_prefix (NULL);
2498 if (m != MATCH_YES)
2499 return m;
2501 m = gfc_match ("subroutine% %n", name);
2502 if (m != MATCH_YES)
2503 return m;
2505 if (get_proc_name (name, &sym))
2506 return MATCH_ERROR;
2507 gfc_new_block = sym;
2509 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
2510 return MATCH_ERROR;
2512 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
2513 return MATCH_ERROR;
2515 if (gfc_match_eos () != MATCH_YES)
2517 gfc_syntax_error (ST_SUBROUTINE);
2518 return MATCH_ERROR;
2521 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2522 return MATCH_ERROR;
2524 return MATCH_YES;
2528 /* Return nonzero if we're currently compiling a contained procedure. */
2530 static int
2531 contained_procedure (void)
2533 gfc_state_data *s;
2535 for (s=gfc_state_stack; s; s=s->previous)
2536 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
2537 && s->previous != NULL
2538 && s->previous->state == COMP_CONTAINS)
2539 return 1;
2541 return 0;
2544 /* Match any of the various end-block statements. Returns the type of
2545 END to the caller. The END INTERFACE, END IF, END DO and END
2546 SELECT statements cannot be replaced by a single END statement. */
2548 match
2549 gfc_match_end (gfc_statement * st)
2551 char name[GFC_MAX_SYMBOL_LEN + 1];
2552 gfc_compile_state state;
2553 locus old_loc;
2554 const char *block_name;
2555 const char *target;
2556 int eos_ok;
2557 match m;
2559 old_loc = gfc_current_locus;
2560 if (gfc_match ("end") != MATCH_YES)
2561 return MATCH_NO;
2563 state = gfc_current_state ();
2564 block_name =
2565 gfc_current_block () == NULL ? NULL : gfc_current_block ()->name;
2567 if (state == COMP_CONTAINS)
2569 state = gfc_state_stack->previous->state;
2570 block_name = gfc_state_stack->previous->sym == NULL ? NULL
2571 : gfc_state_stack->previous->sym->name;
2574 switch (state)
2576 case COMP_NONE:
2577 case COMP_PROGRAM:
2578 *st = ST_END_PROGRAM;
2579 target = " program";
2580 eos_ok = 1;
2581 break;
2583 case COMP_SUBROUTINE:
2584 *st = ST_END_SUBROUTINE;
2585 target = " subroutine";
2586 eos_ok = !contained_procedure ();
2587 break;
2589 case COMP_FUNCTION:
2590 *st = ST_END_FUNCTION;
2591 target = " function";
2592 eos_ok = !contained_procedure ();
2593 break;
2595 case COMP_BLOCK_DATA:
2596 *st = ST_END_BLOCK_DATA;
2597 target = " block data";
2598 eos_ok = 1;
2599 break;
2601 case COMP_MODULE:
2602 *st = ST_END_MODULE;
2603 target = " module";
2604 eos_ok = 1;
2605 break;
2607 case COMP_INTERFACE:
2608 *st = ST_END_INTERFACE;
2609 target = " interface";
2610 eos_ok = 0;
2611 break;
2613 case COMP_DERIVED:
2614 *st = ST_END_TYPE;
2615 target = " type";
2616 eos_ok = 0;
2617 break;
2619 case COMP_IF:
2620 *st = ST_ENDIF;
2621 target = " if";
2622 eos_ok = 0;
2623 break;
2625 case COMP_DO:
2626 *st = ST_ENDDO;
2627 target = " do";
2628 eos_ok = 0;
2629 break;
2631 case COMP_SELECT:
2632 *st = ST_END_SELECT;
2633 target = " select";
2634 eos_ok = 0;
2635 break;
2637 case COMP_FORALL:
2638 *st = ST_END_FORALL;
2639 target = " forall";
2640 eos_ok = 0;
2641 break;
2643 case COMP_WHERE:
2644 *st = ST_END_WHERE;
2645 target = " where";
2646 eos_ok = 0;
2647 break;
2649 default:
2650 gfc_error ("Unexpected END statement at %C");
2651 goto cleanup;
2654 if (gfc_match_eos () == MATCH_YES)
2656 if (!eos_ok)
2658 /* We would have required END [something] */
2659 gfc_error ("%s statement expected at %L",
2660 gfc_ascii_statement (*st), &old_loc);
2661 goto cleanup;
2664 return MATCH_YES;
2667 /* Verify that we've got the sort of end-block that we're expecting. */
2668 if (gfc_match (target) != MATCH_YES)
2670 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
2671 goto cleanup;
2674 /* If we're at the end, make sure a block name wasn't required. */
2675 if (gfc_match_eos () == MATCH_YES)
2678 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT)
2679 return MATCH_YES;
2681 if (gfc_current_block () == NULL)
2682 return MATCH_YES;
2684 gfc_error ("Expected block name of '%s' in %s statement at %C",
2685 block_name, gfc_ascii_statement (*st));
2687 return MATCH_ERROR;
2690 /* END INTERFACE has a special handler for its several possible endings. */
2691 if (*st == ST_END_INTERFACE)
2692 return gfc_match_end_interface ();
2694 /* We haven't hit the end of statement, so what is left must be an end-name. */
2695 m = gfc_match_space ();
2696 if (m == MATCH_YES)
2697 m = gfc_match_name (name);
2699 if (m == MATCH_NO)
2700 gfc_error ("Expected terminating name at %C");
2701 if (m != MATCH_YES)
2702 goto cleanup;
2704 if (block_name == NULL)
2705 goto syntax;
2707 if (strcmp (name, block_name) != 0)
2709 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
2710 gfc_ascii_statement (*st));
2711 goto cleanup;
2714 if (gfc_match_eos () == MATCH_YES)
2715 return MATCH_YES;
2717 syntax:
2718 gfc_syntax_error (*st);
2720 cleanup:
2721 gfc_current_locus = old_loc;
2722 return MATCH_ERROR;
2727 /***************** Attribute declaration statements ****************/
2729 /* Set the attribute of a single variable. */
2731 static match
2732 attr_decl1 (void)
2734 char name[GFC_MAX_SYMBOL_LEN + 1];
2735 gfc_array_spec *as;
2736 gfc_symbol *sym;
2737 locus var_locus;
2738 match m;
2740 as = NULL;
2742 m = gfc_match_name (name);
2743 if (m != MATCH_YES)
2744 goto cleanup;
2746 if (find_special (name, &sym))
2747 return MATCH_ERROR;
2749 var_locus = gfc_current_locus;
2751 /* Deal with possible array specification for certain attributes. */
2752 if (current_attr.dimension
2753 || current_attr.allocatable
2754 || current_attr.pointer
2755 || current_attr.target)
2757 m = gfc_match_array_spec (&as);
2758 if (m == MATCH_ERROR)
2759 goto cleanup;
2761 if (current_attr.dimension && m == MATCH_NO)
2763 gfc_error
2764 ("Missing array specification at %L in DIMENSION statement",
2765 &var_locus);
2766 m = MATCH_ERROR;
2767 goto cleanup;
2770 if ((current_attr.allocatable || current_attr.pointer)
2771 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
2773 gfc_error ("Array specification must be deferred at %L",
2774 &var_locus);
2775 m = MATCH_ERROR;
2776 goto cleanup;
2780 /* Update symbol table. DIMENSION attribute is set in gfc_set_array_spec(). */
2781 if (current_attr.dimension == 0
2782 && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
2784 m = MATCH_ERROR;
2785 goto cleanup;
2788 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
2790 m = MATCH_ERROR;
2791 goto cleanup;
2794 if ((current_attr.external || current_attr.intrinsic)
2795 && sym->attr.flavor != FL_PROCEDURE
2796 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
2798 m = MATCH_ERROR;
2799 goto cleanup;
2802 return MATCH_YES;
2804 cleanup:
2805 gfc_free_array_spec (as);
2806 return m;
2810 /* Generic attribute declaration subroutine. Used for attributes that
2811 just have a list of names. */
2813 static match
2814 attr_decl (void)
2816 match m;
2818 /* Gobble the optional double colon, by simply ignoring the result
2819 of gfc_match(). */
2820 gfc_match (" ::");
2822 for (;;)
2824 m = attr_decl1 ();
2825 if (m != MATCH_YES)
2826 break;
2828 if (gfc_match_eos () == MATCH_YES)
2830 m = MATCH_YES;
2831 break;
2834 if (gfc_match_char (',') != MATCH_YES)
2836 gfc_error ("Unexpected character in variable list at %C");
2837 m = MATCH_ERROR;
2838 break;
2842 return m;
2846 match
2847 gfc_match_external (void)
2850 gfc_clear_attr (&current_attr);
2851 gfc_add_external (&current_attr, NULL);
2853 return attr_decl ();
2858 match
2859 gfc_match_intent (void)
2861 sym_intent intent;
2863 intent = match_intent_spec ();
2864 if (intent == INTENT_UNKNOWN)
2865 return MATCH_ERROR;
2867 gfc_clear_attr (&current_attr);
2868 gfc_add_intent (&current_attr, intent, NULL); /* Can't fail */
2870 return attr_decl ();
2874 match
2875 gfc_match_intrinsic (void)
2878 gfc_clear_attr (&current_attr);
2879 gfc_add_intrinsic (&current_attr, NULL);
2881 return attr_decl ();
2885 match
2886 gfc_match_optional (void)
2889 gfc_clear_attr (&current_attr);
2890 gfc_add_optional (&current_attr, NULL);
2892 return attr_decl ();
2896 match
2897 gfc_match_pointer (void)
2900 gfc_clear_attr (&current_attr);
2901 gfc_add_pointer (&current_attr, NULL);
2903 return attr_decl ();
2907 match
2908 gfc_match_allocatable (void)
2911 gfc_clear_attr (&current_attr);
2912 gfc_add_allocatable (&current_attr, NULL);
2914 return attr_decl ();
2918 match
2919 gfc_match_dimension (void)
2922 gfc_clear_attr (&current_attr);
2923 gfc_add_dimension (&current_attr, NULL, NULL);
2925 return attr_decl ();
2929 match
2930 gfc_match_target (void)
2933 gfc_clear_attr (&current_attr);
2934 gfc_add_target (&current_attr, NULL);
2936 return attr_decl ();
2940 /* Match the list of entities being specified in a PUBLIC or PRIVATE
2941 statement. */
2943 static match
2944 access_attr_decl (gfc_statement st)
2946 char name[GFC_MAX_SYMBOL_LEN + 1];
2947 interface_type type;
2948 gfc_user_op *uop;
2949 gfc_symbol *sym;
2950 gfc_intrinsic_op operator;
2951 match m;
2953 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
2954 goto done;
2956 for (;;)
2958 m = gfc_match_generic_spec (&type, name, &operator);
2959 if (m == MATCH_NO)
2960 goto syntax;
2961 if (m == MATCH_ERROR)
2962 return MATCH_ERROR;
2964 switch (type)
2966 case INTERFACE_NAMELESS:
2967 goto syntax;
2969 case INTERFACE_GENERIC:
2970 if (gfc_get_symbol (name, NULL, &sym))
2971 goto done;
2973 if (gfc_add_access (&sym->attr,
2974 (st ==
2975 ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE,
2976 sym->name, NULL) == FAILURE)
2977 return MATCH_ERROR;
2979 break;
2981 case INTERFACE_INTRINSIC_OP:
2982 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
2984 gfc_current_ns->operator_access[operator] =
2985 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
2987 else
2989 gfc_error ("Access specification of the %s operator at %C has "
2990 "already been specified", gfc_op2string (operator));
2991 goto done;
2994 break;
2996 case INTERFACE_USER_OP:
2997 uop = gfc_get_uop (name);
2999 if (uop->access == ACCESS_UNKNOWN)
3001 uop->access =
3002 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3004 else
3006 gfc_error
3007 ("Access specification of the .%s. operator at %C has "
3008 "already been specified", sym->name);
3009 goto done;
3012 break;
3015 if (gfc_match_char (',') == MATCH_NO)
3016 break;
3019 if (gfc_match_eos () != MATCH_YES)
3020 goto syntax;
3021 return MATCH_YES;
3023 syntax:
3024 gfc_syntax_error (st);
3026 done:
3027 return MATCH_ERROR;
3031 /* The PRIVATE statement is a bit weird in that it can be a attribute
3032 declaration, but also works as a standlone statement inside of a
3033 type declaration or a module. */
3035 match
3036 gfc_match_private (gfc_statement * st)
3039 if (gfc_match ("private") != MATCH_YES)
3040 return MATCH_NO;
3042 if (gfc_current_state () == COMP_DERIVED)
3044 if (gfc_match_eos () == MATCH_YES)
3046 *st = ST_PRIVATE;
3047 return MATCH_YES;
3050 gfc_syntax_error (ST_PRIVATE);
3051 return MATCH_ERROR;
3054 if (gfc_match_eos () == MATCH_YES)
3056 *st = ST_PRIVATE;
3057 return MATCH_YES;
3060 *st = ST_ATTR_DECL;
3061 return access_attr_decl (ST_PRIVATE);
3065 match
3066 gfc_match_public (gfc_statement * st)
3069 if (gfc_match ("public") != MATCH_YES)
3070 return MATCH_NO;
3072 if (gfc_match_eos () == MATCH_YES)
3074 *st = ST_PUBLIC;
3075 return MATCH_YES;
3078 *st = ST_ATTR_DECL;
3079 return access_attr_decl (ST_PUBLIC);
3083 /* Workhorse for gfc_match_parameter. */
3085 static match
3086 do_parm (void)
3088 gfc_symbol *sym;
3089 gfc_expr *init;
3090 match m;
3092 m = gfc_match_symbol (&sym, 0);
3093 if (m == MATCH_NO)
3094 gfc_error ("Expected variable name at %C in PARAMETER statement");
3096 if (m != MATCH_YES)
3097 return m;
3099 if (gfc_match_char ('=') == MATCH_NO)
3101 gfc_error ("Expected = sign in PARAMETER statement at %C");
3102 return MATCH_ERROR;
3105 m = gfc_match_init_expr (&init);
3106 if (m == MATCH_NO)
3107 gfc_error ("Expected expression at %C in PARAMETER statement");
3108 if (m != MATCH_YES)
3109 return m;
3111 if (sym->ts.type == BT_UNKNOWN
3112 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
3114 m = MATCH_ERROR;
3115 goto cleanup;
3118 if (gfc_check_assign_symbol (sym, init) == FAILURE
3119 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
3121 m = MATCH_ERROR;
3122 goto cleanup;
3125 if (sym->ts.type == BT_CHARACTER
3126 && sym->ts.cl != NULL
3127 && sym->ts.cl->length != NULL
3128 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
3129 && init->expr_type == EXPR_CONSTANT
3130 && init->ts.type == BT_CHARACTER
3131 && init->ts.kind == 1)
3132 gfc_set_constant_character_len (
3133 mpz_get_si (sym->ts.cl->length->value.integer), init);
3135 sym->value = init;
3136 return MATCH_YES;
3138 cleanup:
3139 gfc_free_expr (init);
3140 return m;
3144 /* Match a parameter statement, with the weird syntax that these have. */
3146 match
3147 gfc_match_parameter (void)
3149 match m;
3151 if (gfc_match_char ('(') == MATCH_NO)
3152 return MATCH_NO;
3154 for (;;)
3156 m = do_parm ();
3157 if (m != MATCH_YES)
3158 break;
3160 if (gfc_match (" )%t") == MATCH_YES)
3161 break;
3163 if (gfc_match_char (',') != MATCH_YES)
3165 gfc_error ("Unexpected characters in PARAMETER statement at %C");
3166 m = MATCH_ERROR;
3167 break;
3171 return m;
3175 /* Save statements have a special syntax. */
3177 match
3178 gfc_match_save (void)
3180 char n[GFC_MAX_SYMBOL_LEN+1];
3181 gfc_common_head *c;
3182 gfc_symbol *sym;
3183 match m;
3185 if (gfc_match_eos () == MATCH_YES)
3187 if (gfc_current_ns->seen_save)
3189 gfc_error ("Blanket SAVE statement at %C follows previous "
3190 "SAVE statement");
3192 return MATCH_ERROR;
3195 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
3196 return MATCH_YES;
3199 if (gfc_current_ns->save_all)
3201 gfc_error ("SAVE statement at %C follows blanket SAVE statement");
3202 return MATCH_ERROR;
3205 gfc_match (" ::");
3207 for (;;)
3209 m = gfc_match_symbol (&sym, 0);
3210 switch (m)
3212 case MATCH_YES:
3213 if (gfc_add_save (&sym->attr, sym->name,
3214 &gfc_current_locus) == FAILURE)
3215 return MATCH_ERROR;
3216 goto next_item;
3218 case MATCH_NO:
3219 break;
3221 case MATCH_ERROR:
3222 return MATCH_ERROR;
3225 m = gfc_match (" / %n /", &n);
3226 if (m == MATCH_ERROR)
3227 return MATCH_ERROR;
3228 if (m == MATCH_NO)
3229 goto syntax;
3231 c = gfc_get_common (n, 0);
3232 c->saved = 1;
3234 gfc_current_ns->seen_save = 1;
3236 next_item:
3237 if (gfc_match_eos () == MATCH_YES)
3238 break;
3239 if (gfc_match_char (',') != MATCH_YES)
3240 goto syntax;
3243 return MATCH_YES;
3245 syntax:
3246 gfc_error ("Syntax error in SAVE statement at %C");
3247 return MATCH_ERROR;
3251 /* Match a module procedure statement. Note that we have to modify
3252 symbols in the parent's namespace because the current one was there
3253 to receive symbols that are in an interface's formal argument list. */
3255 match
3256 gfc_match_modproc (void)
3258 char name[GFC_MAX_SYMBOL_LEN + 1];
3259 gfc_symbol *sym;
3260 match m;
3262 if (gfc_state_stack->state != COMP_INTERFACE
3263 || gfc_state_stack->previous == NULL
3264 || current_interface.type == INTERFACE_NAMELESS)
3266 gfc_error
3267 ("MODULE PROCEDURE at %C must be in a generic module interface");
3268 return MATCH_ERROR;
3271 for (;;)
3273 m = gfc_match_name (name);
3274 if (m == MATCH_NO)
3275 goto syntax;
3276 if (m != MATCH_YES)
3277 return MATCH_ERROR;
3279 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
3280 return MATCH_ERROR;
3282 if (sym->attr.proc != PROC_MODULE
3283 && gfc_add_procedure (&sym->attr, PROC_MODULE,
3284 sym->name, NULL) == FAILURE)
3285 return MATCH_ERROR;
3287 if (gfc_add_interface (sym) == FAILURE)
3288 return MATCH_ERROR;
3290 if (gfc_match_eos () == MATCH_YES)
3291 break;
3292 if (gfc_match_char (',') != MATCH_YES)
3293 goto syntax;
3296 return MATCH_YES;
3298 syntax:
3299 gfc_syntax_error (ST_MODULE_PROC);
3300 return MATCH_ERROR;
3304 /* Match the beginning of a derived type declaration. If a type name
3305 was the result of a function, then it is possible to have a symbol
3306 already to be known as a derived type yet have no components. */
3308 match
3309 gfc_match_derived_decl (void)
3311 char name[GFC_MAX_SYMBOL_LEN + 1];
3312 symbol_attribute attr;
3313 gfc_symbol *sym;
3314 match m;
3316 if (gfc_current_state () == COMP_DERIVED)
3317 return MATCH_NO;
3319 gfc_clear_attr (&attr);
3321 loop:
3322 if (gfc_match (" , private") == MATCH_YES)
3324 if (gfc_find_state (COMP_MODULE) == FAILURE)
3326 gfc_error
3327 ("Derived type at %C can only be PRIVATE within a MODULE");
3328 return MATCH_ERROR;
3331 if (gfc_add_access (&attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
3332 return MATCH_ERROR;
3333 goto loop;
3336 if (gfc_match (" , public") == MATCH_YES)
3338 if (gfc_find_state (COMP_MODULE) == FAILURE)
3340 gfc_error ("Derived type at %C can only be PUBLIC within a MODULE");
3341 return MATCH_ERROR;
3344 if (gfc_add_access (&attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
3345 return MATCH_ERROR;
3346 goto loop;
3349 if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
3351 gfc_error ("Expected :: in TYPE definition at %C");
3352 return MATCH_ERROR;
3355 m = gfc_match (" %n%t", name);
3356 if (m != MATCH_YES)
3357 return m;
3359 /* Make sure the name isn't the name of an intrinsic type. The
3360 'double precision' type doesn't get past the name matcher. */
3361 if (strcmp (name, "integer") == 0
3362 || strcmp (name, "real") == 0
3363 || strcmp (name, "character") == 0
3364 || strcmp (name, "logical") == 0
3365 || strcmp (name, "complex") == 0)
3367 gfc_error
3368 ("Type name '%s' at %C cannot be the same as an intrinsic type",
3369 name);
3370 return MATCH_ERROR;
3373 if (gfc_get_symbol (name, NULL, &sym))
3374 return MATCH_ERROR;
3376 if (sym->ts.type != BT_UNKNOWN)
3378 gfc_error ("Derived type name '%s' at %C already has a basic type "
3379 "of %s", sym->name, gfc_typename (&sym->ts));
3380 return MATCH_ERROR;
3383 /* The symbol may already have the derived attribute without the
3384 components. The ways this can happen is via a function
3385 definition, an INTRINSIC statement or a subtype in another
3386 derived type that is a pointer. The first part of the AND clause
3387 is true if a the symbol is not the return value of a function. */
3388 if (sym->attr.flavor != FL_DERIVED
3389 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
3390 return MATCH_ERROR;
3392 if (sym->components != NULL)
3394 gfc_error
3395 ("Derived type definition of '%s' at %C has already been defined",
3396 sym->name);
3397 return MATCH_ERROR;
3400 if (attr.access != ACCESS_UNKNOWN
3401 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
3402 return MATCH_ERROR;
3404 gfc_new_block = sym;
3406 return MATCH_YES;