1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
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 COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
29 /* Macros to access allocate memory for gfc_data_variable,
30 gfc_data_value and gfc_data. */
31 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
32 #define gfc_get_data_value() XCNEW (gfc_data_value)
33 #define gfc_get_data() XCNEW (gfc_data)
36 /* This flag is set if an old-style length selector is matched
37 during a type-declaration statement. */
39 static int old_char_selector;
41 /* When variables acquire types and attributes from a declaration
42 statement, they get them from the following static variables. The
43 first part of a declaration sets these variables and the second
44 part copies these into symbol structures. */
46 static gfc_typespec current_ts;
48 static symbol_attribute current_attr;
49 static gfc_array_spec *current_as;
50 static int colon_seen;
52 /* The current binding label (if any). */
53 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
54 /* Need to know how many identifiers are on the current data declaration
55 line in case we're given the BIND(C) attribute with a NAME= specifier. */
56 static int num_idents_on_line;
57 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
58 can supply a name if the curr_binding_label is nil and NAME= was not. */
59 static int has_name_equals = 0;
61 /* Initializer of the previous enumerator. */
63 static gfc_expr *last_initializer;
65 /* History of all the enumerators is maintained, so that
66 kind values of all the enumerators could be updated depending
67 upon the maximum initialized value. */
69 typedef struct enumerator_history
72 gfc_expr *initializer;
73 struct enumerator_history *next;
77 /* Header of enum history chain. */
79 static enumerator_history *enum_history = NULL;
81 /* Pointer of enum history node containing largest initializer. */
83 static enumerator_history *max_enum = NULL;
85 /* gfc_new_block points to the symbol of a newly matched block. */
87 gfc_symbol *gfc_new_block;
89 bool gfc_matching_function;
92 /********************* DATA statement subroutines *********************/
94 static bool in_match_data = false;
97 gfc_in_match_data (void)
103 set_in_match_data (bool set_value)
105 in_match_data = set_value;
108 /* Free a gfc_data_variable structure and everything beneath it. */
111 free_variable (gfc_data_variable *p)
113 gfc_data_variable *q;
118 gfc_free_expr (p->expr);
119 gfc_free_iterator (&p->iter, 0);
120 free_variable (p->list);
126 /* Free a gfc_data_value structure and everything beneath it. */
129 free_value (gfc_data_value *p)
136 gfc_free_expr (p->expr);
142 /* Free a list of gfc_data structures. */
145 gfc_free_data (gfc_data *p)
152 free_variable (p->var);
153 free_value (p->value);
159 /* Free all data in a namespace. */
162 gfc_free_data_all (gfc_namespace *ns)
175 static match var_element (gfc_data_variable *);
177 /* Match a list of variables terminated by an iterator and a right
181 var_list (gfc_data_variable *parent)
183 gfc_data_variable *tail, var;
186 m = var_element (&var);
187 if (m == MATCH_ERROR)
192 tail = gfc_get_data_variable ();
199 if (gfc_match_char (',') != MATCH_YES)
202 m = gfc_match_iterator (&parent->iter, 1);
205 if (m == MATCH_ERROR)
208 m = var_element (&var);
209 if (m == MATCH_ERROR)
214 tail->next = gfc_get_data_variable ();
220 if (gfc_match_char (')') != MATCH_YES)
225 gfc_syntax_error (ST_DATA);
230 /* Match a single element in a data variable list, which can be a
231 variable-iterator list. */
234 var_element (gfc_data_variable *new_var)
239 memset (new_var, 0, sizeof (gfc_data_variable));
241 if (gfc_match_char ('(') == MATCH_YES)
242 return var_list (new_var);
244 m = gfc_match_variable (&new_var->expr, 0);
248 sym = new_var->expr->symtree->n.sym;
250 /* Symbol should already have an associated type. */
251 if (gfc_check_symbol_typed (sym, gfc_current_ns,
252 false, gfc_current_locus) == FAILURE)
255 if (!sym->attr.function && gfc_current_ns->parent
256 && gfc_current_ns->parent == sym->ns)
258 gfc_error ("Host associated variable '%s' may not be in the DATA "
259 "statement at %C", sym->name);
263 if (gfc_current_state () != COMP_BLOCK_DATA
264 && sym->attr.in_common
265 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
266 "common block variable '%s' in DATA statement at %C",
267 sym->name) == FAILURE)
270 if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
277 /* Match the top-level list of data variables. */
280 top_var_list (gfc_data *d)
282 gfc_data_variable var, *tail, *new_var;
289 m = var_element (&var);
292 if (m == MATCH_ERROR)
295 new_var = gfc_get_data_variable ();
301 tail->next = new_var;
305 if (gfc_match_char ('/') == MATCH_YES)
307 if (gfc_match_char (',') != MATCH_YES)
314 gfc_syntax_error (ST_DATA);
315 gfc_free_data_all (gfc_current_ns);
321 match_data_constant (gfc_expr **result)
323 char name[GFC_MAX_SYMBOL_LEN + 1];
329 m = gfc_match_literal_constant (&expr, 1);
336 if (m == MATCH_ERROR)
339 m = gfc_match_null (result);
343 old_loc = gfc_current_locus;
345 /* Should this be a structure component, try to match it
346 before matching a name. */
347 m = gfc_match_rvalue (result);
348 if (m == MATCH_ERROR)
351 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
353 if (gfc_simplify_expr (*result, 0) == FAILURE)
358 gfc_current_locus = old_loc;
360 m = gfc_match_name (name);
364 if (gfc_find_symbol (name, NULL, 1, &sym))
368 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
370 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
374 else if (sym->attr.flavor == FL_DERIVED)
375 return gfc_match_structure_constructor (sym, result, false);
377 /* Check to see if the value is an initialization array expression. */
378 if (sym->value->expr_type == EXPR_ARRAY)
380 gfc_current_locus = old_loc;
382 m = gfc_match_init_expr (result);
383 if (m == MATCH_ERROR)
388 if (gfc_simplify_expr (*result, 0) == FAILURE)
391 if ((*result)->expr_type == EXPR_CONSTANT)
395 gfc_error ("Invalid initializer %s in Data statement at %C", name);
401 *result = gfc_copy_expr (sym->value);
406 /* Match a list of values in a DATA statement. The leading '/' has
407 already been seen at this point. */
410 top_val_list (gfc_data *data)
412 gfc_data_value *new_val, *tail;
420 m = match_data_constant (&expr);
423 if (m == MATCH_ERROR)
426 new_val = gfc_get_data_value ();
427 mpz_init (new_val->repeat);
430 data->value = new_val;
432 tail->next = new_val;
436 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
439 mpz_set_ui (tail->repeat, 1);
443 if (expr->ts.type == BT_INTEGER)
444 mpz_set (tail->repeat, expr->value.integer);
445 gfc_free_expr (expr);
447 m = match_data_constant (&tail->expr);
450 if (m == MATCH_ERROR)
454 if (gfc_match_char ('/') == MATCH_YES)
456 if (gfc_match_char (',') == MATCH_NO)
463 gfc_syntax_error (ST_DATA);
464 gfc_free_data_all (gfc_current_ns);
469 /* Matches an old style initialization. */
472 match_old_style_init (const char *name)
479 /* Set up data structure to hold initializers. */
480 gfc_find_sym_tree (name, NULL, 0, &st);
483 newdata = gfc_get_data ();
484 newdata->var = gfc_get_data_variable ();
485 newdata->var->expr = gfc_get_variable_expr (st);
486 newdata->where = gfc_current_locus;
488 /* Match initial value list. This also eats the terminal '/'. */
489 m = top_val_list (newdata);
498 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
503 /* Mark the variable as having appeared in a data statement. */
504 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
510 /* Chain in namespace list of DATA initializers. */
511 newdata->next = gfc_current_ns->data;
512 gfc_current_ns->data = newdata;
518 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
519 we are matching a DATA statement and are therefore issuing an error
520 if we encounter something unexpected, if not, we're trying to match
521 an old-style initialization expression of the form INTEGER I /2/. */
524 gfc_match_data (void)
529 set_in_match_data (true);
533 new_data = gfc_get_data ();
534 new_data->where = gfc_current_locus;
536 m = top_var_list (new_data);
540 m = top_val_list (new_data);
544 new_data->next = gfc_current_ns->data;
545 gfc_current_ns->data = new_data;
547 if (gfc_match_eos () == MATCH_YES)
550 gfc_match_char (','); /* Optional comma */
553 set_in_match_data (false);
557 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
564 set_in_match_data (false);
565 gfc_free_data (new_data);
570 /************************ Declaration statements *********************/
572 /* Match an intent specification. Since this can only happen after an
573 INTENT word, a legal intent-spec must follow. */
576 match_intent_spec (void)
579 if (gfc_match (" ( in out )") == MATCH_YES)
581 if (gfc_match (" ( in )") == MATCH_YES)
583 if (gfc_match (" ( out )") == MATCH_YES)
586 gfc_error ("Bad INTENT specification at %C");
587 return INTENT_UNKNOWN;
591 /* Matches a character length specification, which is either a
592 specification expression or a '*'. */
595 char_len_param_value (gfc_expr **expr)
599 if (gfc_match_char ('*') == MATCH_YES)
605 m = gfc_match_expr (expr);
608 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
611 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
613 if ((*expr)->value.function.actual
614 && (*expr)->value.function.actual->expr->symtree)
617 e = (*expr)->value.function.actual->expr;
618 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
619 && e->expr_type == EXPR_VARIABLE)
621 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
623 if (e->symtree->n.sym->ts.type == BT_CHARACTER
624 && e->symtree->n.sym->ts.cl
625 && e->symtree->n.sym->ts.cl->length->ts.type == BT_UNKNOWN)
633 gfc_error ("Conflict in attributes of function argument at %C");
638 /* A character length is a '*' followed by a literal integer or a
639 char_len_param_value in parenthesis. */
642 match_char_length (gfc_expr **expr)
647 m = gfc_match_char ('*');
651 m = gfc_match_small_literal_int (&length, NULL);
652 if (m == MATCH_ERROR)
657 *expr = gfc_int_expr (length);
661 if (gfc_match_char ('(') == MATCH_NO)
664 m = char_len_param_value (expr);
665 if (m != MATCH_YES && gfc_matching_function)
671 if (m == MATCH_ERROR)
676 if (gfc_match_char (')') == MATCH_NO)
678 gfc_free_expr (*expr);
686 gfc_error ("Syntax error in character length specification at %C");
691 /* Special subroutine for finding a symbol. Check if the name is found
692 in the current name space. If not, and we're compiling a function or
693 subroutine and the parent compilation unit is an interface, then check
694 to see if the name we've been given is the name of the interface
695 (located in another namespace). */
698 find_special (const char *name, gfc_symbol **result)
703 i = gfc_get_symbol (name, NULL, result);
707 if (gfc_current_state () != COMP_SUBROUTINE
708 && gfc_current_state () != COMP_FUNCTION)
711 s = gfc_state_stack->previous;
715 if (s->state != COMP_INTERFACE)
718 goto end; /* Nameless interface. */
720 if (strcmp (name, s->sym->name) == 0)
731 /* Special subroutine for getting a symbol node associated with a
732 procedure name, used in SUBROUTINE and FUNCTION statements. The
733 symbol is created in the parent using with symtree node in the
734 child unit pointing to the symbol. If the current namespace has no
735 parent, then the symbol is just created in the current unit. */
738 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
744 /* Module functions have to be left in their own namespace because
745 they have potentially (almost certainly!) already been referenced.
746 In this sense, they are rather like external functions. This is
747 fixed up in resolve.c(resolve_entries), where the symbol name-
748 space is set to point to the master function, so that the fake
749 result mechanism can work. */
750 if (module_fcn_entry)
752 /* Present if entry is declared to be a module procedure. */
753 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
756 rc = gfc_get_symbol (name, NULL, result);
757 else if (!gfc_get_symbol (name, NULL, &sym) && sym
758 && (*result)->ts.type == BT_UNKNOWN
759 && sym->attr.flavor == FL_UNKNOWN)
760 /* Pick up the typespec for the entry, if declared in the function
761 body. Note that this symbol is FL_UNKNOWN because it will
762 only have appeared in a type declaration. The local symtree
763 is set to point to the module symbol and a unique symtree
764 to the local version. This latter ensures a correct clearing
767 /* If the ENTRY proceeds its specification, we need to ensure
768 that this does not raise a "has no IMPLICIT type" error. */
769 if (sym->ts.type == BT_UNKNOWN)
770 sym->attr.untyped = 1;
772 (*result)->ts = sym->ts;
774 /* Put the symbol in the procedure namespace so that, should
775 the ENTRY precede its specification, the specification
777 (*result)->ns = gfc_current_ns;
779 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
781 st = gfc_get_unique_symtree (gfc_current_ns);
786 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
792 gfc_current_ns->refs++;
794 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
796 /* Trap another encompassed procedure with the same name. All
797 these conditions are necessary to avoid picking up an entry
798 whose name clashes with that of the encompassing procedure;
799 this is handled using gsymbols to register unique,globally
801 if (sym->attr.flavor != 0
802 && sym->attr.proc != 0
803 && (sym->attr.subroutine || sym->attr.function)
804 && sym->attr.if_source != IFSRC_UNKNOWN)
805 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
806 name, &sym->declared_at);
808 /* Trap a procedure with a name the same as interface in the
809 encompassing scope. */
810 if (sym->attr.generic != 0
811 && (sym->attr.subroutine || sym->attr.function)
812 && !sym->attr.mod_proc)
813 gfc_error_now ("Name '%s' at %C is already defined"
814 " as a generic interface at %L",
815 name, &sym->declared_at);
817 /* Trap declarations of attributes in encompassing scope. The
818 signature for this is that ts.kind is set. Legitimate
819 references only set ts.type. */
820 if (sym->ts.kind != 0
821 && !sym->attr.implicit_type
822 && sym->attr.proc == 0
823 && gfc_current_ns->parent != NULL
824 && sym->attr.access == 0
825 && !module_fcn_entry)
826 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
827 "and must not have attributes declared at %L",
828 name, &sym->declared_at);
831 if (gfc_current_ns->parent == NULL || *result == NULL)
834 /* Module function entries will already have a symtree in
835 the current namespace but will need one at module level. */
836 if (module_fcn_entry)
838 /* Present if entry is declared to be a module procedure. */
839 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
841 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
844 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
849 /* See if the procedure should be a module procedure. */
851 if (((sym->ns->proc_name != NULL
852 && sym->ns->proc_name->attr.flavor == FL_MODULE
853 && sym->attr.proc != PROC_MODULE)
854 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
855 && gfc_add_procedure (&sym->attr, PROC_MODULE,
856 sym->name, NULL) == FAILURE)
863 /* Verify that the given symbol representing a parameter is C
864 interoperable, by checking to see if it was marked as such after
865 its declaration. If the given symbol is not interoperable, a
866 warning is reported, thus removing the need to return the status to
867 the calling function. The standard does not require the user use
868 one of the iso_c_binding named constants to declare an
869 interoperable parameter, but we can't be sure if the param is C
870 interop or not if the user doesn't. For example, integer(4) may be
871 legal Fortran, but doesn't have meaning in C. It may interop with
872 a number of the C types, which causes a problem because the
873 compiler can't know which one. This code is almost certainly not
874 portable, and the user will get what they deserve if the C type
875 across platforms isn't always interoperable with integer(4). If
876 the user had used something like integer(c_int) or integer(c_long),
877 the compiler could have automatically handled the varying sizes
881 verify_c_interop_param (gfc_symbol *sym)
883 int is_c_interop = 0;
884 gfc_try retval = SUCCESS;
886 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
887 Don't repeat the checks here. */
888 if (sym->attr.implicit_type)
891 /* For subroutines or functions that are passed to a BIND(C) procedure,
892 they're interoperable if they're BIND(C) and their params are all
894 if (sym->attr.flavor == FL_PROCEDURE)
896 if (sym->attr.is_bind_c == 0)
898 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
899 "attribute to be C interoperable", sym->name,
900 &(sym->declared_at));
906 if (sym->attr.is_c_interop == 1)
907 /* We've already checked this procedure; don't check it again. */
910 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
915 /* See if we've stored a reference to a procedure that owns sym. */
916 if (sym->ns != NULL && sym->ns->proc_name != NULL)
918 if (sym->ns->proc_name->attr.is_bind_c == 1)
921 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
924 if (is_c_interop != 1)
926 /* Make personalized messages to give better feedback. */
927 if (sym->ts.type == BT_DERIVED)
928 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
929 " procedure '%s' but is not C interoperable "
930 "because derived type '%s' is not C interoperable",
931 sym->name, &(sym->declared_at),
932 sym->ns->proc_name->name,
933 sym->ts.derived->name);
935 gfc_warning ("Variable '%s' at %L is a parameter to the "
936 "BIND(C) procedure '%s' but may not be C "
938 sym->name, &(sym->declared_at),
939 sym->ns->proc_name->name);
942 /* Character strings are only C interoperable if they have a
944 if (sym->ts.type == BT_CHARACTER)
946 gfc_charlen *cl = sym->ts.cl;
947 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
948 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
950 gfc_error ("Character argument '%s' at %L "
951 "must be length 1 because "
952 "procedure '%s' is BIND(C)",
953 sym->name, &sym->declared_at,
954 sym->ns->proc_name->name);
959 /* We have to make sure that any param to a bind(c) routine does
960 not have the allocatable, pointer, or optional attributes,
961 according to J3/04-007, section 5.1. */
962 if (sym->attr.allocatable == 1)
964 gfc_error ("Variable '%s' at %L cannot have the "
965 "ALLOCATABLE attribute because procedure '%s'"
966 " is BIND(C)", sym->name, &(sym->declared_at),
967 sym->ns->proc_name->name);
971 if (sym->attr.pointer == 1)
973 gfc_error ("Variable '%s' at %L cannot have the "
974 "POINTER attribute because procedure '%s'"
975 " is BIND(C)", sym->name, &(sym->declared_at),
976 sym->ns->proc_name->name);
980 if (sym->attr.optional == 1)
982 gfc_error ("Variable '%s' at %L cannot have the "
983 "OPTIONAL attribute because procedure '%s'"
984 " is BIND(C)", sym->name, &(sym->declared_at),
985 sym->ns->proc_name->name);
989 /* Make sure that if it has the dimension attribute, that it is
990 either assumed size or explicit shape. */
993 if (sym->as->type == AS_ASSUMED_SHAPE)
995 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
996 "argument to the procedure '%s' at %L because "
997 "the procedure is BIND(C)", sym->name,
998 &(sym->declared_at), sym->ns->proc_name->name,
999 &(sym->ns->proc_name->declared_at));
1003 if (sym->as->type == AS_DEFERRED)
1005 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1006 "argument to the procedure '%s' at %L because "
1007 "the procedure is BIND(C)", sym->name,
1008 &(sym->declared_at), sym->ns->proc_name->name,
1009 &(sym->ns->proc_name->declared_at));
1020 /* Function called by variable_decl() that adds a name to the symbol table. */
1023 build_sym (const char *name, gfc_charlen *cl,
1024 gfc_array_spec **as, locus *var_locus)
1026 symbol_attribute attr;
1029 if (gfc_get_symbol (name, NULL, &sym))
1032 /* Start updating the symbol table. Add basic type attribute if present. */
1033 if (current_ts.type != BT_UNKNOWN
1034 && (sym->attr.implicit_type == 0
1035 || !gfc_compare_types (&sym->ts, ¤t_ts))
1036 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
1039 if (sym->ts.type == BT_CHARACTER)
1042 /* Add dimension attribute if present. */
1043 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1047 /* Add attribute to symbol. The copy is so that we can reset the
1048 dimension attribute. */
1049 attr = current_attr;
1052 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1055 /* Finish any work that may need to be done for the binding label,
1056 if it's a bind(c). The bind(c) attr is found before the symbol
1057 is made, and before the symbol name (for data decls), so the
1058 current_ts is holding the binding label, or nothing if the
1059 name= attr wasn't given. Therefore, test here if we're dealing
1060 with a bind(c) and make sure the binding label is set correctly. */
1061 if (sym->attr.is_bind_c == 1)
1063 if (sym->binding_label[0] == '\0')
1065 /* Set the binding label and verify that if a NAME= was specified
1066 then only one identifier was in the entity-decl-list. */
1067 if (set_binding_label (sym->binding_label, sym->name,
1068 num_idents_on_line) == FAILURE)
1073 /* See if we know we're in a common block, and if it's a bind(c)
1074 common then we need to make sure we're an interoperable type. */
1075 if (sym->attr.in_common == 1)
1077 /* Test the common block object. */
1078 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1079 && sym->ts.is_c_interop != 1)
1081 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1082 "must be declared with a C interoperable "
1083 "kind since common block '%s' is BIND(C)",
1084 sym->name, sym->common_block->name,
1085 sym->common_block->name);
1090 sym->attr.implied_index = 0;
1096 /* Set character constant to the given length. The constant will be padded or
1097 truncated. If we're inside an array constructor without a typespec, we
1098 additionally check that all elements have the same length; check_len -1
1099 means no checking. */
1102 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1107 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1108 gcc_assert (expr->ts.type == BT_CHARACTER);
1110 slen = expr->value.character.length;
1113 s = gfc_get_wide_string (len + 1);
1114 memcpy (s, expr->value.character.string,
1115 MIN (len, slen) * sizeof (gfc_char_t));
1117 gfc_wide_memset (&s[slen], ' ', len - slen);
1119 if (gfc_option.warn_character_truncation && slen > len)
1120 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1121 "(%d/%d)", &expr->where, slen, len);
1123 /* Apply the standard by 'hand' otherwise it gets cleared for
1125 if (check_len != -1 && slen != check_len
1126 && !(gfc_option.allow_std & GFC_STD_GNU))
1127 gfc_error_now ("The CHARACTER elements of the array constructor "
1128 "at %L must have the same length (%d/%d)",
1129 &expr->where, slen, check_len);
1132 gfc_free (expr->value.character.string);
1133 expr->value.character.string = s;
1134 expr->value.character.length = len;
1139 /* Function to create and update the enumerator history
1140 using the information passed as arguments.
1141 Pointer "max_enum" is also updated, to point to
1142 enum history node containing largest initializer.
1144 SYM points to the symbol node of enumerator.
1145 INIT points to its enumerator value. */
1148 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1150 enumerator_history *new_enum_history;
1151 gcc_assert (sym != NULL && init != NULL);
1153 new_enum_history = XCNEW (enumerator_history);
1155 new_enum_history->sym = sym;
1156 new_enum_history->initializer = init;
1157 new_enum_history->next = NULL;
1159 if (enum_history == NULL)
1161 enum_history = new_enum_history;
1162 max_enum = enum_history;
1166 new_enum_history->next = enum_history;
1167 enum_history = new_enum_history;
1169 if (mpz_cmp (max_enum->initializer->value.integer,
1170 new_enum_history->initializer->value.integer) < 0)
1171 max_enum = new_enum_history;
1176 /* Function to free enum kind history. */
1179 gfc_free_enum_history (void)
1181 enumerator_history *current = enum_history;
1182 enumerator_history *next;
1184 while (current != NULL)
1186 next = current->next;
1191 enum_history = NULL;
1195 /* Function called by variable_decl() that adds an initialization
1196 expression to a symbol. */
1199 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1201 symbol_attribute attr;
1206 if (find_special (name, &sym))
1211 /* If this symbol is confirming an implicit parameter type,
1212 then an initialization expression is not allowed. */
1213 if (attr.flavor == FL_PARAMETER
1214 && sym->value != NULL
1217 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1224 /* An initializer is required for PARAMETER declarations. */
1225 if (attr.flavor == FL_PARAMETER)
1227 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1233 /* If a variable appears in a DATA block, it cannot have an
1237 gfc_error ("Variable '%s' at %C with an initializer already "
1238 "appears in a DATA statement", sym->name);
1242 /* Check if the assignment can happen. This has to be put off
1243 until later for a derived type variable. */
1244 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1245 && gfc_check_assign_symbol (sym, init) == FAILURE)
1248 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1250 /* Update symbol character length according initializer. */
1251 if (sym->ts.cl->length == NULL)
1254 /* If there are multiple CHARACTER variables declared on the
1255 same line, we don't want them to share the same length. */
1256 sym->ts.cl = gfc_get_charlen ();
1257 sym->ts.cl->next = gfc_current_ns->cl_list;
1258 gfc_current_ns->cl_list = sym->ts.cl;
1260 if (sym->attr.flavor == FL_PARAMETER)
1262 if (init->expr_type == EXPR_CONSTANT)
1264 clen = init->value.character.length;
1265 sym->ts.cl->length = gfc_int_expr (clen);
1267 else if (init->expr_type == EXPR_ARRAY)
1269 gfc_expr *p = init->value.constructor->expr;
1270 clen = p->value.character.length;
1271 sym->ts.cl->length = gfc_int_expr (clen);
1273 else if (init->ts.cl && init->ts.cl->length)
1274 sym->ts.cl->length =
1275 gfc_copy_expr (sym->value->ts.cl->length);
1278 /* Update initializer character length according symbol. */
1279 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1281 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1282 gfc_constructor * p;
1284 if (init->expr_type == EXPR_CONSTANT)
1285 gfc_set_constant_character_len (len, init, -1);
1286 else if (init->expr_type == EXPR_ARRAY)
1288 /* Build a new charlen to prevent simplification from
1289 deleting the length before it is resolved. */
1290 init->ts.cl = gfc_get_charlen ();
1291 init->ts.cl->next = gfc_current_ns->cl_list;
1292 gfc_current_ns->cl_list = sym->ts.cl;
1293 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
1295 for (p = init->value.constructor; p; p = p->next)
1296 gfc_set_constant_character_len (len, p->expr, -1);
1301 /* Need to check if the expression we initialized this
1302 to was one of the iso_c_binding named constants. If so,
1303 and we're a parameter (constant), let it be iso_c.
1305 integer(c_int), parameter :: my_int = c_int
1306 integer(my_int) :: my_int_2
1307 If we mark my_int as iso_c (since we can see it's value
1308 is equal to one of the named constants), then my_int_2
1309 will be considered C interoperable. */
1310 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1312 sym->ts.is_iso_c |= init->ts.is_iso_c;
1313 sym->ts.is_c_interop |= init->ts.is_c_interop;
1314 /* attr bits needed for module files. */
1315 sym->attr.is_iso_c |= init->ts.is_iso_c;
1316 sym->attr.is_c_interop |= init->ts.is_c_interop;
1317 if (init->ts.is_iso_c)
1318 sym->ts.f90_type = init->ts.f90_type;
1321 /* Add initializer. Make sure we keep the ranks sane. */
1322 if (sym->attr.dimension && init->rank == 0)
1328 if (sym->attr.flavor == FL_PARAMETER
1329 && init->expr_type == EXPR_CONSTANT
1330 && spec_size (sym->as, &size) == SUCCESS
1331 && mpz_cmp_si (size, 0) > 0)
1333 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1336 array->value.constructor = c = NULL;
1337 for (n = 0; n < (int)mpz_get_si (size); n++)
1339 if (array->value.constructor == NULL)
1341 array->value.constructor = c = gfc_get_constructor ();
1346 c->next = gfc_get_constructor ();
1348 c->expr = gfc_copy_expr (init);
1352 array->shape = gfc_get_shape (sym->as->rank);
1353 for (n = 0; n < sym->as->rank; n++)
1354 spec_dimen_size (sym->as, n, &array->shape[n]);
1359 init->rank = sym->as->rank;
1363 if (sym->attr.save == SAVE_NONE)
1364 sym->attr.save = SAVE_IMPLICIT;
1372 /* Function called by variable_decl() that adds a name to a structure
1376 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1377 gfc_array_spec **as)
1381 /* If the current symbol is of the same derived type that we're
1382 constructing, it must have the pointer attribute. */
1383 if (current_ts.type == BT_DERIVED
1384 && current_ts.derived == gfc_current_block ()
1385 && current_attr.pointer == 0)
1387 gfc_error ("Component at %C must have the POINTER attribute");
1391 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1393 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1395 gfc_error ("Array component of structure at %C must have explicit "
1396 "or deferred shape");
1401 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1406 gfc_set_component_attr (c, ¤t_attr);
1408 c->initializer = *init;
1416 /* Should this ever get more complicated, combine with similar section
1417 in add_init_expr_to_sym into a separate function. */
1418 if (c->ts.type == BT_CHARACTER && !c->pointer && c->initializer && c->ts.cl
1419 && c->ts.cl->length && c->ts.cl->length->expr_type == EXPR_CONSTANT)
1423 gcc_assert (c->ts.cl && c->ts.cl->length);
1424 gcc_assert (c->ts.cl->length->expr_type == EXPR_CONSTANT);
1425 gcc_assert (c->ts.cl->length->ts.type == BT_INTEGER);
1427 len = mpz_get_si (c->ts.cl->length->value.integer);
1429 if (c->initializer->expr_type == EXPR_CONSTANT)
1430 gfc_set_constant_character_len (len, c->initializer, -1);
1431 else if (mpz_cmp (c->ts.cl->length->value.integer,
1432 c->initializer->ts.cl->length->value.integer))
1435 gfc_constructor *ctor = c->initializer->value.constructor;
1440 has_ts = (c->initializer->ts.cl
1441 && c->initializer->ts.cl->length_from_typespec);
1443 for (; ctor; ctor = ctor->next)
1445 /* Remember the length of the first element for checking that
1446 all elements *in the constructor* have the same length. This
1447 need not be the length of the LHS! */
1450 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1451 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1452 first_len = ctor->expr->value.character.length;
1456 if (ctor->expr->expr_type == EXPR_CONSTANT)
1457 gfc_set_constant_character_len (len, ctor->expr,
1458 has_ts ? -1 : first_len);
1463 /* Check array components. */
1468 gfc_error ("Allocatable component at %C must be an array");
1477 if (c->as->type != AS_DEFERRED)
1479 gfc_error ("Pointer array component of structure at %C must have a "
1484 else if (c->allocatable)
1486 if (c->as->type != AS_DEFERRED)
1488 gfc_error ("Allocatable component of structure at %C must have a "
1495 if (c->as->type != AS_EXPLICIT)
1497 gfc_error ("Array component of structure at %C must have an "
1507 /* Match a 'NULL()', and possibly take care of some side effects. */
1510 gfc_match_null (gfc_expr **result)
1516 m = gfc_match (" null ( )");
1520 /* The NULL symbol now has to be/become an intrinsic function. */
1521 if (gfc_get_symbol ("null", NULL, &sym))
1523 gfc_error ("NULL() initialization at %C is ambiguous");
1527 gfc_intrinsic_symbol (sym);
1529 if (sym->attr.proc != PROC_INTRINSIC
1530 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1531 sym->name, NULL) == FAILURE
1532 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1535 e = gfc_get_expr ();
1536 e->where = gfc_current_locus;
1537 e->expr_type = EXPR_NULL;
1538 e->ts.type = BT_UNKNOWN;
1546 /* Match a variable name with an optional initializer. When this
1547 subroutine is called, a variable is expected to be parsed next.
1548 Depending on what is happening at the moment, updates either the
1549 symbol table or the current interface. */
1552 variable_decl (int elem)
1554 char name[GFC_MAX_SYMBOL_LEN + 1];
1555 gfc_expr *initializer, *char_len;
1557 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1568 old_locus = gfc_current_locus;
1570 /* When we get here, we've just matched a list of attributes and
1571 maybe a type and a double colon. The next thing we expect to see
1572 is the name of the symbol. */
1573 m = gfc_match_name (name);
1577 var_locus = gfc_current_locus;
1579 /* Now we could see the optional array spec. or character length. */
1580 m = gfc_match_array_spec (&as);
1581 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1582 cp_as = gfc_copy_array_spec (as);
1583 else if (m == MATCH_ERROR)
1587 as = gfc_copy_array_spec (current_as);
1592 if (current_ts.type == BT_CHARACTER)
1594 switch (match_char_length (&char_len))
1597 cl = gfc_get_charlen ();
1598 cl->next = gfc_current_ns->cl_list;
1599 gfc_current_ns->cl_list = cl;
1601 cl->length = char_len;
1604 /* Non-constant lengths need to be copied after the first
1605 element. Also copy assumed lengths. */
1608 && (current_ts.cl->length == NULL
1609 || current_ts.cl->length->expr_type != EXPR_CONSTANT))
1611 cl = gfc_get_charlen ();
1612 cl->next = gfc_current_ns->cl_list;
1613 gfc_current_ns->cl_list = cl;
1614 cl->length = gfc_copy_expr (current_ts.cl->length);
1626 /* If this symbol has already shown up in a Cray Pointer declaration,
1627 then we want to set the type & bail out. */
1628 if (gfc_option.flag_cray_pointer)
1630 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1631 if (sym != NULL && sym->attr.cray_pointee)
1633 sym->ts.type = current_ts.type;
1634 sym->ts.kind = current_ts.kind;
1636 sym->ts.derived = current_ts.derived;
1637 sym->ts.is_c_interop = current_ts.is_c_interop;
1638 sym->ts.is_iso_c = current_ts.is_iso_c;
1641 /* Check to see if we have an array specification. */
1644 if (sym->as != NULL)
1646 gfc_error ("Duplicate array spec for Cray pointee at %C");
1647 gfc_free_array_spec (cp_as);
1653 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1654 gfc_internal_error ("Couldn't set pointee array spec.");
1656 /* Fix the array spec. */
1657 m = gfc_mod_pointee_as (sym->as);
1658 if (m == MATCH_ERROR)
1666 gfc_free_array_spec (cp_as);
1671 /* OK, we've successfully matched the declaration. Now put the
1672 symbol in the current namespace, because it might be used in the
1673 optional initialization expression for this symbol, e.g. this is
1676 integer, parameter :: i = huge(i)
1678 This is only true for parameters or variables of a basic type.
1679 For components of derived types, it is not true, so we don't
1680 create a symbol for those yet. If we fail to create the symbol,
1682 if (gfc_current_state () != COMP_DERIVED
1683 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1689 /* An interface body specifies all of the procedure's
1690 characteristics and these shall be consistent with those
1691 specified in the procedure definition, except that the interface
1692 may specify a procedure that is not pure if the procedure is
1693 defined to be pure(12.3.2). */
1694 if (current_ts.type == BT_DERIVED
1695 && gfc_current_ns->proc_name
1696 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1697 && current_ts.derived->ns != gfc_current_ns)
1700 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.derived->name);
1701 if (!(current_ts.derived->attr.imported
1703 && st->n.sym == current_ts.derived)
1704 && !gfc_current_ns->has_import_set)
1706 gfc_error ("the type of '%s' at %C has not been declared within the "
1713 /* In functions that have a RESULT variable defined, the function
1714 name always refers to function calls. Therefore, the name is
1715 not allowed to appear in specification statements. */
1716 if (gfc_current_state () == COMP_FUNCTION
1717 && gfc_current_block () != NULL
1718 && gfc_current_block ()->result != NULL
1719 && gfc_current_block ()->result != gfc_current_block ()
1720 && strcmp (gfc_current_block ()->name, name) == 0)
1722 gfc_error ("Function name '%s' not allowed at %C", name);
1727 /* We allow old-style initializations of the form
1728 integer i /2/, j(4) /3*3, 1/
1729 (if no colon has been seen). These are different from data
1730 statements in that initializers are only allowed to apply to the
1731 variable immediately preceding, i.e.
1733 is not allowed. Therefore we have to do some work manually, that
1734 could otherwise be left to the matchers for DATA statements. */
1736 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1738 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1739 "initialization at %C") == FAILURE)
1742 return match_old_style_init (name);
1745 /* The double colon must be present in order to have initializers.
1746 Otherwise the statement is ambiguous with an assignment statement. */
1749 if (gfc_match (" =>") == MATCH_YES)
1751 if (!current_attr.pointer)
1753 gfc_error ("Initialization at %C isn't for a pointer variable");
1758 m = gfc_match_null (&initializer);
1761 gfc_error ("Pointer initialization requires a NULL() at %C");
1765 if (gfc_pure (NULL))
1767 gfc_error ("Initialization of pointer at %C is not allowed in "
1768 "a PURE procedure");
1776 else if (gfc_match_char ('=') == MATCH_YES)
1778 if (current_attr.pointer)
1780 gfc_error ("Pointer initialization at %C requires '=>', "
1786 m = gfc_match_init_expr (&initializer);
1789 gfc_error ("Expected an initialization expression at %C");
1793 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1795 gfc_error ("Initialization of variable at %C is not allowed in "
1796 "a PURE procedure");
1805 if (initializer != NULL && current_attr.allocatable
1806 && gfc_current_state () == COMP_DERIVED)
1808 gfc_error ("Initialization of allocatable component at %C is not "
1814 /* Add the initializer. Note that it is fine if initializer is
1815 NULL here, because we sometimes also need to check if a
1816 declaration *must* have an initialization expression. */
1817 if (gfc_current_state () != COMP_DERIVED)
1818 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1821 if (current_ts.type == BT_DERIVED
1822 && !current_attr.pointer && !initializer)
1823 initializer = gfc_default_initializer (¤t_ts);
1824 t = build_struct (name, cl, &initializer, &as);
1827 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1830 /* Free stuff up and return. */
1831 gfc_free_expr (initializer);
1832 gfc_free_array_spec (as);
1838 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1839 This assumes that the byte size is equal to the kind number for
1840 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1843 gfc_match_old_kind_spec (gfc_typespec *ts)
1848 if (gfc_match_char ('*') != MATCH_YES)
1851 m = gfc_match_small_literal_int (&ts->kind, NULL);
1855 original_kind = ts->kind;
1857 /* Massage the kind numbers for complex types. */
1858 if (ts->type == BT_COMPLEX)
1862 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1863 gfc_basic_typename (ts->type), original_kind);
1869 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1871 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1872 gfc_basic_typename (ts->type), original_kind);
1876 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1877 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1884 /* Match a kind specification. Since kinds are generally optional, we
1885 usually return MATCH_NO if something goes wrong. If a "kind="
1886 string is found, then we know we have an error. */
1889 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
1901 where = loc = gfc_current_locus;
1906 if (gfc_match_char ('(') == MATCH_NO)
1909 /* Also gobbles optional text. */
1910 if (gfc_match (" kind = ") == MATCH_YES)
1913 loc = gfc_current_locus;
1916 n = gfc_match_init_expr (&e);
1920 if (gfc_matching_function)
1922 /* The function kind expression might include use associated or
1923 imported parameters and try again after the specification
1925 if (gfc_match_char (')') != MATCH_YES)
1927 gfc_error ("Missing right parenthesis at %C");
1933 gfc_undo_symbols ();
1938 /* ....or else, the match is real. */
1940 gfc_error ("Expected initialization expression at %C");
1948 gfc_error ("Expected scalar initialization expression at %C");
1953 msg = gfc_extract_int (e, &ts->kind);
1962 /* Before throwing away the expression, let's see if we had a
1963 C interoperable kind (and store the fact). */
1964 if (e->ts.is_c_interop == 1)
1966 /* Mark this as c interoperable if being declared with one
1967 of the named constants from iso_c_binding. */
1968 ts->is_c_interop = e->ts.is_iso_c;
1969 ts->f90_type = e->ts.f90_type;
1975 /* Ignore errors to this point, if we've gotten here. This means
1976 we ignore the m=MATCH_ERROR from above. */
1977 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1979 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1980 gfc_basic_typename (ts->type));
1981 gfc_current_locus = where;
1985 gfc_gobble_whitespace ();
1986 if ((c = gfc_next_ascii_char ()) != ')'
1987 && (ts->type != BT_CHARACTER || c != ','))
1989 if (ts->type == BT_CHARACTER)
1990 gfc_error ("Missing right parenthesis or comma at %C");
1992 gfc_error ("Missing right parenthesis at %C");
1996 /* All tests passed. */
1999 if(m == MATCH_ERROR)
2000 gfc_current_locus = where;
2002 /* Return what we know from the test(s). */
2007 gfc_current_locus = where;
2013 match_char_kind (int * kind, int * is_iso_c)
2022 where = gfc_current_locus;
2024 n = gfc_match_init_expr (&e);
2026 if (n != MATCH_YES && gfc_matching_function)
2028 /* The expression might include use-associated or imported
2029 parameters and try again after the specification
2032 gfc_undo_symbols ();
2037 gfc_error ("Expected initialization expression at %C");
2043 gfc_error ("Expected scalar initialization expression at %C");
2048 msg = gfc_extract_int (e, kind);
2049 *is_iso_c = e->ts.is_iso_c;
2059 /* Ignore errors to this point, if we've gotten here. This means
2060 we ignore the m=MATCH_ERROR from above. */
2061 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2063 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2067 /* All tests passed. */
2070 if (m == MATCH_ERROR)
2071 gfc_current_locus = where;
2073 /* Return what we know from the test(s). */
2078 gfc_current_locus = where;
2082 /* Match the various kind/length specifications in a CHARACTER
2083 declaration. We don't return MATCH_NO. */
2086 match_char_spec (gfc_typespec *ts)
2088 int kind, seen_length, is_iso_c;
2098 /* Try the old-style specification first. */
2099 old_char_selector = 0;
2101 m = match_char_length (&len);
2105 old_char_selector = 1;
2110 m = gfc_match_char ('(');
2113 m = MATCH_YES; /* Character without length is a single char. */
2117 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2118 if (gfc_match (" kind =") == MATCH_YES)
2120 m = match_char_kind (&kind, &is_iso_c);
2122 if (m == MATCH_ERROR)
2127 if (gfc_match (" , len =") == MATCH_NO)
2130 m = char_len_param_value (&len);
2133 if (m == MATCH_ERROR)
2140 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2141 if (gfc_match (" len =") == MATCH_YES)
2143 m = char_len_param_value (&len);
2146 if (m == MATCH_ERROR)
2150 if (gfc_match_char (')') == MATCH_YES)
2153 if (gfc_match (" , kind =") != MATCH_YES)
2156 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2162 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2163 m = char_len_param_value (&len);
2166 if (m == MATCH_ERROR)
2170 m = gfc_match_char (')');
2174 if (gfc_match_char (',') != MATCH_YES)
2177 gfc_match (" kind ="); /* Gobble optional text. */
2179 m = match_char_kind (&kind, &is_iso_c);
2180 if (m == MATCH_ERROR)
2186 /* Require a right-paren at this point. */
2187 m = gfc_match_char (')');
2192 gfc_error ("Syntax error in CHARACTER declaration at %C");
2194 gfc_free_expr (len);
2198 /* Deal with character functions after USE and IMPORT statements. */
2199 if (gfc_matching_function)
2201 gfc_free_expr (len);
2202 gfc_undo_symbols ();
2208 gfc_free_expr (len);
2212 /* Do some final massaging of the length values. */
2213 cl = gfc_get_charlen ();
2214 cl->next = gfc_current_ns->cl_list;
2215 gfc_current_ns->cl_list = cl;
2217 if (seen_length == 0)
2218 cl->length = gfc_int_expr (1);
2223 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2225 /* We have to know if it was a c interoperable kind so we can
2226 do accurate type checking of bind(c) procs, etc. */
2228 /* Mark this as c interoperable if being declared with one
2229 of the named constants from iso_c_binding. */
2230 ts->is_c_interop = is_iso_c;
2231 else if (len != NULL)
2232 /* Here, we might have parsed something such as: character(c_char)
2233 In this case, the parsing code above grabs the c_char when
2234 looking for the length (line 1690, roughly). it's the last
2235 testcase for parsing the kind params of a character variable.
2236 However, it's not actually the length. this seems like it
2238 To see if the user used a C interop kind, test the expr
2239 of the so called length, and see if it's C interoperable. */
2240 ts->is_c_interop = len->ts.is_iso_c;
2246 /* Matches a type specification. If successful, sets the ts structure
2247 to the matched specification. This is necessary for FUNCTION and
2248 IMPLICIT statements.
2250 If implicit_flag is nonzero, then we don't check for the optional
2251 kind specification. Not doing so is needed for matching an IMPLICIT
2252 statement correctly. */
2255 gfc_match_type_spec (gfc_typespec *ts, int implicit_flag)
2257 char name[GFC_MAX_SYMBOL_LEN + 1];
2261 bool seen_deferred_kind;
2263 /* A belt and braces check that the typespec is correctly being treated
2264 as a deferred characteristic association. */
2265 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2266 && (gfc_current_block ()->result->ts.kind == -1)
2267 && (ts->kind == -1);
2269 if (seen_deferred_kind)
2272 /* Clear the current binding label, in case one is given. */
2273 curr_binding_label[0] = '\0';
2275 if (gfc_match (" byte") == MATCH_YES)
2277 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
2281 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2283 gfc_error ("BYTE type used at %C "
2284 "is not available on the target machine");
2288 ts->type = BT_INTEGER;
2293 if (gfc_match (" integer") == MATCH_YES)
2295 ts->type = BT_INTEGER;
2296 ts->kind = gfc_default_integer_kind;
2300 if (gfc_match (" character") == MATCH_YES)
2302 ts->type = BT_CHARACTER;
2303 if (implicit_flag == 0)
2304 return match_char_spec (ts);
2309 if (gfc_match (" real") == MATCH_YES)
2312 ts->kind = gfc_default_real_kind;
2316 if (gfc_match (" double precision") == MATCH_YES)
2319 ts->kind = gfc_default_double_kind;
2323 if (gfc_match (" complex") == MATCH_YES)
2325 ts->type = BT_COMPLEX;
2326 ts->kind = gfc_default_complex_kind;
2330 if (gfc_match (" double complex") == MATCH_YES)
2332 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2333 "conform to the Fortran 95 standard") == FAILURE)
2336 ts->type = BT_COMPLEX;
2337 ts->kind = gfc_default_double_kind;
2341 if (gfc_match (" logical") == MATCH_YES)
2343 ts->type = BT_LOGICAL;
2344 ts->kind = gfc_default_logical_kind;
2348 m = gfc_match (" type ( %n )", name);
2352 ts->type = BT_DERIVED;
2354 /* Defer association of the derived type until the end of the
2355 specification block. However, if the derived type can be
2356 found, add it to the typespec. */
2357 if (gfc_matching_function)
2360 if (gfc_current_state () != COMP_INTERFACE
2361 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2366 /* Search for the name but allow the components to be defined later. If
2367 type = -1, this typespec has been seen in a function declaration but
2368 the type could not be accessed at that point. */
2370 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2372 gfc_error ("Type name '%s' at %C is ambiguous", name);
2375 else if (ts->kind == -1)
2377 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2378 || gfc_current_ns->has_import_set;
2379 if (gfc_find_symbol (name, NULL, iface, &sym))
2381 gfc_error ("Type name '%s' at %C is ambiguous", name);
2390 if (sym->attr.flavor != FL_DERIVED
2391 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2394 gfc_set_sym_referenced (sym);
2400 /* For all types except double, derived and character, look for an
2401 optional kind specifier. MATCH_NO is actually OK at this point. */
2402 if (implicit_flag == 1)
2405 if (gfc_current_form == FORM_FREE)
2407 c = gfc_peek_ascii_char();
2408 if (!gfc_is_whitespace(c) && c != '*' && c != '('
2409 && c != ':' && c != ',')
2413 m = gfc_match_kind_spec (ts, false);
2414 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2415 m = gfc_match_old_kind_spec (ts);
2417 /* Defer association of the KIND expression of function results
2418 until after USE and IMPORT statements. */
2419 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2420 || gfc_matching_function)
2424 m = MATCH_YES; /* No kind specifier found. */
2430 /* Match an IMPLICIT NONE statement. Actually, this statement is
2431 already matched in parse.c, or we would not end up here in the
2432 first place. So the only thing we need to check, is if there is
2433 trailing garbage. If not, the match is successful. */
2436 gfc_match_implicit_none (void)
2438 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2442 /* Match the letter range(s) of an IMPLICIT statement. */
2445 match_implicit_range (void)
2451 cur_loc = gfc_current_locus;
2453 gfc_gobble_whitespace ();
2454 c = gfc_next_ascii_char ();
2457 gfc_error ("Missing character range in IMPLICIT at %C");
2464 gfc_gobble_whitespace ();
2465 c1 = gfc_next_ascii_char ();
2469 gfc_gobble_whitespace ();
2470 c = gfc_next_ascii_char ();
2475 inner = 0; /* Fall through. */
2482 gfc_gobble_whitespace ();
2483 c2 = gfc_next_ascii_char ();
2487 gfc_gobble_whitespace ();
2488 c = gfc_next_ascii_char ();
2490 if ((c != ',') && (c != ')'))
2503 gfc_error ("Letters must be in alphabetic order in "
2504 "IMPLICIT statement at %C");
2508 /* See if we can add the newly matched range to the pending
2509 implicits from this IMPLICIT statement. We do not check for
2510 conflicts with whatever earlier IMPLICIT statements may have
2511 set. This is done when we've successfully finished matching
2513 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2520 gfc_syntax_error (ST_IMPLICIT);
2522 gfc_current_locus = cur_loc;
2527 /* Match an IMPLICIT statement, storing the types for
2528 gfc_set_implicit() if the statement is accepted by the parser.
2529 There is a strange looking, but legal syntactic construction
2530 possible. It looks like:
2532 IMPLICIT INTEGER (a-b) (c-d)
2534 This is legal if "a-b" is a constant expression that happens to
2535 equal one of the legal kinds for integers. The real problem
2536 happens with an implicit specification that looks like:
2538 IMPLICIT INTEGER (a-b)
2540 In this case, a typespec matcher that is "greedy" (as most of the
2541 matchers are) gobbles the character range as a kindspec, leaving
2542 nothing left. We therefore have to go a bit more slowly in the
2543 matching process by inhibiting the kindspec checking during
2544 typespec matching and checking for a kind later. */
2547 gfc_match_implicit (void)
2556 /* We don't allow empty implicit statements. */
2557 if (gfc_match_eos () == MATCH_YES)
2559 gfc_error ("Empty IMPLICIT statement at %C");
2565 /* First cleanup. */
2566 gfc_clear_new_implicit ();
2568 /* A basic type is mandatory here. */
2569 m = gfc_match_type_spec (&ts, 1);
2570 if (m == MATCH_ERROR)
2575 cur_loc = gfc_current_locus;
2576 m = match_implicit_range ();
2580 /* We may have <TYPE> (<RANGE>). */
2581 gfc_gobble_whitespace ();
2582 c = gfc_next_ascii_char ();
2583 if ((c == '\n') || (c == ','))
2585 /* Check for CHARACTER with no length parameter. */
2586 if (ts.type == BT_CHARACTER && !ts.cl)
2588 ts.kind = gfc_default_character_kind;
2589 ts.cl = gfc_get_charlen ();
2590 ts.cl->next = gfc_current_ns->cl_list;
2591 gfc_current_ns->cl_list = ts.cl;
2592 ts.cl->length = gfc_int_expr (1);
2595 /* Record the Successful match. */
2596 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2601 gfc_current_locus = cur_loc;
2604 /* Discard the (incorrectly) matched range. */
2605 gfc_clear_new_implicit ();
2607 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2608 if (ts.type == BT_CHARACTER)
2609 m = match_char_spec (&ts);
2612 m = gfc_match_kind_spec (&ts, false);
2615 m = gfc_match_old_kind_spec (&ts);
2616 if (m == MATCH_ERROR)
2622 if (m == MATCH_ERROR)
2625 m = match_implicit_range ();
2626 if (m == MATCH_ERROR)
2631 gfc_gobble_whitespace ();
2632 c = gfc_next_ascii_char ();
2633 if ((c != '\n') && (c != ','))
2636 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2644 gfc_syntax_error (ST_IMPLICIT);
2652 gfc_match_import (void)
2654 char name[GFC_MAX_SYMBOL_LEN + 1];
2659 if (gfc_current_ns->proc_name == NULL
2660 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2662 gfc_error ("IMPORT statement at %C only permitted in "
2663 "an INTERFACE body");
2667 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2671 if (gfc_match_eos () == MATCH_YES)
2673 /* All host variables should be imported. */
2674 gfc_current_ns->has_import_set = 1;
2678 if (gfc_match (" ::") == MATCH_YES)
2680 if (gfc_match_eos () == MATCH_YES)
2682 gfc_error ("Expecting list of named entities at %C");
2689 m = gfc_match (" %n", name);
2693 if (gfc_current_ns->parent != NULL
2694 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2696 gfc_error ("Type name '%s' at %C is ambiguous", name);
2699 else if (gfc_current_ns->proc_name->ns->parent != NULL
2700 && gfc_find_symbol (name,
2701 gfc_current_ns->proc_name->ns->parent,
2704 gfc_error ("Type name '%s' at %C is ambiguous", name);
2710 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2711 "at %C - does not exist.", name);
2715 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2717 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2722 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2725 sym->attr.imported = 1;
2737 if (gfc_match_eos () == MATCH_YES)
2739 if (gfc_match_char (',') != MATCH_YES)
2746 gfc_error ("Syntax error in IMPORT statement at %C");
2751 /* A minimal implementation of gfc_match without whitespace, escape
2752 characters or variable arguments. Returns true if the next
2753 characters match the TARGET template exactly. */
2756 match_string_p (const char *target)
2760 for (p = target; *p; p++)
2761 if ((char) gfc_next_ascii_char () != *p)
2766 /* Matches an attribute specification including array specs. If
2767 successful, leaves the variables current_attr and current_as
2768 holding the specification. Also sets the colon_seen variable for
2769 later use by matchers associated with initializations.
2771 This subroutine is a little tricky in the sense that we don't know
2772 if we really have an attr-spec until we hit the double colon.
2773 Until that time, we can only return MATCH_NO. This forces us to
2774 check for duplicate specification at this level. */
2777 match_attr_spec (void)
2779 /* Modifiers that can exist in a type statement. */
2781 { GFC_DECL_BEGIN = 0,
2782 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2783 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2784 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2785 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2786 DECL_IS_BIND_C, DECL_NONE,
2787 GFC_DECL_END /* Sentinel */
2791 /* GFC_DECL_END is the sentinel, index starts at 0. */
2792 #define NUM_DECL GFC_DECL_END
2794 locus start, seen_at[NUM_DECL];
2801 gfc_clear_attr (¤t_attr);
2802 start = gfc_current_locus;
2807 /* See if we get all of the keywords up to the final double colon. */
2808 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2816 gfc_gobble_whitespace ();
2818 ch = gfc_next_ascii_char ();
2821 /* This is the successful exit condition for the loop. */
2822 if (gfc_next_ascii_char () == ':')
2827 gfc_gobble_whitespace ();
2828 switch (gfc_peek_ascii_char ())
2831 if (match_string_p ("allocatable"))
2832 d = DECL_ALLOCATABLE;
2836 /* Try and match the bind(c). */
2837 m = gfc_match_bind_c (NULL, true);
2840 else if (m == MATCH_ERROR)
2845 if (match_string_p ("dimension"))
2850 if (match_string_p ("external"))
2855 if (match_string_p ("int"))
2857 ch = gfc_next_ascii_char ();
2860 if (match_string_p ("nt"))
2862 /* Matched "intent". */
2863 /* TODO: Call match_intent_spec from here. */
2864 if (gfc_match (" ( in out )") == MATCH_YES)
2866 else if (gfc_match (" ( in )") == MATCH_YES)
2868 else if (gfc_match (" ( out )") == MATCH_YES)
2874 if (match_string_p ("insic"))
2876 /* Matched "intrinsic". */
2884 if (match_string_p ("optional"))
2889 gfc_next_ascii_char ();
2890 switch (gfc_next_ascii_char ())
2893 if (match_string_p ("rameter"))
2895 /* Matched "parameter". */
2901 if (match_string_p ("inter"))
2903 /* Matched "pointer". */
2909 ch = gfc_next_ascii_char ();
2912 if (match_string_p ("vate"))
2914 /* Matched "private". */
2920 if (match_string_p ("tected"))
2922 /* Matched "protected". */
2929 if (match_string_p ("blic"))
2931 /* Matched "public". */
2939 if (match_string_p ("save"))
2944 if (match_string_p ("target"))
2949 gfc_next_ascii_char ();
2950 ch = gfc_next_ascii_char ();
2953 if (match_string_p ("lue"))
2955 /* Matched "value". */
2961 if (match_string_p ("latile"))
2963 /* Matched "volatile". */
2971 /* No double colon and no recognizable decl_type, so assume that
2972 we've been looking at something else the whole time. */
2979 /* Check to make sure any parens are paired up correctly. */
2980 if (gfc_match_parens () == MATCH_ERROR)
2987 seen_at[d] = gfc_current_locus;
2989 if (d == DECL_DIMENSION)
2991 m = gfc_match_array_spec (¤t_as);
2995 gfc_error ("Missing dimension specification at %C");
2999 if (m == MATCH_ERROR)
3004 /* Since we've seen a double colon, we have to be looking at an
3005 attr-spec. This means that we can now issue errors. */
3006 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3011 case DECL_ALLOCATABLE:
3012 attr = "ALLOCATABLE";
3014 case DECL_DIMENSION:
3021 attr = "INTENT (IN)";
3024 attr = "INTENT (OUT)";
3027 attr = "INTENT (IN OUT)";
3029 case DECL_INTRINSIC:
3035 case DECL_PARAMETER:
3041 case DECL_PROTECTED:
3056 case DECL_IS_BIND_C:
3066 attr = NULL; /* This shouldn't happen. */
3069 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3074 /* Now that we've dealt with duplicate attributes, add the attributes
3075 to the current attribute. */
3076 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3081 if (gfc_current_state () == COMP_DERIVED
3082 && d != DECL_DIMENSION && d != DECL_POINTER
3083 && d != DECL_PRIVATE && d != DECL_PUBLIC
3086 if (d == DECL_ALLOCATABLE)
3088 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3089 "attribute at %C in a TYPE definition")
3098 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3105 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3106 && gfc_current_state () != COMP_MODULE)
3108 if (d == DECL_PRIVATE)
3112 if (gfc_current_state () == COMP_DERIVED
3113 && gfc_state_stack->previous
3114 && gfc_state_stack->previous->state == COMP_MODULE)
3116 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3117 "at %L in a TYPE definition", attr,
3127 gfc_error ("%s attribute at %L is not allowed outside of the "
3128 "specification part of a module", attr, &seen_at[d]);
3136 case DECL_ALLOCATABLE:
3137 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
3140 case DECL_DIMENSION:
3141 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
3145 t = gfc_add_external (¤t_attr, &seen_at[d]);
3149 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
3153 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
3157 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
3160 case DECL_INTRINSIC:
3161 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
3165 t = gfc_add_optional (¤t_attr, &seen_at[d]);
3168 case DECL_PARAMETER:
3169 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
3173 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
3176 case DECL_PROTECTED:
3177 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3179 gfc_error ("PROTECTED at %C only allowed in specification "
3180 "part of a module");
3185 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3190 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
3194 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
3199 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
3204 t = gfc_add_save (¤t_attr, NULL, &seen_at[d]);
3208 t = gfc_add_target (¤t_attr, &seen_at[d]);
3211 case DECL_IS_BIND_C:
3212 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
3216 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3221 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
3225 if (gfc_notify_std (GFC_STD_F2003,
3226 "Fortran 2003: VOLATILE attribute at %C")
3230 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
3234 gfc_internal_error ("match_attr_spec(): Bad attribute");
3248 gfc_current_locus = start;
3249 gfc_free_array_spec (current_as);
3255 /* Set the binding label, dest_label, either with the binding label
3256 stored in the given gfc_typespec, ts, or if none was provided, it
3257 will be the symbol name in all lower case, as required by the draft
3258 (J3/04-007, section 15.4.1). If a binding label was given and
3259 there is more than one argument (num_idents), it is an error. */
3262 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3264 if (num_idents > 1 && has_name_equals)
3266 gfc_error ("Multiple identifiers provided with "
3267 "single NAME= specifier at %C");
3271 if (curr_binding_label[0] != '\0')
3273 /* Binding label given; store in temp holder til have sym. */
3274 strcpy (dest_label, curr_binding_label);
3278 /* No binding label given, and the NAME= specifier did not exist,
3279 which means there was no NAME="". */
3280 if (sym_name != NULL && has_name_equals == 0)
3281 strcpy (dest_label, sym_name);
3288 /* Set the status of the given common block as being BIND(C) or not,
3289 depending on the given parameter, is_bind_c. */
3292 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3294 com_block->is_bind_c = is_bind_c;
3299 /* Verify that the given gfc_typespec is for a C interoperable type. */
3302 verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
3306 /* Make sure the kind used is appropriate for the type.
3307 The f90_type is unknown if an integer constant was
3308 used (e.g., real(4), bind(c) :: myFloat). */
3309 if (ts->f90_type != BT_UNKNOWN)
3311 t = gfc_validate_c_kind (ts);
3314 /* Print an error, but continue parsing line. */
3315 gfc_error_now ("C kind parameter is for type %s but "
3316 "symbol '%s' at %L is of type %s",
3317 gfc_basic_typename (ts->f90_type),
3319 gfc_basic_typename (ts->type));
3323 /* Make sure the kind is C interoperable. This does not care about the
3324 possible error above. */
3325 if (ts->type == BT_DERIVED && ts->derived != NULL)
3326 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
3327 else if (ts->is_c_interop != 1)
3334 /* Verify that the variables of a given common block, which has been
3335 defined with the attribute specifier bind(c), to be of a C
3336 interoperable type. Errors will be reported here, if
3340 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3342 gfc_symbol *curr_sym = NULL;
3343 gfc_try retval = SUCCESS;
3345 curr_sym = com_block->head;
3347 /* Make sure we have at least one symbol. */
3348 if (curr_sym == NULL)
3351 /* Here we know we have a symbol, so we'll execute this loop
3355 /* The second to last param, 1, says this is in a common block. */
3356 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3357 curr_sym = curr_sym->common_next;
3358 } while (curr_sym != NULL);
3364 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3365 an appropriate error message is reported. */
3368 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3369 int is_in_common, gfc_common_head *com_block)
3371 gfc_try retval = SUCCESS;
3373 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3375 tmp_sym = tmp_sym->result;
3376 /* Make sure it wasn't an implicitly typed result. */
3377 if (tmp_sym->attr.implicit_type)
3379 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3380 "%L may not be C interoperable", tmp_sym->name,
3381 &tmp_sym->declared_at);
3382 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3383 /* Mark it as C interoperable to prevent duplicate warnings. */
3384 tmp_sym->ts.is_c_interop = 1;
3385 tmp_sym->attr.is_c_interop = 1;
3389 /* Here, we know we have the bind(c) attribute, so if we have
3390 enough type info, then verify that it's a C interop kind.
3391 The info could be in the symbol already, or possibly still in
3392 the given ts (current_ts), so look in both. */
3393 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3395 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
3396 &(tmp_sym->declared_at)) != SUCCESS)
3398 /* See if we're dealing with a sym in a common block or not. */
3399 if (is_in_common == 1)
3401 gfc_warning ("Variable '%s' in common block '%s' at %L "
3402 "may not be a C interoperable "
3403 "kind though common block '%s' is BIND(C)",
3404 tmp_sym->name, com_block->name,
3405 &(tmp_sym->declared_at), com_block->name);
3409 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3410 gfc_error ("Type declaration '%s' at %L is not C "
3411 "interoperable but it is BIND(C)",
3412 tmp_sym->name, &(tmp_sym->declared_at));
3414 gfc_warning ("Variable '%s' at %L "
3415 "may not be a C interoperable "
3416 "kind but it is bind(c)",
3417 tmp_sym->name, &(tmp_sym->declared_at));
3421 /* Variables declared w/in a common block can't be bind(c)
3422 since there's no way for C to see these variables, so there's
3423 semantically no reason for the attribute. */
3424 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3426 gfc_error ("Variable '%s' in common block '%s' at "
3427 "%L cannot be declared with BIND(C) "
3428 "since it is not a global",
3429 tmp_sym->name, com_block->name,
3430 &(tmp_sym->declared_at));
3434 /* Scalar variables that are bind(c) can not have the pointer
3435 or allocatable attributes. */
3436 if (tmp_sym->attr.is_bind_c == 1)
3438 if (tmp_sym->attr.pointer == 1)
3440 gfc_error ("Variable '%s' at %L cannot have both the "
3441 "POINTER and BIND(C) attributes",
3442 tmp_sym->name, &(tmp_sym->declared_at));
3446 if (tmp_sym->attr.allocatable == 1)
3448 gfc_error ("Variable '%s' at %L cannot have both the "
3449 "ALLOCATABLE and BIND(C) attributes",
3450 tmp_sym->name, &(tmp_sym->declared_at));
3454 /* If it is a BIND(C) function, make sure the return value is a
3455 scalar value. The previous tests in this function made sure
3456 the type is interoperable. */
3457 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
3458 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3459 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3461 /* BIND(C) functions can not return a character string. */
3462 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
3463 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3464 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3465 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3466 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3467 "be a character string", tmp_sym->name,
3468 &(tmp_sym->declared_at));
3472 /* See if the symbol has been marked as private. If it has, make sure
3473 there is no binding label and warn the user if there is one. */
3474 if (tmp_sym->attr.access == ACCESS_PRIVATE
3475 && tmp_sym->binding_label[0] != '\0')
3476 /* Use gfc_warning_now because we won't say that the symbol fails
3477 just because of this. */
3478 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3479 "given the binding label '%s'", tmp_sym->name,
3480 &(tmp_sym->declared_at), tmp_sym->binding_label);
3486 /* Set the appropriate fields for a symbol that's been declared as
3487 BIND(C) (the is_bind_c flag and the binding label), and verify that
3488 the type is C interoperable. Errors are reported by the functions
3489 used to set/test these fields. */
3492 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3494 gfc_try retval = SUCCESS;
3496 /* TODO: Do we need to make sure the vars aren't marked private? */
3498 /* Set the is_bind_c bit in symbol_attribute. */
3499 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3501 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3502 num_idents) != SUCCESS)
3509 /* Set the fields marking the given common block as BIND(C), including
3510 a binding label, and report any errors encountered. */
3513 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3515 gfc_try retval = SUCCESS;
3517 /* destLabel, common name, typespec (which may have binding label). */
3518 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3522 /* Set the given common block (com_block) to being bind(c) (1). */
3523 set_com_block_bind_c (com_block, 1);
3529 /* Retrieve the list of one or more identifiers that the given bind(c)
3530 attribute applies to. */
3533 get_bind_c_idents (void)
3535 char name[GFC_MAX_SYMBOL_LEN + 1];
3537 gfc_symbol *tmp_sym = NULL;
3539 gfc_common_head *com_block = NULL;
3541 if (gfc_match_name (name) == MATCH_YES)
3543 found_id = MATCH_YES;
3544 gfc_get_ha_symbol (name, &tmp_sym);
3546 else if (match_common_name (name) == MATCH_YES)
3548 found_id = MATCH_YES;
3549 com_block = gfc_get_common (name, 0);
3553 gfc_error ("Need either entity or common block name for "
3554 "attribute specification statement at %C");
3558 /* Save the current identifier and look for more. */
3561 /* Increment the number of identifiers found for this spec stmt. */
3564 /* Make sure we have a sym or com block, and verify that it can
3565 be bind(c). Set the appropriate field(s) and look for more
3567 if (tmp_sym != NULL || com_block != NULL)
3569 if (tmp_sym != NULL)
3571 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3577 if (set_verify_bind_c_com_block(com_block, num_idents)
3582 /* Look to see if we have another identifier. */
3584 if (gfc_match_eos () == MATCH_YES)
3585 found_id = MATCH_NO;
3586 else if (gfc_match_char (',') != MATCH_YES)
3587 found_id = MATCH_NO;
3588 else if (gfc_match_name (name) == MATCH_YES)
3590 found_id = MATCH_YES;
3591 gfc_get_ha_symbol (name, &tmp_sym);
3593 else if (match_common_name (name) == MATCH_YES)
3595 found_id = MATCH_YES;
3596 com_block = gfc_get_common (name, 0);
3600 gfc_error ("Missing entity or common block name for "
3601 "attribute specification statement at %C");
3607 gfc_internal_error ("Missing symbol");
3609 } while (found_id == MATCH_YES);
3611 /* if we get here we were successful */
3616 /* Try and match a BIND(C) attribute specification statement. */
3619 gfc_match_bind_c_stmt (void)
3621 match found_match = MATCH_NO;
3626 /* This may not be necessary. */
3628 /* Clear the temporary binding label holder. */
3629 curr_binding_label[0] = '\0';
3631 /* Look for the bind(c). */
3632 found_match = gfc_match_bind_c (NULL, true);
3634 if (found_match == MATCH_YES)
3636 /* Look for the :: now, but it is not required. */
3639 /* Get the identifier(s) that needs to be updated. This may need to
3640 change to hand the flag(s) for the attr specified so all identifiers
3641 found can have all appropriate parts updated (assuming that the same
3642 spec stmt can have multiple attrs, such as both bind(c) and
3644 if (get_bind_c_idents () != SUCCESS)
3645 /* Error message should have printed already. */
3653 /* Match a data declaration statement. */
3656 gfc_match_data_decl (void)
3662 num_idents_on_line = 0;
3664 m = gfc_match_type_spec (¤t_ts, 0);
3668 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3670 sym = gfc_use_derived (current_ts.derived);
3678 current_ts.derived = sym;
3681 m = match_attr_spec ();
3682 if (m == MATCH_ERROR)
3688 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL
3689 && !current_ts.derived->attr.zero_comp)
3692 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3695 gfc_find_symbol (current_ts.derived->name,
3696 current_ts.derived->ns->parent, 1, &sym);
3698 /* Any symbol that we find had better be a type definition
3699 which has its components defined. */
3700 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3701 && (current_ts.derived->components != NULL
3702 || current_ts.derived->attr.zero_comp))
3705 /* Now we have an error, which we signal, and then fix up
3706 because the knock-on is plain and simple confusing. */
3707 gfc_error_now ("Derived type at %C has not been previously defined "
3708 "and so cannot appear in a derived type definition");
3709 current_attr.pointer = 1;
3714 /* If we have an old-style character declaration, and no new-style
3715 attribute specifications, then there a comma is optional between
3716 the type specification and the variable list. */
3717 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3718 gfc_match_char (',');
3720 /* Give the types/attributes to symbols that follow. Give the element
3721 a number so that repeat character length expressions can be copied. */
3725 num_idents_on_line++;
3726 m = variable_decl (elem++);
3727 if (m == MATCH_ERROR)
3732 if (gfc_match_eos () == MATCH_YES)
3734 if (gfc_match_char (',') != MATCH_YES)
3738 if (gfc_error_flag_test () == 0)
3739 gfc_error ("Syntax error in data declaration at %C");
3742 gfc_free_data_all (gfc_current_ns);
3745 gfc_free_array_spec (current_as);
3751 /* Match a prefix associated with a function or subroutine
3752 declaration. If the typespec pointer is nonnull, then a typespec
3753 can be matched. Note that if nothing matches, MATCH_YES is
3754 returned (the null string was matched). */
3757 gfc_match_prefix (gfc_typespec *ts)
3761 gfc_clear_attr (¤t_attr);
3764 gcc_assert (!gfc_matching_prefix);
3765 gfc_matching_prefix = true;
3768 if (!seen_type && ts != NULL
3769 && gfc_match_type_spec (ts, 0) == MATCH_YES
3770 && gfc_match_space () == MATCH_YES)
3777 if (gfc_match ("elemental% ") == MATCH_YES)
3779 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
3785 if (gfc_match ("pure% ") == MATCH_YES)
3787 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
3793 if (gfc_match ("recursive% ") == MATCH_YES)
3795 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
3801 /* At this point, the next item is not a prefix. */
3802 gcc_assert (gfc_matching_prefix);
3803 gfc_matching_prefix = false;
3807 gcc_assert (gfc_matching_prefix);
3808 gfc_matching_prefix = false;
3813 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
3816 copy_prefix (symbol_attribute *dest, locus *where)
3818 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3821 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3824 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3831 /* Match a formal argument list. */
3834 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3836 gfc_formal_arglist *head, *tail, *p, *q;
3837 char name[GFC_MAX_SYMBOL_LEN + 1];
3843 if (gfc_match_char ('(') != MATCH_YES)
3850 if (gfc_match_char (')') == MATCH_YES)
3855 if (gfc_match_char ('*') == MATCH_YES)
3859 m = gfc_match_name (name);
3863 if (gfc_get_symbol (name, NULL, &sym))
3867 p = gfc_get_formal_arglist ();
3879 /* We don't add the VARIABLE flavor because the name could be a
3880 dummy procedure. We don't apply these attributes to formal
3881 arguments of statement functions. */
3882 if (sym != NULL && !st_flag
3883 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3884 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3890 /* The name of a program unit can be in a different namespace,
3891 so check for it explicitly. After the statement is accepted,
3892 the name is checked for especially in gfc_get_symbol(). */
3893 if (gfc_new_block != NULL && sym != NULL
3894 && strcmp (sym->name, gfc_new_block->name) == 0)
3896 gfc_error ("Name '%s' at %C is the name of the procedure",
3902 if (gfc_match_char (')') == MATCH_YES)
3905 m = gfc_match_char (',');
3908 gfc_error ("Unexpected junk in formal argument list at %C");
3914 /* Check for duplicate symbols in the formal argument list. */
3917 for (p = head; p->next; p = p->next)
3922 for (q = p->next; q; q = q->next)
3923 if (p->sym == q->sym)
3925 gfc_error ("Duplicate symbol '%s' in formal argument list "
3926 "at %C", p->sym->name);
3934 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3944 gfc_free_formal_arglist (head);
3949 /* Match a RESULT specification following a function declaration or
3950 ENTRY statement. Also matches the end-of-statement. */
3953 match_result (gfc_symbol *function, gfc_symbol **result)
3955 char name[GFC_MAX_SYMBOL_LEN + 1];
3959 if (gfc_match (" result (") != MATCH_YES)
3962 m = gfc_match_name (name);
3966 /* Get the right paren, and that's it because there could be the
3967 bind(c) attribute after the result clause. */
3968 if (gfc_match_char(')') != MATCH_YES)
3970 /* TODO: should report the missing right paren here. */
3974 if (strcmp (function->name, name) == 0)
3976 gfc_error ("RESULT variable at %C must be different than function name");
3980 if (gfc_get_symbol (name, NULL, &r))
3983 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3984 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
3993 /* Match a function suffix, which could be a combination of a result
3994 clause and BIND(C), either one, or neither. The draft does not
3995 require them to come in a specific order. */
3998 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4000 match is_bind_c; /* Found bind(c). */
4001 match is_result; /* Found result clause. */
4002 match found_match; /* Status of whether we've found a good match. */
4003 char peek_char; /* Character we're going to peek at. */
4004 bool allow_binding_name;
4006 /* Initialize to having found nothing. */
4007 found_match = MATCH_NO;
4008 is_bind_c = MATCH_NO;
4009 is_result = MATCH_NO;
4011 /* Get the next char to narrow between result and bind(c). */
4012 gfc_gobble_whitespace ();
4013 peek_char = gfc_peek_ascii_char ();
4015 /* C binding names are not allowed for internal procedures. */
4016 if (gfc_current_state () == COMP_CONTAINS
4017 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4018 allow_binding_name = false;
4020 allow_binding_name = true;
4025 /* Look for result clause. */
4026 is_result = match_result (sym, result);
4027 if (is_result == MATCH_YES)
4029 /* Now see if there is a bind(c) after it. */
4030 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4031 /* We've found the result clause and possibly bind(c). */
4032 found_match = MATCH_YES;
4035 /* This should only be MATCH_ERROR. */
4036 found_match = is_result;
4039 /* Look for bind(c) first. */
4040 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4041 if (is_bind_c == MATCH_YES)
4043 /* Now see if a result clause followed it. */
4044 is_result = match_result (sym, result);
4045 found_match = MATCH_YES;
4049 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4050 found_match = MATCH_ERROR;
4054 gfc_error ("Unexpected junk after function declaration at %C");
4055 found_match = MATCH_ERROR;
4059 if (is_bind_c == MATCH_YES)
4061 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4062 if (gfc_current_state () == COMP_CONTAINS
4063 && sym->ns->proc_name->attr.flavor != FL_MODULE
4064 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4065 "at %L may not be specified for an internal "
4066 "procedure", &gfc_current_locus)
4070 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4079 /* Match a PROCEDURE declaration (R1211). */
4082 match_procedure_decl (void)
4085 locus old_loc, entry_loc;
4086 gfc_symbol *sym, *proc_if = NULL;
4088 gfc_expr *initializer = NULL;
4090 old_loc = entry_loc = gfc_current_locus;
4092 gfc_clear_ts (¤t_ts);
4094 if (gfc_match (" (") != MATCH_YES)
4096 gfc_current_locus = entry_loc;
4100 /* Get the type spec. for the procedure interface. */
4101 old_loc = gfc_current_locus;
4102 m = gfc_match_type_spec (¤t_ts, 0);
4103 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4106 if (m == MATCH_ERROR)
4109 gfc_current_locus = old_loc;
4111 /* Get the name of the procedure or abstract interface
4112 to inherit the interface from. */
4113 m = gfc_match_symbol (&proc_if, 1);
4117 else if (m == MATCH_ERROR)
4120 /* Various interface checks. */
4123 /* Resolve interface if possible. That way, attr.procedure is only set
4124 if it is declared by a later procedure-declaration-stmt, which is
4125 invalid per C1212. */
4126 while (proc_if->ts.interface)
4127 proc_if = proc_if->ts.interface;
4129 if (proc_if->generic)
4131 gfc_error ("Interface '%s' at %C may not be generic", proc_if->name);
4134 if (proc_if->attr.proc == PROC_ST_FUNCTION)
4136 gfc_error ("Interface '%s' at %C may not be a statement function",
4140 /* Handle intrinsic procedures. */
4141 if (!(proc_if->attr.external || proc_if->attr.use_assoc
4142 || proc_if->attr.if_source == IFSRC_IFBODY)
4143 && (gfc_is_intrinsic (proc_if, 0, gfc_current_locus)
4144 || gfc_is_intrinsic (proc_if, 1, gfc_current_locus)))
4145 proc_if->attr.intrinsic = 1;
4146 if (proc_if->attr.intrinsic
4147 && !gfc_intrinsic_actual_ok (proc_if->name, 0))
4149 gfc_error ("Intrinsic procedure '%s' not allowed "
4150 "in PROCEDURE statement at %C", proc_if->name);
4156 if (gfc_match (" )") != MATCH_YES)
4158 gfc_current_locus = entry_loc;
4162 /* Parse attributes. */
4163 m = match_attr_spec();
4164 if (m == MATCH_ERROR)
4167 /* Get procedure symbols. */
4170 m = gfc_match_symbol (&sym, 0);
4173 else if (m == MATCH_ERROR)
4176 /* Add current_attr to the symbol attributes. */
4177 if (gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
4180 if (sym->attr.is_bind_c)
4182 /* Check for C1218. */
4183 if (!proc_if || !proc_if->attr.is_bind_c)
4185 gfc_error ("BIND(C) attribute at %C requires "
4186 "an interface with BIND(C)");
4189 /* Check for C1217. */
4190 if (has_name_equals && sym->attr.pointer)
4192 gfc_error ("BIND(C) procedure with NAME may not have "
4193 "POINTER attribute at %C");
4196 if (has_name_equals && sym->attr.dummy)
4198 gfc_error ("Dummy procedure at %C may not have "
4199 "BIND(C) attribute with NAME");
4202 /* Set binding label for BIND(C). */
4203 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4207 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4209 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4212 /* Set interface. */
4213 if (proc_if != NULL)
4215 sym->ts.interface = proc_if;
4216 sym->attr.untyped = 1;
4218 else if (current_ts.type != BT_UNKNOWN)
4220 sym->ts = current_ts;
4221 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4222 sym->ts.interface->ts = current_ts;
4223 sym->ts.interface->attr.function = 1;
4224 sym->attr.function = sym->ts.interface->attr.function;
4227 if (gfc_match (" =>") == MATCH_YES)
4229 if (!current_attr.pointer)
4231 gfc_error ("Initialization at %C isn't for a pointer variable");
4236 m = gfc_match_null (&initializer);
4239 gfc_error ("Pointer initialization requires a NULL() at %C");
4243 if (gfc_pure (NULL))
4245 gfc_error ("Initialization of pointer at %C is not allowed in "
4246 "a PURE procedure");
4253 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4259 gfc_set_sym_referenced (sym);
4261 if (gfc_match_eos () == MATCH_YES)
4263 if (gfc_match_char (',') != MATCH_YES)
4268 gfc_error ("Syntax error in PROCEDURE statement at %C");
4272 /* Free stuff up and return. */
4273 gfc_free_expr (initializer);
4278 /* Match a PROCEDURE declaration inside an interface (R1206). */
4281 match_procedure_in_interface (void)
4285 char name[GFC_MAX_SYMBOL_LEN + 1];
4287 if (current_interface.type == INTERFACE_NAMELESS
4288 || current_interface.type == INTERFACE_ABSTRACT)
4290 gfc_error ("PROCEDURE at %C must be in a generic interface");
4296 m = gfc_match_name (name);
4299 else if (m == MATCH_ERROR)
4301 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4304 if (gfc_add_interface (sym) == FAILURE)
4307 if (gfc_match_eos () == MATCH_YES)
4309 if (gfc_match_char (',') != MATCH_YES)
4316 gfc_error ("Syntax error in PROCEDURE statement at %C");
4321 /* General matcher for PROCEDURE declarations. */
4324 gfc_match_procedure (void)
4328 switch (gfc_current_state ())
4333 case COMP_SUBROUTINE:
4335 m = match_procedure_decl ();
4337 case COMP_INTERFACE:
4338 m = match_procedure_in_interface ();
4341 gfc_error ("Fortran 2003: Procedure components at %C are "
4342 "not yet implemented in gfortran");
4351 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4359 /* Warn if a matched procedure has the same name as an intrinsic; this is
4360 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4361 parser-state-stack to find out whether we're in a module. */
4364 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
4368 in_module = (gfc_state_stack->previous
4369 && gfc_state_stack->previous->state == COMP_MODULE);
4371 gfc_warn_intrinsic_shadow (sym, in_module, func);
4375 /* Match a function declaration. */
4378 gfc_match_function_decl (void)
4380 char name[GFC_MAX_SYMBOL_LEN + 1];
4381 gfc_symbol *sym, *result;
4385 match found_match; /* Status returned by match func. */
4387 if (gfc_current_state () != COMP_NONE
4388 && gfc_current_state () != COMP_INTERFACE
4389 && gfc_current_state () != COMP_CONTAINS)
4392 gfc_clear_ts (¤t_ts);
4394 old_loc = gfc_current_locus;
4396 m = gfc_match_prefix (¤t_ts);
4399 gfc_current_locus = old_loc;
4403 if (gfc_match ("function% %n", name) != MATCH_YES)
4405 gfc_current_locus = old_loc;
4408 if (get_proc_name (name, &sym, false))
4410 gfc_new_block = sym;
4412 m = gfc_match_formal_arglist (sym, 0, 0);
4415 gfc_error ("Expected formal argument list in function "
4416 "definition at %C");
4420 else if (m == MATCH_ERROR)
4425 /* According to the draft, the bind(c) and result clause can
4426 come in either order after the formal_arg_list (i.e., either
4427 can be first, both can exist together or by themselves or neither
4428 one). Therefore, the match_result can't match the end of the
4429 string, and check for the bind(c) or result clause in either order. */
4430 found_match = gfc_match_eos ();
4432 /* Make sure that it isn't already declared as BIND(C). If it is, it
4433 must have been marked BIND(C) with a BIND(C) attribute and that is
4434 not allowed for procedures. */
4435 if (sym->attr.is_bind_c == 1)
4437 sym->attr.is_bind_c = 0;
4438 if (sym->old_symbol != NULL)
4439 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4440 "variables or common blocks",
4441 &(sym->old_symbol->declared_at));
4443 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4444 "variables or common blocks", &gfc_current_locus);
4447 if (found_match != MATCH_YES)
4449 /* If we haven't found the end-of-statement, look for a suffix. */
4450 suffix_match = gfc_match_suffix (sym, &result);
4451 if (suffix_match == MATCH_YES)
4452 /* Need to get the eos now. */
4453 found_match = gfc_match_eos ();
4455 found_match = suffix_match;
4458 if(found_match != MATCH_YES)
4462 /* Make changes to the symbol. */
4465 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4468 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4469 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4472 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
4473 && !sym->attr.implicit_type)
4475 gfc_error ("Function '%s' at %C already has a type of %s", name,
4476 gfc_basic_typename (sym->ts.type));
4480 /* Delay matching the function characteristics until after the
4481 specification block by signalling kind=-1. */
4482 sym->declared_at = old_loc;
4483 if (current_ts.type != BT_UNKNOWN)
4484 current_ts.kind = -1;
4486 current_ts.kind = 0;
4490 sym->ts = current_ts;
4495 result->ts = current_ts;
4496 sym->result = result;
4499 /* Warn if this procedure has the same name as an intrinsic. */
4500 warn_intrinsic_shadow (sym, true);
4506 gfc_current_locus = old_loc;
4511 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4512 pass the name of the entry, rather than the gfc_current_block name, and
4513 to return false upon finding an existing global entry. */
4516 add_global_entry (const char *name, int sub)
4521 s = gfc_get_gsymbol(name);
4522 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4525 || (s->type != GSYM_UNKNOWN
4526 && s->type != type))
4527 gfc_global_used(s, NULL);
4531 s->where = gfc_current_locus;
4539 /* Match an ENTRY statement. */
4542 gfc_match_entry (void)
4547 char name[GFC_MAX_SYMBOL_LEN + 1];
4548 gfc_compile_state state;
4552 bool module_procedure;
4556 m = gfc_match_name (name);
4560 state = gfc_current_state ();
4561 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
4566 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4569 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4571 case COMP_BLOCK_DATA:
4572 gfc_error ("ENTRY statement at %C cannot appear within "
4575 case COMP_INTERFACE:
4576 gfc_error ("ENTRY statement at %C cannot appear within "
4580 gfc_error ("ENTRY statement at %C cannot appear within "
4581 "a DERIVED TYPE block");
4584 gfc_error ("ENTRY statement at %C cannot appear within "
4585 "an IF-THEN block");
4588 gfc_error ("ENTRY statement at %C cannot appear within "
4592 gfc_error ("ENTRY statement at %C cannot appear within "
4596 gfc_error ("ENTRY statement at %C cannot appear within "
4600 gfc_error ("ENTRY statement at %C cannot appear within "
4604 gfc_error ("ENTRY statement at %C cannot appear within "
4605 "a contained subprogram");
4608 gfc_internal_error ("gfc_match_entry(): Bad state");
4613 module_procedure = gfc_current_ns->parent != NULL
4614 && gfc_current_ns->parent->proc_name
4615 && gfc_current_ns->parent->proc_name->attr.flavor
4618 if (gfc_current_ns->parent != NULL
4619 && gfc_current_ns->parent->proc_name
4620 && !module_procedure)
4622 gfc_error("ENTRY statement at %C cannot appear in a "
4623 "contained procedure");
4627 /* Module function entries need special care in get_proc_name
4628 because previous references within the function will have
4629 created symbols attached to the current namespace. */
4630 if (get_proc_name (name, &entry,
4631 gfc_current_ns->parent != NULL
4633 && gfc_current_ns->proc_name->attr.function))
4636 proc = gfc_current_block ();
4638 /* Make sure that it isn't already declared as BIND(C). If it is, it
4639 must have been marked BIND(C) with a BIND(C) attribute and that is
4640 not allowed for procedures. */
4641 if (entry->attr.is_bind_c == 1)
4643 entry->attr.is_bind_c = 0;
4644 if (entry->old_symbol != NULL)
4645 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4646 "variables or common blocks",
4647 &(entry->old_symbol->declared_at));
4649 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4650 "variables or common blocks", &gfc_current_locus);
4653 /* Check what next non-whitespace character is so we can tell if there
4654 is the required parens if we have a BIND(C). */
4655 gfc_gobble_whitespace ();
4656 peek_char = gfc_peek_ascii_char ();
4658 if (state == COMP_SUBROUTINE)
4660 /* An entry in a subroutine. */
4661 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
4664 m = gfc_match_formal_arglist (entry, 0, 1);
4668 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
4669 never be an internal procedure. */
4670 is_bind_c = gfc_match_bind_c (entry, true);
4671 if (is_bind_c == MATCH_ERROR)
4673 if (is_bind_c == MATCH_YES)
4675 if (peek_char != '(')
4677 gfc_error ("Missing required parentheses before BIND(C) at %C");
4680 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
4685 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4686 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4691 /* An entry in a function.
4692 We need to take special care because writing
4697 ENTRY f() RESULT (r)
4699 ENTRY f RESULT (r). */
4700 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
4703 old_loc = gfc_current_locus;
4704 if (gfc_match_eos () == MATCH_YES)
4706 gfc_current_locus = old_loc;
4707 /* Match the empty argument list, and add the interface to
4709 m = gfc_match_formal_arglist (entry, 0, 1);
4712 m = gfc_match_formal_arglist (entry, 0, 0);
4719 if (gfc_match_eos () == MATCH_YES)
4721 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4722 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4725 entry->result = entry;
4729 m = gfc_match_suffix (entry, &result);
4731 gfc_syntax_error (ST_ENTRY);
4737 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4738 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
4739 || gfc_add_function (&entry->attr, result->name, NULL)
4742 entry->result = result;
4746 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4747 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4749 entry->result = entry;
4754 if (gfc_match_eos () != MATCH_YES)
4756 gfc_syntax_error (ST_ENTRY);
4760 entry->attr.recursive = proc->attr.recursive;
4761 entry->attr.elemental = proc->attr.elemental;
4762 entry->attr.pure = proc->attr.pure;
4764 el = gfc_get_entry_list ();
4766 el->next = gfc_current_ns->entries;
4767 gfc_current_ns->entries = el;
4769 el->id = el->next->id + 1;
4773 new_st.op = EXEC_ENTRY;
4774 new_st.ext.entry = el;
4780 /* Match a subroutine statement, including optional prefixes. */
4783 gfc_match_subroutine (void)
4785 char name[GFC_MAX_SYMBOL_LEN + 1];
4790 bool allow_binding_name;
4792 if (gfc_current_state () != COMP_NONE
4793 && gfc_current_state () != COMP_INTERFACE
4794 && gfc_current_state () != COMP_CONTAINS)
4797 m = gfc_match_prefix (NULL);
4801 m = gfc_match ("subroutine% %n", name);
4805 if (get_proc_name (name, &sym, false))
4807 gfc_new_block = sym;
4809 /* Check what next non-whitespace character is so we can tell if there
4810 is the required parens if we have a BIND(C). */
4811 gfc_gobble_whitespace ();
4812 peek_char = gfc_peek_ascii_char ();
4814 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
4817 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4820 /* Make sure that it isn't already declared as BIND(C). If it is, it
4821 must have been marked BIND(C) with a BIND(C) attribute and that is
4822 not allowed for procedures. */
4823 if (sym->attr.is_bind_c == 1)
4825 sym->attr.is_bind_c = 0;
4826 if (sym->old_symbol != NULL)
4827 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4828 "variables or common blocks",
4829 &(sym->old_symbol->declared_at));
4831 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4832 "variables or common blocks", &gfc_current_locus);
4835 /* C binding names are not allowed for internal procedures. */
4836 if (gfc_current_state () == COMP_CONTAINS
4837 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4838 allow_binding_name = false;
4840 allow_binding_name = true;
4842 /* Here, we are just checking if it has the bind(c) attribute, and if
4843 so, then we need to make sure it's all correct. If it doesn't,
4844 we still need to continue matching the rest of the subroutine line. */
4845 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4846 if (is_bind_c == MATCH_ERROR)
4848 /* There was an attempt at the bind(c), but it was wrong. An
4849 error message should have been printed w/in the gfc_match_bind_c
4850 so here we'll just return the MATCH_ERROR. */
4854 if (is_bind_c == MATCH_YES)
4856 /* The following is allowed in the Fortran 2008 draft. */
4857 if (gfc_current_state () == COMP_CONTAINS
4858 && sym->ns->proc_name->attr.flavor != FL_MODULE
4859 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4860 "at %L may not be specified for an internal "
4861 "procedure", &gfc_current_locus)
4865 if (peek_char != '(')
4867 gfc_error ("Missing required parentheses before BIND(C) at %C");
4870 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4875 if (gfc_match_eos () != MATCH_YES)
4877 gfc_syntax_error (ST_SUBROUTINE);
4881 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4884 /* Warn if it has the same name as an intrinsic. */
4885 warn_intrinsic_shadow (sym, false);
4891 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4892 given, and set the binding label in either the given symbol (if not
4893 NULL), or in the current_ts. The symbol may be NULL because we may
4894 encounter the BIND(C) before the declaration itself. Return
4895 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4896 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4897 or MATCH_YES if the specifier was correct and the binding label and
4898 bind(c) fields were set correctly for the given symbol or the
4899 current_ts. If allow_binding_name is false, no binding name may be
4903 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
4905 /* binding label, if exists */
4906 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4910 /* Initialize the flag that specifies whether we encountered a NAME=
4911 specifier or not. */
4912 has_name_equals = 0;
4914 /* Init the first char to nil so we can catch if we don't have
4915 the label (name attr) or the symbol name yet. */
4916 binding_label[0] = '\0';
4918 /* This much we have to be able to match, in this order, if
4919 there is a bind(c) label. */
4920 if (gfc_match (" bind ( c ") != MATCH_YES)
4923 /* Now see if there is a binding label, or if we've reached the
4924 end of the bind(c) attribute without one. */
4925 if (gfc_match_char (',') == MATCH_YES)
4927 if (gfc_match (" name = ") != MATCH_YES)
4929 gfc_error ("Syntax error in NAME= specifier for binding label "
4931 /* should give an error message here */
4935 has_name_equals = 1;
4937 /* Get the opening quote. */
4938 double_quote = MATCH_YES;
4939 single_quote = MATCH_YES;
4940 double_quote = gfc_match_char ('"');
4941 if (double_quote != MATCH_YES)
4942 single_quote = gfc_match_char ('\'');
4943 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4945 gfc_error ("Syntax error in NAME= specifier for binding label "
4950 /* Grab the binding label, using functions that will not lower
4951 case the names automatically. */
4952 if (gfc_match_name_C (binding_label) != MATCH_YES)
4955 /* Get the closing quotation. */
4956 if (double_quote == MATCH_YES)
4958 if (gfc_match_char ('"') != MATCH_YES)
4960 gfc_error ("Missing closing quote '\"' for binding label at %C");
4961 /* User started string with '"' so looked to match it. */
4967 if (gfc_match_char ('\'') != MATCH_YES)
4969 gfc_error ("Missing closing quote '\'' for binding label at %C");
4970 /* User started string with "'" char. */
4976 /* Get the required right paren. */
4977 if (gfc_match_char (')') != MATCH_YES)
4979 gfc_error ("Missing closing paren for binding label at %C");
4983 if (has_name_equals && !allow_binding_name)
4985 gfc_error ("No binding name is allowed in BIND(C) at %C");
4989 if (has_name_equals && sym != NULL && sym->attr.dummy)
4991 gfc_error ("For dummy procedure %s, no binding name is "
4992 "allowed in BIND(C) at %C", sym->name);
4997 /* Save the binding label to the symbol. If sym is null, we're
4998 probably matching the typespec attributes of a declaration and
4999 haven't gotten the name yet, and therefore, no symbol yet. */
5000 if (binding_label[0] != '\0')
5004 strcpy (sym->binding_label, binding_label);
5007 strcpy (curr_binding_label, binding_label);
5009 else if (allow_binding_name)
5011 /* No binding label, but if symbol isn't null, we
5012 can set the label for it here.
5013 If name="" or allow_binding_name is false, no C binding name is
5015 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5016 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
5019 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5020 && current_interface.type == INTERFACE_ABSTRACT)
5022 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5030 /* Return nonzero if we're currently compiling a contained procedure. */
5033 contained_procedure (void)
5035 gfc_state_data *s = gfc_state_stack;
5037 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5038 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5044 /* Set the kind of each enumerator. The kind is selected such that it is
5045 interoperable with the corresponding C enumeration type, making
5046 sure that -fshort-enums is honored. */
5051 enumerator_history *current_history = NULL;
5055 if (max_enum == NULL || enum_history == NULL)
5058 if (!gfc_option.fshort_enums)
5064 kind = gfc_integer_kinds[i++].kind;
5066 while (kind < gfc_c_int_kind
5067 && gfc_check_integer_range (max_enum->initializer->value.integer,
5070 current_history = enum_history;
5071 while (current_history != NULL)
5073 current_history->sym->ts.kind = kind;
5074 current_history = current_history->next;
5079 /* Match any of the various end-block statements. Returns the type of
5080 END to the caller. The END INTERFACE, END IF, END DO and END
5081 SELECT statements cannot be replaced by a single END statement. */
5084 gfc_match_end (gfc_statement *st)
5086 char name[GFC_MAX_SYMBOL_LEN + 1];
5087 gfc_compile_state state;
5089 const char *block_name;
5094 old_loc = gfc_current_locus;
5095 if (gfc_match ("end") != MATCH_YES)
5098 state = gfc_current_state ();
5099 block_name = gfc_current_block () == NULL
5100 ? NULL : gfc_current_block ()->name;
5102 if (state == COMP_CONTAINS)
5104 state = gfc_state_stack->previous->state;
5105 block_name = gfc_state_stack->previous->sym == NULL
5106 ? NULL : gfc_state_stack->previous->sym->name;
5113 *st = ST_END_PROGRAM;
5114 target = " program";
5118 case COMP_SUBROUTINE:
5119 *st = ST_END_SUBROUTINE;
5120 target = " subroutine";
5121 eos_ok = !contained_procedure ();
5125 *st = ST_END_FUNCTION;
5126 target = " function";
5127 eos_ok = !contained_procedure ();
5130 case COMP_BLOCK_DATA:
5131 *st = ST_END_BLOCK_DATA;
5132 target = " block data";
5137 *st = ST_END_MODULE;
5142 case COMP_INTERFACE:
5143 *st = ST_END_INTERFACE;
5144 target = " interface";
5167 *st = ST_END_SELECT;
5173 *st = ST_END_FORALL;
5188 last_initializer = NULL;
5190 gfc_free_enum_history ();
5194 gfc_error ("Unexpected END statement at %C");
5198 if (gfc_match_eos () == MATCH_YES)
5202 /* We would have required END [something]. */
5203 gfc_error ("%s statement expected at %L",
5204 gfc_ascii_statement (*st), &old_loc);
5211 /* Verify that we've got the sort of end-block that we're expecting. */
5212 if (gfc_match (target) != MATCH_YES)
5214 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5218 /* If we're at the end, make sure a block name wasn't required. */
5219 if (gfc_match_eos () == MATCH_YES)
5222 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5223 && *st != ST_END_FORALL && *st != ST_END_WHERE)
5226 if (gfc_current_block () == NULL)
5229 gfc_error ("Expected block name of '%s' in %s statement at %C",
5230 block_name, gfc_ascii_statement (*st));
5235 /* END INTERFACE has a special handler for its several possible endings. */
5236 if (*st == ST_END_INTERFACE)
5237 return gfc_match_end_interface ();
5239 /* We haven't hit the end of statement, so what is left must be an
5241 m = gfc_match_space ();
5243 m = gfc_match_name (name);
5246 gfc_error ("Expected terminating name at %C");
5250 if (block_name == NULL)
5253 if (strcmp (name, block_name) != 0)
5255 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5256 gfc_ascii_statement (*st));
5260 if (gfc_match_eos () == MATCH_YES)
5264 gfc_syntax_error (*st);
5267 gfc_current_locus = old_loc;
5273 /***************** Attribute declaration statements ****************/
5275 /* Set the attribute of a single variable. */
5280 char name[GFC_MAX_SYMBOL_LEN + 1];
5288 m = gfc_match_name (name);
5292 if (find_special (name, &sym))
5295 var_locus = gfc_current_locus;
5297 /* Deal with possible array specification for certain attributes. */
5298 if (current_attr.dimension
5299 || current_attr.allocatable
5300 || current_attr.pointer
5301 || current_attr.target)
5303 m = gfc_match_array_spec (&as);
5304 if (m == MATCH_ERROR)
5307 if (current_attr.dimension && m == MATCH_NO)
5309 gfc_error ("Missing array specification at %L in DIMENSION "
5310 "statement", &var_locus);
5315 if (current_attr.dimension && sym->value)
5317 gfc_error ("Dimensions specified for %s at %L after its "
5318 "initialisation", sym->name, &var_locus);
5323 if ((current_attr.allocatable || current_attr.pointer)
5324 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5326 gfc_error ("Array specification must be deferred at %L", &var_locus);
5332 /* Update symbol table. DIMENSION attribute is set
5333 in gfc_set_array_spec(). */
5334 if (current_attr.dimension == 0
5335 && gfc_copy_attr (&sym->attr, ¤t_attr, &var_locus) == FAILURE)
5341 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5347 if (sym->attr.cray_pointee && sym->as != NULL)
5349 /* Fix the array spec. */
5350 m = gfc_mod_pointee_as (sym->as);
5351 if (m == MATCH_ERROR)
5355 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
5361 if ((current_attr.external || current_attr.intrinsic)
5362 && sym->attr.flavor != FL_PROCEDURE
5363 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
5372 gfc_free_array_spec (as);
5377 /* Generic attribute declaration subroutine. Used for attributes that
5378 just have a list of names. */
5385 /* Gobble the optional double colon, by simply ignoring the result
5395 if (gfc_match_eos () == MATCH_YES)
5401 if (gfc_match_char (',') != MATCH_YES)
5403 gfc_error ("Unexpected character in variable list at %C");
5413 /* This routine matches Cray Pointer declarations of the form:
5414 pointer ( <pointer>, <pointee> )
5416 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5417 The pointer, if already declared, should be an integer. Otherwise, we
5418 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5419 be either a scalar, or an array declaration. No space is allocated for
5420 the pointee. For the statement
5421 pointer (ipt, ar(10))
5422 any subsequent uses of ar will be translated (in C-notation) as
5423 ar(i) => ((<type> *) ipt)(i)
5424 After gimplification, pointee variable will disappear in the code. */
5427 cray_pointer_decl (void)
5431 gfc_symbol *cptr; /* Pointer symbol. */
5432 gfc_symbol *cpte; /* Pointee symbol. */
5438 if (gfc_match_char ('(') != MATCH_YES)
5440 gfc_error ("Expected '(' at %C");
5444 /* Match pointer. */
5445 var_locus = gfc_current_locus;
5446 gfc_clear_attr (¤t_attr);
5447 gfc_add_cray_pointer (¤t_attr, &var_locus);
5448 current_ts.type = BT_INTEGER;
5449 current_ts.kind = gfc_index_integer_kind;
5451 m = gfc_match_symbol (&cptr, 0);
5454 gfc_error ("Expected variable name at %C");
5458 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5461 gfc_set_sym_referenced (cptr);
5463 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5465 cptr->ts.type = BT_INTEGER;
5466 cptr->ts.kind = gfc_index_integer_kind;
5468 else if (cptr->ts.type != BT_INTEGER)
5470 gfc_error ("Cray pointer at %C must be an integer");
5473 else if (cptr->ts.kind < gfc_index_integer_kind)
5474 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5475 " memory addresses require %d bytes",
5476 cptr->ts.kind, gfc_index_integer_kind);
5478 if (gfc_match_char (',') != MATCH_YES)
5480 gfc_error ("Expected \",\" at %C");
5484 /* Match Pointee. */
5485 var_locus = gfc_current_locus;
5486 gfc_clear_attr (¤t_attr);
5487 gfc_add_cray_pointee (¤t_attr, &var_locus);
5488 current_ts.type = BT_UNKNOWN;
5489 current_ts.kind = 0;
5491 m = gfc_match_symbol (&cpte, 0);
5494 gfc_error ("Expected variable name at %C");
5498 /* Check for an optional array spec. */
5499 m = gfc_match_array_spec (&as);
5500 if (m == MATCH_ERROR)
5502 gfc_free_array_spec (as);
5505 else if (m == MATCH_NO)
5507 gfc_free_array_spec (as);
5511 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5514 gfc_set_sym_referenced (cpte);
5516 if (cpte->as == NULL)
5518 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5519 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5521 else if (as != NULL)
5523 gfc_error ("Duplicate array spec for Cray pointee at %C");
5524 gfc_free_array_spec (as);
5530 if (cpte->as != NULL)
5532 /* Fix array spec. */
5533 m = gfc_mod_pointee_as (cpte->as);
5534 if (m == MATCH_ERROR)
5538 /* Point the Pointee at the Pointer. */
5539 cpte->cp_pointer = cptr;
5541 if (gfc_match_char (')') != MATCH_YES)
5543 gfc_error ("Expected \")\" at %C");
5546 m = gfc_match_char (',');
5548 done = true; /* Stop searching for more declarations. */
5552 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5553 || gfc_match_eos () != MATCH_YES)
5555 gfc_error ("Expected \",\" or end of statement at %C");
5563 gfc_match_external (void)
5566 gfc_clear_attr (¤t_attr);
5567 current_attr.external = 1;
5569 return attr_decl ();
5574 gfc_match_intent (void)
5578 intent = match_intent_spec ();
5579 if (intent == INTENT_UNKNOWN)
5582 gfc_clear_attr (¤t_attr);
5583 current_attr.intent = intent;
5585 return attr_decl ();
5590 gfc_match_intrinsic (void)
5593 gfc_clear_attr (¤t_attr);
5594 current_attr.intrinsic = 1;
5596 return attr_decl ();
5601 gfc_match_optional (void)
5604 gfc_clear_attr (¤t_attr);
5605 current_attr.optional = 1;
5607 return attr_decl ();
5612 gfc_match_pointer (void)
5614 gfc_gobble_whitespace ();
5615 if (gfc_peek_ascii_char () == '(')
5617 if (!gfc_option.flag_cray_pointer)
5619 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5623 return cray_pointer_decl ();
5627 gfc_clear_attr (¤t_attr);
5628 current_attr.pointer = 1;
5630 return attr_decl ();
5636 gfc_match_allocatable (void)
5638 gfc_clear_attr (¤t_attr);
5639 current_attr.allocatable = 1;
5641 return attr_decl ();
5646 gfc_match_dimension (void)
5648 gfc_clear_attr (¤t_attr);
5649 current_attr.dimension = 1;
5651 return attr_decl ();
5656 gfc_match_target (void)
5658 gfc_clear_attr (¤t_attr);
5659 current_attr.target = 1;
5661 return attr_decl ();
5665 /* Match the list of entities being specified in a PUBLIC or PRIVATE
5669 access_attr_decl (gfc_statement st)
5671 char name[GFC_MAX_SYMBOL_LEN + 1];
5672 interface_type type;
5675 gfc_intrinsic_op op;
5678 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5683 m = gfc_match_generic_spec (&type, name, &op);
5686 if (m == MATCH_ERROR)
5691 case INTERFACE_NAMELESS:
5692 case INTERFACE_ABSTRACT:
5695 case INTERFACE_GENERIC:
5696 if (gfc_get_symbol (name, NULL, &sym))
5699 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5700 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
5701 sym->name, NULL) == FAILURE)
5706 case INTERFACE_INTRINSIC_OP:
5707 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
5709 gfc_current_ns->operator_access[op] =
5710 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5714 gfc_error ("Access specification of the %s operator at %C has "
5715 "already been specified", gfc_op2string (op));
5721 case INTERFACE_USER_OP:
5722 uop = gfc_get_uop (name);
5724 if (uop->access == ACCESS_UNKNOWN)
5726 uop->access = (st == ST_PUBLIC)
5727 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5731 gfc_error ("Access specification of the .%s. operator at %C "
5732 "has already been specified", sym->name);
5739 if (gfc_match_char (',') == MATCH_NO)
5743 if (gfc_match_eos () != MATCH_YES)
5748 gfc_syntax_error (st);
5756 gfc_match_protected (void)
5761 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5763 gfc_error ("PROTECTED at %C only allowed in specification "
5764 "part of a module");
5769 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
5773 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5778 if (gfc_match_eos () == MATCH_YES)
5783 m = gfc_match_symbol (&sym, 0);
5787 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
5800 if (gfc_match_eos () == MATCH_YES)
5802 if (gfc_match_char (',') != MATCH_YES)
5809 gfc_error ("Syntax error in PROTECTED statement at %C");
5814 /* The PRIVATE statement is a bit weird in that it can be an attribute
5815 declaration, but also works as a standalone statement inside of a
5816 type declaration or a module. */
5819 gfc_match_private (gfc_statement *st)
5822 if (gfc_match ("private") != MATCH_YES)
5825 if (gfc_current_state () != COMP_MODULE
5826 && (gfc_current_state () != COMP_DERIVED
5827 || !gfc_state_stack->previous
5828 || gfc_state_stack->previous->state != COMP_MODULE))
5830 gfc_error ("PRIVATE statement at %C is only allowed in the "
5831 "specification part of a module");
5835 if (gfc_current_state () == COMP_DERIVED)
5837 if (gfc_match_eos () == MATCH_YES)
5843 gfc_syntax_error (ST_PRIVATE);
5847 if (gfc_match_eos () == MATCH_YES)
5854 return access_attr_decl (ST_PRIVATE);
5859 gfc_match_public (gfc_statement *st)
5862 if (gfc_match ("public") != MATCH_YES)
5865 if (gfc_current_state () != COMP_MODULE)
5867 gfc_error ("PUBLIC statement at %C is only allowed in the "
5868 "specification part of a module");
5872 if (gfc_match_eos () == MATCH_YES)
5879 return access_attr_decl (ST_PUBLIC);
5883 /* Workhorse for gfc_match_parameter. */
5892 m = gfc_match_symbol (&sym, 0);
5894 gfc_error ("Expected variable name at %C in PARAMETER statement");
5899 if (gfc_match_char ('=') == MATCH_NO)
5901 gfc_error ("Expected = sign in PARAMETER statement at %C");
5905 m = gfc_match_init_expr (&init);
5907 gfc_error ("Expected expression at %C in PARAMETER statement");
5911 if (sym->ts.type == BT_UNKNOWN
5912 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5918 if (gfc_check_assign_symbol (sym, init) == FAILURE
5919 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
5927 gfc_error ("Initializing already initialized variable at %C");
5932 if (sym->ts.type == BT_CHARACTER
5933 && sym->ts.cl != NULL
5934 && sym->ts.cl->length != NULL
5935 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5936 && init->expr_type == EXPR_CONSTANT
5937 && init->ts.type == BT_CHARACTER)
5938 gfc_set_constant_character_len (
5939 mpz_get_si (sym->ts.cl->length->value.integer), init, -1);
5940 else if (sym->ts.type == BT_CHARACTER && sym->ts.cl != NULL
5941 && sym->ts.cl->length == NULL)
5944 if (init->expr_type == EXPR_CONSTANT)
5946 clen = init->value.character.length;
5947 sym->ts.cl->length = gfc_int_expr (clen);
5949 else if (init->expr_type == EXPR_ARRAY)
5951 gfc_expr *p = init->value.constructor->expr;
5952 clen = p->value.character.length;
5953 sym->ts.cl->length = gfc_int_expr (clen);
5955 else if (init->ts.cl && init->ts.cl->length)
5956 sym->ts.cl->length = gfc_copy_expr (sym->value->ts.cl->length);
5963 gfc_free_expr (init);
5968 /* Match a parameter statement, with the weird syntax that these have. */
5971 gfc_match_parameter (void)
5975 if (gfc_match_char ('(') == MATCH_NO)
5984 if (gfc_match (" )%t") == MATCH_YES)
5987 if (gfc_match_char (',') != MATCH_YES)
5989 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5999 /* Save statements have a special syntax. */
6002 gfc_match_save (void)
6004 char n[GFC_MAX_SYMBOL_LEN+1];
6009 if (gfc_match_eos () == MATCH_YES)
6011 if (gfc_current_ns->seen_save)
6013 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6014 "follows previous SAVE statement")
6019 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6023 if (gfc_current_ns->save_all)
6025 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6026 "blanket SAVE statement")
6035 m = gfc_match_symbol (&sym, 0);
6039 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
6051 m = gfc_match (" / %n /", &n);
6052 if (m == MATCH_ERROR)
6057 c = gfc_get_common (n, 0);
6060 gfc_current_ns->seen_save = 1;
6063 if (gfc_match_eos () == MATCH_YES)
6065 if (gfc_match_char (',') != MATCH_YES)
6072 gfc_error ("Syntax error in SAVE statement at %C");
6078 gfc_match_value (void)
6083 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
6087 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6092 if (gfc_match_eos () == MATCH_YES)
6097 m = gfc_match_symbol (&sym, 0);
6101 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
6114 if (gfc_match_eos () == MATCH_YES)
6116 if (gfc_match_char (',') != MATCH_YES)
6123 gfc_error ("Syntax error in VALUE statement at %C");
6129 gfc_match_volatile (void)
6134 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
6138 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6143 if (gfc_match_eos () == MATCH_YES)
6148 /* VOLATILE is special because it can be added to host-associated
6150 m = gfc_match_symbol (&sym, 1);
6154 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6167 if (gfc_match_eos () == MATCH_YES)
6169 if (gfc_match_char (',') != MATCH_YES)
6176 gfc_error ("Syntax error in VOLATILE statement at %C");
6181 /* Match a module procedure statement. Note that we have to modify
6182 symbols in the parent's namespace because the current one was there
6183 to receive symbols that are in an interface's formal argument list. */
6186 gfc_match_modproc (void)
6188 char name[GFC_MAX_SYMBOL_LEN + 1];
6191 gfc_namespace *module_ns;
6192 gfc_interface *old_interface_head, *interface;
6194 if (gfc_state_stack->state != COMP_INTERFACE
6195 || gfc_state_stack->previous == NULL
6196 || current_interface.type == INTERFACE_NAMELESS
6197 || current_interface.type == INTERFACE_ABSTRACT)
6199 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6204 module_ns = gfc_current_ns->parent;
6205 for (; module_ns; module_ns = module_ns->parent)
6206 if (module_ns->proc_name->attr.flavor == FL_MODULE)
6209 if (module_ns == NULL)
6212 /* Store the current state of the interface. We will need it if we
6213 end up with a syntax error and need to recover. */
6214 old_interface_head = gfc_current_interface_head ();
6220 m = gfc_match_name (name);
6226 /* Check for syntax error before starting to add symbols to the
6227 current namespace. */
6228 if (gfc_match_eos () == MATCH_YES)
6230 if (!last && gfc_match_char (',') != MATCH_YES)
6233 /* Now we're sure the syntax is valid, we process this item
6235 if (gfc_get_symbol (name, module_ns, &sym))
6238 if (sym->attr.proc != PROC_MODULE
6239 && gfc_add_procedure (&sym->attr, PROC_MODULE,
6240 sym->name, NULL) == FAILURE)
6243 if (gfc_add_interface (sym) == FAILURE)
6246 sym->attr.mod_proc = 1;
6255 /* Restore the previous state of the interface. */
6256 interface = gfc_current_interface_head ();
6257 gfc_set_current_interface_head (old_interface_head);
6259 /* Free the new interfaces. */
6260 while (interface != old_interface_head)
6262 gfc_interface *i = interface->next;
6263 gfc_free (interface);
6267 /* And issue a syntax error. */
6268 gfc_syntax_error (ST_MODULE_PROC);
6273 /* Check a derived type that is being extended. */
6275 check_extended_derived_type (char *name)
6277 gfc_symbol *extended;
6279 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
6281 gfc_error ("Ambiguous symbol in TYPE definition at %C");
6287 gfc_error ("No such symbol in TYPE definition at %C");
6291 if (extended->attr.flavor != FL_DERIVED)
6293 gfc_error ("'%s' in EXTENDS expression at %C is not a "
6294 "derived type", name);
6298 if (extended->attr.is_bind_c)
6300 gfc_error ("'%s' cannot be extended at %C because it "
6301 "is BIND(C)", extended->name);
6305 if (extended->attr.sequence)
6307 gfc_error ("'%s' cannot be extended at %C because it "
6308 "is a SEQUENCE type", extended->name);
6316 /* Match the optional attribute specifiers for a type declaration.
6317 Return MATCH_ERROR if an error is encountered in one of the handled
6318 attributes (public, private, bind(c)), MATCH_NO if what's found is
6319 not a handled attribute, and MATCH_YES otherwise. TODO: More error
6320 checking on attribute conflicts needs to be done. */
6323 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
6325 /* See if the derived type is marked as private. */
6326 if (gfc_match (" , private") == MATCH_YES)
6328 if (gfc_current_state () != COMP_MODULE)
6330 gfc_error ("Derived type at %C can only be PRIVATE in the "
6331 "specification part of a module");
6335 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6338 else if (gfc_match (" , public") == MATCH_YES)
6340 if (gfc_current_state () != COMP_MODULE)
6342 gfc_error ("Derived type at %C can only be PUBLIC in the "
6343 "specification part of a module");
6347 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6350 else if (gfc_match(" , bind ( c )") == MATCH_YES)
6352 /* If the type is defined to be bind(c) it then needs to make
6353 sure that all fields are interoperable. This will
6354 need to be a semantic check on the finished derived type.
6355 See 15.2.3 (lines 9-12) of F2003 draft. */
6356 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
6359 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
6361 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
6363 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
6369 /* If we get here, something matched. */
6374 /* Match the beginning of a derived type declaration. If a type name
6375 was the result of a function, then it is possible to have a symbol
6376 already to be known as a derived type yet have no components. */
6379 gfc_match_derived_decl (void)
6381 char name[GFC_MAX_SYMBOL_LEN + 1];
6382 char parent[GFC_MAX_SYMBOL_LEN + 1];
6383 symbol_attribute attr;
6385 gfc_symbol *extended;
6387 match is_type_attr_spec = MATCH_NO;
6388 bool seen_attr = false;
6390 if (gfc_current_state () == COMP_DERIVED)
6395 gfc_clear_attr (&attr);
6400 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
6401 if (is_type_attr_spec == MATCH_ERROR)
6403 if (is_type_attr_spec == MATCH_YES)
6405 } while (is_type_attr_spec == MATCH_YES);
6407 /* Deal with derived type extensions. The extension attribute has
6408 been added to 'attr' but now the parent type must be found and
6411 extended = check_extended_derived_type (parent);
6413 if (parent[0] && !extended)
6416 if (gfc_match (" ::") != MATCH_YES && seen_attr)
6418 gfc_error ("Expected :: in TYPE definition at %C");
6422 m = gfc_match (" %n%t", name);
6426 /* Make sure the name is not the name of an intrinsic type. */
6427 if (gfc_is_intrinsic_typename (name))
6429 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
6434 if (gfc_get_symbol (name, NULL, &sym))
6437 if (sym->ts.type != BT_UNKNOWN)
6439 gfc_error ("Derived type name '%s' at %C already has a basic type "
6440 "of %s", sym->name, gfc_typename (&sym->ts));
6444 /* The symbol may already have the derived attribute without the
6445 components. The ways this can happen is via a function
6446 definition, an INTRINSIC statement or a subtype in another
6447 derived type that is a pointer. The first part of the AND clause
6448 is true if the symbol is not the return value of a function. */
6449 if (sym->attr.flavor != FL_DERIVED
6450 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6453 if (sym->components != NULL || sym->attr.zero_comp)
6455 gfc_error ("Derived type definition of '%s' at %C has already been "
6456 "defined", sym->name);
6460 if (attr.access != ACCESS_UNKNOWN
6461 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6464 /* See if the derived type was labeled as bind(c). */
6465 if (attr.is_bind_c != 0)
6466 sym->attr.is_bind_c = attr.is_bind_c;
6469 /* Construct the f2k_derived namespace if it is not yet there. */
6470 if (!sym->f2k_derived)
6471 sym->f2k_derived = gfc_get_namespace (NULL, 0);
6474 if (extended && !sym->components)
6479 /* Add the extended derived type as the first component. */
6480 gfc_add_component (sym, parent, &p);
6481 sym->attr.extension = attr.extension;
6483 gfc_set_sym_referenced (extended);
6485 p->ts.type = BT_DERIVED;
6486 p->ts.derived = extended;
6487 p->initializer = gfc_default_initializer (&p->ts);
6489 /* Provide the links between the extended type and its extension. */
6490 if (!extended->f2k_derived)
6491 extended->f2k_derived = gfc_get_namespace (NULL, 0);
6492 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
6496 gfc_new_block = sym;
6502 /* Cray Pointees can be declared as:
6503 pointer (ipt, a (n,m,...,*))
6504 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6505 cheat and set a constant bound of 1 for the last dimension, if this
6506 is the case. Since there is no bounds-checking for Cray Pointees,
6507 this will be okay. */
6510 gfc_mod_pointee_as (gfc_array_spec *as)
6512 as->cray_pointee = true; /* This will be useful to know later. */
6513 if (as->type == AS_ASSUMED_SIZE)
6515 as->type = AS_EXPLICIT;
6516 as->upper[as->rank - 1] = gfc_int_expr (1);
6517 as->cp_was_assumed = true;
6519 else if (as->type == AS_ASSUMED_SHAPE)
6521 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6528 /* Match the enum definition statement, here we are trying to match
6529 the first line of enum definition statement.
6530 Returns MATCH_YES if match is found. */
6533 gfc_match_enum (void)
6537 m = gfc_match_eos ();
6541 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
6549 /* Match a variable name with an optional initializer. When this
6550 subroutine is called, a variable is expected to be parsed next.
6551 Depending on what is happening at the moment, updates either the
6552 symbol table or the current interface. */
6555 enumerator_decl (void)
6557 char name[GFC_MAX_SYMBOL_LEN + 1];
6558 gfc_expr *initializer;
6559 gfc_array_spec *as = NULL;
6567 old_locus = gfc_current_locus;
6569 /* When we get here, we've just matched a list of attributes and
6570 maybe a type and a double colon. The next thing we expect to see
6571 is the name of the symbol. */
6572 m = gfc_match_name (name);
6576 var_locus = gfc_current_locus;
6578 /* OK, we've successfully matched the declaration. Now put the
6579 symbol in the current namespace. If we fail to create the symbol,
6581 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6587 /* The double colon must be present in order to have initializers.
6588 Otherwise the statement is ambiguous with an assignment statement. */
6591 if (gfc_match_char ('=') == MATCH_YES)
6593 m = gfc_match_init_expr (&initializer);
6596 gfc_error ("Expected an initialization expression at %C");
6605 /* If we do not have an initializer, the initialization value of the
6606 previous enumerator (stored in last_initializer) is incremented
6607 by 1 and is used to initialize the current enumerator. */
6608 if (initializer == NULL)
6609 initializer = gfc_enum_initializer (last_initializer, old_locus);
6611 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6613 gfc_error("ENUMERATOR %L not initialized with integer expression",
6616 gfc_free_enum_history ();
6620 /* Store this current initializer, for the next enumerator variable
6621 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6622 use last_initializer below. */
6623 last_initializer = initializer;
6624 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6626 /* Maintain enumerator history. */
6627 gfc_find_symbol (name, NULL, 0, &sym);
6628 create_enum_history (sym, last_initializer);
6630 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6633 /* Free stuff up and return. */
6634 gfc_free_expr (initializer);
6640 /* Match the enumerator definition statement. */
6643 gfc_match_enumerator_def (void)
6648 gfc_clear_ts (¤t_ts);
6650 m = gfc_match (" enumerator");
6654 m = gfc_match (" :: ");
6655 if (m == MATCH_ERROR)
6658 colon_seen = (m == MATCH_YES);
6660 if (gfc_current_state () != COMP_ENUM)
6662 gfc_error ("ENUM definition statement expected before %C");
6663 gfc_free_enum_history ();
6667 (¤t_ts)->type = BT_INTEGER;
6668 (¤t_ts)->kind = gfc_c_int_kind;
6670 gfc_clear_attr (¤t_attr);
6671 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
6680 m = enumerator_decl ();
6681 if (m == MATCH_ERROR)
6686 if (gfc_match_eos () == MATCH_YES)
6688 if (gfc_match_char (',') != MATCH_YES)
6692 if (gfc_current_state () == COMP_ENUM)
6694 gfc_free_enum_history ();
6695 gfc_error ("Syntax error in ENUMERATOR definition at %C");
6700 gfc_free_array_spec (current_as);
6707 /* Match a FINAL declaration inside a derived type. */
6710 gfc_match_final_decl (void)
6712 char name[GFC_MAX_SYMBOL_LEN + 1];
6715 gfc_namespace* module_ns;
6718 if (gfc_state_stack->state != COMP_DERIVED)
6720 gfc_error ("FINAL declaration at %C must be inside a derived type "
6725 gcc_assert (gfc_current_block ());
6727 if (!gfc_state_stack->previous
6728 || gfc_state_stack->previous->state != COMP_MODULE)
6730 gfc_error ("Derived type declaration with FINAL at %C must be in the"
6731 " specification part of a MODULE");
6735 module_ns = gfc_current_ns;
6736 gcc_assert (module_ns);
6737 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
6739 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
6740 if (gfc_match (" ::") == MATCH_ERROR)
6743 /* Match the sequence of procedure names. */
6750 if (first && gfc_match_eos () == MATCH_YES)
6752 gfc_error ("Empty FINAL at %C");
6756 m = gfc_match_name (name);
6759 gfc_error ("Expected module procedure name at %C");
6762 else if (m != MATCH_YES)
6765 if (gfc_match_eos () == MATCH_YES)
6767 if (!last && gfc_match_char (',') != MATCH_YES)
6769 gfc_error ("Expected ',' at %C");
6773 if (gfc_get_symbol (name, module_ns, &sym))
6775 gfc_error ("Unknown procedure name \"%s\" at %C", name);
6779 /* Mark the symbol as module procedure. */
6780 if (sym->attr.proc != PROC_MODULE
6781 && gfc_add_procedure (&sym->attr, PROC_MODULE,
6782 sym->name, NULL) == FAILURE)
6785 /* Check if we already have this symbol in the list, this is an error. */
6786 for (f = gfc_current_block ()->f2k_derived->finalizers; f; f = f->next)
6787 if (f->proc_sym == sym)
6789 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
6794 /* Add this symbol to the list of finalizers. */
6795 gcc_assert (gfc_current_block ()->f2k_derived);
6797 f = XCNEW (gfc_finalizer);
6799 f->proc_tree = NULL;
6800 f->where = gfc_current_locus;
6801 f->next = gfc_current_block ()->f2k_derived->finalizers;
6802 gfc_current_block ()->f2k_derived->finalizers = f;