1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
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
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 /* This flag is set if an old-style length selector is matched
30 during a type-declaration statement. */
32 static int old_char_selector;
34 /* When variables acquire types and attributes from a declaration
35 statement, they get them from the following static variables. The
36 first part of a declaration sets these variables and the second
37 part copies these into symbol structures. */
39 static gfc_typespec current_ts;
41 static symbol_attribute current_attr;
42 static gfc_array_spec *current_as;
43 static int colon_seen;
45 /* The current binding label (if any). */
46 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
47 /* Need to know how many identifiers are on the current data declaration
48 line in case we're given the BIND(C) attribute with a NAME= specifier. */
49 static int num_idents_on_line;
50 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
51 can supply a name if the curr_binding_label is nil and NAME= was not. */
52 static int has_name_equals = 0;
54 /* Initializer of the previous enumerator. */
56 static gfc_expr *last_initializer;
58 /* History of all the enumerators is maintained, so that
59 kind values of all the enumerators could be updated depending
60 upon the maximum initialized value. */
62 typedef struct enumerator_history
65 gfc_expr *initializer;
66 struct enumerator_history *next;
70 /* Header of enum history chain. */
72 static enumerator_history *enum_history = NULL;
74 /* Pointer of enum history node containing largest initializer. */
76 static enumerator_history *max_enum = NULL;
78 /* gfc_new_block points to the symbol of a newly matched block. */
80 gfc_symbol *gfc_new_block;
83 /********************* DATA statement subroutines *********************/
85 static bool in_match_data = false;
88 gfc_in_match_data (void)
94 gfc_set_in_match_data (bool set_value)
96 in_match_data = set_value;
99 /* Free a gfc_data_variable structure and everything beneath it. */
102 free_variable (gfc_data_variable *p)
104 gfc_data_variable *q;
109 gfc_free_expr (p->expr);
110 gfc_free_iterator (&p->iter, 0);
111 free_variable (p->list);
117 /* Free a gfc_data_value structure and everything beneath it. */
120 free_value (gfc_data_value *p)
127 gfc_free_expr (p->expr);
133 /* Free a list of gfc_data structures. */
136 gfc_free_data (gfc_data *p)
143 free_variable (p->var);
144 free_value (p->value);
150 /* Free all data in a namespace. */
153 gfc_free_data_all (gfc_namespace *ns)
166 static match var_element (gfc_data_variable *);
168 /* Match a list of variables terminated by an iterator and a right
172 var_list (gfc_data_variable *parent)
174 gfc_data_variable *tail, var;
177 m = var_element (&var);
178 if (m == MATCH_ERROR)
183 tail = gfc_get_data_variable ();
190 if (gfc_match_char (',') != MATCH_YES)
193 m = gfc_match_iterator (&parent->iter, 1);
196 if (m == MATCH_ERROR)
199 m = var_element (&var);
200 if (m == MATCH_ERROR)
205 tail->next = gfc_get_data_variable ();
211 if (gfc_match_char (')') != MATCH_YES)
216 gfc_syntax_error (ST_DATA);
221 /* Match a single element in a data variable list, which can be a
222 variable-iterator list. */
225 var_element (gfc_data_variable *new)
230 memset (new, 0, sizeof (gfc_data_variable));
232 if (gfc_match_char ('(') == MATCH_YES)
233 return var_list (new);
235 m = gfc_match_variable (&new->expr, 0);
239 sym = new->expr->symtree->n.sym;
241 if (!sym->attr.function && gfc_current_ns->parent
242 && gfc_current_ns->parent == sym->ns)
244 gfc_error ("Host associated variable '%s' may not be in the DATA "
245 "statement at %C", sym->name);
249 if (gfc_current_state () != COMP_BLOCK_DATA
250 && sym->attr.in_common
251 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
252 "common block variable '%s' in DATA statement at %C",
253 sym->name) == FAILURE)
256 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
263 /* Match the top-level list of data variables. */
266 top_var_list (gfc_data *d)
268 gfc_data_variable var, *tail, *new;
275 m = var_element (&var);
278 if (m == MATCH_ERROR)
281 new = gfc_get_data_variable ();
291 if (gfc_match_char ('/') == MATCH_YES)
293 if (gfc_match_char (',') != MATCH_YES)
300 gfc_syntax_error (ST_DATA);
301 gfc_free_data_all (gfc_current_ns);
307 match_data_constant (gfc_expr **result)
309 char name[GFC_MAX_SYMBOL_LEN + 1];
315 m = gfc_match_literal_constant (&expr, 1);
322 if (m == MATCH_ERROR)
325 m = gfc_match_null (result);
329 old_loc = gfc_current_locus;
331 /* Should this be a structure component, try to match it
332 before matching a name. */
333 m = gfc_match_rvalue (result);
334 if (m == MATCH_ERROR)
337 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
339 if (gfc_simplify_expr (*result, 0) == FAILURE)
344 gfc_current_locus = old_loc;
346 m = gfc_match_name (name);
350 if (gfc_find_symbol (name, NULL, 1, &sym))
354 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
356 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
360 else if (sym->attr.flavor == FL_DERIVED)
361 return gfc_match_structure_constructor (sym, result);
363 *result = gfc_copy_expr (sym->value);
368 /* Match a list of values in a DATA statement. The leading '/' has
369 already been seen at this point. */
372 top_val_list (gfc_data *data)
374 gfc_data_value *new, *tail;
383 m = match_data_constant (&expr);
386 if (m == MATCH_ERROR)
389 new = gfc_get_data_value ();
398 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
406 msg = gfc_extract_int (expr, &tmp);
407 gfc_free_expr (expr);
415 m = match_data_constant (&tail->expr);
418 if (m == MATCH_ERROR)
422 if (gfc_match_char ('/') == MATCH_YES)
424 if (gfc_match_char (',') == MATCH_NO)
431 gfc_syntax_error (ST_DATA);
432 gfc_free_data_all (gfc_current_ns);
437 /* Matches an old style initialization. */
440 match_old_style_init (const char *name)
447 /* Set up data structure to hold initializers. */
448 gfc_find_sym_tree (name, NULL, 0, &st);
451 newdata = gfc_get_data ();
452 newdata->var = gfc_get_data_variable ();
453 newdata->var->expr = gfc_get_variable_expr (st);
454 newdata->where = gfc_current_locus;
456 /* Match initial value list. This also eats the terminal '/'. */
457 m = top_val_list (newdata);
466 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
471 /* Mark the variable as having appeared in a data statement. */
472 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
478 /* Chain in namespace list of DATA initializers. */
479 newdata->next = gfc_current_ns->data;
480 gfc_current_ns->data = newdata;
486 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
487 we are matching a DATA statement and are therefore issuing an error
488 if we encounter something unexpected, if not, we're trying to match
489 an old-style initialization expression of the form INTEGER I /2/. */
492 gfc_match_data (void)
497 gfc_set_in_match_data (true);
501 new = gfc_get_data ();
502 new->where = gfc_current_locus;
504 m = top_var_list (new);
508 m = top_val_list (new);
512 new->next = gfc_current_ns->data;
513 gfc_current_ns->data = new;
515 if (gfc_match_eos () == MATCH_YES)
518 gfc_match_char (','); /* Optional comma */
521 gfc_set_in_match_data (false);
525 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
532 gfc_set_in_match_data (false);
538 /************************ Declaration statements *********************/
540 /* Match an intent specification. Since this can only happen after an
541 INTENT word, a legal intent-spec must follow. */
544 match_intent_spec (void)
547 if (gfc_match (" ( in out )") == MATCH_YES)
549 if (gfc_match (" ( in )") == MATCH_YES)
551 if (gfc_match (" ( out )") == MATCH_YES)
554 gfc_error ("Bad INTENT specification at %C");
555 return INTENT_UNKNOWN;
559 /* Matches a character length specification, which is either a
560 specification expression or a '*'. */
563 char_len_param_value (gfc_expr **expr)
565 if (gfc_match_char ('*') == MATCH_YES)
571 return gfc_match_expr (expr);
575 /* A character length is a '*' followed by a literal integer or a
576 char_len_param_value in parenthesis. */
579 match_char_length (gfc_expr **expr)
584 m = gfc_match_char ('*');
588 m = gfc_match_small_literal_int (&length, NULL);
589 if (m == MATCH_ERROR)
594 *expr = gfc_int_expr (length);
598 if (gfc_match_char ('(') == MATCH_NO)
601 m = char_len_param_value (expr);
602 if (m == MATCH_ERROR)
607 if (gfc_match_char (')') == MATCH_NO)
609 gfc_free_expr (*expr);
617 gfc_error ("Syntax error in character length specification at %C");
622 /* Special subroutine for finding a symbol. Check if the name is found
623 in the current name space. If not, and we're compiling a function or
624 subroutine and the parent compilation unit is an interface, then check
625 to see if the name we've been given is the name of the interface
626 (located in another namespace). */
629 find_special (const char *name, gfc_symbol **result)
634 i = gfc_get_symbol (name, NULL, result);
638 if (gfc_current_state () != COMP_SUBROUTINE
639 && gfc_current_state () != COMP_FUNCTION)
642 s = gfc_state_stack->previous;
646 if (s->state != COMP_INTERFACE)
649 goto end; /* Nameless interface. */
651 if (strcmp (name, s->sym->name) == 0)
662 /* Special subroutine for getting a symbol node associated with a
663 procedure name, used in SUBROUTINE and FUNCTION statements. The
664 symbol is created in the parent using with symtree node in the
665 child unit pointing to the symbol. If the current namespace has no
666 parent, then the symbol is just created in the current unit. */
669 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
675 /* Module functions have to be left in their own namespace because
676 they have potentially (almost certainly!) already been referenced.
677 In this sense, they are rather like external functions. This is
678 fixed up in resolve.c(resolve_entries), where the symbol name-
679 space is set to point to the master function, so that the fake
680 result mechanism can work. */
681 if (module_fcn_entry)
683 /* Present if entry is declared to be a module procedure. */
684 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
686 rc = gfc_get_symbol (name, NULL, result);
689 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
692 gfc_current_ns->refs++;
694 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
696 /* Trap another encompassed procedure with the same name. All
697 these conditions are necessary to avoid picking up an entry
698 whose name clashes with that of the encompassing procedure;
699 this is handled using gsymbols to register unique,globally
701 if (sym->attr.flavor != 0
702 && sym->attr.proc != 0
703 && (sym->attr.subroutine || sym->attr.function)
704 && sym->attr.if_source != IFSRC_UNKNOWN)
705 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
706 name, &sym->declared_at);
708 /* Trap a procedure with a name the same as interface in the
709 encompassing scope. */
710 if (sym->attr.generic != 0
711 && (sym->attr.subroutine || sym->attr.function)
712 && !sym->attr.mod_proc)
713 gfc_error_now ("Name '%s' at %C is already defined"
714 " as a generic interface at %L",
715 name, &sym->declared_at);
717 /* Trap declarations of attributes in encompassing scope. The
718 signature for this is that ts.kind is set. Legitimate
719 references only set ts.type. */
720 if (sym->ts.kind != 0
721 && !sym->attr.implicit_type
722 && sym->attr.proc == 0
723 && gfc_current_ns->parent != NULL
724 && sym->attr.access == 0
725 && !module_fcn_entry)
726 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
727 "and must not have attributes declared at %L",
728 name, &sym->declared_at);
731 if (gfc_current_ns->parent == NULL || *result == NULL)
734 /* Module function entries will already have a symtree in
735 the current namespace but will need one at module level. */
736 if (module_fcn_entry)
738 /* Present if entry is declared to be a module procedure. */
739 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
741 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
744 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
749 /* See if the procedure should be a module procedure. */
751 if (((sym->ns->proc_name != NULL
752 && sym->ns->proc_name->attr.flavor == FL_MODULE
753 && sym->attr.proc != PROC_MODULE)
754 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
755 && gfc_add_procedure (&sym->attr, PROC_MODULE,
756 sym->name, NULL) == FAILURE)
763 /* Verify that the given symbol representing a parameter is C
764 interoperable, by checking to see if it was marked as such after
765 its declaration. If the given symbol is not interoperable, a
766 warning is reported, thus removing the need to return the status to
767 the calling function. The standard does not require the user use
768 one of the iso_c_binding named constants to declare an
769 interoperable parameter, but we can't be sure if the param is C
770 interop or not if the user doesn't. For example, integer(4) may be
771 legal Fortran, but doesn't have meaning in C. It may interop with
772 a number of the C types, which causes a problem because the
773 compiler can't know which one. This code is almost certainly not
774 portable, and the user will get what they deserve if the C type
775 across platforms isn't always interoperable with integer(4). If
776 the user had used something like integer(c_int) or integer(c_long),
777 the compiler could have automatically handled the varying sizes
781 verify_c_interop_param (gfc_symbol *sym)
783 int is_c_interop = 0;
784 try retval = SUCCESS;
786 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
787 Don't repeat the checks here. */
788 if (sym->attr.implicit_type)
791 /* For subroutines or functions that are passed to a BIND(C) procedure,
792 they're interoperable if they're BIND(C) and their params are all
794 if (sym->attr.flavor == FL_PROCEDURE)
796 if (sym->attr.is_bind_c == 0)
798 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
799 "attribute to be C interoperable", sym->name,
800 &(sym->declared_at));
806 if (sym->attr.is_c_interop == 1)
807 /* We've already checked this procedure; don't check it again. */
810 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
815 /* See if we've stored a reference to a procedure that owns sym. */
816 if (sym->ns != NULL && sym->ns->proc_name != NULL)
818 if (sym->ns->proc_name->attr.is_bind_c == 1)
821 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
824 if (is_c_interop != 1)
826 /* Make personalized messages to give better feedback. */
827 if (sym->ts.type == BT_DERIVED)
828 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
829 " procedure '%s' but is not C interoperable "
830 "because derived type '%s' is not C interoperable",
831 sym->name, &(sym->declared_at),
832 sym->ns->proc_name->name,
833 sym->ts.derived->name);
835 gfc_warning ("Variable '%s' at %L is a parameter to the "
836 "BIND(C) procedure '%s' but may not be C "
838 sym->name, &(sym->declared_at),
839 sym->ns->proc_name->name);
842 /* We have to make sure that any param to a bind(c) routine does
843 not have the allocatable, pointer, or optional attributes,
844 according to J3/04-007, section 5.1. */
845 if (sym->attr.allocatable == 1)
847 gfc_error ("Variable '%s' at %L cannot have the "
848 "ALLOCATABLE attribute because procedure '%s'"
849 " is BIND(C)", sym->name, &(sym->declared_at),
850 sym->ns->proc_name->name);
854 if (sym->attr.pointer == 1)
856 gfc_error ("Variable '%s' at %L cannot have the "
857 "POINTER attribute because procedure '%s'"
858 " is BIND(C)", sym->name, &(sym->declared_at),
859 sym->ns->proc_name->name);
863 if (sym->attr.optional == 1)
865 gfc_error ("Variable '%s' at %L cannot have the "
866 "OPTIONAL attribute because procedure '%s'"
867 " is BIND(C)", sym->name, &(sym->declared_at),
868 sym->ns->proc_name->name);
872 /* Make sure that if it has the dimension attribute, that it is
873 either assumed size or explicit shape. */
876 if (sym->as->type == AS_ASSUMED_SHAPE)
878 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
879 "argument to the procedure '%s' at %L because "
880 "the procedure is BIND(C)", sym->name,
881 &(sym->declared_at), sym->ns->proc_name->name,
882 &(sym->ns->proc_name->declared_at));
886 if (sym->as->type == AS_DEFERRED)
888 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
889 "argument to the procedure '%s' at %L because "
890 "the procedure is BIND(C)", sym->name,
891 &(sym->declared_at), sym->ns->proc_name->name,
892 &(sym->ns->proc_name->declared_at));
903 /* Function called by variable_decl() that adds a name to the symbol table. */
906 build_sym (const char *name, gfc_charlen *cl,
907 gfc_array_spec **as, locus *var_locus)
909 symbol_attribute attr;
912 if (gfc_get_symbol (name, NULL, &sym))
915 /* Start updating the symbol table. Add basic type attribute if present. */
916 if (current_ts.type != BT_UNKNOWN
917 && (sym->attr.implicit_type == 0
918 || !gfc_compare_types (&sym->ts, ¤t_ts))
919 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
922 if (sym->ts.type == BT_CHARACTER)
925 /* Add dimension attribute if present. */
926 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
930 /* Add attribute to symbol. The copy is so that we can reset the
931 dimension attribute. */
935 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
938 /* Finish any work that may need to be done for the binding label,
939 if it's a bind(c). The bind(c) attr is found before the symbol
940 is made, and before the symbol name (for data decls), so the
941 current_ts is holding the binding label, or nothing if the
942 name= attr wasn't given. Therefore, test here if we're dealing
943 with a bind(c) and make sure the binding label is set correctly. */
944 if (sym->attr.is_bind_c == 1)
946 if (sym->binding_label[0] == '\0')
948 /* Here, we're not checking the numIdents (the last param).
949 This could be an error we're letting slip through! */
950 if (set_binding_label (sym->binding_label, sym->name, 1) == FAILURE)
955 /* See if we know we're in a common block, and if it's a bind(c)
956 common then we need to make sure we're an interoperable type. */
957 if (sym->attr.in_common == 1)
959 /* Test the common block object. */
960 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
961 && sym->ts.is_c_interop != 1)
963 gfc_error_now ("Variable '%s' in common block '%s' at %C "
964 "must be declared with a C interoperable "
965 "kind since common block '%s' is BIND(C)",
966 sym->name, sym->common_block->name,
967 sym->common_block->name);
972 sym->attr.implied_index = 0;
978 /* Set character constant to the given length. The constant will be padded or
982 gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
987 gcc_assert (expr->expr_type == EXPR_CONSTANT);
988 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
990 slen = expr->value.character.length;
993 s = gfc_getmem (len + 1);
994 memcpy (s, expr->value.character.string, MIN (len, slen));
996 memset (&s[slen], ' ', len - slen);
998 if (gfc_option.warn_character_truncation && slen > len)
999 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1000 "(%d/%d)", &expr->where, slen, len);
1002 /* Apply the standard by 'hand' otherwise it gets cleared for
1004 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
1005 gfc_error_now ("The CHARACTER elements of the array constructor "
1006 "at %L must have the same length (%d/%d)",
1007 &expr->where, slen, len);
1010 gfc_free (expr->value.character.string);
1011 expr->value.character.string = s;
1012 expr->value.character.length = len;
1017 /* Function to create and update the enumerator history
1018 using the information passed as arguments.
1019 Pointer "max_enum" is also updated, to point to
1020 enum history node containing largest initializer.
1022 SYM points to the symbol node of enumerator.
1023 INIT points to its enumerator value. */
1026 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1028 enumerator_history *new_enum_history;
1029 gcc_assert (sym != NULL && init != NULL);
1031 new_enum_history = gfc_getmem (sizeof (enumerator_history));
1033 new_enum_history->sym = sym;
1034 new_enum_history->initializer = init;
1035 new_enum_history->next = NULL;
1037 if (enum_history == NULL)
1039 enum_history = new_enum_history;
1040 max_enum = enum_history;
1044 new_enum_history->next = enum_history;
1045 enum_history = new_enum_history;
1047 if (mpz_cmp (max_enum->initializer->value.integer,
1048 new_enum_history->initializer->value.integer) < 0)
1049 max_enum = new_enum_history;
1054 /* Function to free enum kind history. */
1057 gfc_free_enum_history (void)
1059 enumerator_history *current = enum_history;
1060 enumerator_history *next;
1062 while (current != NULL)
1064 next = current->next;
1069 enum_history = NULL;
1073 /* Function called by variable_decl() that adds an initialization
1074 expression to a symbol. */
1077 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1079 symbol_attribute attr;
1084 if (find_special (name, &sym))
1089 /* If this symbol is confirming an implicit parameter type,
1090 then an initialization expression is not allowed. */
1091 if (attr.flavor == FL_PARAMETER
1092 && sym->value != NULL
1095 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1104 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
1111 /* An initializer is required for PARAMETER declarations. */
1112 if (attr.flavor == FL_PARAMETER)
1114 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1120 /* If a variable appears in a DATA block, it cannot have an
1124 gfc_error ("Variable '%s' at %C with an initializer already "
1125 "appears in a DATA statement", sym->name);
1129 /* Check if the assignment can happen. This has to be put off
1130 until later for a derived type variable. */
1131 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1132 && gfc_check_assign_symbol (sym, init) == FAILURE)
1135 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1137 /* Update symbol character length according initializer. */
1138 if (sym->ts.cl->length == NULL)
1140 /* If there are multiple CHARACTER variables declared on the
1141 same line, we don't want them to share the same length. */
1142 sym->ts.cl = gfc_get_charlen ();
1143 sym->ts.cl->next = gfc_current_ns->cl_list;
1144 gfc_current_ns->cl_list = sym->ts.cl;
1146 if (sym->attr.flavor == FL_PARAMETER
1147 && init->expr_type == EXPR_ARRAY)
1148 sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
1150 /* Update initializer character length according symbol. */
1151 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1153 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1154 gfc_constructor * p;
1156 if (init->expr_type == EXPR_CONSTANT)
1157 gfc_set_constant_character_len (len, init, false);
1158 else if (init->expr_type == EXPR_ARRAY)
1160 /* Build a new charlen to prevent simplification from
1161 deleting the length before it is resolved. */
1162 init->ts.cl = gfc_get_charlen ();
1163 init->ts.cl->next = gfc_current_ns->cl_list;
1164 gfc_current_ns->cl_list = sym->ts.cl;
1165 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
1167 for (p = init->value.constructor; p; p = p->next)
1168 gfc_set_constant_character_len (len, p->expr, false);
1173 /* Need to check if the expression we initialized this
1174 to was one of the iso_c_binding named constants. If so,
1175 and we're a parameter (constant), let it be iso_c.
1177 integer(c_int), parameter :: my_int = c_int
1178 integer(my_int) :: my_int_2
1179 If we mark my_int as iso_c (since we can see it's value
1180 is equal to one of the named constants), then my_int_2
1181 will be considered C interoperable. */
1182 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1184 sym->ts.is_iso_c |= init->ts.is_iso_c;
1185 sym->ts.is_c_interop |= init->ts.is_c_interop;
1186 /* attr bits needed for module files. */
1187 sym->attr.is_iso_c |= init->ts.is_iso_c;
1188 sym->attr.is_c_interop |= init->ts.is_c_interop;
1189 if (init->ts.is_iso_c)
1190 sym->ts.f90_type = init->ts.f90_type;
1193 /* Add initializer. Make sure we keep the ranks sane. */
1194 if (sym->attr.dimension && init->rank == 0)
1200 if (sym->attr.flavor == FL_PARAMETER
1201 && init->expr_type == EXPR_CONSTANT
1202 && spec_size (sym->as, &size) == SUCCESS
1203 && mpz_cmp_si (size, 0) > 0)
1205 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1208 array->value.constructor = c = NULL;
1209 for (n = 0; n < (int)mpz_get_si (size); n++)
1211 if (array->value.constructor == NULL)
1213 array->value.constructor = c = gfc_get_constructor ();
1218 c->next = gfc_get_constructor ();
1220 c->expr = gfc_copy_expr (init);
1224 array->shape = gfc_get_shape (sym->as->rank);
1225 for (n = 0; n < sym->as->rank; n++)
1226 spec_dimen_size (sym->as, n, &array->shape[n]);
1231 init->rank = sym->as->rank;
1235 if (sym->attr.save == SAVE_NONE)
1236 sym->attr.save = SAVE_IMPLICIT;
1244 /* Function called by variable_decl() that adds a name to a structure
1248 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1249 gfc_array_spec **as)
1253 /* If the current symbol is of the same derived type that we're
1254 constructing, it must have the pointer attribute. */
1255 if (current_ts.type == BT_DERIVED
1256 && current_ts.derived == gfc_current_block ()
1257 && current_attr.pointer == 0)
1259 gfc_error ("Component at %C must have the POINTER attribute");
1263 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1265 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1267 gfc_error ("Array component of structure at %C must have explicit "
1268 "or deferred shape");
1273 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1278 gfc_set_component_attr (c, ¤t_attr);
1280 c->initializer = *init;
1288 /* Check array components. */
1293 gfc_error ("Allocatable component at %C must be an array");
1302 if (c->as->type != AS_DEFERRED)
1304 gfc_error ("Pointer array component of structure at %C must have a "
1309 else if (c->allocatable)
1311 if (c->as->type != AS_DEFERRED)
1313 gfc_error ("Allocatable component of structure at %C must have a "
1320 if (c->as->type != AS_EXPLICIT)
1322 gfc_error ("Array component of structure at %C must have an "
1332 /* Match a 'NULL()', and possibly take care of some side effects. */
1335 gfc_match_null (gfc_expr **result)
1341 m = gfc_match (" null ( )");
1345 /* The NULL symbol now has to be/become an intrinsic function. */
1346 if (gfc_get_symbol ("null", NULL, &sym))
1348 gfc_error ("NULL() initialization at %C is ambiguous");
1352 gfc_intrinsic_symbol (sym);
1354 if (sym->attr.proc != PROC_INTRINSIC
1355 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1356 sym->name, NULL) == FAILURE
1357 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1360 e = gfc_get_expr ();
1361 e->where = gfc_current_locus;
1362 e->expr_type = EXPR_NULL;
1363 e->ts.type = BT_UNKNOWN;
1371 /* Match a variable name with an optional initializer. When this
1372 subroutine is called, a variable is expected to be parsed next.
1373 Depending on what is happening at the moment, updates either the
1374 symbol table or the current interface. */
1377 variable_decl (int elem)
1379 char name[GFC_MAX_SYMBOL_LEN + 1];
1380 gfc_expr *initializer, *char_len;
1382 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1393 old_locus = gfc_current_locus;
1395 /* When we get here, we've just matched a list of attributes and
1396 maybe a type and a double colon. The next thing we expect to see
1397 is the name of the symbol. */
1398 m = gfc_match_name (name);
1402 var_locus = gfc_current_locus;
1404 /* Now we could see the optional array spec. or character length. */
1405 m = gfc_match_array_spec (&as);
1406 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1407 cp_as = gfc_copy_array_spec (as);
1408 else if (m == MATCH_ERROR)
1412 as = gfc_copy_array_spec (current_as);
1417 if (current_ts.type == BT_CHARACTER)
1419 switch (match_char_length (&char_len))
1422 cl = gfc_get_charlen ();
1423 cl->next = gfc_current_ns->cl_list;
1424 gfc_current_ns->cl_list = cl;
1426 cl->length = char_len;
1429 /* Non-constant lengths need to be copied after the first
1432 if (elem > 1 && current_ts.cl->length
1433 && current_ts.cl->length->expr_type != EXPR_CONSTANT)
1435 cl = gfc_get_charlen ();
1436 cl->next = gfc_current_ns->cl_list;
1437 gfc_current_ns->cl_list = cl;
1438 cl->length = gfc_copy_expr (current_ts.cl->length);
1450 /* If this symbol has already shown up in a Cray Pointer declaration,
1451 then we want to set the type & bail out. */
1452 if (gfc_option.flag_cray_pointer)
1454 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1455 if (sym != NULL && sym->attr.cray_pointee)
1457 sym->ts.type = current_ts.type;
1458 sym->ts.kind = current_ts.kind;
1460 sym->ts.derived = current_ts.derived;
1461 sym->ts.is_c_interop = current_ts.is_c_interop;
1462 sym->ts.is_iso_c = current_ts.is_iso_c;
1465 /* Check to see if we have an array specification. */
1468 if (sym->as != NULL)
1470 gfc_error ("Duplicate array spec for Cray pointee at %C");
1471 gfc_free_array_spec (cp_as);
1477 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1478 gfc_internal_error ("Couldn't set pointee array spec.");
1480 /* Fix the array spec. */
1481 m = gfc_mod_pointee_as (sym->as);
1482 if (m == MATCH_ERROR)
1490 gfc_free_array_spec (cp_as);
1495 /* OK, we've successfully matched the declaration. Now put the
1496 symbol in the current namespace, because it might be used in the
1497 optional initialization expression for this symbol, e.g. this is
1500 integer, parameter :: i = huge(i)
1502 This is only true for parameters or variables of a basic type.
1503 For components of derived types, it is not true, so we don't
1504 create a symbol for those yet. If we fail to create the symbol,
1506 if (gfc_current_state () != COMP_DERIVED
1507 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1513 /* An interface body specifies all of the procedure's
1514 characteristics and these shall be consistent with those
1515 specified in the procedure definition, except that the interface
1516 may specify a procedure that is not pure if the procedure is
1517 defined to be pure(12.3.2). */
1518 if (current_ts.type == BT_DERIVED
1519 && gfc_current_ns->proc_name
1520 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1521 && current_ts.derived->ns != gfc_current_ns
1522 && !gfc_current_ns->has_import_set)
1524 gfc_error ("the type of '%s' at %C has not been declared within the "
1530 /* In functions that have a RESULT variable defined, the function
1531 name always refers to function calls. Therefore, the name is
1532 not allowed to appear in specification statements. */
1533 if (gfc_current_state () == COMP_FUNCTION
1534 && gfc_current_block () != NULL
1535 && gfc_current_block ()->result != NULL
1536 && gfc_current_block ()->result != gfc_current_block ()
1537 && strcmp (gfc_current_block ()->name, name) == 0)
1539 gfc_error ("Function name '%s' not allowed at %C", name);
1544 /* We allow old-style initializations of the form
1545 integer i /2/, j(4) /3*3, 1/
1546 (if no colon has been seen). These are different from data
1547 statements in that initializers are only allowed to apply to the
1548 variable immediately preceding, i.e.
1550 is not allowed. Therefore we have to do some work manually, that
1551 could otherwise be left to the matchers for DATA statements. */
1553 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1555 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1556 "initialization at %C") == FAILURE)
1559 return match_old_style_init (name);
1562 /* The double colon must be present in order to have initializers.
1563 Otherwise the statement is ambiguous with an assignment statement. */
1566 if (gfc_match (" =>") == MATCH_YES)
1568 if (!current_attr.pointer)
1570 gfc_error ("Initialization at %C isn't for a pointer variable");
1575 m = gfc_match_null (&initializer);
1578 gfc_error ("Pointer initialization requires a NULL() at %C");
1582 if (gfc_pure (NULL))
1584 gfc_error ("Initialization of pointer at %C is not allowed in "
1585 "a PURE procedure");
1593 else if (gfc_match_char ('=') == MATCH_YES)
1595 if (current_attr.pointer)
1597 gfc_error ("Pointer initialization at %C requires '=>', "
1603 m = gfc_match_init_expr (&initializer);
1606 gfc_error ("Expected an initialization expression at %C");
1610 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1612 gfc_error ("Initialization of variable at %C is not allowed in "
1613 "a PURE procedure");
1622 if (initializer != NULL && current_attr.allocatable
1623 && gfc_current_state () == COMP_DERIVED)
1625 gfc_error ("Initialization of allocatable component at %C is not "
1631 /* Add the initializer. Note that it is fine if initializer is
1632 NULL here, because we sometimes also need to check if a
1633 declaration *must* have an initialization expression. */
1634 if (gfc_current_state () != COMP_DERIVED)
1635 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1638 if (current_ts.type == BT_DERIVED
1639 && !current_attr.pointer && !initializer)
1640 initializer = gfc_default_initializer (¤t_ts);
1641 t = build_struct (name, cl, &initializer, &as);
1644 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1647 /* Free stuff up and return. */
1648 gfc_free_expr (initializer);
1649 gfc_free_array_spec (as);
1655 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1656 This assumes that the byte size is equal to the kind number for
1657 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1660 gfc_match_old_kind_spec (gfc_typespec *ts)
1665 if (gfc_match_char ('*') != MATCH_YES)
1668 m = gfc_match_small_literal_int (&ts->kind, NULL);
1672 original_kind = ts->kind;
1674 /* Massage the kind numbers for complex types. */
1675 if (ts->type == BT_COMPLEX)
1679 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1680 gfc_basic_typename (ts->type), original_kind);
1686 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1688 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1689 gfc_basic_typename (ts->type), original_kind);
1693 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1694 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1701 /* Match a kind specification. Since kinds are generally optional, we
1702 usually return MATCH_NO if something goes wrong. If a "kind="
1703 string is found, then we know we have an error. */
1706 gfc_match_kind_spec (gfc_typespec *ts)
1716 where = gfc_current_locus;
1718 if (gfc_match_char ('(') == MATCH_NO)
1721 /* Also gobbles optional text. */
1722 if (gfc_match (" kind = ") == MATCH_YES)
1725 n = gfc_match_init_expr (&e);
1727 gfc_error ("Expected initialization expression at %C");
1733 gfc_error ("Expected scalar initialization expression at %C");
1738 msg = gfc_extract_int (e, &ts->kind);
1746 /* Before throwing away the expression, let's see if we had a
1747 C interoperable kind (and store the fact). */
1748 if (e->ts.is_c_interop == 1)
1750 /* Mark this as c interoperable if being declared with one
1751 of the named constants from iso_c_binding. */
1752 ts->is_c_interop = e->ts.is_iso_c;
1753 ts->f90_type = e->ts.f90_type;
1759 /* Ignore errors to this point, if we've gotten here. This means
1760 we ignore the m=MATCH_ERROR from above. */
1761 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1763 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1764 gfc_basic_typename (ts->type));
1767 else if (gfc_match_char (')') != MATCH_YES)
1769 gfc_error ("Missing right parenthesis at %C");
1773 /* All tests passed. */
1776 if(m == MATCH_ERROR)
1777 gfc_current_locus = where;
1779 /* Return what we know from the test(s). */
1784 gfc_current_locus = where;
1789 /* Match the various kind/length specifications in a CHARACTER
1790 declaration. We don't return MATCH_NO. */
1793 match_char_spec (gfc_typespec *ts)
1795 int kind, seen_length;
1799 gfc_expr *kind_expr = NULL;
1800 kind = gfc_default_character_kind;
1804 /* Try the old-style specification first. */
1805 old_char_selector = 0;
1807 m = match_char_length (&len);
1811 old_char_selector = 1;
1816 m = gfc_match_char ('(');
1819 m = MATCH_YES; /* Character without length is a single char. */
1823 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
1824 if (gfc_match (" kind =") == MATCH_YES)
1826 m = gfc_match_small_int_expr(&kind, &kind_expr);
1828 if (m == MATCH_ERROR)
1833 if (gfc_match (" , len =") == MATCH_NO)
1836 m = char_len_param_value (&len);
1839 if (m == MATCH_ERROR)
1846 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
1847 if (gfc_match (" len =") == MATCH_YES)
1849 m = char_len_param_value (&len);
1852 if (m == MATCH_ERROR)
1856 if (gfc_match_char (')') == MATCH_YES)
1859 if (gfc_match (" , kind =") != MATCH_YES)
1862 gfc_match_small_int_expr(&kind, &kind_expr);
1864 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1866 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1873 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
1874 m = char_len_param_value (&len);
1877 if (m == MATCH_ERROR)
1881 m = gfc_match_char (')');
1885 if (gfc_match_char (',') != MATCH_YES)
1888 gfc_match (" kind ="); /* Gobble optional text. */
1890 m = gfc_match_small_int_expr(&kind, &kind_expr);
1891 if (m == MATCH_ERROR)
1897 /* Require a right-paren at this point. */
1898 m = gfc_match_char (')');
1903 gfc_error ("Syntax error in CHARACTER declaration at %C");
1905 gfc_free_expr (len);
1909 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1911 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1915 if (seen_length == 1 && len != NULL
1916 && len->ts.type != BT_INTEGER && len->ts.type != BT_UNKNOWN)
1918 gfc_error ("Expression at %C must be of INTEGER type");
1924 gfc_free_expr (len);
1925 gfc_free_expr (kind_expr);
1929 /* Do some final massaging of the length values. */
1930 cl = gfc_get_charlen ();
1931 cl->next = gfc_current_ns->cl_list;
1932 gfc_current_ns->cl_list = cl;
1934 if (seen_length == 0)
1935 cl->length = gfc_int_expr (1);
1942 /* We have to know if it was a c interoperable kind so we can
1943 do accurate type checking of bind(c) procs, etc. */
1944 if (kind_expr != NULL)
1946 /* Mark this as c interoperable if being declared with one
1947 of the named constants from iso_c_binding. */
1948 ts->is_c_interop = kind_expr->ts.is_iso_c;
1949 gfc_free_expr (kind_expr);
1951 else if (len != NULL)
1953 /* Here, we might have parsed something such as:
1955 In this case, the parsing code above grabs the c_char when
1956 looking for the length (line 1690, roughly). it's the last
1957 testcase for parsing the kind params of a character variable.
1958 However, it's not actually the length. this seems like it
1960 To see if the user used a C interop kind, test the expr
1961 of the so called length, and see if it's C interoperable. */
1962 ts->is_c_interop = len->ts.is_iso_c;
1969 /* Matches a type specification. If successful, sets the ts structure
1970 to the matched specification. This is necessary for FUNCTION and
1971 IMPLICIT statements.
1973 If implicit_flag is nonzero, then we don't check for the optional
1974 kind specification. Not doing so is needed for matching an IMPLICIT
1975 statement correctly. */
1978 match_type_spec (gfc_typespec *ts, int implicit_flag)
1980 char name[GFC_MAX_SYMBOL_LEN + 1];
1987 /* Clear the current binding label, in case one is given. */
1988 curr_binding_label[0] = '\0';
1990 if (gfc_match (" byte") == MATCH_YES)
1992 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
1996 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
1998 gfc_error ("BYTE type used at %C "
1999 "is not available on the target machine");
2003 ts->type = BT_INTEGER;
2008 if (gfc_match (" integer") == MATCH_YES)
2010 ts->type = BT_INTEGER;
2011 ts->kind = gfc_default_integer_kind;
2015 if (gfc_match (" character") == MATCH_YES)
2017 ts->type = BT_CHARACTER;
2018 if (implicit_flag == 0)
2019 return match_char_spec (ts);
2024 if (gfc_match (" real") == MATCH_YES)
2027 ts->kind = gfc_default_real_kind;
2031 if (gfc_match (" double precision") == MATCH_YES)
2034 ts->kind = gfc_default_double_kind;
2038 if (gfc_match (" complex") == MATCH_YES)
2040 ts->type = BT_COMPLEX;
2041 ts->kind = gfc_default_complex_kind;
2045 if (gfc_match (" double complex") == MATCH_YES)
2047 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2048 "conform to the Fortran 95 standard") == FAILURE)
2051 ts->type = BT_COMPLEX;
2052 ts->kind = gfc_default_double_kind;
2056 if (gfc_match (" logical") == MATCH_YES)
2058 ts->type = BT_LOGICAL;
2059 ts->kind = gfc_default_logical_kind;
2063 m = gfc_match (" type ( %n )", name);
2067 /* Search for the name but allow the components to be defined later. */
2068 if (gfc_get_ha_symbol (name, &sym))
2070 gfc_error ("Type name '%s' at %C is ambiguous", name);
2074 if (sym->attr.flavor != FL_DERIVED
2075 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2078 ts->type = BT_DERIVED;
2085 /* For all types except double, derived and character, look for an
2086 optional kind specifier. MATCH_NO is actually OK at this point. */
2087 if (implicit_flag == 1)
2090 if (gfc_current_form == FORM_FREE)
2092 c = gfc_peek_char();
2093 if (!gfc_is_whitespace(c) && c != '*' && c != '('
2094 && c != ':' && c != ',')
2098 m = gfc_match_kind_spec (ts);
2099 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2100 m = gfc_match_old_kind_spec (ts);
2103 m = MATCH_YES; /* No kind specifier found. */
2109 /* Match an IMPLICIT NONE statement. Actually, this statement is
2110 already matched in parse.c, or we would not end up here in the
2111 first place. So the only thing we need to check, is if there is
2112 trailing garbage. If not, the match is successful. */
2115 gfc_match_implicit_none (void)
2117 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2121 /* Match the letter range(s) of an IMPLICIT statement. */
2124 match_implicit_range (void)
2126 int c, c1, c2, inner;
2129 cur_loc = gfc_current_locus;
2131 gfc_gobble_whitespace ();
2132 c = gfc_next_char ();
2135 gfc_error ("Missing character range in IMPLICIT at %C");
2142 gfc_gobble_whitespace ();
2143 c1 = gfc_next_char ();
2147 gfc_gobble_whitespace ();
2148 c = gfc_next_char ();
2153 inner = 0; /* Fall through. */
2160 gfc_gobble_whitespace ();
2161 c2 = gfc_next_char ();
2165 gfc_gobble_whitespace ();
2166 c = gfc_next_char ();
2168 if ((c != ',') && (c != ')'))
2181 gfc_error ("Letters must be in alphabetic order in "
2182 "IMPLICIT statement at %C");
2186 /* See if we can add the newly matched range to the pending
2187 implicits from this IMPLICIT statement. We do not check for
2188 conflicts with whatever earlier IMPLICIT statements may have
2189 set. This is done when we've successfully finished matching
2191 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2198 gfc_syntax_error (ST_IMPLICIT);
2200 gfc_current_locus = cur_loc;
2205 /* Match an IMPLICIT statement, storing the types for
2206 gfc_set_implicit() if the statement is accepted by the parser.
2207 There is a strange looking, but legal syntactic construction
2208 possible. It looks like:
2210 IMPLICIT INTEGER (a-b) (c-d)
2212 This is legal if "a-b" is a constant expression that happens to
2213 equal one of the legal kinds for integers. The real problem
2214 happens with an implicit specification that looks like:
2216 IMPLICIT INTEGER (a-b)
2218 In this case, a typespec matcher that is "greedy" (as most of the
2219 matchers are) gobbles the character range as a kindspec, leaving
2220 nothing left. We therefore have to go a bit more slowly in the
2221 matching process by inhibiting the kindspec checking during
2222 typespec matching and checking for a kind later. */
2225 gfc_match_implicit (void)
2232 /* We don't allow empty implicit statements. */
2233 if (gfc_match_eos () == MATCH_YES)
2235 gfc_error ("Empty IMPLICIT statement at %C");
2241 /* First cleanup. */
2242 gfc_clear_new_implicit ();
2244 /* A basic type is mandatory here. */
2245 m = match_type_spec (&ts, 1);
2246 if (m == MATCH_ERROR)
2251 cur_loc = gfc_current_locus;
2252 m = match_implicit_range ();
2256 /* We may have <TYPE> (<RANGE>). */
2257 gfc_gobble_whitespace ();
2258 c = gfc_next_char ();
2259 if ((c == '\n') || (c == ','))
2261 /* Check for CHARACTER with no length parameter. */
2262 if (ts.type == BT_CHARACTER && !ts.cl)
2264 ts.kind = gfc_default_character_kind;
2265 ts.cl = gfc_get_charlen ();
2266 ts.cl->next = gfc_current_ns->cl_list;
2267 gfc_current_ns->cl_list = ts.cl;
2268 ts.cl->length = gfc_int_expr (1);
2271 /* Record the Successful match. */
2272 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2277 gfc_current_locus = cur_loc;
2280 /* Discard the (incorrectly) matched range. */
2281 gfc_clear_new_implicit ();
2283 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2284 if (ts.type == BT_CHARACTER)
2285 m = match_char_spec (&ts);
2288 m = gfc_match_kind_spec (&ts);
2291 m = gfc_match_old_kind_spec (&ts);
2292 if (m == MATCH_ERROR)
2298 if (m == MATCH_ERROR)
2301 m = match_implicit_range ();
2302 if (m == MATCH_ERROR)
2307 gfc_gobble_whitespace ();
2308 c = gfc_next_char ();
2309 if ((c != '\n') && (c != ','))
2312 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2320 gfc_syntax_error (ST_IMPLICIT);
2328 gfc_match_import (void)
2330 char name[GFC_MAX_SYMBOL_LEN + 1];
2335 if (gfc_current_ns->proc_name == NULL
2336 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2338 gfc_error ("IMPORT statement at %C only permitted in "
2339 "an INTERFACE body");
2343 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2347 if (gfc_match_eos () == MATCH_YES)
2349 /* All host variables should be imported. */
2350 gfc_current_ns->has_import_set = 1;
2354 if (gfc_match (" ::") == MATCH_YES)
2356 if (gfc_match_eos () == MATCH_YES)
2358 gfc_error ("Expecting list of named entities at %C");
2365 m = gfc_match (" %n", name);
2369 if (gfc_current_ns->parent != NULL
2370 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2372 gfc_error ("Type name '%s' at %C is ambiguous", name);
2375 else if (gfc_current_ns->proc_name->ns->parent != NULL
2376 && gfc_find_symbol (name,
2377 gfc_current_ns->proc_name->ns->parent,
2380 gfc_error ("Type name '%s' at %C is ambiguous", name);
2386 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2387 "at %C - does not exist.", name);
2391 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2393 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2398 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2401 sym->ns = gfc_current_ns;
2413 if (gfc_match_eos () == MATCH_YES)
2415 if (gfc_match_char (',') != MATCH_YES)
2422 gfc_error ("Syntax error in IMPORT statement at %C");
2427 /* Matches an attribute specification including array specs. If
2428 successful, leaves the variables current_attr and current_as
2429 holding the specification. Also sets the colon_seen variable for
2430 later use by matchers associated with initializations.
2432 This subroutine is a little tricky in the sense that we don't know
2433 if we really have an attr-spec until we hit the double colon.
2434 Until that time, we can only return MATCH_NO. This forces us to
2435 check for duplicate specification at this level. */
2438 match_attr_spec (void)
2440 /* Modifiers that can exist in a type statement. */
2442 { GFC_DECL_BEGIN = 0,
2443 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2444 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2445 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2446 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2447 DECL_IS_BIND_C, DECL_COLON, DECL_NONE,
2448 GFC_DECL_END /* Sentinel */
2452 /* GFC_DECL_END is the sentinel, index starts at 0. */
2453 #define NUM_DECL GFC_DECL_END
2455 static mstring decls[] = {
2456 minit (", allocatable", DECL_ALLOCATABLE),
2457 minit (", dimension", DECL_DIMENSION),
2458 minit (", external", DECL_EXTERNAL),
2459 minit (", intent ( in )", DECL_IN),
2460 minit (", intent ( out )", DECL_OUT),
2461 minit (", intent ( in out )", DECL_INOUT),
2462 minit (", intrinsic", DECL_INTRINSIC),
2463 minit (", optional", DECL_OPTIONAL),
2464 minit (", parameter", DECL_PARAMETER),
2465 minit (", pointer", DECL_POINTER),
2466 minit (", protected", DECL_PROTECTED),
2467 minit (", private", DECL_PRIVATE),
2468 minit (", public", DECL_PUBLIC),
2469 minit (", save", DECL_SAVE),
2470 minit (", target", DECL_TARGET),
2471 minit (", value", DECL_VALUE),
2472 minit (", volatile", DECL_VOLATILE),
2473 minit ("::", DECL_COLON),
2474 minit (NULL, DECL_NONE)
2477 locus start, seen_at[NUM_DECL];
2485 gfc_clear_attr (¤t_attr);
2486 start = gfc_current_locus;
2491 /* See if we get all of the keywords up to the final double colon. */
2492 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2497 d = (decl_types) gfc_match_strings (decls);
2501 /* See if we can find the bind(c) since all else failed.
2502 We need to skip over any whitespace and stop on the ','. */
2503 gfc_gobble_whitespace ();
2504 peek_char = gfc_peek_char ();
2505 if (peek_char == ',')
2507 /* Chomp the comma. */
2508 peek_char = gfc_next_char ();
2509 /* Try and match the bind(c). */
2510 if (gfc_match_bind_c (NULL) == MATCH_YES)
2515 if (d == DECL_NONE || d == DECL_COLON)
2519 seen_at[d] = gfc_current_locus;
2521 if (d == DECL_DIMENSION)
2523 m = gfc_match_array_spec (¤t_as);
2527 gfc_error ("Missing dimension specification at %C");
2531 if (m == MATCH_ERROR)
2536 /* No double colon, so assume that we've been looking at something
2537 else the whole time. */
2544 /* Since we've seen a double colon, we have to be looking at an
2545 attr-spec. This means that we can now issue errors. */
2546 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2551 case DECL_ALLOCATABLE:
2552 attr = "ALLOCATABLE";
2554 case DECL_DIMENSION:
2561 attr = "INTENT (IN)";
2564 attr = "INTENT (OUT)";
2567 attr = "INTENT (IN OUT)";
2569 case DECL_INTRINSIC:
2575 case DECL_PARAMETER:
2581 case DECL_PROTECTED:
2596 case DECL_IS_BIND_C:
2606 attr = NULL; /* This shouldn't happen. */
2609 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2614 /* Now that we've dealt with duplicate attributes, add the attributes
2615 to the current attribute. */
2616 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2621 if (gfc_current_state () == COMP_DERIVED
2622 && d != DECL_DIMENSION && d != DECL_POINTER
2623 && d != DECL_COLON && d != DECL_PRIVATE
2624 && d != DECL_PUBLIC && d != DECL_NONE)
2626 if (d == DECL_ALLOCATABLE)
2628 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
2629 "attribute at %C in a TYPE definition")
2638 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2645 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2646 && gfc_current_state () != COMP_MODULE)
2648 if (d == DECL_PRIVATE)
2652 if (gfc_current_state () == COMP_DERIVED
2653 && gfc_state_stack->previous
2654 && gfc_state_stack->previous->state == COMP_MODULE)
2656 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
2657 "at %L in a TYPE definition", attr,
2667 gfc_error ("%s attribute at %L is not allowed outside of the "
2668 "specification part of a module", attr, &seen_at[d]);
2676 case DECL_ALLOCATABLE:
2677 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
2680 case DECL_DIMENSION:
2681 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
2685 t = gfc_add_external (¤t_attr, &seen_at[d]);
2689 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
2693 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
2697 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
2700 case DECL_INTRINSIC:
2701 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
2705 t = gfc_add_optional (¤t_attr, &seen_at[d]);
2708 case DECL_PARAMETER:
2709 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
2713 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
2716 case DECL_PROTECTED:
2717 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
2719 gfc_error ("PROTECTED at %C only allowed in specification "
2720 "part of a module");
2725 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
2730 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
2734 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
2739 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
2744 t = gfc_add_save (¤t_attr, NULL, &seen_at[d]);
2748 t = gfc_add_target (¤t_attr, &seen_at[d]);
2751 case DECL_IS_BIND_C:
2752 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
2756 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
2761 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
2765 if (gfc_notify_std (GFC_STD_F2003,
2766 "Fortran 2003: VOLATILE attribute at %C")
2770 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
2774 gfc_internal_error ("match_attr_spec(): Bad attribute");
2788 gfc_current_locus = start;
2789 gfc_free_array_spec (current_as);
2795 /* Set the binding label, dest_label, either with the binding label
2796 stored in the given gfc_typespec, ts, or if none was provided, it
2797 will be the symbol name in all lower case, as required by the draft
2798 (J3/04-007, section 15.4.1). If a binding label was given and
2799 there is more than one argument (num_idents), it is an error. */
2802 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
2804 if (curr_binding_label[0] != '\0')
2806 if (num_idents > 1 || num_idents_on_line > 1)
2808 gfc_error ("Multiple identifiers provided with "
2809 "single NAME= specifier at %C");
2813 /* Binding label given; store in temp holder til have sym. */
2814 strncpy (dest_label, curr_binding_label,
2815 strlen (curr_binding_label) + 1);
2819 /* No binding label given, and the NAME= specifier did not exist,
2820 which means there was no NAME="". */
2821 if (sym_name != NULL && has_name_equals == 0)
2822 strncpy (dest_label, sym_name, strlen (sym_name) + 1);
2829 /* Set the status of the given common block as being BIND(C) or not,
2830 depending on the given parameter, is_bind_c. */
2833 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
2835 com_block->is_bind_c = is_bind_c;
2840 /* Verify that the given gfc_typespec is for a C interoperable type. */
2843 verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
2847 /* Make sure the kind used is appropriate for the type.
2848 The f90_type is unknown if an integer constant was
2849 used (e.g., real(4), bind(c) :: myFloat). */
2850 if (ts->f90_type != BT_UNKNOWN)
2852 t = gfc_validate_c_kind (ts);
2855 /* Print an error, but continue parsing line. */
2856 gfc_error_now ("C kind parameter is for type %s but "
2857 "symbol '%s' at %L is of type %s",
2858 gfc_basic_typename (ts->f90_type),
2860 gfc_basic_typename (ts->type));
2864 /* Make sure the kind is C interoperable. This does not care about the
2865 possible error above. */
2866 if (ts->type == BT_DERIVED && ts->derived != NULL)
2867 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
2868 else if (ts->is_c_interop != 1)
2875 /* Verify that the variables of a given common block, which has been
2876 defined with the attribute specifier bind(c), to be of a C
2877 interoperable type. Errors will be reported here, if
2881 verify_com_block_vars_c_interop (gfc_common_head *com_block)
2883 gfc_symbol *curr_sym = NULL;
2884 try retval = SUCCESS;
2886 curr_sym = com_block->head;
2888 /* Make sure we have at least one symbol. */
2889 if (curr_sym == NULL)
2892 /* Here we know we have a symbol, so we'll execute this loop
2896 /* The second to last param, 1, says this is in a common block. */
2897 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
2898 curr_sym = curr_sym->common_next;
2899 } while (curr_sym != NULL);
2905 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
2906 an appropriate error message is reported. */
2909 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
2910 int is_in_common, gfc_common_head *com_block)
2912 try retval = SUCCESS;
2914 /* Here, we know we have the bind(c) attribute, so if we have
2915 enough type info, then verify that it's a C interop kind.
2916 The info could be in the symbol already, or possibly still in
2917 the given ts (current_ts), so look in both. */
2918 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
2920 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
2921 &(tmp_sym->declared_at)) != SUCCESS)
2923 /* See if we're dealing with a sym in a common block or not. */
2924 if (is_in_common == 1)
2926 gfc_warning ("Variable '%s' in common block '%s' at %L "
2927 "may not be a C interoperable "
2928 "kind though common block '%s' is BIND(C)",
2929 tmp_sym->name, com_block->name,
2930 &(tmp_sym->declared_at), com_block->name);
2934 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
2935 gfc_error ("Type declaration '%s' at %L is not C "
2936 "interoperable but it is BIND(C)",
2937 tmp_sym->name, &(tmp_sym->declared_at));
2939 gfc_warning ("Variable '%s' at %L "
2940 "may not be a C interoperable "
2941 "kind but it is bind(c)",
2942 tmp_sym->name, &(tmp_sym->declared_at));
2946 /* Variables declared w/in a common block can't be bind(c)
2947 since there's no way for C to see these variables, so there's
2948 semantically no reason for the attribute. */
2949 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
2951 gfc_error ("Variable '%s' in common block '%s' at "
2952 "%L cannot be declared with BIND(C) "
2953 "since it is not a global",
2954 tmp_sym->name, com_block->name,
2955 &(tmp_sym->declared_at));
2959 /* Scalar variables that are bind(c) can not have the pointer
2960 or allocatable attributes. */
2961 if (tmp_sym->attr.is_bind_c == 1)
2963 if (tmp_sym->attr.pointer == 1)
2965 gfc_error ("Variable '%s' at %L cannot have both the "
2966 "POINTER and BIND(C) attributes",
2967 tmp_sym->name, &(tmp_sym->declared_at));
2971 if (tmp_sym->attr.allocatable == 1)
2973 gfc_error ("Variable '%s' at %L cannot have both the "
2974 "ALLOCATABLE and BIND(C) attributes",
2975 tmp_sym->name, &(tmp_sym->declared_at));
2979 /* If it is a BIND(C) function, make sure the return value is a
2980 scalar value. The previous tests in this function made sure
2981 the type is interoperable. */
2982 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
2983 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
2984 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
2986 /* BIND(C) functions can not return a character string. */
2987 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
2988 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
2989 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
2990 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
2991 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
2992 "be a character string", tmp_sym->name,
2993 &(tmp_sym->declared_at));
2997 /* See if the symbol has been marked as private. If it has, make sure
2998 there is no binding label and warn the user if there is one. */
2999 if (tmp_sym->attr.access == ACCESS_PRIVATE
3000 && tmp_sym->binding_label[0] != '\0')
3001 /* Use gfc_warning_now because we won't say that the symbol fails
3002 just because of this. */
3003 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3004 "given the binding label '%s'", tmp_sym->name,
3005 &(tmp_sym->declared_at), tmp_sym->binding_label);
3011 /* Set the appropriate fields for a symbol that's been declared as
3012 BIND(C) (the is_bind_c flag and the binding label), and verify that
3013 the type is C interoperable. Errors are reported by the functions
3014 used to set/test these fields. */
3017 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3019 try retval = SUCCESS;
3021 /* TODO: Do we need to make sure the vars aren't marked private? */
3023 /* Set the is_bind_c bit in symbol_attribute. */
3024 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3026 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3027 num_idents) != SUCCESS)
3034 /* Set the fields marking the given common block as BIND(C), including
3035 a binding label, and report any errors encountered. */
3038 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3040 try retval = SUCCESS;
3042 /* destLabel, common name, typespec (which may have binding label). */
3043 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3047 /* Set the given common block (com_block) to being bind(c) (1). */
3048 set_com_block_bind_c (com_block, 1);
3054 /* Retrieve the list of one or more identifiers that the given bind(c)
3055 attribute applies to. */
3058 get_bind_c_idents (void)
3060 char name[GFC_MAX_SYMBOL_LEN + 1];
3062 gfc_symbol *tmp_sym = NULL;
3064 gfc_common_head *com_block = NULL;
3066 if (gfc_match_name (name) == MATCH_YES)
3068 found_id = MATCH_YES;
3069 gfc_get_ha_symbol (name, &tmp_sym);
3071 else if (match_common_name (name) == MATCH_YES)
3073 found_id = MATCH_YES;
3074 com_block = gfc_get_common (name, 0);
3078 gfc_error ("Need either entity or common block name for "
3079 "attribute specification statement at %C");
3083 /* Save the current identifier and look for more. */
3086 /* Increment the number of identifiers found for this spec stmt. */
3089 /* Make sure we have a sym or com block, and verify that it can
3090 be bind(c). Set the appropriate field(s) and look for more
3092 if (tmp_sym != NULL || com_block != NULL)
3094 if (tmp_sym != NULL)
3096 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3102 if (set_verify_bind_c_com_block(com_block, num_idents)
3107 /* Look to see if we have another identifier. */
3109 if (gfc_match_eos () == MATCH_YES)
3110 found_id = MATCH_NO;
3111 else if (gfc_match_char (',') != MATCH_YES)
3112 found_id = MATCH_NO;
3113 else if (gfc_match_name (name) == MATCH_YES)
3115 found_id = MATCH_YES;
3116 gfc_get_ha_symbol (name, &tmp_sym);
3118 else if (match_common_name (name) == MATCH_YES)
3120 found_id = MATCH_YES;
3121 com_block = gfc_get_common (name, 0);
3125 gfc_error ("Missing entity or common block name for "
3126 "attribute specification statement at %C");
3132 gfc_internal_error ("Missing symbol");
3134 } while (found_id == MATCH_YES);
3136 /* if we get here we were successful */
3141 /* Try and match a BIND(C) attribute specification statement. */
3144 gfc_match_bind_c_stmt (void)
3146 match found_match = MATCH_NO;
3151 /* This may not be necessary. */
3153 /* Clear the temporary binding label holder. */
3154 curr_binding_label[0] = '\0';
3156 /* Look for the bind(c). */
3157 found_match = gfc_match_bind_c (NULL);
3159 if (found_match == MATCH_YES)
3161 /* Look for the :: now, but it is not required. */
3164 /* Get the identifier(s) that needs to be updated. This may need to
3165 change to hand the flag(s) for the attr specified so all identifiers
3166 found can have all appropriate parts updated (assuming that the same
3167 spec stmt can have multiple attrs, such as both bind(c) and
3169 if (get_bind_c_idents () != SUCCESS)
3170 /* Error message should have printed already. */
3178 /* Match a data declaration statement. */
3181 gfc_match_data_decl (void)
3187 num_idents_on_line = 0;
3189 m = match_type_spec (¤t_ts, 0);
3193 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3195 sym = gfc_use_derived (current_ts.derived);
3203 current_ts.derived = sym;
3206 m = match_attr_spec ();
3207 if (m == MATCH_ERROR)
3213 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
3216 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3219 gfc_find_symbol (current_ts.derived->name,
3220 current_ts.derived->ns->parent, 1, &sym);
3222 /* Any symbol that we find had better be a type definition
3223 which has its components defined. */
3224 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3225 && current_ts.derived->components != NULL)
3228 /* Now we have an error, which we signal, and then fix up
3229 because the knock-on is plain and simple confusing. */
3230 gfc_error_now ("Derived type at %C has not been previously defined "
3231 "and so cannot appear in a derived type definition");
3232 current_attr.pointer = 1;
3237 /* If we have an old-style character declaration, and no new-style
3238 attribute specifications, then there a comma is optional between
3239 the type specification and the variable list. */
3240 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3241 gfc_match_char (',');
3243 /* Give the types/attributes to symbols that follow. Give the element
3244 a number so that repeat character length expressions can be copied. */
3248 num_idents_on_line++;
3249 m = variable_decl (elem++);
3250 if (m == MATCH_ERROR)
3255 if (gfc_match_eos () == MATCH_YES)
3257 if (gfc_match_char (',') != MATCH_YES)
3261 if (gfc_error_flag_test () == 0)
3262 gfc_error ("Syntax error in data declaration at %C");
3265 gfc_free_data_all (gfc_current_ns);
3268 gfc_free_array_spec (current_as);
3274 /* Match a prefix associated with a function or subroutine
3275 declaration. If the typespec pointer is nonnull, then a typespec
3276 can be matched. Note that if nothing matches, MATCH_YES is
3277 returned (the null string was matched). */
3280 match_prefix (gfc_typespec *ts)
3284 gfc_clear_attr (¤t_attr);
3288 if (!seen_type && ts != NULL
3289 && match_type_spec (ts, 0) == MATCH_YES
3290 && gfc_match_space () == MATCH_YES)
3297 if (gfc_match ("elemental% ") == MATCH_YES)
3299 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
3305 if (gfc_match ("pure% ") == MATCH_YES)
3307 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
3313 if (gfc_match ("recursive% ") == MATCH_YES)
3315 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
3321 /* At this point, the next item is not a prefix. */
3326 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
3329 copy_prefix (symbol_attribute *dest, locus *where)
3331 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3334 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3337 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3344 /* Match a formal argument list. */
3347 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3349 gfc_formal_arglist *head, *tail, *p, *q;
3350 char name[GFC_MAX_SYMBOL_LEN + 1];
3356 if (gfc_match_char ('(') != MATCH_YES)
3363 if (gfc_match_char (')') == MATCH_YES)
3368 if (gfc_match_char ('*') == MATCH_YES)
3372 m = gfc_match_name (name);
3376 if (gfc_get_symbol (name, NULL, &sym))
3380 p = gfc_get_formal_arglist ();
3392 /* We don't add the VARIABLE flavor because the name could be a
3393 dummy procedure. We don't apply these attributes to formal
3394 arguments of statement functions. */
3395 if (sym != NULL && !st_flag
3396 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3397 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3403 /* The name of a program unit can be in a different namespace,
3404 so check for it explicitly. After the statement is accepted,
3405 the name is checked for especially in gfc_get_symbol(). */
3406 if (gfc_new_block != NULL && sym != NULL
3407 && strcmp (sym->name, gfc_new_block->name) == 0)
3409 gfc_error ("Name '%s' at %C is the name of the procedure",
3415 if (gfc_match_char (')') == MATCH_YES)
3418 m = gfc_match_char (',');
3421 gfc_error ("Unexpected junk in formal argument list at %C");
3427 /* Check for duplicate symbols in the formal argument list. */
3430 for (p = head; p->next; p = p->next)
3435 for (q = p->next; q; q = q->next)
3436 if (p->sym == q->sym)
3438 gfc_error ("Duplicate symbol '%s' in formal argument list "
3439 "at %C", p->sym->name);
3447 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3457 gfc_free_formal_arglist (head);
3462 /* Match a RESULT specification following a function declaration or
3463 ENTRY statement. Also matches the end-of-statement. */
3466 match_result (gfc_symbol *function, gfc_symbol **result)
3468 char name[GFC_MAX_SYMBOL_LEN + 1];
3472 if (gfc_match (" result (") != MATCH_YES)
3475 m = gfc_match_name (name);
3479 /* Get the right paren, and that's it because there could be the
3480 bind(c) attribute after the result clause. */
3481 if (gfc_match_char(')') != MATCH_YES)
3483 /* TODO: should report the missing right paren here. */
3487 if (strcmp (function->name, name) == 0)
3489 gfc_error ("RESULT variable at %C must be different than function name");
3493 if (gfc_get_symbol (name, NULL, &r))
3496 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3497 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
3506 /* Match a function suffix, which could be a combination of a result
3507 clause and BIND(C), either one, or neither. The draft does not
3508 require them to come in a specific order. */
3511 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3513 match is_bind_c; /* Found bind(c). */
3514 match is_result; /* Found result clause. */
3515 match found_match; /* Status of whether we've found a good match. */
3516 int peek_char; /* Character we're going to peek at. */
3518 /* Initialize to having found nothing. */
3519 found_match = MATCH_NO;
3520 is_bind_c = MATCH_NO;
3521 is_result = MATCH_NO;
3523 /* Get the next char to narrow between result and bind(c). */
3524 gfc_gobble_whitespace ();
3525 peek_char = gfc_peek_char ();
3530 /* Look for result clause. */
3531 is_result = match_result (sym, result);
3532 if (is_result == MATCH_YES)
3534 /* Now see if there is a bind(c) after it. */
3535 is_bind_c = gfc_match_bind_c (sym);
3536 /* We've found the result clause and possibly bind(c). */
3537 found_match = MATCH_YES;
3540 /* This should only be MATCH_ERROR. */
3541 found_match = is_result;
3544 /* Look for bind(c) first. */
3545 is_bind_c = gfc_match_bind_c (sym);
3546 if (is_bind_c == MATCH_YES)
3548 /* Now see if a result clause followed it. */
3549 is_result = match_result (sym, result);
3550 found_match = MATCH_YES;
3554 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3555 found_match = MATCH_ERROR;
3559 gfc_error ("Unexpected junk after function declaration at %C");
3560 found_match = MATCH_ERROR;
3564 if (is_result == MATCH_ERROR || is_bind_c == MATCH_ERROR)
3566 gfc_error ("Error in function suffix at %C");
3570 if (is_bind_c == MATCH_YES)
3571 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
3579 /* Match a function declaration. */
3582 gfc_match_function_decl (void)
3584 char name[GFC_MAX_SYMBOL_LEN + 1];
3585 gfc_symbol *sym, *result;
3589 match found_match; /* Status returned by match func. */
3591 if (gfc_current_state () != COMP_NONE
3592 && gfc_current_state () != COMP_INTERFACE
3593 && gfc_current_state () != COMP_CONTAINS)
3596 gfc_clear_ts (¤t_ts);
3598 old_loc = gfc_current_locus;
3600 m = match_prefix (¤t_ts);
3603 gfc_current_locus = old_loc;
3607 if (gfc_match ("function% %n", name) != MATCH_YES)
3609 gfc_current_locus = old_loc;
3612 if (get_proc_name (name, &sym, false))
3614 gfc_new_block = sym;
3616 m = gfc_match_formal_arglist (sym, 0, 0);
3619 gfc_error ("Expected formal argument list in function "
3620 "definition at %C");
3624 else if (m == MATCH_ERROR)
3629 /* According to the draft, the bind(c) and result clause can
3630 come in either order after the formal_arg_list (i.e., either
3631 can be first, both can exist together or by themselves or neither
3632 one). Therefore, the match_result can't match the end of the
3633 string, and check for the bind(c) or result clause in either order. */
3634 found_match = gfc_match_eos ();
3636 /* Make sure that it isn't already declared as BIND(C). If it is, it
3637 must have been marked BIND(C) with a BIND(C) attribute and that is
3638 not allowed for procedures. */
3639 if (sym->attr.is_bind_c == 1)
3641 sym->attr.is_bind_c = 0;
3642 if (sym->old_symbol != NULL)
3643 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3644 "variables or common blocks",
3645 &(sym->old_symbol->declared_at));
3647 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3648 "variables or common blocks", &gfc_current_locus);
3651 if (found_match != MATCH_YES)
3653 /* If we haven't found the end-of-statement, look for a suffix. */
3654 suffix_match = gfc_match_suffix (sym, &result);
3655 if (suffix_match == MATCH_YES)
3656 /* Need to get the eos now. */
3657 found_match = gfc_match_eos ();
3659 found_match = suffix_match;
3662 if(found_match != MATCH_YES)
3666 /* Make changes to the symbol. */
3669 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
3672 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
3673 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
3676 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
3677 && !sym->attr.implicit_type)
3679 gfc_error ("Function '%s' at %C already has a type of %s", name,
3680 gfc_basic_typename (sym->ts.type));
3686 sym->ts = current_ts;
3691 result->ts = current_ts;
3692 sym->result = result;
3699 gfc_current_locus = old_loc;
3704 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
3705 pass the name of the entry, rather than the gfc_current_block name, and
3706 to return false upon finding an existing global entry. */
3709 add_global_entry (const char *name, int sub)
3713 s = gfc_get_gsymbol(name);
3716 || (s->type != GSYM_UNKNOWN
3717 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3718 global_used(s, NULL);
3721 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3722 s->where = gfc_current_locus;
3730 /* Match an ENTRY statement. */
3733 gfc_match_entry (void)
3738 char name[GFC_MAX_SYMBOL_LEN + 1];
3739 gfc_compile_state state;
3743 bool module_procedure;
3745 m = gfc_match_name (name);
3749 state = gfc_current_state ();
3750 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
3755 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
3758 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
3760 case COMP_BLOCK_DATA:
3761 gfc_error ("ENTRY statement at %C cannot appear within "
3764 case COMP_INTERFACE:
3765 gfc_error ("ENTRY statement at %C cannot appear within "
3769 gfc_error ("ENTRY statement at %C cannot appear within "
3770 "a DERIVED TYPE block");
3773 gfc_error ("ENTRY statement at %C cannot appear within "
3774 "an IF-THEN block");
3777 gfc_error ("ENTRY statement at %C cannot appear within "
3781 gfc_error ("ENTRY statement at %C cannot appear within "
3785 gfc_error ("ENTRY statement at %C cannot appear within "
3789 gfc_error ("ENTRY statement at %C cannot appear within "
3793 gfc_error ("ENTRY statement at %C cannot appear within "
3794 "a contained subprogram");
3797 gfc_internal_error ("gfc_match_entry(): Bad state");
3802 module_procedure = gfc_current_ns->parent != NULL
3803 && gfc_current_ns->parent->proc_name
3804 && gfc_current_ns->parent->proc_name->attr.flavor
3807 if (gfc_current_ns->parent != NULL
3808 && gfc_current_ns->parent->proc_name
3809 && !module_procedure)
3811 gfc_error("ENTRY statement at %C cannot appear in a "
3812 "contained procedure");
3816 /* Module function entries need special care in get_proc_name
3817 because previous references within the function will have
3818 created symbols attached to the current namespace. */
3819 if (get_proc_name (name, &entry,
3820 gfc_current_ns->parent != NULL
3822 && gfc_current_ns->proc_name->attr.function))
3825 proc = gfc_current_block ();
3827 if (state == COMP_SUBROUTINE)
3829 /* An entry in a subroutine. */
3830 if (!add_global_entry (name, 1))
3833 m = gfc_match_formal_arglist (entry, 0, 1);
3837 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3838 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
3843 /* An entry in a function.
3844 We need to take special care because writing
3849 ENTRY f() RESULT (r)
3851 ENTRY f RESULT (r). */
3852 if (!add_global_entry (name, 0))
3855 old_loc = gfc_current_locus;
3856 if (gfc_match_eos () == MATCH_YES)
3858 gfc_current_locus = old_loc;
3859 /* Match the empty argument list, and add the interface to
3861 m = gfc_match_formal_arglist (entry, 0, 1);
3864 m = gfc_match_formal_arglist (entry, 0, 0);
3871 if (gfc_match_eos () == MATCH_YES)
3873 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3874 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
3877 entry->result = entry;
3881 m = match_result (proc, &result);
3883 gfc_syntax_error (ST_ENTRY);
3887 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
3888 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
3889 || gfc_add_function (&entry->attr, result->name, NULL)
3893 entry->result = result;
3897 if (gfc_match_eos () != MATCH_YES)
3899 gfc_syntax_error (ST_ENTRY);
3903 entry->attr.recursive = proc->attr.recursive;
3904 entry->attr.elemental = proc->attr.elemental;
3905 entry->attr.pure = proc->attr.pure;
3907 el = gfc_get_entry_list ();
3909 el->next = gfc_current_ns->entries;
3910 gfc_current_ns->entries = el;
3912 el->id = el->next->id + 1;
3916 new_st.op = EXEC_ENTRY;
3917 new_st.ext.entry = el;
3923 /* Match a subroutine statement, including optional prefixes. */
3926 gfc_match_subroutine (void)
3928 char name[GFC_MAX_SYMBOL_LEN + 1];
3934 if (gfc_current_state () != COMP_NONE
3935 && gfc_current_state () != COMP_INTERFACE
3936 && gfc_current_state () != COMP_CONTAINS)
3939 m = match_prefix (NULL);
3943 m = gfc_match ("subroutine% %n", name);
3947 if (get_proc_name (name, &sym, false))
3949 gfc_new_block = sym;
3951 /* Check what next non-whitespace character is so we can tell if there
3952 where the required parens if we have a BIND(C). */
3953 gfc_gobble_whitespace ();
3954 peek_char = gfc_peek_char ();
3956 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
3959 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
3962 /* Make sure that it isn't already declared as BIND(C). If it is, it
3963 must have been marked BIND(C) with a BIND(C) attribute and that is
3964 not allowed for procedures. */
3965 if (sym->attr.is_bind_c == 1)
3967 sym->attr.is_bind_c = 0;
3968 if (sym->old_symbol != NULL)
3969 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3970 "variables or common blocks",
3971 &(sym->old_symbol->declared_at));
3973 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3974 "variables or common blocks", &gfc_current_locus);
3977 /* Here, we are just checking if it has the bind(c) attribute, and if
3978 so, then we need to make sure it's all correct. If it doesn't,
3979 we still need to continue matching the rest of the subroutine line. */
3980 is_bind_c = gfc_match_bind_c (sym);
3981 if (is_bind_c == MATCH_ERROR)
3983 /* There was an attempt at the bind(c), but it was wrong. An
3984 error message should have been printed w/in the gfc_match_bind_c
3985 so here we'll just return the MATCH_ERROR. */
3989 if (is_bind_c == MATCH_YES)
3991 if (peek_char != '(')
3993 gfc_error ("Missing required parentheses before BIND(C) at %C");
3996 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4001 if (gfc_match_eos () != MATCH_YES)
4003 gfc_syntax_error (ST_SUBROUTINE);
4007 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4014 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4015 given, and set the binding label in either the given symbol (if not
4016 NULL), or in the current_ts. The symbol may be NULL because we may
4017 encounter the BIND(C) before the declaration itself. Return
4018 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4019 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4020 or MATCH_YES if the specifier was correct and the binding label and
4021 bind(c) fields were set correctly for the given symbol or the
4025 gfc_match_bind_c (gfc_symbol *sym)
4027 /* binding label, if exists */
4028 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4031 int has_name_equals = 0;
4033 /* Initialize the flag that specifies whether we encountered a NAME=
4034 specifier or not. */
4035 has_name_equals = 0;
4037 /* Init the first char to nil so we can catch if we don't have
4038 the label (name attr) or the symbol name yet. */
4039 binding_label[0] = '\0';
4041 /* This much we have to be able to match, in this order, if
4042 there is a bind(c) label. */
4043 if (gfc_match (" bind ( c ") != MATCH_YES)
4046 /* Now see if there is a binding label, or if we've reached the
4047 end of the bind(c) attribute without one. */
4048 if (gfc_match_char (',') == MATCH_YES)
4050 if (gfc_match (" name = ") != MATCH_YES)
4052 gfc_error ("Syntax error in NAME= specifier for binding label "
4054 /* should give an error message here */
4058 has_name_equals = 1;
4060 /* Get the opening quote. */
4061 double_quote = MATCH_YES;
4062 single_quote = MATCH_YES;
4063 double_quote = gfc_match_char ('"');
4064 if (double_quote != MATCH_YES)
4065 single_quote = gfc_match_char ('\'');
4066 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4068 gfc_error ("Syntax error in NAME= specifier for binding label "
4073 /* Grab the binding label, using functions that will not lower
4074 case the names automatically. */
4075 if (gfc_match_name_C (binding_label) != MATCH_YES)
4078 /* Get the closing quotation. */
4079 if (double_quote == MATCH_YES)
4081 if (gfc_match_char ('"') != MATCH_YES)
4083 gfc_error ("Missing closing quote '\"' for binding label at %C");
4084 /* User started string with '"' so looked to match it. */
4090 if (gfc_match_char ('\'') != MATCH_YES)
4092 gfc_error ("Missing closing quote '\'' for binding label at %C");
4093 /* User started string with "'" char. */
4099 /* Get the required right paren. */
4100 if (gfc_match_char (')') != MATCH_YES)
4102 gfc_error ("Missing closing paren for binding label at %C");
4106 /* Save the binding label to the symbol. If sym is null, we're
4107 probably matching the typespec attributes of a declaration and
4108 haven't gotten the name yet, and therefore, no symbol yet. */
4109 if (binding_label[0] != '\0')
4113 strncpy (sym->binding_label, binding_label,
4114 strlen (binding_label)+1);
4117 strncpy (curr_binding_label, binding_label,
4118 strlen (binding_label) + 1);
4122 /* No binding label, but if symbol isn't null, we
4123 can set the label for it here. */
4124 /* TODO: If the name= was given and no binding label (name=""), we simply
4125 will let fortran mangle the symbol name as it usually would.
4126 However, this could still let C call it if the user looked up the
4127 symbol in the object file. Should the name set during mangling in
4128 trans-decl.c be marked with characters that are invalid for C to
4130 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4131 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4138 /* Return nonzero if we're currently compiling a contained procedure. */
4141 contained_procedure (void)
4145 for (s=gfc_state_stack; s; s=s->previous)
4146 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
4147 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
4153 /* Set the kind of each enumerator. The kind is selected such that it is
4154 interoperable with the corresponding C enumeration type, making
4155 sure that -fshort-enums is honored. */
4160 enumerator_history *current_history = NULL;
4164 if (max_enum == NULL || enum_history == NULL)
4167 if (!gfc_option.fshort_enums)
4173 kind = gfc_integer_kinds[i++].kind;
4175 while (kind < gfc_c_int_kind
4176 && gfc_check_integer_range (max_enum->initializer->value.integer,
4179 current_history = enum_history;
4180 while (current_history != NULL)
4182 current_history->sym->ts.kind = kind;
4183 current_history = current_history->next;
4188 /* Match any of the various end-block statements. Returns the type of
4189 END to the caller. The END INTERFACE, END IF, END DO and END
4190 SELECT statements cannot be replaced by a single END statement. */
4193 gfc_match_end (gfc_statement *st)
4195 char name[GFC_MAX_SYMBOL_LEN + 1];
4196 gfc_compile_state state;
4198 const char *block_name;
4203 old_loc = gfc_current_locus;
4204 if (gfc_match ("end") != MATCH_YES)
4207 state = gfc_current_state ();
4208 block_name = gfc_current_block () == NULL
4209 ? NULL : gfc_current_block ()->name;
4211 if (state == COMP_CONTAINS)
4213 state = gfc_state_stack->previous->state;
4214 block_name = gfc_state_stack->previous->sym == NULL
4215 ? NULL : gfc_state_stack->previous->sym->name;
4222 *st = ST_END_PROGRAM;
4223 target = " program";
4227 case COMP_SUBROUTINE:
4228 *st = ST_END_SUBROUTINE;
4229 target = " subroutine";
4230 eos_ok = !contained_procedure ();
4234 *st = ST_END_FUNCTION;
4235 target = " function";
4236 eos_ok = !contained_procedure ();
4239 case COMP_BLOCK_DATA:
4240 *st = ST_END_BLOCK_DATA;
4241 target = " block data";
4246 *st = ST_END_MODULE;
4251 case COMP_INTERFACE:
4252 *st = ST_END_INTERFACE;
4253 target = " interface";
4276 *st = ST_END_SELECT;
4282 *st = ST_END_FORALL;
4297 last_initializer = NULL;
4299 gfc_free_enum_history ();
4303 gfc_error ("Unexpected END statement at %C");
4307 if (gfc_match_eos () == MATCH_YES)
4311 /* We would have required END [something]. */
4312 gfc_error ("%s statement expected at %L",
4313 gfc_ascii_statement (*st), &old_loc);
4320 /* Verify that we've got the sort of end-block that we're expecting. */
4321 if (gfc_match (target) != MATCH_YES)
4323 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
4327 /* If we're at the end, make sure a block name wasn't required. */
4328 if (gfc_match_eos () == MATCH_YES)
4331 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
4332 && *st != ST_END_FORALL && *st != ST_END_WHERE)
4335 if (gfc_current_block () == NULL)
4338 gfc_error ("Expected block name of '%s' in %s statement at %C",
4339 block_name, gfc_ascii_statement (*st));
4344 /* END INTERFACE has a special handler for its several possible endings. */
4345 if (*st == ST_END_INTERFACE)
4346 return gfc_match_end_interface ();
4348 /* We haven't hit the end of statement, so what is left must be an
4350 m = gfc_match_space ();
4352 m = gfc_match_name (name);
4355 gfc_error ("Expected terminating name at %C");
4359 if (block_name == NULL)
4362 if (strcmp (name, block_name) != 0)
4364 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
4365 gfc_ascii_statement (*st));
4369 if (gfc_match_eos () == MATCH_YES)
4373 gfc_syntax_error (*st);
4376 gfc_current_locus = old_loc;
4382 /***************** Attribute declaration statements ****************/
4384 /* Set the attribute of a single variable. */
4389 char name[GFC_MAX_SYMBOL_LEN + 1];
4397 m = gfc_match_name (name);
4401 if (find_special (name, &sym))
4404 var_locus = gfc_current_locus;
4406 /* Deal with possible array specification for certain attributes. */
4407 if (current_attr.dimension
4408 || current_attr.allocatable
4409 || current_attr.pointer
4410 || current_attr.target)
4412 m = gfc_match_array_spec (&as);
4413 if (m == MATCH_ERROR)
4416 if (current_attr.dimension && m == MATCH_NO)
4418 gfc_error ("Missing array specification at %L in DIMENSION "
4419 "statement", &var_locus);
4424 if ((current_attr.allocatable || current_attr.pointer)
4425 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
4427 gfc_error ("Array specification must be deferred at %L", &var_locus);
4433 /* Update symbol table. DIMENSION attribute is set
4434 in gfc_set_array_spec(). */
4435 if (current_attr.dimension == 0
4436 && gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
4442 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
4448 if (sym->attr.cray_pointee && sym->as != NULL)
4450 /* Fix the array spec. */
4451 m = gfc_mod_pointee_as (sym->as);
4452 if (m == MATCH_ERROR)
4456 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
4462 if ((current_attr.external || current_attr.intrinsic)
4463 && sym->attr.flavor != FL_PROCEDURE
4464 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
4473 gfc_free_array_spec (as);
4478 /* Generic attribute declaration subroutine. Used for attributes that
4479 just have a list of names. */
4486 /* Gobble the optional double colon, by simply ignoring the result
4496 if (gfc_match_eos () == MATCH_YES)
4502 if (gfc_match_char (',') != MATCH_YES)
4504 gfc_error ("Unexpected character in variable list at %C");
4514 /* This routine matches Cray Pointer declarations of the form:
4515 pointer ( <pointer>, <pointee> )
4517 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
4518 The pointer, if already declared, should be an integer. Otherwise, we
4519 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
4520 be either a scalar, or an array declaration. No space is allocated for
4521 the pointee. For the statement
4522 pointer (ipt, ar(10))
4523 any subsequent uses of ar will be translated (in C-notation) as
4524 ar(i) => ((<type> *) ipt)(i)
4525 After gimplification, pointee variable will disappear in the code. */
4528 cray_pointer_decl (void)
4532 gfc_symbol *cptr; /* Pointer symbol. */
4533 gfc_symbol *cpte; /* Pointee symbol. */
4539 if (gfc_match_char ('(') != MATCH_YES)
4541 gfc_error ("Expected '(' at %C");
4545 /* Match pointer. */
4546 var_locus = gfc_current_locus;
4547 gfc_clear_attr (¤t_attr);
4548 gfc_add_cray_pointer (¤t_attr, &var_locus);
4549 current_ts.type = BT_INTEGER;
4550 current_ts.kind = gfc_index_integer_kind;
4552 m = gfc_match_symbol (&cptr, 0);
4555 gfc_error ("Expected variable name at %C");
4559 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
4562 gfc_set_sym_referenced (cptr);
4564 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
4566 cptr->ts.type = BT_INTEGER;
4567 cptr->ts.kind = gfc_index_integer_kind;
4569 else if (cptr->ts.type != BT_INTEGER)
4571 gfc_error ("Cray pointer at %C must be an integer");
4574 else if (cptr->ts.kind < gfc_index_integer_kind)
4575 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
4576 " memory addresses require %d bytes",
4577 cptr->ts.kind, gfc_index_integer_kind);
4579 if (gfc_match_char (',') != MATCH_YES)
4581 gfc_error ("Expected \",\" at %C");
4585 /* Match Pointee. */
4586 var_locus = gfc_current_locus;
4587 gfc_clear_attr (¤t_attr);
4588 gfc_add_cray_pointee (¤t_attr, &var_locus);
4589 current_ts.type = BT_UNKNOWN;
4590 current_ts.kind = 0;
4592 m = gfc_match_symbol (&cpte, 0);
4595 gfc_error ("Expected variable name at %C");
4599 /* Check for an optional array spec. */
4600 m = gfc_match_array_spec (&as);
4601 if (m == MATCH_ERROR)
4603 gfc_free_array_spec (as);
4606 else if (m == MATCH_NO)
4608 gfc_free_array_spec (as);
4612 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
4615 gfc_set_sym_referenced (cpte);
4617 if (cpte->as == NULL)
4619 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
4620 gfc_internal_error ("Couldn't set Cray pointee array spec.");
4622 else if (as != NULL)
4624 gfc_error ("Duplicate array spec for Cray pointee at %C");
4625 gfc_free_array_spec (as);
4631 if (cpte->as != NULL)
4633 /* Fix array spec. */
4634 m = gfc_mod_pointee_as (cpte->as);
4635 if (m == MATCH_ERROR)
4639 /* Point the Pointee at the Pointer. */
4640 cpte->cp_pointer = cptr;
4642 if (gfc_match_char (')') != MATCH_YES)
4644 gfc_error ("Expected \")\" at %C");
4647 m = gfc_match_char (',');
4649 done = true; /* Stop searching for more declarations. */
4653 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
4654 || gfc_match_eos () != MATCH_YES)
4656 gfc_error ("Expected \",\" or end of statement at %C");
4664 gfc_match_external (void)
4667 gfc_clear_attr (¤t_attr);
4668 current_attr.external = 1;
4670 return attr_decl ();
4675 gfc_match_intent (void)
4679 intent = match_intent_spec ();
4680 if (intent == INTENT_UNKNOWN)
4683 gfc_clear_attr (¤t_attr);
4684 current_attr.intent = intent;
4686 return attr_decl ();
4691 gfc_match_intrinsic (void)
4694 gfc_clear_attr (¤t_attr);
4695 current_attr.intrinsic = 1;
4697 return attr_decl ();
4702 gfc_match_optional (void)
4705 gfc_clear_attr (¤t_attr);
4706 current_attr.optional = 1;
4708 return attr_decl ();
4713 gfc_match_pointer (void)
4715 gfc_gobble_whitespace ();
4716 if (gfc_peek_char () == '(')
4718 if (!gfc_option.flag_cray_pointer)
4720 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
4724 return cray_pointer_decl ();
4728 gfc_clear_attr (¤t_attr);
4729 current_attr.pointer = 1;
4731 return attr_decl ();
4737 gfc_match_allocatable (void)
4739 gfc_clear_attr (¤t_attr);
4740 current_attr.allocatable = 1;
4742 return attr_decl ();
4747 gfc_match_dimension (void)
4749 gfc_clear_attr (¤t_attr);
4750 current_attr.dimension = 1;
4752 return attr_decl ();
4757 gfc_match_target (void)
4759 gfc_clear_attr (¤t_attr);
4760 current_attr.target = 1;
4762 return attr_decl ();
4766 /* Match the list of entities being specified in a PUBLIC or PRIVATE
4770 access_attr_decl (gfc_statement st)
4772 char name[GFC_MAX_SYMBOL_LEN + 1];
4773 interface_type type;
4776 gfc_intrinsic_op operator;
4779 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4784 m = gfc_match_generic_spec (&type, name, &operator);
4787 if (m == MATCH_ERROR)
4792 case INTERFACE_NAMELESS:
4795 case INTERFACE_GENERIC:
4796 if (gfc_get_symbol (name, NULL, &sym))
4799 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
4800 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
4801 sym->name, NULL) == FAILURE)
4806 case INTERFACE_INTRINSIC_OP:
4807 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
4809 gfc_current_ns->operator_access[operator] =
4810 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
4814 gfc_error ("Access specification of the %s operator at %C has "
4815 "already been specified", gfc_op2string (operator));
4821 case INTERFACE_USER_OP:
4822 uop = gfc_get_uop (name);
4824 if (uop->access == ACCESS_UNKNOWN)
4826 uop->access = (st == ST_PUBLIC)
4827 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
4831 gfc_error ("Access specification of the .%s. operator at %C "
4832 "has already been specified", sym->name);
4839 if (gfc_match_char (',') == MATCH_NO)
4843 if (gfc_match_eos () != MATCH_YES)
4848 gfc_syntax_error (st);
4856 gfc_match_protected (void)
4861 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
4863 gfc_error ("PROTECTED at %C only allowed in specification "
4864 "part of a module");
4869 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
4873 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4878 if (gfc_match_eos () == MATCH_YES)
4883 m = gfc_match_symbol (&sym, 0);
4887 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
4900 if (gfc_match_eos () == MATCH_YES)
4902 if (gfc_match_char (',') != MATCH_YES)
4909 gfc_error ("Syntax error in PROTECTED statement at %C");
4914 /* The PRIVATE statement is a bit weird in that it can be an attribute
4915 declaration, but also works as a standlone statement inside of a
4916 type declaration or a module. */
4919 gfc_match_private (gfc_statement *st)
4922 if (gfc_match ("private") != MATCH_YES)
4925 if (gfc_current_state () != COMP_MODULE
4926 && (gfc_current_state () != COMP_DERIVED
4927 || !gfc_state_stack->previous
4928 || gfc_state_stack->previous->state != COMP_MODULE))
4930 gfc_error ("PRIVATE statement at %C is only allowed in the "
4931 "specification part of a module");
4935 if (gfc_current_state () == COMP_DERIVED)
4937 if (gfc_match_eos () == MATCH_YES)
4943 gfc_syntax_error (ST_PRIVATE);
4947 if (gfc_match_eos () == MATCH_YES)
4954 return access_attr_decl (ST_PRIVATE);
4959 gfc_match_public (gfc_statement *st)
4962 if (gfc_match ("public") != MATCH_YES)
4965 if (gfc_current_state () != COMP_MODULE)
4967 gfc_error ("PUBLIC statement at %C is only allowed in the "
4968 "specification part of a module");
4972 if (gfc_match_eos () == MATCH_YES)
4979 return access_attr_decl (ST_PUBLIC);
4983 /* Workhorse for gfc_match_parameter. */
4992 m = gfc_match_symbol (&sym, 0);
4994 gfc_error ("Expected variable name at %C in PARAMETER statement");
4999 if (gfc_match_char ('=') == MATCH_NO)
5001 gfc_error ("Expected = sign in PARAMETER statement at %C");
5005 m = gfc_match_init_expr (&init);
5007 gfc_error ("Expected expression at %C in PARAMETER statement");
5011 if (sym->ts.type == BT_UNKNOWN
5012 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5018 if (gfc_check_assign_symbol (sym, init) == FAILURE
5019 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
5025 if (sym->ts.type == BT_CHARACTER
5026 && sym->ts.cl != NULL
5027 && sym->ts.cl->length != NULL
5028 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5029 && init->expr_type == EXPR_CONSTANT
5030 && init->ts.type == BT_CHARACTER
5031 && init->ts.kind == 1)
5032 gfc_set_constant_character_len (
5033 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
5039 gfc_free_expr (init);
5044 /* Match a parameter statement, with the weird syntax that these have. */
5047 gfc_match_parameter (void)
5051 if (gfc_match_char ('(') == MATCH_NO)
5060 if (gfc_match (" )%t") == MATCH_YES)
5063 if (gfc_match_char (',') != MATCH_YES)
5065 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5075 /* Save statements have a special syntax. */
5078 gfc_match_save (void)
5080 char n[GFC_MAX_SYMBOL_LEN+1];
5085 if (gfc_match_eos () == MATCH_YES)
5087 if (gfc_current_ns->seen_save)
5089 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5090 "follows previous SAVE statement")
5095 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5099 if (gfc_current_ns->save_all)
5101 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5102 "blanket SAVE statement")
5111 m = gfc_match_symbol (&sym, 0);
5115 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5127 m = gfc_match (" / %n /", &n);
5128 if (m == MATCH_ERROR)
5133 c = gfc_get_common (n, 0);
5136 gfc_current_ns->seen_save = 1;
5139 if (gfc_match_eos () == MATCH_YES)
5141 if (gfc_match_char (',') != MATCH_YES)
5148 gfc_error ("Syntax error in SAVE statement at %C");
5154 gfc_match_value (void)
5159 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
5163 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5168 if (gfc_match_eos () == MATCH_YES)
5173 m = gfc_match_symbol (&sym, 0);
5177 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5190 if (gfc_match_eos () == MATCH_YES)
5192 if (gfc_match_char (',') != MATCH_YES)
5199 gfc_error ("Syntax error in VALUE statement at %C");
5205 gfc_match_volatile (void)
5210 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
5214 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5219 if (gfc_match_eos () == MATCH_YES)
5224 /* VOLATILE is special because it can be added to host-associated
5226 m = gfc_match_symbol (&sym, 1);
5230 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
5243 if (gfc_match_eos () == MATCH_YES)
5245 if (gfc_match_char (',') != MATCH_YES)
5252 gfc_error ("Syntax error in VOLATILE statement at %C");
5257 /* Match a module procedure statement. Note that we have to modify
5258 symbols in the parent's namespace because the current one was there
5259 to receive symbols that are in an interface's formal argument list. */
5262 gfc_match_modproc (void)
5264 char name[GFC_MAX_SYMBOL_LEN + 1];
5267 gfc_namespace *module_ns;
5269 if (gfc_state_stack->state != COMP_INTERFACE
5270 || gfc_state_stack->previous == NULL
5271 || current_interface.type == INTERFACE_NAMELESS)
5273 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5278 module_ns = gfc_current_ns->parent;
5279 for (; module_ns; module_ns = module_ns->parent)
5280 if (module_ns->proc_name->attr.flavor == FL_MODULE)
5283 if (module_ns == NULL)
5288 m = gfc_match_name (name);
5294 if (gfc_get_symbol (name, module_ns, &sym))
5297 if (sym->attr.proc != PROC_MODULE
5298 && gfc_add_procedure (&sym->attr, PROC_MODULE,
5299 sym->name, NULL) == FAILURE)
5302 if (gfc_add_interface (sym) == FAILURE)
5305 sym->attr.mod_proc = 1;
5307 if (gfc_match_eos () == MATCH_YES)
5309 if (gfc_match_char (',') != MATCH_YES)
5316 gfc_syntax_error (ST_MODULE_PROC);
5321 /* Match the optional attribute specifiers for a type declaration.
5322 Return MATCH_ERROR if an error is encountered in one of the handled
5323 attributes (public, private, bind(c)), MATCH_NO if what's found is
5324 not a handled attribute, and MATCH_YES otherwise. TODO: More error
5325 checking on attribute conflicts needs to be done. */
5328 gfc_get_type_attr_spec (symbol_attribute *attr)
5330 /* See if the derived type is marked as private. */
5331 if (gfc_match (" , private") == MATCH_YES)
5333 if (gfc_current_state () != COMP_MODULE)
5335 gfc_error ("Derived type at %C can only be PRIVATE in the "
5336 "specification part of a module");
5340 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
5343 else if (gfc_match (" , public") == MATCH_YES)
5345 if (gfc_current_state () != COMP_MODULE)
5347 gfc_error ("Derived type at %C can only be PUBLIC in the "
5348 "specification part of a module");
5352 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
5355 else if(gfc_match(" , bind ( c )") == MATCH_YES)
5357 /* If the type is defined to be bind(c) it then needs to make
5358 sure that all fields are interoperable. This will
5359 need to be a semantic check on the finished derived type.
5360 See 15.2.3 (lines 9-12) of F2003 draft. */
5361 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
5364 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
5369 /* If we get here, something matched. */
5374 /* Match the beginning of a derived type declaration. If a type name
5375 was the result of a function, then it is possible to have a symbol
5376 already to be known as a derived type yet have no components. */
5379 gfc_match_derived_decl (void)
5381 char name[GFC_MAX_SYMBOL_LEN + 1];
5382 symbol_attribute attr;
5385 match is_type_attr_spec = MATCH_NO;
5387 if (gfc_current_state () == COMP_DERIVED)
5390 gfc_clear_attr (&attr);
5394 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
5395 if (is_type_attr_spec == MATCH_ERROR)
5397 } while (is_type_attr_spec == MATCH_YES);
5399 if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
5401 gfc_error ("Expected :: in TYPE definition at %C");
5405 m = gfc_match (" %n%t", name);
5409 /* Make sure the name isn't the name of an intrinsic type. The
5410 'double {precision,complex}' types don't get past the name
5411 matcher, unless they're written as a single word or in fixed
5413 if (strcmp (name, "integer") == 0
5414 || strcmp (name, "real") == 0
5415 || strcmp (name, "character") == 0
5416 || strcmp (name, "logical") == 0
5417 || strcmp (name, "complex") == 0
5418 || strcmp (name, "doubleprecision") == 0
5419 || strcmp (name, "doublecomplex") == 0)
5421 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
5426 if (gfc_get_symbol (name, NULL, &sym))
5429 if (sym->ts.type != BT_UNKNOWN)
5431 gfc_error ("Derived type name '%s' at %C already has a basic type "
5432 "of %s", sym->name, gfc_typename (&sym->ts));
5436 /* The symbol may already have the derived attribute without the
5437 components. The ways this can happen is via a function
5438 definition, an INTRINSIC statement or a subtype in another
5439 derived type that is a pointer. The first part of the AND clause
5440 is true if a the symbol is not the return value of a function. */
5441 if (sym->attr.flavor != FL_DERIVED
5442 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
5445 if (sym->components != NULL)
5447 gfc_error ("Derived type definition of '%s' at %C has already been "
5448 "defined", sym->name);
5452 if (attr.access != ACCESS_UNKNOWN
5453 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
5456 /* See if the derived type was labeled as bind(c). */
5457 if (attr.is_bind_c != 0)
5458 sym->attr.is_bind_c = attr.is_bind_c;
5460 gfc_new_block = sym;
5466 /* Cray Pointees can be declared as:
5467 pointer (ipt, a (n,m,...,*))
5468 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
5469 cheat and set a constant bound of 1 for the last dimension, if this
5470 is the case. Since there is no bounds-checking for Cray Pointees,
5471 this will be okay. */
5474 gfc_mod_pointee_as (gfc_array_spec *as)
5476 as->cray_pointee = true; /* This will be useful to know later. */
5477 if (as->type == AS_ASSUMED_SIZE)
5479 as->type = AS_EXPLICIT;
5480 as->upper[as->rank - 1] = gfc_int_expr (1);
5481 as->cp_was_assumed = true;
5483 else if (as->type == AS_ASSUMED_SHAPE)
5485 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
5492 /* Match the enum definition statement, here we are trying to match
5493 the first line of enum definition statement.
5494 Returns MATCH_YES if match is found. */
5497 gfc_match_enum (void)
5501 m = gfc_match_eos ();
5505 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
5513 /* Match a variable name with an optional initializer. When this
5514 subroutine is called, a variable is expected to be parsed next.
5515 Depending on what is happening at the moment, updates either the
5516 symbol table or the current interface. */
5519 enumerator_decl (void)
5521 char name[GFC_MAX_SYMBOL_LEN + 1];
5522 gfc_expr *initializer;
5523 gfc_array_spec *as = NULL;
5531 old_locus = gfc_current_locus;
5533 /* When we get here, we've just matched a list of attributes and
5534 maybe a type and a double colon. The next thing we expect to see
5535 is the name of the symbol. */
5536 m = gfc_match_name (name);
5540 var_locus = gfc_current_locus;
5542 /* OK, we've successfully matched the declaration. Now put the
5543 symbol in the current namespace. If we fail to create the symbol,
5545 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
5551 /* The double colon must be present in order to have initializers.
5552 Otherwise the statement is ambiguous with an assignment statement. */
5555 if (gfc_match_char ('=') == MATCH_YES)
5557 m = gfc_match_init_expr (&initializer);
5560 gfc_error ("Expected an initialization expression at %C");
5569 /* If we do not have an initializer, the initialization value of the
5570 previous enumerator (stored in last_initializer) is incremented
5571 by 1 and is used to initialize the current enumerator. */
5572 if (initializer == NULL)
5573 initializer = gfc_enum_initializer (last_initializer, old_locus);
5575 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
5577 gfc_error("ENUMERATOR %L not initialized with integer expression",
5580 gfc_free_enum_history ();
5584 /* Store this current initializer, for the next enumerator variable
5585 to be parsed. add_init_expr_to_sym() zeros initializer, so we
5586 use last_initializer below. */
5587 last_initializer = initializer;
5588 t = add_init_expr_to_sym (name, &initializer, &var_locus);
5590 /* Maintain enumerator history. */
5591 gfc_find_symbol (name, NULL, 0, &sym);
5592 create_enum_history (sym, last_initializer);
5594 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
5597 /* Free stuff up and return. */
5598 gfc_free_expr (initializer);
5604 /* Match the enumerator definition statement. */
5607 gfc_match_enumerator_def (void)
5612 gfc_clear_ts (¤t_ts);
5614 m = gfc_match (" enumerator");
5618 m = gfc_match (" :: ");
5619 if (m == MATCH_ERROR)
5622 colon_seen = (m == MATCH_YES);
5624 if (gfc_current_state () != COMP_ENUM)
5626 gfc_error ("ENUM definition statement expected before %C");
5627 gfc_free_enum_history ();
5631 (¤t_ts)->type = BT_INTEGER;
5632 (¤t_ts)->kind = gfc_c_int_kind;
5634 gfc_clear_attr (¤t_attr);
5635 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
5644 m = enumerator_decl ();
5645 if (m == MATCH_ERROR)
5650 if (gfc_match_eos () == MATCH_YES)
5652 if (gfc_match_char (',') != MATCH_YES)
5656 if (gfc_current_state () == COMP_ENUM)
5658 gfc_free_enum_history ();
5659 gfc_error ("Syntax error in ENUMERATOR definition at %C");
5664 gfc_free_array_spec (current_as);