Merged with mainline at revision 126229.
[official-gcc.git] / gcc / fortran / decl.c
blob24f1a3d1b59521b4b45b9fc405339dcecc4b41f8
1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA. */
23 #include "config.h"
24 #include "system.h"
25 #include "gfortran.h"
26 #include "match.h"
27 #include "parse.h"
29 /* This flag is set if an old-style length selector is matched
30 during a type-declaration statement. */
32 static int old_char_selector;
34 /* When variables acquire types and attributes from a declaration
35 statement, they get them from the following static variables. The
36 first part of a declaration sets these variables and the second
37 part copies these into symbol structures. */
39 static gfc_typespec current_ts;
41 static symbol_attribute current_attr;
42 static gfc_array_spec *current_as;
43 static int colon_seen;
45 /* The current binding label (if any). */
46 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
47 /* Need to know how many identifiers are on the current data declaration
48 line in case we're given the BIND(C) attribute with a NAME= specifier. */
49 static int num_idents_on_line;
50 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
51 can supply a name if the curr_binding_label is nil and NAME= was not. */
52 static int has_name_equals = 0;
54 /* Initializer of the previous enumerator. */
56 static gfc_expr *last_initializer;
58 /* History of all the enumerators is maintained, so that
59 kind values of all the enumerators could be updated depending
60 upon the maximum initialized value. */
62 typedef struct enumerator_history
64 gfc_symbol *sym;
65 gfc_expr *initializer;
66 struct enumerator_history *next;
68 enumerator_history;
70 /* Header of enum history chain. */
72 static enumerator_history *enum_history = NULL;
74 /* Pointer of enum history node containing largest initializer. */
76 static enumerator_history *max_enum = NULL;
78 /* gfc_new_block points to the symbol of a newly matched block. */
80 gfc_symbol *gfc_new_block;
83 /********************* DATA statement subroutines *********************/
85 static bool in_match_data = false;
87 bool
88 gfc_in_match_data (void)
90 return in_match_data;
93 void
94 gfc_set_in_match_data (bool set_value)
96 in_match_data = set_value;
99 /* Free a gfc_data_variable structure and everything beneath it. */
101 static void
102 free_variable (gfc_data_variable *p)
104 gfc_data_variable *q;
106 for (; p; p = q)
108 q = p->next;
109 gfc_free_expr (p->expr);
110 gfc_free_iterator (&p->iter, 0);
111 free_variable (p->list);
112 gfc_free (p);
117 /* Free a gfc_data_value structure and everything beneath it. */
119 static void
120 free_value (gfc_data_value *p)
122 gfc_data_value *q;
124 for (; p; p = q)
126 q = p->next;
127 gfc_free_expr (p->expr);
128 gfc_free (p);
133 /* Free a list of gfc_data structures. */
135 void
136 gfc_free_data (gfc_data *p)
138 gfc_data *q;
140 for (; p; p = q)
142 q = p->next;
143 free_variable (p->var);
144 free_value (p->value);
145 gfc_free (p);
150 /* Free all data in a namespace. */
152 static void
153 gfc_free_data_all (gfc_namespace *ns)
155 gfc_data *d;
157 for (;ns->data;)
159 d = ns->data->next;
160 gfc_free (ns->data);
161 ns->data = d;
166 static match var_element (gfc_data_variable *);
168 /* Match a list of variables terminated by an iterator and a right
169 parenthesis. */
171 static match
172 var_list (gfc_data_variable *parent)
174 gfc_data_variable *tail, var;
175 match m;
177 m = var_element (&var);
178 if (m == MATCH_ERROR)
179 return MATCH_ERROR;
180 if (m == MATCH_NO)
181 goto syntax;
183 tail = gfc_get_data_variable ();
184 *tail = var;
186 parent->list = tail;
188 for (;;)
190 if (gfc_match_char (',') != MATCH_YES)
191 goto syntax;
193 m = gfc_match_iterator (&parent->iter, 1);
194 if (m == MATCH_YES)
195 break;
196 if (m == MATCH_ERROR)
197 return MATCH_ERROR;
199 m = var_element (&var);
200 if (m == MATCH_ERROR)
201 return MATCH_ERROR;
202 if (m == MATCH_NO)
203 goto syntax;
205 tail->next = gfc_get_data_variable ();
206 tail = tail->next;
208 *tail = var;
211 if (gfc_match_char (')') != MATCH_YES)
212 goto syntax;
213 return MATCH_YES;
215 syntax:
216 gfc_syntax_error (ST_DATA);
217 return MATCH_ERROR;
221 /* Match a single element in a data variable list, which can be a
222 variable-iterator list. */
224 static match
225 var_element (gfc_data_variable *new)
227 match m;
228 gfc_symbol *sym;
230 memset (new, 0, sizeof (gfc_data_variable));
232 if (gfc_match_char ('(') == MATCH_YES)
233 return var_list (new);
235 m = gfc_match_variable (&new->expr, 0);
236 if (m != MATCH_YES)
237 return m;
239 sym = new->expr->symtree->n.sym;
241 if (!sym->attr.function && gfc_current_ns->parent
242 && gfc_current_ns->parent == sym->ns)
244 gfc_error ("Host associated variable '%s' may not be in the DATA "
245 "statement at %C", sym->name);
246 return MATCH_ERROR;
249 if (gfc_current_state () != COMP_BLOCK_DATA
250 && sym->attr.in_common
251 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
252 "common block variable '%s' in DATA statement at %C",
253 sym->name) == FAILURE)
254 return MATCH_ERROR;
256 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
257 return MATCH_ERROR;
259 return MATCH_YES;
263 /* Match the top-level list of data variables. */
265 static match
266 top_var_list (gfc_data *d)
268 gfc_data_variable var, *tail, *new;
269 match m;
271 tail = NULL;
273 for (;;)
275 m = var_element (&var);
276 if (m == MATCH_NO)
277 goto syntax;
278 if (m == MATCH_ERROR)
279 return MATCH_ERROR;
281 new = gfc_get_data_variable ();
282 *new = var;
284 if (tail == NULL)
285 d->var = new;
286 else
287 tail->next = new;
289 tail = new;
291 if (gfc_match_char ('/') == MATCH_YES)
292 break;
293 if (gfc_match_char (',') != MATCH_YES)
294 goto syntax;
297 return MATCH_YES;
299 syntax:
300 gfc_syntax_error (ST_DATA);
301 gfc_free_data_all (gfc_current_ns);
302 return MATCH_ERROR;
306 static match
307 match_data_constant (gfc_expr **result)
309 char name[GFC_MAX_SYMBOL_LEN + 1];
310 gfc_symbol *sym;
311 gfc_expr *expr;
312 match m;
313 locus old_loc;
315 m = gfc_match_literal_constant (&expr, 1);
316 if (m == MATCH_YES)
318 *result = expr;
319 return MATCH_YES;
322 if (m == MATCH_ERROR)
323 return MATCH_ERROR;
325 m = gfc_match_null (result);
326 if (m != MATCH_NO)
327 return m;
329 old_loc = gfc_current_locus;
331 /* Should this be a structure component, try to match it
332 before matching a name. */
333 m = gfc_match_rvalue (result);
334 if (m == MATCH_ERROR)
335 return m;
337 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
339 if (gfc_simplify_expr (*result, 0) == FAILURE)
340 m = MATCH_ERROR;
341 return m;
344 gfc_current_locus = old_loc;
346 m = gfc_match_name (name);
347 if (m != MATCH_YES)
348 return m;
350 if (gfc_find_symbol (name, NULL, 1, &sym))
351 return MATCH_ERROR;
353 if (sym == NULL
354 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
356 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
357 name);
358 return MATCH_ERROR;
360 else if (sym->attr.flavor == FL_DERIVED)
361 return gfc_match_structure_constructor (sym, result);
363 *result = gfc_copy_expr (sym->value);
364 return MATCH_YES;
368 /* Match a list of values in a DATA statement. The leading '/' has
369 already been seen at this point. */
371 static match
372 top_val_list (gfc_data *data)
374 gfc_data_value *new, *tail;
375 gfc_expr *expr;
376 const char *msg;
377 match m;
379 tail = NULL;
381 for (;;)
383 m = match_data_constant (&expr);
384 if (m == MATCH_NO)
385 goto syntax;
386 if (m == MATCH_ERROR)
387 return MATCH_ERROR;
389 new = gfc_get_data_value ();
391 if (tail == NULL)
392 data->value = new;
393 else
394 tail->next = new;
396 tail = new;
398 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
400 tail->expr = expr;
401 tail->repeat = 1;
403 else
405 signed int tmp;
406 msg = gfc_extract_int (expr, &tmp);
407 gfc_free_expr (expr);
408 if (msg != NULL)
410 gfc_error (msg);
411 return MATCH_ERROR;
413 tail->repeat = tmp;
415 m = match_data_constant (&tail->expr);
416 if (m == MATCH_NO)
417 goto syntax;
418 if (m == MATCH_ERROR)
419 return MATCH_ERROR;
422 if (gfc_match_char ('/') == MATCH_YES)
423 break;
424 if (gfc_match_char (',') == MATCH_NO)
425 goto syntax;
428 return MATCH_YES;
430 syntax:
431 gfc_syntax_error (ST_DATA);
432 gfc_free_data_all (gfc_current_ns);
433 return MATCH_ERROR;
437 /* Matches an old style initialization. */
439 static match
440 match_old_style_init (const char *name)
442 match m;
443 gfc_symtree *st;
444 gfc_symbol *sym;
445 gfc_data *newdata;
447 /* Set up data structure to hold initializers. */
448 gfc_find_sym_tree (name, NULL, 0, &st);
449 sym = st->n.sym;
451 newdata = gfc_get_data ();
452 newdata->var = gfc_get_data_variable ();
453 newdata->var->expr = gfc_get_variable_expr (st);
454 newdata->where = gfc_current_locus;
456 /* Match initial value list. This also eats the terminal '/'. */
457 m = top_val_list (newdata);
458 if (m != MATCH_YES)
460 gfc_free (newdata);
461 return m;
464 if (gfc_pure (NULL))
466 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
467 gfc_free (newdata);
468 return MATCH_ERROR;
471 /* Mark the variable as having appeared in a data statement. */
472 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
474 gfc_free (newdata);
475 return MATCH_ERROR;
478 /* Chain in namespace list of DATA initializers. */
479 newdata->next = gfc_current_ns->data;
480 gfc_current_ns->data = newdata;
482 return m;
486 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
487 we are matching a DATA statement and are therefore issuing an error
488 if we encounter something unexpected, if not, we're trying to match
489 an old-style initialization expression of the form INTEGER I /2/. */
491 match
492 gfc_match_data (void)
494 gfc_data *new;
495 match m;
497 gfc_set_in_match_data (true);
499 for (;;)
501 new = gfc_get_data ();
502 new->where = gfc_current_locus;
504 m = top_var_list (new);
505 if (m != MATCH_YES)
506 goto cleanup;
508 m = top_val_list (new);
509 if (m != MATCH_YES)
510 goto cleanup;
512 new->next = gfc_current_ns->data;
513 gfc_current_ns->data = new;
515 if (gfc_match_eos () == MATCH_YES)
516 break;
518 gfc_match_char (','); /* Optional comma */
521 gfc_set_in_match_data (false);
523 if (gfc_pure (NULL))
525 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
526 return MATCH_ERROR;
529 return MATCH_YES;
531 cleanup:
532 gfc_set_in_match_data (false);
533 gfc_free_data (new);
534 return MATCH_ERROR;
538 /************************ Declaration statements *********************/
540 /* Match an intent specification. Since this can only happen after an
541 INTENT word, a legal intent-spec must follow. */
543 static sym_intent
544 match_intent_spec (void)
547 if (gfc_match (" ( in out )") == MATCH_YES)
548 return INTENT_INOUT;
549 if (gfc_match (" ( in )") == MATCH_YES)
550 return INTENT_IN;
551 if (gfc_match (" ( out )") == MATCH_YES)
552 return INTENT_OUT;
554 gfc_error ("Bad INTENT specification at %C");
555 return INTENT_UNKNOWN;
559 /* Matches a character length specification, which is either a
560 specification expression or a '*'. */
562 static match
563 char_len_param_value (gfc_expr **expr)
565 if (gfc_match_char ('*') == MATCH_YES)
567 *expr = NULL;
568 return MATCH_YES;
571 return gfc_match_expr (expr);
575 /* A character length is a '*' followed by a literal integer or a
576 char_len_param_value in parenthesis. */
578 static match
579 match_char_length (gfc_expr **expr)
581 int length;
582 match m;
584 m = gfc_match_char ('*');
585 if (m != MATCH_YES)
586 return m;
588 m = gfc_match_small_literal_int (&length, NULL);
589 if (m == MATCH_ERROR)
590 return m;
592 if (m == MATCH_YES)
594 *expr = gfc_int_expr (length);
595 return m;
598 if (gfc_match_char ('(') == MATCH_NO)
599 goto syntax;
601 m = char_len_param_value (expr);
602 if (m == MATCH_ERROR)
603 return m;
604 if (m == MATCH_NO)
605 goto syntax;
607 if (gfc_match_char (')') == MATCH_NO)
609 gfc_free_expr (*expr);
610 *expr = NULL;
611 goto syntax;
614 return MATCH_YES;
616 syntax:
617 gfc_error ("Syntax error in character length specification at %C");
618 return MATCH_ERROR;
622 /* Special subroutine for finding a symbol. Check if the name is found
623 in the current name space. If not, and we're compiling a function or
624 subroutine and the parent compilation unit is an interface, then check
625 to see if the name we've been given is the name of the interface
626 (located in another namespace). */
628 static int
629 find_special (const char *name, gfc_symbol **result)
631 gfc_state_data *s;
632 int i;
634 i = gfc_get_symbol (name, NULL, result);
635 if (i == 0)
636 goto end;
638 if (gfc_current_state () != COMP_SUBROUTINE
639 && gfc_current_state () != COMP_FUNCTION)
640 goto end;
642 s = gfc_state_stack->previous;
643 if (s == NULL)
644 goto end;
646 if (s->state != COMP_INTERFACE)
647 goto end;
648 if (s->sym == NULL)
649 goto end; /* Nameless interface. */
651 if (strcmp (name, s->sym->name) == 0)
653 *result = s->sym;
654 return 0;
657 end:
658 return i;
662 /* Special subroutine for getting a symbol node associated with a
663 procedure name, used in SUBROUTINE and FUNCTION statements. The
664 symbol is created in the parent using with symtree node in the
665 child unit pointing to the symbol. If the current namespace has no
666 parent, then the symbol is just created in the current unit. */
668 static int
669 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
671 gfc_symtree *st;
672 gfc_symbol *sym;
673 int rc;
675 /* Module functions have to be left in their own namespace because
676 they have potentially (almost certainly!) already been referenced.
677 In this sense, they are rather like external functions. This is
678 fixed up in resolve.c(resolve_entries), where the symbol name-
679 space is set to point to the master function, so that the fake
680 result mechanism can work. */
681 if (module_fcn_entry)
683 /* Present if entry is declared to be a module procedure. */
684 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
685 if (*result == NULL)
686 rc = gfc_get_symbol (name, NULL, result);
688 else
689 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
691 sym = *result;
692 gfc_current_ns->refs++;
694 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
696 /* Trap another encompassed procedure with the same name. All
697 these conditions are necessary to avoid picking up an entry
698 whose name clashes with that of the encompassing procedure;
699 this is handled using gsymbols to register unique,globally
700 accessible names. */
701 if (sym->attr.flavor != 0
702 && sym->attr.proc != 0
703 && (sym->attr.subroutine || sym->attr.function)
704 && sym->attr.if_source != IFSRC_UNKNOWN)
705 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
706 name, &sym->declared_at);
708 /* Trap a procedure with a name the same as interface in the
709 encompassing scope. */
710 if (sym->attr.generic != 0
711 && (sym->attr.subroutine || sym->attr.function))
712 gfc_error_now ("Name '%s' at %C is already defined"
713 " as a generic interface at %L",
714 name, &sym->declared_at);
716 /* Trap declarations of attributes in encompassing scope. The
717 signature for this is that ts.kind is set. Legitimate
718 references only set ts.type. */
719 if (sym->ts.kind != 0
720 && !sym->attr.implicit_type
721 && sym->attr.proc == 0
722 && gfc_current_ns->parent != NULL
723 && sym->attr.access == 0
724 && !module_fcn_entry)
725 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
726 "and must not have attributes declared at %L",
727 name, &sym->declared_at);
730 if (gfc_current_ns->parent == NULL || *result == NULL)
731 return rc;
733 /* Module function entries will already have a symtree in
734 the current namespace but will need one at module level. */
735 if (module_fcn_entry)
737 /* Present if entry is declared to be a module procedure. */
738 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
739 if (st == NULL)
740 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
742 else
743 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
745 st->n.sym = sym;
746 sym->refs++;
748 /* See if the procedure should be a module procedure. */
750 if (((sym->ns->proc_name != NULL
751 && sym->ns->proc_name->attr.flavor == FL_MODULE
752 && sym->attr.proc != PROC_MODULE)
753 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
754 && gfc_add_procedure (&sym->attr, PROC_MODULE,
755 sym->name, NULL) == FAILURE)
756 rc = 2;
758 return rc;
762 /* Verify that the given symbol representing a parameter is C
763 interoperable, by checking to see if it was marked as such after
764 its declaration. If the given symbol is not interoperable, a
765 warning is reported, thus removing the need to return the status to
766 the calling function. The standard does not require the user use
767 one of the iso_c_binding named constants to declare an
768 interoperable parameter, but we can't be sure if the param is C
769 interop or not if the user doesn't. For example, integer(4) may be
770 legal Fortran, but doesn't have meaning in C. It may interop with
771 a number of the C types, which causes a problem because the
772 compiler can't know which one. This code is almost certainly not
773 portable, and the user will get what they deserve if the C type
774 across platforms isn't always interoperable with integer(4). If
775 the user had used something like integer(c_int) or integer(c_long),
776 the compiler could have automatically handled the varying sizes
777 across platforms. */
780 verify_c_interop_param (gfc_symbol *sym)
782 int is_c_interop = 0;
783 try retval = SUCCESS;
785 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
786 Don't repeat the checks here. */
787 if (sym->attr.implicit_type)
788 return SUCCESS;
790 /* For subroutines or functions that are passed to a BIND(C) procedure,
791 they're interoperable if they're BIND(C) and their params are all
792 interoperable. */
793 if (sym->attr.flavor == FL_PROCEDURE)
795 if (sym->attr.is_bind_c == 0)
797 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
798 "attribute to be C interoperable", sym->name,
799 &(sym->declared_at));
801 return FAILURE;
803 else
805 if (sym->attr.is_c_interop == 1)
806 /* We've already checked this procedure; don't check it again. */
807 return SUCCESS;
808 else
809 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
810 sym->common_block);
814 /* See if we've stored a reference to a procedure that owns sym. */
815 if (sym->ns != NULL && sym->ns->proc_name != NULL)
817 if (sym->ns->proc_name->attr.is_bind_c == 1)
819 is_c_interop =
820 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
821 == SUCCESS ? 1 : 0);
823 if (is_c_interop != 1)
825 /* Make personalized messages to give better feedback. */
826 if (sym->ts.type == BT_DERIVED)
827 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
828 " procedure '%s' but is not C interoperable "
829 "because derived type '%s' is not C interoperable",
830 sym->name, &(sym->declared_at),
831 sym->ns->proc_name->name,
832 sym->ts.derived->name);
833 else
834 gfc_warning ("Variable '%s' at %L is a parameter to the "
835 "BIND(C) procedure '%s' but may not be C "
836 "interoperable",
837 sym->name, &(sym->declared_at),
838 sym->ns->proc_name->name);
841 /* We have to make sure that any param to a bind(c) routine does
842 not have the allocatable, pointer, or optional attributes,
843 according to J3/04-007, section 5.1. */
844 if (sym->attr.allocatable == 1)
846 gfc_error ("Variable '%s' at %L cannot have the "
847 "ALLOCATABLE attribute because procedure '%s'"
848 " is BIND(C)", sym->name, &(sym->declared_at),
849 sym->ns->proc_name->name);
850 retval = FAILURE;
853 if (sym->attr.pointer == 1)
855 gfc_error ("Variable '%s' at %L cannot have the "
856 "POINTER attribute because procedure '%s'"
857 " is BIND(C)", sym->name, &(sym->declared_at),
858 sym->ns->proc_name->name);
859 retval = FAILURE;
862 if (sym->attr.optional == 1)
864 gfc_error ("Variable '%s' at %L cannot have the "
865 "OPTIONAL attribute because procedure '%s'"
866 " is BIND(C)", sym->name, &(sym->declared_at),
867 sym->ns->proc_name->name);
868 retval = FAILURE;
871 /* Make sure that if it has the dimension attribute, that it is
872 either assumed size or explicit shape. */
873 if (sym->as != NULL)
875 if (sym->as->type == AS_ASSUMED_SHAPE)
877 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
878 "argument to the procedure '%s' at %L because "
879 "the procedure is BIND(C)", sym->name,
880 &(sym->declared_at), sym->ns->proc_name->name,
881 &(sym->ns->proc_name->declared_at));
882 retval = FAILURE;
885 if (sym->as->type == AS_DEFERRED)
887 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
888 "argument to the procedure '%s' at %L because "
889 "the procedure is BIND(C)", sym->name,
890 &(sym->declared_at), sym->ns->proc_name->name,
891 &(sym->ns->proc_name->declared_at));
892 retval = FAILURE;
898 return retval;
902 /* Function called by variable_decl() that adds a name to the symbol table. */
904 static try
905 build_sym (const char *name, gfc_charlen *cl,
906 gfc_array_spec **as, locus *var_locus)
908 symbol_attribute attr;
909 gfc_symbol *sym;
911 if (gfc_get_symbol (name, NULL, &sym))
912 return FAILURE;
914 /* Start updating the symbol table. Add basic type attribute if present. */
915 if (current_ts.type != BT_UNKNOWN
916 && (sym->attr.implicit_type == 0
917 || !gfc_compare_types (&sym->ts, &current_ts))
918 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
919 return FAILURE;
921 if (sym->ts.type == BT_CHARACTER)
922 sym->ts.cl = cl;
924 /* Add dimension attribute if present. */
925 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
926 return FAILURE;
927 *as = NULL;
929 /* Add attribute to symbol. The copy is so that we can reset the
930 dimension attribute. */
931 attr = current_attr;
932 attr.dimension = 0;
934 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
935 return FAILURE;
937 /* Finish any work that may need to be done for the binding label,
938 if it's a bind(c). The bind(c) attr is found before the symbol
939 is made, and before the symbol name (for data decls), so the
940 current_ts is holding the binding label, or nothing if the
941 name= attr wasn't given. Therefore, test here if we're dealing
942 with a bind(c) and make sure the binding label is set correctly. */
943 if (sym->attr.is_bind_c == 1)
945 if (sym->binding_label[0] == '\0')
947 /* Here, we're not checking the numIdents (the last param).
948 This could be an error we're letting slip through! */
949 if (set_binding_label (sym->binding_label, sym->name, 1) == FAILURE)
950 return FAILURE;
954 /* See if we know we're in a common block, and if it's a bind(c)
955 common then we need to make sure we're an interoperable type. */
956 if (sym->attr.in_common == 1)
958 /* Test the common block object. */
959 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
960 && sym->ts.is_c_interop != 1)
962 gfc_error_now ("Variable '%s' in common block '%s' at %C "
963 "must be declared with a C interoperable "
964 "kind since common block '%s' is BIND(C)",
965 sym->name, sym->common_block->name,
966 sym->common_block->name);
967 gfc_clear_error ();
971 sym->attr.implied_index = 0;
973 return SUCCESS;
977 /* Set character constant to the given length. The constant will be padded or
978 truncated. */
980 void
981 gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
983 char *s;
984 int slen;
986 gcc_assert (expr->expr_type == EXPR_CONSTANT);
987 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
989 slen = expr->value.character.length;
990 if (len != slen)
992 s = gfc_getmem (len + 1);
993 memcpy (s, expr->value.character.string, MIN (len, slen));
994 if (len > slen)
995 memset (&s[slen], ' ', len - slen);
997 if (gfc_option.warn_character_truncation && slen > len)
998 gfc_warning_now ("CHARACTER expression at %L is being truncated "
999 "(%d/%d)", &expr->where, slen, len);
1001 /* Apply the standard by 'hand' otherwise it gets cleared for
1002 initializers. */
1003 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
1004 gfc_error_now ("The CHARACTER elements of the array constructor "
1005 "at %L must have the same length (%d/%d)",
1006 &expr->where, slen, len);
1008 s[len] = '\0';
1009 gfc_free (expr->value.character.string);
1010 expr->value.character.string = s;
1011 expr->value.character.length = len;
1016 /* Function to create and update the enumerator history
1017 using the information passed as arguments.
1018 Pointer "max_enum" is also updated, to point to
1019 enum history node containing largest initializer.
1021 SYM points to the symbol node of enumerator.
1022 INIT points to its enumerator value. */
1024 static void
1025 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1027 enumerator_history *new_enum_history;
1028 gcc_assert (sym != NULL && init != NULL);
1030 new_enum_history = gfc_getmem (sizeof (enumerator_history));
1032 new_enum_history->sym = sym;
1033 new_enum_history->initializer = init;
1034 new_enum_history->next = NULL;
1036 if (enum_history == NULL)
1038 enum_history = new_enum_history;
1039 max_enum = enum_history;
1041 else
1043 new_enum_history->next = enum_history;
1044 enum_history = new_enum_history;
1046 if (mpz_cmp (max_enum->initializer->value.integer,
1047 new_enum_history->initializer->value.integer) < 0)
1048 max_enum = new_enum_history;
1053 /* Function to free enum kind history. */
1055 void
1056 gfc_free_enum_history (void)
1058 enumerator_history *current = enum_history;
1059 enumerator_history *next;
1061 while (current != NULL)
1063 next = current->next;
1064 gfc_free (current);
1065 current = next;
1067 max_enum = NULL;
1068 enum_history = NULL;
1072 /* Function called by variable_decl() that adds an initialization
1073 expression to a symbol. */
1075 static try
1076 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1078 symbol_attribute attr;
1079 gfc_symbol *sym;
1080 gfc_expr *init;
1082 init = *initp;
1083 if (find_special (name, &sym))
1084 return FAILURE;
1086 attr = sym->attr;
1088 /* If this symbol is confirming an implicit parameter type,
1089 then an initialization expression is not allowed. */
1090 if (attr.flavor == FL_PARAMETER
1091 && sym->value != NULL
1092 && *initp != NULL)
1094 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1095 sym->name);
1096 return FAILURE;
1099 if (attr.in_common
1100 && !attr.data
1101 && *initp != NULL)
1103 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
1104 sym->name);
1105 return FAILURE;
1108 if (init == NULL)
1110 /* An initializer is required for PARAMETER declarations. */
1111 if (attr.flavor == FL_PARAMETER)
1113 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1114 return FAILURE;
1117 else
1119 /* If a variable appears in a DATA block, it cannot have an
1120 initializer. */
1121 if (sym->attr.data)
1123 gfc_error ("Variable '%s' at %C with an initializer already "
1124 "appears in a DATA statement", sym->name);
1125 return FAILURE;
1128 /* Check if the assignment can happen. This has to be put off
1129 until later for a derived type variable. */
1130 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1131 && gfc_check_assign_symbol (sym, init) == FAILURE)
1132 return FAILURE;
1134 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1136 /* Update symbol character length according initializer. */
1137 if (sym->ts.cl->length == NULL)
1139 /* If there are multiple CHARACTER variables declared on the
1140 same line, we don't want them to share the same length. */
1141 sym->ts.cl = gfc_get_charlen ();
1142 sym->ts.cl->next = gfc_current_ns->cl_list;
1143 gfc_current_ns->cl_list = sym->ts.cl;
1145 if (sym->attr.flavor == FL_PARAMETER
1146 && init->expr_type == EXPR_ARRAY)
1147 sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
1149 /* Update initializer character length according symbol. */
1150 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1152 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1153 gfc_constructor * p;
1155 if (init->expr_type == EXPR_CONSTANT)
1156 gfc_set_constant_character_len (len, init, false);
1157 else if (init->expr_type == EXPR_ARRAY)
1159 /* Build a new charlen to prevent simplification from
1160 deleting the length before it is resolved. */
1161 init->ts.cl = gfc_get_charlen ();
1162 init->ts.cl->next = gfc_current_ns->cl_list;
1163 gfc_current_ns->cl_list = sym->ts.cl;
1164 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
1166 for (p = init->value.constructor; p; p = p->next)
1167 gfc_set_constant_character_len (len, p->expr, false);
1172 /* Need to check if the expression we initialized this
1173 to was one of the iso_c_binding named constants. If so,
1174 and we're a parameter (constant), let it be iso_c.
1175 For example:
1176 integer(c_int), parameter :: my_int = c_int
1177 integer(my_int) :: my_int_2
1178 If we mark my_int as iso_c (since we can see it's value
1179 is equal to one of the named constants), then my_int_2
1180 will be considered C interoperable. */
1181 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1183 sym->ts.is_iso_c |= init->ts.is_iso_c;
1184 sym->ts.is_c_interop |= init->ts.is_c_interop;
1185 /* attr bits needed for module files. */
1186 sym->attr.is_iso_c |= init->ts.is_iso_c;
1187 sym->attr.is_c_interop |= init->ts.is_c_interop;
1188 if (init->ts.is_iso_c)
1189 sym->ts.f90_type = init->ts.f90_type;
1192 /* Add initializer. Make sure we keep the ranks sane. */
1193 if (sym->attr.dimension && init->rank == 0)
1195 mpz_t size;
1196 gfc_expr *array;
1197 gfc_constructor *c;
1198 int n;
1199 if (sym->attr.flavor == FL_PARAMETER
1200 && init->expr_type == EXPR_CONSTANT
1201 && spec_size (sym->as, &size) == SUCCESS
1202 && mpz_cmp_si (size, 0) > 0)
1204 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1205 &init->where);
1207 array->value.constructor = c = NULL;
1208 for (n = 0; n < (int)mpz_get_si (size); n++)
1210 if (array->value.constructor == NULL)
1212 array->value.constructor = c = gfc_get_constructor ();
1213 c->expr = init;
1215 else
1217 c->next = gfc_get_constructor ();
1218 c = c->next;
1219 c->expr = gfc_copy_expr (init);
1223 array->shape = gfc_get_shape (sym->as->rank);
1224 for (n = 0; n < sym->as->rank; n++)
1225 spec_dimen_size (sym->as, n, &array->shape[n]);
1227 init = array;
1228 mpz_clear (size);
1230 init->rank = sym->as->rank;
1233 sym->value = init;
1234 *initp = NULL;
1237 return SUCCESS;
1241 /* Function called by variable_decl() that adds a name to a structure
1242 being built. */
1244 static try
1245 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1246 gfc_array_spec **as)
1248 gfc_component *c;
1250 /* If the current symbol is of the same derived type that we're
1251 constructing, it must have the pointer attribute. */
1252 if (current_ts.type == BT_DERIVED
1253 && current_ts.derived == gfc_current_block ()
1254 && current_attr.pointer == 0)
1256 gfc_error ("Component at %C must have the POINTER attribute");
1257 return FAILURE;
1260 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1262 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1264 gfc_error ("Array component of structure at %C must have explicit "
1265 "or deferred shape");
1266 return FAILURE;
1270 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1271 return FAILURE;
1273 c->ts = current_ts;
1274 c->ts.cl = cl;
1275 gfc_set_component_attr (c, &current_attr);
1277 c->initializer = *init;
1278 *init = NULL;
1280 c->as = *as;
1281 if (c->as != NULL)
1282 c->dimension = 1;
1283 *as = NULL;
1285 /* Check array components. */
1286 if (!c->dimension)
1288 if (c->allocatable)
1290 gfc_error ("Allocatable component at %C must be an array");
1291 return FAILURE;
1293 else
1294 return SUCCESS;
1297 if (c->pointer)
1299 if (c->as->type != AS_DEFERRED)
1301 gfc_error ("Pointer array component of structure at %C must have a "
1302 "deferred shape");
1303 return FAILURE;
1306 else if (c->allocatable)
1308 if (c->as->type != AS_DEFERRED)
1310 gfc_error ("Allocatable component of structure at %C must have a "
1311 "deferred shape");
1312 return FAILURE;
1315 else
1317 if (c->as->type != AS_EXPLICIT)
1319 gfc_error ("Array component of structure at %C must have an "
1320 "explicit shape");
1321 return FAILURE;
1325 return SUCCESS;
1329 /* Match a 'NULL()', and possibly take care of some side effects. */
1331 match
1332 gfc_match_null (gfc_expr **result)
1334 gfc_symbol *sym;
1335 gfc_expr *e;
1336 match m;
1338 m = gfc_match (" null ( )");
1339 if (m != MATCH_YES)
1340 return m;
1342 /* The NULL symbol now has to be/become an intrinsic function. */
1343 if (gfc_get_symbol ("null", NULL, &sym))
1345 gfc_error ("NULL() initialization at %C is ambiguous");
1346 return MATCH_ERROR;
1349 gfc_intrinsic_symbol (sym);
1351 if (sym->attr.proc != PROC_INTRINSIC
1352 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1353 sym->name, NULL) == FAILURE
1354 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1355 return MATCH_ERROR;
1357 e = gfc_get_expr ();
1358 e->where = gfc_current_locus;
1359 e->expr_type = EXPR_NULL;
1360 e->ts.type = BT_UNKNOWN;
1362 *result = e;
1364 return MATCH_YES;
1368 /* Match a variable name with an optional initializer. When this
1369 subroutine is called, a variable is expected to be parsed next.
1370 Depending on what is happening at the moment, updates either the
1371 symbol table or the current interface. */
1373 static match
1374 variable_decl (int elem)
1376 char name[GFC_MAX_SYMBOL_LEN + 1];
1377 gfc_expr *initializer, *char_len;
1378 gfc_array_spec *as;
1379 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1380 gfc_charlen *cl;
1381 locus var_locus;
1382 match m;
1383 try t;
1384 gfc_symbol *sym;
1385 locus old_locus;
1387 initializer = NULL;
1388 as = NULL;
1389 cp_as = NULL;
1390 old_locus = gfc_current_locus;
1392 /* When we get here, we've just matched a list of attributes and
1393 maybe a type and a double colon. The next thing we expect to see
1394 is the name of the symbol. */
1395 m = gfc_match_name (name);
1396 if (m != MATCH_YES)
1397 goto cleanup;
1399 var_locus = gfc_current_locus;
1401 /* Now we could see the optional array spec. or character length. */
1402 m = gfc_match_array_spec (&as);
1403 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1404 cp_as = gfc_copy_array_spec (as);
1405 else if (m == MATCH_ERROR)
1406 goto cleanup;
1408 if (m == MATCH_NO)
1409 as = gfc_copy_array_spec (current_as);
1411 char_len = NULL;
1412 cl = NULL;
1414 if (current_ts.type == BT_CHARACTER)
1416 switch (match_char_length (&char_len))
1418 case MATCH_YES:
1419 cl = gfc_get_charlen ();
1420 cl->next = gfc_current_ns->cl_list;
1421 gfc_current_ns->cl_list = cl;
1423 cl->length = char_len;
1424 break;
1426 /* Non-constant lengths need to be copied after the first
1427 element. */
1428 case MATCH_NO:
1429 if (elem > 1 && current_ts.cl->length
1430 && current_ts.cl->length->expr_type != EXPR_CONSTANT)
1432 cl = gfc_get_charlen ();
1433 cl->next = gfc_current_ns->cl_list;
1434 gfc_current_ns->cl_list = cl;
1435 cl->length = gfc_copy_expr (current_ts.cl->length);
1437 else
1438 cl = current_ts.cl;
1440 break;
1442 case MATCH_ERROR:
1443 goto cleanup;
1447 /* If this symbol has already shown up in a Cray Pointer declaration,
1448 then we want to set the type & bail out. */
1449 if (gfc_option.flag_cray_pointer)
1451 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1452 if (sym != NULL && sym->attr.cray_pointee)
1454 sym->ts.type = current_ts.type;
1455 sym->ts.kind = current_ts.kind;
1456 sym->ts.cl = cl;
1457 sym->ts.derived = current_ts.derived;
1458 sym->ts.is_c_interop = current_ts.is_c_interop;
1459 sym->ts.is_iso_c = current_ts.is_iso_c;
1460 m = MATCH_YES;
1462 /* Check to see if we have an array specification. */
1463 if (cp_as != NULL)
1465 if (sym->as != NULL)
1467 gfc_error ("Duplicate array spec for Cray pointee at %C");
1468 gfc_free_array_spec (cp_as);
1469 m = MATCH_ERROR;
1470 goto cleanup;
1472 else
1474 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1475 gfc_internal_error ("Couldn't set pointee array spec.");
1477 /* Fix the array spec. */
1478 m = gfc_mod_pointee_as (sym->as);
1479 if (m == MATCH_ERROR)
1480 goto cleanup;
1483 goto cleanup;
1485 else
1487 gfc_free_array_spec (cp_as);
1492 /* OK, we've successfully matched the declaration. Now put the
1493 symbol in the current namespace, because it might be used in the
1494 optional initialization expression for this symbol, e.g. this is
1495 perfectly legal:
1497 integer, parameter :: i = huge(i)
1499 This is only true for parameters or variables of a basic type.
1500 For components of derived types, it is not true, so we don't
1501 create a symbol for those yet. If we fail to create the symbol,
1502 bail out. */
1503 if (gfc_current_state () != COMP_DERIVED
1504 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1506 m = MATCH_ERROR;
1507 goto cleanup;
1510 /* An interface body specifies all of the procedure's
1511 characteristics and these shall be consistent with those
1512 specified in the procedure definition, except that the interface
1513 may specify a procedure that is not pure if the procedure is
1514 defined to be pure(12.3.2). */
1515 if (current_ts.type == BT_DERIVED
1516 && gfc_current_ns->proc_name
1517 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1518 && current_ts.derived->ns != gfc_current_ns
1519 && !gfc_current_ns->has_import_set)
1521 gfc_error ("the type of '%s' at %C has not been declared within the "
1522 "interface", name);
1523 m = MATCH_ERROR;
1524 goto cleanup;
1527 /* In functions that have a RESULT variable defined, the function
1528 name always refers to function calls. Therefore, the name is
1529 not allowed to appear in specification statements. */
1530 if (gfc_current_state () == COMP_FUNCTION
1531 && gfc_current_block () != NULL
1532 && gfc_current_block ()->result != NULL
1533 && gfc_current_block ()->result != gfc_current_block ()
1534 && strcmp (gfc_current_block ()->name, name) == 0)
1536 gfc_error ("Function name '%s' not allowed at %C", name);
1537 m = MATCH_ERROR;
1538 goto cleanup;
1541 /* We allow old-style initializations of the form
1542 integer i /2/, j(4) /3*3, 1/
1543 (if no colon has been seen). These are different from data
1544 statements in that initializers are only allowed to apply to the
1545 variable immediately preceding, i.e.
1546 integer i, j /1, 2/
1547 is not allowed. Therefore we have to do some work manually, that
1548 could otherwise be left to the matchers for DATA statements. */
1550 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1552 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1553 "initialization at %C") == FAILURE)
1554 return MATCH_ERROR;
1556 return match_old_style_init (name);
1559 /* The double colon must be present in order to have initializers.
1560 Otherwise the statement is ambiguous with an assignment statement. */
1561 if (colon_seen)
1563 if (gfc_match (" =>") == MATCH_YES)
1565 if (!current_attr.pointer)
1567 gfc_error ("Initialization at %C isn't for a pointer variable");
1568 m = MATCH_ERROR;
1569 goto cleanup;
1572 m = gfc_match_null (&initializer);
1573 if (m == MATCH_NO)
1575 gfc_error ("Pointer initialization requires a NULL() at %C");
1576 m = MATCH_ERROR;
1579 if (gfc_pure (NULL))
1581 gfc_error ("Initialization of pointer at %C is not allowed in "
1582 "a PURE procedure");
1583 m = MATCH_ERROR;
1586 if (m != MATCH_YES)
1587 goto cleanup;
1590 else if (gfc_match_char ('=') == MATCH_YES)
1592 if (current_attr.pointer)
1594 gfc_error ("Pointer initialization at %C requires '=>', "
1595 "not '='");
1596 m = MATCH_ERROR;
1597 goto cleanup;
1600 m = gfc_match_init_expr (&initializer);
1601 if (m == MATCH_NO)
1603 gfc_error ("Expected an initialization expression at %C");
1604 m = MATCH_ERROR;
1607 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1609 gfc_error ("Initialization of variable at %C is not allowed in "
1610 "a PURE procedure");
1611 m = MATCH_ERROR;
1614 if (m != MATCH_YES)
1615 goto cleanup;
1619 if (initializer != NULL && current_attr.allocatable
1620 && gfc_current_state () == COMP_DERIVED)
1622 gfc_error ("Initialization of allocatable component at %C is not "
1623 "allowed");
1624 m = MATCH_ERROR;
1625 goto cleanup;
1628 /* Add the initializer. Note that it is fine if initializer is
1629 NULL here, because we sometimes also need to check if a
1630 declaration *must* have an initialization expression. */
1631 if (gfc_current_state () != COMP_DERIVED)
1632 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1633 else
1635 if (current_ts.type == BT_DERIVED
1636 && !current_attr.pointer && !initializer)
1637 initializer = gfc_default_initializer (&current_ts);
1638 t = build_struct (name, cl, &initializer, &as);
1641 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1643 cleanup:
1644 /* Free stuff up and return. */
1645 gfc_free_expr (initializer);
1646 gfc_free_array_spec (as);
1648 return m;
1652 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1653 This assumes that the byte size is equal to the kind number for
1654 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1656 match
1657 gfc_match_old_kind_spec (gfc_typespec *ts)
1659 match m;
1660 int original_kind;
1662 if (gfc_match_char ('*') != MATCH_YES)
1663 return MATCH_NO;
1665 m = gfc_match_small_literal_int (&ts->kind, NULL);
1666 if (m != MATCH_YES)
1667 return MATCH_ERROR;
1669 original_kind = ts->kind;
1671 /* Massage the kind numbers for complex types. */
1672 if (ts->type == BT_COMPLEX)
1674 if (ts->kind % 2)
1676 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1677 gfc_basic_typename (ts->type), original_kind);
1678 return MATCH_ERROR;
1680 ts->kind /= 2;
1683 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1685 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1686 gfc_basic_typename (ts->type), original_kind);
1687 return MATCH_ERROR;
1690 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1691 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1692 return MATCH_ERROR;
1694 return MATCH_YES;
1698 /* Match a kind specification. Since kinds are generally optional, we
1699 usually return MATCH_NO if something goes wrong. If a "kind="
1700 string is found, then we know we have an error. */
1702 match
1703 gfc_match_kind_spec (gfc_typespec *ts)
1705 locus where;
1706 gfc_expr *e;
1707 match m, n;
1708 const char *msg;
1710 m = MATCH_NO;
1711 e = NULL;
1713 where = gfc_current_locus;
1715 if (gfc_match_char ('(') == MATCH_NO)
1716 return MATCH_NO;
1718 /* Also gobbles optional text. */
1719 if (gfc_match (" kind = ") == MATCH_YES)
1720 m = MATCH_ERROR;
1722 n = gfc_match_init_expr (&e);
1723 if (n == MATCH_NO)
1724 gfc_error ("Expected initialization expression at %C");
1725 if (n != MATCH_YES)
1726 return MATCH_ERROR;
1728 if (e->rank != 0)
1730 gfc_error ("Expected scalar initialization expression at %C");
1731 m = MATCH_ERROR;
1732 goto no_match;
1735 msg = gfc_extract_int (e, &ts->kind);
1736 if (msg != NULL)
1738 gfc_error (msg);
1739 m = MATCH_ERROR;
1740 goto no_match;
1743 /* Before throwing away the expression, let's see if we had a
1744 C interoperable kind (and store the fact). */
1745 if (e->ts.is_c_interop == 1)
1747 /* Mark this as c interoperable if being declared with one
1748 of the named constants from iso_c_binding. */
1749 ts->is_c_interop = e->ts.is_iso_c;
1750 ts->f90_type = e->ts.f90_type;
1753 gfc_free_expr (e);
1754 e = NULL;
1756 /* Ignore errors to this point, if we've gotten here. This means
1757 we ignore the m=MATCH_ERROR from above. */
1758 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1760 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1761 gfc_basic_typename (ts->type));
1762 m = MATCH_ERROR;
1764 else if (gfc_match_char (')') != MATCH_YES)
1766 gfc_error ("Missing right parenthesis at %C");
1767 m = MATCH_ERROR;
1769 else
1770 /* All tests passed. */
1771 m = MATCH_YES;
1773 if(m == MATCH_ERROR)
1774 gfc_current_locus = where;
1776 /* Return what we know from the test(s). */
1777 return m;
1779 no_match:
1780 gfc_free_expr (e);
1781 gfc_current_locus = where;
1782 return m;
1786 /* Match the various kind/length specifications in a CHARACTER
1787 declaration. We don't return MATCH_NO. */
1789 static match
1790 match_char_spec (gfc_typespec *ts)
1792 int kind, seen_length;
1793 gfc_charlen *cl;
1794 gfc_expr *len;
1795 match m;
1796 gfc_expr *kind_expr = NULL;
1797 kind = gfc_default_character_kind;
1798 len = NULL;
1799 seen_length = 0;
1801 /* Try the old-style specification first. */
1802 old_char_selector = 0;
1804 m = match_char_length (&len);
1805 if (m != MATCH_NO)
1807 if (m == MATCH_YES)
1808 old_char_selector = 1;
1809 seen_length = 1;
1810 goto done;
1813 m = gfc_match_char ('(');
1814 if (m != MATCH_YES)
1816 m = MATCH_YES; /* Character without length is a single char. */
1817 goto done;
1820 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
1821 if (gfc_match (" kind =") == MATCH_YES)
1823 m = gfc_match_small_int_expr(&kind, &kind_expr);
1825 if (m == MATCH_ERROR)
1826 goto done;
1827 if (m == MATCH_NO)
1828 goto syntax;
1830 if (gfc_match (" , len =") == MATCH_NO)
1831 goto rparen;
1833 m = char_len_param_value (&len);
1834 if (m == MATCH_NO)
1835 goto syntax;
1836 if (m == MATCH_ERROR)
1837 goto done;
1838 seen_length = 1;
1840 goto rparen;
1843 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
1844 if (gfc_match (" len =") == MATCH_YES)
1846 m = char_len_param_value (&len);
1847 if (m == MATCH_NO)
1848 goto syntax;
1849 if (m == MATCH_ERROR)
1850 goto done;
1851 seen_length = 1;
1853 if (gfc_match_char (')') == MATCH_YES)
1854 goto done;
1856 if (gfc_match (" , kind =") != MATCH_YES)
1857 goto syntax;
1859 gfc_match_small_int_expr(&kind, &kind_expr);
1861 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1863 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1864 return MATCH_YES;
1867 goto rparen;
1870 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
1871 m = char_len_param_value (&len);
1872 if (m == MATCH_NO)
1873 goto syntax;
1874 if (m == MATCH_ERROR)
1875 goto done;
1876 seen_length = 1;
1878 m = gfc_match_char (')');
1879 if (m == MATCH_YES)
1880 goto done;
1882 if (gfc_match_char (',') != MATCH_YES)
1883 goto syntax;
1885 gfc_match (" kind ="); /* Gobble optional text. */
1887 m = gfc_match_small_int_expr(&kind, &kind_expr);
1888 if (m == MATCH_ERROR)
1889 goto done;
1890 if (m == MATCH_NO)
1891 goto syntax;
1893 rparen:
1894 /* Require a right-paren at this point. */
1895 m = gfc_match_char (')');
1896 if (m == MATCH_YES)
1897 goto done;
1899 syntax:
1900 gfc_error ("Syntax error in CHARACTER declaration at %C");
1901 m = MATCH_ERROR;
1902 gfc_free_expr (len);
1903 return m;
1905 done:
1906 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1908 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1909 m = MATCH_ERROR;
1912 if (seen_length == 1 && len != NULL
1913 && len->ts.type != BT_INTEGER && len->ts.type != BT_UNKNOWN)
1915 gfc_error ("Expression at %C must be of INTEGER type");
1916 m = MATCH_ERROR;
1919 if (m != MATCH_YES)
1921 gfc_free_expr (len);
1922 gfc_free_expr (kind_expr);
1923 return m;
1926 /* Do some final massaging of the length values. */
1927 cl = gfc_get_charlen ();
1928 cl->next = gfc_current_ns->cl_list;
1929 gfc_current_ns->cl_list = cl;
1931 if (seen_length == 0)
1932 cl->length = gfc_int_expr (1);
1933 else
1934 cl->length = len;
1936 ts->cl = cl;
1937 ts->kind = kind;
1939 /* We have to know if it was a c interoperable kind so we can
1940 do accurate type checking of bind(c) procs, etc. */
1941 if (kind_expr != NULL)
1943 /* Mark this as c interoperable if being declared with one
1944 of the named constants from iso_c_binding. */
1945 ts->is_c_interop = kind_expr->ts.is_iso_c;
1946 gfc_free_expr (kind_expr);
1948 else if (len != NULL)
1950 /* Here, we might have parsed something such as:
1951 character(c_char)
1952 In this case, the parsing code above grabs the c_char when
1953 looking for the length (line 1690, roughly). it's the last
1954 testcase for parsing the kind params of a character variable.
1955 However, it's not actually the length. this seems like it
1956 could be an error.
1957 To see if the user used a C interop kind, test the expr
1958 of the so called length, and see if it's C interoperable. */
1959 ts->is_c_interop = len->ts.is_iso_c;
1962 return MATCH_YES;
1966 /* Matches a type specification. If successful, sets the ts structure
1967 to the matched specification. This is necessary for FUNCTION and
1968 IMPLICIT statements.
1970 If implicit_flag is nonzero, then we don't check for the optional
1971 kind specification. Not doing so is needed for matching an IMPLICIT
1972 statement correctly. */
1974 static match
1975 match_type_spec (gfc_typespec *ts, int implicit_flag)
1977 char name[GFC_MAX_SYMBOL_LEN + 1];
1978 gfc_symbol *sym;
1979 match m;
1980 int c;
1982 gfc_clear_ts (ts);
1984 /* Clear the current binding label, in case one is given. */
1985 curr_binding_label[0] = '\0';
1987 if (gfc_match (" byte") == MATCH_YES)
1989 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
1990 == FAILURE)
1991 return MATCH_ERROR;
1993 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
1995 gfc_error ("BYTE type used at %C "
1996 "is not available on the target machine");
1997 return MATCH_ERROR;
2000 ts->type = BT_INTEGER;
2001 ts->kind = 1;
2002 return MATCH_YES;
2005 if (gfc_match (" integer") == MATCH_YES)
2007 ts->type = BT_INTEGER;
2008 ts->kind = gfc_default_integer_kind;
2009 goto get_kind;
2012 if (gfc_match (" character") == MATCH_YES)
2014 ts->type = BT_CHARACTER;
2015 if (implicit_flag == 0)
2016 return match_char_spec (ts);
2017 else
2018 return MATCH_YES;
2021 if (gfc_match (" real") == MATCH_YES)
2023 ts->type = BT_REAL;
2024 ts->kind = gfc_default_real_kind;
2025 goto get_kind;
2028 if (gfc_match (" double precision") == MATCH_YES)
2030 ts->type = BT_REAL;
2031 ts->kind = gfc_default_double_kind;
2032 return MATCH_YES;
2035 if (gfc_match (" complex") == MATCH_YES)
2037 ts->type = BT_COMPLEX;
2038 ts->kind = gfc_default_complex_kind;
2039 goto get_kind;
2042 if (gfc_match (" double complex") == MATCH_YES)
2044 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2045 "conform to the Fortran 95 standard") == FAILURE)
2046 return MATCH_ERROR;
2048 ts->type = BT_COMPLEX;
2049 ts->kind = gfc_default_double_kind;
2050 return MATCH_YES;
2053 if (gfc_match (" logical") == MATCH_YES)
2055 ts->type = BT_LOGICAL;
2056 ts->kind = gfc_default_logical_kind;
2057 goto get_kind;
2060 m = gfc_match (" type ( %n )", name);
2061 if (m != MATCH_YES)
2062 return m;
2064 /* Search for the name but allow the components to be defined later. */
2065 if (gfc_get_ha_symbol (name, &sym))
2067 gfc_error ("Type name '%s' at %C is ambiguous", name);
2068 return MATCH_ERROR;
2071 if (sym->attr.flavor != FL_DERIVED
2072 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2073 return MATCH_ERROR;
2075 ts->type = BT_DERIVED;
2076 ts->kind = 0;
2077 ts->derived = sym;
2079 return MATCH_YES;
2081 get_kind:
2082 /* For all types except double, derived and character, look for an
2083 optional kind specifier. MATCH_NO is actually OK at this point. */
2084 if (implicit_flag == 1)
2085 return MATCH_YES;
2087 if (gfc_current_form == FORM_FREE)
2089 c = gfc_peek_char();
2090 if (!gfc_is_whitespace(c) && c != '*' && c != '('
2091 && c != ':' && c != ',')
2092 return MATCH_NO;
2095 m = gfc_match_kind_spec (ts);
2096 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2097 m = gfc_match_old_kind_spec (ts);
2099 if (m == MATCH_NO)
2100 m = MATCH_YES; /* No kind specifier found. */
2102 return m;
2106 /* Match an IMPLICIT NONE statement. Actually, this statement is
2107 already matched in parse.c, or we would not end up here in the
2108 first place. So the only thing we need to check, is if there is
2109 trailing garbage. If not, the match is successful. */
2111 match
2112 gfc_match_implicit_none (void)
2114 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2118 /* Match the letter range(s) of an IMPLICIT statement. */
2120 static match
2121 match_implicit_range (void)
2123 int c, c1, c2, inner;
2124 locus cur_loc;
2126 cur_loc = gfc_current_locus;
2128 gfc_gobble_whitespace ();
2129 c = gfc_next_char ();
2130 if (c != '(')
2132 gfc_error ("Missing character range in IMPLICIT at %C");
2133 goto bad;
2136 inner = 1;
2137 while (inner)
2139 gfc_gobble_whitespace ();
2140 c1 = gfc_next_char ();
2141 if (!ISALPHA (c1))
2142 goto bad;
2144 gfc_gobble_whitespace ();
2145 c = gfc_next_char ();
2147 switch (c)
2149 case ')':
2150 inner = 0; /* Fall through. */
2152 case ',':
2153 c2 = c1;
2154 break;
2156 case '-':
2157 gfc_gobble_whitespace ();
2158 c2 = gfc_next_char ();
2159 if (!ISALPHA (c2))
2160 goto bad;
2162 gfc_gobble_whitespace ();
2163 c = gfc_next_char ();
2165 if ((c != ',') && (c != ')'))
2166 goto bad;
2167 if (c == ')')
2168 inner = 0;
2170 break;
2172 default:
2173 goto bad;
2176 if (c1 > c2)
2178 gfc_error ("Letters must be in alphabetic order in "
2179 "IMPLICIT statement at %C");
2180 goto bad;
2183 /* See if we can add the newly matched range to the pending
2184 implicits from this IMPLICIT statement. We do not check for
2185 conflicts with whatever earlier IMPLICIT statements may have
2186 set. This is done when we've successfully finished matching
2187 the current one. */
2188 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2189 goto bad;
2192 return MATCH_YES;
2194 bad:
2195 gfc_syntax_error (ST_IMPLICIT);
2197 gfc_current_locus = cur_loc;
2198 return MATCH_ERROR;
2202 /* Match an IMPLICIT statement, storing the types for
2203 gfc_set_implicit() if the statement is accepted by the parser.
2204 There is a strange looking, but legal syntactic construction
2205 possible. It looks like:
2207 IMPLICIT INTEGER (a-b) (c-d)
2209 This is legal if "a-b" is a constant expression that happens to
2210 equal one of the legal kinds for integers. The real problem
2211 happens with an implicit specification that looks like:
2213 IMPLICIT INTEGER (a-b)
2215 In this case, a typespec matcher that is "greedy" (as most of the
2216 matchers are) gobbles the character range as a kindspec, leaving
2217 nothing left. We therefore have to go a bit more slowly in the
2218 matching process by inhibiting the kindspec checking during
2219 typespec matching and checking for a kind later. */
2221 match
2222 gfc_match_implicit (void)
2224 gfc_typespec ts;
2225 locus cur_loc;
2226 int c;
2227 match m;
2229 /* We don't allow empty implicit statements. */
2230 if (gfc_match_eos () == MATCH_YES)
2232 gfc_error ("Empty IMPLICIT statement at %C");
2233 return MATCH_ERROR;
2238 /* First cleanup. */
2239 gfc_clear_new_implicit ();
2241 /* A basic type is mandatory here. */
2242 m = match_type_spec (&ts, 1);
2243 if (m == MATCH_ERROR)
2244 goto error;
2245 if (m == MATCH_NO)
2246 goto syntax;
2248 cur_loc = gfc_current_locus;
2249 m = match_implicit_range ();
2251 if (m == MATCH_YES)
2253 /* We may have <TYPE> (<RANGE>). */
2254 gfc_gobble_whitespace ();
2255 c = gfc_next_char ();
2256 if ((c == '\n') || (c == ','))
2258 /* Check for CHARACTER with no length parameter. */
2259 if (ts.type == BT_CHARACTER && !ts.cl)
2261 ts.kind = gfc_default_character_kind;
2262 ts.cl = gfc_get_charlen ();
2263 ts.cl->next = gfc_current_ns->cl_list;
2264 gfc_current_ns->cl_list = ts.cl;
2265 ts.cl->length = gfc_int_expr (1);
2268 /* Record the Successful match. */
2269 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2270 return MATCH_ERROR;
2271 continue;
2274 gfc_current_locus = cur_loc;
2277 /* Discard the (incorrectly) matched range. */
2278 gfc_clear_new_implicit ();
2280 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2281 if (ts.type == BT_CHARACTER)
2282 m = match_char_spec (&ts);
2283 else
2285 m = gfc_match_kind_spec (&ts);
2286 if (m == MATCH_NO)
2288 m = gfc_match_old_kind_spec (&ts);
2289 if (m == MATCH_ERROR)
2290 goto error;
2291 if (m == MATCH_NO)
2292 goto syntax;
2295 if (m == MATCH_ERROR)
2296 goto error;
2298 m = match_implicit_range ();
2299 if (m == MATCH_ERROR)
2300 goto error;
2301 if (m == MATCH_NO)
2302 goto syntax;
2304 gfc_gobble_whitespace ();
2305 c = gfc_next_char ();
2306 if ((c != '\n') && (c != ','))
2307 goto syntax;
2309 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2310 return MATCH_ERROR;
2312 while (c == ',');
2314 return MATCH_YES;
2316 syntax:
2317 gfc_syntax_error (ST_IMPLICIT);
2319 error:
2320 return MATCH_ERROR;
2324 match
2325 gfc_match_import (void)
2327 char name[GFC_MAX_SYMBOL_LEN + 1];
2328 match m;
2329 gfc_symbol *sym;
2330 gfc_symtree *st;
2332 if (gfc_current_ns->proc_name == NULL
2333 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2335 gfc_error ("IMPORT statement at %C only permitted in "
2336 "an INTERFACE body");
2337 return MATCH_ERROR;
2340 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2341 == FAILURE)
2342 return MATCH_ERROR;
2344 if (gfc_match_eos () == MATCH_YES)
2346 /* All host variables should be imported. */
2347 gfc_current_ns->has_import_set = 1;
2348 return MATCH_YES;
2351 if (gfc_match (" ::") == MATCH_YES)
2353 if (gfc_match_eos () == MATCH_YES)
2355 gfc_error ("Expecting list of named entities at %C");
2356 return MATCH_ERROR;
2360 for(;;)
2362 m = gfc_match (" %n", name);
2363 switch (m)
2365 case MATCH_YES:
2366 if (gfc_current_ns->parent != NULL
2367 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2369 gfc_error ("Type name '%s' at %C is ambiguous", name);
2370 return MATCH_ERROR;
2372 else if (gfc_current_ns->proc_name->ns->parent != NULL
2373 && gfc_find_symbol (name,
2374 gfc_current_ns->proc_name->ns->parent,
2375 1, &sym))
2377 gfc_error ("Type name '%s' at %C is ambiguous", name);
2378 return MATCH_ERROR;
2381 if (sym == NULL)
2383 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2384 "at %C - does not exist.", name);
2385 return MATCH_ERROR;
2388 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2390 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2391 "at %C.", name);
2392 goto next_item;
2395 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2396 st->n.sym = sym;
2397 sym->refs++;
2398 sym->ns = gfc_current_ns;
2400 goto next_item;
2402 case MATCH_NO:
2403 break;
2405 case MATCH_ERROR:
2406 return MATCH_ERROR;
2409 next_item:
2410 if (gfc_match_eos () == MATCH_YES)
2411 break;
2412 if (gfc_match_char (',') != MATCH_YES)
2413 goto syntax;
2416 return MATCH_YES;
2418 syntax:
2419 gfc_error ("Syntax error in IMPORT statement at %C");
2420 return MATCH_ERROR;
2424 /* Matches an attribute specification including array specs. If
2425 successful, leaves the variables current_attr and current_as
2426 holding the specification. Also sets the colon_seen variable for
2427 later use by matchers associated with initializations.
2429 This subroutine is a little tricky in the sense that we don't know
2430 if we really have an attr-spec until we hit the double colon.
2431 Until that time, we can only return MATCH_NO. This forces us to
2432 check for duplicate specification at this level. */
2434 static match
2435 match_attr_spec (void)
2437 /* Modifiers that can exist in a type statement. */
2438 typedef enum
2439 { GFC_DECL_BEGIN = 0,
2440 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2441 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2442 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2443 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2444 DECL_IS_BIND_C, DECL_COLON, DECL_NONE,
2445 GFC_DECL_END /* Sentinel */
2447 decl_types;
2449 /* GFC_DECL_END is the sentinel, index starts at 0. */
2450 #define NUM_DECL GFC_DECL_END
2452 static mstring decls[] = {
2453 minit (", allocatable", DECL_ALLOCATABLE),
2454 minit (", dimension", DECL_DIMENSION),
2455 minit (", external", DECL_EXTERNAL),
2456 minit (", intent ( in )", DECL_IN),
2457 minit (", intent ( out )", DECL_OUT),
2458 minit (", intent ( in out )", DECL_INOUT),
2459 minit (", intrinsic", DECL_INTRINSIC),
2460 minit (", optional", DECL_OPTIONAL),
2461 minit (", parameter", DECL_PARAMETER),
2462 minit (", pointer", DECL_POINTER),
2463 minit (", protected", DECL_PROTECTED),
2464 minit (", private", DECL_PRIVATE),
2465 minit (", public", DECL_PUBLIC),
2466 minit (", save", DECL_SAVE),
2467 minit (", target", DECL_TARGET),
2468 minit (", value", DECL_VALUE),
2469 minit (", volatile", DECL_VOLATILE),
2470 minit ("::", DECL_COLON),
2471 minit (NULL, DECL_NONE)
2474 locus start, seen_at[NUM_DECL];
2475 int seen[NUM_DECL];
2476 decl_types d;
2477 const char *attr;
2478 match m;
2479 try t;
2480 char peek_char;
2482 gfc_clear_attr (&current_attr);
2483 start = gfc_current_locus;
2485 current_as = NULL;
2486 colon_seen = 0;
2488 /* See if we get all of the keywords up to the final double colon. */
2489 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2490 seen[d] = 0;
2492 for (;;)
2494 d = (decl_types) gfc_match_strings (decls);
2496 if (d == DECL_NONE)
2498 /* See if we can find the bind(c) since all else failed.
2499 We need to skip over any whitespace and stop on the ','. */
2500 gfc_gobble_whitespace ();
2501 peek_char = gfc_peek_char ();
2502 if (peek_char == ',')
2504 /* Chomp the comma. */
2505 peek_char = gfc_next_char ();
2506 /* Try and match the bind(c). */
2507 if (gfc_match_bind_c (NULL) == MATCH_YES)
2508 d = DECL_IS_BIND_C;
2509 else
2511 return MATCH_ERROR;
2516 if (d == DECL_NONE || d == DECL_COLON)
2517 break;
2519 seen[d]++;
2520 seen_at[d] = gfc_current_locus;
2522 if (d == DECL_DIMENSION)
2524 m = gfc_match_array_spec (&current_as);
2526 if (m == MATCH_NO)
2528 gfc_error ("Missing dimension specification at %C");
2529 m = MATCH_ERROR;
2532 if (m == MATCH_ERROR)
2533 goto cleanup;
2537 /* No double colon, so assume that we've been looking at something
2538 else the whole time. */
2539 if (d == DECL_NONE)
2541 m = MATCH_NO;
2542 goto cleanup;
2545 /* Since we've seen a double colon, we have to be looking at an
2546 attr-spec. This means that we can now issue errors. */
2547 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2548 if (seen[d] > 1)
2550 switch (d)
2552 case DECL_ALLOCATABLE:
2553 attr = "ALLOCATABLE";
2554 break;
2555 case DECL_DIMENSION:
2556 attr = "DIMENSION";
2557 break;
2558 case DECL_EXTERNAL:
2559 attr = "EXTERNAL";
2560 break;
2561 case DECL_IN:
2562 attr = "INTENT (IN)";
2563 break;
2564 case DECL_OUT:
2565 attr = "INTENT (OUT)";
2566 break;
2567 case DECL_INOUT:
2568 attr = "INTENT (IN OUT)";
2569 break;
2570 case DECL_INTRINSIC:
2571 attr = "INTRINSIC";
2572 break;
2573 case DECL_OPTIONAL:
2574 attr = "OPTIONAL";
2575 break;
2576 case DECL_PARAMETER:
2577 attr = "PARAMETER";
2578 break;
2579 case DECL_POINTER:
2580 attr = "POINTER";
2581 break;
2582 case DECL_PROTECTED:
2583 attr = "PROTECTED";
2584 break;
2585 case DECL_PRIVATE:
2586 attr = "PRIVATE";
2587 break;
2588 case DECL_PUBLIC:
2589 attr = "PUBLIC";
2590 break;
2591 case DECL_SAVE:
2592 attr = "SAVE";
2593 break;
2594 case DECL_TARGET:
2595 attr = "TARGET";
2596 break;
2597 case DECL_IS_BIND_C:
2598 attr = "IS_BIND_C";
2599 break;
2600 case DECL_VALUE:
2601 attr = "VALUE";
2602 break;
2603 case DECL_VOLATILE:
2604 attr = "VOLATILE";
2605 break;
2606 default:
2607 attr = NULL; /* This shouldn't happen. */
2610 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2611 m = MATCH_ERROR;
2612 goto cleanup;
2615 /* Now that we've dealt with duplicate attributes, add the attributes
2616 to the current attribute. */
2617 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2619 if (seen[d] == 0)
2620 continue;
2622 if (gfc_current_state () == COMP_DERIVED
2623 && d != DECL_DIMENSION && d != DECL_POINTER
2624 && d != DECL_COLON && d != DECL_PRIVATE
2625 && d != DECL_PUBLIC && d != DECL_NONE)
2627 if (d == DECL_ALLOCATABLE)
2629 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
2630 "attribute at %C in a TYPE definition")
2631 == FAILURE)
2633 m = MATCH_ERROR;
2634 goto cleanup;
2637 else
2639 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2640 &seen_at[d]);
2641 m = MATCH_ERROR;
2642 goto cleanup;
2646 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2647 && gfc_current_state () != COMP_MODULE)
2649 if (d == DECL_PRIVATE)
2650 attr = "PRIVATE";
2651 else
2652 attr = "PUBLIC";
2653 if (gfc_current_state () == COMP_DERIVED
2654 && gfc_state_stack->previous
2655 && gfc_state_stack->previous->state == COMP_MODULE)
2657 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
2658 "at %L in a TYPE definition", attr,
2659 &seen_at[d])
2660 == FAILURE)
2662 m = MATCH_ERROR;
2663 goto cleanup;
2666 else
2668 gfc_error ("%s attribute at %L is not allowed outside of the "
2669 "specification part of a module", attr, &seen_at[d]);
2670 m = MATCH_ERROR;
2671 goto cleanup;
2675 switch (d)
2677 case DECL_ALLOCATABLE:
2678 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
2679 break;
2681 case DECL_DIMENSION:
2682 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
2683 break;
2685 case DECL_EXTERNAL:
2686 t = gfc_add_external (&current_attr, &seen_at[d]);
2687 break;
2689 case DECL_IN:
2690 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
2691 break;
2693 case DECL_OUT:
2694 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
2695 break;
2697 case DECL_INOUT:
2698 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
2699 break;
2701 case DECL_INTRINSIC:
2702 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
2703 break;
2705 case DECL_OPTIONAL:
2706 t = gfc_add_optional (&current_attr, &seen_at[d]);
2707 break;
2709 case DECL_PARAMETER:
2710 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
2711 break;
2713 case DECL_POINTER:
2714 t = gfc_add_pointer (&current_attr, &seen_at[d]);
2715 break;
2717 case DECL_PROTECTED:
2718 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
2720 gfc_error ("PROTECTED at %C only allowed in specification "
2721 "part of a module");
2722 t = FAILURE;
2723 break;
2726 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
2727 "attribute at %C")
2728 == FAILURE)
2729 t = FAILURE;
2730 else
2731 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
2732 break;
2734 case DECL_PRIVATE:
2735 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
2736 &seen_at[d]);
2737 break;
2739 case DECL_PUBLIC:
2740 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
2741 &seen_at[d]);
2742 break;
2744 case DECL_SAVE:
2745 t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
2746 break;
2748 case DECL_TARGET:
2749 t = gfc_add_target (&current_attr, &seen_at[d]);
2750 break;
2752 case DECL_IS_BIND_C:
2753 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
2754 break;
2756 case DECL_VALUE:
2757 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
2758 "at %C")
2759 == FAILURE)
2760 t = FAILURE;
2761 else
2762 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
2763 break;
2765 case DECL_VOLATILE:
2766 if (gfc_notify_std (GFC_STD_F2003,
2767 "Fortran 2003: VOLATILE attribute at %C")
2768 == FAILURE)
2769 t = FAILURE;
2770 else
2771 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
2772 break;
2774 default:
2775 gfc_internal_error ("match_attr_spec(): Bad attribute");
2778 if (t == FAILURE)
2780 m = MATCH_ERROR;
2781 goto cleanup;
2785 colon_seen = 1;
2786 return MATCH_YES;
2788 cleanup:
2789 gfc_current_locus = start;
2790 gfc_free_array_spec (current_as);
2791 current_as = NULL;
2792 return m;
2796 /* Set the binding label, dest_label, either with the binding label
2797 stored in the given gfc_typespec, ts, or if none was provided, it
2798 will be the symbol name in all lower case, as required by the draft
2799 (J3/04-007, section 15.4.1). If a binding label was given and
2800 there is more than one argument (num_idents), it is an error. */
2803 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
2805 if (curr_binding_label[0] != '\0')
2807 if (num_idents > 1 || num_idents_on_line > 1)
2809 gfc_error ("Multiple identifiers provided with "
2810 "single NAME= specifier at %C");
2811 return FAILURE;
2814 /* Binding label given; store in temp holder til have sym. */
2815 strncpy (dest_label, curr_binding_label,
2816 strlen (curr_binding_label) + 1);
2818 else
2820 /* No binding label given, and the NAME= specifier did not exist,
2821 which means there was no NAME="". */
2822 if (sym_name != NULL && has_name_equals == 0)
2823 strncpy (dest_label, sym_name, strlen (sym_name) + 1);
2826 return SUCCESS;
2830 /* Set the status of the given common block as being BIND(C) or not,
2831 depending on the given parameter, is_bind_c. */
2833 void
2834 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
2836 com_block->is_bind_c = is_bind_c;
2837 return;
2841 /* Verify that the given gfc_typespec is for a C interoperable type. */
2844 verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
2846 try t;
2848 /* Make sure the kind used is appropriate for the type.
2849 The f90_type is unknown if an integer constant was
2850 used (e.g., real(4), bind(c) :: myFloat). */
2851 if (ts->f90_type != BT_UNKNOWN)
2853 t = gfc_validate_c_kind (ts);
2854 if (t != SUCCESS)
2856 /* Print an error, but continue parsing line. */
2857 gfc_error_now ("C kind parameter is for type %s but "
2858 "symbol '%s' at %L is of type %s",
2859 gfc_basic_typename (ts->f90_type),
2860 name, where,
2861 gfc_basic_typename (ts->type));
2865 /* Make sure the kind is C interoperable. This does not care about the
2866 possible error above. */
2867 if (ts->type == BT_DERIVED && ts->derived != NULL)
2868 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
2869 else if (ts->is_c_interop != 1)
2870 return FAILURE;
2872 return SUCCESS;
2876 /* Verify that the variables of a given common block, which has been
2877 defined with the attribute specifier bind(c), to be of a C
2878 interoperable type. Errors will be reported here, if
2879 encountered. */
2882 verify_com_block_vars_c_interop (gfc_common_head *com_block)
2884 gfc_symbol *curr_sym = NULL;
2885 try retval = SUCCESS;
2887 curr_sym = com_block->head;
2889 /* Make sure we have at least one symbol. */
2890 if (curr_sym == NULL)
2891 return retval;
2893 /* Here we know we have a symbol, so we'll execute this loop
2894 at least once. */
2897 /* The second to last param, 1, says this is in a common block. */
2898 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
2899 curr_sym = curr_sym->common_next;
2900 } while (curr_sym != NULL);
2902 return retval;
2906 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
2907 an appropriate error message is reported. */
2910 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
2911 int is_in_common, gfc_common_head *com_block)
2913 try retval = SUCCESS;
2915 /* Here, we know we have the bind(c) attribute, so if we have
2916 enough type info, then verify that it's a C interop kind.
2917 The info could be in the symbol already, or possibly still in
2918 the given ts (current_ts), so look in both. */
2919 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
2921 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
2922 &(tmp_sym->declared_at)) != SUCCESS)
2924 /* See if we're dealing with a sym in a common block or not. */
2925 if (is_in_common == 1)
2927 gfc_warning ("Variable '%s' in common block '%s' at %L "
2928 "may not be a C interoperable "
2929 "kind though common block '%s' is BIND(C)",
2930 tmp_sym->name, com_block->name,
2931 &(tmp_sym->declared_at), com_block->name);
2933 else
2935 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
2936 gfc_error ("Type declaration '%s' at %L is not C "
2937 "interoperable but it is BIND(C)",
2938 tmp_sym->name, &(tmp_sym->declared_at));
2939 else
2940 gfc_warning ("Variable '%s' at %L "
2941 "may not be a C interoperable "
2942 "kind but it is bind(c)",
2943 tmp_sym->name, &(tmp_sym->declared_at));
2947 /* Variables declared w/in a common block can't be bind(c)
2948 since there's no way for C to see these variables, so there's
2949 semantically no reason for the attribute. */
2950 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
2952 gfc_error ("Variable '%s' in common block '%s' at "
2953 "%L cannot be declared with BIND(C) "
2954 "since it is not a global",
2955 tmp_sym->name, com_block->name,
2956 &(tmp_sym->declared_at));
2957 retval = FAILURE;
2960 /* Scalar variables that are bind(c) can not have the pointer
2961 or allocatable attributes. */
2962 if (tmp_sym->attr.is_bind_c == 1)
2964 if (tmp_sym->attr.pointer == 1)
2966 gfc_error ("Variable '%s' at %L cannot have both the "
2967 "POINTER and BIND(C) attributes",
2968 tmp_sym->name, &(tmp_sym->declared_at));
2969 retval = FAILURE;
2972 if (tmp_sym->attr.allocatable == 1)
2974 gfc_error ("Variable '%s' at %L cannot have both the "
2975 "ALLOCATABLE and BIND(C) attributes",
2976 tmp_sym->name, &(tmp_sym->declared_at));
2977 retval = FAILURE;
2980 /* If it is a BIND(C) function, make sure the return value is a
2981 scalar value. The previous tests in this function made sure
2982 the type is interoperable. */
2983 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
2984 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
2985 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
2987 /* BIND(C) functions can not return a character string. */
2988 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
2989 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
2990 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
2991 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
2992 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
2993 "be a character string", tmp_sym->name,
2994 &(tmp_sym->declared_at));
2998 /* See if the symbol has been marked as private. If it has, make sure
2999 there is no binding label and warn the user if there is one. */
3000 if (tmp_sym->attr.access == ACCESS_PRIVATE
3001 && tmp_sym->binding_label[0] != '\0')
3002 /* Use gfc_warning_now because we won't say that the symbol fails
3003 just because of this. */
3004 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3005 "given the binding label '%s'", tmp_sym->name,
3006 &(tmp_sym->declared_at), tmp_sym->binding_label);
3008 return retval;
3012 /* Set the appropriate fields for a symbol that's been declared as
3013 BIND(C) (the is_bind_c flag and the binding label), and verify that
3014 the type is C interoperable. Errors are reported by the functions
3015 used to set/test these fields. */
3018 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3020 try retval = SUCCESS;
3022 /* TODO: Do we need to make sure the vars aren't marked private? */
3024 /* Set the is_bind_c bit in symbol_attribute. */
3025 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3027 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3028 num_idents) != SUCCESS)
3029 return FAILURE;
3031 return retval;
3035 /* Set the fields marking the given common block as BIND(C), including
3036 a binding label, and report any errors encountered. */
3039 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3041 try retval = SUCCESS;
3043 /* destLabel, common name, typespec (which may have binding label). */
3044 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3045 != SUCCESS)
3046 return FAILURE;
3048 /* Set the given common block (com_block) to being bind(c) (1). */
3049 set_com_block_bind_c (com_block, 1);
3051 return retval;
3055 /* Retrieve the list of one or more identifiers that the given bind(c)
3056 attribute applies to. */
3059 get_bind_c_idents (void)
3061 char name[GFC_MAX_SYMBOL_LEN + 1];
3062 int num_idents = 0;
3063 gfc_symbol *tmp_sym = NULL;
3064 match found_id;
3065 gfc_common_head *com_block = NULL;
3067 if (gfc_match_name (name) == MATCH_YES)
3069 found_id = MATCH_YES;
3070 gfc_get_ha_symbol (name, &tmp_sym);
3072 else if (match_common_name (name) == MATCH_YES)
3074 found_id = MATCH_YES;
3075 com_block = gfc_get_common (name, 0);
3077 else
3079 gfc_error ("Need either entity or common block name for "
3080 "attribute specification statement at %C");
3081 return FAILURE;
3084 /* Save the current identifier and look for more. */
3087 /* Increment the number of identifiers found for this spec stmt. */
3088 num_idents++;
3090 /* Make sure we have a sym or com block, and verify that it can
3091 be bind(c). Set the appropriate field(s) and look for more
3092 identifiers. */
3093 if (tmp_sym != NULL || com_block != NULL)
3095 if (tmp_sym != NULL)
3097 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3098 != SUCCESS)
3099 return FAILURE;
3101 else
3103 if (set_verify_bind_c_com_block(com_block, num_idents)
3104 != SUCCESS)
3105 return FAILURE;
3108 /* Look to see if we have another identifier. */
3109 tmp_sym = NULL;
3110 if (gfc_match_eos () == MATCH_YES)
3111 found_id = MATCH_NO;
3112 else if (gfc_match_char (',') != MATCH_YES)
3113 found_id = MATCH_NO;
3114 else if (gfc_match_name (name) == MATCH_YES)
3116 found_id = MATCH_YES;
3117 gfc_get_ha_symbol (name, &tmp_sym);
3119 else if (match_common_name (name) == MATCH_YES)
3121 found_id = MATCH_YES;
3122 com_block = gfc_get_common (name, 0);
3124 else
3126 gfc_error ("Missing entity or common block name for "
3127 "attribute specification statement at %C");
3128 return FAILURE;
3131 else
3133 gfc_internal_error ("Missing symbol");
3135 } while (found_id == MATCH_YES);
3137 /* if we get here we were successful */
3138 return SUCCESS;
3142 /* Try and match a BIND(C) attribute specification statement. */
3144 match
3145 gfc_match_bind_c_stmt (void)
3147 match found_match = MATCH_NO;
3148 gfc_typespec *ts;
3150 ts = &current_ts;
3152 /* This may not be necessary. */
3153 gfc_clear_ts (ts);
3154 /* Clear the temporary binding label holder. */
3155 curr_binding_label[0] = '\0';
3157 /* Look for the bind(c). */
3158 found_match = gfc_match_bind_c (NULL);
3160 if (found_match == MATCH_YES)
3162 /* Look for the :: now, but it is not required. */
3163 gfc_match (" :: ");
3165 /* Get the identifier(s) that needs to be updated. This may need to
3166 change to hand the flag(s) for the attr specified so all identifiers
3167 found can have all appropriate parts updated (assuming that the same
3168 spec stmt can have multiple attrs, such as both bind(c) and
3169 allocatable...). */
3170 if (get_bind_c_idents () != SUCCESS)
3171 /* Error message should have printed already. */
3172 return MATCH_ERROR;
3175 return found_match;
3179 /* Match a data declaration statement. */
3181 match
3182 gfc_match_data_decl (void)
3184 gfc_symbol *sym;
3185 match m;
3186 int elem;
3188 num_idents_on_line = 0;
3190 m = match_type_spec (&current_ts, 0);
3191 if (m != MATCH_YES)
3192 return m;
3194 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3196 sym = gfc_use_derived (current_ts.derived);
3198 if (sym == NULL)
3200 m = MATCH_ERROR;
3201 goto cleanup;
3204 current_ts.derived = sym;
3207 m = match_attr_spec ();
3208 if (m == MATCH_ERROR)
3210 m = MATCH_NO;
3211 goto cleanup;
3214 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
3217 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3218 goto ok;
3220 gfc_find_symbol (current_ts.derived->name,
3221 current_ts.derived->ns->parent, 1, &sym);
3223 /* Any symbol that we find had better be a type definition
3224 which has its components defined. */
3225 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3226 && current_ts.derived->components != NULL)
3227 goto ok;
3229 /* Now we have an error, which we signal, and then fix up
3230 because the knock-on is plain and simple confusing. */
3231 gfc_error_now ("Derived type at %C has not been previously defined "
3232 "and so cannot appear in a derived type definition");
3233 current_attr.pointer = 1;
3234 goto ok;
3238 /* If we have an old-style character declaration, and no new-style
3239 attribute specifications, then there a comma is optional between
3240 the type specification and the variable list. */
3241 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3242 gfc_match_char (',');
3244 /* Give the types/attributes to symbols that follow. Give the element
3245 a number so that repeat character length expressions can be copied. */
3246 elem = 1;
3247 for (;;)
3249 num_idents_on_line++;
3250 m = variable_decl (elem++);
3251 if (m == MATCH_ERROR)
3252 goto cleanup;
3253 if (m == MATCH_NO)
3254 break;
3256 if (gfc_match_eos () == MATCH_YES)
3257 goto cleanup;
3258 if (gfc_match_char (',') != MATCH_YES)
3259 break;
3262 if (gfc_error_flag_test () == 0)
3263 gfc_error ("Syntax error in data declaration at %C");
3264 m = MATCH_ERROR;
3266 gfc_free_data_all (gfc_current_ns);
3268 cleanup:
3269 gfc_free_array_spec (current_as);
3270 current_as = NULL;
3271 return m;
3275 /* Match a prefix associated with a function or subroutine
3276 declaration. If the typespec pointer is nonnull, then a typespec
3277 can be matched. Note that if nothing matches, MATCH_YES is
3278 returned (the null string was matched). */
3280 static match
3281 match_prefix (gfc_typespec *ts)
3283 int seen_type;
3285 gfc_clear_attr (&current_attr);
3286 seen_type = 0;
3288 loop:
3289 if (!seen_type && ts != NULL
3290 && match_type_spec (ts, 0) == MATCH_YES
3291 && gfc_match_space () == MATCH_YES)
3294 seen_type = 1;
3295 goto loop;
3298 if (gfc_match ("elemental% ") == MATCH_YES)
3300 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
3301 return MATCH_ERROR;
3303 goto loop;
3306 if (gfc_match ("pure% ") == MATCH_YES)
3308 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
3309 return MATCH_ERROR;
3311 goto loop;
3314 if (gfc_match ("recursive% ") == MATCH_YES)
3316 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
3317 return MATCH_ERROR;
3319 goto loop;
3322 /* At this point, the next item is not a prefix. */
3323 return MATCH_YES;
3327 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
3329 static try
3330 copy_prefix (symbol_attribute *dest, locus *where)
3332 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3333 return FAILURE;
3335 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3336 return FAILURE;
3338 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3339 return FAILURE;
3341 return SUCCESS;
3345 /* Match a formal argument list. */
3347 match
3348 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3350 gfc_formal_arglist *head, *tail, *p, *q;
3351 char name[GFC_MAX_SYMBOL_LEN + 1];
3352 gfc_symbol *sym;
3353 match m;
3355 head = tail = NULL;
3357 if (gfc_match_char ('(') != MATCH_YES)
3359 if (null_flag)
3360 goto ok;
3361 return MATCH_NO;
3364 if (gfc_match_char (')') == MATCH_YES)
3365 goto ok;
3367 for (;;)
3369 if (gfc_match_char ('*') == MATCH_YES)
3370 sym = NULL;
3371 else
3373 m = gfc_match_name (name);
3374 if (m != MATCH_YES)
3375 goto cleanup;
3377 if (gfc_get_symbol (name, NULL, &sym))
3378 goto cleanup;
3381 p = gfc_get_formal_arglist ();
3383 if (head == NULL)
3384 head = tail = p;
3385 else
3387 tail->next = p;
3388 tail = p;
3391 tail->sym = sym;
3393 /* We don't add the VARIABLE flavor because the name could be a
3394 dummy procedure. We don't apply these attributes to formal
3395 arguments of statement functions. */
3396 if (sym != NULL && !st_flag
3397 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3398 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3400 m = MATCH_ERROR;
3401 goto cleanup;
3404 /* The name of a program unit can be in a different namespace,
3405 so check for it explicitly. After the statement is accepted,
3406 the name is checked for especially in gfc_get_symbol(). */
3407 if (gfc_new_block != NULL && sym != NULL
3408 && strcmp (sym->name, gfc_new_block->name) == 0)
3410 gfc_error ("Name '%s' at %C is the name of the procedure",
3411 sym->name);
3412 m = MATCH_ERROR;
3413 goto cleanup;
3416 if (gfc_match_char (')') == MATCH_YES)
3417 goto ok;
3419 m = gfc_match_char (',');
3420 if (m != MATCH_YES)
3422 gfc_error ("Unexpected junk in formal argument list at %C");
3423 goto cleanup;
3428 /* Check for duplicate symbols in the formal argument list. */
3429 if (head != NULL)
3431 for (p = head; p->next; p = p->next)
3433 if (p->sym == NULL)
3434 continue;
3436 for (q = p->next; q; q = q->next)
3437 if (p->sym == q->sym)
3439 gfc_error ("Duplicate symbol '%s' in formal argument list "
3440 "at %C", p->sym->name);
3442 m = MATCH_ERROR;
3443 goto cleanup;
3448 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3449 == FAILURE)
3451 m = MATCH_ERROR;
3452 goto cleanup;
3455 return MATCH_YES;
3457 cleanup:
3458 gfc_free_formal_arglist (head);
3459 return m;
3463 /* Match a RESULT specification following a function declaration or
3464 ENTRY statement. Also matches the end-of-statement. */
3466 static match
3467 match_result (gfc_symbol *function, gfc_symbol **result)
3469 char name[GFC_MAX_SYMBOL_LEN + 1];
3470 gfc_symbol *r;
3471 match m;
3473 if (gfc_match (" result (") != MATCH_YES)
3474 return MATCH_NO;
3476 m = gfc_match_name (name);
3477 if (m != MATCH_YES)
3478 return m;
3480 /* Get the right paren, and that's it because there could be the
3481 bind(c) attribute after the result clause. */
3482 if (gfc_match_char(')') != MATCH_YES)
3484 /* TODO: should report the missing right paren here. */
3485 return MATCH_ERROR;
3488 if (strcmp (function->name, name) == 0)
3490 gfc_error ("RESULT variable at %C must be different than function name");
3491 return MATCH_ERROR;
3494 if (gfc_get_symbol (name, NULL, &r))
3495 return MATCH_ERROR;
3497 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3498 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
3499 return MATCH_ERROR;
3501 *result = r;
3503 return MATCH_YES;
3507 /* Match a function suffix, which could be a combination of a result
3508 clause and BIND(C), either one, or neither. The draft does not
3509 require them to come in a specific order. */
3511 match
3512 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3514 match is_bind_c; /* Found bind(c). */
3515 match is_result; /* Found result clause. */
3516 match found_match; /* Status of whether we've found a good match. */
3517 int peek_char; /* Character we're going to peek at. */
3519 /* Initialize to having found nothing. */
3520 found_match = MATCH_NO;
3521 is_bind_c = MATCH_NO;
3522 is_result = MATCH_NO;
3524 /* Get the next char to narrow between result and bind(c). */
3525 gfc_gobble_whitespace ();
3526 peek_char = gfc_peek_char ();
3528 switch (peek_char)
3530 case 'r':
3531 /* Look for result clause. */
3532 is_result = match_result (sym, result);
3533 if (is_result == MATCH_YES)
3535 /* Now see if there is a bind(c) after it. */
3536 is_bind_c = gfc_match_bind_c (sym);
3537 /* We've found the result clause and possibly bind(c). */
3538 found_match = MATCH_YES;
3540 else
3541 /* This should only be MATCH_ERROR. */
3542 found_match = is_result;
3543 break;
3544 case 'b':
3545 /* Look for bind(c) first. */
3546 is_bind_c = gfc_match_bind_c (sym);
3547 if (is_bind_c == MATCH_YES)
3549 /* Now see if a result clause followed it. */
3550 is_result = match_result (sym, result);
3551 found_match = MATCH_YES;
3553 else
3555 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3556 found_match = MATCH_ERROR;
3558 break;
3559 default:
3560 gfc_error ("Unexpected junk after function declaration at %C");
3561 found_match = MATCH_ERROR;
3562 break;
3565 if (is_result == MATCH_ERROR || is_bind_c == MATCH_ERROR)
3567 gfc_error ("Error in function suffix at %C");
3568 return MATCH_ERROR;
3571 if (is_bind_c == MATCH_YES)
3572 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
3573 == FAILURE)
3574 return MATCH_ERROR;
3576 return found_match;
3580 /* Match a function declaration. */
3582 match
3583 gfc_match_function_decl (void)
3585 char name[GFC_MAX_SYMBOL_LEN + 1];
3586 gfc_symbol *sym, *result;
3587 locus old_loc;
3588 match m;
3589 match suffix_match;
3590 match found_match; /* Status returned by match func. */
3592 if (gfc_current_state () != COMP_NONE
3593 && gfc_current_state () != COMP_INTERFACE
3594 && gfc_current_state () != COMP_CONTAINS)
3595 return MATCH_NO;
3597 gfc_clear_ts (&current_ts);
3599 old_loc = gfc_current_locus;
3601 m = match_prefix (&current_ts);
3602 if (m != MATCH_YES)
3604 gfc_current_locus = old_loc;
3605 return m;
3608 if (gfc_match ("function% %n", name) != MATCH_YES)
3610 gfc_current_locus = old_loc;
3611 return MATCH_NO;
3613 if (get_proc_name (name, &sym, false))
3614 return MATCH_ERROR;
3615 gfc_new_block = sym;
3617 m = gfc_match_formal_arglist (sym, 0, 0);
3618 if (m == MATCH_NO)
3620 gfc_error ("Expected formal argument list in function "
3621 "definition at %C");
3622 m = MATCH_ERROR;
3623 goto cleanup;
3625 else if (m == MATCH_ERROR)
3626 goto cleanup;
3628 result = NULL;
3630 /* According to the draft, the bind(c) and result clause can
3631 come in either order after the formal_arg_list (i.e., either
3632 can be first, both can exist together or by themselves or neither
3633 one). Therefore, the match_result can't match the end of the
3634 string, and check for the bind(c) or result clause in either order. */
3635 found_match = gfc_match_eos ();
3637 /* Make sure that it isn't already declared as BIND(C). If it is, it
3638 must have been marked BIND(C) with a BIND(C) attribute and that is
3639 not allowed for procedures. */
3640 if (sym->attr.is_bind_c == 1)
3642 sym->attr.is_bind_c = 0;
3643 if (sym->old_symbol != NULL)
3644 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3645 "variables or common blocks",
3646 &(sym->old_symbol->declared_at));
3647 else
3648 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3649 "variables or common blocks", &gfc_current_locus);
3652 if (found_match != MATCH_YES)
3654 /* If we haven't found the end-of-statement, look for a suffix. */
3655 suffix_match = gfc_match_suffix (sym, &result);
3656 if (suffix_match == MATCH_YES)
3657 /* Need to get the eos now. */
3658 found_match = gfc_match_eos ();
3659 else
3660 found_match = suffix_match;
3663 if(found_match != MATCH_YES)
3664 m = MATCH_ERROR;
3665 else
3667 /* Make changes to the symbol. */
3668 m = MATCH_ERROR;
3670 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
3671 goto cleanup;
3673 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
3674 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
3675 goto cleanup;
3677 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
3678 && !sym->attr.implicit_type)
3680 gfc_error ("Function '%s' at %C already has a type of %s", name,
3681 gfc_basic_typename (sym->ts.type));
3682 goto cleanup;
3685 if (result == NULL)
3687 sym->ts = current_ts;
3688 sym->result = sym;
3690 else
3692 result->ts = current_ts;
3693 sym->result = result;
3696 return MATCH_YES;
3699 cleanup:
3700 gfc_current_locus = old_loc;
3701 return m;
3705 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
3706 pass the name of the entry, rather than the gfc_current_block name, and
3707 to return false upon finding an existing global entry. */
3709 static bool
3710 add_global_entry (const char *name, int sub)
3712 gfc_gsymbol *s;
3714 s = gfc_get_gsymbol(name);
3716 if (s->defined
3717 || (s->type != GSYM_UNKNOWN
3718 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3719 global_used(s, NULL);
3720 else
3722 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3723 s->where = gfc_current_locus;
3724 s->defined = 1;
3725 return true;
3727 return false;
3731 /* Match an ENTRY statement. */
3733 match
3734 gfc_match_entry (void)
3736 gfc_symbol *proc;
3737 gfc_symbol *result;
3738 gfc_symbol *entry;
3739 char name[GFC_MAX_SYMBOL_LEN + 1];
3740 gfc_compile_state state;
3741 match m;
3742 gfc_entry_list *el;
3743 locus old_loc;
3744 bool module_procedure;
3746 m = gfc_match_name (name);
3747 if (m != MATCH_YES)
3748 return m;
3750 state = gfc_current_state ();
3751 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
3753 switch (state)
3755 case COMP_PROGRAM:
3756 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
3757 break;
3758 case COMP_MODULE:
3759 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
3760 break;
3761 case COMP_BLOCK_DATA:
3762 gfc_error ("ENTRY statement at %C cannot appear within "
3763 "a BLOCK DATA");
3764 break;
3765 case COMP_INTERFACE:
3766 gfc_error ("ENTRY statement at %C cannot appear within "
3767 "an INTERFACE");
3768 break;
3769 case COMP_DERIVED:
3770 gfc_error ("ENTRY statement at %C cannot appear within "
3771 "a DERIVED TYPE block");
3772 break;
3773 case COMP_IF:
3774 gfc_error ("ENTRY statement at %C cannot appear within "
3775 "an IF-THEN block");
3776 break;
3777 case COMP_DO:
3778 gfc_error ("ENTRY statement at %C cannot appear within "
3779 "a DO block");
3780 break;
3781 case COMP_SELECT:
3782 gfc_error ("ENTRY statement at %C cannot appear within "
3783 "a SELECT block");
3784 break;
3785 case COMP_FORALL:
3786 gfc_error ("ENTRY statement at %C cannot appear within "
3787 "a FORALL block");
3788 break;
3789 case COMP_WHERE:
3790 gfc_error ("ENTRY statement at %C cannot appear within "
3791 "a WHERE block");
3792 break;
3793 case COMP_CONTAINS:
3794 gfc_error ("ENTRY statement at %C cannot appear within "
3795 "a contained subprogram");
3796 break;
3797 default:
3798 gfc_internal_error ("gfc_match_entry(): Bad state");
3800 return MATCH_ERROR;
3803 module_procedure = gfc_current_ns->parent != NULL
3804 && gfc_current_ns->parent->proc_name
3805 && gfc_current_ns->parent->proc_name->attr.flavor
3806 == FL_MODULE;
3808 if (gfc_current_ns->parent != NULL
3809 && gfc_current_ns->parent->proc_name
3810 && !module_procedure)
3812 gfc_error("ENTRY statement at %C cannot appear in a "
3813 "contained procedure");
3814 return MATCH_ERROR;
3817 /* Module function entries need special care in get_proc_name
3818 because previous references within the function will have
3819 created symbols attached to the current namespace. */
3820 if (get_proc_name (name, &entry,
3821 gfc_current_ns->parent != NULL
3822 && module_procedure
3823 && gfc_current_ns->proc_name->attr.function))
3824 return MATCH_ERROR;
3826 proc = gfc_current_block ();
3828 if (state == COMP_SUBROUTINE)
3830 /* An entry in a subroutine. */
3831 if (!add_global_entry (name, 1))
3832 return MATCH_ERROR;
3834 m = gfc_match_formal_arglist (entry, 0, 1);
3835 if (m != MATCH_YES)
3836 return MATCH_ERROR;
3838 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3839 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
3840 return MATCH_ERROR;
3842 else
3844 /* An entry in a function.
3845 We need to take special care because writing
3846 ENTRY f()
3848 ENTRY f
3849 is allowed, whereas
3850 ENTRY f() RESULT (r)
3851 can't be written as
3852 ENTRY f RESULT (r). */
3853 if (!add_global_entry (name, 0))
3854 return MATCH_ERROR;
3856 old_loc = gfc_current_locus;
3857 if (gfc_match_eos () == MATCH_YES)
3859 gfc_current_locus = old_loc;
3860 /* Match the empty argument list, and add the interface to
3861 the symbol. */
3862 m = gfc_match_formal_arglist (entry, 0, 1);
3864 else
3865 m = gfc_match_formal_arglist (entry, 0, 0);
3867 if (m != MATCH_YES)
3868 return MATCH_ERROR;
3870 result = NULL;
3872 if (gfc_match_eos () == MATCH_YES)
3874 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3875 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
3876 return MATCH_ERROR;
3878 entry->result = entry;
3880 else
3882 m = match_result (proc, &result);
3883 if (m == MATCH_NO)
3884 gfc_syntax_error (ST_ENTRY);
3885 if (m != MATCH_YES)
3886 return MATCH_ERROR;
3888 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
3889 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
3890 || gfc_add_function (&entry->attr, result->name, NULL)
3891 == FAILURE)
3892 return MATCH_ERROR;
3894 entry->result = result;
3898 if (gfc_match_eos () != MATCH_YES)
3900 gfc_syntax_error (ST_ENTRY);
3901 return MATCH_ERROR;
3904 entry->attr.recursive = proc->attr.recursive;
3905 entry->attr.elemental = proc->attr.elemental;
3906 entry->attr.pure = proc->attr.pure;
3908 el = gfc_get_entry_list ();
3909 el->sym = entry;
3910 el->next = gfc_current_ns->entries;
3911 gfc_current_ns->entries = el;
3912 if (el->next)
3913 el->id = el->next->id + 1;
3914 else
3915 el->id = 1;
3917 new_st.op = EXEC_ENTRY;
3918 new_st.ext.entry = el;
3920 return MATCH_YES;
3924 /* Match a subroutine statement, including optional prefixes. */
3926 match
3927 gfc_match_subroutine (void)
3929 char name[GFC_MAX_SYMBOL_LEN + 1];
3930 gfc_symbol *sym;
3931 match m;
3932 match is_bind_c;
3933 char peek_char;
3935 if (gfc_current_state () != COMP_NONE
3936 && gfc_current_state () != COMP_INTERFACE
3937 && gfc_current_state () != COMP_CONTAINS)
3938 return MATCH_NO;
3940 m = match_prefix (NULL);
3941 if (m != MATCH_YES)
3942 return m;
3944 m = gfc_match ("subroutine% %n", name);
3945 if (m != MATCH_YES)
3946 return m;
3948 if (get_proc_name (name, &sym, false))
3949 return MATCH_ERROR;
3950 gfc_new_block = sym;
3952 /* Check what next non-whitespace character is so we can tell if there
3953 where the required parens if we have a BIND(C). */
3954 gfc_gobble_whitespace ();
3955 peek_char = gfc_peek_char ();
3957 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
3958 return MATCH_ERROR;
3960 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
3961 return MATCH_ERROR;
3963 /* Make sure that it isn't already declared as BIND(C). If it is, it
3964 must have been marked BIND(C) with a BIND(C) attribute and that is
3965 not allowed for procedures. */
3966 if (sym->attr.is_bind_c == 1)
3968 sym->attr.is_bind_c = 0;
3969 if (sym->old_symbol != NULL)
3970 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3971 "variables or common blocks",
3972 &(sym->old_symbol->declared_at));
3973 else
3974 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3975 "variables or common blocks", &gfc_current_locus);
3978 /* Here, we are just checking if it has the bind(c) attribute, and if
3979 so, then we need to make sure it's all correct. If it doesn't,
3980 we still need to continue matching the rest of the subroutine line. */
3981 is_bind_c = gfc_match_bind_c (sym);
3982 if (is_bind_c == MATCH_ERROR)
3984 /* There was an attempt at the bind(c), but it was wrong. An
3985 error message should have been printed w/in the gfc_match_bind_c
3986 so here we'll just return the MATCH_ERROR. */
3987 return MATCH_ERROR;
3990 if (is_bind_c == MATCH_YES)
3992 if (peek_char != '(')
3994 gfc_error ("Missing required parentheses before BIND(C) at %C");
3995 return MATCH_ERROR;
3997 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
3998 == FAILURE)
3999 return MATCH_ERROR;
4002 if (gfc_match_eos () != MATCH_YES)
4004 gfc_syntax_error (ST_SUBROUTINE);
4005 return MATCH_ERROR;
4008 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4009 return MATCH_ERROR;
4011 return MATCH_YES;
4015 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4016 given, and set the binding label in either the given symbol (if not
4017 NULL), or in the current_ts. The symbol may be NULL becuase we may
4018 encounter the BIND(C) before the declaration itself. Return
4019 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4020 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4021 or MATCH_YES if the specifier was correct and the binding label and
4022 bind(c) fields were set correctly for the given symbol or the
4023 current_ts. */
4025 match
4026 gfc_match_bind_c (gfc_symbol *sym)
4028 /* binding label, if exists */
4029 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4030 match double_quote;
4031 match single_quote;
4032 int has_name_equals = 0;
4034 /* Initialize the flag that specifies whether we encountered a NAME=
4035 specifier or not. */
4036 has_name_equals = 0;
4038 /* Init the first char to nil so we can catch if we don't have
4039 the label (name attr) or the symbol name yet. */
4040 binding_label[0] = '\0';
4042 /* This much we have to be able to match, in this order, if
4043 there is a bind(c) label. */
4044 if (gfc_match (" bind ( c ") != MATCH_YES)
4045 return MATCH_NO;
4047 /* Now see if there is a binding label, or if we've reached the
4048 end of the bind(c) attribute without one. */
4049 if (gfc_match_char (',') == MATCH_YES)
4051 if (gfc_match (" name = ") != MATCH_YES)
4053 gfc_error ("Syntax error in NAME= specifier for binding label "
4054 "at %C");
4055 /* should give an error message here */
4056 return MATCH_ERROR;
4059 has_name_equals = 1;
4061 /* Get the opening quote. */
4062 double_quote = MATCH_YES;
4063 single_quote = MATCH_YES;
4064 double_quote = gfc_match_char ('"');
4065 if (double_quote != MATCH_YES)
4066 single_quote = gfc_match_char ('\'');
4067 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4069 gfc_error ("Syntax error in NAME= specifier for binding label "
4070 "at %C");
4071 return MATCH_ERROR;
4074 /* Grab the binding label, using functions that will not lower
4075 case the names automatically. */
4076 if (gfc_match_name_C (binding_label) != MATCH_YES)
4077 return MATCH_ERROR;
4079 /* Get the closing quotation. */
4080 if (double_quote == MATCH_YES)
4082 if (gfc_match_char ('"') != MATCH_YES)
4084 gfc_error ("Missing closing quote '\"' for binding label at %C");
4085 /* User started string with '"' so looked to match it. */
4086 return MATCH_ERROR;
4089 else
4091 if (gfc_match_char ('\'') != MATCH_YES)
4093 gfc_error ("Missing closing quote '\'' for binding label at %C");
4094 /* User started string with "'" char. */
4095 return MATCH_ERROR;
4100 /* Get the required right paren. */
4101 if (gfc_match_char (')') != MATCH_YES)
4103 gfc_error ("Missing closing paren for binding label at %C");
4104 return MATCH_ERROR;
4107 /* Save the binding label to the symbol. If sym is null, we're
4108 probably matching the typespec attributes of a declaration and
4109 haven't gotten the name yet, and therefore, no symbol yet. */
4110 if (binding_label[0] != '\0')
4112 if (sym != NULL)
4114 strncpy (sym->binding_label, binding_label,
4115 strlen (binding_label)+1);
4117 else
4118 strncpy (curr_binding_label, binding_label,
4119 strlen (binding_label) + 1);
4121 else
4123 /* No binding label, but if symbol isn't null, we
4124 can set the label for it here. */
4125 /* TODO: If the name= was given and no binding label (name=""), we simply
4126 will let fortran mangle the symbol name as it usually would.
4127 However, this could still let C call it if the user looked up the
4128 symbol in the object file. Should the name set during mangling in
4129 trans-decl.c be marked with characters that are invalid for C to
4130 prevent this? */
4131 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4132 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4135 return MATCH_YES;
4139 /* Return nonzero if we're currently compiling a contained procedure. */
4141 static int
4142 contained_procedure (void)
4144 gfc_state_data *s;
4146 for (s=gfc_state_stack; s; s=s->previous)
4147 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
4148 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
4149 return 1;
4151 return 0;
4154 /* Set the kind of each enumerator. The kind is selected such that it is
4155 interoperable with the corresponding C enumeration type, making
4156 sure that -fshort-enums is honored. */
4158 static void
4159 set_enum_kind(void)
4161 enumerator_history *current_history = NULL;
4162 int kind;
4163 int i;
4165 if (max_enum == NULL || enum_history == NULL)
4166 return;
4168 if (!gfc_option.fshort_enums)
4169 return;
4171 i = 0;
4174 kind = gfc_integer_kinds[i++].kind;
4176 while (kind < gfc_c_int_kind
4177 && gfc_check_integer_range (max_enum->initializer->value.integer,
4178 kind) != ARITH_OK);
4180 current_history = enum_history;
4181 while (current_history != NULL)
4183 current_history->sym->ts.kind = kind;
4184 current_history = current_history->next;
4189 /* Match any of the various end-block statements. Returns the type of
4190 END to the caller. The END INTERFACE, END IF, END DO and END
4191 SELECT statements cannot be replaced by a single END statement. */
4193 match
4194 gfc_match_end (gfc_statement *st)
4196 char name[GFC_MAX_SYMBOL_LEN + 1];
4197 gfc_compile_state state;
4198 locus old_loc;
4199 const char *block_name;
4200 const char *target;
4201 int eos_ok;
4202 match m;
4204 old_loc = gfc_current_locus;
4205 if (gfc_match ("end") != MATCH_YES)
4206 return MATCH_NO;
4208 state = gfc_current_state ();
4209 block_name = gfc_current_block () == NULL
4210 ? NULL : gfc_current_block ()->name;
4212 if (state == COMP_CONTAINS)
4214 state = gfc_state_stack->previous->state;
4215 block_name = gfc_state_stack->previous->sym == NULL
4216 ? NULL : gfc_state_stack->previous->sym->name;
4219 switch (state)
4221 case COMP_NONE:
4222 case COMP_PROGRAM:
4223 *st = ST_END_PROGRAM;
4224 target = " program";
4225 eos_ok = 1;
4226 break;
4228 case COMP_SUBROUTINE:
4229 *st = ST_END_SUBROUTINE;
4230 target = " subroutine";
4231 eos_ok = !contained_procedure ();
4232 break;
4234 case COMP_FUNCTION:
4235 *st = ST_END_FUNCTION;
4236 target = " function";
4237 eos_ok = !contained_procedure ();
4238 break;
4240 case COMP_BLOCK_DATA:
4241 *st = ST_END_BLOCK_DATA;
4242 target = " block data";
4243 eos_ok = 1;
4244 break;
4246 case COMP_MODULE:
4247 *st = ST_END_MODULE;
4248 target = " module";
4249 eos_ok = 1;
4250 break;
4252 case COMP_INTERFACE:
4253 *st = ST_END_INTERFACE;
4254 target = " interface";
4255 eos_ok = 0;
4256 break;
4258 case COMP_DERIVED:
4259 *st = ST_END_TYPE;
4260 target = " type";
4261 eos_ok = 0;
4262 break;
4264 case COMP_IF:
4265 *st = ST_ENDIF;
4266 target = " if";
4267 eos_ok = 0;
4268 break;
4270 case COMP_DO:
4271 *st = ST_ENDDO;
4272 target = " do";
4273 eos_ok = 0;
4274 break;
4276 case COMP_SELECT:
4277 *st = ST_END_SELECT;
4278 target = " select";
4279 eos_ok = 0;
4280 break;
4282 case COMP_FORALL:
4283 *st = ST_END_FORALL;
4284 target = " forall";
4285 eos_ok = 0;
4286 break;
4288 case COMP_WHERE:
4289 *st = ST_END_WHERE;
4290 target = " where";
4291 eos_ok = 0;
4292 break;
4294 case COMP_ENUM:
4295 *st = ST_END_ENUM;
4296 target = " enum";
4297 eos_ok = 0;
4298 last_initializer = NULL;
4299 set_enum_kind ();
4300 gfc_free_enum_history ();
4301 break;
4303 default:
4304 gfc_error ("Unexpected END statement at %C");
4305 goto cleanup;
4308 if (gfc_match_eos () == MATCH_YES)
4310 if (!eos_ok)
4312 /* We would have required END [something]. */
4313 gfc_error ("%s statement expected at %L",
4314 gfc_ascii_statement (*st), &old_loc);
4315 goto cleanup;
4318 return MATCH_YES;
4321 /* Verify that we've got the sort of end-block that we're expecting. */
4322 if (gfc_match (target) != MATCH_YES)
4324 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
4325 goto cleanup;
4328 /* If we're at the end, make sure a block name wasn't required. */
4329 if (gfc_match_eos () == MATCH_YES)
4332 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
4333 && *st != ST_END_FORALL && *st != ST_END_WHERE)
4334 return MATCH_YES;
4336 if (gfc_current_block () == NULL)
4337 return MATCH_YES;
4339 gfc_error ("Expected block name of '%s' in %s statement at %C",
4340 block_name, gfc_ascii_statement (*st));
4342 return MATCH_ERROR;
4345 /* END INTERFACE has a special handler for its several possible endings. */
4346 if (*st == ST_END_INTERFACE)
4347 return gfc_match_end_interface ();
4349 /* We haven't hit the end of statement, so what is left must be an
4350 end-name. */
4351 m = gfc_match_space ();
4352 if (m == MATCH_YES)
4353 m = gfc_match_name (name);
4355 if (m == MATCH_NO)
4356 gfc_error ("Expected terminating name at %C");
4357 if (m != MATCH_YES)
4358 goto cleanup;
4360 if (block_name == NULL)
4361 goto syntax;
4363 if (strcmp (name, block_name) != 0)
4365 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
4366 gfc_ascii_statement (*st));
4367 goto cleanup;
4370 if (gfc_match_eos () == MATCH_YES)
4371 return MATCH_YES;
4373 syntax:
4374 gfc_syntax_error (*st);
4376 cleanup:
4377 gfc_current_locus = old_loc;
4378 return MATCH_ERROR;
4383 /***************** Attribute declaration statements ****************/
4385 /* Set the attribute of a single variable. */
4387 static match
4388 attr_decl1 (void)
4390 char name[GFC_MAX_SYMBOL_LEN + 1];
4391 gfc_array_spec *as;
4392 gfc_symbol *sym;
4393 locus var_locus;
4394 match m;
4396 as = NULL;
4398 m = gfc_match_name (name);
4399 if (m != MATCH_YES)
4400 goto cleanup;
4402 if (find_special (name, &sym))
4403 return MATCH_ERROR;
4405 var_locus = gfc_current_locus;
4407 /* Deal with possible array specification for certain attributes. */
4408 if (current_attr.dimension
4409 || current_attr.allocatable
4410 || current_attr.pointer
4411 || current_attr.target)
4413 m = gfc_match_array_spec (&as);
4414 if (m == MATCH_ERROR)
4415 goto cleanup;
4417 if (current_attr.dimension && m == MATCH_NO)
4419 gfc_error ("Missing array specification at %L in DIMENSION "
4420 "statement", &var_locus);
4421 m = MATCH_ERROR;
4422 goto cleanup;
4425 if ((current_attr.allocatable || current_attr.pointer)
4426 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
4428 gfc_error ("Array specification must be deferred at %L", &var_locus);
4429 m = MATCH_ERROR;
4430 goto cleanup;
4434 /* Update symbol table. DIMENSION attribute is set
4435 in gfc_set_array_spec(). */
4436 if (current_attr.dimension == 0
4437 && gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4439 m = MATCH_ERROR;
4440 goto cleanup;
4443 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
4445 m = MATCH_ERROR;
4446 goto cleanup;
4449 if (sym->attr.cray_pointee && sym->as != NULL)
4451 /* Fix the array spec. */
4452 m = gfc_mod_pointee_as (sym->as);
4453 if (m == MATCH_ERROR)
4454 goto cleanup;
4457 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
4459 m = MATCH_ERROR;
4460 goto cleanup;
4463 if ((current_attr.external || current_attr.intrinsic)
4464 && sym->attr.flavor != FL_PROCEDURE
4465 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
4467 m = MATCH_ERROR;
4468 goto cleanup;
4471 return MATCH_YES;
4473 cleanup:
4474 gfc_free_array_spec (as);
4475 return m;
4479 /* Generic attribute declaration subroutine. Used for attributes that
4480 just have a list of names. */
4482 static match
4483 attr_decl (void)
4485 match m;
4487 /* Gobble the optional double colon, by simply ignoring the result
4488 of gfc_match(). */
4489 gfc_match (" ::");
4491 for (;;)
4493 m = attr_decl1 ();
4494 if (m != MATCH_YES)
4495 break;
4497 if (gfc_match_eos () == MATCH_YES)
4499 m = MATCH_YES;
4500 break;
4503 if (gfc_match_char (',') != MATCH_YES)
4505 gfc_error ("Unexpected character in variable list at %C");
4506 m = MATCH_ERROR;
4507 break;
4511 return m;
4515 /* This routine matches Cray Pointer declarations of the form:
4516 pointer ( <pointer>, <pointee> )
4518 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
4519 The pointer, if already declared, should be an integer. Otherwise, we
4520 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
4521 be either a scalar, or an array declaration. No space is allocated for
4522 the pointee. For the statement
4523 pointer (ipt, ar(10))
4524 any subsequent uses of ar will be translated (in C-notation) as
4525 ar(i) => ((<type> *) ipt)(i)
4526 After gimplification, pointee variable will disappear in the code. */
4528 static match
4529 cray_pointer_decl (void)
4531 match m;
4532 gfc_array_spec *as;
4533 gfc_symbol *cptr; /* Pointer symbol. */
4534 gfc_symbol *cpte; /* Pointee symbol. */
4535 locus var_locus;
4536 bool done = false;
4538 while (!done)
4540 if (gfc_match_char ('(') != MATCH_YES)
4542 gfc_error ("Expected '(' at %C");
4543 return MATCH_ERROR;
4546 /* Match pointer. */
4547 var_locus = gfc_current_locus;
4548 gfc_clear_attr (&current_attr);
4549 gfc_add_cray_pointer (&current_attr, &var_locus);
4550 current_ts.type = BT_INTEGER;
4551 current_ts.kind = gfc_index_integer_kind;
4553 m = gfc_match_symbol (&cptr, 0);
4554 if (m != MATCH_YES)
4556 gfc_error ("Expected variable name at %C");
4557 return m;
4560 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
4561 return MATCH_ERROR;
4563 gfc_set_sym_referenced (cptr);
4565 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
4567 cptr->ts.type = BT_INTEGER;
4568 cptr->ts.kind = gfc_index_integer_kind;
4570 else if (cptr->ts.type != BT_INTEGER)
4572 gfc_error ("Cray pointer at %C must be an integer");
4573 return MATCH_ERROR;
4575 else if (cptr->ts.kind < gfc_index_integer_kind)
4576 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
4577 " memory addresses require %d bytes",
4578 cptr->ts.kind, gfc_index_integer_kind);
4580 if (gfc_match_char (',') != MATCH_YES)
4582 gfc_error ("Expected \",\" at %C");
4583 return MATCH_ERROR;
4586 /* Match Pointee. */
4587 var_locus = gfc_current_locus;
4588 gfc_clear_attr (&current_attr);
4589 gfc_add_cray_pointee (&current_attr, &var_locus);
4590 current_ts.type = BT_UNKNOWN;
4591 current_ts.kind = 0;
4593 m = gfc_match_symbol (&cpte, 0);
4594 if (m != MATCH_YES)
4596 gfc_error ("Expected variable name at %C");
4597 return m;
4600 /* Check for an optional array spec. */
4601 m = gfc_match_array_spec (&as);
4602 if (m == MATCH_ERROR)
4604 gfc_free_array_spec (as);
4605 return m;
4607 else if (m == MATCH_NO)
4609 gfc_free_array_spec (as);
4610 as = NULL;
4613 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
4614 return MATCH_ERROR;
4616 gfc_set_sym_referenced (cpte);
4618 if (cpte->as == NULL)
4620 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
4621 gfc_internal_error ("Couldn't set Cray pointee array spec.");
4623 else if (as != NULL)
4625 gfc_error ("Duplicate array spec for Cray pointee at %C");
4626 gfc_free_array_spec (as);
4627 return MATCH_ERROR;
4630 as = NULL;
4632 if (cpte->as != NULL)
4634 /* Fix array spec. */
4635 m = gfc_mod_pointee_as (cpte->as);
4636 if (m == MATCH_ERROR)
4637 return m;
4640 /* Point the Pointee at the Pointer. */
4641 cpte->cp_pointer = cptr;
4643 if (gfc_match_char (')') != MATCH_YES)
4645 gfc_error ("Expected \")\" at %C");
4646 return MATCH_ERROR;
4648 m = gfc_match_char (',');
4649 if (m != MATCH_YES)
4650 done = true; /* Stop searching for more declarations. */
4654 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
4655 || gfc_match_eos () != MATCH_YES)
4657 gfc_error ("Expected \",\" or end of statement at %C");
4658 return MATCH_ERROR;
4660 return MATCH_YES;
4664 match
4665 gfc_match_external (void)
4668 gfc_clear_attr (&current_attr);
4669 current_attr.external = 1;
4671 return attr_decl ();
4675 match
4676 gfc_match_intent (void)
4678 sym_intent intent;
4680 intent = match_intent_spec ();
4681 if (intent == INTENT_UNKNOWN)
4682 return MATCH_ERROR;
4684 gfc_clear_attr (&current_attr);
4685 current_attr.intent = intent;
4687 return attr_decl ();
4691 match
4692 gfc_match_intrinsic (void)
4695 gfc_clear_attr (&current_attr);
4696 current_attr.intrinsic = 1;
4698 return attr_decl ();
4702 match
4703 gfc_match_optional (void)
4706 gfc_clear_attr (&current_attr);
4707 current_attr.optional = 1;
4709 return attr_decl ();
4713 match
4714 gfc_match_pointer (void)
4716 gfc_gobble_whitespace ();
4717 if (gfc_peek_char () == '(')
4719 if (!gfc_option.flag_cray_pointer)
4721 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
4722 "flag");
4723 return MATCH_ERROR;
4725 return cray_pointer_decl ();
4727 else
4729 gfc_clear_attr (&current_attr);
4730 current_attr.pointer = 1;
4732 return attr_decl ();
4737 match
4738 gfc_match_allocatable (void)
4740 gfc_clear_attr (&current_attr);
4741 current_attr.allocatable = 1;
4743 return attr_decl ();
4747 match
4748 gfc_match_dimension (void)
4750 gfc_clear_attr (&current_attr);
4751 current_attr.dimension = 1;
4753 return attr_decl ();
4757 match
4758 gfc_match_target (void)
4760 gfc_clear_attr (&current_attr);
4761 current_attr.target = 1;
4763 return attr_decl ();
4767 /* Match the list of entities being specified in a PUBLIC or PRIVATE
4768 statement. */
4770 static match
4771 access_attr_decl (gfc_statement st)
4773 char name[GFC_MAX_SYMBOL_LEN + 1];
4774 interface_type type;
4775 gfc_user_op *uop;
4776 gfc_symbol *sym;
4777 gfc_intrinsic_op operator;
4778 match m;
4780 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4781 goto done;
4783 for (;;)
4785 m = gfc_match_generic_spec (&type, name, &operator);
4786 if (m == MATCH_NO)
4787 goto syntax;
4788 if (m == MATCH_ERROR)
4789 return MATCH_ERROR;
4791 switch (type)
4793 case INTERFACE_NAMELESS:
4794 goto syntax;
4796 case INTERFACE_GENERIC:
4797 if (gfc_get_symbol (name, NULL, &sym))
4798 goto done;
4800 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
4801 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
4802 sym->name, NULL) == FAILURE)
4803 return MATCH_ERROR;
4805 break;
4807 case INTERFACE_INTRINSIC_OP:
4808 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
4810 gfc_current_ns->operator_access[operator] =
4811 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
4813 else
4815 gfc_error ("Access specification of the %s operator at %C has "
4816 "already been specified", gfc_op2string (operator));
4817 goto done;
4820 break;
4822 case INTERFACE_USER_OP:
4823 uop = gfc_get_uop (name);
4825 if (uop->access == ACCESS_UNKNOWN)
4827 uop->access = (st == ST_PUBLIC)
4828 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
4830 else
4832 gfc_error ("Access specification of the .%s. operator at %C "
4833 "has already been specified", sym->name);
4834 goto done;
4837 break;
4840 if (gfc_match_char (',') == MATCH_NO)
4841 break;
4844 if (gfc_match_eos () != MATCH_YES)
4845 goto syntax;
4846 return MATCH_YES;
4848 syntax:
4849 gfc_syntax_error (st);
4851 done:
4852 return MATCH_ERROR;
4856 match
4857 gfc_match_protected (void)
4859 gfc_symbol *sym;
4860 match m;
4862 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
4864 gfc_error ("PROTECTED at %C only allowed in specification "
4865 "part of a module");
4866 return MATCH_ERROR;
4870 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
4871 == FAILURE)
4872 return MATCH_ERROR;
4874 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4876 return MATCH_ERROR;
4879 if (gfc_match_eos () == MATCH_YES)
4880 goto syntax;
4882 for(;;)
4884 m = gfc_match_symbol (&sym, 0);
4885 switch (m)
4887 case MATCH_YES:
4888 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
4889 == FAILURE)
4890 return MATCH_ERROR;
4891 goto next_item;
4893 case MATCH_NO:
4894 break;
4896 case MATCH_ERROR:
4897 return MATCH_ERROR;
4900 next_item:
4901 if (gfc_match_eos () == MATCH_YES)
4902 break;
4903 if (gfc_match_char (',') != MATCH_YES)
4904 goto syntax;
4907 return MATCH_YES;
4909 syntax:
4910 gfc_error ("Syntax error in PROTECTED statement at %C");
4911 return MATCH_ERROR;
4915 /* The PRIVATE statement is a bit weird in that it can be a attribute
4916 declaration, but also works as a standlone statement inside of a
4917 type declaration or a module. */
4919 match
4920 gfc_match_private (gfc_statement *st)
4923 if (gfc_match ("private") != MATCH_YES)
4924 return MATCH_NO;
4926 if (gfc_current_state () != COMP_MODULE
4927 && (gfc_current_state () != COMP_DERIVED
4928 || !gfc_state_stack->previous
4929 || gfc_state_stack->previous->state != COMP_MODULE))
4931 gfc_error ("PRIVATE statement at %C is only allowed in the "
4932 "specification part of a module");
4933 return MATCH_ERROR;
4936 if (gfc_current_state () == COMP_DERIVED)
4938 if (gfc_match_eos () == MATCH_YES)
4940 *st = ST_PRIVATE;
4941 return MATCH_YES;
4944 gfc_syntax_error (ST_PRIVATE);
4945 return MATCH_ERROR;
4948 if (gfc_match_eos () == MATCH_YES)
4950 *st = ST_PRIVATE;
4951 return MATCH_YES;
4954 *st = ST_ATTR_DECL;
4955 return access_attr_decl (ST_PRIVATE);
4959 match
4960 gfc_match_public (gfc_statement *st)
4963 if (gfc_match ("public") != MATCH_YES)
4964 return MATCH_NO;
4966 if (gfc_current_state () != COMP_MODULE)
4968 gfc_error ("PUBLIC statement at %C is only allowed in the "
4969 "specification part of a module");
4970 return MATCH_ERROR;
4973 if (gfc_match_eos () == MATCH_YES)
4975 *st = ST_PUBLIC;
4976 return MATCH_YES;
4979 *st = ST_ATTR_DECL;
4980 return access_attr_decl (ST_PUBLIC);
4984 /* Workhorse for gfc_match_parameter. */
4986 static match
4987 do_parm (void)
4989 gfc_symbol *sym;
4990 gfc_expr *init;
4991 match m;
4993 m = gfc_match_symbol (&sym, 0);
4994 if (m == MATCH_NO)
4995 gfc_error ("Expected variable name at %C in PARAMETER statement");
4997 if (m != MATCH_YES)
4998 return m;
5000 if (gfc_match_char ('=') == MATCH_NO)
5002 gfc_error ("Expected = sign in PARAMETER statement at %C");
5003 return MATCH_ERROR;
5006 m = gfc_match_init_expr (&init);
5007 if (m == MATCH_NO)
5008 gfc_error ("Expected expression at %C in PARAMETER statement");
5009 if (m != MATCH_YES)
5010 return m;
5012 if (sym->ts.type == BT_UNKNOWN
5013 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5015 m = MATCH_ERROR;
5016 goto cleanup;
5019 if (gfc_check_assign_symbol (sym, init) == FAILURE
5020 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
5022 m = MATCH_ERROR;
5023 goto cleanup;
5026 if (sym->ts.type == BT_CHARACTER
5027 && sym->ts.cl != NULL
5028 && sym->ts.cl->length != NULL
5029 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5030 && init->expr_type == EXPR_CONSTANT
5031 && init->ts.type == BT_CHARACTER
5032 && init->ts.kind == 1)
5033 gfc_set_constant_character_len (
5034 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
5036 sym->value = init;
5037 return MATCH_YES;
5039 cleanup:
5040 gfc_free_expr (init);
5041 return m;
5045 /* Match a parameter statement, with the weird syntax that these have. */
5047 match
5048 gfc_match_parameter (void)
5050 match m;
5052 if (gfc_match_char ('(') == MATCH_NO)
5053 return MATCH_NO;
5055 for (;;)
5057 m = do_parm ();
5058 if (m != MATCH_YES)
5059 break;
5061 if (gfc_match (" )%t") == MATCH_YES)
5062 break;
5064 if (gfc_match_char (',') != MATCH_YES)
5066 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5067 m = MATCH_ERROR;
5068 break;
5072 return m;
5076 /* Save statements have a special syntax. */
5078 match
5079 gfc_match_save (void)
5081 char n[GFC_MAX_SYMBOL_LEN+1];
5082 gfc_common_head *c;
5083 gfc_symbol *sym;
5084 match m;
5086 if (gfc_match_eos () == MATCH_YES)
5088 if (gfc_current_ns->seen_save)
5090 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5091 "follows previous SAVE statement")
5092 == FAILURE)
5093 return MATCH_ERROR;
5096 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5097 return MATCH_YES;
5100 if (gfc_current_ns->save_all)
5102 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5103 "blanket SAVE statement")
5104 == FAILURE)
5105 return MATCH_ERROR;
5108 gfc_match (" ::");
5110 for (;;)
5112 m = gfc_match_symbol (&sym, 0);
5113 switch (m)
5115 case MATCH_YES:
5116 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5117 == FAILURE)
5118 return MATCH_ERROR;
5119 goto next_item;
5121 case MATCH_NO:
5122 break;
5124 case MATCH_ERROR:
5125 return MATCH_ERROR;
5128 m = gfc_match (" / %n /", &n);
5129 if (m == MATCH_ERROR)
5130 return MATCH_ERROR;
5131 if (m == MATCH_NO)
5132 goto syntax;
5134 c = gfc_get_common (n, 0);
5135 c->saved = 1;
5137 gfc_current_ns->seen_save = 1;
5139 next_item:
5140 if (gfc_match_eos () == MATCH_YES)
5141 break;
5142 if (gfc_match_char (',') != MATCH_YES)
5143 goto syntax;
5146 return MATCH_YES;
5148 syntax:
5149 gfc_error ("Syntax error in SAVE statement at %C");
5150 return MATCH_ERROR;
5154 match
5155 gfc_match_value (void)
5157 gfc_symbol *sym;
5158 match m;
5160 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
5161 == FAILURE)
5162 return MATCH_ERROR;
5164 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5166 return MATCH_ERROR;
5169 if (gfc_match_eos () == MATCH_YES)
5170 goto syntax;
5172 for(;;)
5174 m = gfc_match_symbol (&sym, 0);
5175 switch (m)
5177 case MATCH_YES:
5178 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5179 == FAILURE)
5180 return MATCH_ERROR;
5181 goto next_item;
5183 case MATCH_NO:
5184 break;
5186 case MATCH_ERROR:
5187 return MATCH_ERROR;
5190 next_item:
5191 if (gfc_match_eos () == MATCH_YES)
5192 break;
5193 if (gfc_match_char (',') != MATCH_YES)
5194 goto syntax;
5197 return MATCH_YES;
5199 syntax:
5200 gfc_error ("Syntax error in VALUE statement at %C");
5201 return MATCH_ERROR;
5205 match
5206 gfc_match_volatile (void)
5208 gfc_symbol *sym;
5209 match m;
5211 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
5212 == FAILURE)
5213 return MATCH_ERROR;
5215 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5217 return MATCH_ERROR;
5220 if (gfc_match_eos () == MATCH_YES)
5221 goto syntax;
5223 for(;;)
5225 /* VOLATILE is special because it can be added to host-associated
5226 symbols locally. */
5227 m = gfc_match_symbol (&sym, 1);
5228 switch (m)
5230 case MATCH_YES:
5231 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
5232 == FAILURE)
5233 return MATCH_ERROR;
5234 goto next_item;
5236 case MATCH_NO:
5237 break;
5239 case MATCH_ERROR:
5240 return MATCH_ERROR;
5243 next_item:
5244 if (gfc_match_eos () == MATCH_YES)
5245 break;
5246 if (gfc_match_char (',') != MATCH_YES)
5247 goto syntax;
5250 return MATCH_YES;
5252 syntax:
5253 gfc_error ("Syntax error in VOLATILE statement at %C");
5254 return MATCH_ERROR;
5258 /* Match a module procedure statement. Note that we have to modify
5259 symbols in the parent's namespace because the current one was there
5260 to receive symbols that are in an interface's formal argument list. */
5262 match
5263 gfc_match_modproc (void)
5265 char name[GFC_MAX_SYMBOL_LEN + 1];
5266 gfc_symbol *sym;
5267 match m;
5268 gfc_namespace *module_ns;
5270 if (gfc_state_stack->state != COMP_INTERFACE
5271 || gfc_state_stack->previous == NULL
5272 || current_interface.type == INTERFACE_NAMELESS)
5274 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5275 "interface");
5276 return MATCH_ERROR;
5279 module_ns = gfc_current_ns->parent;
5280 for (; module_ns; module_ns = module_ns->parent)
5281 if (module_ns->proc_name->attr.flavor == FL_MODULE)
5282 break;
5284 if (module_ns == NULL)
5285 return MATCH_ERROR;
5287 for (;;)
5289 m = gfc_match_name (name);
5290 if (m == MATCH_NO)
5291 goto syntax;
5292 if (m != MATCH_YES)
5293 return MATCH_ERROR;
5295 if (gfc_get_symbol (name, module_ns, &sym))
5296 return MATCH_ERROR;
5298 if (sym->attr.proc != PROC_MODULE
5299 && gfc_add_procedure (&sym->attr, PROC_MODULE,
5300 sym->name, NULL) == FAILURE)
5301 return MATCH_ERROR;
5303 if (gfc_add_interface (sym) == FAILURE)
5304 return MATCH_ERROR;
5306 sym->attr.mod_proc = 1;
5308 if (gfc_match_eos () == MATCH_YES)
5309 break;
5310 if (gfc_match_char (',') != MATCH_YES)
5311 goto syntax;
5314 return MATCH_YES;
5316 syntax:
5317 gfc_syntax_error (ST_MODULE_PROC);
5318 return MATCH_ERROR;
5322 /* Match the optional attribute specifiers for a type declaration.
5323 Return MATCH_ERROR if an error is encountered in one of the handled
5324 attributes (public, private, bind(c)), MATCH_NO if what's found is
5325 not a handled attribute, and MATCH_YES otherwise. TODO: More error
5326 checking on attribute conflicts needs to be done. */
5328 match
5329 gfc_get_type_attr_spec (symbol_attribute *attr)
5331 /* See if the derived type is marked as private. */
5332 if (gfc_match (" , private") == MATCH_YES)
5334 if (gfc_current_state () != COMP_MODULE)
5336 gfc_error ("Derived type at %C can only be PRIVATE in the "
5337 "specification part of a module");
5338 return MATCH_ERROR;
5341 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
5342 return MATCH_ERROR;
5344 else if (gfc_match (" , public") == MATCH_YES)
5346 if (gfc_current_state () != COMP_MODULE)
5348 gfc_error ("Derived type at %C can only be PUBLIC in the "
5349 "specification part of a module");
5350 return MATCH_ERROR;
5353 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
5354 return MATCH_ERROR;
5356 else if(gfc_match(" , bind ( c )") == MATCH_YES)
5358 /* If the type is defined to be bind(c) it then needs to make
5359 sure that all fields are interoperable. This will
5360 need to be a semantic check on the finished derived type.
5361 See 15.2.3 (lines 9-12) of F2003 draft. */
5362 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
5363 return MATCH_ERROR;
5365 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
5367 else
5368 return MATCH_NO;
5370 /* If we get here, something matched. */
5371 return MATCH_YES;
5375 /* Match the beginning of a derived type declaration. If a type name
5376 was the result of a function, then it is possible to have a symbol
5377 already to be known as a derived type yet have no components. */
5379 match
5380 gfc_match_derived_decl (void)
5382 char name[GFC_MAX_SYMBOL_LEN + 1];
5383 symbol_attribute attr;
5384 gfc_symbol *sym;
5385 match m;
5386 match is_type_attr_spec = MATCH_NO;
5388 if (gfc_current_state () == COMP_DERIVED)
5389 return MATCH_NO;
5391 gfc_clear_attr (&attr);
5395 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
5396 if (is_type_attr_spec == MATCH_ERROR)
5397 return MATCH_ERROR;
5398 } while (is_type_attr_spec == MATCH_YES);
5400 if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
5402 gfc_error ("Expected :: in TYPE definition at %C");
5403 return MATCH_ERROR;
5406 m = gfc_match (" %n%t", name);
5407 if (m != MATCH_YES)
5408 return m;
5410 /* Make sure the name isn't the name of an intrinsic type. The
5411 'double {precision,complex}' types don't get past the name
5412 matcher, unless they're written as a single word or in fixed
5413 form. */
5414 if (strcmp (name, "integer") == 0
5415 || strcmp (name, "real") == 0
5416 || strcmp (name, "character") == 0
5417 || strcmp (name, "logical") == 0
5418 || strcmp (name, "complex") == 0
5419 || strcmp (name, "doubleprecision") == 0
5420 || strcmp (name, "doublecomplex") == 0)
5422 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
5423 "type", name);
5424 return MATCH_ERROR;
5427 if (gfc_get_symbol (name, NULL, &sym))
5428 return MATCH_ERROR;
5430 if (sym->ts.type != BT_UNKNOWN)
5432 gfc_error ("Derived type name '%s' at %C already has a basic type "
5433 "of %s", sym->name, gfc_typename (&sym->ts));
5434 return MATCH_ERROR;
5437 /* The symbol may already have the derived attribute without the
5438 components. The ways this can happen is via a function
5439 definition, an INTRINSIC statement or a subtype in another
5440 derived type that is a pointer. The first part of the AND clause
5441 is true if a the symbol is not the return value of a function. */
5442 if (sym->attr.flavor != FL_DERIVED
5443 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
5444 return MATCH_ERROR;
5446 if (sym->components != NULL)
5448 gfc_error ("Derived type definition of '%s' at %C has already been "
5449 "defined", sym->name);
5450 return MATCH_ERROR;
5453 if (attr.access != ACCESS_UNKNOWN
5454 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
5455 return MATCH_ERROR;
5457 /* See if the derived type was labeled as bind(c). */
5458 if (attr.is_bind_c != 0)
5459 sym->attr.is_bind_c = attr.is_bind_c;
5461 gfc_new_block = sym;
5463 return MATCH_YES;
5467 /* Cray Pointees can be declared as:
5468 pointer (ipt, a (n,m,...,*))
5469 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
5470 cheat and set a constant bound of 1 for the last dimension, if this
5471 is the case. Since there is no bounds-checking for Cray Pointees,
5472 this will be okay. */
5475 gfc_mod_pointee_as (gfc_array_spec *as)
5477 as->cray_pointee = true; /* This will be useful to know later. */
5478 if (as->type == AS_ASSUMED_SIZE)
5480 as->type = AS_EXPLICIT;
5481 as->upper[as->rank - 1] = gfc_int_expr (1);
5482 as->cp_was_assumed = true;
5484 else if (as->type == AS_ASSUMED_SHAPE)
5486 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
5487 return MATCH_ERROR;
5489 return MATCH_YES;
5493 /* Match the enum definition statement, here we are trying to match
5494 the first line of enum definition statement.
5495 Returns MATCH_YES if match is found. */
5497 match
5498 gfc_match_enum (void)
5500 match m;
5502 m = gfc_match_eos ();
5503 if (m != MATCH_YES)
5504 return m;
5506 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
5507 == FAILURE)
5508 return MATCH_ERROR;
5510 return MATCH_YES;
5514 /* Match a variable name with an optional initializer. When this
5515 subroutine is called, a variable is expected to be parsed next.
5516 Depending on what is happening at the moment, updates either the
5517 symbol table or the current interface. */
5519 static match
5520 enumerator_decl (void)
5522 char name[GFC_MAX_SYMBOL_LEN + 1];
5523 gfc_expr *initializer;
5524 gfc_array_spec *as = NULL;
5525 gfc_symbol *sym;
5526 locus var_locus;
5527 match m;
5528 try t;
5529 locus old_locus;
5531 initializer = NULL;
5532 old_locus = gfc_current_locus;
5534 /* When we get here, we've just matched a list of attributes and
5535 maybe a type and a double colon. The next thing we expect to see
5536 is the name of the symbol. */
5537 m = gfc_match_name (name);
5538 if (m != MATCH_YES)
5539 goto cleanup;
5541 var_locus = gfc_current_locus;
5543 /* OK, we've successfully matched the declaration. Now put the
5544 symbol in the current namespace. If we fail to create the symbol,
5545 bail out. */
5546 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
5548 m = MATCH_ERROR;
5549 goto cleanup;
5552 /* The double colon must be present in order to have initializers.
5553 Otherwise the statement is ambiguous with an assignment statement. */
5554 if (colon_seen)
5556 if (gfc_match_char ('=') == MATCH_YES)
5558 m = gfc_match_init_expr (&initializer);
5559 if (m == MATCH_NO)
5561 gfc_error ("Expected an initialization expression at %C");
5562 m = MATCH_ERROR;
5565 if (m != MATCH_YES)
5566 goto cleanup;
5570 /* If we do not have an initializer, the initialization value of the
5571 previous enumerator (stored in last_initializer) is incremented
5572 by 1 and is used to initialize the current enumerator. */
5573 if (initializer == NULL)
5574 initializer = gfc_enum_initializer (last_initializer, old_locus);
5576 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
5578 gfc_error("ENUMERATOR %L not initialized with integer expression",
5579 &var_locus);
5580 m = MATCH_ERROR;
5581 gfc_free_enum_history ();
5582 goto cleanup;
5585 /* Store this current initializer, for the next enumerator variable
5586 to be parsed. add_init_expr_to_sym() zeros initializer, so we
5587 use last_initializer below. */
5588 last_initializer = initializer;
5589 t = add_init_expr_to_sym (name, &initializer, &var_locus);
5591 /* Maintain enumerator history. */
5592 gfc_find_symbol (name, NULL, 0, &sym);
5593 create_enum_history (sym, last_initializer);
5595 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
5597 cleanup:
5598 /* Free stuff up and return. */
5599 gfc_free_expr (initializer);
5601 return m;
5605 /* Match the enumerator definition statement. */
5607 match
5608 gfc_match_enumerator_def (void)
5610 match m;
5611 try t;
5613 gfc_clear_ts (&current_ts);
5615 m = gfc_match (" enumerator");
5616 if (m != MATCH_YES)
5617 return m;
5619 m = gfc_match (" :: ");
5620 if (m == MATCH_ERROR)
5621 return m;
5623 colon_seen = (m == MATCH_YES);
5625 if (gfc_current_state () != COMP_ENUM)
5627 gfc_error ("ENUM definition statement expected before %C");
5628 gfc_free_enum_history ();
5629 return MATCH_ERROR;
5632 (&current_ts)->type = BT_INTEGER;
5633 (&current_ts)->kind = gfc_c_int_kind;
5635 gfc_clear_attr (&current_attr);
5636 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
5637 if (t == FAILURE)
5639 m = MATCH_ERROR;
5640 goto cleanup;
5643 for (;;)
5645 m = enumerator_decl ();
5646 if (m == MATCH_ERROR)
5647 goto cleanup;
5648 if (m == MATCH_NO)
5649 break;
5651 if (gfc_match_eos () == MATCH_YES)
5652 goto cleanup;
5653 if (gfc_match_char (',') != MATCH_YES)
5654 break;
5657 if (gfc_current_state () == COMP_ENUM)
5659 gfc_free_enum_history ();
5660 gfc_error ("Syntax error in ENUMERATOR definition at %C");
5661 m = MATCH_ERROR;
5664 cleanup:
5665 gfc_free_array_spec (current_as);
5666 current_as = NULL;
5667 return m;