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 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/>. */
28 /* This flag is set if an old-style length selector is matched
29 during a type-declaration statement. */
31 static int old_char_selector;
33 /* When variables acquire types and attributes from a declaration
34 statement, they get them from the following static variables. The
35 first part of a declaration sets these variables and the second
36 part copies these into symbol structures. */
38 static gfc_typespec current_ts;
40 static symbol_attribute current_attr;
41 static gfc_array_spec *current_as;
42 static int colon_seen;
44 /* The current binding label (if any). */
45 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
46 /* Need to know how many identifiers are on the current data declaration
47 line in case we're given the BIND(C) attribute with a NAME= specifier. */
48 static int num_idents_on_line;
49 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
50 can supply a name if the curr_binding_label is nil and NAME= was not. */
51 static int has_name_equals = 0;
53 /* Initializer of the previous enumerator. */
55 static gfc_expr *last_initializer;
57 /* History of all the enumerators is maintained, so that
58 kind values of all the enumerators could be updated depending
59 upon the maximum initialized value. */
61 typedef struct enumerator_history
64 gfc_expr *initializer;
65 struct enumerator_history *next;
69 /* Header of enum history chain. */
71 static enumerator_history *enum_history = NULL;
73 /* Pointer of enum history node containing largest initializer. */
75 static enumerator_history *max_enum = NULL;
77 /* gfc_new_block points to the symbol of a newly matched block. */
79 gfc_symbol *gfc_new_block;
82 /********************* DATA statement subroutines *********************/
84 static bool in_match_data = false;
87 gfc_in_match_data (void)
93 gfc_set_in_match_data (bool set_value)
95 in_match_data = set_value;
98 /* Free a gfc_data_variable structure and everything beneath it. */
101 free_variable (gfc_data_variable *p)
103 gfc_data_variable *q;
108 gfc_free_expr (p->expr);
109 gfc_free_iterator (&p->iter, 0);
110 free_variable (p->list);
116 /* Free a gfc_data_value structure and everything beneath it. */
119 free_value (gfc_data_value *p)
126 gfc_free_expr (p->expr);
132 /* Free a list of gfc_data structures. */
135 gfc_free_data (gfc_data *p)
142 free_variable (p->var);
143 free_value (p->value);
149 /* Free all data in a namespace. */
152 gfc_free_data_all (gfc_namespace *ns)
165 static match var_element (gfc_data_variable *);
167 /* Match a list of variables terminated by an iterator and a right
171 var_list (gfc_data_variable *parent)
173 gfc_data_variable *tail, var;
176 m = var_element (&var);
177 if (m == MATCH_ERROR)
182 tail = gfc_get_data_variable ();
189 if (gfc_match_char (',') != MATCH_YES)
192 m = gfc_match_iterator (&parent->iter, 1);
195 if (m == MATCH_ERROR)
198 m = var_element (&var);
199 if (m == MATCH_ERROR)
204 tail->next = gfc_get_data_variable ();
210 if (gfc_match_char (')') != MATCH_YES)
215 gfc_syntax_error (ST_DATA);
220 /* Match a single element in a data variable list, which can be a
221 variable-iterator list. */
224 var_element (gfc_data_variable *new)
229 memset (new, 0, sizeof (gfc_data_variable));
231 if (gfc_match_char ('(') == MATCH_YES)
232 return var_list (new);
234 m = gfc_match_variable (&new->expr, 0);
238 sym = new->expr->symtree->n.sym;
240 if (!sym->attr.function && gfc_current_ns->parent
241 && gfc_current_ns->parent == sym->ns)
243 gfc_error ("Host associated variable '%s' may not be in the DATA "
244 "statement at %C", sym->name);
248 if (gfc_current_state () != COMP_BLOCK_DATA
249 && sym->attr.in_common
250 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
251 "common block variable '%s' in DATA statement at %C",
252 sym->name) == FAILURE)
255 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
262 /* Match the top-level list of data variables. */
265 top_var_list (gfc_data *d)
267 gfc_data_variable var, *tail, *new;
274 m = var_element (&var);
277 if (m == MATCH_ERROR)
280 new = gfc_get_data_variable ();
290 if (gfc_match_char ('/') == MATCH_YES)
292 if (gfc_match_char (',') != MATCH_YES)
299 gfc_syntax_error (ST_DATA);
300 gfc_free_data_all (gfc_current_ns);
306 match_data_constant (gfc_expr **result)
308 char name[GFC_MAX_SYMBOL_LEN + 1];
314 m = gfc_match_literal_constant (&expr, 1);
321 if (m == MATCH_ERROR)
324 m = gfc_match_null (result);
328 old_loc = gfc_current_locus;
330 /* Should this be a structure component, try to match it
331 before matching a name. */
332 m = gfc_match_rvalue (result);
333 if (m == MATCH_ERROR)
336 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
338 if (gfc_simplify_expr (*result, 0) == FAILURE)
343 gfc_current_locus = old_loc;
345 m = gfc_match_name (name);
349 if (gfc_find_symbol (name, NULL, 1, &sym))
353 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
355 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
359 else if (sym->attr.flavor == FL_DERIVED)
360 return gfc_match_structure_constructor (sym, result);
362 *result = gfc_copy_expr (sym->value);
367 /* Match a list of values in a DATA statement. The leading '/' has
368 already been seen at this point. */
371 top_val_list (gfc_data *data)
373 gfc_data_value *new, *tail;
382 m = match_data_constant (&expr);
385 if (m == MATCH_ERROR)
388 new = gfc_get_data_value ();
397 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
405 msg = gfc_extract_int (expr, &tmp);
406 gfc_free_expr (expr);
414 m = match_data_constant (&tail->expr);
417 if (m == MATCH_ERROR)
421 if (gfc_match_char ('/') == MATCH_YES)
423 if (gfc_match_char (',') == MATCH_NO)
430 gfc_syntax_error (ST_DATA);
431 gfc_free_data_all (gfc_current_ns);
436 /* Matches an old style initialization. */
439 match_old_style_init (const char *name)
446 /* Set up data structure to hold initializers. */
447 gfc_find_sym_tree (name, NULL, 0, &st);
450 newdata = gfc_get_data ();
451 newdata->var = gfc_get_data_variable ();
452 newdata->var->expr = gfc_get_variable_expr (st);
453 newdata->where = gfc_current_locus;
455 /* Match initial value list. This also eats the terminal '/'. */
456 m = top_val_list (newdata);
465 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
470 /* Mark the variable as having appeared in a data statement. */
471 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
477 /* Chain in namespace list of DATA initializers. */
478 newdata->next = gfc_current_ns->data;
479 gfc_current_ns->data = newdata;
485 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
486 we are matching a DATA statement and are therefore issuing an error
487 if we encounter something unexpected, if not, we're trying to match
488 an old-style initialization expression of the form INTEGER I /2/. */
491 gfc_match_data (void)
496 gfc_set_in_match_data (true);
500 new = gfc_get_data ();
501 new->where = gfc_current_locus;
503 m = top_var_list (new);
507 m = top_val_list (new);
511 new->next = gfc_current_ns->data;
512 gfc_current_ns->data = new;
514 if (gfc_match_eos () == MATCH_YES)
517 gfc_match_char (','); /* Optional comma */
520 gfc_set_in_match_data (false);
524 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
531 gfc_set_in_match_data (false);
537 /************************ Declaration statements *********************/
539 /* Match an intent specification. Since this can only happen after an
540 INTENT word, a legal intent-spec must follow. */
543 match_intent_spec (void)
546 if (gfc_match (" ( in out )") == MATCH_YES)
548 if (gfc_match (" ( in )") == MATCH_YES)
550 if (gfc_match (" ( out )") == MATCH_YES)
553 gfc_error ("Bad INTENT specification at %C");
554 return INTENT_UNKNOWN;
558 /* Matches a character length specification, which is either a
559 specification expression or a '*'. */
562 char_len_param_value (gfc_expr **expr)
564 if (gfc_match_char ('*') == MATCH_YES)
570 return gfc_match_expr (expr);
574 /* A character length is a '*' followed by a literal integer or a
575 char_len_param_value in parenthesis. */
578 match_char_length (gfc_expr **expr)
583 m = gfc_match_char ('*');
587 m = gfc_match_small_literal_int (&length, NULL);
588 if (m == MATCH_ERROR)
593 *expr = gfc_int_expr (length);
597 if (gfc_match_char ('(') == MATCH_NO)
600 m = char_len_param_value (expr);
601 if (m == MATCH_ERROR)
606 if (gfc_match_char (')') == MATCH_NO)
608 gfc_free_expr (*expr);
616 gfc_error ("Syntax error in character length specification at %C");
621 /* Special subroutine for finding a symbol. Check if the name is found
622 in the current name space. If not, and we're compiling a function or
623 subroutine and the parent compilation unit is an interface, then check
624 to see if the name we've been given is the name of the interface
625 (located in another namespace). */
628 find_special (const char *name, gfc_symbol **result)
633 i = gfc_get_symbol (name, NULL, result);
637 if (gfc_current_state () != COMP_SUBROUTINE
638 && gfc_current_state () != COMP_FUNCTION)
641 s = gfc_state_stack->previous;
645 if (s->state != COMP_INTERFACE)
648 goto end; /* Nameless interface. */
650 if (strcmp (name, s->sym->name) == 0)
661 /* Special subroutine for getting a symbol node associated with a
662 procedure name, used in SUBROUTINE and FUNCTION statements. The
663 symbol is created in the parent using with symtree node in the
664 child unit pointing to the symbol. If the current namespace has no
665 parent, then the symbol is just created in the current unit. */
668 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
674 /* Module functions have to be left in their own namespace because
675 they have potentially (almost certainly!) already been referenced.
676 In this sense, they are rather like external functions. This is
677 fixed up in resolve.c(resolve_entries), where the symbol name-
678 space is set to point to the master function, so that the fake
679 result mechanism can work. */
680 if (module_fcn_entry)
682 /* Present if entry is declared to be a module procedure. */
683 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
686 rc = gfc_get_symbol (name, NULL, result);
687 else if (gfc_get_symbol (name, NULL, &sym) == 0
689 && sym->ts.type != BT_UNKNOWN
690 && (*result)->ts.type == BT_UNKNOWN
691 && sym->attr.flavor == FL_UNKNOWN)
692 /* Pick up the typespec for the entry, if declared in the function
693 body. Note that this symbol is FL_UNKNOWN because it will
694 only have appeared in a type declaration. The local symtree
695 is set to point to the module symbol and a unique symtree
696 to the local version. This latter ensures a correct clearing
699 (*result)->ts = sym->ts;
700 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
702 st = gfc_get_unique_symtree (gfc_current_ns);
707 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
710 gfc_current_ns->refs++;
712 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
714 /* Trap another encompassed procedure with the same name. All
715 these conditions are necessary to avoid picking up an entry
716 whose name clashes with that of the encompassing procedure;
717 this is handled using gsymbols to register unique,globally
719 if (sym->attr.flavor != 0
720 && sym->attr.proc != 0
721 && (sym->attr.subroutine || sym->attr.function)
722 && sym->attr.if_source != IFSRC_UNKNOWN)
723 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
724 name, &sym->declared_at);
726 /* Trap a procedure with a name the same as interface in the
727 encompassing scope. */
728 if (sym->attr.generic != 0
729 && (sym->attr.subroutine || sym->attr.function)
730 && !sym->attr.mod_proc)
731 gfc_error_now ("Name '%s' at %C is already defined"
732 " as a generic interface at %L",
733 name, &sym->declared_at);
735 /* Trap declarations of attributes in encompassing scope. The
736 signature for this is that ts.kind is set. Legitimate
737 references only set ts.type. */
738 if (sym->ts.kind != 0
739 && !sym->attr.implicit_type
740 && sym->attr.proc == 0
741 && gfc_current_ns->parent != NULL
742 && sym->attr.access == 0
743 && !module_fcn_entry)
744 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
745 "and must not have attributes declared at %L",
746 name, &sym->declared_at);
749 if (gfc_current_ns->parent == NULL || *result == NULL)
752 /* Module function entries will already have a symtree in
753 the current namespace but will need one at module level. */
754 if (module_fcn_entry)
756 /* Present if entry is declared to be a module procedure. */
757 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
759 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
762 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
767 /* See if the procedure should be a module procedure. */
769 if (((sym->ns->proc_name != NULL
770 && sym->ns->proc_name->attr.flavor == FL_MODULE
771 && sym->attr.proc != PROC_MODULE)
772 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
773 && gfc_add_procedure (&sym->attr, PROC_MODULE,
774 sym->name, NULL) == FAILURE)
781 /* Verify that the given symbol representing a parameter is C
782 interoperable, by checking to see if it was marked as such after
783 its declaration. If the given symbol is not interoperable, a
784 warning is reported, thus removing the need to return the status to
785 the calling function. The standard does not require the user use
786 one of the iso_c_binding named constants to declare an
787 interoperable parameter, but we can't be sure if the param is C
788 interop or not if the user doesn't. For example, integer(4) may be
789 legal Fortran, but doesn't have meaning in C. It may interop with
790 a number of the C types, which causes a problem because the
791 compiler can't know which one. This code is almost certainly not
792 portable, and the user will get what they deserve if the C type
793 across platforms isn't always interoperable with integer(4). If
794 the user had used something like integer(c_int) or integer(c_long),
795 the compiler could have automatically handled the varying sizes
799 verify_c_interop_param (gfc_symbol *sym)
801 int is_c_interop = 0;
802 try retval = SUCCESS;
804 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
805 Don't repeat the checks here. */
806 if (sym->attr.implicit_type)
809 /* For subroutines or functions that are passed to a BIND(C) procedure,
810 they're interoperable if they're BIND(C) and their params are all
812 if (sym->attr.flavor == FL_PROCEDURE)
814 if (sym->attr.is_bind_c == 0)
816 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
817 "attribute to be C interoperable", sym->name,
818 &(sym->declared_at));
824 if (sym->attr.is_c_interop == 1)
825 /* We've already checked this procedure; don't check it again. */
828 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
833 /* See if we've stored a reference to a procedure that owns sym. */
834 if (sym->ns != NULL && sym->ns->proc_name != NULL)
836 if (sym->ns->proc_name->attr.is_bind_c == 1)
839 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
842 if (is_c_interop != 1)
844 /* Make personalized messages to give better feedback. */
845 if (sym->ts.type == BT_DERIVED)
846 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
847 " procedure '%s' but is not C interoperable "
848 "because derived type '%s' is not C interoperable",
849 sym->name, &(sym->declared_at),
850 sym->ns->proc_name->name,
851 sym->ts.derived->name);
853 gfc_warning ("Variable '%s' at %L is a parameter to the "
854 "BIND(C) procedure '%s' but may not be C "
856 sym->name, &(sym->declared_at),
857 sym->ns->proc_name->name);
860 /* Character strings are only C interoperable if they have a
862 if (sym->ts.type == BT_CHARACTER)
864 gfc_charlen *cl = sym->ts.cl;
865 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
866 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
868 gfc_error ("Character argument '%s' at %L "
869 "must be length 1 because "
870 "procedure '%s' is BIND(C)",
871 sym->name, &sym->declared_at,
872 sym->ns->proc_name->name);
877 /* We have to make sure that any param to a bind(c) routine does
878 not have the allocatable, pointer, or optional attributes,
879 according to J3/04-007, section 5.1. */
880 if (sym->attr.allocatable == 1)
882 gfc_error ("Variable '%s' at %L cannot have the "
883 "ALLOCATABLE attribute because procedure '%s'"
884 " is BIND(C)", sym->name, &(sym->declared_at),
885 sym->ns->proc_name->name);
889 if (sym->attr.pointer == 1)
891 gfc_error ("Variable '%s' at %L cannot have the "
892 "POINTER attribute because procedure '%s'"
893 " is BIND(C)", sym->name, &(sym->declared_at),
894 sym->ns->proc_name->name);
898 if (sym->attr.optional == 1)
900 gfc_error ("Variable '%s' at %L cannot have the "
901 "OPTIONAL attribute because procedure '%s'"
902 " is BIND(C)", sym->name, &(sym->declared_at),
903 sym->ns->proc_name->name);
907 /* Make sure that if it has the dimension attribute, that it is
908 either assumed size or explicit shape. */
911 if (sym->as->type == AS_ASSUMED_SHAPE)
913 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
914 "argument to the procedure '%s' at %L because "
915 "the procedure is BIND(C)", sym->name,
916 &(sym->declared_at), sym->ns->proc_name->name,
917 &(sym->ns->proc_name->declared_at));
921 if (sym->as->type == AS_DEFERRED)
923 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
924 "argument to the procedure '%s' at %L because "
925 "the procedure is BIND(C)", sym->name,
926 &(sym->declared_at), sym->ns->proc_name->name,
927 &(sym->ns->proc_name->declared_at));
938 /* Function called by variable_decl() that adds a name to the symbol table. */
941 build_sym (const char *name, gfc_charlen *cl,
942 gfc_array_spec **as, locus *var_locus)
944 symbol_attribute attr;
947 if (gfc_get_symbol (name, NULL, &sym))
950 /* Start updating the symbol table. Add basic type attribute if present. */
951 if (current_ts.type != BT_UNKNOWN
952 && (sym->attr.implicit_type == 0
953 || !gfc_compare_types (&sym->ts, ¤t_ts))
954 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
957 if (sym->ts.type == BT_CHARACTER)
960 /* Add dimension attribute if present. */
961 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
965 /* Add attribute to symbol. The copy is so that we can reset the
966 dimension attribute. */
970 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
973 /* Finish any work that may need to be done for the binding label,
974 if it's a bind(c). The bind(c) attr is found before the symbol
975 is made, and before the symbol name (for data decls), so the
976 current_ts is holding the binding label, or nothing if the
977 name= attr wasn't given. Therefore, test here if we're dealing
978 with a bind(c) and make sure the binding label is set correctly. */
979 if (sym->attr.is_bind_c == 1)
981 if (sym->binding_label[0] == '\0')
983 /* Set the binding label and verify that if a NAME= was specified
984 then only one identifier was in the entity-decl-list. */
985 if (set_binding_label (sym->binding_label, sym->name,
986 num_idents_on_line) == FAILURE)
991 /* See if we know we're in a common block, and if it's a bind(c)
992 common then we need to make sure we're an interoperable type. */
993 if (sym->attr.in_common == 1)
995 /* Test the common block object. */
996 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
997 && sym->ts.is_c_interop != 1)
999 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1000 "must be declared with a C interoperable "
1001 "kind since common block '%s' is BIND(C)",
1002 sym->name, sym->common_block->name,
1003 sym->common_block->name);
1008 sym->attr.implied_index = 0;
1014 /* Set character constant to the given length. The constant will be padded or
1018 gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
1023 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1024 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
1026 slen = expr->value.character.length;
1029 s = gfc_getmem (len + 1);
1030 memcpy (s, expr->value.character.string, MIN (len, slen));
1032 memset (&s[slen], ' ', len - slen);
1034 if (gfc_option.warn_character_truncation && slen > len)
1035 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1036 "(%d/%d)", &expr->where, slen, len);
1038 /* Apply the standard by 'hand' otherwise it gets cleared for
1040 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
1041 gfc_error_now ("The CHARACTER elements of the array constructor "
1042 "at %L must have the same length (%d/%d)",
1043 &expr->where, slen, len);
1046 gfc_free (expr->value.character.string);
1047 expr->value.character.string = s;
1048 expr->value.character.length = len;
1053 /* Function to create and update the enumerator history
1054 using the information passed as arguments.
1055 Pointer "max_enum" is also updated, to point to
1056 enum history node containing largest initializer.
1058 SYM points to the symbol node of enumerator.
1059 INIT points to its enumerator value. */
1062 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1064 enumerator_history *new_enum_history;
1065 gcc_assert (sym != NULL && init != NULL);
1067 new_enum_history = gfc_getmem (sizeof (enumerator_history));
1069 new_enum_history->sym = sym;
1070 new_enum_history->initializer = init;
1071 new_enum_history->next = NULL;
1073 if (enum_history == NULL)
1075 enum_history = new_enum_history;
1076 max_enum = enum_history;
1080 new_enum_history->next = enum_history;
1081 enum_history = new_enum_history;
1083 if (mpz_cmp (max_enum->initializer->value.integer,
1084 new_enum_history->initializer->value.integer) < 0)
1085 max_enum = new_enum_history;
1090 /* Function to free enum kind history. */
1093 gfc_free_enum_history (void)
1095 enumerator_history *current = enum_history;
1096 enumerator_history *next;
1098 while (current != NULL)
1100 next = current->next;
1105 enum_history = NULL;
1109 /* Function called by variable_decl() that adds an initialization
1110 expression to a symbol. */
1113 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1115 symbol_attribute attr;
1120 if (find_special (name, &sym))
1125 /* If this symbol is confirming an implicit parameter type,
1126 then an initialization expression is not allowed. */
1127 if (attr.flavor == FL_PARAMETER
1128 && sym->value != NULL
1131 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1140 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
1147 /* An initializer is required for PARAMETER declarations. */
1148 if (attr.flavor == FL_PARAMETER)
1150 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1156 /* If a variable appears in a DATA block, it cannot have an
1160 gfc_error ("Variable '%s' at %C with an initializer already "
1161 "appears in a DATA statement", sym->name);
1165 /* Check if the assignment can happen. This has to be put off
1166 until later for a derived type variable. */
1167 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1168 && gfc_check_assign_symbol (sym, init) == FAILURE)
1171 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1173 /* Update symbol character length according initializer. */
1174 if (sym->ts.cl->length == NULL)
1177 /* If there are multiple CHARACTER variables declared on the
1178 same line, we don't want them to share the same length. */
1179 sym->ts.cl = gfc_get_charlen ();
1180 sym->ts.cl->next = gfc_current_ns->cl_list;
1181 gfc_current_ns->cl_list = sym->ts.cl;
1183 if (sym->attr.flavor == FL_PARAMETER)
1185 if (init->expr_type == EXPR_CONSTANT)
1187 clen = init->value.character.length;
1188 sym->ts.cl->length = gfc_int_expr (clen);
1190 else if (init->expr_type == EXPR_ARRAY)
1192 gfc_expr *p = init->value.constructor->expr;
1193 clen = p->value.character.length;
1194 sym->ts.cl->length = gfc_int_expr (clen);
1196 else if (init->ts.cl && init->ts.cl->length)
1197 sym->ts.cl->length =
1198 gfc_copy_expr (sym->value->ts.cl->length);
1201 /* Update initializer character length according symbol. */
1202 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1204 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1205 gfc_constructor * p;
1207 if (init->expr_type == EXPR_CONSTANT)
1208 gfc_set_constant_character_len (len, init, false);
1209 else if (init->expr_type == EXPR_ARRAY)
1211 /* Build a new charlen to prevent simplification from
1212 deleting the length before it is resolved. */
1213 init->ts.cl = gfc_get_charlen ();
1214 init->ts.cl->next = gfc_current_ns->cl_list;
1215 gfc_current_ns->cl_list = sym->ts.cl;
1216 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
1218 for (p = init->value.constructor; p; p = p->next)
1219 gfc_set_constant_character_len (len, p->expr, false);
1224 /* Need to check if the expression we initialized this
1225 to was one of the iso_c_binding named constants. If so,
1226 and we're a parameter (constant), let it be iso_c.
1228 integer(c_int), parameter :: my_int = c_int
1229 integer(my_int) :: my_int_2
1230 If we mark my_int as iso_c (since we can see it's value
1231 is equal to one of the named constants), then my_int_2
1232 will be considered C interoperable. */
1233 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1235 sym->ts.is_iso_c |= init->ts.is_iso_c;
1236 sym->ts.is_c_interop |= init->ts.is_c_interop;
1237 /* attr bits needed for module files. */
1238 sym->attr.is_iso_c |= init->ts.is_iso_c;
1239 sym->attr.is_c_interop |= init->ts.is_c_interop;
1240 if (init->ts.is_iso_c)
1241 sym->ts.f90_type = init->ts.f90_type;
1244 /* Add initializer. Make sure we keep the ranks sane. */
1245 if (sym->attr.dimension && init->rank == 0)
1251 if (sym->attr.flavor == FL_PARAMETER
1252 && init->expr_type == EXPR_CONSTANT
1253 && spec_size (sym->as, &size) == SUCCESS
1254 && mpz_cmp_si (size, 0) > 0)
1256 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1259 array->value.constructor = c = NULL;
1260 for (n = 0; n < (int)mpz_get_si (size); n++)
1262 if (array->value.constructor == NULL)
1264 array->value.constructor = c = gfc_get_constructor ();
1269 c->next = gfc_get_constructor ();
1271 c->expr = gfc_copy_expr (init);
1275 array->shape = gfc_get_shape (sym->as->rank);
1276 for (n = 0; n < sym->as->rank; n++)
1277 spec_dimen_size (sym->as, n, &array->shape[n]);
1282 init->rank = sym->as->rank;
1286 if (sym->attr.save == SAVE_NONE)
1287 sym->attr.save = SAVE_IMPLICIT;
1295 /* Function called by variable_decl() that adds a name to a structure
1299 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1300 gfc_array_spec **as)
1304 /* If the current symbol is of the same derived type that we're
1305 constructing, it must have the pointer attribute. */
1306 if (current_ts.type == BT_DERIVED
1307 && current_ts.derived == gfc_current_block ()
1308 && current_attr.pointer == 0)
1310 gfc_error ("Component at %C must have the POINTER attribute");
1314 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1316 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1318 gfc_error ("Array component of structure at %C must have explicit "
1319 "or deferred shape");
1324 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1329 gfc_set_component_attr (c, ¤t_attr);
1331 c->initializer = *init;
1339 /* Check array components. */
1344 gfc_error ("Allocatable component at %C must be an array");
1353 if (c->as->type != AS_DEFERRED)
1355 gfc_error ("Pointer array component of structure at %C must have a "
1360 else if (c->allocatable)
1362 if (c->as->type != AS_DEFERRED)
1364 gfc_error ("Allocatable component of structure at %C must have a "
1371 if (c->as->type != AS_EXPLICIT)
1373 gfc_error ("Array component of structure at %C must have an "
1383 /* Match a 'NULL()', and possibly take care of some side effects. */
1386 gfc_match_null (gfc_expr **result)
1392 m = gfc_match (" null ( )");
1396 /* The NULL symbol now has to be/become an intrinsic function. */
1397 if (gfc_get_symbol ("null", NULL, &sym))
1399 gfc_error ("NULL() initialization at %C is ambiguous");
1403 gfc_intrinsic_symbol (sym);
1405 if (sym->attr.proc != PROC_INTRINSIC
1406 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1407 sym->name, NULL) == FAILURE
1408 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1411 e = gfc_get_expr ();
1412 e->where = gfc_current_locus;
1413 e->expr_type = EXPR_NULL;
1414 e->ts.type = BT_UNKNOWN;
1422 /* Match a variable name with an optional initializer. When this
1423 subroutine is called, a variable is expected to be parsed next.
1424 Depending on what is happening at the moment, updates either the
1425 symbol table or the current interface. */
1428 variable_decl (int elem)
1430 char name[GFC_MAX_SYMBOL_LEN + 1];
1431 gfc_expr *initializer, *char_len;
1433 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1444 old_locus = gfc_current_locus;
1446 /* When we get here, we've just matched a list of attributes and
1447 maybe a type and a double colon. The next thing we expect to see
1448 is the name of the symbol. */
1449 m = gfc_match_name (name);
1453 var_locus = gfc_current_locus;
1455 /* Now we could see the optional array spec. or character length. */
1456 m = gfc_match_array_spec (&as);
1457 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1458 cp_as = gfc_copy_array_spec (as);
1459 else if (m == MATCH_ERROR)
1463 as = gfc_copy_array_spec (current_as);
1468 if (current_ts.type == BT_CHARACTER)
1470 switch (match_char_length (&char_len))
1473 cl = gfc_get_charlen ();
1474 cl->next = gfc_current_ns->cl_list;
1475 gfc_current_ns->cl_list = cl;
1477 cl->length = char_len;
1480 /* Non-constant lengths need to be copied after the first
1481 element. Also copy assumed lengths. */
1484 && (current_ts.cl->length == NULL
1485 || current_ts.cl->length->expr_type != EXPR_CONSTANT))
1487 cl = gfc_get_charlen ();
1488 cl->next = gfc_current_ns->cl_list;
1489 gfc_current_ns->cl_list = cl;
1490 cl->length = gfc_copy_expr (current_ts.cl->length);
1502 /* If this symbol has already shown up in a Cray Pointer declaration,
1503 then we want to set the type & bail out. */
1504 if (gfc_option.flag_cray_pointer)
1506 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1507 if (sym != NULL && sym->attr.cray_pointee)
1509 sym->ts.type = current_ts.type;
1510 sym->ts.kind = current_ts.kind;
1512 sym->ts.derived = current_ts.derived;
1513 sym->ts.is_c_interop = current_ts.is_c_interop;
1514 sym->ts.is_iso_c = current_ts.is_iso_c;
1517 /* Check to see if we have an array specification. */
1520 if (sym->as != NULL)
1522 gfc_error ("Duplicate array spec for Cray pointee at %C");
1523 gfc_free_array_spec (cp_as);
1529 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1530 gfc_internal_error ("Couldn't set pointee array spec.");
1532 /* Fix the array spec. */
1533 m = gfc_mod_pointee_as (sym->as);
1534 if (m == MATCH_ERROR)
1542 gfc_free_array_spec (cp_as);
1547 /* OK, we've successfully matched the declaration. Now put the
1548 symbol in the current namespace, because it might be used in the
1549 optional initialization expression for this symbol, e.g. this is
1552 integer, parameter :: i = huge(i)
1554 This is only true for parameters or variables of a basic type.
1555 For components of derived types, it is not true, so we don't
1556 create a symbol for those yet. If we fail to create the symbol,
1558 if (gfc_current_state () != COMP_DERIVED
1559 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1565 /* An interface body specifies all of the procedure's
1566 characteristics and these shall be consistent with those
1567 specified in the procedure definition, except that the interface
1568 may specify a procedure that is not pure if the procedure is
1569 defined to be pure(12.3.2). */
1570 if (current_ts.type == BT_DERIVED
1571 && gfc_current_ns->proc_name
1572 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1573 && current_ts.derived->ns != gfc_current_ns)
1576 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.derived->name);
1577 if (!(current_ts.derived->attr.imported
1579 && st->n.sym == current_ts.derived)
1580 && !gfc_current_ns->has_import_set)
1582 gfc_error ("the type of '%s' at %C has not been declared within the "
1589 /* In functions that have a RESULT variable defined, the function
1590 name always refers to function calls. Therefore, the name is
1591 not allowed to appear in specification statements. */
1592 if (gfc_current_state () == COMP_FUNCTION
1593 && gfc_current_block () != NULL
1594 && gfc_current_block ()->result != NULL
1595 && gfc_current_block ()->result != gfc_current_block ()
1596 && strcmp (gfc_current_block ()->name, name) == 0)
1598 gfc_error ("Function name '%s' not allowed at %C", name);
1603 /* We allow old-style initializations of the form
1604 integer i /2/, j(4) /3*3, 1/
1605 (if no colon has been seen). These are different from data
1606 statements in that initializers are only allowed to apply to the
1607 variable immediately preceding, i.e.
1609 is not allowed. Therefore we have to do some work manually, that
1610 could otherwise be left to the matchers for DATA statements. */
1612 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1614 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1615 "initialization at %C") == FAILURE)
1618 return match_old_style_init (name);
1621 /* The double colon must be present in order to have initializers.
1622 Otherwise the statement is ambiguous with an assignment statement. */
1625 if (gfc_match (" =>") == MATCH_YES)
1627 if (!current_attr.pointer)
1629 gfc_error ("Initialization at %C isn't for a pointer variable");
1634 m = gfc_match_null (&initializer);
1637 gfc_error ("Pointer initialization requires a NULL() at %C");
1641 if (gfc_pure (NULL))
1643 gfc_error ("Initialization of pointer at %C is not allowed in "
1644 "a PURE procedure");
1652 else if (gfc_match_char ('=') == MATCH_YES)
1654 if (current_attr.pointer)
1656 gfc_error ("Pointer initialization at %C requires '=>', "
1662 m = gfc_match_init_expr (&initializer);
1665 gfc_error ("Expected an initialization expression at %C");
1669 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1671 gfc_error ("Initialization of variable at %C is not allowed in "
1672 "a PURE procedure");
1681 if (initializer != NULL && current_attr.allocatable
1682 && gfc_current_state () == COMP_DERIVED)
1684 gfc_error ("Initialization of allocatable component at %C is not "
1690 /* Add the initializer. Note that it is fine if initializer is
1691 NULL here, because we sometimes also need to check if a
1692 declaration *must* have an initialization expression. */
1693 if (gfc_current_state () != COMP_DERIVED)
1694 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1697 if (current_ts.type == BT_DERIVED
1698 && !current_attr.pointer && !initializer)
1699 initializer = gfc_default_initializer (¤t_ts);
1700 t = build_struct (name, cl, &initializer, &as);
1703 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1706 /* Free stuff up and return. */
1707 gfc_free_expr (initializer);
1708 gfc_free_array_spec (as);
1714 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1715 This assumes that the byte size is equal to the kind number for
1716 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1719 gfc_match_old_kind_spec (gfc_typespec *ts)
1724 if (gfc_match_char ('*') != MATCH_YES)
1727 m = gfc_match_small_literal_int (&ts->kind, NULL);
1731 original_kind = ts->kind;
1733 /* Massage the kind numbers for complex types. */
1734 if (ts->type == BT_COMPLEX)
1738 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1739 gfc_basic_typename (ts->type), original_kind);
1745 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1747 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1748 gfc_basic_typename (ts->type), original_kind);
1752 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1753 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1760 /* Match a kind specification. Since kinds are generally optional, we
1761 usually return MATCH_NO if something goes wrong. If a "kind="
1762 string is found, then we know we have an error. */
1765 gfc_match_kind_spec (gfc_typespec *ts)
1775 where = gfc_current_locus;
1777 if (gfc_match_char ('(') == MATCH_NO)
1780 /* Also gobbles optional text. */
1781 if (gfc_match (" kind = ") == MATCH_YES)
1784 n = gfc_match_init_expr (&e);
1786 gfc_error ("Expected initialization expression at %C");
1792 gfc_error ("Expected scalar initialization expression at %C");
1797 msg = gfc_extract_int (e, &ts->kind);
1805 /* Before throwing away the expression, let's see if we had a
1806 C interoperable kind (and store the fact). */
1807 if (e->ts.is_c_interop == 1)
1809 /* Mark this as c interoperable if being declared with one
1810 of the named constants from iso_c_binding. */
1811 ts->is_c_interop = e->ts.is_iso_c;
1812 ts->f90_type = e->ts.f90_type;
1818 /* Ignore errors to this point, if we've gotten here. This means
1819 we ignore the m=MATCH_ERROR from above. */
1820 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1822 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1823 gfc_basic_typename (ts->type));
1826 else if (gfc_match_char (')') != MATCH_YES)
1828 gfc_error ("Missing right parenthesis at %C");
1832 /* All tests passed. */
1835 if(m == MATCH_ERROR)
1836 gfc_current_locus = where;
1838 /* Return what we know from the test(s). */
1843 gfc_current_locus = where;
1848 /* Match the various kind/length specifications in a CHARACTER
1849 declaration. We don't return MATCH_NO. */
1852 match_char_spec (gfc_typespec *ts)
1854 int kind, seen_length;
1858 gfc_expr *kind_expr = NULL;
1859 kind = gfc_default_character_kind;
1863 /* Try the old-style specification first. */
1864 old_char_selector = 0;
1866 m = match_char_length (&len);
1870 old_char_selector = 1;
1875 m = gfc_match_char ('(');
1878 m = MATCH_YES; /* Character without length is a single char. */
1882 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
1883 if (gfc_match (" kind =") == MATCH_YES)
1885 m = gfc_match_small_int_expr(&kind, &kind_expr);
1887 if (m == MATCH_ERROR)
1892 if (gfc_match (" , len =") == MATCH_NO)
1895 m = char_len_param_value (&len);
1898 if (m == MATCH_ERROR)
1905 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
1906 if (gfc_match (" len =") == MATCH_YES)
1908 m = char_len_param_value (&len);
1911 if (m == MATCH_ERROR)
1915 if (gfc_match_char (')') == MATCH_YES)
1918 if (gfc_match (" , kind =") != MATCH_YES)
1921 gfc_match_small_int_expr(&kind, &kind_expr);
1923 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1925 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1932 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
1933 m = char_len_param_value (&len);
1936 if (m == MATCH_ERROR)
1940 m = gfc_match_char (')');
1944 if (gfc_match_char (',') != MATCH_YES)
1947 gfc_match (" kind ="); /* Gobble optional text. */
1949 m = gfc_match_small_int_expr(&kind, &kind_expr);
1950 if (m == MATCH_ERROR)
1956 /* Require a right-paren at this point. */
1957 m = gfc_match_char (')');
1962 gfc_error ("Syntax error in CHARACTER declaration at %C");
1964 gfc_free_expr (len);
1968 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1970 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1974 if (seen_length == 1 && len != NULL
1975 && len->ts.type != BT_INTEGER && len->ts.type != BT_UNKNOWN)
1977 gfc_error ("Expression at %C must be of INTEGER type");
1983 gfc_free_expr (len);
1984 gfc_free_expr (kind_expr);
1988 /* Do some final massaging of the length values. */
1989 cl = gfc_get_charlen ();
1990 cl->next = gfc_current_ns->cl_list;
1991 gfc_current_ns->cl_list = cl;
1993 if (seen_length == 0)
1994 cl->length = gfc_int_expr (1);
2001 /* We have to know if it was a c interoperable kind so we can
2002 do accurate type checking of bind(c) procs, etc. */
2003 if (kind_expr != NULL)
2005 /* Mark this as c interoperable if being declared with one
2006 of the named constants from iso_c_binding. */
2007 ts->is_c_interop = kind_expr->ts.is_iso_c;
2008 gfc_free_expr (kind_expr);
2010 else if (len != NULL)
2012 /* Here, we might have parsed something such as:
2014 In this case, the parsing code above grabs the c_char when
2015 looking for the length (line 1690, roughly). it's the last
2016 testcase for parsing the kind params of a character variable.
2017 However, it's not actually the length. this seems like it
2019 To see if the user used a C interop kind, test the expr
2020 of the so called length, and see if it's C interoperable. */
2021 ts->is_c_interop = len->ts.is_iso_c;
2028 /* Matches a type specification. If successful, sets the ts structure
2029 to the matched specification. This is necessary for FUNCTION and
2030 IMPLICIT statements.
2032 If implicit_flag is nonzero, then we don't check for the optional
2033 kind specification. Not doing so is needed for matching an IMPLICIT
2034 statement correctly. */
2037 match_type_spec (gfc_typespec *ts, int implicit_flag)
2039 char name[GFC_MAX_SYMBOL_LEN + 1];
2046 /* Clear the current binding label, in case one is given. */
2047 curr_binding_label[0] = '\0';
2049 if (gfc_match (" byte") == MATCH_YES)
2051 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
2055 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2057 gfc_error ("BYTE type used at %C "
2058 "is not available on the target machine");
2062 ts->type = BT_INTEGER;
2067 if (gfc_match (" integer") == MATCH_YES)
2069 ts->type = BT_INTEGER;
2070 ts->kind = gfc_default_integer_kind;
2074 if (gfc_match (" character") == MATCH_YES)
2076 ts->type = BT_CHARACTER;
2077 if (implicit_flag == 0)
2078 return match_char_spec (ts);
2083 if (gfc_match (" real") == MATCH_YES)
2086 ts->kind = gfc_default_real_kind;
2090 if (gfc_match (" double precision") == MATCH_YES)
2093 ts->kind = gfc_default_double_kind;
2097 if (gfc_match (" complex") == MATCH_YES)
2099 ts->type = BT_COMPLEX;
2100 ts->kind = gfc_default_complex_kind;
2104 if (gfc_match (" double complex") == MATCH_YES)
2106 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2107 "conform to the Fortran 95 standard") == FAILURE)
2110 ts->type = BT_COMPLEX;
2111 ts->kind = gfc_default_double_kind;
2115 if (gfc_match (" logical") == MATCH_YES)
2117 ts->type = BT_LOGICAL;
2118 ts->kind = gfc_default_logical_kind;
2122 m = gfc_match (" type ( %n )", name);
2126 /* Search for the name but allow the components to be defined later. */
2127 if (gfc_get_ha_symbol (name, &sym))
2129 gfc_error ("Type name '%s' at %C is ambiguous", name);
2133 if (sym->attr.flavor != FL_DERIVED
2134 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2137 ts->type = BT_DERIVED;
2144 /* For all types except double, derived and character, look for an
2145 optional kind specifier. MATCH_NO is actually OK at this point. */
2146 if (implicit_flag == 1)
2149 if (gfc_current_form == FORM_FREE)
2151 c = gfc_peek_char();
2152 if (!gfc_is_whitespace(c) && c != '*' && c != '('
2153 && c != ':' && c != ',')
2157 m = gfc_match_kind_spec (ts);
2158 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2159 m = gfc_match_old_kind_spec (ts);
2162 m = MATCH_YES; /* No kind specifier found. */
2168 /* Match an IMPLICIT NONE statement. Actually, this statement is
2169 already matched in parse.c, or we would not end up here in the
2170 first place. So the only thing we need to check, is if there is
2171 trailing garbage. If not, the match is successful. */
2174 gfc_match_implicit_none (void)
2176 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2180 /* Match the letter range(s) of an IMPLICIT statement. */
2183 match_implicit_range (void)
2185 int c, c1, c2, inner;
2188 cur_loc = gfc_current_locus;
2190 gfc_gobble_whitespace ();
2191 c = gfc_next_char ();
2194 gfc_error ("Missing character range in IMPLICIT at %C");
2201 gfc_gobble_whitespace ();
2202 c1 = gfc_next_char ();
2206 gfc_gobble_whitespace ();
2207 c = gfc_next_char ();
2212 inner = 0; /* Fall through. */
2219 gfc_gobble_whitespace ();
2220 c2 = gfc_next_char ();
2224 gfc_gobble_whitespace ();
2225 c = gfc_next_char ();
2227 if ((c != ',') && (c != ')'))
2240 gfc_error ("Letters must be in alphabetic order in "
2241 "IMPLICIT statement at %C");
2245 /* See if we can add the newly matched range to the pending
2246 implicits from this IMPLICIT statement. We do not check for
2247 conflicts with whatever earlier IMPLICIT statements may have
2248 set. This is done when we've successfully finished matching
2250 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2257 gfc_syntax_error (ST_IMPLICIT);
2259 gfc_current_locus = cur_loc;
2264 /* Match an IMPLICIT statement, storing the types for
2265 gfc_set_implicit() if the statement is accepted by the parser.
2266 There is a strange looking, but legal syntactic construction
2267 possible. It looks like:
2269 IMPLICIT INTEGER (a-b) (c-d)
2271 This is legal if "a-b" is a constant expression that happens to
2272 equal one of the legal kinds for integers. The real problem
2273 happens with an implicit specification that looks like:
2275 IMPLICIT INTEGER (a-b)
2277 In this case, a typespec matcher that is "greedy" (as most of the
2278 matchers are) gobbles the character range as a kindspec, leaving
2279 nothing left. We therefore have to go a bit more slowly in the
2280 matching process by inhibiting the kindspec checking during
2281 typespec matching and checking for a kind later. */
2284 gfc_match_implicit (void)
2291 /* We don't allow empty implicit statements. */
2292 if (gfc_match_eos () == MATCH_YES)
2294 gfc_error ("Empty IMPLICIT statement at %C");
2300 /* First cleanup. */
2301 gfc_clear_new_implicit ();
2303 /* A basic type is mandatory here. */
2304 m = match_type_spec (&ts, 1);
2305 if (m == MATCH_ERROR)
2310 cur_loc = gfc_current_locus;
2311 m = match_implicit_range ();
2315 /* We may have <TYPE> (<RANGE>). */
2316 gfc_gobble_whitespace ();
2317 c = gfc_next_char ();
2318 if ((c == '\n') || (c == ','))
2320 /* Check for CHARACTER with no length parameter. */
2321 if (ts.type == BT_CHARACTER && !ts.cl)
2323 ts.kind = gfc_default_character_kind;
2324 ts.cl = gfc_get_charlen ();
2325 ts.cl->next = gfc_current_ns->cl_list;
2326 gfc_current_ns->cl_list = ts.cl;
2327 ts.cl->length = gfc_int_expr (1);
2330 /* Record the Successful match. */
2331 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2336 gfc_current_locus = cur_loc;
2339 /* Discard the (incorrectly) matched range. */
2340 gfc_clear_new_implicit ();
2342 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2343 if (ts.type == BT_CHARACTER)
2344 m = match_char_spec (&ts);
2347 m = gfc_match_kind_spec (&ts);
2350 m = gfc_match_old_kind_spec (&ts);
2351 if (m == MATCH_ERROR)
2357 if (m == MATCH_ERROR)
2360 m = match_implicit_range ();
2361 if (m == MATCH_ERROR)
2366 gfc_gobble_whitespace ();
2367 c = gfc_next_char ();
2368 if ((c != '\n') && (c != ','))
2371 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2379 gfc_syntax_error (ST_IMPLICIT);
2387 gfc_match_import (void)
2389 char name[GFC_MAX_SYMBOL_LEN + 1];
2394 if (gfc_current_ns->proc_name == NULL
2395 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2397 gfc_error ("IMPORT statement at %C only permitted in "
2398 "an INTERFACE body");
2402 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2406 if (gfc_match_eos () == MATCH_YES)
2408 /* All host variables should be imported. */
2409 gfc_current_ns->has_import_set = 1;
2413 if (gfc_match (" ::") == MATCH_YES)
2415 if (gfc_match_eos () == MATCH_YES)
2417 gfc_error ("Expecting list of named entities at %C");
2424 m = gfc_match (" %n", name);
2428 if (gfc_current_ns->parent != NULL
2429 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2431 gfc_error ("Type name '%s' at %C is ambiguous", name);
2434 else if (gfc_current_ns->proc_name->ns->parent != NULL
2435 && gfc_find_symbol (name,
2436 gfc_current_ns->proc_name->ns->parent,
2439 gfc_error ("Type name '%s' at %C is ambiguous", name);
2445 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2446 "at %C - does not exist.", name);
2450 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2452 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2457 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2460 sym->attr.imported = 1;
2472 if (gfc_match_eos () == MATCH_YES)
2474 if (gfc_match_char (',') != MATCH_YES)
2481 gfc_error ("Syntax error in IMPORT statement at %C");
2486 /* A minimal implementation of gfc_match without whitespace, escape
2487 characters or variable arguments. Returns true if the next
2488 characters match the TARGET template exactly. */
2491 match_string_p (const char *target)
2495 for (p = target; *p; p++)
2496 if (gfc_next_char () != *p)
2501 /* Matches an attribute specification including array specs. If
2502 successful, leaves the variables current_attr and current_as
2503 holding the specification. Also sets the colon_seen variable for
2504 later use by matchers associated with initializations.
2506 This subroutine is a little tricky in the sense that we don't know
2507 if we really have an attr-spec until we hit the double colon.
2508 Until that time, we can only return MATCH_NO. This forces us to
2509 check for duplicate specification at this level. */
2512 match_attr_spec (void)
2514 /* Modifiers that can exist in a type statement. */
2516 { GFC_DECL_BEGIN = 0,
2517 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2518 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2519 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2520 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2521 DECL_IS_BIND_C, DECL_NONE,
2522 GFC_DECL_END /* Sentinel */
2526 /* GFC_DECL_END is the sentinel, index starts at 0. */
2527 #define NUM_DECL GFC_DECL_END
2529 locus start, seen_at[NUM_DECL];
2536 gfc_clear_attr (¤t_attr);
2537 start = gfc_current_locus;
2542 /* See if we get all of the keywords up to the final double colon. */
2543 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2551 gfc_gobble_whitespace ();
2553 ch = gfc_next_char ();
2556 /* This is the successful exit condition for the loop. */
2557 if (gfc_next_char () == ':')
2562 gfc_gobble_whitespace ();
2563 switch (gfc_peek_char ())
2566 if (match_string_p ("allocatable"))
2567 d = DECL_ALLOCATABLE;
2571 /* Try and match the bind(c). */
2572 m = gfc_match_bind_c (NULL);
2575 else if (m == MATCH_ERROR)
2580 if (match_string_p ("dimension"))
2585 if (match_string_p ("external"))
2590 if (match_string_p ("int"))
2592 ch = gfc_next_char ();
2595 if (match_string_p ("nt"))
2597 /* Matched "intent". */
2598 /* TODO: Call match_intent_spec from here. */
2599 if (gfc_match (" ( in out )") == MATCH_YES)
2601 else if (gfc_match (" ( in )") == MATCH_YES)
2603 else if (gfc_match (" ( out )") == MATCH_YES)
2609 if (match_string_p ("insic"))
2611 /* Matched "intrinsic". */
2619 if (match_string_p ("optional"))
2625 switch (gfc_next_char ())
2628 if (match_string_p ("rameter"))
2630 /* Matched "parameter". */
2636 if (match_string_p ("inter"))
2638 /* Matched "pointer". */
2644 ch = gfc_next_char ();
2647 if (match_string_p ("vate"))
2649 /* Matched "private". */
2655 if (match_string_p ("tected"))
2657 /* Matched "protected". */
2664 if (match_string_p ("blic"))
2666 /* Matched "public". */
2674 if (match_string_p ("save"))
2679 if (match_string_p ("target"))
2685 ch = gfc_next_char ();
2688 if (match_string_p ("lue"))
2690 /* Matched "value". */
2696 if (match_string_p ("latile"))
2698 /* Matched "volatile". */
2706 /* No double colon and no recognizable decl_type, so assume that
2707 we've been looking at something else the whole time. */
2715 seen_at[d] = gfc_current_locus;
2717 if (d == DECL_DIMENSION)
2719 m = gfc_match_array_spec (¤t_as);
2723 gfc_error ("Missing dimension specification at %C");
2727 if (m == MATCH_ERROR)
2732 /* Since we've seen a double colon, we have to be looking at an
2733 attr-spec. This means that we can now issue errors. */
2734 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2739 case DECL_ALLOCATABLE:
2740 attr = "ALLOCATABLE";
2742 case DECL_DIMENSION:
2749 attr = "INTENT (IN)";
2752 attr = "INTENT (OUT)";
2755 attr = "INTENT (IN OUT)";
2757 case DECL_INTRINSIC:
2763 case DECL_PARAMETER:
2769 case DECL_PROTECTED:
2784 case DECL_IS_BIND_C:
2794 attr = NULL; /* This shouldn't happen. */
2797 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2802 /* Now that we've dealt with duplicate attributes, add the attributes
2803 to the current attribute. */
2804 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2809 if (gfc_current_state () == COMP_DERIVED
2810 && d != DECL_DIMENSION && d != DECL_POINTER
2811 && d != DECL_PRIVATE && d != DECL_PUBLIC
2814 if (d == DECL_ALLOCATABLE)
2816 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
2817 "attribute at %C in a TYPE definition")
2826 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2833 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2834 && gfc_current_state () != COMP_MODULE)
2836 if (d == DECL_PRIVATE)
2840 if (gfc_current_state () == COMP_DERIVED
2841 && gfc_state_stack->previous
2842 && gfc_state_stack->previous->state == COMP_MODULE)
2844 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
2845 "at %L in a TYPE definition", attr,
2855 gfc_error ("%s attribute at %L is not allowed outside of the "
2856 "specification part of a module", attr, &seen_at[d]);
2864 case DECL_ALLOCATABLE:
2865 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
2868 case DECL_DIMENSION:
2869 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
2873 t = gfc_add_external (¤t_attr, &seen_at[d]);
2877 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
2881 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
2885 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
2888 case DECL_INTRINSIC:
2889 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
2893 t = gfc_add_optional (¤t_attr, &seen_at[d]);
2896 case DECL_PARAMETER:
2897 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
2901 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
2904 case DECL_PROTECTED:
2905 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
2907 gfc_error ("PROTECTED at %C only allowed in specification "
2908 "part of a module");
2913 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
2918 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
2922 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
2927 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
2932 t = gfc_add_save (¤t_attr, NULL, &seen_at[d]);
2936 t = gfc_add_target (¤t_attr, &seen_at[d]);
2939 case DECL_IS_BIND_C:
2940 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
2944 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
2949 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
2953 if (gfc_notify_std (GFC_STD_F2003,
2954 "Fortran 2003: VOLATILE attribute at %C")
2958 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
2962 gfc_internal_error ("match_attr_spec(): Bad attribute");
2976 gfc_current_locus = start;
2977 gfc_free_array_spec (current_as);
2983 /* Set the binding label, dest_label, either with the binding label
2984 stored in the given gfc_typespec, ts, or if none was provided, it
2985 will be the symbol name in all lower case, as required by the draft
2986 (J3/04-007, section 15.4.1). If a binding label was given and
2987 there is more than one argument (num_idents), it is an error. */
2990 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
2992 if (num_idents > 1 && has_name_equals)
2994 gfc_error ("Multiple identifiers provided with "
2995 "single NAME= specifier at %C");
2999 if (curr_binding_label[0] != '\0')
3001 /* Binding label given; store in temp holder til have sym. */
3002 strncpy (dest_label, curr_binding_label,
3003 strlen (curr_binding_label) + 1);
3007 /* No binding label given, and the NAME= specifier did not exist,
3008 which means there was no NAME="". */
3009 if (sym_name != NULL && has_name_equals == 0)
3010 strncpy (dest_label, sym_name, strlen (sym_name) + 1);
3017 /* Set the status of the given common block as being BIND(C) or not,
3018 depending on the given parameter, is_bind_c. */
3021 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3023 com_block->is_bind_c = is_bind_c;
3028 /* Verify that the given gfc_typespec is for a C interoperable type. */
3031 verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
3035 /* Make sure the kind used is appropriate for the type.
3036 The f90_type is unknown if an integer constant was
3037 used (e.g., real(4), bind(c) :: myFloat). */
3038 if (ts->f90_type != BT_UNKNOWN)
3040 t = gfc_validate_c_kind (ts);
3043 /* Print an error, but continue parsing line. */
3044 gfc_error_now ("C kind parameter is for type %s but "
3045 "symbol '%s' at %L is of type %s",
3046 gfc_basic_typename (ts->f90_type),
3048 gfc_basic_typename (ts->type));
3052 /* Make sure the kind is C interoperable. This does not care about the
3053 possible error above. */
3054 if (ts->type == BT_DERIVED && ts->derived != NULL)
3055 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
3056 else if (ts->is_c_interop != 1)
3063 /* Verify that the variables of a given common block, which has been
3064 defined with the attribute specifier bind(c), to be of a C
3065 interoperable type. Errors will be reported here, if
3069 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3071 gfc_symbol *curr_sym = NULL;
3072 try retval = SUCCESS;
3074 curr_sym = com_block->head;
3076 /* Make sure we have at least one symbol. */
3077 if (curr_sym == NULL)
3080 /* Here we know we have a symbol, so we'll execute this loop
3084 /* The second to last param, 1, says this is in a common block. */
3085 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3086 curr_sym = curr_sym->common_next;
3087 } while (curr_sym != NULL);
3093 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3094 an appropriate error message is reported. */
3097 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3098 int is_in_common, gfc_common_head *com_block)
3100 try retval = SUCCESS;
3102 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3104 tmp_sym = tmp_sym->result;
3105 /* Make sure it wasn't an implicitly typed result. */
3106 if (tmp_sym->attr.implicit_type)
3108 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3109 "%L may not be C interoperable", tmp_sym->name,
3110 &tmp_sym->declared_at);
3111 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3112 /* Mark it as C interoperable to prevent duplicate warnings. */
3113 tmp_sym->ts.is_c_interop = 1;
3114 tmp_sym->attr.is_c_interop = 1;
3118 /* Here, we know we have the bind(c) attribute, so if we have
3119 enough type info, then verify that it's a C interop kind.
3120 The info could be in the symbol already, or possibly still in
3121 the given ts (current_ts), so look in both. */
3122 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3124 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
3125 &(tmp_sym->declared_at)) != SUCCESS)
3127 /* See if we're dealing with a sym in a common block or not. */
3128 if (is_in_common == 1)
3130 gfc_warning ("Variable '%s' in common block '%s' at %L "
3131 "may not be a C interoperable "
3132 "kind though common block '%s' is BIND(C)",
3133 tmp_sym->name, com_block->name,
3134 &(tmp_sym->declared_at), com_block->name);
3138 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3139 gfc_error ("Type declaration '%s' at %L is not C "
3140 "interoperable but it is BIND(C)",
3141 tmp_sym->name, &(tmp_sym->declared_at));
3143 gfc_warning ("Variable '%s' at %L "
3144 "may not be a C interoperable "
3145 "kind but it is bind(c)",
3146 tmp_sym->name, &(tmp_sym->declared_at));
3150 /* Variables declared w/in a common block can't be bind(c)
3151 since there's no way for C to see these variables, so there's
3152 semantically no reason for the attribute. */
3153 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3155 gfc_error ("Variable '%s' in common block '%s' at "
3156 "%L cannot be declared with BIND(C) "
3157 "since it is not a global",
3158 tmp_sym->name, com_block->name,
3159 &(tmp_sym->declared_at));
3163 /* Scalar variables that are bind(c) can not have the pointer
3164 or allocatable attributes. */
3165 if (tmp_sym->attr.is_bind_c == 1)
3167 if (tmp_sym->attr.pointer == 1)
3169 gfc_error ("Variable '%s' at %L cannot have both the "
3170 "POINTER and BIND(C) attributes",
3171 tmp_sym->name, &(tmp_sym->declared_at));
3175 if (tmp_sym->attr.allocatable == 1)
3177 gfc_error ("Variable '%s' at %L cannot have both the "
3178 "ALLOCATABLE and BIND(C) attributes",
3179 tmp_sym->name, &(tmp_sym->declared_at));
3183 /* If it is a BIND(C) function, make sure the return value is a
3184 scalar value. The previous tests in this function made sure
3185 the type is interoperable. */
3186 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
3187 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3188 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3190 /* BIND(C) functions can not return a character string. */
3191 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
3192 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3193 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3194 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3195 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3196 "be a character string", tmp_sym->name,
3197 &(tmp_sym->declared_at));
3201 /* See if the symbol has been marked as private. If it has, make sure
3202 there is no binding label and warn the user if there is one. */
3203 if (tmp_sym->attr.access == ACCESS_PRIVATE
3204 && tmp_sym->binding_label[0] != '\0')
3205 /* Use gfc_warning_now because we won't say that the symbol fails
3206 just because of this. */
3207 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3208 "given the binding label '%s'", tmp_sym->name,
3209 &(tmp_sym->declared_at), tmp_sym->binding_label);
3215 /* Set the appropriate fields for a symbol that's been declared as
3216 BIND(C) (the is_bind_c flag and the binding label), and verify that
3217 the type is C interoperable. Errors are reported by the functions
3218 used to set/test these fields. */
3221 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3223 try retval = SUCCESS;
3225 /* TODO: Do we need to make sure the vars aren't marked private? */
3227 /* Set the is_bind_c bit in symbol_attribute. */
3228 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3230 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3231 num_idents) != SUCCESS)
3238 /* Set the fields marking the given common block as BIND(C), including
3239 a binding label, and report any errors encountered. */
3242 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3244 try retval = SUCCESS;
3246 /* destLabel, common name, typespec (which may have binding label). */
3247 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3251 /* Set the given common block (com_block) to being bind(c) (1). */
3252 set_com_block_bind_c (com_block, 1);
3258 /* Retrieve the list of one or more identifiers that the given bind(c)
3259 attribute applies to. */
3262 get_bind_c_idents (void)
3264 char name[GFC_MAX_SYMBOL_LEN + 1];
3266 gfc_symbol *tmp_sym = NULL;
3268 gfc_common_head *com_block = NULL;
3270 if (gfc_match_name (name) == MATCH_YES)
3272 found_id = MATCH_YES;
3273 gfc_get_ha_symbol (name, &tmp_sym);
3275 else if (match_common_name (name) == MATCH_YES)
3277 found_id = MATCH_YES;
3278 com_block = gfc_get_common (name, 0);
3282 gfc_error ("Need either entity or common block name for "
3283 "attribute specification statement at %C");
3287 /* Save the current identifier and look for more. */
3290 /* Increment the number of identifiers found for this spec stmt. */
3293 /* Make sure we have a sym or com block, and verify that it can
3294 be bind(c). Set the appropriate field(s) and look for more
3296 if (tmp_sym != NULL || com_block != NULL)
3298 if (tmp_sym != NULL)
3300 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3306 if (set_verify_bind_c_com_block(com_block, num_idents)
3311 /* Look to see if we have another identifier. */
3313 if (gfc_match_eos () == MATCH_YES)
3314 found_id = MATCH_NO;
3315 else if (gfc_match_char (',') != MATCH_YES)
3316 found_id = MATCH_NO;
3317 else if (gfc_match_name (name) == MATCH_YES)
3319 found_id = MATCH_YES;
3320 gfc_get_ha_symbol (name, &tmp_sym);
3322 else if (match_common_name (name) == MATCH_YES)
3324 found_id = MATCH_YES;
3325 com_block = gfc_get_common (name, 0);
3329 gfc_error ("Missing entity or common block name for "
3330 "attribute specification statement at %C");
3336 gfc_internal_error ("Missing symbol");
3338 } while (found_id == MATCH_YES);
3340 /* if we get here we were successful */
3345 /* Try and match a BIND(C) attribute specification statement. */
3348 gfc_match_bind_c_stmt (void)
3350 match found_match = MATCH_NO;
3355 /* This may not be necessary. */
3357 /* Clear the temporary binding label holder. */
3358 curr_binding_label[0] = '\0';
3360 /* Look for the bind(c). */
3361 found_match = gfc_match_bind_c (NULL);
3363 if (found_match == MATCH_YES)
3365 /* Look for the :: now, but it is not required. */
3368 /* Get the identifier(s) that needs to be updated. This may need to
3369 change to hand the flag(s) for the attr specified so all identifiers
3370 found can have all appropriate parts updated (assuming that the same
3371 spec stmt can have multiple attrs, such as both bind(c) and
3373 if (get_bind_c_idents () != SUCCESS)
3374 /* Error message should have printed already. */
3382 /* Match a data declaration statement. */
3385 gfc_match_data_decl (void)
3391 num_idents_on_line = 0;
3393 m = match_type_spec (¤t_ts, 0);
3397 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3399 sym = gfc_use_derived (current_ts.derived);
3407 current_ts.derived = sym;
3410 m = match_attr_spec ();
3411 if (m == MATCH_ERROR)
3417 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL
3418 && !current_ts.derived->attr.zero_comp)
3421 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3424 gfc_find_symbol (current_ts.derived->name,
3425 current_ts.derived->ns->parent, 1, &sym);
3427 /* Any symbol that we find had better be a type definition
3428 which has its components defined. */
3429 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3430 && (current_ts.derived->components != NULL
3431 || current_ts.derived->attr.zero_comp))
3434 /* Now we have an error, which we signal, and then fix up
3435 because the knock-on is plain and simple confusing. */
3436 gfc_error_now ("Derived type at %C has not been previously defined "
3437 "and so cannot appear in a derived type definition");
3438 current_attr.pointer = 1;
3443 /* If we have an old-style character declaration, and no new-style
3444 attribute specifications, then there a comma is optional between
3445 the type specification and the variable list. */
3446 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3447 gfc_match_char (',');
3449 /* Give the types/attributes to symbols that follow. Give the element
3450 a number so that repeat character length expressions can be copied. */
3454 num_idents_on_line++;
3455 m = variable_decl (elem++);
3456 if (m == MATCH_ERROR)
3461 if (gfc_match_eos () == MATCH_YES)
3463 if (gfc_match_char (',') != MATCH_YES)
3467 if (gfc_error_flag_test () == 0)
3468 gfc_error ("Syntax error in data declaration at %C");
3471 gfc_free_data_all (gfc_current_ns);
3474 gfc_free_array_spec (current_as);
3480 /* Match a prefix associated with a function or subroutine
3481 declaration. If the typespec pointer is nonnull, then a typespec
3482 can be matched. Note that if nothing matches, MATCH_YES is
3483 returned (the null string was matched). */
3486 match_prefix (gfc_typespec *ts)
3490 gfc_clear_attr (¤t_attr);
3494 if (!seen_type && ts != NULL
3495 && match_type_spec (ts, 0) == MATCH_YES
3496 && gfc_match_space () == MATCH_YES)
3503 if (gfc_match ("elemental% ") == MATCH_YES)
3505 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
3511 if (gfc_match ("pure% ") == MATCH_YES)
3513 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
3519 if (gfc_match ("recursive% ") == MATCH_YES)
3521 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
3527 /* At this point, the next item is not a prefix. */
3532 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
3535 copy_prefix (symbol_attribute *dest, locus *where)
3537 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3540 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3543 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3550 /* Match a formal argument list. */
3553 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3555 gfc_formal_arglist *head, *tail, *p, *q;
3556 char name[GFC_MAX_SYMBOL_LEN + 1];
3562 if (gfc_match_char ('(') != MATCH_YES)
3569 if (gfc_match_char (')') == MATCH_YES)
3574 if (gfc_match_char ('*') == MATCH_YES)
3578 m = gfc_match_name (name);
3582 if (gfc_get_symbol (name, NULL, &sym))
3586 p = gfc_get_formal_arglist ();
3598 /* We don't add the VARIABLE flavor because the name could be a
3599 dummy procedure. We don't apply these attributes to formal
3600 arguments of statement functions. */
3601 if (sym != NULL && !st_flag
3602 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3603 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3609 /* The name of a program unit can be in a different namespace,
3610 so check for it explicitly. After the statement is accepted,
3611 the name is checked for especially in gfc_get_symbol(). */
3612 if (gfc_new_block != NULL && sym != NULL
3613 && strcmp (sym->name, gfc_new_block->name) == 0)
3615 gfc_error ("Name '%s' at %C is the name of the procedure",
3621 if (gfc_match_char (')') == MATCH_YES)
3624 m = gfc_match_char (',');
3627 gfc_error ("Unexpected junk in formal argument list at %C");
3633 /* Check for duplicate symbols in the formal argument list. */
3636 for (p = head; p->next; p = p->next)
3641 for (q = p->next; q; q = q->next)
3642 if (p->sym == q->sym)
3644 gfc_error ("Duplicate symbol '%s' in formal argument list "
3645 "at %C", p->sym->name);
3653 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3663 gfc_free_formal_arglist (head);
3668 /* Match a RESULT specification following a function declaration or
3669 ENTRY statement. Also matches the end-of-statement. */
3672 match_result (gfc_symbol *function, gfc_symbol **result)
3674 char name[GFC_MAX_SYMBOL_LEN + 1];
3678 if (gfc_match (" result (") != MATCH_YES)
3681 m = gfc_match_name (name);
3685 /* Get the right paren, and that's it because there could be the
3686 bind(c) attribute after the result clause. */
3687 if (gfc_match_char(')') != MATCH_YES)
3689 /* TODO: should report the missing right paren here. */
3693 if (strcmp (function->name, name) == 0)
3695 gfc_error ("RESULT variable at %C must be different than function name");
3699 if (gfc_get_symbol (name, NULL, &r))
3702 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3703 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
3712 /* Match a function suffix, which could be a combination of a result
3713 clause and BIND(C), either one, or neither. The draft does not
3714 require them to come in a specific order. */
3717 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3719 match is_bind_c; /* Found bind(c). */
3720 match is_result; /* Found result clause. */
3721 match found_match; /* Status of whether we've found a good match. */
3722 int peek_char; /* Character we're going to peek at. */
3724 /* Initialize to having found nothing. */
3725 found_match = MATCH_NO;
3726 is_bind_c = MATCH_NO;
3727 is_result = MATCH_NO;
3729 /* Get the next char to narrow between result and bind(c). */
3730 gfc_gobble_whitespace ();
3731 peek_char = gfc_peek_char ();
3736 /* Look for result clause. */
3737 is_result = match_result (sym, result);
3738 if (is_result == MATCH_YES)
3740 /* Now see if there is a bind(c) after it. */
3741 is_bind_c = gfc_match_bind_c (sym);
3742 /* We've found the result clause and possibly bind(c). */
3743 found_match = MATCH_YES;
3746 /* This should only be MATCH_ERROR. */
3747 found_match = is_result;
3750 /* Look for bind(c) first. */
3751 is_bind_c = gfc_match_bind_c (sym);
3752 if (is_bind_c == MATCH_YES)
3754 /* Now see if a result clause followed it. */
3755 is_result = match_result (sym, result);
3756 found_match = MATCH_YES;
3760 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3761 found_match = MATCH_ERROR;
3765 gfc_error ("Unexpected junk after function declaration at %C");
3766 found_match = MATCH_ERROR;
3770 if (is_bind_c == MATCH_YES)
3771 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
3779 /* Match a PROCEDURE declaration (R1211). */
3782 match_procedure_decl (void)
3785 locus old_loc, entry_loc;
3786 gfc_symbol *sym, *proc_if = NULL;
3789 old_loc = entry_loc = gfc_current_locus;
3791 gfc_clear_ts (¤t_ts);
3793 if (gfc_match (" (") != MATCH_YES)
3795 gfc_current_locus = entry_loc;
3799 /* Get the type spec. for the procedure interface. */
3800 old_loc = gfc_current_locus;
3801 m = match_type_spec (¤t_ts, 0);
3802 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_char () == ')'))
3805 if (m == MATCH_ERROR)
3808 gfc_current_locus = old_loc;
3810 /* Get the name of the procedure or abstract interface
3811 to inherit the interface from. */
3812 m = gfc_match_symbol (&proc_if, 1);
3816 else if (m == MATCH_ERROR)
3819 /* Various interface checks. */
3822 if (proc_if->generic)
3824 gfc_error ("Interface '%s' at %C may not be generic", proc_if->name);
3827 if (proc_if->attr.proc == PROC_ST_FUNCTION)
3829 gfc_error ("Interface '%s' at %C may not be a statement function",
3833 /* Handle intrinsic procedures. */
3834 if (gfc_intrinsic_name (proc_if->name, 0)
3835 || gfc_intrinsic_name (proc_if->name, 1))
3836 proc_if->attr.intrinsic = 1;
3837 if (proc_if->attr.intrinsic
3838 && !gfc_intrinsic_actual_ok (proc_if->name, 0))
3840 gfc_error ("Intrinsic procedure '%s' not allowed "
3841 "in PROCEDURE statement at %C", proc_if->name);
3844 /* TODO: Allow intrinsics with gfc_intrinsic_actual_ok
3845 (proc_if->name, 0) after PR33162 is fixed. */
3846 if (proc_if->attr.intrinsic)
3848 gfc_error ("Fortran 2003: Support for intrinsic procedure '%s' "
3849 "in PROCEDURE statement at %C not yet implemented "
3850 "in gfortran", proc_if->name);
3857 if (gfc_match (" )") != MATCH_YES)
3859 gfc_current_locus = entry_loc;
3863 /* Parse attributes. */
3864 m = match_attr_spec();
3865 if (m == MATCH_ERROR)
3868 /* Get procedure symbols. */
3872 m = gfc_match_symbol (&sym, 0);
3875 else if (m == MATCH_ERROR)
3878 /* Add current_attr to the symbol attributes. */
3879 if (gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
3882 if (sym->attr.is_bind_c)
3884 /* Check for C1218. */
3885 if (!proc_if || !proc_if->attr.is_bind_c)
3887 gfc_error ("BIND(C) attribute at %C requires "
3888 "an interface with BIND(C)");
3891 /* Check for C1217. */
3892 if (has_name_equals && sym->attr.pointer)
3894 gfc_error ("BIND(C) procedure with NAME may not have "
3895 "POINTER attribute at %C");
3898 if (has_name_equals && sym->attr.dummy)
3900 gfc_error ("Dummy procedure at %C may not have "
3901 "BIND(C) attribute with NAME");
3904 /* Set binding label for BIND(C). */
3905 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
3909 if (!sym->attr.pointer && gfc_add_external (&sym->attr, NULL) == FAILURE)
3911 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
3914 /* Set interface. */
3915 if (proc_if != NULL)
3916 sym->interface = proc_if;
3917 else if (current_ts.type != BT_UNKNOWN)
3919 sym->interface = gfc_new_symbol ("", gfc_current_ns);
3920 sym->interface->ts = current_ts;
3921 sym->interface->attr.function = 1;
3922 sym->ts = sym->interface->ts;
3923 sym->attr.function = sym->interface->attr.function;
3926 if (gfc_match_eos () == MATCH_YES)
3928 if (gfc_match_char (',') != MATCH_YES)
3933 gfc_error ("Syntax error in PROCEDURE statement at %C");
3938 /* Match a PROCEDURE declaration inside an interface (R1206). */
3941 match_procedure_in_interface (void)
3945 char name[GFC_MAX_SYMBOL_LEN + 1];
3947 if (current_interface.type == INTERFACE_NAMELESS
3948 || current_interface.type == INTERFACE_ABSTRACT)
3950 gfc_error ("PROCEDURE at %C must be in a generic interface");
3956 m = gfc_match_name (name);
3959 else if (m == MATCH_ERROR)
3961 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
3964 if (gfc_add_interface (sym) == FAILURE)
3967 sym->attr.procedure = 1;
3969 if (gfc_match_eos () == MATCH_YES)
3971 if (gfc_match_char (',') != MATCH_YES)
3978 gfc_error ("Syntax error in PROCEDURE statement at %C");
3983 /* General matcher for PROCEDURE declarations. */
3986 gfc_match_procedure (void)
3990 switch (gfc_current_state ())
3995 case COMP_SUBROUTINE:
3997 m = match_procedure_decl ();
3999 case COMP_INTERFACE:
4000 m = match_procedure_in_interface ();
4003 gfc_error ("Fortran 2003: Procedure components at %C are "
4004 "not yet implemented in gfortran");
4013 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4021 /* Match a function declaration. */
4024 gfc_match_function_decl (void)
4026 char name[GFC_MAX_SYMBOL_LEN + 1];
4027 gfc_symbol *sym, *result;
4031 match found_match; /* Status returned by match func. */
4033 if (gfc_current_state () != COMP_NONE
4034 && gfc_current_state () != COMP_INTERFACE
4035 && gfc_current_state () != COMP_CONTAINS)
4038 gfc_clear_ts (¤t_ts);
4040 old_loc = gfc_current_locus;
4042 m = match_prefix (¤t_ts);
4045 gfc_current_locus = old_loc;
4049 if (gfc_match ("function% %n", name) != MATCH_YES)
4051 gfc_current_locus = old_loc;
4054 if (get_proc_name (name, &sym, false))
4056 gfc_new_block = sym;
4058 m = gfc_match_formal_arglist (sym, 0, 0);
4061 gfc_error ("Expected formal argument list in function "
4062 "definition at %C");
4066 else if (m == MATCH_ERROR)
4071 /* According to the draft, the bind(c) and result clause can
4072 come in either order after the formal_arg_list (i.e., either
4073 can be first, both can exist together or by themselves or neither
4074 one). Therefore, the match_result can't match the end of the
4075 string, and check for the bind(c) or result clause in either order. */
4076 found_match = gfc_match_eos ();
4078 /* Make sure that it isn't already declared as BIND(C). If it is, it
4079 must have been marked BIND(C) with a BIND(C) attribute and that is
4080 not allowed for procedures. */
4081 if (sym->attr.is_bind_c == 1)
4083 sym->attr.is_bind_c = 0;
4084 if (sym->old_symbol != NULL)
4085 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4086 "variables or common blocks",
4087 &(sym->old_symbol->declared_at));
4089 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4090 "variables or common blocks", &gfc_current_locus);
4093 if (found_match != MATCH_YES)
4095 /* If we haven't found the end-of-statement, look for a suffix. */
4096 suffix_match = gfc_match_suffix (sym, &result);
4097 if (suffix_match == MATCH_YES)
4098 /* Need to get the eos now. */
4099 found_match = gfc_match_eos ();
4101 found_match = suffix_match;
4104 if(found_match != MATCH_YES)
4108 /* Make changes to the symbol. */
4111 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4114 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4115 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4118 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
4119 && !sym->attr.implicit_type)
4121 gfc_error ("Function '%s' at %C already has a type of %s", name,
4122 gfc_basic_typename (sym->ts.type));
4128 sym->ts = current_ts;
4133 result->ts = current_ts;
4134 sym->result = result;
4141 gfc_current_locus = old_loc;
4146 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4147 pass the name of the entry, rather than the gfc_current_block name, and
4148 to return false upon finding an existing global entry. */
4151 add_global_entry (const char *name, int sub)
4155 s = gfc_get_gsymbol(name);
4158 || (s->type != GSYM_UNKNOWN
4159 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
4160 global_used(s, NULL);
4163 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4164 s->where = gfc_current_locus;
4172 /* Match an ENTRY statement. */
4175 gfc_match_entry (void)
4180 char name[GFC_MAX_SYMBOL_LEN + 1];
4181 gfc_compile_state state;
4185 bool module_procedure;
4187 m = gfc_match_name (name);
4191 state = gfc_current_state ();
4192 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
4197 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4200 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4202 case COMP_BLOCK_DATA:
4203 gfc_error ("ENTRY statement at %C cannot appear within "
4206 case COMP_INTERFACE:
4207 gfc_error ("ENTRY statement at %C cannot appear within "
4211 gfc_error ("ENTRY statement at %C cannot appear within "
4212 "a DERIVED TYPE block");
4215 gfc_error ("ENTRY statement at %C cannot appear within "
4216 "an IF-THEN block");
4219 gfc_error ("ENTRY statement at %C cannot appear within "
4223 gfc_error ("ENTRY statement at %C cannot appear within "
4227 gfc_error ("ENTRY statement at %C cannot appear within "
4231 gfc_error ("ENTRY statement at %C cannot appear within "
4235 gfc_error ("ENTRY statement at %C cannot appear within "
4236 "a contained subprogram");
4239 gfc_internal_error ("gfc_match_entry(): Bad state");
4244 module_procedure = gfc_current_ns->parent != NULL
4245 && gfc_current_ns->parent->proc_name
4246 && gfc_current_ns->parent->proc_name->attr.flavor
4249 if (gfc_current_ns->parent != NULL
4250 && gfc_current_ns->parent->proc_name
4251 && !module_procedure)
4253 gfc_error("ENTRY statement at %C cannot appear in a "
4254 "contained procedure");
4258 /* Module function entries need special care in get_proc_name
4259 because previous references within the function will have
4260 created symbols attached to the current namespace. */
4261 if (get_proc_name (name, &entry,
4262 gfc_current_ns->parent != NULL
4264 && gfc_current_ns->proc_name->attr.function))
4267 proc = gfc_current_block ();
4269 if (state == COMP_SUBROUTINE)
4271 /* An entry in a subroutine. */
4272 if (!add_global_entry (name, 1))
4275 m = gfc_match_formal_arglist (entry, 0, 1);
4279 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4280 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4285 /* An entry in a function.
4286 We need to take special care because writing
4291 ENTRY f() RESULT (r)
4293 ENTRY f RESULT (r). */
4294 if (!add_global_entry (name, 0))
4297 old_loc = gfc_current_locus;
4298 if (gfc_match_eos () == MATCH_YES)
4300 gfc_current_locus = old_loc;
4301 /* Match the empty argument list, and add the interface to
4303 m = gfc_match_formal_arglist (entry, 0, 1);
4306 m = gfc_match_formal_arglist (entry, 0, 0);
4313 if (gfc_match_eos () == MATCH_YES)
4315 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4316 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4319 entry->result = entry;
4323 m = match_result (proc, &result);
4325 gfc_syntax_error (ST_ENTRY);
4329 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4330 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
4331 || gfc_add_function (&entry->attr, result->name, NULL)
4335 entry->result = result;
4339 if (gfc_match_eos () != MATCH_YES)
4341 gfc_syntax_error (ST_ENTRY);
4345 entry->attr.recursive = proc->attr.recursive;
4346 entry->attr.elemental = proc->attr.elemental;
4347 entry->attr.pure = proc->attr.pure;
4349 el = gfc_get_entry_list ();
4351 el->next = gfc_current_ns->entries;
4352 gfc_current_ns->entries = el;
4354 el->id = el->next->id + 1;
4358 new_st.op = EXEC_ENTRY;
4359 new_st.ext.entry = el;
4365 /* Match a subroutine statement, including optional prefixes. */
4368 gfc_match_subroutine (void)
4370 char name[GFC_MAX_SYMBOL_LEN + 1];
4376 if (gfc_current_state () != COMP_NONE
4377 && gfc_current_state () != COMP_INTERFACE
4378 && gfc_current_state () != COMP_CONTAINS)
4381 m = match_prefix (NULL);
4385 m = gfc_match ("subroutine% %n", name);
4389 if (get_proc_name (name, &sym, false))
4391 gfc_new_block = sym;
4393 /* Check what next non-whitespace character is so we can tell if there
4394 where the required parens if we have a BIND(C). */
4395 gfc_gobble_whitespace ();
4396 peek_char = gfc_peek_char ();
4398 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
4401 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4404 /* Make sure that it isn't already declared as BIND(C). If it is, it
4405 must have been marked BIND(C) with a BIND(C) attribute and that is
4406 not allowed for procedures. */
4407 if (sym->attr.is_bind_c == 1)
4409 sym->attr.is_bind_c = 0;
4410 if (sym->old_symbol != NULL)
4411 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4412 "variables or common blocks",
4413 &(sym->old_symbol->declared_at));
4415 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4416 "variables or common blocks", &gfc_current_locus);
4419 /* Here, we are just checking if it has the bind(c) attribute, and if
4420 so, then we need to make sure it's all correct. If it doesn't,
4421 we still need to continue matching the rest of the subroutine line. */
4422 is_bind_c = gfc_match_bind_c (sym);
4423 if (is_bind_c == MATCH_ERROR)
4425 /* There was an attempt at the bind(c), but it was wrong. An
4426 error message should have been printed w/in the gfc_match_bind_c
4427 so here we'll just return the MATCH_ERROR. */
4431 if (is_bind_c == MATCH_YES)
4433 if (peek_char != '(')
4435 gfc_error ("Missing required parentheses before BIND(C) at %C");
4438 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4443 if (gfc_match_eos () != MATCH_YES)
4445 gfc_syntax_error (ST_SUBROUTINE);
4449 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4456 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4457 given, and set the binding label in either the given symbol (if not
4458 NULL), or in the current_ts. The symbol may be NULL because we may
4459 encounter the BIND(C) before the declaration itself. Return
4460 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4461 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4462 or MATCH_YES if the specifier was correct and the binding label and
4463 bind(c) fields were set correctly for the given symbol or the
4467 gfc_match_bind_c (gfc_symbol *sym)
4469 /* binding label, if exists */
4470 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4474 /* Initialize the flag that specifies whether we encountered a NAME=
4475 specifier or not. */
4476 has_name_equals = 0;
4478 /* Init the first char to nil so we can catch if we don't have
4479 the label (name attr) or the symbol name yet. */
4480 binding_label[0] = '\0';
4482 /* This much we have to be able to match, in this order, if
4483 there is a bind(c) label. */
4484 if (gfc_match (" bind ( c ") != MATCH_YES)
4487 /* Now see if there is a binding label, or if we've reached the
4488 end of the bind(c) attribute without one. */
4489 if (gfc_match_char (',') == MATCH_YES)
4491 if (gfc_match (" name = ") != MATCH_YES)
4493 gfc_error ("Syntax error in NAME= specifier for binding label "
4495 /* should give an error message here */
4499 has_name_equals = 1;
4501 /* Get the opening quote. */
4502 double_quote = MATCH_YES;
4503 single_quote = MATCH_YES;
4504 double_quote = gfc_match_char ('"');
4505 if (double_quote != MATCH_YES)
4506 single_quote = gfc_match_char ('\'');
4507 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4509 gfc_error ("Syntax error in NAME= specifier for binding label "
4514 /* Grab the binding label, using functions that will not lower
4515 case the names automatically. */
4516 if (gfc_match_name_C (binding_label) != MATCH_YES)
4519 /* Get the closing quotation. */
4520 if (double_quote == MATCH_YES)
4522 if (gfc_match_char ('"') != MATCH_YES)
4524 gfc_error ("Missing closing quote '\"' for binding label at %C");
4525 /* User started string with '"' so looked to match it. */
4531 if (gfc_match_char ('\'') != MATCH_YES)
4533 gfc_error ("Missing closing quote '\'' for binding label at %C");
4534 /* User started string with "'" char. */
4540 /* Get the required right paren. */
4541 if (gfc_match_char (')') != MATCH_YES)
4543 gfc_error ("Missing closing paren for binding label at %C");
4547 /* Save the binding label to the symbol. If sym is null, we're
4548 probably matching the typespec attributes of a declaration and
4549 haven't gotten the name yet, and therefore, no symbol yet. */
4550 if (binding_label[0] != '\0')
4554 strncpy (sym->binding_label, binding_label,
4555 strlen (binding_label)+1);
4558 strncpy (curr_binding_label, binding_label,
4559 strlen (binding_label) + 1);
4563 /* No binding label, but if symbol isn't null, we
4564 can set the label for it here. */
4565 /* TODO: If the name= was given and no binding label (name=""), we simply
4566 will let fortran mangle the symbol name as it usually would.
4567 However, this could still let C call it if the user looked up the
4568 symbol in the object file. Should the name set during mangling in
4569 trans-decl.c be marked with characters that are invalid for C to
4571 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4572 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4575 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
4576 && current_interface.type == INTERFACE_ABSTRACT)
4578 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
4586 /* Return nonzero if we're currently compiling a contained procedure. */
4589 contained_procedure (void)
4593 for (s=gfc_state_stack; s; s=s->previous)
4594 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
4595 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
4601 /* Set the kind of each enumerator. The kind is selected such that it is
4602 interoperable with the corresponding C enumeration type, making
4603 sure that -fshort-enums is honored. */
4608 enumerator_history *current_history = NULL;
4612 if (max_enum == NULL || enum_history == NULL)
4615 if (!gfc_option.fshort_enums)
4621 kind = gfc_integer_kinds[i++].kind;
4623 while (kind < gfc_c_int_kind
4624 && gfc_check_integer_range (max_enum->initializer->value.integer,
4627 current_history = enum_history;
4628 while (current_history != NULL)
4630 current_history->sym->ts.kind = kind;
4631 current_history = current_history->next;
4636 /* Match any of the various end-block statements. Returns the type of
4637 END to the caller. The END INTERFACE, END IF, END DO and END
4638 SELECT statements cannot be replaced by a single END statement. */
4641 gfc_match_end (gfc_statement *st)
4643 char name[GFC_MAX_SYMBOL_LEN + 1];
4644 gfc_compile_state state;
4646 const char *block_name;
4651 old_loc = gfc_current_locus;
4652 if (gfc_match ("end") != MATCH_YES)
4655 state = gfc_current_state ();
4656 block_name = gfc_current_block () == NULL
4657 ? NULL : gfc_current_block ()->name;
4659 if (state == COMP_CONTAINS)
4661 state = gfc_state_stack->previous->state;
4662 block_name = gfc_state_stack->previous->sym == NULL
4663 ? NULL : gfc_state_stack->previous->sym->name;
4670 *st = ST_END_PROGRAM;
4671 target = " program";
4675 case COMP_SUBROUTINE:
4676 *st = ST_END_SUBROUTINE;
4677 target = " subroutine";
4678 eos_ok = !contained_procedure ();
4682 *st = ST_END_FUNCTION;
4683 target = " function";
4684 eos_ok = !contained_procedure ();
4687 case COMP_BLOCK_DATA:
4688 *st = ST_END_BLOCK_DATA;
4689 target = " block data";
4694 *st = ST_END_MODULE;
4699 case COMP_INTERFACE:
4700 *st = ST_END_INTERFACE;
4701 target = " interface";
4724 *st = ST_END_SELECT;
4730 *st = ST_END_FORALL;
4745 last_initializer = NULL;
4747 gfc_free_enum_history ();
4751 gfc_error ("Unexpected END statement at %C");
4755 if (gfc_match_eos () == MATCH_YES)
4759 /* We would have required END [something]. */
4760 gfc_error ("%s statement expected at %L",
4761 gfc_ascii_statement (*st), &old_loc);
4768 /* Verify that we've got the sort of end-block that we're expecting. */
4769 if (gfc_match (target) != MATCH_YES)
4771 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
4775 /* If we're at the end, make sure a block name wasn't required. */
4776 if (gfc_match_eos () == MATCH_YES)
4779 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
4780 && *st != ST_END_FORALL && *st != ST_END_WHERE)
4783 if (gfc_current_block () == NULL)
4786 gfc_error ("Expected block name of '%s' in %s statement at %C",
4787 block_name, gfc_ascii_statement (*st));
4792 /* END INTERFACE has a special handler for its several possible endings. */
4793 if (*st == ST_END_INTERFACE)
4794 return gfc_match_end_interface ();
4796 /* We haven't hit the end of statement, so what is left must be an
4798 m = gfc_match_space ();
4800 m = gfc_match_name (name);
4803 gfc_error ("Expected terminating name at %C");
4807 if (block_name == NULL)
4810 if (strcmp (name, block_name) != 0)
4812 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
4813 gfc_ascii_statement (*st));
4817 if (gfc_match_eos () == MATCH_YES)
4821 gfc_syntax_error (*st);
4824 gfc_current_locus = old_loc;
4830 /***************** Attribute declaration statements ****************/
4832 /* Set the attribute of a single variable. */
4837 char name[GFC_MAX_SYMBOL_LEN + 1];
4845 m = gfc_match_name (name);
4849 if (find_special (name, &sym))
4852 var_locus = gfc_current_locus;
4854 /* Deal with possible array specification for certain attributes. */
4855 if (current_attr.dimension
4856 || current_attr.allocatable
4857 || current_attr.pointer
4858 || current_attr.target)
4860 m = gfc_match_array_spec (&as);
4861 if (m == MATCH_ERROR)
4864 if (current_attr.dimension && m == MATCH_NO)
4866 gfc_error ("Missing array specification at %L in DIMENSION "
4867 "statement", &var_locus);
4872 if ((current_attr.allocatable || current_attr.pointer)
4873 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
4875 gfc_error ("Array specification must be deferred at %L", &var_locus);
4881 /* Update symbol table. DIMENSION attribute is set
4882 in gfc_set_array_spec(). */
4883 if (current_attr.dimension == 0
4884 && gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
4890 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
4896 if (sym->attr.cray_pointee && sym->as != NULL)
4898 /* Fix the array spec. */
4899 m = gfc_mod_pointee_as (sym->as);
4900 if (m == MATCH_ERROR)
4904 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
4910 if ((current_attr.external || current_attr.intrinsic)
4911 && sym->attr.flavor != FL_PROCEDURE
4912 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
4921 gfc_free_array_spec (as);
4926 /* Generic attribute declaration subroutine. Used for attributes that
4927 just have a list of names. */
4934 /* Gobble the optional double colon, by simply ignoring the result
4944 if (gfc_match_eos () == MATCH_YES)
4950 if (gfc_match_char (',') != MATCH_YES)
4952 gfc_error ("Unexpected character in variable list at %C");
4962 /* This routine matches Cray Pointer declarations of the form:
4963 pointer ( <pointer>, <pointee> )
4965 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
4966 The pointer, if already declared, should be an integer. Otherwise, we
4967 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
4968 be either a scalar, or an array declaration. No space is allocated for
4969 the pointee. For the statement
4970 pointer (ipt, ar(10))
4971 any subsequent uses of ar will be translated (in C-notation) as
4972 ar(i) => ((<type> *) ipt)(i)
4973 After gimplification, pointee variable will disappear in the code. */
4976 cray_pointer_decl (void)
4980 gfc_symbol *cptr; /* Pointer symbol. */
4981 gfc_symbol *cpte; /* Pointee symbol. */
4987 if (gfc_match_char ('(') != MATCH_YES)
4989 gfc_error ("Expected '(' at %C");
4993 /* Match pointer. */
4994 var_locus = gfc_current_locus;
4995 gfc_clear_attr (¤t_attr);
4996 gfc_add_cray_pointer (¤t_attr, &var_locus);
4997 current_ts.type = BT_INTEGER;
4998 current_ts.kind = gfc_index_integer_kind;
5000 m = gfc_match_symbol (&cptr, 0);
5003 gfc_error ("Expected variable name at %C");
5007 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5010 gfc_set_sym_referenced (cptr);
5012 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5014 cptr->ts.type = BT_INTEGER;
5015 cptr->ts.kind = gfc_index_integer_kind;
5017 else if (cptr->ts.type != BT_INTEGER)
5019 gfc_error ("Cray pointer at %C must be an integer");
5022 else if (cptr->ts.kind < gfc_index_integer_kind)
5023 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5024 " memory addresses require %d bytes",
5025 cptr->ts.kind, gfc_index_integer_kind);
5027 if (gfc_match_char (',') != MATCH_YES)
5029 gfc_error ("Expected \",\" at %C");
5033 /* Match Pointee. */
5034 var_locus = gfc_current_locus;
5035 gfc_clear_attr (¤t_attr);
5036 gfc_add_cray_pointee (¤t_attr, &var_locus);
5037 current_ts.type = BT_UNKNOWN;
5038 current_ts.kind = 0;
5040 m = gfc_match_symbol (&cpte, 0);
5043 gfc_error ("Expected variable name at %C");
5047 /* Check for an optional array spec. */
5048 m = gfc_match_array_spec (&as);
5049 if (m == MATCH_ERROR)
5051 gfc_free_array_spec (as);
5054 else if (m == MATCH_NO)
5056 gfc_free_array_spec (as);
5060 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5063 gfc_set_sym_referenced (cpte);
5065 if (cpte->as == NULL)
5067 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5068 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5070 else if (as != NULL)
5072 gfc_error ("Duplicate array spec for Cray pointee at %C");
5073 gfc_free_array_spec (as);
5079 if (cpte->as != NULL)
5081 /* Fix array spec. */
5082 m = gfc_mod_pointee_as (cpte->as);
5083 if (m == MATCH_ERROR)
5087 /* Point the Pointee at the Pointer. */
5088 cpte->cp_pointer = cptr;
5090 if (gfc_match_char (')') != MATCH_YES)
5092 gfc_error ("Expected \")\" at %C");
5095 m = gfc_match_char (',');
5097 done = true; /* Stop searching for more declarations. */
5101 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5102 || gfc_match_eos () != MATCH_YES)
5104 gfc_error ("Expected \",\" or end of statement at %C");
5112 gfc_match_external (void)
5115 gfc_clear_attr (¤t_attr);
5116 current_attr.external = 1;
5118 return attr_decl ();
5123 gfc_match_intent (void)
5127 intent = match_intent_spec ();
5128 if (intent == INTENT_UNKNOWN)
5131 gfc_clear_attr (¤t_attr);
5132 current_attr.intent = intent;
5134 return attr_decl ();
5139 gfc_match_intrinsic (void)
5142 gfc_clear_attr (¤t_attr);
5143 current_attr.intrinsic = 1;
5145 return attr_decl ();
5150 gfc_match_optional (void)
5153 gfc_clear_attr (¤t_attr);
5154 current_attr.optional = 1;
5156 return attr_decl ();
5161 gfc_match_pointer (void)
5163 gfc_gobble_whitespace ();
5164 if (gfc_peek_char () == '(')
5166 if (!gfc_option.flag_cray_pointer)
5168 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5172 return cray_pointer_decl ();
5176 gfc_clear_attr (¤t_attr);
5177 current_attr.pointer = 1;
5179 return attr_decl ();
5185 gfc_match_allocatable (void)
5187 gfc_clear_attr (¤t_attr);
5188 current_attr.allocatable = 1;
5190 return attr_decl ();
5195 gfc_match_dimension (void)
5197 gfc_clear_attr (¤t_attr);
5198 current_attr.dimension = 1;
5200 return attr_decl ();
5205 gfc_match_target (void)
5207 gfc_clear_attr (¤t_attr);
5208 current_attr.target = 1;
5210 return attr_decl ();
5214 /* Match the list of entities being specified in a PUBLIC or PRIVATE
5218 access_attr_decl (gfc_statement st)
5220 char name[GFC_MAX_SYMBOL_LEN + 1];
5221 interface_type type;
5224 gfc_intrinsic_op operator;
5227 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5232 m = gfc_match_generic_spec (&type, name, &operator);
5235 if (m == MATCH_ERROR)
5240 case INTERFACE_NAMELESS:
5241 case INTERFACE_ABSTRACT:
5244 case INTERFACE_GENERIC:
5245 if (gfc_get_symbol (name, NULL, &sym))
5248 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5249 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
5250 sym->name, NULL) == FAILURE)
5255 case INTERFACE_INTRINSIC_OP:
5256 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
5258 gfc_current_ns->operator_access[operator] =
5259 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5263 gfc_error ("Access specification of the %s operator at %C has "
5264 "already been specified", gfc_op2string (operator));
5270 case INTERFACE_USER_OP:
5271 uop = gfc_get_uop (name);
5273 if (uop->access == ACCESS_UNKNOWN)
5275 uop->access = (st == ST_PUBLIC)
5276 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5280 gfc_error ("Access specification of the .%s. operator at %C "
5281 "has already been specified", sym->name);
5288 if (gfc_match_char (',') == MATCH_NO)
5292 if (gfc_match_eos () != MATCH_YES)
5297 gfc_syntax_error (st);
5305 gfc_match_protected (void)
5310 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5312 gfc_error ("PROTECTED at %C only allowed in specification "
5313 "part of a module");
5318 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
5322 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5327 if (gfc_match_eos () == MATCH_YES)
5332 m = gfc_match_symbol (&sym, 0);
5336 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
5349 if (gfc_match_eos () == MATCH_YES)
5351 if (gfc_match_char (',') != MATCH_YES)
5358 gfc_error ("Syntax error in PROTECTED statement at %C");
5363 /* The PRIVATE statement is a bit weird in that it can be an attribute
5364 declaration, but also works as a standlone statement inside of a
5365 type declaration or a module. */
5368 gfc_match_private (gfc_statement *st)
5371 if (gfc_match ("private") != MATCH_YES)
5374 if (gfc_current_state () != COMP_MODULE
5375 && (gfc_current_state () != COMP_DERIVED
5376 || !gfc_state_stack->previous
5377 || gfc_state_stack->previous->state != COMP_MODULE))
5379 gfc_error ("PRIVATE statement at %C is only allowed in the "
5380 "specification part of a module");
5384 if (gfc_current_state () == COMP_DERIVED)
5386 if (gfc_match_eos () == MATCH_YES)
5392 gfc_syntax_error (ST_PRIVATE);
5396 if (gfc_match_eos () == MATCH_YES)
5403 return access_attr_decl (ST_PRIVATE);
5408 gfc_match_public (gfc_statement *st)
5411 if (gfc_match ("public") != MATCH_YES)
5414 if (gfc_current_state () != COMP_MODULE)
5416 gfc_error ("PUBLIC statement at %C is only allowed in the "
5417 "specification part of a module");
5421 if (gfc_match_eos () == MATCH_YES)
5428 return access_attr_decl (ST_PUBLIC);
5432 /* Workhorse for gfc_match_parameter. */
5441 m = gfc_match_symbol (&sym, 0);
5443 gfc_error ("Expected variable name at %C in PARAMETER statement");
5448 if (gfc_match_char ('=') == MATCH_NO)
5450 gfc_error ("Expected = sign in PARAMETER statement at %C");
5454 m = gfc_match_init_expr (&init);
5456 gfc_error ("Expected expression at %C in PARAMETER statement");
5460 if (sym->ts.type == BT_UNKNOWN
5461 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5467 if (gfc_check_assign_symbol (sym, init) == FAILURE
5468 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
5474 if (sym->ts.type == BT_CHARACTER
5475 && sym->ts.cl != NULL
5476 && sym->ts.cl->length != NULL
5477 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5478 && init->expr_type == EXPR_CONSTANT
5479 && init->ts.type == BT_CHARACTER
5480 && init->ts.kind == 1)
5481 gfc_set_constant_character_len (
5482 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
5488 gfc_free_expr (init);
5493 /* Match a parameter statement, with the weird syntax that these have. */
5496 gfc_match_parameter (void)
5500 if (gfc_match_char ('(') == MATCH_NO)
5509 if (gfc_match (" )%t") == MATCH_YES)
5512 if (gfc_match_char (',') != MATCH_YES)
5514 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5524 /* Save statements have a special syntax. */
5527 gfc_match_save (void)
5529 char n[GFC_MAX_SYMBOL_LEN+1];
5534 if (gfc_match_eos () == MATCH_YES)
5536 if (gfc_current_ns->seen_save)
5538 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5539 "follows previous SAVE statement")
5544 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5548 if (gfc_current_ns->save_all)
5550 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5551 "blanket SAVE statement")
5560 m = gfc_match_symbol (&sym, 0);
5564 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5576 m = gfc_match (" / %n /", &n);
5577 if (m == MATCH_ERROR)
5582 c = gfc_get_common (n, 0);
5585 gfc_current_ns->seen_save = 1;
5588 if (gfc_match_eos () == MATCH_YES)
5590 if (gfc_match_char (',') != MATCH_YES)
5597 gfc_error ("Syntax error in SAVE statement at %C");
5603 gfc_match_value (void)
5608 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
5612 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5617 if (gfc_match_eos () == MATCH_YES)
5622 m = gfc_match_symbol (&sym, 0);
5626 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5639 if (gfc_match_eos () == MATCH_YES)
5641 if (gfc_match_char (',') != MATCH_YES)
5648 gfc_error ("Syntax error in VALUE statement at %C");
5654 gfc_match_volatile (void)
5659 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
5663 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5668 if (gfc_match_eos () == MATCH_YES)
5673 /* VOLATILE is special because it can be added to host-associated
5675 m = gfc_match_symbol (&sym, 1);
5679 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
5692 if (gfc_match_eos () == MATCH_YES)
5694 if (gfc_match_char (',') != MATCH_YES)
5701 gfc_error ("Syntax error in VOLATILE statement at %C");
5706 /* Match a module procedure statement. Note that we have to modify
5707 symbols in the parent's namespace because the current one was there
5708 to receive symbols that are in an interface's formal argument list. */
5711 gfc_match_modproc (void)
5713 char name[GFC_MAX_SYMBOL_LEN + 1];
5716 gfc_namespace *module_ns;
5718 if (gfc_state_stack->state != COMP_INTERFACE
5719 || gfc_state_stack->previous == NULL
5720 || current_interface.type == INTERFACE_NAMELESS
5721 || current_interface.type == INTERFACE_ABSTRACT)
5723 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5728 module_ns = gfc_current_ns->parent;
5729 for (; module_ns; module_ns = module_ns->parent)
5730 if (module_ns->proc_name->attr.flavor == FL_MODULE)
5733 if (module_ns == NULL)
5738 m = gfc_match_name (name);
5744 if (gfc_get_symbol (name, module_ns, &sym))
5747 if (sym->attr.proc != PROC_MODULE
5748 && gfc_add_procedure (&sym->attr, PROC_MODULE,
5749 sym->name, NULL) == FAILURE)
5752 if (gfc_add_interface (sym) == FAILURE)
5755 sym->attr.mod_proc = 1;
5757 if (gfc_match_eos () == MATCH_YES)
5759 if (gfc_match_char (',') != MATCH_YES)
5766 gfc_syntax_error (ST_MODULE_PROC);
5771 /* Match the optional attribute specifiers for a type declaration.
5772 Return MATCH_ERROR if an error is encountered in one of the handled
5773 attributes (public, private, bind(c)), MATCH_NO if what's found is
5774 not a handled attribute, and MATCH_YES otherwise. TODO: More error
5775 checking on attribute conflicts needs to be done. */
5778 gfc_get_type_attr_spec (symbol_attribute *attr)
5780 /* See if the derived type is marked as private. */
5781 if (gfc_match (" , private") == MATCH_YES)
5783 if (gfc_current_state () != COMP_MODULE)
5785 gfc_error ("Derived type at %C can only be PRIVATE in the "
5786 "specification part of a module");
5790 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
5793 else if (gfc_match (" , public") == MATCH_YES)
5795 if (gfc_current_state () != COMP_MODULE)
5797 gfc_error ("Derived type at %C can only be PUBLIC in the "
5798 "specification part of a module");
5802 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
5805 else if (gfc_match(" , bind ( c )") == MATCH_YES)
5807 /* If the type is defined to be bind(c) it then needs to make
5808 sure that all fields are interoperable. This will
5809 need to be a semantic check on the finished derived type.
5810 See 15.2.3 (lines 9-12) of F2003 draft. */
5811 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
5814 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
5819 /* If we get here, something matched. */
5824 /* Match the beginning of a derived type declaration. If a type name
5825 was the result of a function, then it is possible to have a symbol
5826 already to be known as a derived type yet have no components. */
5829 gfc_match_derived_decl (void)
5831 char name[GFC_MAX_SYMBOL_LEN + 1];
5832 symbol_attribute attr;
5835 match is_type_attr_spec = MATCH_NO;
5836 bool seen_attr = false;
5838 if (gfc_current_state () == COMP_DERIVED)
5841 gfc_clear_attr (&attr);
5845 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
5846 if (is_type_attr_spec == MATCH_ERROR)
5848 if (is_type_attr_spec == MATCH_YES)
5850 } while (is_type_attr_spec == MATCH_YES);
5852 if (gfc_match (" ::") != MATCH_YES && seen_attr)
5854 gfc_error ("Expected :: in TYPE definition at %C");
5858 m = gfc_match (" %n%t", name);
5862 /* Make sure the name is not the name of an intrinsic type. */
5863 if (gfc_is_intrinsic_typename (name))
5865 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
5870 if (gfc_get_symbol (name, NULL, &sym))
5873 if (sym->ts.type != BT_UNKNOWN)
5875 gfc_error ("Derived type name '%s' at %C already has a basic type "
5876 "of %s", sym->name, gfc_typename (&sym->ts));
5880 /* The symbol may already have the derived attribute without the
5881 components. The ways this can happen is via a function
5882 definition, an INTRINSIC statement or a subtype in another
5883 derived type that is a pointer. The first part of the AND clause
5884 is true if a the symbol is not the return value of a function. */
5885 if (sym->attr.flavor != FL_DERIVED
5886 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
5889 if (sym->components != NULL || sym->attr.zero_comp)
5891 gfc_error ("Derived type definition of '%s' at %C has already been "
5892 "defined", sym->name);
5896 if (attr.access != ACCESS_UNKNOWN
5897 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
5900 /* See if the derived type was labeled as bind(c). */
5901 if (attr.is_bind_c != 0)
5902 sym->attr.is_bind_c = attr.is_bind_c;
5904 gfc_new_block = sym;
5910 /* Cray Pointees can be declared as:
5911 pointer (ipt, a (n,m,...,*))
5912 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
5913 cheat and set a constant bound of 1 for the last dimension, if this
5914 is the case. Since there is no bounds-checking for Cray Pointees,
5915 this will be okay. */
5918 gfc_mod_pointee_as (gfc_array_spec *as)
5920 as->cray_pointee = true; /* This will be useful to know later. */
5921 if (as->type == AS_ASSUMED_SIZE)
5923 as->type = AS_EXPLICIT;
5924 as->upper[as->rank - 1] = gfc_int_expr (1);
5925 as->cp_was_assumed = true;
5927 else if (as->type == AS_ASSUMED_SHAPE)
5929 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
5936 /* Match the enum definition statement, here we are trying to match
5937 the first line of enum definition statement.
5938 Returns MATCH_YES if match is found. */
5941 gfc_match_enum (void)
5945 m = gfc_match_eos ();
5949 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
5957 /* Match a variable name with an optional initializer. When this
5958 subroutine is called, a variable is expected to be parsed next.
5959 Depending on what is happening at the moment, updates either the
5960 symbol table or the current interface. */
5963 enumerator_decl (void)
5965 char name[GFC_MAX_SYMBOL_LEN + 1];
5966 gfc_expr *initializer;
5967 gfc_array_spec *as = NULL;
5975 old_locus = gfc_current_locus;
5977 /* When we get here, we've just matched a list of attributes and
5978 maybe a type and a double colon. The next thing we expect to see
5979 is the name of the symbol. */
5980 m = gfc_match_name (name);
5984 var_locus = gfc_current_locus;
5986 /* OK, we've successfully matched the declaration. Now put the
5987 symbol in the current namespace. If we fail to create the symbol,
5989 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
5995 /* The double colon must be present in order to have initializers.
5996 Otherwise the statement is ambiguous with an assignment statement. */
5999 if (gfc_match_char ('=') == MATCH_YES)
6001 m = gfc_match_init_expr (&initializer);
6004 gfc_error ("Expected an initialization expression at %C");
6013 /* If we do not have an initializer, the initialization value of the
6014 previous enumerator (stored in last_initializer) is incremented
6015 by 1 and is used to initialize the current enumerator. */
6016 if (initializer == NULL)
6017 initializer = gfc_enum_initializer (last_initializer, old_locus);
6019 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6021 gfc_error("ENUMERATOR %L not initialized with integer expression",
6024 gfc_free_enum_history ();
6028 /* Store this current initializer, for the next enumerator variable
6029 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6030 use last_initializer below. */
6031 last_initializer = initializer;
6032 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6034 /* Maintain enumerator history. */
6035 gfc_find_symbol (name, NULL, 0, &sym);
6036 create_enum_history (sym, last_initializer);
6038 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6041 /* Free stuff up and return. */
6042 gfc_free_expr (initializer);
6048 /* Match the enumerator definition statement. */
6051 gfc_match_enumerator_def (void)
6056 gfc_clear_ts (¤t_ts);
6058 m = gfc_match (" enumerator");
6062 m = gfc_match (" :: ");
6063 if (m == MATCH_ERROR)
6066 colon_seen = (m == MATCH_YES);
6068 if (gfc_current_state () != COMP_ENUM)
6070 gfc_error ("ENUM definition statement expected before %C");
6071 gfc_free_enum_history ();
6075 (¤t_ts)->type = BT_INTEGER;
6076 (¤t_ts)->kind = gfc_c_int_kind;
6078 gfc_clear_attr (¤t_attr);
6079 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
6088 m = enumerator_decl ();
6089 if (m == MATCH_ERROR)
6094 if (gfc_match_eos () == MATCH_YES)
6096 if (gfc_match_char (',') != MATCH_YES)
6100 if (gfc_current_state () == COMP_ENUM)
6102 gfc_free_enum_history ();
6103 gfc_error ("Syntax error in ENUMERATOR definition at %C");
6108 gfc_free_array_spec (current_as);