Fix ChangeLog
[official-gcc.git] / gcc / fortran / decl.c
blobea87c211d4938677847e9ddc4e2773e7a65ff51b
1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.h"
24 #include "gfortran.h"
25 #include "match.h"
26 #include "parse.h"
29 /* Macros to access allocate memory for gfc_data_variable,
30 gfc_data_value and gfc_data. */
31 #define gfc_get_data_variable() gfc_getmem (sizeof (gfc_data_variable))
32 #define gfc_get_data_value() gfc_getmem (sizeof (gfc_data_value))
33 #define gfc_get_data() gfc_getmem( sizeof (gfc_data))
36 /* This flag is set if an old-style length selector is matched
37 during a type-declaration statement. */
39 static int old_char_selector;
41 /* When variables acquire types and attributes from a declaration
42 statement, they get them from the following static variables. The
43 first part of a declaration sets these variables and the second
44 part copies these into symbol structures. */
46 static gfc_typespec current_ts;
48 static symbol_attribute current_attr;
49 static gfc_array_spec *current_as;
50 static int colon_seen;
52 /* The current binding label (if any). */
53 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
54 /* Need to know how many identifiers are on the current data declaration
55 line in case we're given the BIND(C) attribute with a NAME= specifier. */
56 static int num_idents_on_line;
57 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
58 can supply a name if the curr_binding_label is nil and NAME= was not. */
59 static int has_name_equals = 0;
61 /* Initializer of the previous enumerator. */
63 static gfc_expr *last_initializer;
65 /* History of all the enumerators is maintained, so that
66 kind values of all the enumerators could be updated depending
67 upon the maximum initialized value. */
69 typedef struct enumerator_history
71 gfc_symbol *sym;
72 gfc_expr *initializer;
73 struct enumerator_history *next;
75 enumerator_history;
77 /* Header of enum history chain. */
79 static enumerator_history *enum_history = NULL;
81 /* Pointer of enum history node containing largest initializer. */
83 static enumerator_history *max_enum = NULL;
85 /* gfc_new_block points to the symbol of a newly matched block. */
87 gfc_symbol *gfc_new_block;
89 bool gfc_matching_function;
92 /********************* DATA statement subroutines *********************/
94 static bool in_match_data = false;
96 bool
97 gfc_in_match_data (void)
99 return in_match_data;
102 static void
103 set_in_match_data (bool set_value)
105 in_match_data = set_value;
108 /* Free a gfc_data_variable structure and everything beneath it. */
110 static void
111 free_variable (gfc_data_variable *p)
113 gfc_data_variable *q;
115 for (; p; p = q)
117 q = p->next;
118 gfc_free_expr (p->expr);
119 gfc_free_iterator (&p->iter, 0);
120 free_variable (p->list);
121 gfc_free (p);
126 /* Free a gfc_data_value structure and everything beneath it. */
128 static void
129 free_value (gfc_data_value *p)
131 gfc_data_value *q;
133 for (; p; p = q)
135 q = p->next;
136 gfc_free_expr (p->expr);
137 gfc_free (p);
142 /* Free a list of gfc_data structures. */
144 void
145 gfc_free_data (gfc_data *p)
147 gfc_data *q;
149 for (; p; p = q)
151 q = p->next;
152 free_variable (p->var);
153 free_value (p->value);
154 gfc_free (p);
159 /* Free all data in a namespace. */
161 static void
162 gfc_free_data_all (gfc_namespace *ns)
164 gfc_data *d;
166 for (;ns->data;)
168 d = ns->data->next;
169 gfc_free (ns->data);
170 ns->data = d;
175 static match var_element (gfc_data_variable *);
177 /* Match a list of variables terminated by an iterator and a right
178 parenthesis. */
180 static match
181 var_list (gfc_data_variable *parent)
183 gfc_data_variable *tail, var;
184 match m;
186 m = var_element (&var);
187 if (m == MATCH_ERROR)
188 return MATCH_ERROR;
189 if (m == MATCH_NO)
190 goto syntax;
192 tail = gfc_get_data_variable ();
193 *tail = var;
195 parent->list = tail;
197 for (;;)
199 if (gfc_match_char (',') != MATCH_YES)
200 goto syntax;
202 m = gfc_match_iterator (&parent->iter, 1);
203 if (m == MATCH_YES)
204 break;
205 if (m == MATCH_ERROR)
206 return MATCH_ERROR;
208 m = var_element (&var);
209 if (m == MATCH_ERROR)
210 return MATCH_ERROR;
211 if (m == MATCH_NO)
212 goto syntax;
214 tail->next = gfc_get_data_variable ();
215 tail = tail->next;
217 *tail = var;
220 if (gfc_match_char (')') != MATCH_YES)
221 goto syntax;
222 return MATCH_YES;
224 syntax:
225 gfc_syntax_error (ST_DATA);
226 return MATCH_ERROR;
230 /* Match a single element in a data variable list, which can be a
231 variable-iterator list. */
233 static match
234 var_element (gfc_data_variable *new)
236 match m;
237 gfc_symbol *sym;
239 memset (new, 0, sizeof (gfc_data_variable));
241 if (gfc_match_char ('(') == MATCH_YES)
242 return var_list (new);
244 m = gfc_match_variable (&new->expr, 0);
245 if (m != MATCH_YES)
246 return m;
248 sym = new->expr->symtree->n.sym;
250 if (!sym->attr.function && gfc_current_ns->parent
251 && gfc_current_ns->parent == sym->ns)
253 gfc_error ("Host associated variable '%s' may not be in the DATA "
254 "statement at %C", sym->name);
255 return MATCH_ERROR;
258 if (gfc_current_state () != COMP_BLOCK_DATA
259 && sym->attr.in_common
260 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
261 "common block variable '%s' in DATA statement at %C",
262 sym->name) == FAILURE)
263 return MATCH_ERROR;
265 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
266 return MATCH_ERROR;
268 return MATCH_YES;
272 /* Match the top-level list of data variables. */
274 static match
275 top_var_list (gfc_data *d)
277 gfc_data_variable var, *tail, *new;
278 match m;
280 tail = NULL;
282 for (;;)
284 m = var_element (&var);
285 if (m == MATCH_NO)
286 goto syntax;
287 if (m == MATCH_ERROR)
288 return MATCH_ERROR;
290 new = gfc_get_data_variable ();
291 *new = var;
293 if (tail == NULL)
294 d->var = new;
295 else
296 tail->next = new;
298 tail = new;
300 if (gfc_match_char ('/') == MATCH_YES)
301 break;
302 if (gfc_match_char (',') != MATCH_YES)
303 goto syntax;
306 return MATCH_YES;
308 syntax:
309 gfc_syntax_error (ST_DATA);
310 gfc_free_data_all (gfc_current_ns);
311 return MATCH_ERROR;
315 static match
316 match_data_constant (gfc_expr **result)
318 char name[GFC_MAX_SYMBOL_LEN + 1];
319 gfc_symbol *sym;
320 gfc_expr *expr;
321 match m;
322 locus old_loc;
324 m = gfc_match_literal_constant (&expr, 1);
325 if (m == MATCH_YES)
327 *result = expr;
328 return MATCH_YES;
331 if (m == MATCH_ERROR)
332 return MATCH_ERROR;
334 m = gfc_match_null (result);
335 if (m != MATCH_NO)
336 return m;
338 old_loc = gfc_current_locus;
340 /* Should this be a structure component, try to match it
341 before matching a name. */
342 m = gfc_match_rvalue (result);
343 if (m == MATCH_ERROR)
344 return m;
346 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
348 if (gfc_simplify_expr (*result, 0) == FAILURE)
349 m = MATCH_ERROR;
350 return m;
353 gfc_current_locus = old_loc;
355 m = gfc_match_name (name);
356 if (m != MATCH_YES)
357 return m;
359 if (gfc_find_symbol (name, NULL, 1, &sym))
360 return MATCH_ERROR;
362 if (sym == NULL
363 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
365 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
366 name);
367 return MATCH_ERROR;
369 else if (sym->attr.flavor == FL_DERIVED)
370 return gfc_match_structure_constructor (sym, result);
372 /* Check to see if the value is an initialization array expression. */
373 if (sym->value->expr_type == EXPR_ARRAY)
375 gfc_current_locus = old_loc;
377 m = gfc_match_init_expr (result);
378 if (m == MATCH_ERROR)
379 return m;
381 if (m == MATCH_YES)
383 if (gfc_simplify_expr (*result, 0) == FAILURE)
384 m = MATCH_ERROR;
386 if ((*result)->expr_type == EXPR_CONSTANT)
387 return m;
388 else
390 gfc_error ("Invalid initializer %s in Data statement at %C", name);
391 return MATCH_ERROR;
396 *result = gfc_copy_expr (sym->value);
397 return MATCH_YES;
401 /* Match a list of values in a DATA statement. The leading '/' has
402 already been seen at this point. */
404 static match
405 top_val_list (gfc_data *data)
407 gfc_data_value *new, *tail;
408 gfc_expr *expr;
409 match m;
411 tail = NULL;
413 for (;;)
415 m = match_data_constant (&expr);
416 if (m == MATCH_NO)
417 goto syntax;
418 if (m == MATCH_ERROR)
419 return MATCH_ERROR;
421 new = gfc_get_data_value ();
422 mpz_init (new->repeat);
424 if (tail == NULL)
425 data->value = new;
426 else
427 tail->next = new;
429 tail = new;
431 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
433 tail->expr = expr;
434 mpz_set_ui (tail->repeat, 1);
436 else
438 if (expr->ts.type == BT_INTEGER)
439 mpz_set (tail->repeat, expr->value.integer);
440 gfc_free_expr (expr);
442 m = match_data_constant (&tail->expr);
443 if (m == MATCH_NO)
444 goto syntax;
445 if (m == MATCH_ERROR)
446 return MATCH_ERROR;
449 if (gfc_match_char ('/') == MATCH_YES)
450 break;
451 if (gfc_match_char (',') == MATCH_NO)
452 goto syntax;
455 return MATCH_YES;
457 syntax:
458 gfc_syntax_error (ST_DATA);
459 gfc_free_data_all (gfc_current_ns);
460 return MATCH_ERROR;
464 /* Matches an old style initialization. */
466 static match
467 match_old_style_init (const char *name)
469 match m;
470 gfc_symtree *st;
471 gfc_symbol *sym;
472 gfc_data *newdata;
474 /* Set up data structure to hold initializers. */
475 gfc_find_sym_tree (name, NULL, 0, &st);
476 sym = st->n.sym;
478 newdata = gfc_get_data ();
479 newdata->var = gfc_get_data_variable ();
480 newdata->var->expr = gfc_get_variable_expr (st);
481 newdata->where = gfc_current_locus;
483 /* Match initial value list. This also eats the terminal '/'. */
484 m = top_val_list (newdata);
485 if (m != MATCH_YES)
487 gfc_free (newdata);
488 return m;
491 if (gfc_pure (NULL))
493 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
494 gfc_free (newdata);
495 return MATCH_ERROR;
498 /* Mark the variable as having appeared in a data statement. */
499 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
501 gfc_free (newdata);
502 return MATCH_ERROR;
505 /* Chain in namespace list of DATA initializers. */
506 newdata->next = gfc_current_ns->data;
507 gfc_current_ns->data = newdata;
509 return m;
513 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
514 we are matching a DATA statement and are therefore issuing an error
515 if we encounter something unexpected, if not, we're trying to match
516 an old-style initialization expression of the form INTEGER I /2/. */
518 match
519 gfc_match_data (void)
521 gfc_data *new;
522 match m;
524 set_in_match_data (true);
526 for (;;)
528 new = gfc_get_data ();
529 new->where = gfc_current_locus;
531 m = top_var_list (new);
532 if (m != MATCH_YES)
533 goto cleanup;
535 m = top_val_list (new);
536 if (m != MATCH_YES)
537 goto cleanup;
539 new->next = gfc_current_ns->data;
540 gfc_current_ns->data = new;
542 if (gfc_match_eos () == MATCH_YES)
543 break;
545 gfc_match_char (','); /* Optional comma */
548 set_in_match_data (false);
550 if (gfc_pure (NULL))
552 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
553 return MATCH_ERROR;
556 return MATCH_YES;
558 cleanup:
559 set_in_match_data (false);
560 gfc_free_data (new);
561 return MATCH_ERROR;
565 /************************ Declaration statements *********************/
567 /* Match an intent specification. Since this can only happen after an
568 INTENT word, a legal intent-spec must follow. */
570 static sym_intent
571 match_intent_spec (void)
574 if (gfc_match (" ( in out )") == MATCH_YES)
575 return INTENT_INOUT;
576 if (gfc_match (" ( in )") == MATCH_YES)
577 return INTENT_IN;
578 if (gfc_match (" ( out )") == MATCH_YES)
579 return INTENT_OUT;
581 gfc_error ("Bad INTENT specification at %C");
582 return INTENT_UNKNOWN;
586 /* Matches a character length specification, which is either a
587 specification expression or a '*'. */
589 static match
590 char_len_param_value (gfc_expr **expr)
592 match m;
594 if (gfc_match_char ('*') == MATCH_YES)
596 *expr = NULL;
597 return MATCH_YES;
600 m = gfc_match_expr (expr);
601 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
603 if ((*expr)->value.function.actual
604 && (*expr)->value.function.actual->expr->symtree)
606 gfc_expr *e;
607 e = (*expr)->value.function.actual->expr;
608 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
609 && e->expr_type == EXPR_VARIABLE)
611 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
612 goto syntax;
613 if (e->symtree->n.sym->ts.type == BT_CHARACTER
614 && e->symtree->n.sym->ts.cl
615 && e->symtree->n.sym->ts.cl->length->ts.type == BT_UNKNOWN)
616 goto syntax;
620 return m;
622 syntax:
623 gfc_error ("Conflict in attributes of function argument at %C");
624 return MATCH_ERROR;
628 /* A character length is a '*' followed by a literal integer or a
629 char_len_param_value in parenthesis. */
631 static match
632 match_char_length (gfc_expr **expr)
634 int length;
635 match m;
637 m = gfc_match_char ('*');
638 if (m != MATCH_YES)
639 return m;
641 m = gfc_match_small_literal_int (&length, NULL);
642 if (m == MATCH_ERROR)
643 return m;
645 if (m == MATCH_YES)
647 *expr = gfc_int_expr (length);
648 return m;
651 if (gfc_match_char ('(') == MATCH_NO)
652 goto syntax;
654 m = char_len_param_value (expr);
655 if (m != MATCH_YES && gfc_matching_function)
657 gfc_undo_symbols ();
658 m = MATCH_YES;
661 if (m == MATCH_ERROR)
662 return m;
663 if (m == MATCH_NO)
664 goto syntax;
666 if (gfc_match_char (')') == MATCH_NO)
668 gfc_free_expr (*expr);
669 *expr = NULL;
670 goto syntax;
673 return MATCH_YES;
675 syntax:
676 gfc_error ("Syntax error in character length specification at %C");
677 return MATCH_ERROR;
681 /* Special subroutine for finding a symbol. Check if the name is found
682 in the current name space. If not, and we're compiling a function or
683 subroutine and the parent compilation unit is an interface, then check
684 to see if the name we've been given is the name of the interface
685 (located in another namespace). */
687 static int
688 find_special (const char *name, gfc_symbol **result)
690 gfc_state_data *s;
691 int i;
693 i = gfc_get_symbol (name, NULL, result);
694 if (i == 0)
695 goto end;
697 if (gfc_current_state () != COMP_SUBROUTINE
698 && gfc_current_state () != COMP_FUNCTION)
699 goto end;
701 s = gfc_state_stack->previous;
702 if (s == NULL)
703 goto end;
705 if (s->state != COMP_INTERFACE)
706 goto end;
707 if (s->sym == NULL)
708 goto end; /* Nameless interface. */
710 if (strcmp (name, s->sym->name) == 0)
712 *result = s->sym;
713 return 0;
716 end:
717 return i;
721 /* Special subroutine for getting a symbol node associated with a
722 procedure name, used in SUBROUTINE and FUNCTION statements. The
723 symbol is created in the parent using with symtree node in the
724 child unit pointing to the symbol. If the current namespace has no
725 parent, then the symbol is just created in the current unit. */
727 static int
728 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
730 gfc_symtree *st;
731 gfc_symbol *sym;
732 int rc = 0;
734 /* Module functions have to be left in their own namespace because
735 they have potentially (almost certainly!) already been referenced.
736 In this sense, they are rather like external functions. This is
737 fixed up in resolve.c(resolve_entries), where the symbol name-
738 space is set to point to the master function, so that the fake
739 result mechanism can work. */
740 if (module_fcn_entry)
742 /* Present if entry is declared to be a module procedure. */
743 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
745 if (*result == NULL)
746 rc = gfc_get_symbol (name, NULL, result);
747 else if (!gfc_get_symbol (name, NULL, &sym) && sym
748 && (*result)->ts.type == BT_UNKNOWN
749 && sym->attr.flavor == FL_UNKNOWN)
750 /* Pick up the typespec for the entry, if declared in the function
751 body. Note that this symbol is FL_UNKNOWN because it will
752 only have appeared in a type declaration. The local symtree
753 is set to point to the module symbol and a unique symtree
754 to the local version. This latter ensures a correct clearing
755 of the symbols. */
757 /* If the ENTRY proceeds its specification, we need to ensure
758 that this does not raise a "has no IMPLICIT type" error. */
759 if (sym->ts.type == BT_UNKNOWN)
760 sym->attr.untyped = 1;
762 (*result)->ts = sym->ts;
764 /* Put the symbol in the procedure namespace so that, should
765 the ENTRY preceed its specification, the specification
766 can be applied. */
767 (*result)->ns = gfc_current_ns;
769 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
770 st->n.sym = *result;
771 st = gfc_get_unique_symtree (gfc_current_ns);
772 st->n.sym = sym;
775 else
776 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
778 if (rc)
779 return rc;
781 sym = *result;
782 gfc_current_ns->refs++;
784 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
786 /* Trap another encompassed procedure with the same name. All
787 these conditions are necessary to avoid picking up an entry
788 whose name clashes with that of the encompassing procedure;
789 this is handled using gsymbols to register unique,globally
790 accessible names. */
791 if (sym->attr.flavor != 0
792 && sym->attr.proc != 0
793 && (sym->attr.subroutine || sym->attr.function)
794 && sym->attr.if_source != IFSRC_UNKNOWN)
795 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
796 name, &sym->declared_at);
798 /* Trap a procedure with a name the same as interface in the
799 encompassing scope. */
800 if (sym->attr.generic != 0
801 && (sym->attr.subroutine || sym->attr.function)
802 && !sym->attr.mod_proc)
803 gfc_error_now ("Name '%s' at %C is already defined"
804 " as a generic interface at %L",
805 name, &sym->declared_at);
807 /* Trap declarations of attributes in encompassing scope. The
808 signature for this is that ts.kind is set. Legitimate
809 references only set ts.type. */
810 if (sym->ts.kind != 0
811 && !sym->attr.implicit_type
812 && sym->attr.proc == 0
813 && gfc_current_ns->parent != NULL
814 && sym->attr.access == 0
815 && !module_fcn_entry)
816 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
817 "and must not have attributes declared at %L",
818 name, &sym->declared_at);
821 if (gfc_current_ns->parent == NULL || *result == NULL)
822 return rc;
824 /* Module function entries will already have a symtree in
825 the current namespace but will need one at module level. */
826 if (module_fcn_entry)
828 /* Present if entry is declared to be a module procedure. */
829 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
830 if (st == NULL)
831 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
833 else
834 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
836 st->n.sym = sym;
837 sym->refs++;
839 /* See if the procedure should be a module procedure. */
841 if (((sym->ns->proc_name != NULL
842 && sym->ns->proc_name->attr.flavor == FL_MODULE
843 && sym->attr.proc != PROC_MODULE)
844 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
845 && gfc_add_procedure (&sym->attr, PROC_MODULE,
846 sym->name, NULL) == FAILURE)
847 rc = 2;
849 return rc;
853 /* Verify that the given symbol representing a parameter is C
854 interoperable, by checking to see if it was marked as such after
855 its declaration. If the given symbol is not interoperable, a
856 warning is reported, thus removing the need to return the status to
857 the calling function. The standard does not require the user use
858 one of the iso_c_binding named constants to declare an
859 interoperable parameter, but we can't be sure if the param is C
860 interop or not if the user doesn't. For example, integer(4) may be
861 legal Fortran, but doesn't have meaning in C. It may interop with
862 a number of the C types, which causes a problem because the
863 compiler can't know which one. This code is almost certainly not
864 portable, and the user will get what they deserve if the C type
865 across platforms isn't always interoperable with integer(4). If
866 the user had used something like integer(c_int) or integer(c_long),
867 the compiler could have automatically handled the varying sizes
868 across platforms. */
871 verify_c_interop_param (gfc_symbol *sym)
873 int is_c_interop = 0;
874 try retval = SUCCESS;
876 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
877 Don't repeat the checks here. */
878 if (sym->attr.implicit_type)
879 return SUCCESS;
881 /* For subroutines or functions that are passed to a BIND(C) procedure,
882 they're interoperable if they're BIND(C) and their params are all
883 interoperable. */
884 if (sym->attr.flavor == FL_PROCEDURE)
886 if (sym->attr.is_bind_c == 0)
888 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
889 "attribute to be C interoperable", sym->name,
890 &(sym->declared_at));
892 return FAILURE;
894 else
896 if (sym->attr.is_c_interop == 1)
897 /* We've already checked this procedure; don't check it again. */
898 return SUCCESS;
899 else
900 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
901 sym->common_block);
905 /* See if we've stored a reference to a procedure that owns sym. */
906 if (sym->ns != NULL && sym->ns->proc_name != NULL)
908 if (sym->ns->proc_name->attr.is_bind_c == 1)
910 is_c_interop =
911 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
912 == SUCCESS ? 1 : 0);
914 if (is_c_interop != 1)
916 /* Make personalized messages to give better feedback. */
917 if (sym->ts.type == BT_DERIVED)
918 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
919 " procedure '%s' but is not C interoperable "
920 "because derived type '%s' is not C interoperable",
921 sym->name, &(sym->declared_at),
922 sym->ns->proc_name->name,
923 sym->ts.derived->name);
924 else
925 gfc_warning ("Variable '%s' at %L is a parameter to the "
926 "BIND(C) procedure '%s' but may not be C "
927 "interoperable",
928 sym->name, &(sym->declared_at),
929 sym->ns->proc_name->name);
932 /* Character strings are only C interoperable if they have a
933 length of 1. */
934 if (sym->ts.type == BT_CHARACTER)
936 gfc_charlen *cl = sym->ts.cl;
937 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
938 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
940 gfc_error ("Character argument '%s' at %L "
941 "must be length 1 because "
942 "procedure '%s' is BIND(C)",
943 sym->name, &sym->declared_at,
944 sym->ns->proc_name->name);
945 retval = FAILURE;
949 /* We have to make sure that any param to a bind(c) routine does
950 not have the allocatable, pointer, or optional attributes,
951 according to J3/04-007, section 5.1. */
952 if (sym->attr.allocatable == 1)
954 gfc_error ("Variable '%s' at %L cannot have the "
955 "ALLOCATABLE attribute because procedure '%s'"
956 " is BIND(C)", sym->name, &(sym->declared_at),
957 sym->ns->proc_name->name);
958 retval = FAILURE;
961 if (sym->attr.pointer == 1)
963 gfc_error ("Variable '%s' at %L cannot have the "
964 "POINTER attribute because procedure '%s'"
965 " is BIND(C)", sym->name, &(sym->declared_at),
966 sym->ns->proc_name->name);
967 retval = FAILURE;
970 if (sym->attr.optional == 1)
972 gfc_error ("Variable '%s' at %L cannot have the "
973 "OPTIONAL attribute because procedure '%s'"
974 " is BIND(C)", sym->name, &(sym->declared_at),
975 sym->ns->proc_name->name);
976 retval = FAILURE;
979 /* Make sure that if it has the dimension attribute, that it is
980 either assumed size or explicit shape. */
981 if (sym->as != NULL)
983 if (sym->as->type == AS_ASSUMED_SHAPE)
985 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
986 "argument to the procedure '%s' at %L because "
987 "the procedure is BIND(C)", sym->name,
988 &(sym->declared_at), sym->ns->proc_name->name,
989 &(sym->ns->proc_name->declared_at));
990 retval = FAILURE;
993 if (sym->as->type == AS_DEFERRED)
995 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
996 "argument to the procedure '%s' at %L because "
997 "the procedure is BIND(C)", sym->name,
998 &(sym->declared_at), sym->ns->proc_name->name,
999 &(sym->ns->proc_name->declared_at));
1000 retval = FAILURE;
1006 return retval;
1010 /* Function called by variable_decl() that adds a name to the symbol table. */
1012 static try
1013 build_sym (const char *name, gfc_charlen *cl,
1014 gfc_array_spec **as, locus *var_locus)
1016 symbol_attribute attr;
1017 gfc_symbol *sym;
1019 if (gfc_get_symbol (name, NULL, &sym))
1020 return FAILURE;
1022 /* Start updating the symbol table. Add basic type attribute if present. */
1023 if (current_ts.type != BT_UNKNOWN
1024 && (sym->attr.implicit_type == 0
1025 || !gfc_compare_types (&sym->ts, &current_ts))
1026 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
1027 return FAILURE;
1029 if (sym->ts.type == BT_CHARACTER)
1030 sym->ts.cl = cl;
1032 /* Add dimension attribute if present. */
1033 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1034 return FAILURE;
1035 *as = NULL;
1037 /* Add attribute to symbol. The copy is so that we can reset the
1038 dimension attribute. */
1039 attr = current_attr;
1040 attr.dimension = 0;
1042 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1043 return FAILURE;
1045 /* Finish any work that may need to be done for the binding label,
1046 if it's a bind(c). The bind(c) attr is found before the symbol
1047 is made, and before the symbol name (for data decls), so the
1048 current_ts is holding the binding label, or nothing if the
1049 name= attr wasn't given. Therefore, test here if we're dealing
1050 with a bind(c) and make sure the binding label is set correctly. */
1051 if (sym->attr.is_bind_c == 1)
1053 if (sym->binding_label[0] == '\0')
1055 /* Set the binding label and verify that if a NAME= was specified
1056 then only one identifier was in the entity-decl-list. */
1057 if (set_binding_label (sym->binding_label, sym->name,
1058 num_idents_on_line) == FAILURE)
1059 return FAILURE;
1063 /* See if we know we're in a common block, and if it's a bind(c)
1064 common then we need to make sure we're an interoperable type. */
1065 if (sym->attr.in_common == 1)
1067 /* Test the common block object. */
1068 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1069 && sym->ts.is_c_interop != 1)
1071 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1072 "must be declared with a C interoperable "
1073 "kind since common block '%s' is BIND(C)",
1074 sym->name, sym->common_block->name,
1075 sym->common_block->name);
1076 gfc_clear_error ();
1080 sym->attr.implied_index = 0;
1082 return SUCCESS;
1086 /* Set character constant to the given length. The constant will be padded or
1087 truncated. */
1089 void
1090 gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
1092 gfc_char_t *s;
1093 int slen;
1095 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1096 gcc_assert (expr->ts.type == BT_CHARACTER);
1098 slen = expr->value.character.length;
1099 if (len != slen)
1101 s = gfc_get_wide_string (len + 1);
1102 memcpy (s, expr->value.character.string,
1103 MIN (len, slen) * sizeof (gfc_char_t));
1104 if (len > slen)
1105 gfc_wide_memset (&s[slen], ' ', len - slen);
1107 if (gfc_option.warn_character_truncation && slen > len)
1108 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1109 "(%d/%d)", &expr->where, slen, len);
1111 /* Apply the standard by 'hand' otherwise it gets cleared for
1112 initializers. */
1113 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
1114 gfc_error_now ("The CHARACTER elements of the array constructor "
1115 "at %L must have the same length (%d/%d)",
1116 &expr->where, slen, len);
1118 s[len] = '\0';
1119 gfc_free (expr->value.character.string);
1120 expr->value.character.string = s;
1121 expr->value.character.length = len;
1126 /* Function to create and update the enumerator history
1127 using the information passed as arguments.
1128 Pointer "max_enum" is also updated, to point to
1129 enum history node containing largest initializer.
1131 SYM points to the symbol node of enumerator.
1132 INIT points to its enumerator value. */
1134 static void
1135 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1137 enumerator_history *new_enum_history;
1138 gcc_assert (sym != NULL && init != NULL);
1140 new_enum_history = gfc_getmem (sizeof (enumerator_history));
1142 new_enum_history->sym = sym;
1143 new_enum_history->initializer = init;
1144 new_enum_history->next = NULL;
1146 if (enum_history == NULL)
1148 enum_history = new_enum_history;
1149 max_enum = enum_history;
1151 else
1153 new_enum_history->next = enum_history;
1154 enum_history = new_enum_history;
1156 if (mpz_cmp (max_enum->initializer->value.integer,
1157 new_enum_history->initializer->value.integer) < 0)
1158 max_enum = new_enum_history;
1163 /* Function to free enum kind history. */
1165 void
1166 gfc_free_enum_history (void)
1168 enumerator_history *current = enum_history;
1169 enumerator_history *next;
1171 while (current != NULL)
1173 next = current->next;
1174 gfc_free (current);
1175 current = next;
1177 max_enum = NULL;
1178 enum_history = NULL;
1182 /* Function called by variable_decl() that adds an initialization
1183 expression to a symbol. */
1185 static try
1186 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1188 symbol_attribute attr;
1189 gfc_symbol *sym;
1190 gfc_expr *init;
1192 init = *initp;
1193 if (find_special (name, &sym))
1194 return FAILURE;
1196 attr = sym->attr;
1198 /* If this symbol is confirming an implicit parameter type,
1199 then an initialization expression is not allowed. */
1200 if (attr.flavor == FL_PARAMETER
1201 && sym->value != NULL
1202 && *initp != NULL)
1204 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1205 sym->name);
1206 return FAILURE;
1209 if (init == NULL)
1211 /* An initializer is required for PARAMETER declarations. */
1212 if (attr.flavor == FL_PARAMETER)
1214 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1215 return FAILURE;
1218 else
1220 /* If a variable appears in a DATA block, it cannot have an
1221 initializer. */
1222 if (sym->attr.data)
1224 gfc_error ("Variable '%s' at %C with an initializer already "
1225 "appears in a DATA statement", sym->name);
1226 return FAILURE;
1229 /* Check if the assignment can happen. This has to be put off
1230 until later for a derived type variable. */
1231 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1232 && gfc_check_assign_symbol (sym, init) == FAILURE)
1233 return FAILURE;
1235 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1237 /* Update symbol character length according initializer. */
1238 if (sym->ts.cl->length == NULL)
1240 int clen;
1241 /* If there are multiple CHARACTER variables declared on the
1242 same line, we don't want them to share the same length. */
1243 sym->ts.cl = gfc_get_charlen ();
1244 sym->ts.cl->next = gfc_current_ns->cl_list;
1245 gfc_current_ns->cl_list = sym->ts.cl;
1247 if (sym->attr.flavor == FL_PARAMETER)
1249 if (init->expr_type == EXPR_CONSTANT)
1251 clen = init->value.character.length;
1252 sym->ts.cl->length = gfc_int_expr (clen);
1254 else if (init->expr_type == EXPR_ARRAY)
1256 gfc_expr *p = init->value.constructor->expr;
1257 clen = p->value.character.length;
1258 sym->ts.cl->length = gfc_int_expr (clen);
1260 else if (init->ts.cl && init->ts.cl->length)
1261 sym->ts.cl->length =
1262 gfc_copy_expr (sym->value->ts.cl->length);
1265 /* Update initializer character length according symbol. */
1266 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1268 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1269 gfc_constructor * p;
1271 if (init->expr_type == EXPR_CONSTANT)
1272 gfc_set_constant_character_len (len, init, false);
1273 else if (init->expr_type == EXPR_ARRAY)
1275 /* Build a new charlen to prevent simplification from
1276 deleting the length before it is resolved. */
1277 init->ts.cl = gfc_get_charlen ();
1278 init->ts.cl->next = gfc_current_ns->cl_list;
1279 gfc_current_ns->cl_list = sym->ts.cl;
1280 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
1282 for (p = init->value.constructor; p; p = p->next)
1283 gfc_set_constant_character_len (len, p->expr, false);
1288 /* Need to check if the expression we initialized this
1289 to was one of the iso_c_binding named constants. If so,
1290 and we're a parameter (constant), let it be iso_c.
1291 For example:
1292 integer(c_int), parameter :: my_int = c_int
1293 integer(my_int) :: my_int_2
1294 If we mark my_int as iso_c (since we can see it's value
1295 is equal to one of the named constants), then my_int_2
1296 will be considered C interoperable. */
1297 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1299 sym->ts.is_iso_c |= init->ts.is_iso_c;
1300 sym->ts.is_c_interop |= init->ts.is_c_interop;
1301 /* attr bits needed for module files. */
1302 sym->attr.is_iso_c |= init->ts.is_iso_c;
1303 sym->attr.is_c_interop |= init->ts.is_c_interop;
1304 if (init->ts.is_iso_c)
1305 sym->ts.f90_type = init->ts.f90_type;
1308 /* Add initializer. Make sure we keep the ranks sane. */
1309 if (sym->attr.dimension && init->rank == 0)
1311 mpz_t size;
1312 gfc_expr *array;
1313 gfc_constructor *c;
1314 int n;
1315 if (sym->attr.flavor == FL_PARAMETER
1316 && init->expr_type == EXPR_CONSTANT
1317 && spec_size (sym->as, &size) == SUCCESS
1318 && mpz_cmp_si (size, 0) > 0)
1320 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1321 &init->where);
1323 array->value.constructor = c = NULL;
1324 for (n = 0; n < (int)mpz_get_si (size); n++)
1326 if (array->value.constructor == NULL)
1328 array->value.constructor = c = gfc_get_constructor ();
1329 c->expr = init;
1331 else
1333 c->next = gfc_get_constructor ();
1334 c = c->next;
1335 c->expr = gfc_copy_expr (init);
1339 array->shape = gfc_get_shape (sym->as->rank);
1340 for (n = 0; n < sym->as->rank; n++)
1341 spec_dimen_size (sym->as, n, &array->shape[n]);
1343 init = array;
1344 mpz_clear (size);
1346 init->rank = sym->as->rank;
1349 sym->value = init;
1350 if (sym->attr.save == SAVE_NONE)
1351 sym->attr.save = SAVE_IMPLICIT;
1352 *initp = NULL;
1355 return SUCCESS;
1359 /* Function called by variable_decl() that adds a name to a structure
1360 being built. */
1362 static try
1363 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1364 gfc_array_spec **as)
1366 gfc_component *c;
1368 /* If the current symbol is of the same derived type that we're
1369 constructing, it must have the pointer attribute. */
1370 if (current_ts.type == BT_DERIVED
1371 && current_ts.derived == gfc_current_block ()
1372 && current_attr.pointer == 0)
1374 gfc_error ("Component at %C must have the POINTER attribute");
1375 return FAILURE;
1378 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1380 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1382 gfc_error ("Array component of structure at %C must have explicit "
1383 "or deferred shape");
1384 return FAILURE;
1388 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1389 return FAILURE;
1391 c->ts = current_ts;
1392 c->ts.cl = cl;
1393 gfc_set_component_attr (c, &current_attr);
1395 c->initializer = *init;
1396 *init = NULL;
1398 c->as = *as;
1399 if (c->as != NULL)
1400 c->dimension = 1;
1401 *as = NULL;
1403 /* Should this ever get more complicated, combine with similar section
1404 in add_init_expr_to_sym into a separate function. */
1405 if (c->ts.type == BT_CHARACTER && !c->pointer && c->initializer)
1407 int len = mpz_get_si (c->ts.cl->length->value.integer);
1409 if (c->initializer->expr_type == EXPR_CONSTANT)
1410 gfc_set_constant_character_len (len, c->initializer, false);
1411 else if (mpz_cmp (c->ts.cl->length->value.integer,
1412 c->initializer->ts.cl->length->value.integer))
1414 gfc_constructor *ctor = c->initializer->value.constructor;
1415 for (;ctor ; ctor = ctor->next)
1416 if (ctor->expr->expr_type == EXPR_CONSTANT)
1417 gfc_set_constant_character_len (len, ctor->expr, true);
1421 /* Check array components. */
1422 if (!c->dimension)
1424 if (c->allocatable)
1426 gfc_error ("Allocatable component at %C must be an array");
1427 return FAILURE;
1429 else
1430 return SUCCESS;
1433 if (c->pointer)
1435 if (c->as->type != AS_DEFERRED)
1437 gfc_error ("Pointer array component of structure at %C must have a "
1438 "deferred shape");
1439 return FAILURE;
1442 else if (c->allocatable)
1444 if (c->as->type != AS_DEFERRED)
1446 gfc_error ("Allocatable component of structure at %C must have a "
1447 "deferred shape");
1448 return FAILURE;
1451 else
1453 if (c->as->type != AS_EXPLICIT)
1455 gfc_error ("Array component of structure at %C must have an "
1456 "explicit shape");
1457 return FAILURE;
1461 return SUCCESS;
1465 /* Match a 'NULL()', and possibly take care of some side effects. */
1467 match
1468 gfc_match_null (gfc_expr **result)
1470 gfc_symbol *sym;
1471 gfc_expr *e;
1472 match m;
1474 m = gfc_match (" null ( )");
1475 if (m != MATCH_YES)
1476 return m;
1478 /* The NULL symbol now has to be/become an intrinsic function. */
1479 if (gfc_get_symbol ("null", NULL, &sym))
1481 gfc_error ("NULL() initialization at %C is ambiguous");
1482 return MATCH_ERROR;
1485 gfc_intrinsic_symbol (sym);
1487 if (sym->attr.proc != PROC_INTRINSIC
1488 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1489 sym->name, NULL) == FAILURE
1490 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1491 return MATCH_ERROR;
1493 e = gfc_get_expr ();
1494 e->where = gfc_current_locus;
1495 e->expr_type = EXPR_NULL;
1496 e->ts.type = BT_UNKNOWN;
1498 *result = e;
1500 return MATCH_YES;
1504 /* Match a variable name with an optional initializer. When this
1505 subroutine is called, a variable is expected to be parsed next.
1506 Depending on what is happening at the moment, updates either the
1507 symbol table or the current interface. */
1509 static match
1510 variable_decl (int elem)
1512 char name[GFC_MAX_SYMBOL_LEN + 1];
1513 gfc_expr *initializer, *char_len;
1514 gfc_array_spec *as;
1515 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1516 gfc_charlen *cl;
1517 locus var_locus;
1518 match m;
1519 try t;
1520 gfc_symbol *sym;
1521 locus old_locus;
1523 initializer = NULL;
1524 as = NULL;
1525 cp_as = NULL;
1526 old_locus = gfc_current_locus;
1528 /* When we get here, we've just matched a list of attributes and
1529 maybe a type and a double colon. The next thing we expect to see
1530 is the name of the symbol. */
1531 m = gfc_match_name (name);
1532 if (m != MATCH_YES)
1533 goto cleanup;
1535 var_locus = gfc_current_locus;
1537 /* Now we could see the optional array spec. or character length. */
1538 m = gfc_match_array_spec (&as);
1539 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1540 cp_as = gfc_copy_array_spec (as);
1541 else if (m == MATCH_ERROR)
1542 goto cleanup;
1544 if (m == MATCH_NO)
1545 as = gfc_copy_array_spec (current_as);
1547 char_len = NULL;
1548 cl = NULL;
1550 if (current_ts.type == BT_CHARACTER)
1552 switch (match_char_length (&char_len))
1554 case MATCH_YES:
1555 cl = gfc_get_charlen ();
1556 cl->next = gfc_current_ns->cl_list;
1557 gfc_current_ns->cl_list = cl;
1559 cl->length = char_len;
1560 break;
1562 /* Non-constant lengths need to be copied after the first
1563 element. Also copy assumed lengths. */
1564 case MATCH_NO:
1565 if (elem > 1
1566 && (current_ts.cl->length == NULL
1567 || current_ts.cl->length->expr_type != EXPR_CONSTANT))
1569 cl = gfc_get_charlen ();
1570 cl->next = gfc_current_ns->cl_list;
1571 gfc_current_ns->cl_list = cl;
1572 cl->length = gfc_copy_expr (current_ts.cl->length);
1574 else
1575 cl = current_ts.cl;
1577 break;
1579 case MATCH_ERROR:
1580 goto cleanup;
1584 /* If this symbol has already shown up in a Cray Pointer declaration,
1585 then we want to set the type & bail out. */
1586 if (gfc_option.flag_cray_pointer)
1588 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1589 if (sym != NULL && sym->attr.cray_pointee)
1591 sym->ts.type = current_ts.type;
1592 sym->ts.kind = current_ts.kind;
1593 sym->ts.cl = cl;
1594 sym->ts.derived = current_ts.derived;
1595 sym->ts.is_c_interop = current_ts.is_c_interop;
1596 sym->ts.is_iso_c = current_ts.is_iso_c;
1597 m = MATCH_YES;
1599 /* Check to see if we have an array specification. */
1600 if (cp_as != NULL)
1602 if (sym->as != NULL)
1604 gfc_error ("Duplicate array spec for Cray pointee at %C");
1605 gfc_free_array_spec (cp_as);
1606 m = MATCH_ERROR;
1607 goto cleanup;
1609 else
1611 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1612 gfc_internal_error ("Couldn't set pointee array spec.");
1614 /* Fix the array spec. */
1615 m = gfc_mod_pointee_as (sym->as);
1616 if (m == MATCH_ERROR)
1617 goto cleanup;
1620 goto cleanup;
1622 else
1624 gfc_free_array_spec (cp_as);
1629 /* OK, we've successfully matched the declaration. Now put the
1630 symbol in the current namespace, because it might be used in the
1631 optional initialization expression for this symbol, e.g. this is
1632 perfectly legal:
1634 integer, parameter :: i = huge(i)
1636 This is only true for parameters or variables of a basic type.
1637 For components of derived types, it is not true, so we don't
1638 create a symbol for those yet. If we fail to create the symbol,
1639 bail out. */
1640 if (gfc_current_state () != COMP_DERIVED
1641 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1643 m = MATCH_ERROR;
1644 goto cleanup;
1647 /* An interface body specifies all of the procedure's
1648 characteristics and these shall be consistent with those
1649 specified in the procedure definition, except that the interface
1650 may specify a procedure that is not pure if the procedure is
1651 defined to be pure(12.3.2). */
1652 if (current_ts.type == BT_DERIVED
1653 && gfc_current_ns->proc_name
1654 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1655 && current_ts.derived->ns != gfc_current_ns)
1657 gfc_symtree *st;
1658 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.derived->name);
1659 if (!(current_ts.derived->attr.imported
1660 && st != NULL
1661 && st->n.sym == current_ts.derived)
1662 && !gfc_current_ns->has_import_set)
1664 gfc_error ("the type of '%s' at %C has not been declared within the "
1665 "interface", name);
1666 m = MATCH_ERROR;
1667 goto cleanup;
1671 /* In functions that have a RESULT variable defined, the function
1672 name always refers to function calls. Therefore, the name is
1673 not allowed to appear in specification statements. */
1674 if (gfc_current_state () == COMP_FUNCTION
1675 && gfc_current_block () != NULL
1676 && gfc_current_block ()->result != NULL
1677 && gfc_current_block ()->result != gfc_current_block ()
1678 && strcmp (gfc_current_block ()->name, name) == 0)
1680 gfc_error ("Function name '%s' not allowed at %C", name);
1681 m = MATCH_ERROR;
1682 goto cleanup;
1685 /* We allow old-style initializations of the form
1686 integer i /2/, j(4) /3*3, 1/
1687 (if no colon has been seen). These are different from data
1688 statements in that initializers are only allowed to apply to the
1689 variable immediately preceding, i.e.
1690 integer i, j /1, 2/
1691 is not allowed. Therefore we have to do some work manually, that
1692 could otherwise be left to the matchers for DATA statements. */
1694 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1696 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1697 "initialization at %C") == FAILURE)
1698 return MATCH_ERROR;
1700 return match_old_style_init (name);
1703 /* The double colon must be present in order to have initializers.
1704 Otherwise the statement is ambiguous with an assignment statement. */
1705 if (colon_seen)
1707 if (gfc_match (" =>") == MATCH_YES)
1709 if (!current_attr.pointer)
1711 gfc_error ("Initialization at %C isn't for a pointer variable");
1712 m = MATCH_ERROR;
1713 goto cleanup;
1716 m = gfc_match_null (&initializer);
1717 if (m == MATCH_NO)
1719 gfc_error ("Pointer initialization requires a NULL() at %C");
1720 m = MATCH_ERROR;
1723 if (gfc_pure (NULL))
1725 gfc_error ("Initialization of pointer at %C is not allowed in "
1726 "a PURE procedure");
1727 m = MATCH_ERROR;
1730 if (m != MATCH_YES)
1731 goto cleanup;
1734 else if (gfc_match_char ('=') == MATCH_YES)
1736 if (current_attr.pointer)
1738 gfc_error ("Pointer initialization at %C requires '=>', "
1739 "not '='");
1740 m = MATCH_ERROR;
1741 goto cleanup;
1744 m = gfc_match_init_expr (&initializer);
1745 if (m == MATCH_NO)
1747 gfc_error ("Expected an initialization expression at %C");
1748 m = MATCH_ERROR;
1751 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1753 gfc_error ("Initialization of variable at %C is not allowed in "
1754 "a PURE procedure");
1755 m = MATCH_ERROR;
1758 if (m != MATCH_YES)
1759 goto cleanup;
1763 if (initializer != NULL && current_attr.allocatable
1764 && gfc_current_state () == COMP_DERIVED)
1766 gfc_error ("Initialization of allocatable component at %C is not "
1767 "allowed");
1768 m = MATCH_ERROR;
1769 goto cleanup;
1772 /* Add the initializer. Note that it is fine if initializer is
1773 NULL here, because we sometimes also need to check if a
1774 declaration *must* have an initialization expression. */
1775 if (gfc_current_state () != COMP_DERIVED)
1776 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1777 else
1779 if (current_ts.type == BT_DERIVED
1780 && !current_attr.pointer && !initializer)
1781 initializer = gfc_default_initializer (&current_ts);
1782 t = build_struct (name, cl, &initializer, &as);
1785 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1787 cleanup:
1788 /* Free stuff up and return. */
1789 gfc_free_expr (initializer);
1790 gfc_free_array_spec (as);
1792 return m;
1796 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1797 This assumes that the byte size is equal to the kind number for
1798 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1800 match
1801 gfc_match_old_kind_spec (gfc_typespec *ts)
1803 match m;
1804 int original_kind;
1806 if (gfc_match_char ('*') != MATCH_YES)
1807 return MATCH_NO;
1809 m = gfc_match_small_literal_int (&ts->kind, NULL);
1810 if (m != MATCH_YES)
1811 return MATCH_ERROR;
1813 original_kind = ts->kind;
1815 /* Massage the kind numbers for complex types. */
1816 if (ts->type == BT_COMPLEX)
1818 if (ts->kind % 2)
1820 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1821 gfc_basic_typename (ts->type), original_kind);
1822 return MATCH_ERROR;
1824 ts->kind /= 2;
1827 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1829 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1830 gfc_basic_typename (ts->type), original_kind);
1831 return MATCH_ERROR;
1834 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1835 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1836 return MATCH_ERROR;
1838 return MATCH_YES;
1842 /* Match a kind specification. Since kinds are generally optional, we
1843 usually return MATCH_NO if something goes wrong. If a "kind="
1844 string is found, then we know we have an error. */
1846 match
1847 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
1849 locus where, loc;
1850 gfc_expr *e;
1851 match m, n;
1852 char c;
1853 const char *msg;
1855 m = MATCH_NO;
1856 n = MATCH_YES;
1857 e = NULL;
1859 where = loc = gfc_current_locus;
1861 if (kind_expr_only)
1862 goto kind_expr;
1864 if (gfc_match_char ('(') == MATCH_NO)
1865 return MATCH_NO;
1867 /* Also gobbles optional text. */
1868 if (gfc_match (" kind = ") == MATCH_YES)
1869 m = MATCH_ERROR;
1871 loc = gfc_current_locus;
1873 kind_expr:
1874 n = gfc_match_init_expr (&e);
1876 if (n != MATCH_YES)
1878 if (gfc_matching_function)
1880 /* The function kind expression might include use associated or
1881 imported parameters and try again after the specification
1882 expressions..... */
1883 if (gfc_match_char (')') != MATCH_YES)
1885 gfc_error ("Missing right parenthesis at %C");
1886 m = MATCH_ERROR;
1887 goto no_match;
1890 gfc_free_expr (e);
1891 gfc_undo_symbols ();
1892 return MATCH_YES;
1894 else
1896 /* ....or else, the match is real. */
1897 if (n == MATCH_NO)
1898 gfc_error ("Expected initialization expression at %C");
1899 if (n != MATCH_YES)
1900 return MATCH_ERROR;
1904 if (e->rank != 0)
1906 gfc_error ("Expected scalar initialization expression at %C");
1907 m = MATCH_ERROR;
1908 goto no_match;
1911 msg = gfc_extract_int (e, &ts->kind);
1913 if (msg != NULL)
1915 gfc_error (msg);
1916 m = MATCH_ERROR;
1917 goto no_match;
1920 /* Before throwing away the expression, let's see if we had a
1921 C interoperable kind (and store the fact). */
1922 if (e->ts.is_c_interop == 1)
1924 /* Mark this as c interoperable if being declared with one
1925 of the named constants from iso_c_binding. */
1926 ts->is_c_interop = e->ts.is_iso_c;
1927 ts->f90_type = e->ts.f90_type;
1930 gfc_free_expr (e);
1931 e = NULL;
1933 /* Ignore errors to this point, if we've gotten here. This means
1934 we ignore the m=MATCH_ERROR from above. */
1935 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1937 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1938 gfc_basic_typename (ts->type));
1939 gfc_current_locus = where;
1940 return MATCH_ERROR;
1943 gfc_gobble_whitespace ();
1944 if ((c = gfc_next_ascii_char ()) != ')'
1945 && (ts->type != BT_CHARACTER || c != ','))
1947 if (ts->type == BT_CHARACTER)
1948 gfc_error ("Missing right parenthesis or comma at %C");
1949 else
1950 gfc_error ("Missing right parenthesis at %C");
1951 m = MATCH_ERROR;
1953 else
1954 /* All tests passed. */
1955 m = MATCH_YES;
1957 if(m == MATCH_ERROR)
1958 gfc_current_locus = where;
1960 /* Return what we know from the test(s). */
1961 return m;
1963 no_match:
1964 gfc_free_expr (e);
1965 gfc_current_locus = where;
1966 return m;
1970 static match
1971 match_char_kind (int * kind, int * is_iso_c)
1973 locus where;
1974 gfc_expr *e;
1975 match m, n;
1976 const char *msg;
1978 m = MATCH_NO;
1979 e = NULL;
1980 where = gfc_current_locus;
1982 n = gfc_match_init_expr (&e);
1984 if (n != MATCH_YES && gfc_matching_function)
1986 /* The expression might include use-associated or imported
1987 parameters and try again after the specification
1988 expressions. */
1989 gfc_free_expr (e);
1990 gfc_undo_symbols ();
1991 return MATCH_YES;
1994 if (n == MATCH_NO)
1995 gfc_error ("Expected initialization expression at %C");
1996 if (n != MATCH_YES)
1997 return MATCH_ERROR;
1999 if (e->rank != 0)
2001 gfc_error ("Expected scalar initialization expression at %C");
2002 m = MATCH_ERROR;
2003 goto no_match;
2006 msg = gfc_extract_int (e, kind);
2007 *is_iso_c = e->ts.is_iso_c;
2008 if (msg != NULL)
2010 gfc_error (msg);
2011 m = MATCH_ERROR;
2012 goto no_match;
2015 gfc_free_expr (e);
2017 /* Ignore errors to this point, if we've gotten here. This means
2018 we ignore the m=MATCH_ERROR from above. */
2019 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2021 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2022 m = MATCH_ERROR;
2024 else
2025 /* All tests passed. */
2026 m = MATCH_YES;
2028 if (m == MATCH_ERROR)
2029 gfc_current_locus = where;
2031 /* Return what we know from the test(s). */
2032 return m;
2034 no_match:
2035 gfc_free_expr (e);
2036 gfc_current_locus = where;
2037 return m;
2040 /* Match the various kind/length specifications in a CHARACTER
2041 declaration. We don't return MATCH_NO. */
2043 static match
2044 match_char_spec (gfc_typespec *ts)
2046 int kind, seen_length, is_iso_c;
2047 gfc_charlen *cl;
2048 gfc_expr *len;
2049 match m;
2051 len = NULL;
2052 seen_length = 0;
2053 kind = 0;
2054 is_iso_c = 0;
2056 /* Try the old-style specification first. */
2057 old_char_selector = 0;
2059 m = match_char_length (&len);
2060 if (m != MATCH_NO)
2062 if (m == MATCH_YES)
2063 old_char_selector = 1;
2064 seen_length = 1;
2065 goto done;
2068 m = gfc_match_char ('(');
2069 if (m != MATCH_YES)
2071 m = MATCH_YES; /* Character without length is a single char. */
2072 goto done;
2075 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2076 if (gfc_match (" kind =") == MATCH_YES)
2078 m = match_char_kind (&kind, &is_iso_c);
2080 if (m == MATCH_ERROR)
2081 goto done;
2082 if (m == MATCH_NO)
2083 goto syntax;
2085 if (gfc_match (" , len =") == MATCH_NO)
2086 goto rparen;
2088 m = char_len_param_value (&len);
2089 if (m == MATCH_NO)
2090 goto syntax;
2091 if (m == MATCH_ERROR)
2092 goto done;
2093 seen_length = 1;
2095 goto rparen;
2098 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2099 if (gfc_match (" len =") == MATCH_YES)
2101 m = char_len_param_value (&len);
2102 if (m == MATCH_NO)
2103 goto syntax;
2104 if (m == MATCH_ERROR)
2105 goto done;
2106 seen_length = 1;
2108 if (gfc_match_char (')') == MATCH_YES)
2109 goto done;
2111 if (gfc_match (" , kind =") != MATCH_YES)
2112 goto syntax;
2114 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2115 goto done;
2117 goto rparen;
2120 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2121 m = char_len_param_value (&len);
2122 if (m == MATCH_NO)
2123 goto syntax;
2124 if (m == MATCH_ERROR)
2125 goto done;
2126 seen_length = 1;
2128 m = gfc_match_char (')');
2129 if (m == MATCH_YES)
2130 goto done;
2132 if (gfc_match_char (',') != MATCH_YES)
2133 goto syntax;
2135 gfc_match (" kind ="); /* Gobble optional text. */
2137 m = match_char_kind (&kind, &is_iso_c);
2138 if (m == MATCH_ERROR)
2139 goto done;
2140 if (m == MATCH_NO)
2141 goto syntax;
2143 rparen:
2144 /* Require a right-paren at this point. */
2145 m = gfc_match_char (')');
2146 if (m == MATCH_YES)
2147 goto done;
2149 syntax:
2150 gfc_error ("Syntax error in CHARACTER declaration at %C");
2151 m = MATCH_ERROR;
2152 gfc_free_expr (len);
2153 return m;
2155 done:
2156 /* Deal with character functions after USE and IMPORT statements. */
2157 if (gfc_matching_function)
2159 gfc_free_expr (len);
2160 gfc_undo_symbols ();
2161 return MATCH_YES;
2164 if (m != MATCH_YES)
2166 gfc_free_expr (len);
2167 return m;
2170 /* Do some final massaging of the length values. */
2171 cl = gfc_get_charlen ();
2172 cl->next = gfc_current_ns->cl_list;
2173 gfc_current_ns->cl_list = cl;
2175 if (seen_length == 0)
2176 cl->length = gfc_int_expr (1);
2177 else
2178 cl->length = len;
2180 ts->cl = cl;
2181 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2183 /* We have to know if it was a c interoperable kind so we can
2184 do accurate type checking of bind(c) procs, etc. */
2185 if (kind != 0)
2186 /* Mark this as c interoperable if being declared with one
2187 of the named constants from iso_c_binding. */
2188 ts->is_c_interop = is_iso_c;
2189 else if (len != NULL)
2190 /* Here, we might have parsed something such as: character(c_char)
2191 In this case, the parsing code above grabs the c_char when
2192 looking for the length (line 1690, roughly). it's the last
2193 testcase for parsing the kind params of a character variable.
2194 However, it's not actually the length. this seems like it
2195 could be an error.
2196 To see if the user used a C interop kind, test the expr
2197 of the so called length, and see if it's C interoperable. */
2198 ts->is_c_interop = len->ts.is_iso_c;
2200 return MATCH_YES;
2204 /* Matches a type specification. If successful, sets the ts structure
2205 to the matched specification. This is necessary for FUNCTION and
2206 IMPLICIT statements.
2208 If implicit_flag is nonzero, then we don't check for the optional
2209 kind specification. Not doing so is needed for matching an IMPLICIT
2210 statement correctly. */
2212 match
2213 gfc_match_type_spec (gfc_typespec *ts, int implicit_flag)
2215 char name[GFC_MAX_SYMBOL_LEN + 1];
2216 gfc_symbol *sym;
2217 match m;
2218 char c;
2219 bool seen_deferred_kind;
2221 /* A belt and braces check that the typespec is correctly being treated
2222 as a deferred characteristic association. */
2223 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2224 && (gfc_current_block ()->result->ts.kind == -1)
2225 && (ts->kind == -1);
2226 gfc_clear_ts (ts);
2227 if (seen_deferred_kind)
2228 ts->kind = -1;
2230 /* Clear the current binding label, in case one is given. */
2231 curr_binding_label[0] = '\0';
2233 if (gfc_match (" byte") == MATCH_YES)
2235 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
2236 == FAILURE)
2237 return MATCH_ERROR;
2239 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2241 gfc_error ("BYTE type used at %C "
2242 "is not available on the target machine");
2243 return MATCH_ERROR;
2246 ts->type = BT_INTEGER;
2247 ts->kind = 1;
2248 return MATCH_YES;
2251 if (gfc_match (" integer") == MATCH_YES)
2253 ts->type = BT_INTEGER;
2254 ts->kind = gfc_default_integer_kind;
2255 goto get_kind;
2258 if (gfc_match (" character") == MATCH_YES)
2260 ts->type = BT_CHARACTER;
2261 if (implicit_flag == 0)
2262 return match_char_spec (ts);
2263 else
2264 return MATCH_YES;
2267 if (gfc_match (" real") == MATCH_YES)
2269 ts->type = BT_REAL;
2270 ts->kind = gfc_default_real_kind;
2271 goto get_kind;
2274 if (gfc_match (" double precision") == MATCH_YES)
2276 ts->type = BT_REAL;
2277 ts->kind = gfc_default_double_kind;
2278 return MATCH_YES;
2281 if (gfc_match (" complex") == MATCH_YES)
2283 ts->type = BT_COMPLEX;
2284 ts->kind = gfc_default_complex_kind;
2285 goto get_kind;
2288 if (gfc_match (" double complex") == MATCH_YES)
2290 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2291 "conform to the Fortran 95 standard") == FAILURE)
2292 return MATCH_ERROR;
2294 ts->type = BT_COMPLEX;
2295 ts->kind = gfc_default_double_kind;
2296 return MATCH_YES;
2299 if (gfc_match (" logical") == MATCH_YES)
2301 ts->type = BT_LOGICAL;
2302 ts->kind = gfc_default_logical_kind;
2303 goto get_kind;
2306 m = gfc_match (" type ( %n )", name);
2307 if (m != MATCH_YES)
2308 return m;
2310 ts->type = BT_DERIVED;
2312 /* Defer association of the derived type until the end of the
2313 specification block. However, if the derived type can be
2314 found, add it to the typespec. */
2315 if (gfc_matching_function)
2317 ts->derived = NULL;
2318 if (gfc_current_state () != COMP_INTERFACE
2319 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2320 ts->derived = sym;
2321 return MATCH_YES;
2324 /* Search for the name but allow the components to be defined later. If
2325 type = -1, this typespec has been seen in a function declaration but
2326 the type could not be accessed at that point. */
2327 sym = NULL;
2328 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2330 gfc_error ("Type name '%s' at %C is ambiguous", name);
2331 return MATCH_ERROR;
2333 else if (ts->kind == -1)
2335 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2336 || gfc_current_ns->has_import_set;
2337 if (gfc_find_symbol (name, NULL, iface, &sym))
2339 gfc_error ("Type name '%s' at %C is ambiguous", name);
2340 return MATCH_ERROR;
2343 ts->kind = 0;
2344 if (sym == NULL)
2345 return MATCH_NO;
2348 if (sym->attr.flavor != FL_DERIVED
2349 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2350 return MATCH_ERROR;
2352 gfc_set_sym_referenced (sym);
2353 ts->derived = sym;
2355 return MATCH_YES;
2357 get_kind:
2358 /* For all types except double, derived and character, look for an
2359 optional kind specifier. MATCH_NO is actually OK at this point. */
2360 if (implicit_flag == 1)
2361 return MATCH_YES;
2363 if (gfc_current_form == FORM_FREE)
2365 c = gfc_peek_ascii_char();
2366 if (!gfc_is_whitespace(c) && c != '*' && c != '('
2367 && c != ':' && c != ',')
2368 return MATCH_NO;
2371 m = gfc_match_kind_spec (ts, false);
2372 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2373 m = gfc_match_old_kind_spec (ts);
2375 /* Defer association of the KIND expression of function results
2376 until after USE and IMPORT statements. */
2377 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2378 || gfc_matching_function)
2379 return MATCH_YES;
2381 if (m == MATCH_NO)
2382 m = MATCH_YES; /* No kind specifier found. */
2384 return m;
2388 /* Match an IMPLICIT NONE statement. Actually, this statement is
2389 already matched in parse.c, or we would not end up here in the
2390 first place. So the only thing we need to check, is if there is
2391 trailing garbage. If not, the match is successful. */
2393 match
2394 gfc_match_implicit_none (void)
2396 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2400 /* Match the letter range(s) of an IMPLICIT statement. */
2402 static match
2403 match_implicit_range (void)
2405 char c, c1, c2;
2406 int inner;
2407 locus cur_loc;
2409 cur_loc = gfc_current_locus;
2411 gfc_gobble_whitespace ();
2412 c = gfc_next_ascii_char ();
2413 if (c != '(')
2415 gfc_error ("Missing character range in IMPLICIT at %C");
2416 goto bad;
2419 inner = 1;
2420 while (inner)
2422 gfc_gobble_whitespace ();
2423 c1 = gfc_next_ascii_char ();
2424 if (!ISALPHA (c1))
2425 goto bad;
2427 gfc_gobble_whitespace ();
2428 c = gfc_next_ascii_char ();
2430 switch (c)
2432 case ')':
2433 inner = 0; /* Fall through. */
2435 case ',':
2436 c2 = c1;
2437 break;
2439 case '-':
2440 gfc_gobble_whitespace ();
2441 c2 = gfc_next_ascii_char ();
2442 if (!ISALPHA (c2))
2443 goto bad;
2445 gfc_gobble_whitespace ();
2446 c = gfc_next_ascii_char ();
2448 if ((c != ',') && (c != ')'))
2449 goto bad;
2450 if (c == ')')
2451 inner = 0;
2453 break;
2455 default:
2456 goto bad;
2459 if (c1 > c2)
2461 gfc_error ("Letters must be in alphabetic order in "
2462 "IMPLICIT statement at %C");
2463 goto bad;
2466 /* See if we can add the newly matched range to the pending
2467 implicits from this IMPLICIT statement. We do not check for
2468 conflicts with whatever earlier IMPLICIT statements may have
2469 set. This is done when we've successfully finished matching
2470 the current one. */
2471 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2472 goto bad;
2475 return MATCH_YES;
2477 bad:
2478 gfc_syntax_error (ST_IMPLICIT);
2480 gfc_current_locus = cur_loc;
2481 return MATCH_ERROR;
2485 /* Match an IMPLICIT statement, storing the types for
2486 gfc_set_implicit() if the statement is accepted by the parser.
2487 There is a strange looking, but legal syntactic construction
2488 possible. It looks like:
2490 IMPLICIT INTEGER (a-b) (c-d)
2492 This is legal if "a-b" is a constant expression that happens to
2493 equal one of the legal kinds for integers. The real problem
2494 happens with an implicit specification that looks like:
2496 IMPLICIT INTEGER (a-b)
2498 In this case, a typespec matcher that is "greedy" (as most of the
2499 matchers are) gobbles the character range as a kindspec, leaving
2500 nothing left. We therefore have to go a bit more slowly in the
2501 matching process by inhibiting the kindspec checking during
2502 typespec matching and checking for a kind later. */
2504 match
2505 gfc_match_implicit (void)
2507 gfc_typespec ts;
2508 locus cur_loc;
2509 char c;
2510 match m;
2512 gfc_clear_ts (&ts);
2514 /* We don't allow empty implicit statements. */
2515 if (gfc_match_eos () == MATCH_YES)
2517 gfc_error ("Empty IMPLICIT statement at %C");
2518 return MATCH_ERROR;
2523 /* First cleanup. */
2524 gfc_clear_new_implicit ();
2526 /* A basic type is mandatory here. */
2527 m = gfc_match_type_spec (&ts, 1);
2528 if (m == MATCH_ERROR)
2529 goto error;
2530 if (m == MATCH_NO)
2531 goto syntax;
2533 cur_loc = gfc_current_locus;
2534 m = match_implicit_range ();
2536 if (m == MATCH_YES)
2538 /* We may have <TYPE> (<RANGE>). */
2539 gfc_gobble_whitespace ();
2540 c = gfc_next_ascii_char ();
2541 if ((c == '\n') || (c == ','))
2543 /* Check for CHARACTER with no length parameter. */
2544 if (ts.type == BT_CHARACTER && !ts.cl)
2546 ts.kind = gfc_default_character_kind;
2547 ts.cl = gfc_get_charlen ();
2548 ts.cl->next = gfc_current_ns->cl_list;
2549 gfc_current_ns->cl_list = ts.cl;
2550 ts.cl->length = gfc_int_expr (1);
2553 /* Record the Successful match. */
2554 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2555 return MATCH_ERROR;
2556 continue;
2559 gfc_current_locus = cur_loc;
2562 /* Discard the (incorrectly) matched range. */
2563 gfc_clear_new_implicit ();
2565 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2566 if (ts.type == BT_CHARACTER)
2567 m = match_char_spec (&ts);
2568 else
2570 m = gfc_match_kind_spec (&ts, false);
2571 if (m == MATCH_NO)
2573 m = gfc_match_old_kind_spec (&ts);
2574 if (m == MATCH_ERROR)
2575 goto error;
2576 if (m == MATCH_NO)
2577 goto syntax;
2580 if (m == MATCH_ERROR)
2581 goto error;
2583 m = match_implicit_range ();
2584 if (m == MATCH_ERROR)
2585 goto error;
2586 if (m == MATCH_NO)
2587 goto syntax;
2589 gfc_gobble_whitespace ();
2590 c = gfc_next_ascii_char ();
2591 if ((c != '\n') && (c != ','))
2592 goto syntax;
2594 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2595 return MATCH_ERROR;
2597 while (c == ',');
2599 return MATCH_YES;
2601 syntax:
2602 gfc_syntax_error (ST_IMPLICIT);
2604 error:
2605 return MATCH_ERROR;
2609 match
2610 gfc_match_import (void)
2612 char name[GFC_MAX_SYMBOL_LEN + 1];
2613 match m;
2614 gfc_symbol *sym;
2615 gfc_symtree *st;
2617 if (gfc_current_ns->proc_name == NULL
2618 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2620 gfc_error ("IMPORT statement at %C only permitted in "
2621 "an INTERFACE body");
2622 return MATCH_ERROR;
2625 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2626 == FAILURE)
2627 return MATCH_ERROR;
2629 if (gfc_match_eos () == MATCH_YES)
2631 /* All host variables should be imported. */
2632 gfc_current_ns->has_import_set = 1;
2633 return MATCH_YES;
2636 if (gfc_match (" ::") == MATCH_YES)
2638 if (gfc_match_eos () == MATCH_YES)
2640 gfc_error ("Expecting list of named entities at %C");
2641 return MATCH_ERROR;
2645 for(;;)
2647 m = gfc_match (" %n", name);
2648 switch (m)
2650 case MATCH_YES:
2651 if (gfc_current_ns->parent != NULL
2652 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2654 gfc_error ("Type name '%s' at %C is ambiguous", name);
2655 return MATCH_ERROR;
2657 else if (gfc_current_ns->proc_name->ns->parent != NULL
2658 && gfc_find_symbol (name,
2659 gfc_current_ns->proc_name->ns->parent,
2660 1, &sym))
2662 gfc_error ("Type name '%s' at %C is ambiguous", name);
2663 return MATCH_ERROR;
2666 if (sym == NULL)
2668 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2669 "at %C - does not exist.", name);
2670 return MATCH_ERROR;
2673 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2675 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2676 "at %C.", name);
2677 goto next_item;
2680 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2681 st->n.sym = sym;
2682 sym->refs++;
2683 sym->attr.imported = 1;
2685 goto next_item;
2687 case MATCH_NO:
2688 break;
2690 case MATCH_ERROR:
2691 return MATCH_ERROR;
2694 next_item:
2695 if (gfc_match_eos () == MATCH_YES)
2696 break;
2697 if (gfc_match_char (',') != MATCH_YES)
2698 goto syntax;
2701 return MATCH_YES;
2703 syntax:
2704 gfc_error ("Syntax error in IMPORT statement at %C");
2705 return MATCH_ERROR;
2709 /* A minimal implementation of gfc_match without whitespace, escape
2710 characters or variable arguments. Returns true if the next
2711 characters match the TARGET template exactly. */
2713 static bool
2714 match_string_p (const char *target)
2716 const char *p;
2718 for (p = target; *p; p++)
2719 if ((char) gfc_next_ascii_char () != *p)
2720 return false;
2721 return true;
2724 /* Matches an attribute specification including array specs. If
2725 successful, leaves the variables current_attr and current_as
2726 holding the specification. Also sets the colon_seen variable for
2727 later use by matchers associated with initializations.
2729 This subroutine is a little tricky in the sense that we don't know
2730 if we really have an attr-spec until we hit the double colon.
2731 Until that time, we can only return MATCH_NO. This forces us to
2732 check for duplicate specification at this level. */
2734 static match
2735 match_attr_spec (void)
2737 /* Modifiers that can exist in a type statement. */
2738 typedef enum
2739 { GFC_DECL_BEGIN = 0,
2740 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2741 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2742 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2743 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2744 DECL_IS_BIND_C, DECL_NONE,
2745 GFC_DECL_END /* Sentinel */
2747 decl_types;
2749 /* GFC_DECL_END is the sentinel, index starts at 0. */
2750 #define NUM_DECL GFC_DECL_END
2752 locus start, seen_at[NUM_DECL];
2753 int seen[NUM_DECL];
2754 decl_types d;
2755 const char *attr;
2756 match m;
2757 try t;
2759 gfc_clear_attr (&current_attr);
2760 start = gfc_current_locus;
2762 current_as = NULL;
2763 colon_seen = 0;
2765 /* See if we get all of the keywords up to the final double colon. */
2766 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2767 seen[d] = 0;
2769 for (;;)
2771 char ch;
2773 d = DECL_NONE;
2774 gfc_gobble_whitespace ();
2776 ch = gfc_next_ascii_char ();
2777 if (ch == ':')
2779 /* This is the successful exit condition for the loop. */
2780 if (gfc_next_ascii_char () == ':')
2781 break;
2783 else if (ch == ',')
2785 gfc_gobble_whitespace ();
2786 switch (gfc_peek_ascii_char ())
2788 case 'a':
2789 if (match_string_p ("allocatable"))
2790 d = DECL_ALLOCATABLE;
2791 break;
2793 case 'b':
2794 /* Try and match the bind(c). */
2795 m = gfc_match_bind_c (NULL, true);
2796 if (m == MATCH_YES)
2797 d = DECL_IS_BIND_C;
2798 else if (m == MATCH_ERROR)
2799 goto cleanup;
2800 break;
2802 case 'd':
2803 if (match_string_p ("dimension"))
2804 d = DECL_DIMENSION;
2805 break;
2807 case 'e':
2808 if (match_string_p ("external"))
2809 d = DECL_EXTERNAL;
2810 break;
2812 case 'i':
2813 if (match_string_p ("int"))
2815 ch = gfc_next_ascii_char ();
2816 if (ch == 'e')
2818 if (match_string_p ("nt"))
2820 /* Matched "intent". */
2821 /* TODO: Call match_intent_spec from here. */
2822 if (gfc_match (" ( in out )") == MATCH_YES)
2823 d = DECL_INOUT;
2824 else if (gfc_match (" ( in )") == MATCH_YES)
2825 d = DECL_IN;
2826 else if (gfc_match (" ( out )") == MATCH_YES)
2827 d = DECL_OUT;
2830 else if (ch == 'r')
2832 if (match_string_p ("insic"))
2834 /* Matched "intrinsic". */
2835 d = DECL_INTRINSIC;
2839 break;
2841 case 'o':
2842 if (match_string_p ("optional"))
2843 d = DECL_OPTIONAL;
2844 break;
2846 case 'p':
2847 gfc_next_ascii_char ();
2848 switch (gfc_next_ascii_char ())
2850 case 'a':
2851 if (match_string_p ("rameter"))
2853 /* Matched "parameter". */
2854 d = DECL_PARAMETER;
2856 break;
2858 case 'o':
2859 if (match_string_p ("inter"))
2861 /* Matched "pointer". */
2862 d = DECL_POINTER;
2864 break;
2866 case 'r':
2867 ch = gfc_next_ascii_char ();
2868 if (ch == 'i')
2870 if (match_string_p ("vate"))
2872 /* Matched "private". */
2873 d = DECL_PRIVATE;
2876 else if (ch == 'o')
2878 if (match_string_p ("tected"))
2880 /* Matched "protected". */
2881 d = DECL_PROTECTED;
2884 break;
2886 case 'u':
2887 if (match_string_p ("blic"))
2889 /* Matched "public". */
2890 d = DECL_PUBLIC;
2892 break;
2894 break;
2896 case 's':
2897 if (match_string_p ("save"))
2898 d = DECL_SAVE;
2899 break;
2901 case 't':
2902 if (match_string_p ("target"))
2903 d = DECL_TARGET;
2904 break;
2906 case 'v':
2907 gfc_next_ascii_char ();
2908 ch = gfc_next_ascii_char ();
2909 if (ch == 'a')
2911 if (match_string_p ("lue"))
2913 /* Matched "value". */
2914 d = DECL_VALUE;
2917 else if (ch == 'o')
2919 if (match_string_p ("latile"))
2921 /* Matched "volatile". */
2922 d = DECL_VOLATILE;
2925 break;
2929 /* No double colon and no recognizable decl_type, so assume that
2930 we've been looking at something else the whole time. */
2931 if (d == DECL_NONE)
2933 m = MATCH_NO;
2934 goto cleanup;
2937 /* Check to make sure any parens are paired up correctly. */
2938 if (gfc_match_parens () == MATCH_ERROR)
2940 m = MATCH_ERROR;
2941 goto cleanup;
2944 seen[d]++;
2945 seen_at[d] = gfc_current_locus;
2947 if (d == DECL_DIMENSION)
2949 m = gfc_match_array_spec (&current_as);
2951 if (m == MATCH_NO)
2953 gfc_error ("Missing dimension specification at %C");
2954 m = MATCH_ERROR;
2957 if (m == MATCH_ERROR)
2958 goto cleanup;
2962 /* Since we've seen a double colon, we have to be looking at an
2963 attr-spec. This means that we can now issue errors. */
2964 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2965 if (seen[d] > 1)
2967 switch (d)
2969 case DECL_ALLOCATABLE:
2970 attr = "ALLOCATABLE";
2971 break;
2972 case DECL_DIMENSION:
2973 attr = "DIMENSION";
2974 break;
2975 case DECL_EXTERNAL:
2976 attr = "EXTERNAL";
2977 break;
2978 case DECL_IN:
2979 attr = "INTENT (IN)";
2980 break;
2981 case DECL_OUT:
2982 attr = "INTENT (OUT)";
2983 break;
2984 case DECL_INOUT:
2985 attr = "INTENT (IN OUT)";
2986 break;
2987 case DECL_INTRINSIC:
2988 attr = "INTRINSIC";
2989 break;
2990 case DECL_OPTIONAL:
2991 attr = "OPTIONAL";
2992 break;
2993 case DECL_PARAMETER:
2994 attr = "PARAMETER";
2995 break;
2996 case DECL_POINTER:
2997 attr = "POINTER";
2998 break;
2999 case DECL_PROTECTED:
3000 attr = "PROTECTED";
3001 break;
3002 case DECL_PRIVATE:
3003 attr = "PRIVATE";
3004 break;
3005 case DECL_PUBLIC:
3006 attr = "PUBLIC";
3007 break;
3008 case DECL_SAVE:
3009 attr = "SAVE";
3010 break;
3011 case DECL_TARGET:
3012 attr = "TARGET";
3013 break;
3014 case DECL_IS_BIND_C:
3015 attr = "IS_BIND_C";
3016 break;
3017 case DECL_VALUE:
3018 attr = "VALUE";
3019 break;
3020 case DECL_VOLATILE:
3021 attr = "VOLATILE";
3022 break;
3023 default:
3024 attr = NULL; /* This shouldn't happen. */
3027 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3028 m = MATCH_ERROR;
3029 goto cleanup;
3032 /* Now that we've dealt with duplicate attributes, add the attributes
3033 to the current attribute. */
3034 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3036 if (seen[d] == 0)
3037 continue;
3039 if (gfc_current_state () == COMP_DERIVED
3040 && d != DECL_DIMENSION && d != DECL_POINTER
3041 && d != DECL_PRIVATE && d != DECL_PUBLIC
3042 && d != DECL_NONE)
3044 if (d == DECL_ALLOCATABLE)
3046 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3047 "attribute at %C in a TYPE definition")
3048 == FAILURE)
3050 m = MATCH_ERROR;
3051 goto cleanup;
3054 else
3056 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3057 &seen_at[d]);
3058 m = MATCH_ERROR;
3059 goto cleanup;
3063 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3064 && gfc_current_state () != COMP_MODULE)
3066 if (d == DECL_PRIVATE)
3067 attr = "PRIVATE";
3068 else
3069 attr = "PUBLIC";
3070 if (gfc_current_state () == COMP_DERIVED
3071 && gfc_state_stack->previous
3072 && gfc_state_stack->previous->state == COMP_MODULE)
3074 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3075 "at %L in a TYPE definition", attr,
3076 &seen_at[d])
3077 == FAILURE)
3079 m = MATCH_ERROR;
3080 goto cleanup;
3083 else
3085 gfc_error ("%s attribute at %L is not allowed outside of the "
3086 "specification part of a module", attr, &seen_at[d]);
3087 m = MATCH_ERROR;
3088 goto cleanup;
3092 switch (d)
3094 case DECL_ALLOCATABLE:
3095 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3096 break;
3098 case DECL_DIMENSION:
3099 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3100 break;
3102 case DECL_EXTERNAL:
3103 t = gfc_add_external (&current_attr, &seen_at[d]);
3104 break;
3106 case DECL_IN:
3107 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3108 break;
3110 case DECL_OUT:
3111 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3112 break;
3114 case DECL_INOUT:
3115 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3116 break;
3118 case DECL_INTRINSIC:
3119 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3120 break;
3122 case DECL_OPTIONAL:
3123 t = gfc_add_optional (&current_attr, &seen_at[d]);
3124 break;
3126 case DECL_PARAMETER:
3127 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3128 break;
3130 case DECL_POINTER:
3131 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3132 break;
3134 case DECL_PROTECTED:
3135 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3137 gfc_error ("PROTECTED at %C only allowed in specification "
3138 "part of a module");
3139 t = FAILURE;
3140 break;
3143 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3144 "attribute at %C")
3145 == FAILURE)
3146 t = FAILURE;
3147 else
3148 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3149 break;
3151 case DECL_PRIVATE:
3152 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3153 &seen_at[d]);
3154 break;
3156 case DECL_PUBLIC:
3157 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3158 &seen_at[d]);
3159 break;
3161 case DECL_SAVE:
3162 t = gfc_add_save (&current_attr, NULL, &seen_at[d]);
3163 break;
3165 case DECL_TARGET:
3166 t = gfc_add_target (&current_attr, &seen_at[d]);
3167 break;
3169 case DECL_IS_BIND_C:
3170 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3171 break;
3173 case DECL_VALUE:
3174 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3175 "at %C")
3176 == FAILURE)
3177 t = FAILURE;
3178 else
3179 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3180 break;
3182 case DECL_VOLATILE:
3183 if (gfc_notify_std (GFC_STD_F2003,
3184 "Fortran 2003: VOLATILE attribute at %C")
3185 == FAILURE)
3186 t = FAILURE;
3187 else
3188 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3189 break;
3191 default:
3192 gfc_internal_error ("match_attr_spec(): Bad attribute");
3195 if (t == FAILURE)
3197 m = MATCH_ERROR;
3198 goto cleanup;
3202 colon_seen = 1;
3203 return MATCH_YES;
3205 cleanup:
3206 gfc_current_locus = start;
3207 gfc_free_array_spec (current_as);
3208 current_as = NULL;
3209 return m;
3213 /* Set the binding label, dest_label, either with the binding label
3214 stored in the given gfc_typespec, ts, or if none was provided, it
3215 will be the symbol name in all lower case, as required by the draft
3216 (J3/04-007, section 15.4.1). If a binding label was given and
3217 there is more than one argument (num_idents), it is an error. */
3220 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3222 if (num_idents > 1 && has_name_equals)
3224 gfc_error ("Multiple identifiers provided with "
3225 "single NAME= specifier at %C");
3226 return FAILURE;
3229 if (curr_binding_label[0] != '\0')
3231 /* Binding label given; store in temp holder til have sym. */
3232 strcpy (dest_label, curr_binding_label);
3234 else
3236 /* No binding label given, and the NAME= specifier did not exist,
3237 which means there was no NAME="". */
3238 if (sym_name != NULL && has_name_equals == 0)
3239 strcpy (dest_label, sym_name);
3242 return SUCCESS;
3246 /* Set the status of the given common block as being BIND(C) or not,
3247 depending on the given parameter, is_bind_c. */
3249 void
3250 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3252 com_block->is_bind_c = is_bind_c;
3253 return;
3257 /* Verify that the given gfc_typespec is for a C interoperable type. */
3260 verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
3262 try t;
3264 /* Make sure the kind used is appropriate for the type.
3265 The f90_type is unknown if an integer constant was
3266 used (e.g., real(4), bind(c) :: myFloat). */
3267 if (ts->f90_type != BT_UNKNOWN)
3269 t = gfc_validate_c_kind (ts);
3270 if (t != SUCCESS)
3272 /* Print an error, but continue parsing line. */
3273 gfc_error_now ("C kind parameter is for type %s but "
3274 "symbol '%s' at %L is of type %s",
3275 gfc_basic_typename (ts->f90_type),
3276 name, where,
3277 gfc_basic_typename (ts->type));
3281 /* Make sure the kind is C interoperable. This does not care about the
3282 possible error above. */
3283 if (ts->type == BT_DERIVED && ts->derived != NULL)
3284 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
3285 else if (ts->is_c_interop != 1)
3286 return FAILURE;
3288 return SUCCESS;
3292 /* Verify that the variables of a given common block, which has been
3293 defined with the attribute specifier bind(c), to be of a C
3294 interoperable type. Errors will be reported here, if
3295 encountered. */
3298 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3300 gfc_symbol *curr_sym = NULL;
3301 try retval = SUCCESS;
3303 curr_sym = com_block->head;
3305 /* Make sure we have at least one symbol. */
3306 if (curr_sym == NULL)
3307 return retval;
3309 /* Here we know we have a symbol, so we'll execute this loop
3310 at least once. */
3313 /* The second to last param, 1, says this is in a common block. */
3314 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3315 curr_sym = curr_sym->common_next;
3316 } while (curr_sym != NULL);
3318 return retval;
3322 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3323 an appropriate error message is reported. */
3326 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3327 int is_in_common, gfc_common_head *com_block)
3329 try retval = SUCCESS;
3331 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3333 tmp_sym = tmp_sym->result;
3334 /* Make sure it wasn't an implicitly typed result. */
3335 if (tmp_sym->attr.implicit_type)
3337 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3338 "%L may not be C interoperable", tmp_sym->name,
3339 &tmp_sym->declared_at);
3340 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3341 /* Mark it as C interoperable to prevent duplicate warnings. */
3342 tmp_sym->ts.is_c_interop = 1;
3343 tmp_sym->attr.is_c_interop = 1;
3347 /* Here, we know we have the bind(c) attribute, so if we have
3348 enough type info, then verify that it's a C interop kind.
3349 The info could be in the symbol already, or possibly still in
3350 the given ts (current_ts), so look in both. */
3351 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3353 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
3354 &(tmp_sym->declared_at)) != SUCCESS)
3356 /* See if we're dealing with a sym in a common block or not. */
3357 if (is_in_common == 1)
3359 gfc_warning ("Variable '%s' in common block '%s' at %L "
3360 "may not be a C interoperable "
3361 "kind though common block '%s' is BIND(C)",
3362 tmp_sym->name, com_block->name,
3363 &(tmp_sym->declared_at), com_block->name);
3365 else
3367 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3368 gfc_error ("Type declaration '%s' at %L is not C "
3369 "interoperable but it is BIND(C)",
3370 tmp_sym->name, &(tmp_sym->declared_at));
3371 else
3372 gfc_warning ("Variable '%s' at %L "
3373 "may not be a C interoperable "
3374 "kind but it is bind(c)",
3375 tmp_sym->name, &(tmp_sym->declared_at));
3379 /* Variables declared w/in a common block can't be bind(c)
3380 since there's no way for C to see these variables, so there's
3381 semantically no reason for the attribute. */
3382 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3384 gfc_error ("Variable '%s' in common block '%s' at "
3385 "%L cannot be declared with BIND(C) "
3386 "since it is not a global",
3387 tmp_sym->name, com_block->name,
3388 &(tmp_sym->declared_at));
3389 retval = FAILURE;
3392 /* Scalar variables that are bind(c) can not have the pointer
3393 or allocatable attributes. */
3394 if (tmp_sym->attr.is_bind_c == 1)
3396 if (tmp_sym->attr.pointer == 1)
3398 gfc_error ("Variable '%s' at %L cannot have both the "
3399 "POINTER and BIND(C) attributes",
3400 tmp_sym->name, &(tmp_sym->declared_at));
3401 retval = FAILURE;
3404 if (tmp_sym->attr.allocatable == 1)
3406 gfc_error ("Variable '%s' at %L cannot have both the "
3407 "ALLOCATABLE and BIND(C) attributes",
3408 tmp_sym->name, &(tmp_sym->declared_at));
3409 retval = FAILURE;
3412 /* If it is a BIND(C) function, make sure the return value is a
3413 scalar value. The previous tests in this function made sure
3414 the type is interoperable. */
3415 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
3416 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3417 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3419 /* BIND(C) functions can not return a character string. */
3420 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
3421 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3422 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3423 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3424 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3425 "be a character string", tmp_sym->name,
3426 &(tmp_sym->declared_at));
3430 /* See if the symbol has been marked as private. If it has, make sure
3431 there is no binding label and warn the user if there is one. */
3432 if (tmp_sym->attr.access == ACCESS_PRIVATE
3433 && tmp_sym->binding_label[0] != '\0')
3434 /* Use gfc_warning_now because we won't say that the symbol fails
3435 just because of this. */
3436 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3437 "given the binding label '%s'", tmp_sym->name,
3438 &(tmp_sym->declared_at), tmp_sym->binding_label);
3440 return retval;
3444 /* Set the appropriate fields for a symbol that's been declared as
3445 BIND(C) (the is_bind_c flag and the binding label), and verify that
3446 the type is C interoperable. Errors are reported by the functions
3447 used to set/test these fields. */
3450 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3452 try retval = SUCCESS;
3454 /* TODO: Do we need to make sure the vars aren't marked private? */
3456 /* Set the is_bind_c bit in symbol_attribute. */
3457 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3459 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3460 num_idents) != SUCCESS)
3461 return FAILURE;
3463 return retval;
3467 /* Set the fields marking the given common block as BIND(C), including
3468 a binding label, and report any errors encountered. */
3471 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3473 try retval = SUCCESS;
3475 /* destLabel, common name, typespec (which may have binding label). */
3476 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3477 != SUCCESS)
3478 return FAILURE;
3480 /* Set the given common block (com_block) to being bind(c) (1). */
3481 set_com_block_bind_c (com_block, 1);
3483 return retval;
3487 /* Retrieve the list of one or more identifiers that the given bind(c)
3488 attribute applies to. */
3491 get_bind_c_idents (void)
3493 char name[GFC_MAX_SYMBOL_LEN + 1];
3494 int num_idents = 0;
3495 gfc_symbol *tmp_sym = NULL;
3496 match found_id;
3497 gfc_common_head *com_block = NULL;
3499 if (gfc_match_name (name) == MATCH_YES)
3501 found_id = MATCH_YES;
3502 gfc_get_ha_symbol (name, &tmp_sym);
3504 else if (match_common_name (name) == MATCH_YES)
3506 found_id = MATCH_YES;
3507 com_block = gfc_get_common (name, 0);
3509 else
3511 gfc_error ("Need either entity or common block name for "
3512 "attribute specification statement at %C");
3513 return FAILURE;
3516 /* Save the current identifier and look for more. */
3519 /* Increment the number of identifiers found for this spec stmt. */
3520 num_idents++;
3522 /* Make sure we have a sym or com block, and verify that it can
3523 be bind(c). Set the appropriate field(s) and look for more
3524 identifiers. */
3525 if (tmp_sym != NULL || com_block != NULL)
3527 if (tmp_sym != NULL)
3529 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3530 != SUCCESS)
3531 return FAILURE;
3533 else
3535 if (set_verify_bind_c_com_block(com_block, num_idents)
3536 != SUCCESS)
3537 return FAILURE;
3540 /* Look to see if we have another identifier. */
3541 tmp_sym = NULL;
3542 if (gfc_match_eos () == MATCH_YES)
3543 found_id = MATCH_NO;
3544 else if (gfc_match_char (',') != MATCH_YES)
3545 found_id = MATCH_NO;
3546 else if (gfc_match_name (name) == MATCH_YES)
3548 found_id = MATCH_YES;
3549 gfc_get_ha_symbol (name, &tmp_sym);
3551 else if (match_common_name (name) == MATCH_YES)
3553 found_id = MATCH_YES;
3554 com_block = gfc_get_common (name, 0);
3556 else
3558 gfc_error ("Missing entity or common block name for "
3559 "attribute specification statement at %C");
3560 return FAILURE;
3563 else
3565 gfc_internal_error ("Missing symbol");
3567 } while (found_id == MATCH_YES);
3569 /* if we get here we were successful */
3570 return SUCCESS;
3574 /* Try and match a BIND(C) attribute specification statement. */
3576 match
3577 gfc_match_bind_c_stmt (void)
3579 match found_match = MATCH_NO;
3580 gfc_typespec *ts;
3582 ts = &current_ts;
3584 /* This may not be necessary. */
3585 gfc_clear_ts (ts);
3586 /* Clear the temporary binding label holder. */
3587 curr_binding_label[0] = '\0';
3589 /* Look for the bind(c). */
3590 found_match = gfc_match_bind_c (NULL, true);
3592 if (found_match == MATCH_YES)
3594 /* Look for the :: now, but it is not required. */
3595 gfc_match (" :: ");
3597 /* Get the identifier(s) that needs to be updated. This may need to
3598 change to hand the flag(s) for the attr specified so all identifiers
3599 found can have all appropriate parts updated (assuming that the same
3600 spec stmt can have multiple attrs, such as both bind(c) and
3601 allocatable...). */
3602 if (get_bind_c_idents () != SUCCESS)
3603 /* Error message should have printed already. */
3604 return MATCH_ERROR;
3607 return found_match;
3611 /* Match a data declaration statement. */
3613 match
3614 gfc_match_data_decl (void)
3616 gfc_symbol *sym;
3617 match m;
3618 int elem;
3620 num_idents_on_line = 0;
3622 m = gfc_match_type_spec (&current_ts, 0);
3623 if (m != MATCH_YES)
3624 return m;
3626 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3628 sym = gfc_use_derived (current_ts.derived);
3630 if (sym == NULL)
3632 m = MATCH_ERROR;
3633 goto cleanup;
3636 current_ts.derived = sym;
3639 m = match_attr_spec ();
3640 if (m == MATCH_ERROR)
3642 m = MATCH_NO;
3643 goto cleanup;
3646 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL
3647 && !current_ts.derived->attr.zero_comp)
3650 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3651 goto ok;
3653 gfc_find_symbol (current_ts.derived->name,
3654 current_ts.derived->ns->parent, 1, &sym);
3656 /* Any symbol that we find had better be a type definition
3657 which has its components defined. */
3658 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3659 && (current_ts.derived->components != NULL
3660 || current_ts.derived->attr.zero_comp))
3661 goto ok;
3663 /* Now we have an error, which we signal, and then fix up
3664 because the knock-on is plain and simple confusing. */
3665 gfc_error_now ("Derived type at %C has not been previously defined "
3666 "and so cannot appear in a derived type definition");
3667 current_attr.pointer = 1;
3668 goto ok;
3672 /* If we have an old-style character declaration, and no new-style
3673 attribute specifications, then there a comma is optional between
3674 the type specification and the variable list. */
3675 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3676 gfc_match_char (',');
3678 /* Give the types/attributes to symbols that follow. Give the element
3679 a number so that repeat character length expressions can be copied. */
3680 elem = 1;
3681 for (;;)
3683 num_idents_on_line++;
3684 m = variable_decl (elem++);
3685 if (m == MATCH_ERROR)
3686 goto cleanup;
3687 if (m == MATCH_NO)
3688 break;
3690 if (gfc_match_eos () == MATCH_YES)
3691 goto cleanup;
3692 if (gfc_match_char (',') != MATCH_YES)
3693 break;
3696 if (gfc_error_flag_test () == 0)
3697 gfc_error ("Syntax error in data declaration at %C");
3698 m = MATCH_ERROR;
3700 gfc_free_data_all (gfc_current_ns);
3702 cleanup:
3703 gfc_free_array_spec (current_as);
3704 current_as = NULL;
3705 return m;
3709 /* Match a prefix associated with a function or subroutine
3710 declaration. If the typespec pointer is nonnull, then a typespec
3711 can be matched. Note that if nothing matches, MATCH_YES is
3712 returned (the null string was matched). */
3714 match
3715 gfc_match_prefix (gfc_typespec *ts)
3717 bool seen_type;
3719 gfc_clear_attr (&current_attr);
3720 seen_type = 0;
3722 loop:
3723 if (!seen_type && ts != NULL
3724 && gfc_match_type_spec (ts, 0) == MATCH_YES
3725 && gfc_match_space () == MATCH_YES)
3728 seen_type = 1;
3729 goto loop;
3732 if (gfc_match ("elemental% ") == MATCH_YES)
3734 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
3735 return MATCH_ERROR;
3737 goto loop;
3740 if (gfc_match ("pure% ") == MATCH_YES)
3742 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
3743 return MATCH_ERROR;
3745 goto loop;
3748 if (gfc_match ("recursive% ") == MATCH_YES)
3750 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
3751 return MATCH_ERROR;
3753 goto loop;
3756 /* At this point, the next item is not a prefix. */
3757 return MATCH_YES;
3761 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
3763 static try
3764 copy_prefix (symbol_attribute *dest, locus *where)
3766 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3767 return FAILURE;
3769 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3770 return FAILURE;
3772 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3773 return FAILURE;
3775 return SUCCESS;
3779 /* Match a formal argument list. */
3781 match
3782 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3784 gfc_formal_arglist *head, *tail, *p, *q;
3785 char name[GFC_MAX_SYMBOL_LEN + 1];
3786 gfc_symbol *sym;
3787 match m;
3789 head = tail = NULL;
3791 if (gfc_match_char ('(') != MATCH_YES)
3793 if (null_flag)
3794 goto ok;
3795 return MATCH_NO;
3798 if (gfc_match_char (')') == MATCH_YES)
3799 goto ok;
3801 for (;;)
3803 if (gfc_match_char ('*') == MATCH_YES)
3804 sym = NULL;
3805 else
3807 m = gfc_match_name (name);
3808 if (m != MATCH_YES)
3809 goto cleanup;
3811 if (gfc_get_symbol (name, NULL, &sym))
3812 goto cleanup;
3815 p = gfc_get_formal_arglist ();
3817 if (head == NULL)
3818 head = tail = p;
3819 else
3821 tail->next = p;
3822 tail = p;
3825 tail->sym = sym;
3827 /* We don't add the VARIABLE flavor because the name could be a
3828 dummy procedure. We don't apply these attributes to formal
3829 arguments of statement functions. */
3830 if (sym != NULL && !st_flag
3831 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3832 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3834 m = MATCH_ERROR;
3835 goto cleanup;
3838 /* The name of a program unit can be in a different namespace,
3839 so check for it explicitly. After the statement is accepted,
3840 the name is checked for especially in gfc_get_symbol(). */
3841 if (gfc_new_block != NULL && sym != NULL
3842 && strcmp (sym->name, gfc_new_block->name) == 0)
3844 gfc_error ("Name '%s' at %C is the name of the procedure",
3845 sym->name);
3846 m = MATCH_ERROR;
3847 goto cleanup;
3850 if (gfc_match_char (')') == MATCH_YES)
3851 goto ok;
3853 m = gfc_match_char (',');
3854 if (m != MATCH_YES)
3856 gfc_error ("Unexpected junk in formal argument list at %C");
3857 goto cleanup;
3862 /* Check for duplicate symbols in the formal argument list. */
3863 if (head != NULL)
3865 for (p = head; p->next; p = p->next)
3867 if (p->sym == NULL)
3868 continue;
3870 for (q = p->next; q; q = q->next)
3871 if (p->sym == q->sym)
3873 gfc_error ("Duplicate symbol '%s' in formal argument list "
3874 "at %C", p->sym->name);
3876 m = MATCH_ERROR;
3877 goto cleanup;
3882 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3883 == FAILURE)
3885 m = MATCH_ERROR;
3886 goto cleanup;
3889 return MATCH_YES;
3891 cleanup:
3892 gfc_free_formal_arglist (head);
3893 return m;
3897 /* Match a RESULT specification following a function declaration or
3898 ENTRY statement. Also matches the end-of-statement. */
3900 static match
3901 match_result (gfc_symbol *function, gfc_symbol **result)
3903 char name[GFC_MAX_SYMBOL_LEN + 1];
3904 gfc_symbol *r;
3905 match m;
3907 if (gfc_match (" result (") != MATCH_YES)
3908 return MATCH_NO;
3910 m = gfc_match_name (name);
3911 if (m != MATCH_YES)
3912 return m;
3914 /* Get the right paren, and that's it because there could be the
3915 bind(c) attribute after the result clause. */
3916 if (gfc_match_char(')') != MATCH_YES)
3918 /* TODO: should report the missing right paren here. */
3919 return MATCH_ERROR;
3922 if (strcmp (function->name, name) == 0)
3924 gfc_error ("RESULT variable at %C must be different than function name");
3925 return MATCH_ERROR;
3928 if (gfc_get_symbol (name, NULL, &r))
3929 return MATCH_ERROR;
3931 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3932 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
3933 return MATCH_ERROR;
3935 *result = r;
3937 return MATCH_YES;
3941 /* Match a function suffix, which could be a combination of a result
3942 clause and BIND(C), either one, or neither. The draft does not
3943 require them to come in a specific order. */
3945 match
3946 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3948 match is_bind_c; /* Found bind(c). */
3949 match is_result; /* Found result clause. */
3950 match found_match; /* Status of whether we've found a good match. */
3951 char peek_char; /* Character we're going to peek at. */
3952 bool allow_binding_name;
3954 /* Initialize to having found nothing. */
3955 found_match = MATCH_NO;
3956 is_bind_c = MATCH_NO;
3957 is_result = MATCH_NO;
3959 /* Get the next char to narrow between result and bind(c). */
3960 gfc_gobble_whitespace ();
3961 peek_char = gfc_peek_ascii_char ();
3963 /* C binding names are not allowed for internal procedures. */
3964 if (gfc_current_state () == COMP_CONTAINS
3965 && sym->ns->proc_name->attr.flavor != FL_MODULE)
3966 allow_binding_name = false;
3967 else
3968 allow_binding_name = true;
3970 switch (peek_char)
3972 case 'r':
3973 /* Look for result clause. */
3974 is_result = match_result (sym, result);
3975 if (is_result == MATCH_YES)
3977 /* Now see if there is a bind(c) after it. */
3978 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
3979 /* We've found the result clause and possibly bind(c). */
3980 found_match = MATCH_YES;
3982 else
3983 /* This should only be MATCH_ERROR. */
3984 found_match = is_result;
3985 break;
3986 case 'b':
3987 /* Look for bind(c) first. */
3988 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
3989 if (is_bind_c == MATCH_YES)
3991 /* Now see if a result clause followed it. */
3992 is_result = match_result (sym, result);
3993 found_match = MATCH_YES;
3995 else
3997 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3998 found_match = MATCH_ERROR;
4000 break;
4001 default:
4002 gfc_error ("Unexpected junk after function declaration at %C");
4003 found_match = MATCH_ERROR;
4004 break;
4007 if (is_bind_c == MATCH_YES)
4009 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4010 if (gfc_current_state () == COMP_CONTAINS
4011 && sym->ns->proc_name->attr.flavor != FL_MODULE
4012 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4013 "at %L may not be specified for an internal "
4014 "procedure", &gfc_current_locus)
4015 == FAILURE)
4016 return MATCH_ERROR;
4018 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4019 == FAILURE)
4020 return MATCH_ERROR;
4023 return found_match;
4027 /* Match a PROCEDURE declaration (R1211). */
4029 static match
4030 match_procedure_decl (void)
4032 match m;
4033 locus old_loc, entry_loc;
4034 gfc_symbol *sym, *proc_if = NULL;
4035 int num;
4037 old_loc = entry_loc = gfc_current_locus;
4039 gfc_clear_ts (&current_ts);
4041 if (gfc_match (" (") != MATCH_YES)
4043 gfc_current_locus = entry_loc;
4044 return MATCH_NO;
4047 /* Get the type spec. for the procedure interface. */
4048 old_loc = gfc_current_locus;
4049 m = gfc_match_type_spec (&current_ts, 0);
4050 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4051 goto got_ts;
4053 if (m == MATCH_ERROR)
4054 return m;
4056 gfc_current_locus = old_loc;
4058 /* Get the name of the procedure or abstract interface
4059 to inherit the interface from. */
4060 m = gfc_match_symbol (&proc_if, 1);
4062 if (m == MATCH_NO)
4063 goto syntax;
4064 else if (m == MATCH_ERROR)
4065 return m;
4067 /* Various interface checks. */
4068 if (proc_if)
4070 /* Resolve interface if possible. That way, attr.procedure is only set
4071 if it is declared by a later procedure-declaration-stmt, which is
4072 invalid per C1212. */
4073 while (proc_if->ts.interface)
4074 proc_if = proc_if->ts.interface;
4076 if (proc_if->generic)
4078 gfc_error ("Interface '%s' at %C may not be generic", proc_if->name);
4079 return MATCH_ERROR;
4081 if (proc_if->attr.proc == PROC_ST_FUNCTION)
4083 gfc_error ("Interface '%s' at %C may not be a statement function",
4084 proc_if->name);
4085 return MATCH_ERROR;
4087 /* Handle intrinsic procedures. */
4088 if (gfc_intrinsic_name (proc_if->name, 0)
4089 || gfc_intrinsic_name (proc_if->name, 1))
4090 proc_if->attr.intrinsic = 1;
4091 if (proc_if->attr.intrinsic
4092 && !gfc_intrinsic_actual_ok (proc_if->name, 0))
4094 gfc_error ("Intrinsic procedure '%s' not allowed "
4095 "in PROCEDURE statement at %C", proc_if->name);
4096 return MATCH_ERROR;
4100 got_ts:
4101 if (gfc_match (" )") != MATCH_YES)
4103 gfc_current_locus = entry_loc;
4104 return MATCH_NO;
4107 /* Parse attributes. */
4108 m = match_attr_spec();
4109 if (m == MATCH_ERROR)
4110 return MATCH_ERROR;
4112 /* Get procedure symbols. */
4113 for(num=1;;num++)
4115 m = gfc_match_symbol (&sym, 0);
4116 if (m == MATCH_NO)
4117 goto syntax;
4118 else if (m == MATCH_ERROR)
4119 return m;
4121 /* Add current_attr to the symbol attributes. */
4122 if (gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4123 return MATCH_ERROR;
4125 if (sym->attr.is_bind_c)
4127 /* Check for C1218. */
4128 if (!proc_if || !proc_if->attr.is_bind_c)
4130 gfc_error ("BIND(C) attribute at %C requires "
4131 "an interface with BIND(C)");
4132 return MATCH_ERROR;
4134 /* Check for C1217. */
4135 if (has_name_equals && sym->attr.pointer)
4137 gfc_error ("BIND(C) procedure with NAME may not have "
4138 "POINTER attribute at %C");
4139 return MATCH_ERROR;
4141 if (has_name_equals && sym->attr.dummy)
4143 gfc_error ("Dummy procedure at %C may not have "
4144 "BIND(C) attribute with NAME");
4145 return MATCH_ERROR;
4147 /* Set binding label for BIND(C). */
4148 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4149 return MATCH_ERROR;
4152 if (!sym->attr.pointer && gfc_add_external (&sym->attr, NULL) == FAILURE)
4153 return MATCH_ERROR;
4154 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4155 return MATCH_ERROR;
4157 /* Set interface. */
4158 if (proc_if != NULL)
4160 sym->ts.interface = proc_if;
4161 sym->attr.untyped = 1;
4163 else if (current_ts.type != BT_UNKNOWN)
4165 sym->ts = current_ts;
4166 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4167 sym->ts.interface->ts = current_ts;
4168 sym->ts.interface->attr.function = 1;
4169 sym->attr.function = sym->ts.interface->attr.function;
4172 if (gfc_match_eos () == MATCH_YES)
4173 return MATCH_YES;
4174 if (gfc_match_char (',') != MATCH_YES)
4175 goto syntax;
4178 syntax:
4179 gfc_error ("Syntax error in PROCEDURE statement at %C");
4180 return MATCH_ERROR;
4184 /* Match a PROCEDURE declaration inside an interface (R1206). */
4186 static match
4187 match_procedure_in_interface (void)
4189 match m;
4190 gfc_symbol *sym;
4191 char name[GFC_MAX_SYMBOL_LEN + 1];
4193 if (current_interface.type == INTERFACE_NAMELESS
4194 || current_interface.type == INTERFACE_ABSTRACT)
4196 gfc_error ("PROCEDURE at %C must be in a generic interface");
4197 return MATCH_ERROR;
4200 for(;;)
4202 m = gfc_match_name (name);
4203 if (m == MATCH_NO)
4204 goto syntax;
4205 else if (m == MATCH_ERROR)
4206 return m;
4207 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4208 return MATCH_ERROR;
4210 if (gfc_add_interface (sym) == FAILURE)
4211 return MATCH_ERROR;
4213 if (gfc_match_eos () == MATCH_YES)
4214 break;
4215 if (gfc_match_char (',') != MATCH_YES)
4216 goto syntax;
4219 return MATCH_YES;
4221 syntax:
4222 gfc_error ("Syntax error in PROCEDURE statement at %C");
4223 return MATCH_ERROR;
4227 /* General matcher for PROCEDURE declarations. */
4229 match
4230 gfc_match_procedure (void)
4232 match m;
4234 switch (gfc_current_state ())
4236 case COMP_NONE:
4237 case COMP_PROGRAM:
4238 case COMP_MODULE:
4239 case COMP_SUBROUTINE:
4240 case COMP_FUNCTION:
4241 m = match_procedure_decl ();
4242 break;
4243 case COMP_INTERFACE:
4244 m = match_procedure_in_interface ();
4245 break;
4246 case COMP_DERIVED:
4247 gfc_error ("Fortran 2003: Procedure components at %C are "
4248 "not yet implemented in gfortran");
4249 return MATCH_ERROR;
4250 default:
4251 return MATCH_NO;
4254 if (m != MATCH_YES)
4255 return m;
4257 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4258 == FAILURE)
4259 return MATCH_ERROR;
4261 return m;
4265 /* Match a function declaration. */
4267 match
4268 gfc_match_function_decl (void)
4270 char name[GFC_MAX_SYMBOL_LEN + 1];
4271 gfc_symbol *sym, *result;
4272 locus old_loc;
4273 match m;
4274 match suffix_match;
4275 match found_match; /* Status returned by match func. */
4277 if (gfc_current_state () != COMP_NONE
4278 && gfc_current_state () != COMP_INTERFACE
4279 && gfc_current_state () != COMP_CONTAINS)
4280 return MATCH_NO;
4282 gfc_clear_ts (&current_ts);
4284 old_loc = gfc_current_locus;
4286 m = gfc_match_prefix (&current_ts);
4287 if (m != MATCH_YES)
4289 gfc_current_locus = old_loc;
4290 return m;
4293 if (gfc_match ("function% %n", name) != MATCH_YES)
4295 gfc_current_locus = old_loc;
4296 return MATCH_NO;
4298 if (get_proc_name (name, &sym, false))
4299 return MATCH_ERROR;
4300 gfc_new_block = sym;
4302 m = gfc_match_formal_arglist (sym, 0, 0);
4303 if (m == MATCH_NO)
4305 gfc_error ("Expected formal argument list in function "
4306 "definition at %C");
4307 m = MATCH_ERROR;
4308 goto cleanup;
4310 else if (m == MATCH_ERROR)
4311 goto cleanup;
4313 result = NULL;
4315 /* According to the draft, the bind(c) and result clause can
4316 come in either order after the formal_arg_list (i.e., either
4317 can be first, both can exist together or by themselves or neither
4318 one). Therefore, the match_result can't match the end of the
4319 string, and check for the bind(c) or result clause in either order. */
4320 found_match = gfc_match_eos ();
4322 /* Make sure that it isn't already declared as BIND(C). If it is, it
4323 must have been marked BIND(C) with a BIND(C) attribute and that is
4324 not allowed for procedures. */
4325 if (sym->attr.is_bind_c == 1)
4327 sym->attr.is_bind_c = 0;
4328 if (sym->old_symbol != NULL)
4329 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4330 "variables or common blocks",
4331 &(sym->old_symbol->declared_at));
4332 else
4333 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4334 "variables or common blocks", &gfc_current_locus);
4337 if (found_match != MATCH_YES)
4339 /* If we haven't found the end-of-statement, look for a suffix. */
4340 suffix_match = gfc_match_suffix (sym, &result);
4341 if (suffix_match == MATCH_YES)
4342 /* Need to get the eos now. */
4343 found_match = gfc_match_eos ();
4344 else
4345 found_match = suffix_match;
4348 if(found_match != MATCH_YES)
4349 m = MATCH_ERROR;
4350 else
4352 /* Make changes to the symbol. */
4353 m = MATCH_ERROR;
4355 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4356 goto cleanup;
4358 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4359 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4360 goto cleanup;
4362 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
4363 && !sym->attr.implicit_type)
4365 gfc_error ("Function '%s' at %C already has a type of %s", name,
4366 gfc_basic_typename (sym->ts.type));
4367 goto cleanup;
4370 /* Delay matching the function characteristics until after the
4371 specification block by signalling kind=-1. */
4372 sym->declared_at = old_loc;
4373 if (current_ts.type != BT_UNKNOWN)
4374 current_ts.kind = -1;
4375 else
4376 current_ts.kind = 0;
4378 if (result == NULL)
4380 sym->ts = current_ts;
4381 sym->result = sym;
4383 else
4385 result->ts = current_ts;
4386 sym->result = result;
4389 return MATCH_YES;
4392 cleanup:
4393 gfc_current_locus = old_loc;
4394 return m;
4398 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4399 pass the name of the entry, rather than the gfc_current_block name, and
4400 to return false upon finding an existing global entry. */
4402 static bool
4403 add_global_entry (const char *name, int sub)
4405 gfc_gsymbol *s;
4406 unsigned int type;
4408 s = gfc_get_gsymbol(name);
4409 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4411 if (s->defined
4412 || (s->type != GSYM_UNKNOWN
4413 && s->type != type))
4414 gfc_global_used(s, NULL);
4415 else
4417 s->type = type;
4418 s->where = gfc_current_locus;
4419 s->defined = 1;
4420 return true;
4422 return false;
4426 /* Match an ENTRY statement. */
4428 match
4429 gfc_match_entry (void)
4431 gfc_symbol *proc;
4432 gfc_symbol *result;
4433 gfc_symbol *entry;
4434 char name[GFC_MAX_SYMBOL_LEN + 1];
4435 gfc_compile_state state;
4436 match m;
4437 gfc_entry_list *el;
4438 locus old_loc;
4439 bool module_procedure;
4440 char peek_char;
4441 match is_bind_c;
4443 m = gfc_match_name (name);
4444 if (m != MATCH_YES)
4445 return m;
4447 state = gfc_current_state ();
4448 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
4450 switch (state)
4452 case COMP_PROGRAM:
4453 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4454 break;
4455 case COMP_MODULE:
4456 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4457 break;
4458 case COMP_BLOCK_DATA:
4459 gfc_error ("ENTRY statement at %C cannot appear within "
4460 "a BLOCK DATA");
4461 break;
4462 case COMP_INTERFACE:
4463 gfc_error ("ENTRY statement at %C cannot appear within "
4464 "an INTERFACE");
4465 break;
4466 case COMP_DERIVED:
4467 gfc_error ("ENTRY statement at %C cannot appear within "
4468 "a DERIVED TYPE block");
4469 break;
4470 case COMP_IF:
4471 gfc_error ("ENTRY statement at %C cannot appear within "
4472 "an IF-THEN block");
4473 break;
4474 case COMP_DO:
4475 gfc_error ("ENTRY statement at %C cannot appear within "
4476 "a DO block");
4477 break;
4478 case COMP_SELECT:
4479 gfc_error ("ENTRY statement at %C cannot appear within "
4480 "a SELECT block");
4481 break;
4482 case COMP_FORALL:
4483 gfc_error ("ENTRY statement at %C cannot appear within "
4484 "a FORALL block");
4485 break;
4486 case COMP_WHERE:
4487 gfc_error ("ENTRY statement at %C cannot appear within "
4488 "a WHERE block");
4489 break;
4490 case COMP_CONTAINS:
4491 gfc_error ("ENTRY statement at %C cannot appear within "
4492 "a contained subprogram");
4493 break;
4494 default:
4495 gfc_internal_error ("gfc_match_entry(): Bad state");
4497 return MATCH_ERROR;
4500 module_procedure = gfc_current_ns->parent != NULL
4501 && gfc_current_ns->parent->proc_name
4502 && gfc_current_ns->parent->proc_name->attr.flavor
4503 == FL_MODULE;
4505 if (gfc_current_ns->parent != NULL
4506 && gfc_current_ns->parent->proc_name
4507 && !module_procedure)
4509 gfc_error("ENTRY statement at %C cannot appear in a "
4510 "contained procedure");
4511 return MATCH_ERROR;
4514 /* Module function entries need special care in get_proc_name
4515 because previous references within the function will have
4516 created symbols attached to the current namespace. */
4517 if (get_proc_name (name, &entry,
4518 gfc_current_ns->parent != NULL
4519 && module_procedure
4520 && gfc_current_ns->proc_name->attr.function))
4521 return MATCH_ERROR;
4523 proc = gfc_current_block ();
4525 /* Make sure that it isn't already declared as BIND(C). If it is, it
4526 must have been marked BIND(C) with a BIND(C) attribute and that is
4527 not allowed for procedures. */
4528 if (entry->attr.is_bind_c == 1)
4530 entry->attr.is_bind_c = 0;
4531 if (entry->old_symbol != NULL)
4532 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4533 "variables or common blocks",
4534 &(entry->old_symbol->declared_at));
4535 else
4536 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4537 "variables or common blocks", &gfc_current_locus);
4540 /* Check what next non-whitespace character is so we can tell if there
4541 is the required parens if we have a BIND(C). */
4542 gfc_gobble_whitespace ();
4543 peek_char = gfc_peek_ascii_char ();
4545 if (state == COMP_SUBROUTINE)
4547 /* An entry in a subroutine. */
4548 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
4549 return MATCH_ERROR;
4551 m = gfc_match_formal_arglist (entry, 0, 1);
4552 if (m != MATCH_YES)
4553 return MATCH_ERROR;
4555 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
4556 never be an internal procedure. */
4557 is_bind_c = gfc_match_bind_c (entry, true);
4558 if (is_bind_c == MATCH_ERROR)
4559 return MATCH_ERROR;
4560 if (is_bind_c == MATCH_YES)
4562 if (peek_char != '(')
4564 gfc_error ("Missing required parentheses before BIND(C) at %C");
4565 return MATCH_ERROR;
4567 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
4568 == FAILURE)
4569 return MATCH_ERROR;
4572 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4573 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4574 return MATCH_ERROR;
4576 else
4578 /* An entry in a function.
4579 We need to take special care because writing
4580 ENTRY f()
4582 ENTRY f
4583 is allowed, whereas
4584 ENTRY f() RESULT (r)
4585 can't be written as
4586 ENTRY f RESULT (r). */
4587 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
4588 return MATCH_ERROR;
4590 old_loc = gfc_current_locus;
4591 if (gfc_match_eos () == MATCH_YES)
4593 gfc_current_locus = old_loc;
4594 /* Match the empty argument list, and add the interface to
4595 the symbol. */
4596 m = gfc_match_formal_arglist (entry, 0, 1);
4598 else
4599 m = gfc_match_formal_arglist (entry, 0, 0);
4601 if (m != MATCH_YES)
4602 return MATCH_ERROR;
4604 result = NULL;
4606 if (gfc_match_eos () == MATCH_YES)
4608 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4609 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4610 return MATCH_ERROR;
4612 entry->result = entry;
4614 else
4616 m = gfc_match_suffix (entry, &result);
4617 if (m == MATCH_NO)
4618 gfc_syntax_error (ST_ENTRY);
4619 if (m != MATCH_YES)
4620 return MATCH_ERROR;
4622 if (result)
4624 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4625 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
4626 || gfc_add_function (&entry->attr, result->name, NULL)
4627 == FAILURE)
4628 return MATCH_ERROR;
4629 entry->result = result;
4631 else
4633 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4634 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4635 return MATCH_ERROR;
4636 entry->result = entry;
4641 if (gfc_match_eos () != MATCH_YES)
4643 gfc_syntax_error (ST_ENTRY);
4644 return MATCH_ERROR;
4647 entry->attr.recursive = proc->attr.recursive;
4648 entry->attr.elemental = proc->attr.elemental;
4649 entry->attr.pure = proc->attr.pure;
4651 el = gfc_get_entry_list ();
4652 el->sym = entry;
4653 el->next = gfc_current_ns->entries;
4654 gfc_current_ns->entries = el;
4655 if (el->next)
4656 el->id = el->next->id + 1;
4657 else
4658 el->id = 1;
4660 new_st.op = EXEC_ENTRY;
4661 new_st.ext.entry = el;
4663 return MATCH_YES;
4667 /* Match a subroutine statement, including optional prefixes. */
4669 match
4670 gfc_match_subroutine (void)
4672 char name[GFC_MAX_SYMBOL_LEN + 1];
4673 gfc_symbol *sym;
4674 match m;
4675 match is_bind_c;
4676 char peek_char;
4677 bool allow_binding_name;
4679 if (gfc_current_state () != COMP_NONE
4680 && gfc_current_state () != COMP_INTERFACE
4681 && gfc_current_state () != COMP_CONTAINS)
4682 return MATCH_NO;
4684 m = gfc_match_prefix (NULL);
4685 if (m != MATCH_YES)
4686 return m;
4688 m = gfc_match ("subroutine% %n", name);
4689 if (m != MATCH_YES)
4690 return m;
4692 if (get_proc_name (name, &sym, false))
4693 return MATCH_ERROR;
4694 gfc_new_block = sym;
4696 /* Check what next non-whitespace character is so we can tell if there
4697 is the required parens if we have a BIND(C). */
4698 gfc_gobble_whitespace ();
4699 peek_char = gfc_peek_ascii_char ();
4701 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
4702 return MATCH_ERROR;
4704 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4705 return MATCH_ERROR;
4707 /* Make sure that it isn't already declared as BIND(C). If it is, it
4708 must have been marked BIND(C) with a BIND(C) attribute and that is
4709 not allowed for procedures. */
4710 if (sym->attr.is_bind_c == 1)
4712 sym->attr.is_bind_c = 0;
4713 if (sym->old_symbol != NULL)
4714 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4715 "variables or common blocks",
4716 &(sym->old_symbol->declared_at));
4717 else
4718 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4719 "variables or common blocks", &gfc_current_locus);
4722 /* C binding names are not allowed for internal procedures. */
4723 if (gfc_current_state () == COMP_CONTAINS
4724 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4725 allow_binding_name = false;
4726 else
4727 allow_binding_name = true;
4729 /* Here, we are just checking if it has the bind(c) attribute, and if
4730 so, then we need to make sure it's all correct. If it doesn't,
4731 we still need to continue matching the rest of the subroutine line. */
4732 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4733 if (is_bind_c == MATCH_ERROR)
4735 /* There was an attempt at the bind(c), but it was wrong. An
4736 error message should have been printed w/in the gfc_match_bind_c
4737 so here we'll just return the MATCH_ERROR. */
4738 return MATCH_ERROR;
4741 if (is_bind_c == MATCH_YES)
4743 /* The following is allowed in the Fortran 2008 draft. */
4744 if (gfc_current_state () == COMP_CONTAINS
4745 && sym->ns->proc_name->attr.flavor != FL_MODULE
4746 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4747 "at %L may not be specified for an internal "
4748 "procedure", &gfc_current_locus)
4749 == FAILURE)
4750 return MATCH_ERROR;
4752 if (peek_char != '(')
4754 gfc_error ("Missing required parentheses before BIND(C) at %C");
4755 return MATCH_ERROR;
4757 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4758 == FAILURE)
4759 return MATCH_ERROR;
4762 if (gfc_match_eos () != MATCH_YES)
4764 gfc_syntax_error (ST_SUBROUTINE);
4765 return MATCH_ERROR;
4768 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4769 return MATCH_ERROR;
4771 return MATCH_YES;
4775 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4776 given, and set the binding label in either the given symbol (if not
4777 NULL), or in the current_ts. The symbol may be NULL because we may
4778 encounter the BIND(C) before the declaration itself. Return
4779 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4780 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4781 or MATCH_YES if the specifier was correct and the binding label and
4782 bind(c) fields were set correctly for the given symbol or the
4783 current_ts. If allow_binding_name is false, no binding name may be
4784 given. */
4786 match
4787 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
4789 /* binding label, if exists */
4790 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4791 match double_quote;
4792 match single_quote;
4794 /* Initialize the flag that specifies whether we encountered a NAME=
4795 specifier or not. */
4796 has_name_equals = 0;
4798 /* Init the first char to nil so we can catch if we don't have
4799 the label (name attr) or the symbol name yet. */
4800 binding_label[0] = '\0';
4802 /* This much we have to be able to match, in this order, if
4803 there is a bind(c) label. */
4804 if (gfc_match (" bind ( c ") != MATCH_YES)
4805 return MATCH_NO;
4807 /* Now see if there is a binding label, or if we've reached the
4808 end of the bind(c) attribute without one. */
4809 if (gfc_match_char (',') == MATCH_YES)
4811 if (gfc_match (" name = ") != MATCH_YES)
4813 gfc_error ("Syntax error in NAME= specifier for binding label "
4814 "at %C");
4815 /* should give an error message here */
4816 return MATCH_ERROR;
4819 has_name_equals = 1;
4821 /* Get the opening quote. */
4822 double_quote = MATCH_YES;
4823 single_quote = MATCH_YES;
4824 double_quote = gfc_match_char ('"');
4825 if (double_quote != MATCH_YES)
4826 single_quote = gfc_match_char ('\'');
4827 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4829 gfc_error ("Syntax error in NAME= specifier for binding label "
4830 "at %C");
4831 return MATCH_ERROR;
4834 /* Grab the binding label, using functions that will not lower
4835 case the names automatically. */
4836 if (gfc_match_name_C (binding_label) != MATCH_YES)
4837 return MATCH_ERROR;
4839 /* Get the closing quotation. */
4840 if (double_quote == MATCH_YES)
4842 if (gfc_match_char ('"') != MATCH_YES)
4844 gfc_error ("Missing closing quote '\"' for binding label at %C");
4845 /* User started string with '"' so looked to match it. */
4846 return MATCH_ERROR;
4849 else
4851 if (gfc_match_char ('\'') != MATCH_YES)
4853 gfc_error ("Missing closing quote '\'' for binding label at %C");
4854 /* User started string with "'" char. */
4855 return MATCH_ERROR;
4860 /* Get the required right paren. */
4861 if (gfc_match_char (')') != MATCH_YES)
4863 gfc_error ("Missing closing paren for binding label at %C");
4864 return MATCH_ERROR;
4867 if (has_name_equals && !allow_binding_name)
4869 gfc_error ("No binding name is allowed in BIND(C) at %C");
4870 return MATCH_ERROR;
4873 if (has_name_equals && sym != NULL && sym->attr.dummy)
4875 gfc_error ("For dummy procedure %s, no binding name is "
4876 "allowed in BIND(C) at %C", sym->name);
4877 return MATCH_ERROR;
4881 /* Save the binding label to the symbol. If sym is null, we're
4882 probably matching the typespec attributes of a declaration and
4883 haven't gotten the name yet, and therefore, no symbol yet. */
4884 if (binding_label[0] != '\0')
4886 if (sym != NULL)
4888 strcpy (sym->binding_label, binding_label);
4890 else
4891 strcpy (curr_binding_label, binding_label);
4893 else if (allow_binding_name)
4895 /* No binding label, but if symbol isn't null, we
4896 can set the label for it here.
4897 If name="" or allow_binding_name is false, no C binding name is
4898 created. */
4899 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4900 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4903 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
4904 && current_interface.type == INTERFACE_ABSTRACT)
4906 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
4907 return MATCH_ERROR;
4910 return MATCH_YES;
4914 /* Return nonzero if we're currently compiling a contained procedure. */
4916 static int
4917 contained_procedure (void)
4919 gfc_state_data *s = gfc_state_stack;
4921 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
4922 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
4923 return 1;
4925 return 0;
4928 /* Set the kind of each enumerator. The kind is selected such that it is
4929 interoperable with the corresponding C enumeration type, making
4930 sure that -fshort-enums is honored. */
4932 static void
4933 set_enum_kind(void)
4935 enumerator_history *current_history = NULL;
4936 int kind;
4937 int i;
4939 if (max_enum == NULL || enum_history == NULL)
4940 return;
4942 if (!gfc_option.fshort_enums)
4943 return;
4945 i = 0;
4948 kind = gfc_integer_kinds[i++].kind;
4950 while (kind < gfc_c_int_kind
4951 && gfc_check_integer_range (max_enum->initializer->value.integer,
4952 kind) != ARITH_OK);
4954 current_history = enum_history;
4955 while (current_history != NULL)
4957 current_history->sym->ts.kind = kind;
4958 current_history = current_history->next;
4963 /* Match any of the various end-block statements. Returns the type of
4964 END to the caller. The END INTERFACE, END IF, END DO and END
4965 SELECT statements cannot be replaced by a single END statement. */
4967 match
4968 gfc_match_end (gfc_statement *st)
4970 char name[GFC_MAX_SYMBOL_LEN + 1];
4971 gfc_compile_state state;
4972 locus old_loc;
4973 const char *block_name;
4974 const char *target;
4975 int eos_ok;
4976 match m;
4978 old_loc = gfc_current_locus;
4979 if (gfc_match ("end") != MATCH_YES)
4980 return MATCH_NO;
4982 state = gfc_current_state ();
4983 block_name = gfc_current_block () == NULL
4984 ? NULL : gfc_current_block ()->name;
4986 if (state == COMP_CONTAINS)
4988 state = gfc_state_stack->previous->state;
4989 block_name = gfc_state_stack->previous->sym == NULL
4990 ? NULL : gfc_state_stack->previous->sym->name;
4993 switch (state)
4995 case COMP_NONE:
4996 case COMP_PROGRAM:
4997 *st = ST_END_PROGRAM;
4998 target = " program";
4999 eos_ok = 1;
5000 break;
5002 case COMP_SUBROUTINE:
5003 *st = ST_END_SUBROUTINE;
5004 target = " subroutine";
5005 eos_ok = !contained_procedure ();
5006 break;
5008 case COMP_FUNCTION:
5009 *st = ST_END_FUNCTION;
5010 target = " function";
5011 eos_ok = !contained_procedure ();
5012 break;
5014 case COMP_BLOCK_DATA:
5015 *st = ST_END_BLOCK_DATA;
5016 target = " block data";
5017 eos_ok = 1;
5018 break;
5020 case COMP_MODULE:
5021 *st = ST_END_MODULE;
5022 target = " module";
5023 eos_ok = 1;
5024 break;
5026 case COMP_INTERFACE:
5027 *st = ST_END_INTERFACE;
5028 target = " interface";
5029 eos_ok = 0;
5030 break;
5032 case COMP_DERIVED:
5033 *st = ST_END_TYPE;
5034 target = " type";
5035 eos_ok = 0;
5036 break;
5038 case COMP_IF:
5039 *st = ST_ENDIF;
5040 target = " if";
5041 eos_ok = 0;
5042 break;
5044 case COMP_DO:
5045 *st = ST_ENDDO;
5046 target = " do";
5047 eos_ok = 0;
5048 break;
5050 case COMP_SELECT:
5051 *st = ST_END_SELECT;
5052 target = " select";
5053 eos_ok = 0;
5054 break;
5056 case COMP_FORALL:
5057 *st = ST_END_FORALL;
5058 target = " forall";
5059 eos_ok = 0;
5060 break;
5062 case COMP_WHERE:
5063 *st = ST_END_WHERE;
5064 target = " where";
5065 eos_ok = 0;
5066 break;
5068 case COMP_ENUM:
5069 *st = ST_END_ENUM;
5070 target = " enum";
5071 eos_ok = 0;
5072 last_initializer = NULL;
5073 set_enum_kind ();
5074 gfc_free_enum_history ();
5075 break;
5077 default:
5078 gfc_error ("Unexpected END statement at %C");
5079 goto cleanup;
5082 if (gfc_match_eos () == MATCH_YES)
5084 if (!eos_ok)
5086 /* We would have required END [something]. */
5087 gfc_error ("%s statement expected at %L",
5088 gfc_ascii_statement (*st), &old_loc);
5089 goto cleanup;
5092 return MATCH_YES;
5095 /* Verify that we've got the sort of end-block that we're expecting. */
5096 if (gfc_match (target) != MATCH_YES)
5098 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5099 goto cleanup;
5102 /* If we're at the end, make sure a block name wasn't required. */
5103 if (gfc_match_eos () == MATCH_YES)
5106 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5107 && *st != ST_END_FORALL && *st != ST_END_WHERE)
5108 return MATCH_YES;
5110 if (gfc_current_block () == NULL)
5111 return MATCH_YES;
5113 gfc_error ("Expected block name of '%s' in %s statement at %C",
5114 block_name, gfc_ascii_statement (*st));
5116 return MATCH_ERROR;
5119 /* END INTERFACE has a special handler for its several possible endings. */
5120 if (*st == ST_END_INTERFACE)
5121 return gfc_match_end_interface ();
5123 /* We haven't hit the end of statement, so what is left must be an
5124 end-name. */
5125 m = gfc_match_space ();
5126 if (m == MATCH_YES)
5127 m = gfc_match_name (name);
5129 if (m == MATCH_NO)
5130 gfc_error ("Expected terminating name at %C");
5131 if (m != MATCH_YES)
5132 goto cleanup;
5134 if (block_name == NULL)
5135 goto syntax;
5137 if (strcmp (name, block_name) != 0)
5139 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5140 gfc_ascii_statement (*st));
5141 goto cleanup;
5144 if (gfc_match_eos () == MATCH_YES)
5145 return MATCH_YES;
5147 syntax:
5148 gfc_syntax_error (*st);
5150 cleanup:
5151 gfc_current_locus = old_loc;
5152 return MATCH_ERROR;
5157 /***************** Attribute declaration statements ****************/
5159 /* Set the attribute of a single variable. */
5161 static match
5162 attr_decl1 (void)
5164 char name[GFC_MAX_SYMBOL_LEN + 1];
5165 gfc_array_spec *as;
5166 gfc_symbol *sym;
5167 locus var_locus;
5168 match m;
5170 as = NULL;
5172 m = gfc_match_name (name);
5173 if (m != MATCH_YES)
5174 goto cleanup;
5176 if (find_special (name, &sym))
5177 return MATCH_ERROR;
5179 var_locus = gfc_current_locus;
5181 /* Deal with possible array specification for certain attributes. */
5182 if (current_attr.dimension
5183 || current_attr.allocatable
5184 || current_attr.pointer
5185 || current_attr.target)
5187 m = gfc_match_array_spec (&as);
5188 if (m == MATCH_ERROR)
5189 goto cleanup;
5191 if (current_attr.dimension && m == MATCH_NO)
5193 gfc_error ("Missing array specification at %L in DIMENSION "
5194 "statement", &var_locus);
5195 m = MATCH_ERROR;
5196 goto cleanup;
5199 if (current_attr.dimension && sym->value)
5201 gfc_error ("Dimensions specified for %s at %L after its "
5202 "initialisation", sym->name, &var_locus);
5203 m = MATCH_ERROR;
5204 goto cleanup;
5207 if ((current_attr.allocatable || current_attr.pointer)
5208 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5210 gfc_error ("Array specification must be deferred at %L", &var_locus);
5211 m = MATCH_ERROR;
5212 goto cleanup;
5216 /* Update symbol table. DIMENSION attribute is set
5217 in gfc_set_array_spec(). */
5218 if (current_attr.dimension == 0
5219 && gfc_copy_attr (&sym->attr, &current_attr, &var_locus) == FAILURE)
5221 m = MATCH_ERROR;
5222 goto cleanup;
5225 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5227 m = MATCH_ERROR;
5228 goto cleanup;
5231 if (sym->attr.cray_pointee && sym->as != NULL)
5233 /* Fix the array spec. */
5234 m = gfc_mod_pointee_as (sym->as);
5235 if (m == MATCH_ERROR)
5236 goto cleanup;
5239 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
5241 m = MATCH_ERROR;
5242 goto cleanup;
5245 if ((current_attr.external || current_attr.intrinsic)
5246 && sym->attr.flavor != FL_PROCEDURE
5247 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
5249 m = MATCH_ERROR;
5250 goto cleanup;
5253 return MATCH_YES;
5255 cleanup:
5256 gfc_free_array_spec (as);
5257 return m;
5261 /* Generic attribute declaration subroutine. Used for attributes that
5262 just have a list of names. */
5264 static match
5265 attr_decl (void)
5267 match m;
5269 /* Gobble the optional double colon, by simply ignoring the result
5270 of gfc_match(). */
5271 gfc_match (" ::");
5273 for (;;)
5275 m = attr_decl1 ();
5276 if (m != MATCH_YES)
5277 break;
5279 if (gfc_match_eos () == MATCH_YES)
5281 m = MATCH_YES;
5282 break;
5285 if (gfc_match_char (',') != MATCH_YES)
5287 gfc_error ("Unexpected character in variable list at %C");
5288 m = MATCH_ERROR;
5289 break;
5293 return m;
5297 /* This routine matches Cray Pointer declarations of the form:
5298 pointer ( <pointer>, <pointee> )
5300 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5301 The pointer, if already declared, should be an integer. Otherwise, we
5302 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5303 be either a scalar, or an array declaration. No space is allocated for
5304 the pointee. For the statement
5305 pointer (ipt, ar(10))
5306 any subsequent uses of ar will be translated (in C-notation) as
5307 ar(i) => ((<type> *) ipt)(i)
5308 After gimplification, pointee variable will disappear in the code. */
5310 static match
5311 cray_pointer_decl (void)
5313 match m;
5314 gfc_array_spec *as;
5315 gfc_symbol *cptr; /* Pointer symbol. */
5316 gfc_symbol *cpte; /* Pointee symbol. */
5317 locus var_locus;
5318 bool done = false;
5320 while (!done)
5322 if (gfc_match_char ('(') != MATCH_YES)
5324 gfc_error ("Expected '(' at %C");
5325 return MATCH_ERROR;
5328 /* Match pointer. */
5329 var_locus = gfc_current_locus;
5330 gfc_clear_attr (&current_attr);
5331 gfc_add_cray_pointer (&current_attr, &var_locus);
5332 current_ts.type = BT_INTEGER;
5333 current_ts.kind = gfc_index_integer_kind;
5335 m = gfc_match_symbol (&cptr, 0);
5336 if (m != MATCH_YES)
5338 gfc_error ("Expected variable name at %C");
5339 return m;
5342 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5343 return MATCH_ERROR;
5345 gfc_set_sym_referenced (cptr);
5347 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5349 cptr->ts.type = BT_INTEGER;
5350 cptr->ts.kind = gfc_index_integer_kind;
5352 else if (cptr->ts.type != BT_INTEGER)
5354 gfc_error ("Cray pointer at %C must be an integer");
5355 return MATCH_ERROR;
5357 else if (cptr->ts.kind < gfc_index_integer_kind)
5358 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5359 " memory addresses require %d bytes",
5360 cptr->ts.kind, gfc_index_integer_kind);
5362 if (gfc_match_char (',') != MATCH_YES)
5364 gfc_error ("Expected \",\" at %C");
5365 return MATCH_ERROR;
5368 /* Match Pointee. */
5369 var_locus = gfc_current_locus;
5370 gfc_clear_attr (&current_attr);
5371 gfc_add_cray_pointee (&current_attr, &var_locus);
5372 current_ts.type = BT_UNKNOWN;
5373 current_ts.kind = 0;
5375 m = gfc_match_symbol (&cpte, 0);
5376 if (m != MATCH_YES)
5378 gfc_error ("Expected variable name at %C");
5379 return m;
5382 /* Check for an optional array spec. */
5383 m = gfc_match_array_spec (&as);
5384 if (m == MATCH_ERROR)
5386 gfc_free_array_spec (as);
5387 return m;
5389 else if (m == MATCH_NO)
5391 gfc_free_array_spec (as);
5392 as = NULL;
5395 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5396 return MATCH_ERROR;
5398 gfc_set_sym_referenced (cpte);
5400 if (cpte->as == NULL)
5402 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5403 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5405 else if (as != NULL)
5407 gfc_error ("Duplicate array spec for Cray pointee at %C");
5408 gfc_free_array_spec (as);
5409 return MATCH_ERROR;
5412 as = NULL;
5414 if (cpte->as != NULL)
5416 /* Fix array spec. */
5417 m = gfc_mod_pointee_as (cpte->as);
5418 if (m == MATCH_ERROR)
5419 return m;
5422 /* Point the Pointee at the Pointer. */
5423 cpte->cp_pointer = cptr;
5425 if (gfc_match_char (')') != MATCH_YES)
5427 gfc_error ("Expected \")\" at %C");
5428 return MATCH_ERROR;
5430 m = gfc_match_char (',');
5431 if (m != MATCH_YES)
5432 done = true; /* Stop searching for more declarations. */
5436 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5437 || gfc_match_eos () != MATCH_YES)
5439 gfc_error ("Expected \",\" or end of statement at %C");
5440 return MATCH_ERROR;
5442 return MATCH_YES;
5446 match
5447 gfc_match_external (void)
5450 gfc_clear_attr (&current_attr);
5451 current_attr.external = 1;
5453 return attr_decl ();
5457 match
5458 gfc_match_intent (void)
5460 sym_intent intent;
5462 intent = match_intent_spec ();
5463 if (intent == INTENT_UNKNOWN)
5464 return MATCH_ERROR;
5466 gfc_clear_attr (&current_attr);
5467 current_attr.intent = intent;
5469 return attr_decl ();
5473 match
5474 gfc_match_intrinsic (void)
5477 gfc_clear_attr (&current_attr);
5478 current_attr.intrinsic = 1;
5480 return attr_decl ();
5484 match
5485 gfc_match_optional (void)
5488 gfc_clear_attr (&current_attr);
5489 current_attr.optional = 1;
5491 return attr_decl ();
5495 match
5496 gfc_match_pointer (void)
5498 gfc_gobble_whitespace ();
5499 if (gfc_peek_ascii_char () == '(')
5501 if (!gfc_option.flag_cray_pointer)
5503 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5504 "flag");
5505 return MATCH_ERROR;
5507 return cray_pointer_decl ();
5509 else
5511 gfc_clear_attr (&current_attr);
5512 current_attr.pointer = 1;
5514 return attr_decl ();
5519 match
5520 gfc_match_allocatable (void)
5522 gfc_clear_attr (&current_attr);
5523 current_attr.allocatable = 1;
5525 return attr_decl ();
5529 match
5530 gfc_match_dimension (void)
5532 gfc_clear_attr (&current_attr);
5533 current_attr.dimension = 1;
5535 return attr_decl ();
5539 match
5540 gfc_match_target (void)
5542 gfc_clear_attr (&current_attr);
5543 current_attr.target = 1;
5545 return attr_decl ();
5549 /* Match the list of entities being specified in a PUBLIC or PRIVATE
5550 statement. */
5552 static match
5553 access_attr_decl (gfc_statement st)
5555 char name[GFC_MAX_SYMBOL_LEN + 1];
5556 interface_type type;
5557 gfc_user_op *uop;
5558 gfc_symbol *sym;
5559 gfc_intrinsic_op operator;
5560 match m;
5562 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5563 goto done;
5565 for (;;)
5567 m = gfc_match_generic_spec (&type, name, &operator);
5568 if (m == MATCH_NO)
5569 goto syntax;
5570 if (m == MATCH_ERROR)
5571 return MATCH_ERROR;
5573 switch (type)
5575 case INTERFACE_NAMELESS:
5576 case INTERFACE_ABSTRACT:
5577 goto syntax;
5579 case INTERFACE_GENERIC:
5580 if (gfc_get_symbol (name, NULL, &sym))
5581 goto done;
5583 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5584 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
5585 sym->name, NULL) == FAILURE)
5586 return MATCH_ERROR;
5588 break;
5590 case INTERFACE_INTRINSIC_OP:
5591 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
5593 gfc_current_ns->operator_access[operator] =
5594 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5596 else
5598 gfc_error ("Access specification of the %s operator at %C has "
5599 "already been specified", gfc_op2string (operator));
5600 goto done;
5603 break;
5605 case INTERFACE_USER_OP:
5606 uop = gfc_get_uop (name);
5608 if (uop->access == ACCESS_UNKNOWN)
5610 uop->access = (st == ST_PUBLIC)
5611 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5613 else
5615 gfc_error ("Access specification of the .%s. operator at %C "
5616 "has already been specified", sym->name);
5617 goto done;
5620 break;
5623 if (gfc_match_char (',') == MATCH_NO)
5624 break;
5627 if (gfc_match_eos () != MATCH_YES)
5628 goto syntax;
5629 return MATCH_YES;
5631 syntax:
5632 gfc_syntax_error (st);
5634 done:
5635 return MATCH_ERROR;
5639 match
5640 gfc_match_protected (void)
5642 gfc_symbol *sym;
5643 match m;
5645 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5647 gfc_error ("PROTECTED at %C only allowed in specification "
5648 "part of a module");
5649 return MATCH_ERROR;
5653 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
5654 == FAILURE)
5655 return MATCH_ERROR;
5657 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5659 return MATCH_ERROR;
5662 if (gfc_match_eos () == MATCH_YES)
5663 goto syntax;
5665 for(;;)
5667 m = gfc_match_symbol (&sym, 0);
5668 switch (m)
5670 case MATCH_YES:
5671 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
5672 == FAILURE)
5673 return MATCH_ERROR;
5674 goto next_item;
5676 case MATCH_NO:
5677 break;
5679 case MATCH_ERROR:
5680 return MATCH_ERROR;
5683 next_item:
5684 if (gfc_match_eos () == MATCH_YES)
5685 break;
5686 if (gfc_match_char (',') != MATCH_YES)
5687 goto syntax;
5690 return MATCH_YES;
5692 syntax:
5693 gfc_error ("Syntax error in PROTECTED statement at %C");
5694 return MATCH_ERROR;
5698 /* The PRIVATE statement is a bit weird in that it can be an attribute
5699 declaration, but also works as a standlone statement inside of a
5700 type declaration or a module. */
5702 match
5703 gfc_match_private (gfc_statement *st)
5706 if (gfc_match ("private") != MATCH_YES)
5707 return MATCH_NO;
5709 if (gfc_current_state () != COMP_MODULE
5710 && (gfc_current_state () != COMP_DERIVED
5711 || !gfc_state_stack->previous
5712 || gfc_state_stack->previous->state != COMP_MODULE))
5714 gfc_error ("PRIVATE statement at %C is only allowed in the "
5715 "specification part of a module");
5716 return MATCH_ERROR;
5719 if (gfc_current_state () == COMP_DERIVED)
5721 if (gfc_match_eos () == MATCH_YES)
5723 *st = ST_PRIVATE;
5724 return MATCH_YES;
5727 gfc_syntax_error (ST_PRIVATE);
5728 return MATCH_ERROR;
5731 if (gfc_match_eos () == MATCH_YES)
5733 *st = ST_PRIVATE;
5734 return MATCH_YES;
5737 *st = ST_ATTR_DECL;
5738 return access_attr_decl (ST_PRIVATE);
5742 match
5743 gfc_match_public (gfc_statement *st)
5746 if (gfc_match ("public") != MATCH_YES)
5747 return MATCH_NO;
5749 if (gfc_current_state () != COMP_MODULE)
5751 gfc_error ("PUBLIC statement at %C is only allowed in the "
5752 "specification part of a module");
5753 return MATCH_ERROR;
5756 if (gfc_match_eos () == MATCH_YES)
5758 *st = ST_PUBLIC;
5759 return MATCH_YES;
5762 *st = ST_ATTR_DECL;
5763 return access_attr_decl (ST_PUBLIC);
5767 /* Workhorse for gfc_match_parameter. */
5769 static match
5770 do_parm (void)
5772 gfc_symbol *sym;
5773 gfc_expr *init;
5774 match m;
5776 m = gfc_match_symbol (&sym, 0);
5777 if (m == MATCH_NO)
5778 gfc_error ("Expected variable name at %C in PARAMETER statement");
5780 if (m != MATCH_YES)
5781 return m;
5783 if (gfc_match_char ('=') == MATCH_NO)
5785 gfc_error ("Expected = sign in PARAMETER statement at %C");
5786 return MATCH_ERROR;
5789 m = gfc_match_init_expr (&init);
5790 if (m == MATCH_NO)
5791 gfc_error ("Expected expression at %C in PARAMETER statement");
5792 if (m != MATCH_YES)
5793 return m;
5795 if (sym->ts.type == BT_UNKNOWN
5796 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5798 m = MATCH_ERROR;
5799 goto cleanup;
5802 if (gfc_check_assign_symbol (sym, init) == FAILURE
5803 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
5805 m = MATCH_ERROR;
5806 goto cleanup;
5809 if (sym->value)
5811 gfc_error ("Initializing already initialized variable at %C");
5812 m = MATCH_ERROR;
5813 goto cleanup;
5816 if (sym->ts.type == BT_CHARACTER
5817 && sym->ts.cl != NULL
5818 && sym->ts.cl->length != NULL
5819 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5820 && init->expr_type == EXPR_CONSTANT
5821 && init->ts.type == BT_CHARACTER
5822 && init->ts.kind == 1)
5823 gfc_set_constant_character_len (
5824 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
5826 sym->value = init;
5827 return MATCH_YES;
5829 cleanup:
5830 gfc_free_expr (init);
5831 return m;
5835 /* Match a parameter statement, with the weird syntax that these have. */
5837 match
5838 gfc_match_parameter (void)
5840 match m;
5842 if (gfc_match_char ('(') == MATCH_NO)
5843 return MATCH_NO;
5845 for (;;)
5847 m = do_parm ();
5848 if (m != MATCH_YES)
5849 break;
5851 if (gfc_match (" )%t") == MATCH_YES)
5852 break;
5854 if (gfc_match_char (',') != MATCH_YES)
5856 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5857 m = MATCH_ERROR;
5858 break;
5862 return m;
5866 /* Save statements have a special syntax. */
5868 match
5869 gfc_match_save (void)
5871 char n[GFC_MAX_SYMBOL_LEN+1];
5872 gfc_common_head *c;
5873 gfc_symbol *sym;
5874 match m;
5876 if (gfc_match_eos () == MATCH_YES)
5878 if (gfc_current_ns->seen_save)
5880 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5881 "follows previous SAVE statement")
5882 == FAILURE)
5883 return MATCH_ERROR;
5886 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5887 return MATCH_YES;
5890 if (gfc_current_ns->save_all)
5892 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5893 "blanket SAVE statement")
5894 == FAILURE)
5895 return MATCH_ERROR;
5898 gfc_match (" ::");
5900 for (;;)
5902 m = gfc_match_symbol (&sym, 0);
5903 switch (m)
5905 case MATCH_YES:
5906 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5907 == FAILURE)
5908 return MATCH_ERROR;
5909 goto next_item;
5911 case MATCH_NO:
5912 break;
5914 case MATCH_ERROR:
5915 return MATCH_ERROR;
5918 m = gfc_match (" / %n /", &n);
5919 if (m == MATCH_ERROR)
5920 return MATCH_ERROR;
5921 if (m == MATCH_NO)
5922 goto syntax;
5924 c = gfc_get_common (n, 0);
5925 c->saved = 1;
5927 gfc_current_ns->seen_save = 1;
5929 next_item:
5930 if (gfc_match_eos () == MATCH_YES)
5931 break;
5932 if (gfc_match_char (',') != MATCH_YES)
5933 goto syntax;
5936 return MATCH_YES;
5938 syntax:
5939 gfc_error ("Syntax error in SAVE statement at %C");
5940 return MATCH_ERROR;
5944 match
5945 gfc_match_value (void)
5947 gfc_symbol *sym;
5948 match m;
5950 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
5951 == FAILURE)
5952 return MATCH_ERROR;
5954 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5956 return MATCH_ERROR;
5959 if (gfc_match_eos () == MATCH_YES)
5960 goto syntax;
5962 for(;;)
5964 m = gfc_match_symbol (&sym, 0);
5965 switch (m)
5967 case MATCH_YES:
5968 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5969 == FAILURE)
5970 return MATCH_ERROR;
5971 goto next_item;
5973 case MATCH_NO:
5974 break;
5976 case MATCH_ERROR:
5977 return MATCH_ERROR;
5980 next_item:
5981 if (gfc_match_eos () == MATCH_YES)
5982 break;
5983 if (gfc_match_char (',') != MATCH_YES)
5984 goto syntax;
5987 return MATCH_YES;
5989 syntax:
5990 gfc_error ("Syntax error in VALUE statement at %C");
5991 return MATCH_ERROR;
5995 match
5996 gfc_match_volatile (void)
5998 gfc_symbol *sym;
5999 match m;
6001 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
6002 == FAILURE)
6003 return MATCH_ERROR;
6005 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6007 return MATCH_ERROR;
6010 if (gfc_match_eos () == MATCH_YES)
6011 goto syntax;
6013 for(;;)
6015 /* VOLATILE is special because it can be added to host-associated
6016 symbols locally. */
6017 m = gfc_match_symbol (&sym, 1);
6018 switch (m)
6020 case MATCH_YES:
6021 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6022 == FAILURE)
6023 return MATCH_ERROR;
6024 goto next_item;
6026 case MATCH_NO:
6027 break;
6029 case MATCH_ERROR:
6030 return MATCH_ERROR;
6033 next_item:
6034 if (gfc_match_eos () == MATCH_YES)
6035 break;
6036 if (gfc_match_char (',') != MATCH_YES)
6037 goto syntax;
6040 return MATCH_YES;
6042 syntax:
6043 gfc_error ("Syntax error in VOLATILE statement at %C");
6044 return MATCH_ERROR;
6048 /* Match a module procedure statement. Note that we have to modify
6049 symbols in the parent's namespace because the current one was there
6050 to receive symbols that are in an interface's formal argument list. */
6052 match
6053 gfc_match_modproc (void)
6055 char name[GFC_MAX_SYMBOL_LEN + 1];
6056 gfc_symbol *sym;
6057 match m;
6058 gfc_namespace *module_ns;
6059 gfc_interface *old_interface_head, *interface;
6061 if (gfc_state_stack->state != COMP_INTERFACE
6062 || gfc_state_stack->previous == NULL
6063 || current_interface.type == INTERFACE_NAMELESS
6064 || current_interface.type == INTERFACE_ABSTRACT)
6066 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6067 "interface");
6068 return MATCH_ERROR;
6071 module_ns = gfc_current_ns->parent;
6072 for (; module_ns; module_ns = module_ns->parent)
6073 if (module_ns->proc_name->attr.flavor == FL_MODULE)
6074 break;
6076 if (module_ns == NULL)
6077 return MATCH_ERROR;
6079 /* Store the current state of the interface. We will need it if we
6080 end up with a syntax error and need to recover. */
6081 old_interface_head = gfc_current_interface_head ();
6083 for (;;)
6085 bool last = false;
6087 m = gfc_match_name (name);
6088 if (m == MATCH_NO)
6089 goto syntax;
6090 if (m != MATCH_YES)
6091 return MATCH_ERROR;
6093 /* Check for syntax error before starting to add symbols to the
6094 current namespace. */
6095 if (gfc_match_eos () == MATCH_YES)
6096 last = true;
6097 if (!last && gfc_match_char (',') != MATCH_YES)
6098 goto syntax;
6100 /* Now we're sure the syntax is valid, we process this item
6101 further. */
6102 if (gfc_get_symbol (name, module_ns, &sym))
6103 return MATCH_ERROR;
6105 if (sym->attr.proc != PROC_MODULE
6106 && gfc_add_procedure (&sym->attr, PROC_MODULE,
6107 sym->name, NULL) == FAILURE)
6108 return MATCH_ERROR;
6110 if (gfc_add_interface (sym) == FAILURE)
6111 return MATCH_ERROR;
6113 sym->attr.mod_proc = 1;
6115 if (last)
6116 break;
6119 return MATCH_YES;
6121 syntax:
6122 /* Restore the previous state of the interface. */
6123 interface = gfc_current_interface_head ();
6124 gfc_set_current_interface_head (old_interface_head);
6126 /* Free the new interfaces. */
6127 while (interface != old_interface_head)
6129 gfc_interface *i = interface->next;
6130 gfc_free (interface);
6131 interface = i;
6134 /* And issue a syntax error. */
6135 gfc_syntax_error (ST_MODULE_PROC);
6136 return MATCH_ERROR;
6140 /* Match the optional attribute specifiers for a type declaration.
6141 Return MATCH_ERROR if an error is encountered in one of the handled
6142 attributes (public, private, bind(c)), MATCH_NO if what's found is
6143 not a handled attribute, and MATCH_YES otherwise. TODO: More error
6144 checking on attribute conflicts needs to be done. */
6146 match
6147 gfc_get_type_attr_spec (symbol_attribute *attr)
6149 /* See if the derived type is marked as private. */
6150 if (gfc_match (" , private") == MATCH_YES)
6152 if (gfc_current_state () != COMP_MODULE)
6154 gfc_error ("Derived type at %C can only be PRIVATE in the "
6155 "specification part of a module");
6156 return MATCH_ERROR;
6159 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6160 return MATCH_ERROR;
6162 else if (gfc_match (" , public") == MATCH_YES)
6164 if (gfc_current_state () != COMP_MODULE)
6166 gfc_error ("Derived type at %C can only be PUBLIC in the "
6167 "specification part of a module");
6168 return MATCH_ERROR;
6171 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6172 return MATCH_ERROR;
6174 else if (gfc_match(" , bind ( c )") == MATCH_YES)
6176 /* If the type is defined to be bind(c) it then needs to make
6177 sure that all fields are interoperable. This will
6178 need to be a semantic check on the finished derived type.
6179 See 15.2.3 (lines 9-12) of F2003 draft. */
6180 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
6181 return MATCH_ERROR;
6183 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
6185 else
6186 return MATCH_NO;
6188 /* If we get here, something matched. */
6189 return MATCH_YES;
6193 /* Match the beginning of a derived type declaration. If a type name
6194 was the result of a function, then it is possible to have a symbol
6195 already to be known as a derived type yet have no components. */
6197 match
6198 gfc_match_derived_decl (void)
6200 char name[GFC_MAX_SYMBOL_LEN + 1];
6201 symbol_attribute attr;
6202 gfc_symbol *sym;
6203 match m;
6204 match is_type_attr_spec = MATCH_NO;
6205 bool seen_attr = false;
6207 if (gfc_current_state () == COMP_DERIVED)
6208 return MATCH_NO;
6210 gfc_clear_attr (&attr);
6214 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
6215 if (is_type_attr_spec == MATCH_ERROR)
6216 return MATCH_ERROR;
6217 if (is_type_attr_spec == MATCH_YES)
6218 seen_attr = true;
6219 } while (is_type_attr_spec == MATCH_YES);
6221 if (gfc_match (" ::") != MATCH_YES && seen_attr)
6223 gfc_error ("Expected :: in TYPE definition at %C");
6224 return MATCH_ERROR;
6227 m = gfc_match (" %n%t", name);
6228 if (m != MATCH_YES)
6229 return m;
6231 /* Make sure the name is not the name of an intrinsic type. */
6232 if (gfc_is_intrinsic_typename (name))
6234 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
6235 "type", name);
6236 return MATCH_ERROR;
6239 if (gfc_get_symbol (name, NULL, &sym))
6240 return MATCH_ERROR;
6242 if (sym->ts.type != BT_UNKNOWN)
6244 gfc_error ("Derived type name '%s' at %C already has a basic type "
6245 "of %s", sym->name, gfc_typename (&sym->ts));
6246 return MATCH_ERROR;
6249 /* The symbol may already have the derived attribute without the
6250 components. The ways this can happen is via a function
6251 definition, an INTRINSIC statement or a subtype in another
6252 derived type that is a pointer. The first part of the AND clause
6253 is true if a the symbol is not the return value of a function. */
6254 if (sym->attr.flavor != FL_DERIVED
6255 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6256 return MATCH_ERROR;
6258 if (sym->components != NULL || sym->attr.zero_comp)
6260 gfc_error ("Derived type definition of '%s' at %C has already been "
6261 "defined", sym->name);
6262 return MATCH_ERROR;
6265 if (attr.access != ACCESS_UNKNOWN
6266 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6267 return MATCH_ERROR;
6269 /* See if the derived type was labeled as bind(c). */
6270 if (attr.is_bind_c != 0)
6271 sym->attr.is_bind_c = attr.is_bind_c;
6273 /* Construct the f2k_derived namespace if it is not yet there. */
6274 if (!sym->f2k_derived)
6275 sym->f2k_derived = gfc_get_namespace (NULL, 0);
6277 gfc_new_block = sym;
6279 return MATCH_YES;
6283 /* Cray Pointees can be declared as:
6284 pointer (ipt, a (n,m,...,*))
6285 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6286 cheat and set a constant bound of 1 for the last dimension, if this
6287 is the case. Since there is no bounds-checking for Cray Pointees,
6288 this will be okay. */
6291 gfc_mod_pointee_as (gfc_array_spec *as)
6293 as->cray_pointee = true; /* This will be useful to know later. */
6294 if (as->type == AS_ASSUMED_SIZE)
6296 as->type = AS_EXPLICIT;
6297 as->upper[as->rank - 1] = gfc_int_expr (1);
6298 as->cp_was_assumed = true;
6300 else if (as->type == AS_ASSUMED_SHAPE)
6302 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6303 return MATCH_ERROR;
6305 return MATCH_YES;
6309 /* Match the enum definition statement, here we are trying to match
6310 the first line of enum definition statement.
6311 Returns MATCH_YES if match is found. */
6313 match
6314 gfc_match_enum (void)
6316 match m;
6318 m = gfc_match_eos ();
6319 if (m != MATCH_YES)
6320 return m;
6322 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
6323 == FAILURE)
6324 return MATCH_ERROR;
6326 return MATCH_YES;
6330 /* Match a variable name with an optional initializer. When this
6331 subroutine is called, a variable is expected to be parsed next.
6332 Depending on what is happening at the moment, updates either the
6333 symbol table or the current interface. */
6335 static match
6336 enumerator_decl (void)
6338 char name[GFC_MAX_SYMBOL_LEN + 1];
6339 gfc_expr *initializer;
6340 gfc_array_spec *as = NULL;
6341 gfc_symbol *sym;
6342 locus var_locus;
6343 match m;
6344 try t;
6345 locus old_locus;
6347 initializer = NULL;
6348 old_locus = gfc_current_locus;
6350 /* When we get here, we've just matched a list of attributes and
6351 maybe a type and a double colon. The next thing we expect to see
6352 is the name of the symbol. */
6353 m = gfc_match_name (name);
6354 if (m != MATCH_YES)
6355 goto cleanup;
6357 var_locus = gfc_current_locus;
6359 /* OK, we've successfully matched the declaration. Now put the
6360 symbol in the current namespace. If we fail to create the symbol,
6361 bail out. */
6362 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6364 m = MATCH_ERROR;
6365 goto cleanup;
6368 /* The double colon must be present in order to have initializers.
6369 Otherwise the statement is ambiguous with an assignment statement. */
6370 if (colon_seen)
6372 if (gfc_match_char ('=') == MATCH_YES)
6374 m = gfc_match_init_expr (&initializer);
6375 if (m == MATCH_NO)
6377 gfc_error ("Expected an initialization expression at %C");
6378 m = MATCH_ERROR;
6381 if (m != MATCH_YES)
6382 goto cleanup;
6386 /* If we do not have an initializer, the initialization value of the
6387 previous enumerator (stored in last_initializer) is incremented
6388 by 1 and is used to initialize the current enumerator. */
6389 if (initializer == NULL)
6390 initializer = gfc_enum_initializer (last_initializer, old_locus);
6392 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6394 gfc_error("ENUMERATOR %L not initialized with integer expression",
6395 &var_locus);
6396 m = MATCH_ERROR;
6397 gfc_free_enum_history ();
6398 goto cleanup;
6401 /* Store this current initializer, for the next enumerator variable
6402 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6403 use last_initializer below. */
6404 last_initializer = initializer;
6405 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6407 /* Maintain enumerator history. */
6408 gfc_find_symbol (name, NULL, 0, &sym);
6409 create_enum_history (sym, last_initializer);
6411 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6413 cleanup:
6414 /* Free stuff up and return. */
6415 gfc_free_expr (initializer);
6417 return m;
6421 /* Match the enumerator definition statement. */
6423 match
6424 gfc_match_enumerator_def (void)
6426 match m;
6427 try t;
6429 gfc_clear_ts (&current_ts);
6431 m = gfc_match (" enumerator");
6432 if (m != MATCH_YES)
6433 return m;
6435 m = gfc_match (" :: ");
6436 if (m == MATCH_ERROR)
6437 return m;
6439 colon_seen = (m == MATCH_YES);
6441 if (gfc_current_state () != COMP_ENUM)
6443 gfc_error ("ENUM definition statement expected before %C");
6444 gfc_free_enum_history ();
6445 return MATCH_ERROR;
6448 (&current_ts)->type = BT_INTEGER;
6449 (&current_ts)->kind = gfc_c_int_kind;
6451 gfc_clear_attr (&current_attr);
6452 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
6453 if (t == FAILURE)
6455 m = MATCH_ERROR;
6456 goto cleanup;
6459 for (;;)
6461 m = enumerator_decl ();
6462 if (m == MATCH_ERROR)
6463 goto cleanup;
6464 if (m == MATCH_NO)
6465 break;
6467 if (gfc_match_eos () == MATCH_YES)
6468 goto cleanup;
6469 if (gfc_match_char (',') != MATCH_YES)
6470 break;
6473 if (gfc_current_state () == COMP_ENUM)
6475 gfc_free_enum_history ();
6476 gfc_error ("Syntax error in ENUMERATOR definition at %C");
6477 m = MATCH_ERROR;
6480 cleanup:
6481 gfc_free_array_spec (current_as);
6482 current_as = NULL;
6483 return m;
6487 /* Match a FINAL declaration inside a derived type. */
6489 match
6490 gfc_match_final_decl (void)
6492 char name[GFC_MAX_SYMBOL_LEN + 1];
6493 gfc_symbol* sym;
6494 match m;
6495 gfc_namespace* module_ns;
6496 bool first, last;
6498 if (gfc_state_stack->state != COMP_DERIVED)
6500 gfc_error ("FINAL declaration at %C must be inside a derived type "
6501 "definition!");
6502 return MATCH_ERROR;
6505 gcc_assert (gfc_current_block ());
6507 if (!gfc_state_stack->previous
6508 || gfc_state_stack->previous->state != COMP_MODULE)
6510 gfc_error ("Derived type declaration with FINAL at %C must be in the"
6511 " specification part of a MODULE");
6512 return MATCH_ERROR;
6515 module_ns = gfc_current_ns;
6516 gcc_assert (module_ns);
6517 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
6519 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
6520 if (gfc_match (" ::") == MATCH_ERROR)
6521 return MATCH_ERROR;
6523 /* Match the sequence of procedure names. */
6524 first = true;
6525 last = false;
6528 gfc_finalizer* f;
6530 if (first && gfc_match_eos () == MATCH_YES)
6532 gfc_error ("Empty FINAL at %C");
6533 return MATCH_ERROR;
6536 m = gfc_match_name (name);
6537 if (m == MATCH_NO)
6539 gfc_error ("Expected module procedure name at %C");
6540 return MATCH_ERROR;
6542 else if (m != MATCH_YES)
6543 return MATCH_ERROR;
6545 if (gfc_match_eos () == MATCH_YES)
6546 last = true;
6547 if (!last && gfc_match_char (',') != MATCH_YES)
6549 gfc_error ("Expected ',' at %C");
6550 return MATCH_ERROR;
6553 if (gfc_get_symbol (name, module_ns, &sym))
6555 gfc_error ("Unknown procedure name \"%s\" at %C", name);
6556 return MATCH_ERROR;
6559 /* Mark the symbol as module procedure. */
6560 if (sym->attr.proc != PROC_MODULE
6561 && gfc_add_procedure (&sym->attr, PROC_MODULE,
6562 sym->name, NULL) == FAILURE)
6563 return MATCH_ERROR;
6565 /* Check if we already have this symbol in the list, this is an error. */
6566 for (f = gfc_current_block ()->f2k_derived->finalizers; f; f = f->next)
6567 if (f->procedure == sym)
6569 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
6570 name);
6571 return MATCH_ERROR;
6574 /* Add this symbol to the list of finalizers. */
6575 gcc_assert (gfc_current_block ()->f2k_derived);
6576 ++sym->refs;
6577 f = gfc_getmem (sizeof (gfc_finalizer));
6578 f->procedure = sym;
6579 f->where = gfc_current_locus;
6580 f->next = gfc_current_block ()->f2k_derived->finalizers;
6581 gfc_current_block ()->f2k_derived->finalizers = f;
6583 first = false;
6585 while (!last);
6587 return MATCH_YES;