1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 2, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 /* This flag is set if an old-style length selector is matched
30 during a type-declaration statement. */
32 static int old_char_selector;
34 /* When variables acquire types and attributes from a declaration
35 statement, they get them from the following static variables. The
36 first part of a declaration sets these variables and the second
37 part copies these into symbol structures. */
39 static gfc_typespec current_ts;
41 static symbol_attribute current_attr;
42 static gfc_array_spec *current_as;
43 static int colon_seen;
45 /* The current binding label (if any). */
46 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
47 /* Need to know how many identifiers are on the current data declaration
48 line in case we're given the BIND(C) attribute with a NAME= specifier. */
49 static int num_idents_on_line;
50 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
51 can supply a name if the curr_binding_label is nil and NAME= was not. */
52 static int has_name_equals = 0;
54 /* Initializer of the previous enumerator. */
56 static gfc_expr *last_initializer;
58 /* History of all the enumerators is maintained, so that
59 kind values of all the enumerators could be updated depending
60 upon the maximum initialized value. */
62 typedef struct enumerator_history
65 gfc_expr *initializer;
66 struct enumerator_history *next;
70 /* Header of enum history chain. */
72 static enumerator_history *enum_history = NULL;
74 /* Pointer of enum history node containing largest initializer. */
76 static enumerator_history *max_enum = NULL;
78 /* gfc_new_block points to the symbol of a newly matched block. */
80 gfc_symbol *gfc_new_block;
83 /********************* DATA statement subroutines *********************/
85 static bool in_match_data = false;
88 gfc_in_match_data (void)
94 gfc_set_in_match_data (bool set_value)
96 in_match_data = set_value;
99 /* Free a gfc_data_variable structure and everything beneath it. */
102 free_variable (gfc_data_variable *p)
104 gfc_data_variable *q;
109 gfc_free_expr (p->expr);
110 gfc_free_iterator (&p->iter, 0);
111 free_variable (p->list);
117 /* Free a gfc_data_value structure and everything beneath it. */
120 free_value (gfc_data_value *p)
127 gfc_free_expr (p->expr);
133 /* Free a list of gfc_data structures. */
136 gfc_free_data (gfc_data *p)
143 free_variable (p->var);
144 free_value (p->value);
150 /* Free all data in a namespace. */
153 gfc_free_data_all (gfc_namespace *ns)
166 static match var_element (gfc_data_variable *);
168 /* Match a list of variables terminated by an iterator and a right
172 var_list (gfc_data_variable *parent)
174 gfc_data_variable *tail, var;
177 m = var_element (&var);
178 if (m == MATCH_ERROR)
183 tail = gfc_get_data_variable ();
190 if (gfc_match_char (',') != MATCH_YES)
193 m = gfc_match_iterator (&parent->iter, 1);
196 if (m == MATCH_ERROR)
199 m = var_element (&var);
200 if (m == MATCH_ERROR)
205 tail->next = gfc_get_data_variable ();
211 if (gfc_match_char (')') != MATCH_YES)
216 gfc_syntax_error (ST_DATA);
221 /* Match a single element in a data variable list, which can be a
222 variable-iterator list. */
225 var_element (gfc_data_variable *new)
230 memset (new, 0, sizeof (gfc_data_variable));
232 if (gfc_match_char ('(') == MATCH_YES)
233 return var_list (new);
235 m = gfc_match_variable (&new->expr, 0);
239 sym = new->expr->symtree->n.sym;
241 if (!sym->attr.function && gfc_current_ns->parent
242 && gfc_current_ns->parent == sym->ns)
244 gfc_error ("Host associated variable '%s' may not be in the DATA "
245 "statement at %C", sym->name);
249 if (gfc_current_state () != COMP_BLOCK_DATA
250 && sym->attr.in_common
251 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
252 "common block variable '%s' in DATA statement at %C",
253 sym->name) == FAILURE)
256 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
263 /* Match the top-level list of data variables. */
266 top_var_list (gfc_data *d)
268 gfc_data_variable var, *tail, *new;
275 m = var_element (&var);
278 if (m == MATCH_ERROR)
281 new = gfc_get_data_variable ();
291 if (gfc_match_char ('/') == MATCH_YES)
293 if (gfc_match_char (',') != MATCH_YES)
300 gfc_syntax_error (ST_DATA);
301 gfc_free_data_all (gfc_current_ns);
307 match_data_constant (gfc_expr **result)
309 char name[GFC_MAX_SYMBOL_LEN + 1];
315 m = gfc_match_literal_constant (&expr, 1);
322 if (m == MATCH_ERROR)
325 m = gfc_match_null (result);
329 old_loc = gfc_current_locus;
331 /* Should this be a structure component, try to match it
332 before matching a name. */
333 m = gfc_match_rvalue (result);
334 if (m == MATCH_ERROR)
337 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
339 if (gfc_simplify_expr (*result, 0) == FAILURE)
344 gfc_current_locus = old_loc;
346 m = gfc_match_name (name);
350 if (gfc_find_symbol (name, NULL, 1, &sym))
354 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
356 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
360 else if (sym->attr.flavor == FL_DERIVED)
361 return gfc_match_structure_constructor (sym, result);
363 *result = gfc_copy_expr (sym->value);
368 /* Match a list of values in a DATA statement. The leading '/' has
369 already been seen at this point. */
372 top_val_list (gfc_data *data)
374 gfc_data_value *new, *tail;
383 m = match_data_constant (&expr);
386 if (m == MATCH_ERROR)
389 new = gfc_get_data_value ();
398 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
406 msg = gfc_extract_int (expr, &tmp);
407 gfc_free_expr (expr);
415 m = match_data_constant (&tail->expr);
418 if (m == MATCH_ERROR)
422 if (gfc_match_char ('/') == MATCH_YES)
424 if (gfc_match_char (',') == MATCH_NO)
431 gfc_syntax_error (ST_DATA);
432 gfc_free_data_all (gfc_current_ns);
437 /* Matches an old style initialization. */
440 match_old_style_init (const char *name)
447 /* Set up data structure to hold initializers. */
448 gfc_find_sym_tree (name, NULL, 0, &st);
451 newdata = gfc_get_data ();
452 newdata->var = gfc_get_data_variable ();
453 newdata->var->expr = gfc_get_variable_expr (st);
454 newdata->where = gfc_current_locus;
456 /* Match initial value list. This also eats the terminal '/'. */
457 m = top_val_list (newdata);
466 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
471 /* Mark the variable as having appeared in a data statement. */
472 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
478 /* Chain in namespace list of DATA initializers. */
479 newdata->next = gfc_current_ns->data;
480 gfc_current_ns->data = newdata;
486 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
487 we are matching a DATA statement and are therefore issuing an error
488 if we encounter something unexpected, if not, we're trying to match
489 an old-style initialization expression of the form INTEGER I /2/. */
492 gfc_match_data (void)
497 gfc_set_in_match_data (true);
501 new = gfc_get_data ();
502 new->where = gfc_current_locus;
504 m = top_var_list (new);
508 m = top_val_list (new);
512 new->next = gfc_current_ns->data;
513 gfc_current_ns->data = new;
515 if (gfc_match_eos () == MATCH_YES)
518 gfc_match_char (','); /* Optional comma */
521 gfc_set_in_match_data (false);
525 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
532 gfc_set_in_match_data (false);
538 /************************ Declaration statements *********************/
540 /* Match an intent specification. Since this can only happen after an
541 INTENT word, a legal intent-spec must follow. */
544 match_intent_spec (void)
547 if (gfc_match (" ( in out )") == MATCH_YES)
549 if (gfc_match (" ( in )") == MATCH_YES)
551 if (gfc_match (" ( out )") == MATCH_YES)
554 gfc_error ("Bad INTENT specification at %C");
555 return INTENT_UNKNOWN;
559 /* Matches a character length specification, which is either a
560 specification expression or a '*'. */
563 char_len_param_value (gfc_expr **expr)
565 if (gfc_match_char ('*') == MATCH_YES)
571 return gfc_match_expr (expr);
575 /* A character length is a '*' followed by a literal integer or a
576 char_len_param_value in parenthesis. */
579 match_char_length (gfc_expr **expr)
584 m = gfc_match_char ('*');
588 m = gfc_match_small_literal_int (&length, NULL);
589 if (m == MATCH_ERROR)
594 *expr = gfc_int_expr (length);
598 if (gfc_match_char ('(') == MATCH_NO)
601 m = char_len_param_value (expr);
602 if (m == MATCH_ERROR)
607 if (gfc_match_char (')') == MATCH_NO)
609 gfc_free_expr (*expr);
617 gfc_error ("Syntax error in character length specification at %C");
622 /* Special subroutine for finding a symbol. Check if the name is found
623 in the current name space. If not, and we're compiling a function or
624 subroutine and the parent compilation unit is an interface, then check
625 to see if the name we've been given is the name of the interface
626 (located in another namespace). */
629 find_special (const char *name, gfc_symbol **result)
634 i = gfc_get_symbol (name, NULL, result);
638 if (gfc_current_state () != COMP_SUBROUTINE
639 && gfc_current_state () != COMP_FUNCTION)
642 s = gfc_state_stack->previous;
646 if (s->state != COMP_INTERFACE)
649 goto end; /* Nameless interface. */
651 if (strcmp (name, s->sym->name) == 0)
662 /* Special subroutine for getting a symbol node associated with a
663 procedure name, used in SUBROUTINE and FUNCTION statements. The
664 symbol is created in the parent using with symtree node in the
665 child unit pointing to the symbol. If the current namespace has no
666 parent, then the symbol is just created in the current unit. */
669 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
675 /* Module functions have to be left in their own namespace because
676 they have potentially (almost certainly!) already been referenced.
677 In this sense, they are rather like external functions. This is
678 fixed up in resolve.c(resolve_entries), where the symbol name-
679 space is set to point to the master function, so that the fake
680 result mechanism can work. */
681 if (module_fcn_entry)
683 /* Present if entry is declared to be a module procedure. */
684 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
686 rc = gfc_get_symbol (name, NULL, result);
689 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
692 gfc_current_ns->refs++;
694 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
696 /* Trap another encompassed procedure with the same name. All
697 these conditions are necessary to avoid picking up an entry
698 whose name clashes with that of the encompassing procedure;
699 this is handled using gsymbols to register unique,globally
701 if (sym->attr.flavor != 0
702 && sym->attr.proc != 0
703 && (sym->attr.subroutine || sym->attr.function)
704 && sym->attr.if_source != IFSRC_UNKNOWN)
705 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
706 name, &sym->declared_at);
708 /* Trap a procedure with a name the same as interface in the
709 encompassing scope. */
710 if (sym->attr.generic != 0
711 && (sym->attr.subroutine || sym->attr.function)
712 && !sym->attr.mod_proc)
713 gfc_error_now ("Name '%s' at %C is already defined"
714 " as a generic interface at %L",
715 name, &sym->declared_at);
717 /* Trap declarations of attributes in encompassing scope. The
718 signature for this is that ts.kind is set. Legitimate
719 references only set ts.type. */
720 if (sym->ts.kind != 0
721 && !sym->attr.implicit_type
722 && sym->attr.proc == 0
723 && gfc_current_ns->parent != NULL
724 && sym->attr.access == 0
725 && !module_fcn_entry)
726 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
727 "and must not have attributes declared at %L",
728 name, &sym->declared_at);
731 if (gfc_current_ns->parent == NULL || *result == NULL)
734 /* Module function entries will already have a symtree in
735 the current namespace but will need one at module level. */
736 if (module_fcn_entry)
738 /* Present if entry is declared to be a module procedure. */
739 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
741 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
744 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
749 /* See if the procedure should be a module procedure. */
751 if (((sym->ns->proc_name != NULL
752 && sym->ns->proc_name->attr.flavor == FL_MODULE
753 && sym->attr.proc != PROC_MODULE)
754 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
755 && gfc_add_procedure (&sym->attr, PROC_MODULE,
756 sym->name, NULL) == FAILURE)
763 /* Verify that the given symbol representing a parameter is C
764 interoperable, by checking to see if it was marked as such after
765 its declaration. If the given symbol is not interoperable, a
766 warning is reported, thus removing the need to return the status to
767 the calling function. The standard does not require the user use
768 one of the iso_c_binding named constants to declare an
769 interoperable parameter, but we can't be sure if the param is C
770 interop or not if the user doesn't. For example, integer(4) may be
771 legal Fortran, but doesn't have meaning in C. It may interop with
772 a number of the C types, which causes a problem because the
773 compiler can't know which one. This code is almost certainly not
774 portable, and the user will get what they deserve if the C type
775 across platforms isn't always interoperable with integer(4). If
776 the user had used something like integer(c_int) or integer(c_long),
777 the compiler could have automatically handled the varying sizes
781 verify_c_interop_param (gfc_symbol *sym)
783 int is_c_interop = 0;
784 try retval = SUCCESS;
786 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
787 Don't repeat the checks here. */
788 if (sym->attr.implicit_type)
791 /* For subroutines or functions that are passed to a BIND(C) procedure,
792 they're interoperable if they're BIND(C) and their params are all
794 if (sym->attr.flavor == FL_PROCEDURE)
796 if (sym->attr.is_bind_c == 0)
798 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
799 "attribute to be C interoperable", sym->name,
800 &(sym->declared_at));
806 if (sym->attr.is_c_interop == 1)
807 /* We've already checked this procedure; don't check it again. */
810 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
815 /* See if we've stored a reference to a procedure that owns sym. */
816 if (sym->ns != NULL && sym->ns->proc_name != NULL)
818 if (sym->ns->proc_name->attr.is_bind_c == 1)
821 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
824 if (is_c_interop != 1)
826 /* Make personalized messages to give better feedback. */
827 if (sym->ts.type == BT_DERIVED)
828 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
829 " procedure '%s' but is not C interoperable "
830 "because derived type '%s' is not C interoperable",
831 sym->name, &(sym->declared_at),
832 sym->ns->proc_name->name,
833 sym->ts.derived->name);
835 gfc_warning ("Variable '%s' at %L is a parameter to the "
836 "BIND(C) procedure '%s' but may not be C "
838 sym->name, &(sym->declared_at),
839 sym->ns->proc_name->name);
842 /* Character strings are only C interoperable if they have a
844 if (sym->ts.type == BT_CHARACTER)
846 gfc_charlen *cl = sym->ts.cl;
847 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
848 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
850 gfc_error ("Character argument '%s' at %L "
851 "must be length 1 because "
852 "procedure '%s' is BIND(C)",
853 sym->name, &sym->declared_at,
854 sym->ns->proc_name->name);
859 /* We have to make sure that any param to a bind(c) routine does
860 not have the allocatable, pointer, or optional attributes,
861 according to J3/04-007, section 5.1. */
862 if (sym->attr.allocatable == 1)
864 gfc_error ("Variable '%s' at %L cannot have the "
865 "ALLOCATABLE attribute because procedure '%s'"
866 " is BIND(C)", sym->name, &(sym->declared_at),
867 sym->ns->proc_name->name);
871 if (sym->attr.pointer == 1)
873 gfc_error ("Variable '%s' at %L cannot have the "
874 "POINTER attribute because procedure '%s'"
875 " is BIND(C)", sym->name, &(sym->declared_at),
876 sym->ns->proc_name->name);
880 if (sym->attr.optional == 1)
882 gfc_error ("Variable '%s' at %L cannot have the "
883 "OPTIONAL attribute because procedure '%s'"
884 " is BIND(C)", sym->name, &(sym->declared_at),
885 sym->ns->proc_name->name);
889 /* Make sure that if it has the dimension attribute, that it is
890 either assumed size or explicit shape. */
893 if (sym->as->type == AS_ASSUMED_SHAPE)
895 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
896 "argument to the procedure '%s' at %L because "
897 "the procedure is BIND(C)", sym->name,
898 &(sym->declared_at), sym->ns->proc_name->name,
899 &(sym->ns->proc_name->declared_at));
903 if (sym->as->type == AS_DEFERRED)
905 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
906 "argument to the procedure '%s' at %L because "
907 "the procedure is BIND(C)", sym->name,
908 &(sym->declared_at), sym->ns->proc_name->name,
909 &(sym->ns->proc_name->declared_at));
920 /* Function called by variable_decl() that adds a name to the symbol table. */
923 build_sym (const char *name, gfc_charlen *cl,
924 gfc_array_spec **as, locus *var_locus)
926 symbol_attribute attr;
929 if (gfc_get_symbol (name, NULL, &sym))
932 /* Start updating the symbol table. Add basic type attribute if present. */
933 if (current_ts.type != BT_UNKNOWN
934 && (sym->attr.implicit_type == 0
935 || !gfc_compare_types (&sym->ts, ¤t_ts))
936 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
939 if (sym->ts.type == BT_CHARACTER)
942 /* Add dimension attribute if present. */
943 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
947 /* Add attribute to symbol. The copy is so that we can reset the
948 dimension attribute. */
952 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
955 /* Finish any work that may need to be done for the binding label,
956 if it's a bind(c). The bind(c) attr is found before the symbol
957 is made, and before the symbol name (for data decls), so the
958 current_ts is holding the binding label, or nothing if the
959 name= attr wasn't given. Therefore, test here if we're dealing
960 with a bind(c) and make sure the binding label is set correctly. */
961 if (sym->attr.is_bind_c == 1)
963 if (sym->binding_label[0] == '\0')
965 /* Here, we're not checking the numIdents (the last param).
966 This could be an error we're letting slip through! */
967 if (set_binding_label (sym->binding_label, sym->name, 1) == FAILURE)
972 /* See if we know we're in a common block, and if it's a bind(c)
973 common then we need to make sure we're an interoperable type. */
974 if (sym->attr.in_common == 1)
976 /* Test the common block object. */
977 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
978 && sym->ts.is_c_interop != 1)
980 gfc_error_now ("Variable '%s' in common block '%s' at %C "
981 "must be declared with a C interoperable "
982 "kind since common block '%s' is BIND(C)",
983 sym->name, sym->common_block->name,
984 sym->common_block->name);
989 sym->attr.implied_index = 0;
995 /* Set character constant to the given length. The constant will be padded or
999 gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
1004 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1005 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
1007 slen = expr->value.character.length;
1010 s = gfc_getmem (len + 1);
1011 memcpy (s, expr->value.character.string, MIN (len, slen));
1013 memset (&s[slen], ' ', len - slen);
1015 if (gfc_option.warn_character_truncation && slen > len)
1016 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1017 "(%d/%d)", &expr->where, slen, len);
1019 /* Apply the standard by 'hand' otherwise it gets cleared for
1021 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
1022 gfc_error_now ("The CHARACTER elements of the array constructor "
1023 "at %L must have the same length (%d/%d)",
1024 &expr->where, slen, len);
1027 gfc_free (expr->value.character.string);
1028 expr->value.character.string = s;
1029 expr->value.character.length = len;
1034 /* Function to create and update the enumerator history
1035 using the information passed as arguments.
1036 Pointer "max_enum" is also updated, to point to
1037 enum history node containing largest initializer.
1039 SYM points to the symbol node of enumerator.
1040 INIT points to its enumerator value. */
1043 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1045 enumerator_history *new_enum_history;
1046 gcc_assert (sym != NULL && init != NULL);
1048 new_enum_history = gfc_getmem (sizeof (enumerator_history));
1050 new_enum_history->sym = sym;
1051 new_enum_history->initializer = init;
1052 new_enum_history->next = NULL;
1054 if (enum_history == NULL)
1056 enum_history = new_enum_history;
1057 max_enum = enum_history;
1061 new_enum_history->next = enum_history;
1062 enum_history = new_enum_history;
1064 if (mpz_cmp (max_enum->initializer->value.integer,
1065 new_enum_history->initializer->value.integer) < 0)
1066 max_enum = new_enum_history;
1071 /* Function to free enum kind history. */
1074 gfc_free_enum_history (void)
1076 enumerator_history *current = enum_history;
1077 enumerator_history *next;
1079 while (current != NULL)
1081 next = current->next;
1086 enum_history = NULL;
1090 /* Function called by variable_decl() that adds an initialization
1091 expression to a symbol. */
1094 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1096 symbol_attribute attr;
1101 if (find_special (name, &sym))
1106 /* If this symbol is confirming an implicit parameter type,
1107 then an initialization expression is not allowed. */
1108 if (attr.flavor == FL_PARAMETER
1109 && sym->value != NULL
1112 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1121 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
1128 /* An initializer is required for PARAMETER declarations. */
1129 if (attr.flavor == FL_PARAMETER)
1131 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1137 /* If a variable appears in a DATA block, it cannot have an
1141 gfc_error ("Variable '%s' at %C with an initializer already "
1142 "appears in a DATA statement", sym->name);
1146 /* Check if the assignment can happen. This has to be put off
1147 until later for a derived type variable. */
1148 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1149 && gfc_check_assign_symbol (sym, init) == FAILURE)
1152 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1154 /* Update symbol character length according initializer. */
1155 if (sym->ts.cl->length == NULL)
1157 /* If there are multiple CHARACTER variables declared on the
1158 same line, we don't want them to share the same length. */
1159 sym->ts.cl = gfc_get_charlen ();
1160 sym->ts.cl->next = gfc_current_ns->cl_list;
1161 gfc_current_ns->cl_list = sym->ts.cl;
1163 if (sym->attr.flavor == FL_PARAMETER
1164 && init->expr_type == EXPR_ARRAY)
1165 sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
1167 /* Update initializer character length according symbol. */
1168 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1170 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1171 gfc_constructor * p;
1173 if (init->expr_type == EXPR_CONSTANT)
1174 gfc_set_constant_character_len (len, init, false);
1175 else if (init->expr_type == EXPR_ARRAY)
1177 /* Build a new charlen to prevent simplification from
1178 deleting the length before it is resolved. */
1179 init->ts.cl = gfc_get_charlen ();
1180 init->ts.cl->next = gfc_current_ns->cl_list;
1181 gfc_current_ns->cl_list = sym->ts.cl;
1182 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
1184 for (p = init->value.constructor; p; p = p->next)
1185 gfc_set_constant_character_len (len, p->expr, false);
1190 /* Need to check if the expression we initialized this
1191 to was one of the iso_c_binding named constants. If so,
1192 and we're a parameter (constant), let it be iso_c.
1194 integer(c_int), parameter :: my_int = c_int
1195 integer(my_int) :: my_int_2
1196 If we mark my_int as iso_c (since we can see it's value
1197 is equal to one of the named constants), then my_int_2
1198 will be considered C interoperable. */
1199 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1201 sym->ts.is_iso_c |= init->ts.is_iso_c;
1202 sym->ts.is_c_interop |= init->ts.is_c_interop;
1203 /* attr bits needed for module files. */
1204 sym->attr.is_iso_c |= init->ts.is_iso_c;
1205 sym->attr.is_c_interop |= init->ts.is_c_interop;
1206 if (init->ts.is_iso_c)
1207 sym->ts.f90_type = init->ts.f90_type;
1210 /* Add initializer. Make sure we keep the ranks sane. */
1211 if (sym->attr.dimension && init->rank == 0)
1217 if (sym->attr.flavor == FL_PARAMETER
1218 && init->expr_type == EXPR_CONSTANT
1219 && spec_size (sym->as, &size) == SUCCESS
1220 && mpz_cmp_si (size, 0) > 0)
1222 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1225 array->value.constructor = c = NULL;
1226 for (n = 0; n < (int)mpz_get_si (size); n++)
1228 if (array->value.constructor == NULL)
1230 array->value.constructor = c = gfc_get_constructor ();
1235 c->next = gfc_get_constructor ();
1237 c->expr = gfc_copy_expr (init);
1241 array->shape = gfc_get_shape (sym->as->rank);
1242 for (n = 0; n < sym->as->rank; n++)
1243 spec_dimen_size (sym->as, n, &array->shape[n]);
1248 init->rank = sym->as->rank;
1252 if (sym->attr.save == SAVE_NONE)
1253 sym->attr.save = SAVE_IMPLICIT;
1261 /* Function called by variable_decl() that adds a name to a structure
1265 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1266 gfc_array_spec **as)
1270 /* If the current symbol is of the same derived type that we're
1271 constructing, it must have the pointer attribute. */
1272 if (current_ts.type == BT_DERIVED
1273 && current_ts.derived == gfc_current_block ()
1274 && current_attr.pointer == 0)
1276 gfc_error ("Component at %C must have the POINTER attribute");
1280 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1282 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1284 gfc_error ("Array component of structure at %C must have explicit "
1285 "or deferred shape");
1290 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1295 gfc_set_component_attr (c, ¤t_attr);
1297 c->initializer = *init;
1305 /* Check array components. */
1310 gfc_error ("Allocatable component at %C must be an array");
1319 if (c->as->type != AS_DEFERRED)
1321 gfc_error ("Pointer array component of structure at %C must have a "
1326 else if (c->allocatable)
1328 if (c->as->type != AS_DEFERRED)
1330 gfc_error ("Allocatable component of structure at %C must have a "
1337 if (c->as->type != AS_EXPLICIT)
1339 gfc_error ("Array component of structure at %C must have an "
1349 /* Match a 'NULL()', and possibly take care of some side effects. */
1352 gfc_match_null (gfc_expr **result)
1358 m = gfc_match (" null ( )");
1362 /* The NULL symbol now has to be/become an intrinsic function. */
1363 if (gfc_get_symbol ("null", NULL, &sym))
1365 gfc_error ("NULL() initialization at %C is ambiguous");
1369 gfc_intrinsic_symbol (sym);
1371 if (sym->attr.proc != PROC_INTRINSIC
1372 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1373 sym->name, NULL) == FAILURE
1374 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1377 e = gfc_get_expr ();
1378 e->where = gfc_current_locus;
1379 e->expr_type = EXPR_NULL;
1380 e->ts.type = BT_UNKNOWN;
1388 /* Match a variable name with an optional initializer. When this
1389 subroutine is called, a variable is expected to be parsed next.
1390 Depending on what is happening at the moment, updates either the
1391 symbol table or the current interface. */
1394 variable_decl (int elem)
1396 char name[GFC_MAX_SYMBOL_LEN + 1];
1397 gfc_expr *initializer, *char_len;
1399 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1410 old_locus = gfc_current_locus;
1412 /* When we get here, we've just matched a list of attributes and
1413 maybe a type and a double colon. The next thing we expect to see
1414 is the name of the symbol. */
1415 m = gfc_match_name (name);
1419 var_locus = gfc_current_locus;
1421 /* Now we could see the optional array spec. or character length. */
1422 m = gfc_match_array_spec (&as);
1423 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1424 cp_as = gfc_copy_array_spec (as);
1425 else if (m == MATCH_ERROR)
1429 as = gfc_copy_array_spec (current_as);
1434 if (current_ts.type == BT_CHARACTER)
1436 switch (match_char_length (&char_len))
1439 cl = gfc_get_charlen ();
1440 cl->next = gfc_current_ns->cl_list;
1441 gfc_current_ns->cl_list = cl;
1443 cl->length = char_len;
1446 /* Non-constant lengths need to be copied after the first
1449 if (elem > 1 && current_ts.cl->length
1450 && current_ts.cl->length->expr_type != EXPR_CONSTANT)
1452 cl = gfc_get_charlen ();
1453 cl->next = gfc_current_ns->cl_list;
1454 gfc_current_ns->cl_list = cl;
1455 cl->length = gfc_copy_expr (current_ts.cl->length);
1467 /* If this symbol has already shown up in a Cray Pointer declaration,
1468 then we want to set the type & bail out. */
1469 if (gfc_option.flag_cray_pointer)
1471 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1472 if (sym != NULL && sym->attr.cray_pointee)
1474 sym->ts.type = current_ts.type;
1475 sym->ts.kind = current_ts.kind;
1477 sym->ts.derived = current_ts.derived;
1478 sym->ts.is_c_interop = current_ts.is_c_interop;
1479 sym->ts.is_iso_c = current_ts.is_iso_c;
1482 /* Check to see if we have an array specification. */
1485 if (sym->as != NULL)
1487 gfc_error ("Duplicate array spec for Cray pointee at %C");
1488 gfc_free_array_spec (cp_as);
1494 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1495 gfc_internal_error ("Couldn't set pointee array spec.");
1497 /* Fix the array spec. */
1498 m = gfc_mod_pointee_as (sym->as);
1499 if (m == MATCH_ERROR)
1507 gfc_free_array_spec (cp_as);
1512 /* OK, we've successfully matched the declaration. Now put the
1513 symbol in the current namespace, because it might be used in the
1514 optional initialization expression for this symbol, e.g. this is
1517 integer, parameter :: i = huge(i)
1519 This is only true for parameters or variables of a basic type.
1520 For components of derived types, it is not true, so we don't
1521 create a symbol for those yet. If we fail to create the symbol,
1523 if (gfc_current_state () != COMP_DERIVED
1524 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1530 /* An interface body specifies all of the procedure's
1531 characteristics and these shall be consistent with those
1532 specified in the procedure definition, except that the interface
1533 may specify a procedure that is not pure if the procedure is
1534 defined to be pure(12.3.2). */
1535 if (current_ts.type == BT_DERIVED
1536 && gfc_current_ns->proc_name
1537 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1538 && current_ts.derived->ns != gfc_current_ns
1539 && !gfc_current_ns->has_import_set)
1541 gfc_error ("the type of '%s' at %C has not been declared within the "
1547 /* In functions that have a RESULT variable defined, the function
1548 name always refers to function calls. Therefore, the name is
1549 not allowed to appear in specification statements. */
1550 if (gfc_current_state () == COMP_FUNCTION
1551 && gfc_current_block () != NULL
1552 && gfc_current_block ()->result != NULL
1553 && gfc_current_block ()->result != gfc_current_block ()
1554 && strcmp (gfc_current_block ()->name, name) == 0)
1556 gfc_error ("Function name '%s' not allowed at %C", name);
1561 /* We allow old-style initializations of the form
1562 integer i /2/, j(4) /3*3, 1/
1563 (if no colon has been seen). These are different from data
1564 statements in that initializers are only allowed to apply to the
1565 variable immediately preceding, i.e.
1567 is not allowed. Therefore we have to do some work manually, that
1568 could otherwise be left to the matchers for DATA statements. */
1570 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1572 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1573 "initialization at %C") == FAILURE)
1576 return match_old_style_init (name);
1579 /* The double colon must be present in order to have initializers.
1580 Otherwise the statement is ambiguous with an assignment statement. */
1583 if (gfc_match (" =>") == MATCH_YES)
1585 if (!current_attr.pointer)
1587 gfc_error ("Initialization at %C isn't for a pointer variable");
1592 m = gfc_match_null (&initializer);
1595 gfc_error ("Pointer initialization requires a NULL() at %C");
1599 if (gfc_pure (NULL))
1601 gfc_error ("Initialization of pointer at %C is not allowed in "
1602 "a PURE procedure");
1610 else if (gfc_match_char ('=') == MATCH_YES)
1612 if (current_attr.pointer)
1614 gfc_error ("Pointer initialization at %C requires '=>', "
1620 m = gfc_match_init_expr (&initializer);
1623 gfc_error ("Expected an initialization expression at %C");
1627 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1629 gfc_error ("Initialization of variable at %C is not allowed in "
1630 "a PURE procedure");
1639 if (initializer != NULL && current_attr.allocatable
1640 && gfc_current_state () == COMP_DERIVED)
1642 gfc_error ("Initialization of allocatable component at %C is not "
1648 /* Add the initializer. Note that it is fine if initializer is
1649 NULL here, because we sometimes also need to check if a
1650 declaration *must* have an initialization expression. */
1651 if (gfc_current_state () != COMP_DERIVED)
1652 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1655 if (current_ts.type == BT_DERIVED
1656 && !current_attr.pointer && !initializer)
1657 initializer = gfc_default_initializer (¤t_ts);
1658 t = build_struct (name, cl, &initializer, &as);
1661 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1664 /* Free stuff up and return. */
1665 gfc_free_expr (initializer);
1666 gfc_free_array_spec (as);
1672 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1673 This assumes that the byte size is equal to the kind number for
1674 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1677 gfc_match_old_kind_spec (gfc_typespec *ts)
1682 if (gfc_match_char ('*') != MATCH_YES)
1685 m = gfc_match_small_literal_int (&ts->kind, NULL);
1689 original_kind = ts->kind;
1691 /* Massage the kind numbers for complex types. */
1692 if (ts->type == BT_COMPLEX)
1696 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1697 gfc_basic_typename (ts->type), original_kind);
1703 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1705 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1706 gfc_basic_typename (ts->type), original_kind);
1710 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1711 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1718 /* Match a kind specification. Since kinds are generally optional, we
1719 usually return MATCH_NO if something goes wrong. If a "kind="
1720 string is found, then we know we have an error. */
1723 gfc_match_kind_spec (gfc_typespec *ts)
1733 where = gfc_current_locus;
1735 if (gfc_match_char ('(') == MATCH_NO)
1738 /* Also gobbles optional text. */
1739 if (gfc_match (" kind = ") == MATCH_YES)
1742 n = gfc_match_init_expr (&e);
1744 gfc_error ("Expected initialization expression at %C");
1750 gfc_error ("Expected scalar initialization expression at %C");
1755 msg = gfc_extract_int (e, &ts->kind);
1763 /* Before throwing away the expression, let's see if we had a
1764 C interoperable kind (and store the fact). */
1765 if (e->ts.is_c_interop == 1)
1767 /* Mark this as c interoperable if being declared with one
1768 of the named constants from iso_c_binding. */
1769 ts->is_c_interop = e->ts.is_iso_c;
1770 ts->f90_type = e->ts.f90_type;
1776 /* Ignore errors to this point, if we've gotten here. This means
1777 we ignore the m=MATCH_ERROR from above. */
1778 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1780 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1781 gfc_basic_typename (ts->type));
1784 else if (gfc_match_char (')') != MATCH_YES)
1786 gfc_error ("Missing right parenthesis at %C");
1790 /* All tests passed. */
1793 if(m == MATCH_ERROR)
1794 gfc_current_locus = where;
1796 /* Return what we know from the test(s). */
1801 gfc_current_locus = where;
1806 /* Match the various kind/length specifications in a CHARACTER
1807 declaration. We don't return MATCH_NO. */
1810 match_char_spec (gfc_typespec *ts)
1812 int kind, seen_length;
1816 gfc_expr *kind_expr = NULL;
1817 kind = gfc_default_character_kind;
1821 /* Try the old-style specification first. */
1822 old_char_selector = 0;
1824 m = match_char_length (&len);
1828 old_char_selector = 1;
1833 m = gfc_match_char ('(');
1836 m = MATCH_YES; /* Character without length is a single char. */
1840 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
1841 if (gfc_match (" kind =") == MATCH_YES)
1843 m = gfc_match_small_int_expr(&kind, &kind_expr);
1845 if (m == MATCH_ERROR)
1850 if (gfc_match (" , len =") == MATCH_NO)
1853 m = char_len_param_value (&len);
1856 if (m == MATCH_ERROR)
1863 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
1864 if (gfc_match (" len =") == MATCH_YES)
1866 m = char_len_param_value (&len);
1869 if (m == MATCH_ERROR)
1873 if (gfc_match_char (')') == MATCH_YES)
1876 if (gfc_match (" , kind =") != MATCH_YES)
1879 gfc_match_small_int_expr(&kind, &kind_expr);
1881 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1883 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1890 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
1891 m = char_len_param_value (&len);
1894 if (m == MATCH_ERROR)
1898 m = gfc_match_char (')');
1902 if (gfc_match_char (',') != MATCH_YES)
1905 gfc_match (" kind ="); /* Gobble optional text. */
1907 m = gfc_match_small_int_expr(&kind, &kind_expr);
1908 if (m == MATCH_ERROR)
1914 /* Require a right-paren at this point. */
1915 m = gfc_match_char (')');
1920 gfc_error ("Syntax error in CHARACTER declaration at %C");
1922 gfc_free_expr (len);
1926 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1928 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1932 if (seen_length == 1 && len != NULL
1933 && len->ts.type != BT_INTEGER && len->ts.type != BT_UNKNOWN)
1935 gfc_error ("Expression at %C must be of INTEGER type");
1941 gfc_free_expr (len);
1942 gfc_free_expr (kind_expr);
1946 /* Do some final massaging of the length values. */
1947 cl = gfc_get_charlen ();
1948 cl->next = gfc_current_ns->cl_list;
1949 gfc_current_ns->cl_list = cl;
1951 if (seen_length == 0)
1952 cl->length = gfc_int_expr (1);
1959 /* We have to know if it was a c interoperable kind so we can
1960 do accurate type checking of bind(c) procs, etc. */
1961 if (kind_expr != NULL)
1963 /* Mark this as c interoperable if being declared with one
1964 of the named constants from iso_c_binding. */
1965 ts->is_c_interop = kind_expr->ts.is_iso_c;
1966 gfc_free_expr (kind_expr);
1968 else if (len != NULL)
1970 /* Here, we might have parsed something such as:
1972 In this case, the parsing code above grabs the c_char when
1973 looking for the length (line 1690, roughly). it's the last
1974 testcase for parsing the kind params of a character variable.
1975 However, it's not actually the length. this seems like it
1977 To see if the user used a C interop kind, test the expr
1978 of the so called length, and see if it's C interoperable. */
1979 ts->is_c_interop = len->ts.is_iso_c;
1986 /* Matches a type specification. If successful, sets the ts structure
1987 to the matched specification. This is necessary for FUNCTION and
1988 IMPLICIT statements.
1990 If implicit_flag is nonzero, then we don't check for the optional
1991 kind specification. Not doing so is needed for matching an IMPLICIT
1992 statement correctly. */
1995 match_type_spec (gfc_typespec *ts, int implicit_flag)
1997 char name[GFC_MAX_SYMBOL_LEN + 1];
2004 /* Clear the current binding label, in case one is given. */
2005 curr_binding_label[0] = '\0';
2007 if (gfc_match (" byte") == MATCH_YES)
2009 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
2013 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2015 gfc_error ("BYTE type used at %C "
2016 "is not available on the target machine");
2020 ts->type = BT_INTEGER;
2025 if (gfc_match (" integer") == MATCH_YES)
2027 ts->type = BT_INTEGER;
2028 ts->kind = gfc_default_integer_kind;
2032 if (gfc_match (" character") == MATCH_YES)
2034 ts->type = BT_CHARACTER;
2035 if (implicit_flag == 0)
2036 return match_char_spec (ts);
2041 if (gfc_match (" real") == MATCH_YES)
2044 ts->kind = gfc_default_real_kind;
2048 if (gfc_match (" double precision") == MATCH_YES)
2051 ts->kind = gfc_default_double_kind;
2055 if (gfc_match (" complex") == MATCH_YES)
2057 ts->type = BT_COMPLEX;
2058 ts->kind = gfc_default_complex_kind;
2062 if (gfc_match (" double complex") == MATCH_YES)
2064 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2065 "conform to the Fortran 95 standard") == FAILURE)
2068 ts->type = BT_COMPLEX;
2069 ts->kind = gfc_default_double_kind;
2073 if (gfc_match (" logical") == MATCH_YES)
2075 ts->type = BT_LOGICAL;
2076 ts->kind = gfc_default_logical_kind;
2080 m = gfc_match (" type ( %n )", name);
2084 /* Search for the name but allow the components to be defined later. */
2085 if (gfc_get_ha_symbol (name, &sym))
2087 gfc_error ("Type name '%s' at %C is ambiguous", name);
2091 if (sym->attr.flavor != FL_DERIVED
2092 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2095 ts->type = BT_DERIVED;
2102 /* For all types except double, derived and character, look for an
2103 optional kind specifier. MATCH_NO is actually OK at this point. */
2104 if (implicit_flag == 1)
2107 if (gfc_current_form == FORM_FREE)
2109 c = gfc_peek_char();
2110 if (!gfc_is_whitespace(c) && c != '*' && c != '('
2111 && c != ':' && c != ',')
2115 m = gfc_match_kind_spec (ts);
2116 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2117 m = gfc_match_old_kind_spec (ts);
2120 m = MATCH_YES; /* No kind specifier found. */
2126 /* Match an IMPLICIT NONE statement. Actually, this statement is
2127 already matched in parse.c, or we would not end up here in the
2128 first place. So the only thing we need to check, is if there is
2129 trailing garbage. If not, the match is successful. */
2132 gfc_match_implicit_none (void)
2134 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2138 /* Match the letter range(s) of an IMPLICIT statement. */
2141 match_implicit_range (void)
2143 int c, c1, c2, inner;
2146 cur_loc = gfc_current_locus;
2148 gfc_gobble_whitespace ();
2149 c = gfc_next_char ();
2152 gfc_error ("Missing character range in IMPLICIT at %C");
2159 gfc_gobble_whitespace ();
2160 c1 = gfc_next_char ();
2164 gfc_gobble_whitespace ();
2165 c = gfc_next_char ();
2170 inner = 0; /* Fall through. */
2177 gfc_gobble_whitespace ();
2178 c2 = gfc_next_char ();
2182 gfc_gobble_whitespace ();
2183 c = gfc_next_char ();
2185 if ((c != ',') && (c != ')'))
2198 gfc_error ("Letters must be in alphabetic order in "
2199 "IMPLICIT statement at %C");
2203 /* See if we can add the newly matched range to the pending
2204 implicits from this IMPLICIT statement. We do not check for
2205 conflicts with whatever earlier IMPLICIT statements may have
2206 set. This is done when we've successfully finished matching
2208 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2215 gfc_syntax_error (ST_IMPLICIT);
2217 gfc_current_locus = cur_loc;
2222 /* Match an IMPLICIT statement, storing the types for
2223 gfc_set_implicit() if the statement is accepted by the parser.
2224 There is a strange looking, but legal syntactic construction
2225 possible. It looks like:
2227 IMPLICIT INTEGER (a-b) (c-d)
2229 This is legal if "a-b" is a constant expression that happens to
2230 equal one of the legal kinds for integers. The real problem
2231 happens with an implicit specification that looks like:
2233 IMPLICIT INTEGER (a-b)
2235 In this case, a typespec matcher that is "greedy" (as most of the
2236 matchers are) gobbles the character range as a kindspec, leaving
2237 nothing left. We therefore have to go a bit more slowly in the
2238 matching process by inhibiting the kindspec checking during
2239 typespec matching and checking for a kind later. */
2242 gfc_match_implicit (void)
2249 /* We don't allow empty implicit statements. */
2250 if (gfc_match_eos () == MATCH_YES)
2252 gfc_error ("Empty IMPLICIT statement at %C");
2258 /* First cleanup. */
2259 gfc_clear_new_implicit ();
2261 /* A basic type is mandatory here. */
2262 m = match_type_spec (&ts, 1);
2263 if (m == MATCH_ERROR)
2268 cur_loc = gfc_current_locus;
2269 m = match_implicit_range ();
2273 /* We may have <TYPE> (<RANGE>). */
2274 gfc_gobble_whitespace ();
2275 c = gfc_next_char ();
2276 if ((c == '\n') || (c == ','))
2278 /* Check for CHARACTER with no length parameter. */
2279 if (ts.type == BT_CHARACTER && !ts.cl)
2281 ts.kind = gfc_default_character_kind;
2282 ts.cl = gfc_get_charlen ();
2283 ts.cl->next = gfc_current_ns->cl_list;
2284 gfc_current_ns->cl_list = ts.cl;
2285 ts.cl->length = gfc_int_expr (1);
2288 /* Record the Successful match. */
2289 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2294 gfc_current_locus = cur_loc;
2297 /* Discard the (incorrectly) matched range. */
2298 gfc_clear_new_implicit ();
2300 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2301 if (ts.type == BT_CHARACTER)
2302 m = match_char_spec (&ts);
2305 m = gfc_match_kind_spec (&ts);
2308 m = gfc_match_old_kind_spec (&ts);
2309 if (m == MATCH_ERROR)
2315 if (m == MATCH_ERROR)
2318 m = match_implicit_range ();
2319 if (m == MATCH_ERROR)
2324 gfc_gobble_whitespace ();
2325 c = gfc_next_char ();
2326 if ((c != '\n') && (c != ','))
2329 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2337 gfc_syntax_error (ST_IMPLICIT);
2345 gfc_match_import (void)
2347 char name[GFC_MAX_SYMBOL_LEN + 1];
2352 if (gfc_current_ns->proc_name == NULL
2353 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2355 gfc_error ("IMPORT statement at %C only permitted in "
2356 "an INTERFACE body");
2360 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2364 if (gfc_match_eos () == MATCH_YES)
2366 /* All host variables should be imported. */
2367 gfc_current_ns->has_import_set = 1;
2371 if (gfc_match (" ::") == MATCH_YES)
2373 if (gfc_match_eos () == MATCH_YES)
2375 gfc_error ("Expecting list of named entities at %C");
2382 m = gfc_match (" %n", name);
2386 if (gfc_current_ns->parent != NULL
2387 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2389 gfc_error ("Type name '%s' at %C is ambiguous", name);
2392 else if (gfc_current_ns->proc_name->ns->parent != NULL
2393 && gfc_find_symbol (name,
2394 gfc_current_ns->proc_name->ns->parent,
2397 gfc_error ("Type name '%s' at %C is ambiguous", name);
2403 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2404 "at %C - does not exist.", name);
2408 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2410 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2415 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2418 sym->ns = gfc_current_ns;
2430 if (gfc_match_eos () == MATCH_YES)
2432 if (gfc_match_char (',') != MATCH_YES)
2439 gfc_error ("Syntax error in IMPORT statement at %C");
2444 /* Matches an attribute specification including array specs. If
2445 successful, leaves the variables current_attr and current_as
2446 holding the specification. Also sets the colon_seen variable for
2447 later use by matchers associated with initializations.
2449 This subroutine is a little tricky in the sense that we don't know
2450 if we really have an attr-spec until we hit the double colon.
2451 Until that time, we can only return MATCH_NO. This forces us to
2452 check for duplicate specification at this level. */
2455 match_attr_spec (void)
2457 /* Modifiers that can exist in a type statement. */
2459 { GFC_DECL_BEGIN = 0,
2460 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2461 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2462 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2463 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2464 DECL_IS_BIND_C, DECL_COLON, DECL_NONE,
2465 GFC_DECL_END /* Sentinel */
2469 /* GFC_DECL_END is the sentinel, index starts at 0. */
2470 #define NUM_DECL GFC_DECL_END
2472 static mstring decls[] = {
2473 minit (", allocatable", DECL_ALLOCATABLE),
2474 minit (", dimension", DECL_DIMENSION),
2475 minit (", external", DECL_EXTERNAL),
2476 minit (", intent ( in )", DECL_IN),
2477 minit (", intent ( out )", DECL_OUT),
2478 minit (", intent ( in out )", DECL_INOUT),
2479 minit (", intrinsic", DECL_INTRINSIC),
2480 minit (", optional", DECL_OPTIONAL),
2481 minit (", parameter", DECL_PARAMETER),
2482 minit (", pointer", DECL_POINTER),
2483 minit (", protected", DECL_PROTECTED),
2484 minit (", private", DECL_PRIVATE),
2485 minit (", public", DECL_PUBLIC),
2486 minit (", save", DECL_SAVE),
2487 minit (", target", DECL_TARGET),
2488 minit (", value", DECL_VALUE),
2489 minit (", volatile", DECL_VOLATILE),
2490 minit ("::", DECL_COLON),
2491 minit (NULL, DECL_NONE)
2494 locus start, seen_at[NUM_DECL];
2502 gfc_clear_attr (¤t_attr);
2503 start = gfc_current_locus;
2508 /* See if we get all of the keywords up to the final double colon. */
2509 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2514 d = (decl_types) gfc_match_strings (decls);
2518 /* See if we can find the bind(c) since all else failed.
2519 We need to skip over any whitespace and stop on the ','. */
2520 gfc_gobble_whitespace ();
2521 peek_char = gfc_peek_char ();
2522 if (peek_char == ',')
2524 /* Chomp the comma. */
2525 peek_char = gfc_next_char ();
2526 /* Try and match the bind(c). */
2527 if (gfc_match_bind_c (NULL) == MATCH_YES)
2532 if (d == DECL_NONE || d == DECL_COLON)
2536 seen_at[d] = gfc_current_locus;
2538 if (d == DECL_DIMENSION)
2540 m = gfc_match_array_spec (¤t_as);
2544 gfc_error ("Missing dimension specification at %C");
2548 if (m == MATCH_ERROR)
2553 /* No double colon, so assume that we've been looking at something
2554 else the whole time. */
2561 /* Since we've seen a double colon, we have to be looking at an
2562 attr-spec. This means that we can now issue errors. */
2563 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2568 case DECL_ALLOCATABLE:
2569 attr = "ALLOCATABLE";
2571 case DECL_DIMENSION:
2578 attr = "INTENT (IN)";
2581 attr = "INTENT (OUT)";
2584 attr = "INTENT (IN OUT)";
2586 case DECL_INTRINSIC:
2592 case DECL_PARAMETER:
2598 case DECL_PROTECTED:
2613 case DECL_IS_BIND_C:
2623 attr = NULL; /* This shouldn't happen. */
2626 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2631 /* Now that we've dealt with duplicate attributes, add the attributes
2632 to the current attribute. */
2633 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2638 if (gfc_current_state () == COMP_DERIVED
2639 && d != DECL_DIMENSION && d != DECL_POINTER
2640 && d != DECL_COLON && d != DECL_PRIVATE
2641 && d != DECL_PUBLIC && d != DECL_NONE)
2643 if (d == DECL_ALLOCATABLE)
2645 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
2646 "attribute at %C in a TYPE definition")
2655 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2662 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2663 && gfc_current_state () != COMP_MODULE)
2665 if (d == DECL_PRIVATE)
2669 if (gfc_current_state () == COMP_DERIVED
2670 && gfc_state_stack->previous
2671 && gfc_state_stack->previous->state == COMP_MODULE)
2673 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
2674 "at %L in a TYPE definition", attr,
2684 gfc_error ("%s attribute at %L is not allowed outside of the "
2685 "specification part of a module", attr, &seen_at[d]);
2693 case DECL_ALLOCATABLE:
2694 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
2697 case DECL_DIMENSION:
2698 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
2702 t = gfc_add_external (¤t_attr, &seen_at[d]);
2706 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
2710 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
2714 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
2717 case DECL_INTRINSIC:
2718 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
2722 t = gfc_add_optional (¤t_attr, &seen_at[d]);
2725 case DECL_PARAMETER:
2726 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
2730 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
2733 case DECL_PROTECTED:
2734 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
2736 gfc_error ("PROTECTED at %C only allowed in specification "
2737 "part of a module");
2742 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
2747 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
2751 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
2756 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
2761 t = gfc_add_save (¤t_attr, NULL, &seen_at[d]);
2765 t = gfc_add_target (¤t_attr, &seen_at[d]);
2768 case DECL_IS_BIND_C:
2769 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
2773 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
2778 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
2782 if (gfc_notify_std (GFC_STD_F2003,
2783 "Fortran 2003: VOLATILE attribute at %C")
2787 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
2791 gfc_internal_error ("match_attr_spec(): Bad attribute");
2805 gfc_current_locus = start;
2806 gfc_free_array_spec (current_as);
2812 /* Set the binding label, dest_label, either with the binding label
2813 stored in the given gfc_typespec, ts, or if none was provided, it
2814 will be the symbol name in all lower case, as required by the draft
2815 (J3/04-007, section 15.4.1). If a binding label was given and
2816 there is more than one argument (num_idents), it is an error. */
2819 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
2821 if (curr_binding_label[0] != '\0')
2823 if (num_idents > 1 || num_idents_on_line > 1)
2825 gfc_error ("Multiple identifiers provided with "
2826 "single NAME= specifier at %C");
2830 /* Binding label given; store in temp holder til have sym. */
2831 strncpy (dest_label, curr_binding_label,
2832 strlen (curr_binding_label) + 1);
2836 /* No binding label given, and the NAME= specifier did not exist,
2837 which means there was no NAME="". */
2838 if (sym_name != NULL && has_name_equals == 0)
2839 strncpy (dest_label, sym_name, strlen (sym_name) + 1);
2846 /* Set the status of the given common block as being BIND(C) or not,
2847 depending on the given parameter, is_bind_c. */
2850 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
2852 com_block->is_bind_c = is_bind_c;
2857 /* Verify that the given gfc_typespec is for a C interoperable type. */
2860 verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
2864 /* Make sure the kind used is appropriate for the type.
2865 The f90_type is unknown if an integer constant was
2866 used (e.g., real(4), bind(c) :: myFloat). */
2867 if (ts->f90_type != BT_UNKNOWN)
2869 t = gfc_validate_c_kind (ts);
2872 /* Print an error, but continue parsing line. */
2873 gfc_error_now ("C kind parameter is for type %s but "
2874 "symbol '%s' at %L is of type %s",
2875 gfc_basic_typename (ts->f90_type),
2877 gfc_basic_typename (ts->type));
2881 /* Make sure the kind is C interoperable. This does not care about the
2882 possible error above. */
2883 if (ts->type == BT_DERIVED && ts->derived != NULL)
2884 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
2885 else if (ts->is_c_interop != 1)
2892 /* Verify that the variables of a given common block, which has been
2893 defined with the attribute specifier bind(c), to be of a C
2894 interoperable type. Errors will be reported here, if
2898 verify_com_block_vars_c_interop (gfc_common_head *com_block)
2900 gfc_symbol *curr_sym = NULL;
2901 try retval = SUCCESS;
2903 curr_sym = com_block->head;
2905 /* Make sure we have at least one symbol. */
2906 if (curr_sym == NULL)
2909 /* Here we know we have a symbol, so we'll execute this loop
2913 /* The second to last param, 1, says this is in a common block. */
2914 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
2915 curr_sym = curr_sym->common_next;
2916 } while (curr_sym != NULL);
2922 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
2923 an appropriate error message is reported. */
2926 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
2927 int is_in_common, gfc_common_head *com_block)
2929 try retval = SUCCESS;
2931 /* Here, we know we have the bind(c) attribute, so if we have
2932 enough type info, then verify that it's a C interop kind.
2933 The info could be in the symbol already, or possibly still in
2934 the given ts (current_ts), so look in both. */
2935 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
2937 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
2938 &(tmp_sym->declared_at)) != SUCCESS)
2940 /* See if we're dealing with a sym in a common block or not. */
2941 if (is_in_common == 1)
2943 gfc_warning ("Variable '%s' in common block '%s' at %L "
2944 "may not be a C interoperable "
2945 "kind though common block '%s' is BIND(C)",
2946 tmp_sym->name, com_block->name,
2947 &(tmp_sym->declared_at), com_block->name);
2951 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
2952 gfc_error ("Type declaration '%s' at %L is not C "
2953 "interoperable but it is BIND(C)",
2954 tmp_sym->name, &(tmp_sym->declared_at));
2956 gfc_warning ("Variable '%s' at %L "
2957 "may not be a C interoperable "
2958 "kind but it is bind(c)",
2959 tmp_sym->name, &(tmp_sym->declared_at));
2963 /* Variables declared w/in a common block can't be bind(c)
2964 since there's no way for C to see these variables, so there's
2965 semantically no reason for the attribute. */
2966 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
2968 gfc_error ("Variable '%s' in common block '%s' at "
2969 "%L cannot be declared with BIND(C) "
2970 "since it is not a global",
2971 tmp_sym->name, com_block->name,
2972 &(tmp_sym->declared_at));
2976 /* Scalar variables that are bind(c) can not have the pointer
2977 or allocatable attributes. */
2978 if (tmp_sym->attr.is_bind_c == 1)
2980 if (tmp_sym->attr.pointer == 1)
2982 gfc_error ("Variable '%s' at %L cannot have both the "
2983 "POINTER and BIND(C) attributes",
2984 tmp_sym->name, &(tmp_sym->declared_at));
2988 if (tmp_sym->attr.allocatable == 1)
2990 gfc_error ("Variable '%s' at %L cannot have both the "
2991 "ALLOCATABLE and BIND(C) attributes",
2992 tmp_sym->name, &(tmp_sym->declared_at));
2996 /* If it is a BIND(C) function, make sure the return value is a
2997 scalar value. The previous tests in this function made sure
2998 the type is interoperable. */
2999 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
3000 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3001 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3003 /* BIND(C) functions can not return a character string. */
3004 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
3005 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3006 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3007 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3008 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3009 "be a character string", tmp_sym->name,
3010 &(tmp_sym->declared_at));
3014 /* See if the symbol has been marked as private. If it has, make sure
3015 there is no binding label and warn the user if there is one. */
3016 if (tmp_sym->attr.access == ACCESS_PRIVATE
3017 && tmp_sym->binding_label[0] != '\0')
3018 /* Use gfc_warning_now because we won't say that the symbol fails
3019 just because of this. */
3020 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3021 "given the binding label '%s'", tmp_sym->name,
3022 &(tmp_sym->declared_at), tmp_sym->binding_label);
3028 /* Set the appropriate fields for a symbol that's been declared as
3029 BIND(C) (the is_bind_c flag and the binding label), and verify that
3030 the type is C interoperable. Errors are reported by the functions
3031 used to set/test these fields. */
3034 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3036 try retval = SUCCESS;
3038 /* TODO: Do we need to make sure the vars aren't marked private? */
3040 /* Set the is_bind_c bit in symbol_attribute. */
3041 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3043 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3044 num_idents) != SUCCESS)
3051 /* Set the fields marking the given common block as BIND(C), including
3052 a binding label, and report any errors encountered. */
3055 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3057 try retval = SUCCESS;
3059 /* destLabel, common name, typespec (which may have binding label). */
3060 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3064 /* Set the given common block (com_block) to being bind(c) (1). */
3065 set_com_block_bind_c (com_block, 1);
3071 /* Retrieve the list of one or more identifiers that the given bind(c)
3072 attribute applies to. */
3075 get_bind_c_idents (void)
3077 char name[GFC_MAX_SYMBOL_LEN + 1];
3079 gfc_symbol *tmp_sym = NULL;
3081 gfc_common_head *com_block = NULL;
3083 if (gfc_match_name (name) == MATCH_YES)
3085 found_id = MATCH_YES;
3086 gfc_get_ha_symbol (name, &tmp_sym);
3088 else if (match_common_name (name) == MATCH_YES)
3090 found_id = MATCH_YES;
3091 com_block = gfc_get_common (name, 0);
3095 gfc_error ("Need either entity or common block name for "
3096 "attribute specification statement at %C");
3100 /* Save the current identifier and look for more. */
3103 /* Increment the number of identifiers found for this spec stmt. */
3106 /* Make sure we have a sym or com block, and verify that it can
3107 be bind(c). Set the appropriate field(s) and look for more
3109 if (tmp_sym != NULL || com_block != NULL)
3111 if (tmp_sym != NULL)
3113 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3119 if (set_verify_bind_c_com_block(com_block, num_idents)
3124 /* Look to see if we have another identifier. */
3126 if (gfc_match_eos () == MATCH_YES)
3127 found_id = MATCH_NO;
3128 else if (gfc_match_char (',') != MATCH_YES)
3129 found_id = MATCH_NO;
3130 else if (gfc_match_name (name) == MATCH_YES)
3132 found_id = MATCH_YES;
3133 gfc_get_ha_symbol (name, &tmp_sym);
3135 else if (match_common_name (name) == MATCH_YES)
3137 found_id = MATCH_YES;
3138 com_block = gfc_get_common (name, 0);
3142 gfc_error ("Missing entity or common block name for "
3143 "attribute specification statement at %C");
3149 gfc_internal_error ("Missing symbol");
3151 } while (found_id == MATCH_YES);
3153 /* if we get here we were successful */
3158 /* Try and match a BIND(C) attribute specification statement. */
3161 gfc_match_bind_c_stmt (void)
3163 match found_match = MATCH_NO;
3168 /* This may not be necessary. */
3170 /* Clear the temporary binding label holder. */
3171 curr_binding_label[0] = '\0';
3173 /* Look for the bind(c). */
3174 found_match = gfc_match_bind_c (NULL);
3176 if (found_match == MATCH_YES)
3178 /* Look for the :: now, but it is not required. */
3181 /* Get the identifier(s) that needs to be updated. This may need to
3182 change to hand the flag(s) for the attr specified so all identifiers
3183 found can have all appropriate parts updated (assuming that the same
3184 spec stmt can have multiple attrs, such as both bind(c) and
3186 if (get_bind_c_idents () != SUCCESS)
3187 /* Error message should have printed already. */
3195 /* Match a data declaration statement. */
3198 gfc_match_data_decl (void)
3204 num_idents_on_line = 0;
3206 m = match_type_spec (¤t_ts, 0);
3210 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3212 sym = gfc_use_derived (current_ts.derived);
3220 current_ts.derived = sym;
3223 m = match_attr_spec ();
3224 if (m == MATCH_ERROR)
3230 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
3233 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3236 gfc_find_symbol (current_ts.derived->name,
3237 current_ts.derived->ns->parent, 1, &sym);
3239 /* Any symbol that we find had better be a type definition
3240 which has its components defined. */
3241 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3242 && current_ts.derived->components != NULL)
3245 /* Now we have an error, which we signal, and then fix up
3246 because the knock-on is plain and simple confusing. */
3247 gfc_error_now ("Derived type at %C has not been previously defined "
3248 "and so cannot appear in a derived type definition");
3249 current_attr.pointer = 1;
3254 /* If we have an old-style character declaration, and no new-style
3255 attribute specifications, then there a comma is optional between
3256 the type specification and the variable list. */
3257 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3258 gfc_match_char (',');
3260 /* Give the types/attributes to symbols that follow. Give the element
3261 a number so that repeat character length expressions can be copied. */
3265 num_idents_on_line++;
3266 m = variable_decl (elem++);
3267 if (m == MATCH_ERROR)
3272 if (gfc_match_eos () == MATCH_YES)
3274 if (gfc_match_char (',') != MATCH_YES)
3278 if (gfc_error_flag_test () == 0)
3279 gfc_error ("Syntax error in data declaration at %C");
3282 gfc_free_data_all (gfc_current_ns);
3285 gfc_free_array_spec (current_as);
3291 /* Match a prefix associated with a function or subroutine
3292 declaration. If the typespec pointer is nonnull, then a typespec
3293 can be matched. Note that if nothing matches, MATCH_YES is
3294 returned (the null string was matched). */
3297 match_prefix (gfc_typespec *ts)
3301 gfc_clear_attr (¤t_attr);
3305 if (!seen_type && ts != NULL
3306 && match_type_spec (ts, 0) == MATCH_YES
3307 && gfc_match_space () == MATCH_YES)
3314 if (gfc_match ("elemental% ") == MATCH_YES)
3316 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
3322 if (gfc_match ("pure% ") == MATCH_YES)
3324 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
3330 if (gfc_match ("recursive% ") == MATCH_YES)
3332 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
3338 /* At this point, the next item is not a prefix. */
3343 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
3346 copy_prefix (symbol_attribute *dest, locus *where)
3348 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3351 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3354 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3361 /* Match a formal argument list. */
3364 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3366 gfc_formal_arglist *head, *tail, *p, *q;
3367 char name[GFC_MAX_SYMBOL_LEN + 1];
3373 if (gfc_match_char ('(') != MATCH_YES)
3380 if (gfc_match_char (')') == MATCH_YES)
3385 if (gfc_match_char ('*') == MATCH_YES)
3389 m = gfc_match_name (name);
3393 if (gfc_get_symbol (name, NULL, &sym))
3397 p = gfc_get_formal_arglist ();
3409 /* We don't add the VARIABLE flavor because the name could be a
3410 dummy procedure. We don't apply these attributes to formal
3411 arguments of statement functions. */
3412 if (sym != NULL && !st_flag
3413 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3414 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3420 /* The name of a program unit can be in a different namespace,
3421 so check for it explicitly. After the statement is accepted,
3422 the name is checked for especially in gfc_get_symbol(). */
3423 if (gfc_new_block != NULL && sym != NULL
3424 && strcmp (sym->name, gfc_new_block->name) == 0)
3426 gfc_error ("Name '%s' at %C is the name of the procedure",
3432 if (gfc_match_char (')') == MATCH_YES)
3435 m = gfc_match_char (',');
3438 gfc_error ("Unexpected junk in formal argument list at %C");
3444 /* Check for duplicate symbols in the formal argument list. */
3447 for (p = head; p->next; p = p->next)
3452 for (q = p->next; q; q = q->next)
3453 if (p->sym == q->sym)
3455 gfc_error ("Duplicate symbol '%s' in formal argument list "
3456 "at %C", p->sym->name);
3464 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3474 gfc_free_formal_arglist (head);
3479 /* Match a RESULT specification following a function declaration or
3480 ENTRY statement. Also matches the end-of-statement. */
3483 match_result (gfc_symbol *function, gfc_symbol **result)
3485 char name[GFC_MAX_SYMBOL_LEN + 1];
3489 if (gfc_match (" result (") != MATCH_YES)
3492 m = gfc_match_name (name);
3496 /* Get the right paren, and that's it because there could be the
3497 bind(c) attribute after the result clause. */
3498 if (gfc_match_char(')') != MATCH_YES)
3500 /* TODO: should report the missing right paren here. */
3504 if (strcmp (function->name, name) == 0)
3506 gfc_error ("RESULT variable at %C must be different than function name");
3510 if (gfc_get_symbol (name, NULL, &r))
3513 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3514 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
3523 /* Match a function suffix, which could be a combination of a result
3524 clause and BIND(C), either one, or neither. The draft does not
3525 require them to come in a specific order. */
3528 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3530 match is_bind_c; /* Found bind(c). */
3531 match is_result; /* Found result clause. */
3532 match found_match; /* Status of whether we've found a good match. */
3533 int peek_char; /* Character we're going to peek at. */
3535 /* Initialize to having found nothing. */
3536 found_match = MATCH_NO;
3537 is_bind_c = MATCH_NO;
3538 is_result = MATCH_NO;
3540 /* Get the next char to narrow between result and bind(c). */
3541 gfc_gobble_whitespace ();
3542 peek_char = gfc_peek_char ();
3547 /* Look for result clause. */
3548 is_result = match_result (sym, result);
3549 if (is_result == MATCH_YES)
3551 /* Now see if there is a bind(c) after it. */
3552 is_bind_c = gfc_match_bind_c (sym);
3553 /* We've found the result clause and possibly bind(c). */
3554 found_match = MATCH_YES;
3557 /* This should only be MATCH_ERROR. */
3558 found_match = is_result;
3561 /* Look for bind(c) first. */
3562 is_bind_c = gfc_match_bind_c (sym);
3563 if (is_bind_c == MATCH_YES)
3565 /* Now see if a result clause followed it. */
3566 is_result = match_result (sym, result);
3567 found_match = MATCH_YES;
3571 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3572 found_match = MATCH_ERROR;
3576 gfc_error ("Unexpected junk after function declaration at %C");
3577 found_match = MATCH_ERROR;
3581 if (is_result == MATCH_ERROR || is_bind_c == MATCH_ERROR)
3583 gfc_error ("Error in function suffix at %C");
3587 if (is_bind_c == MATCH_YES)
3588 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
3596 /* Match a function declaration. */
3599 gfc_match_function_decl (void)
3601 char name[GFC_MAX_SYMBOL_LEN + 1];
3602 gfc_symbol *sym, *result;
3606 match found_match; /* Status returned by match func. */
3608 if (gfc_current_state () != COMP_NONE
3609 && gfc_current_state () != COMP_INTERFACE
3610 && gfc_current_state () != COMP_CONTAINS)
3613 gfc_clear_ts (¤t_ts);
3615 old_loc = gfc_current_locus;
3617 m = match_prefix (¤t_ts);
3620 gfc_current_locus = old_loc;
3624 if (gfc_match ("function% %n", name) != MATCH_YES)
3626 gfc_current_locus = old_loc;
3629 if (get_proc_name (name, &sym, false))
3631 gfc_new_block = sym;
3633 m = gfc_match_formal_arglist (sym, 0, 0);
3636 gfc_error ("Expected formal argument list in function "
3637 "definition at %C");
3641 else if (m == MATCH_ERROR)
3646 /* According to the draft, the bind(c) and result clause can
3647 come in either order after the formal_arg_list (i.e., either
3648 can be first, both can exist together or by themselves or neither
3649 one). Therefore, the match_result can't match the end of the
3650 string, and check for the bind(c) or result clause in either order. */
3651 found_match = gfc_match_eos ();
3653 /* Make sure that it isn't already declared as BIND(C). If it is, it
3654 must have been marked BIND(C) with a BIND(C) attribute and that is
3655 not allowed for procedures. */
3656 if (sym->attr.is_bind_c == 1)
3658 sym->attr.is_bind_c = 0;
3659 if (sym->old_symbol != NULL)
3660 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3661 "variables or common blocks",
3662 &(sym->old_symbol->declared_at));
3664 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3665 "variables or common blocks", &gfc_current_locus);
3668 if (found_match != MATCH_YES)
3670 /* If we haven't found the end-of-statement, look for a suffix. */
3671 suffix_match = gfc_match_suffix (sym, &result);
3672 if (suffix_match == MATCH_YES)
3673 /* Need to get the eos now. */
3674 found_match = gfc_match_eos ();
3676 found_match = suffix_match;
3679 if(found_match != MATCH_YES)
3683 /* Make changes to the symbol. */
3686 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
3689 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
3690 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
3693 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
3694 && !sym->attr.implicit_type)
3696 gfc_error ("Function '%s' at %C already has a type of %s", name,
3697 gfc_basic_typename (sym->ts.type));
3703 sym->ts = current_ts;
3708 result->ts = current_ts;
3709 sym->result = result;
3716 gfc_current_locus = old_loc;
3721 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
3722 pass the name of the entry, rather than the gfc_current_block name, and
3723 to return false upon finding an existing global entry. */
3726 add_global_entry (const char *name, int sub)
3730 s = gfc_get_gsymbol(name);
3733 || (s->type != GSYM_UNKNOWN
3734 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
3735 global_used(s, NULL);
3738 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
3739 s->where = gfc_current_locus;
3747 /* Match an ENTRY statement. */
3750 gfc_match_entry (void)
3755 char name[GFC_MAX_SYMBOL_LEN + 1];
3756 gfc_compile_state state;
3760 bool module_procedure;
3762 m = gfc_match_name (name);
3766 state = gfc_current_state ();
3767 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
3772 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
3775 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
3777 case COMP_BLOCK_DATA:
3778 gfc_error ("ENTRY statement at %C cannot appear within "
3781 case COMP_INTERFACE:
3782 gfc_error ("ENTRY statement at %C cannot appear within "
3786 gfc_error ("ENTRY statement at %C cannot appear within "
3787 "a DERIVED TYPE block");
3790 gfc_error ("ENTRY statement at %C cannot appear within "
3791 "an IF-THEN block");
3794 gfc_error ("ENTRY statement at %C cannot appear within "
3798 gfc_error ("ENTRY statement at %C cannot appear within "
3802 gfc_error ("ENTRY statement at %C cannot appear within "
3806 gfc_error ("ENTRY statement at %C cannot appear within "
3810 gfc_error ("ENTRY statement at %C cannot appear within "
3811 "a contained subprogram");
3814 gfc_internal_error ("gfc_match_entry(): Bad state");
3819 module_procedure = gfc_current_ns->parent != NULL
3820 && gfc_current_ns->parent->proc_name
3821 && gfc_current_ns->parent->proc_name->attr.flavor
3824 if (gfc_current_ns->parent != NULL
3825 && gfc_current_ns->parent->proc_name
3826 && !module_procedure)
3828 gfc_error("ENTRY statement at %C cannot appear in a "
3829 "contained procedure");
3833 /* Module function entries need special care in get_proc_name
3834 because previous references within the function will have
3835 created symbols attached to the current namespace. */
3836 if (get_proc_name (name, &entry,
3837 gfc_current_ns->parent != NULL
3839 && gfc_current_ns->proc_name->attr.function))
3842 proc = gfc_current_block ();
3844 if (state == COMP_SUBROUTINE)
3846 /* An entry in a subroutine. */
3847 if (!add_global_entry (name, 1))
3850 m = gfc_match_formal_arglist (entry, 0, 1);
3854 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3855 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
3860 /* An entry in a function.
3861 We need to take special care because writing
3866 ENTRY f() RESULT (r)
3868 ENTRY f RESULT (r). */
3869 if (!add_global_entry (name, 0))
3872 old_loc = gfc_current_locus;
3873 if (gfc_match_eos () == MATCH_YES)
3875 gfc_current_locus = old_loc;
3876 /* Match the empty argument list, and add the interface to
3878 m = gfc_match_formal_arglist (entry, 0, 1);
3881 m = gfc_match_formal_arglist (entry, 0, 0);
3888 if (gfc_match_eos () == MATCH_YES)
3890 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3891 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
3894 entry->result = entry;
3898 m = match_result (proc, &result);
3900 gfc_syntax_error (ST_ENTRY);
3904 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
3905 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
3906 || gfc_add_function (&entry->attr, result->name, NULL)
3910 entry->result = result;
3914 if (gfc_match_eos () != MATCH_YES)
3916 gfc_syntax_error (ST_ENTRY);
3920 entry->attr.recursive = proc->attr.recursive;
3921 entry->attr.elemental = proc->attr.elemental;
3922 entry->attr.pure = proc->attr.pure;
3924 el = gfc_get_entry_list ();
3926 el->next = gfc_current_ns->entries;
3927 gfc_current_ns->entries = el;
3929 el->id = el->next->id + 1;
3933 new_st.op = EXEC_ENTRY;
3934 new_st.ext.entry = el;
3940 /* Match a subroutine statement, including optional prefixes. */
3943 gfc_match_subroutine (void)
3945 char name[GFC_MAX_SYMBOL_LEN + 1];
3951 if (gfc_current_state () != COMP_NONE
3952 && gfc_current_state () != COMP_INTERFACE
3953 && gfc_current_state () != COMP_CONTAINS)
3956 m = match_prefix (NULL);
3960 m = gfc_match ("subroutine% %n", name);
3964 if (get_proc_name (name, &sym, false))
3966 gfc_new_block = sym;
3968 /* Check what next non-whitespace character is so we can tell if there
3969 where the required parens if we have a BIND(C). */
3970 gfc_gobble_whitespace ();
3971 peek_char = gfc_peek_char ();
3973 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
3976 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
3979 /* Make sure that it isn't already declared as BIND(C). If it is, it
3980 must have been marked BIND(C) with a BIND(C) attribute and that is
3981 not allowed for procedures. */
3982 if (sym->attr.is_bind_c == 1)
3984 sym->attr.is_bind_c = 0;
3985 if (sym->old_symbol != NULL)
3986 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3987 "variables or common blocks",
3988 &(sym->old_symbol->declared_at));
3990 gfc_error_now ("BIND(C) attribute at %L can only be used for "
3991 "variables or common blocks", &gfc_current_locus);
3994 /* Here, we are just checking if it has the bind(c) attribute, and if
3995 so, then we need to make sure it's all correct. If it doesn't,
3996 we still need to continue matching the rest of the subroutine line. */
3997 is_bind_c = gfc_match_bind_c (sym);
3998 if (is_bind_c == MATCH_ERROR)
4000 /* There was an attempt at the bind(c), but it was wrong. An
4001 error message should have been printed w/in the gfc_match_bind_c
4002 so here we'll just return the MATCH_ERROR. */
4006 if (is_bind_c == MATCH_YES)
4008 if (peek_char != '(')
4010 gfc_error ("Missing required parentheses before BIND(C) at %C");
4013 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4018 if (gfc_match_eos () != MATCH_YES)
4020 gfc_syntax_error (ST_SUBROUTINE);
4024 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4031 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4032 given, and set the binding label in either the given symbol (if not
4033 NULL), or in the current_ts. The symbol may be NULL because we may
4034 encounter the BIND(C) before the declaration itself. Return
4035 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4036 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4037 or MATCH_YES if the specifier was correct and the binding label and
4038 bind(c) fields were set correctly for the given symbol or the
4042 gfc_match_bind_c (gfc_symbol *sym)
4044 /* binding label, if exists */
4045 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4048 int has_name_equals = 0;
4050 /* Initialize the flag that specifies whether we encountered a NAME=
4051 specifier or not. */
4052 has_name_equals = 0;
4054 /* Init the first char to nil so we can catch if we don't have
4055 the label (name attr) or the symbol name yet. */
4056 binding_label[0] = '\0';
4058 /* This much we have to be able to match, in this order, if
4059 there is a bind(c) label. */
4060 if (gfc_match (" bind ( c ") != MATCH_YES)
4063 /* Now see if there is a binding label, or if we've reached the
4064 end of the bind(c) attribute without one. */
4065 if (gfc_match_char (',') == MATCH_YES)
4067 if (gfc_match (" name = ") != MATCH_YES)
4069 gfc_error ("Syntax error in NAME= specifier for binding label "
4071 /* should give an error message here */
4075 has_name_equals = 1;
4077 /* Get the opening quote. */
4078 double_quote = MATCH_YES;
4079 single_quote = MATCH_YES;
4080 double_quote = gfc_match_char ('"');
4081 if (double_quote != MATCH_YES)
4082 single_quote = gfc_match_char ('\'');
4083 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4085 gfc_error ("Syntax error in NAME= specifier for binding label "
4090 /* Grab the binding label, using functions that will not lower
4091 case the names automatically. */
4092 if (gfc_match_name_C (binding_label) != MATCH_YES)
4095 /* Get the closing quotation. */
4096 if (double_quote == MATCH_YES)
4098 if (gfc_match_char ('"') != MATCH_YES)
4100 gfc_error ("Missing closing quote '\"' for binding label at %C");
4101 /* User started string with '"' so looked to match it. */
4107 if (gfc_match_char ('\'') != MATCH_YES)
4109 gfc_error ("Missing closing quote '\'' for binding label at %C");
4110 /* User started string with "'" char. */
4116 /* Get the required right paren. */
4117 if (gfc_match_char (')') != MATCH_YES)
4119 gfc_error ("Missing closing paren for binding label at %C");
4123 /* Save the binding label to the symbol. If sym is null, we're
4124 probably matching the typespec attributes of a declaration and
4125 haven't gotten the name yet, and therefore, no symbol yet. */
4126 if (binding_label[0] != '\0')
4130 strncpy (sym->binding_label, binding_label,
4131 strlen (binding_label)+1);
4134 strncpy (curr_binding_label, binding_label,
4135 strlen (binding_label) + 1);
4139 /* No binding label, but if symbol isn't null, we
4140 can set the label for it here. */
4141 /* TODO: If the name= was given and no binding label (name=""), we simply
4142 will let fortran mangle the symbol name as it usually would.
4143 However, this could still let C call it if the user looked up the
4144 symbol in the object file. Should the name set during mangling in
4145 trans-decl.c be marked with characters that are invalid for C to
4147 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4148 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4155 /* Return nonzero if we're currently compiling a contained procedure. */
4158 contained_procedure (void)
4162 for (s=gfc_state_stack; s; s=s->previous)
4163 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
4164 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
4170 /* Set the kind of each enumerator. The kind is selected such that it is
4171 interoperable with the corresponding C enumeration type, making
4172 sure that -fshort-enums is honored. */
4177 enumerator_history *current_history = NULL;
4181 if (max_enum == NULL || enum_history == NULL)
4184 if (!gfc_option.fshort_enums)
4190 kind = gfc_integer_kinds[i++].kind;
4192 while (kind < gfc_c_int_kind
4193 && gfc_check_integer_range (max_enum->initializer->value.integer,
4196 current_history = enum_history;
4197 while (current_history != NULL)
4199 current_history->sym->ts.kind = kind;