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/>. */
29 /* Macros to access allocate memory for gfc_data_variable,
30 gfc_data_value and gfc_data. */
31 #define gfc_get_data_variable() gfc_getmem (sizeof (gfc_data_variable))
32 #define gfc_get_data_value() gfc_getmem (sizeof (gfc_data_value))
33 #define gfc_get_data() gfc_getmem( sizeof (gfc_data))
36 /* This flag is set if an old-style length selector is matched
37 during a type-declaration statement. */
39 static int old_char_selector;
41 /* When variables acquire types and attributes from a declaration
42 statement, they get them from the following static variables. The
43 first part of a declaration sets these variables and the second
44 part copies these into symbol structures. */
46 static gfc_typespec current_ts;
48 static symbol_attribute current_attr;
49 static gfc_array_spec *current_as;
50 static int colon_seen;
52 /* The current binding label (if any). */
53 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
54 /* Need to know how many identifiers are on the current data declaration
55 line in case we're given the BIND(C) attribute with a NAME= specifier. */
56 static int num_idents_on_line;
57 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
58 can supply a name if the curr_binding_label is nil and NAME= was not. */
59 static int has_name_equals = 0;
61 /* Initializer of the previous enumerator. */
63 static gfc_expr *last_initializer;
65 /* History of all the enumerators is maintained, so that
66 kind values of all the enumerators could be updated depending
67 upon the maximum initialized value. */
69 typedef struct enumerator_history
72 gfc_expr *initializer;
73 struct enumerator_history *next;
77 /* Header of enum history chain. */
79 static enumerator_history *enum_history = NULL;
81 /* Pointer of enum history node containing largest initializer. */
83 static enumerator_history *max_enum = NULL;
85 /* gfc_new_block points to the symbol of a newly matched block. */
87 gfc_symbol *gfc_new_block;
89 locus gfc_function_kind_locus;
90 locus gfc_function_type_locus;
93 /********************* DATA statement subroutines *********************/
95 static bool in_match_data = false;
98 gfc_in_match_data (void)
100 return in_match_data;
104 set_in_match_data (bool set_value)
106 in_match_data = set_value;
109 /* Free a gfc_data_variable structure and everything beneath it. */
112 free_variable (gfc_data_variable *p)
114 gfc_data_variable *q;
119 gfc_free_expr (p->expr);
120 gfc_free_iterator (&p->iter, 0);
121 free_variable (p->list);
127 /* Free a gfc_data_value structure and everything beneath it. */
130 free_value (gfc_data_value *p)
137 gfc_free_expr (p->expr);
143 /* Free a list of gfc_data structures. */
146 gfc_free_data (gfc_data *p)
153 free_variable (p->var);
154 free_value (p->value);
160 /* Free all data in a namespace. */
163 gfc_free_data_all (gfc_namespace *ns)
176 static match var_element (gfc_data_variable *);
178 /* Match a list of variables terminated by an iterator and a right
182 var_list (gfc_data_variable *parent)
184 gfc_data_variable *tail, var;
187 m = var_element (&var);
188 if (m == MATCH_ERROR)
193 tail = gfc_get_data_variable ();
200 if (gfc_match_char (',') != MATCH_YES)
203 m = gfc_match_iterator (&parent->iter, 1);
206 if (m == MATCH_ERROR)
209 m = var_element (&var);
210 if (m == MATCH_ERROR)
215 tail->next = gfc_get_data_variable ();
221 if (gfc_match_char (')') != MATCH_YES)
226 gfc_syntax_error (ST_DATA);
231 /* Match a single element in a data variable list, which can be a
232 variable-iterator list. */
235 var_element (gfc_data_variable *new)
240 memset (new, 0, sizeof (gfc_data_variable));
242 if (gfc_match_char ('(') == MATCH_YES)
243 return var_list (new);
245 m = gfc_match_variable (&new->expr, 0);
249 sym = new->expr->symtree->n.sym;
251 if (!sym->attr.function && gfc_current_ns->parent
252 && gfc_current_ns->parent == sym->ns)
254 gfc_error ("Host associated variable '%s' may not be in the DATA "
255 "statement at %C", sym->name);
259 if (gfc_current_state () != COMP_BLOCK_DATA
260 && sym->attr.in_common
261 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
262 "common block variable '%s' in DATA statement at %C",
263 sym->name) == FAILURE)
266 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
273 /* Match the top-level list of data variables. */
276 top_var_list (gfc_data *d)
278 gfc_data_variable var, *tail, *new;
285 m = var_element (&var);
288 if (m == MATCH_ERROR)
291 new = gfc_get_data_variable ();
301 if (gfc_match_char ('/') == MATCH_YES)
303 if (gfc_match_char (',') != MATCH_YES)
310 gfc_syntax_error (ST_DATA);
311 gfc_free_data_all (gfc_current_ns);
317 match_data_constant (gfc_expr **result)
319 char name[GFC_MAX_SYMBOL_LEN + 1];
325 m = gfc_match_literal_constant (&expr, 1);
332 if (m == MATCH_ERROR)
335 m = gfc_match_null (result);
339 old_loc = gfc_current_locus;
341 /* Should this be a structure component, try to match it
342 before matching a name. */
343 m = gfc_match_rvalue (result);
344 if (m == MATCH_ERROR)
347 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
349 if (gfc_simplify_expr (*result, 0) == FAILURE)
354 gfc_current_locus = old_loc;
356 m = gfc_match_name (name);
360 if (gfc_find_symbol (name, NULL, 1, &sym))
364 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
366 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
370 else if (sym->attr.flavor == FL_DERIVED)
371 return gfc_match_structure_constructor (sym, result);
373 /* Check to see if the value is an initialization array expression. */
374 if (sym->value->expr_type == EXPR_ARRAY)
376 gfc_current_locus = old_loc;
378 m = gfc_match_init_expr (result);
379 if (m == MATCH_ERROR)
384 if (gfc_simplify_expr (*result, 0) == FAILURE)
387 if ((*result)->expr_type == EXPR_CONSTANT)
391 gfc_error ("Invalid initializer %s in Data statement at %C", name);
397 *result = gfc_copy_expr (sym->value);
402 /* Match a list of values in a DATA statement. The leading '/' has
403 already been seen at this point. */
406 top_val_list (gfc_data *data)
408 gfc_data_value *new, *tail;
416 m = match_data_constant (&expr);
419 if (m == MATCH_ERROR)
422 new = gfc_get_data_value ();
423 mpz_init (new->repeat);
432 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
435 mpz_set_ui (tail->repeat, 1);
439 if (expr->ts.type == BT_INTEGER)
440 mpz_set (tail->repeat, expr->value.integer);
441 gfc_free_expr (expr);
443 m = match_data_constant (&tail->expr);
446 if (m == MATCH_ERROR)
450 if (gfc_match_char ('/') == MATCH_YES)
452 if (gfc_match_char (',') == MATCH_NO)
459 gfc_syntax_error (ST_DATA);
460 gfc_free_data_all (gfc_current_ns);
465 /* Matches an old style initialization. */
468 match_old_style_init (const char *name)
475 /* Set up data structure to hold initializers. */
476 gfc_find_sym_tree (name, NULL, 0, &st);
479 newdata = gfc_get_data ();
480 newdata->var = gfc_get_data_variable ();
481 newdata->var->expr = gfc_get_variable_expr (st);
482 newdata->where = gfc_current_locus;
484 /* Match initial value list. This also eats the terminal '/'. */
485 m = top_val_list (newdata);
494 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
499 /* Mark the variable as having appeared in a data statement. */
500 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
506 /* Chain in namespace list of DATA initializers. */
507 newdata->next = gfc_current_ns->data;
508 gfc_current_ns->data = newdata;
514 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
515 we are matching a DATA statement and are therefore issuing an error
516 if we encounter something unexpected, if not, we're trying to match
517 an old-style initialization expression of the form INTEGER I /2/. */
520 gfc_match_data (void)
525 set_in_match_data (true);
529 new = gfc_get_data ();
530 new->where = gfc_current_locus;
532 m = top_var_list (new);
536 m = top_val_list (new);
540 new->next = gfc_current_ns->data;
541 gfc_current_ns->data = new;
543 if (gfc_match_eos () == MATCH_YES)
546 gfc_match_char (','); /* Optional comma */
549 set_in_match_data (false);
553 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
560 set_in_match_data (false);
566 /************************ Declaration statements *********************/
568 /* Match an intent specification. Since this can only happen after an
569 INTENT word, a legal intent-spec must follow. */
572 match_intent_spec (void)
575 if (gfc_match (" ( in out )") == MATCH_YES)
577 if (gfc_match (" ( in )") == MATCH_YES)
579 if (gfc_match (" ( out )") == MATCH_YES)
582 gfc_error ("Bad INTENT specification at %C");
583 return INTENT_UNKNOWN;
587 /* Matches a character length specification, which is either a
588 specification expression or a '*'. */
591 char_len_param_value (gfc_expr **expr)
595 if (gfc_match_char ('*') == MATCH_YES)
601 m = gfc_match_expr (expr);
602 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
604 if ((*expr)->value.function.actual
605 && (*expr)->value.function.actual->expr->symtree)
608 e = (*expr)->value.function.actual->expr;
609 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
610 && e->expr_type == EXPR_VARIABLE)
612 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
614 if (e->symtree->n.sym->ts.type == BT_CHARACTER
615 && e->symtree->n.sym->ts.cl
616 && e->symtree->n.sym->ts.cl->length->ts.type == BT_UNKNOWN)
624 gfc_error ("Conflict in attributes of function argument at %C");
629 /* A character length is a '*' followed by a literal integer or a
630 char_len_param_value in parenthesis. */
633 match_char_length (gfc_expr **expr)
638 m = gfc_match_char ('*');
642 m = gfc_match_small_literal_int (&length, NULL);
643 if (m == MATCH_ERROR)
648 *expr = gfc_int_expr (length);
652 if (gfc_match_char ('(') == MATCH_NO)
655 m = char_len_param_value (expr);
656 if (m == MATCH_ERROR)
661 if (gfc_match_char (')') == MATCH_NO)
663 gfc_free_expr (*expr);
671 gfc_error ("Syntax error in character length specification at %C");
676 /* Special subroutine for finding a symbol. Check if the name is found
677 in the current name space. If not, and we're compiling a function or
678 subroutine and the parent compilation unit is an interface, then check
679 to see if the name we've been given is the name of the interface
680 (located in another namespace). */
683 find_special (const char *name, gfc_symbol **result)
688 i = gfc_get_symbol (name, NULL, result);
692 if (gfc_current_state () != COMP_SUBROUTINE
693 && gfc_current_state () != COMP_FUNCTION)
696 s = gfc_state_stack->previous;
700 if (s->state != COMP_INTERFACE)
703 goto end; /* Nameless interface. */
705 if (strcmp (name, s->sym->name) == 0)
716 /* Special subroutine for getting a symbol node associated with a
717 procedure name, used in SUBROUTINE and FUNCTION statements. The
718 symbol is created in the parent using with symtree node in the
719 child unit pointing to the symbol. If the current namespace has no
720 parent, then the symbol is just created in the current unit. */
723 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
729 /* Module functions have to be left in their own namespace because
730 they have potentially (almost certainly!) already been referenced.
731 In this sense, they are rather like external functions. This is
732 fixed up in resolve.c(resolve_entries), where the symbol name-
733 space is set to point to the master function, so that the fake
734 result mechanism can work. */
735 if (module_fcn_entry)
737 /* Present if entry is declared to be a module procedure. */
738 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
741 rc = gfc_get_symbol (name, NULL, result);
742 else if (!gfc_get_symbol (name, NULL, &sym) && sym
743 && (*result)->ts.type == BT_UNKNOWN
744 && sym->attr.flavor == FL_UNKNOWN)
745 /* Pick up the typespec for the entry, if declared in the function
746 body. Note that this symbol is FL_UNKNOWN because it will
747 only have appeared in a type declaration. The local symtree
748 is set to point to the module symbol and a unique symtree
749 to the local version. This latter ensures a correct clearing
752 /* If the ENTRY proceeds its specification, we need to ensure
753 that this does not raise a "has no IMPLICIT type" error. */
754 if (sym->ts.type == BT_UNKNOWN)
755 sym->attr.untyped = 1;
757 (*result)->ts = sym->ts;
759 /* Put the symbol in the procedure namespace so that, should
760 the ENTRY preceed its specification, the specification
762 (*result)->ns = gfc_current_ns;
764 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
766 st = gfc_get_unique_symtree (gfc_current_ns);
771 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
777 gfc_current_ns->refs++;
779 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
781 /* Trap another encompassed procedure with the same name. All
782 these conditions are necessary to avoid picking up an entry
783 whose name clashes with that of the encompassing procedure;
784 this is handled using gsymbols to register unique,globally
786 if (sym->attr.flavor != 0
787 && sym->attr.proc != 0
788 && (sym->attr.subroutine || sym->attr.function)
789 && sym->attr.if_source != IFSRC_UNKNOWN)
790 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
791 name, &sym->declared_at);
793 /* Trap a procedure with a name the same as interface in the
794 encompassing scope. */
795 if (sym->attr.generic != 0
796 && (sym->attr.subroutine || sym->attr.function)
797 && !sym->attr.mod_proc)
798 gfc_error_now ("Name '%s' at %C is already defined"
799 " as a generic interface at %L",
800 name, &sym->declared_at);
802 /* Trap declarations of attributes in encompassing scope. The
803 signature for this is that ts.kind is set. Legitimate
804 references only set ts.type. */
805 if (sym->ts.kind != 0
806 && !sym->attr.implicit_type
807 && sym->attr.proc == 0
808 && gfc_current_ns->parent != NULL
809 && sym->attr.access == 0
810 && !module_fcn_entry)
811 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
812 "and must not have attributes declared at %L",
813 name, &sym->declared_at);
816 if (gfc_current_ns->parent == NULL || *result == NULL)
819 /* Module function entries will already have a symtree in
820 the current namespace but will need one at module level. */
821 if (module_fcn_entry)
823 /* Present if entry is declared to be a module procedure. */
824 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
826 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
829 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
834 /* See if the procedure should be a module procedure. */
836 if (((sym->ns->proc_name != NULL
837 && sym->ns->proc_name->attr.flavor == FL_MODULE
838 && sym->attr.proc != PROC_MODULE)
839 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
840 && gfc_add_procedure (&sym->attr, PROC_MODULE,
841 sym->name, NULL) == FAILURE)
848 /* Verify that the given symbol representing a parameter is C
849 interoperable, by checking to see if it was marked as such after
850 its declaration. If the given symbol is not interoperable, a
851 warning is reported, thus removing the need to return the status to
852 the calling function. The standard does not require the user use
853 one of the iso_c_binding named constants to declare an
854 interoperable parameter, but we can't be sure if the param is C
855 interop or not if the user doesn't. For example, integer(4) may be
856 legal Fortran, but doesn't have meaning in C. It may interop with
857 a number of the C types, which causes a problem because the
858 compiler can't know which one. This code is almost certainly not
859 portable, and the user will get what they deserve if the C type
860 across platforms isn't always interoperable with integer(4). If
861 the user had used something like integer(c_int) or integer(c_long),
862 the compiler could have automatically handled the varying sizes
866 verify_c_interop_param (gfc_symbol *sym)
868 int is_c_interop = 0;
869 try retval = SUCCESS;
871 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
872 Don't repeat the checks here. */
873 if (sym->attr.implicit_type)
876 /* For subroutines or functions that are passed to a BIND(C) procedure,
877 they're interoperable if they're BIND(C) and their params are all
879 if (sym->attr.flavor == FL_PROCEDURE)
881 if (sym->attr.is_bind_c == 0)
883 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
884 "attribute to be C interoperable", sym->name,
885 &(sym->declared_at));
891 if (sym->attr.is_c_interop == 1)
892 /* We've already checked this procedure; don't check it again. */
895 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
900 /* See if we've stored a reference to a procedure that owns sym. */
901 if (sym->ns != NULL && sym->ns->proc_name != NULL)
903 if (sym->ns->proc_name->attr.is_bind_c == 1)
906 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
909 if (is_c_interop != 1)
911 /* Make personalized messages to give better feedback. */
912 if (sym->ts.type == BT_DERIVED)
913 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
914 " procedure '%s' but is not C interoperable "
915 "because derived type '%s' is not C interoperable",
916 sym->name, &(sym->declared_at),
917 sym->ns->proc_name->name,
918 sym->ts.derived->name);
920 gfc_warning ("Variable '%s' at %L is a parameter to the "
921 "BIND(C) procedure '%s' but may not be C "
923 sym->name, &(sym->declared_at),
924 sym->ns->proc_name->name);
927 /* Character strings are only C interoperable if they have a
929 if (sym->ts.type == BT_CHARACTER)
931 gfc_charlen *cl = sym->ts.cl;
932 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
933 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
935 gfc_error ("Character argument '%s' at %L "
936 "must be length 1 because "
937 "procedure '%s' is BIND(C)",
938 sym->name, &sym->declared_at,
939 sym->ns->proc_name->name);
944 /* We have to make sure that any param to a bind(c) routine does
945 not have the allocatable, pointer, or optional attributes,
946 according to J3/04-007, section 5.1. */
947 if (sym->attr.allocatable == 1)
949 gfc_error ("Variable '%s' at %L cannot have the "
950 "ALLOCATABLE attribute because procedure '%s'"
951 " is BIND(C)", sym->name, &(sym->declared_at),
952 sym->ns->proc_name->name);
956 if (sym->attr.pointer == 1)
958 gfc_error ("Variable '%s' at %L cannot have the "
959 "POINTER attribute because procedure '%s'"
960 " is BIND(C)", sym->name, &(sym->declared_at),
961 sym->ns->proc_name->name);
965 if (sym->attr.optional == 1)
967 gfc_error ("Variable '%s' at %L cannot have the "
968 "OPTIONAL attribute because procedure '%s'"
969 " is BIND(C)", sym->name, &(sym->declared_at),
970 sym->ns->proc_name->name);
974 /* Make sure that if it has the dimension attribute, that it is
975 either assumed size or explicit shape. */
978 if (sym->as->type == AS_ASSUMED_SHAPE)
980 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
981 "argument to the procedure '%s' at %L because "
982 "the procedure is BIND(C)", sym->name,
983 &(sym->declared_at), sym->ns->proc_name->name,
984 &(sym->ns->proc_name->declared_at));
988 if (sym->as->type == AS_DEFERRED)
990 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
991 "argument to the procedure '%s' at %L because "
992 "the procedure is BIND(C)", sym->name,
993 &(sym->declared_at), sym->ns->proc_name->name,
994 &(sym->ns->proc_name->declared_at));
1005 /* Function called by variable_decl() that adds a name to the symbol table. */
1008 build_sym (const char *name, gfc_charlen *cl,
1009 gfc_array_spec **as, locus *var_locus)
1011 symbol_attribute attr;
1014 if (gfc_get_symbol (name, NULL, &sym))
1017 /* Start updating the symbol table. Add basic type attribute if present. */
1018 if (current_ts.type != BT_UNKNOWN
1019 && (sym->attr.implicit_type == 0
1020 || !gfc_compare_types (&sym->ts, ¤t_ts))
1021 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
1024 if (sym->ts.type == BT_CHARACTER)
1027 /* Add dimension attribute if present. */
1028 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1032 /* Add attribute to symbol. The copy is so that we can reset the
1033 dimension attribute. */
1034 attr = current_attr;
1037 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1040 /* Finish any work that may need to be done for the binding label,
1041 if it's a bind(c). The bind(c) attr is found before the symbol
1042 is made, and before the symbol name (for data decls), so the
1043 current_ts is holding the binding label, or nothing if the
1044 name= attr wasn't given. Therefore, test here if we're dealing
1045 with a bind(c) and make sure the binding label is set correctly. */
1046 if (sym->attr.is_bind_c == 1)
1048 if (sym->binding_label[0] == '\0')
1050 /* Set the binding label and verify that if a NAME= was specified
1051 then only one identifier was in the entity-decl-list. */
1052 if (set_binding_label (sym->binding_label, sym->name,
1053 num_idents_on_line) == FAILURE)
1058 /* See if we know we're in a common block, and if it's a bind(c)
1059 common then we need to make sure we're an interoperable type. */
1060 if (sym->attr.in_common == 1)
1062 /* Test the common block object. */
1063 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1064 && sym->ts.is_c_interop != 1)
1066 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1067 "must be declared with a C interoperable "
1068 "kind since common block '%s' is BIND(C)",
1069 sym->name, sym->common_block->name,
1070 sym->common_block->name);
1075 sym->attr.implied_index = 0;
1081 /* Set character constant to the given length. The constant will be padded or
1085 gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
1090 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1091 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
1093 slen = expr->value.character.length;
1096 s = gfc_getmem (len + 1);
1097 memcpy (s, expr->value.character.string, MIN (len, slen));
1099 memset (&s[slen], ' ', len - slen);
1101 if (gfc_option.warn_character_truncation && slen > len)
1102 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1103 "(%d/%d)", &expr->where, slen, len);
1105 /* Apply the standard by 'hand' otherwise it gets cleared for
1107 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
1108 gfc_error_now ("The CHARACTER elements of the array constructor "
1109 "at %L must have the same length (%d/%d)",
1110 &expr->where, slen, len);
1113 gfc_free (expr->value.character.string);
1114 expr->value.character.string = s;
1115 expr->value.character.length = len;
1120 /* Function to create and update the enumerator history
1121 using the information passed as arguments.
1122 Pointer "max_enum" is also updated, to point to
1123 enum history node containing largest initializer.
1125 SYM points to the symbol node of enumerator.
1126 INIT points to its enumerator value. */
1129 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1131 enumerator_history *new_enum_history;
1132 gcc_assert (sym != NULL && init != NULL);
1134 new_enum_history = gfc_getmem (sizeof (enumerator_history));
1136 new_enum_history->sym = sym;
1137 new_enum_history->initializer = init;
1138 new_enum_history->next = NULL;
1140 if (enum_history == NULL)
1142 enum_history = new_enum_history;
1143 max_enum = enum_history;
1147 new_enum_history->next = enum_history;
1148 enum_history = new_enum_history;
1150 if (mpz_cmp (max_enum->initializer->value.integer,
1151 new_enum_history->initializer->value.integer) < 0)
1152 max_enum = new_enum_history;
1157 /* Function to free enum kind history. */
1160 gfc_free_enum_history (void)
1162 enumerator_history *current = enum_history;
1163 enumerator_history *next;
1165 while (current != NULL)
1167 next = current->next;
1172 enum_history = NULL;
1176 /* Function called by variable_decl() that adds an initialization
1177 expression to a symbol. */
1180 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1182 symbol_attribute attr;
1187 if (find_special (name, &sym))
1192 /* If this symbol is confirming an implicit parameter type,
1193 then an initialization expression is not allowed. */
1194 if (attr.flavor == FL_PARAMETER
1195 && sym->value != NULL
1198 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1205 /* An initializer is required for PARAMETER declarations. */
1206 if (attr.flavor == FL_PARAMETER)
1208 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1214 /* If a variable appears in a DATA block, it cannot have an
1218 gfc_error ("Variable '%s' at %C with an initializer already "
1219 "appears in a DATA statement", sym->name);
1223 /* Check if the assignment can happen. This has to be put off
1224 until later for a derived type variable. */
1225 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1226 && gfc_check_assign_symbol (sym, init) == FAILURE)
1229 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1231 /* Update symbol character length according initializer. */
1232 if (sym->ts.cl->length == NULL)
1235 /* If there are multiple CHARACTER variables declared on the
1236 same line, we don't want them to share the same length. */
1237 sym->ts.cl = gfc_get_charlen ();
1238 sym->ts.cl->next = gfc_current_ns->cl_list;
1239 gfc_current_ns->cl_list = sym->ts.cl;
1241 if (sym->attr.flavor == FL_PARAMETER)
1243 if (init->expr_type == EXPR_CONSTANT)
1245 clen = init->value.character.length;
1246 sym->ts.cl->length = gfc_int_expr (clen);
1248 else if (init->expr_type == EXPR_ARRAY)
1250 gfc_expr *p = init->value.constructor->expr;
1251 clen = p->value.character.length;
1252 sym->ts.cl->length = gfc_int_expr (clen);
1254 else if (init->ts.cl && init->ts.cl->length)
1255 sym->ts.cl->length =
1256 gfc_copy_expr (sym->value->ts.cl->length);
1259 /* Update initializer character length according symbol. */
1260 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1262 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1263 gfc_constructor * p;
1265 if (init->expr_type == EXPR_CONSTANT)
1266 gfc_set_constant_character_len (len, init, false);
1267 else if (init->expr_type == EXPR_ARRAY)
1269 /* Build a new charlen to prevent simplification from
1270 deleting the length before it is resolved. */
1271 init->ts.cl = gfc_get_charlen ();
1272 init->ts.cl->next = gfc_current_ns->cl_list;
1273 gfc_current_ns->cl_list = sym->ts.cl;
1274 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
1276 for (p = init->value.constructor; p; p = p->next)
1277 gfc_set_constant_character_len (len, p->expr, false);
1282 /* Need to check if the expression we initialized this
1283 to was one of the iso_c_binding named constants. If so,
1284 and we're a parameter (constant), let it be iso_c.
1286 integer(c_int), parameter :: my_int = c_int
1287 integer(my_int) :: my_int_2
1288 If we mark my_int as iso_c (since we can see it's value
1289 is equal to one of the named constants), then my_int_2
1290 will be considered C interoperable. */
1291 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1293 sym->ts.is_iso_c |= init->ts.is_iso_c;
1294 sym->ts.is_c_interop |= init->ts.is_c_interop;
1295 /* attr bits needed for module files. */
1296 sym->attr.is_iso_c |= init->ts.is_iso_c;
1297 sym->attr.is_c_interop |= init->ts.is_c_interop;
1298 if (init->ts.is_iso_c)
1299 sym->ts.f90_type = init->ts.f90_type;
1302 /* Add initializer. Make sure we keep the ranks sane. */
1303 if (sym->attr.dimension && init->rank == 0)
1309 if (sym->attr.flavor == FL_PARAMETER
1310 && init->expr_type == EXPR_CONSTANT
1311 && spec_size (sym->as, &size) == SUCCESS
1312 && mpz_cmp_si (size, 0) > 0)
1314 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1317 array->value.constructor = c = NULL;
1318 for (n = 0; n < (int)mpz_get_si (size); n++)
1320 if (array->value.constructor == NULL)
1322 array->value.constructor = c = gfc_get_constructor ();
1327 c->next = gfc_get_constructor ();
1329 c->expr = gfc_copy_expr (init);
1333 array->shape = gfc_get_shape (sym->as->rank);
1334 for (n = 0; n < sym->as->rank; n++)
1335 spec_dimen_size (sym->as, n, &array->shape[n]);
1340 init->rank = sym->as->rank;
1344 if (sym->attr.save == SAVE_NONE)
1345 sym->attr.save = SAVE_IMPLICIT;
1353 /* Function called by variable_decl() that adds a name to a structure
1357 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1358 gfc_array_spec **as)
1362 /* If the current symbol is of the same derived type that we're
1363 constructing, it must have the pointer attribute. */
1364 if (current_ts.type == BT_DERIVED
1365 && current_ts.derived == gfc_current_block ()
1366 && current_attr.pointer == 0)
1368 gfc_error ("Component at %C must have the POINTER attribute");
1372 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1374 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1376 gfc_error ("Array component of structure at %C must have explicit "
1377 "or deferred shape");
1382 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1387 gfc_set_component_attr (c, ¤t_attr);
1389 c->initializer = *init;
1397 /* Should this ever get more complicated, combine with similar section
1398 in add_init_expr_to_sym into a separate function. */
1399 if (c->ts.type == BT_CHARACTER && !c->pointer && c->initializer)
1401 int len = mpz_get_si (c->ts.cl->length->value.integer);
1403 if (c->initializer->expr_type == EXPR_CONSTANT)
1404 gfc_set_constant_character_len (len, c->initializer, false);
1405 else if (mpz_cmp (c->ts.cl->length->value.integer,
1406 c->initializer->ts.cl->length->value.integer))
1408 gfc_constructor *ctor = c->initializer->value.constructor;
1409 for (;ctor ; ctor = ctor->next)
1410 if (ctor->expr->expr_type == EXPR_CONSTANT)
1411 gfc_set_constant_character_len (len, ctor->expr, true);
1415 /* Check array components. */
1420 gfc_error ("Allocatable component at %C must be an array");
1429 if (c->as->type != AS_DEFERRED)
1431 gfc_error ("Pointer array component of structure at %C must have a "
1436 else if (c->allocatable)
1438 if (c->as->type != AS_DEFERRED)
1440 gfc_error ("Allocatable component of structure at %C must have a "
1447 if (c->as->type != AS_EXPLICIT)
1449 gfc_error ("Array component of structure at %C must have an "
1459 /* Match a 'NULL()', and possibly take care of some side effects. */
1462 gfc_match_null (gfc_expr **result)
1468 m = gfc_match (" null ( )");
1472 /* The NULL symbol now has to be/become an intrinsic function. */
1473 if (gfc_get_symbol ("null", NULL, &sym))
1475 gfc_error ("NULL() initialization at %C is ambiguous");
1479 gfc_intrinsic_symbol (sym);
1481 if (sym->attr.proc != PROC_INTRINSIC
1482 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1483 sym->name, NULL) == FAILURE
1484 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1487 e = gfc_get_expr ();
1488 e->where = gfc_current_locus;
1489 e->expr_type = EXPR_NULL;
1490 e->ts.type = BT_UNKNOWN;
1498 /* Match a variable name with an optional initializer. When this
1499 subroutine is called, a variable is expected to be parsed next.
1500 Depending on what is happening at the moment, updates either the
1501 symbol table or the current interface. */
1504 variable_decl (int elem)
1506 char name[GFC_MAX_SYMBOL_LEN + 1];
1507 gfc_expr *initializer, *char_len;
1509 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1520 old_locus = gfc_current_locus;
1522 /* When we get here, we've just matched a list of attributes and
1523 maybe a type and a double colon. The next thing we expect to see
1524 is the name of the symbol. */
1525 m = gfc_match_name (name);
1529 var_locus = gfc_current_locus;
1531 /* Now we could see the optional array spec. or character length. */
1532 m = gfc_match_array_spec (&as);
1533 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1534 cp_as = gfc_copy_array_spec (as);
1535 else if (m == MATCH_ERROR)
1539 as = gfc_copy_array_spec (current_as);
1544 if (current_ts.type == BT_CHARACTER)
1546 switch (match_char_length (&char_len))
1549 cl = gfc_get_charlen ();
1550 cl->next = gfc_current_ns->cl_list;
1551 gfc_current_ns->cl_list = cl;
1553 cl->length = char_len;
1556 /* Non-constant lengths need to be copied after the first
1557 element. Also copy assumed lengths. */
1560 && (current_ts.cl->length == NULL
1561 || current_ts.cl->length->expr_type != EXPR_CONSTANT))
1563 cl = gfc_get_charlen ();
1564 cl->next = gfc_current_ns->cl_list;
1565 gfc_current_ns->cl_list = cl;
1566 cl->length = gfc_copy_expr (current_ts.cl->length);
1578 /* If this symbol has already shown up in a Cray Pointer declaration,
1579 then we want to set the type & bail out. */
1580 if (gfc_option.flag_cray_pointer)
1582 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1583 if (sym != NULL && sym->attr.cray_pointee)
1585 sym->ts.type = current_ts.type;
1586 sym->ts.kind = current_ts.kind;
1588 sym->ts.derived = current_ts.derived;
1589 sym->ts.is_c_interop = current_ts.is_c_interop;
1590 sym->ts.is_iso_c = current_ts.is_iso_c;
1593 /* Check to see if we have an array specification. */
1596 if (sym->as != NULL)
1598 gfc_error ("Duplicate array spec for Cray pointee at %C");
1599 gfc_free_array_spec (cp_as);
1605 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1606 gfc_internal_error ("Couldn't set pointee array spec.");
1608 /* Fix the array spec. */
1609 m = gfc_mod_pointee_as (sym->as);
1610 if (m == MATCH_ERROR)
1618 gfc_free_array_spec (cp_as);
1623 /* OK, we've successfully matched the declaration. Now put the
1624 symbol in the current namespace, because it might be used in the
1625 optional initialization expression for this symbol, e.g. this is
1628 integer, parameter :: i = huge(i)
1630 This is only true for parameters or variables of a basic type.
1631 For components of derived types, it is not true, so we don't
1632 create a symbol for those yet. If we fail to create the symbol,
1634 if (gfc_current_state () != COMP_DERIVED
1635 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1641 /* An interface body specifies all of the procedure's
1642 characteristics and these shall be consistent with those
1643 specified in the procedure definition, except that the interface
1644 may specify a procedure that is not pure if the procedure is
1645 defined to be pure(12.3.2). */
1646 if (current_ts.type == BT_DERIVED
1647 && gfc_current_ns->proc_name
1648 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1649 && current_ts.derived->ns != gfc_current_ns)
1652 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.derived->name);
1653 if (!(current_ts.derived->attr.imported
1655 && st->n.sym == current_ts.derived)
1656 && !gfc_current_ns->has_import_set)
1658 gfc_error ("the type of '%s' at %C has not been declared within the "
1665 /* In functions that have a RESULT variable defined, the function
1666 name always refers to function calls. Therefore, the name is
1667 not allowed to appear in specification statements. */
1668 if (gfc_current_state () == COMP_FUNCTION
1669 && gfc_current_block () != NULL
1670 && gfc_current_block ()->result != NULL
1671 && gfc_current_block ()->result != gfc_current_block ()
1672 && strcmp (gfc_current_block ()->name, name) == 0)
1674 gfc_error ("Function name '%s' not allowed at %C", name);
1679 /* We allow old-style initializations of the form
1680 integer i /2/, j(4) /3*3, 1/
1681 (if no colon has been seen). These are different from data
1682 statements in that initializers are only allowed to apply to the
1683 variable immediately preceding, i.e.
1685 is not allowed. Therefore we have to do some work manually, that
1686 could otherwise be left to the matchers for DATA statements. */
1688 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1690 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1691 "initialization at %C") == FAILURE)
1694 return match_old_style_init (name);
1697 /* The double colon must be present in order to have initializers.
1698 Otherwise the statement is ambiguous with an assignment statement. */
1701 if (gfc_match (" =>") == MATCH_YES)
1703 if (!current_attr.pointer)
1705 gfc_error ("Initialization at %C isn't for a pointer variable");
1710 m = gfc_match_null (&initializer);
1713 gfc_error ("Pointer initialization requires a NULL() at %C");
1717 if (gfc_pure (NULL))
1719 gfc_error ("Initialization of pointer at %C is not allowed in "
1720 "a PURE procedure");
1728 else if (gfc_match_char ('=') == MATCH_YES)
1730 if (current_attr.pointer)
1732 gfc_error ("Pointer initialization at %C requires '=>', "
1738 m = gfc_match_init_expr (&initializer);
1741 gfc_error ("Expected an initialization expression at %C");
1745 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1747 gfc_error ("Initialization of variable at %C is not allowed in "
1748 "a PURE procedure");
1757 if (initializer != NULL && current_attr.allocatable
1758 && gfc_current_state () == COMP_DERIVED)
1760 gfc_error ("Initialization of allocatable component at %C is not "
1766 /* Add the initializer. Note that it is fine if initializer is
1767 NULL here, because we sometimes also need to check if a
1768 declaration *must* have an initialization expression. */
1769 if (gfc_current_state () != COMP_DERIVED)
1770 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1773 if (current_ts.type == BT_DERIVED
1774 && !current_attr.pointer && !initializer)
1775 initializer = gfc_default_initializer (¤t_ts);
1776 t = build_struct (name, cl, &initializer, &as);
1779 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1782 /* Free stuff up and return. */
1783 gfc_free_expr (initializer);
1784 gfc_free_array_spec (as);
1790 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1791 This assumes that the byte size is equal to the kind number for
1792 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1795 gfc_match_old_kind_spec (gfc_typespec *ts)
1800 if (gfc_match_char ('*') != MATCH_YES)
1803 m = gfc_match_small_literal_int (&ts->kind, NULL);
1807 original_kind = ts->kind;
1809 /* Massage the kind numbers for complex types. */
1810 if (ts->type == BT_COMPLEX)
1814 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1815 gfc_basic_typename (ts->type), original_kind);
1821 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1823 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1824 gfc_basic_typename (ts->type), original_kind);
1828 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1829 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1836 /* Match a kind specification. Since kinds are generally optional, we
1837 usually return MATCH_NO if something goes wrong. If a "kind="
1838 string is found, then we know we have an error. */
1841 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
1853 where = loc = gfc_current_locus;
1858 if (gfc_match_char ('(') == MATCH_NO)
1861 /* Also gobbles optional text. */
1862 if (gfc_match (" kind = ") == MATCH_YES)
1865 loc = gfc_current_locus;
1868 n = gfc_match_init_expr (&e);
1872 if (gfc_current_state () == COMP_INTERFACE
1873 || gfc_current_state () == COMP_NONE
1874 || gfc_current_state () == COMP_CONTAINS)
1876 /* Signal using kind = -1 that the expression might include
1877 use associated or imported parameters and try again after
1878 the specification expressions..... */
1879 if (gfc_match_char (')') != MATCH_YES)
1881 gfc_error ("Missing right parenthesis at %C");
1888 gfc_function_kind_locus = loc;
1889 gfc_undo_symbols ();
1894 /* ....or else, the match is real. */
1896 gfc_error ("Expected initialization expression at %C");
1904 gfc_error ("Expected scalar initialization expression at %C");
1909 msg = gfc_extract_int (e, &ts->kind);
1917 /* Before throwing away the expression, let's see if we had a
1918 C interoperable kind (and store the fact). */
1919 if (e->ts.is_c_interop == 1)
1921 /* Mark this as c interoperable if being declared with one
1922 of the named constants from iso_c_binding. */
1923 ts->is_c_interop = e->ts.is_iso_c;
1924 ts->f90_type = e->ts.f90_type;
1930 /* Ignore errors to this point, if we've gotten here. This means
1931 we ignore the m=MATCH_ERROR from above. */
1932 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1934 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1935 gfc_basic_typename (ts->type));
1936 gfc_current_locus = where;
1940 gfc_gobble_whitespace ();
1941 if ((c = gfc_next_char ()) != ')' && (ts->type != BT_CHARACTER || c != ','))
1943 if (ts->type == BT_CHARACTER)
1944 gfc_error ("Missing right parenthesis or comma at %C");
1946 gfc_error ("Missing right parenthesis at %C");
1950 /* All tests passed. */
1953 if(m == MATCH_ERROR)
1954 gfc_current_locus = where;
1956 /* Return what we know from the test(s). */
1961 gfc_current_locus = where;
1967 match_char_kind (int * kind, int * is_iso_c)
1976 where = gfc_current_locus;
1978 n = gfc_match_init_expr (&e);
1981 && (gfc_current_state () == COMP_INTERFACE
1982 || gfc_current_state () == COMP_NONE
1983 || gfc_current_state () == COMP_CONTAINS))
1985 /* Signal using kind = -1 that the expression might include
1986 use-associated or imported parameters and try again after
1987 the specification expressions. */
1990 gfc_function_kind_locus = where;
1991 gfc_undo_symbols ();
1996 gfc_error ("Expected initialization expression at %C");
2002 gfc_error ("Expected scalar initialization expression at %C");
2007 msg = gfc_extract_int (e, kind);
2008 *is_iso_c = e->ts.is_iso_c;
2018 /* Ignore errors to this point, if we've gotten here. This means
2019 we ignore the m=MATCH_ERROR from above. */
2020 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2022 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2026 /* All tests passed. */
2029 if (m == MATCH_ERROR)
2030 gfc_current_locus = where;
2032 /* Return what we know from the test(s). */
2037 gfc_current_locus = where;
2041 /* Match the various kind/length specifications in a CHARACTER
2042 declaration. We don't return MATCH_NO. */
2045 match_char_spec (gfc_typespec *ts)
2047 int kind, seen_length, is_iso_c;
2057 /* Try the old-style specification first. */
2058 old_char_selector = 0;
2060 m = match_char_length (&len);
2064 old_char_selector = 1;
2069 m = gfc_match_char ('(');
2072 m = MATCH_YES; /* Character without length is a single char. */
2076 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2077 if (gfc_match (" kind =") == MATCH_YES)
2079 m = match_char_kind (&kind, &is_iso_c);
2081 if (m == MATCH_ERROR)
2086 if (gfc_match (" , len =") == MATCH_NO)
2089 m = char_len_param_value (&len);
2092 if (m == MATCH_ERROR)
2099 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2100 if (gfc_match (" len =") == MATCH_YES)
2102 m = char_len_param_value (&len);
2105 if (m == MATCH_ERROR)
2109 if (gfc_match_char (')') == MATCH_YES)
2112 if (gfc_match (" , kind =") != MATCH_YES)
2115 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2121 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2122 m = char_len_param_value (&len);
2125 if (m == MATCH_ERROR)
2129 m = gfc_match_char (')');
2133 if (gfc_match_char (',') != MATCH_YES)
2136 gfc_match (" kind ="); /* Gobble optional text. */
2138 m = match_char_kind (&kind, &is_iso_c);
2139 if (m == MATCH_ERROR)
2145 /* Require a right-paren at this point. */
2146 m = gfc_match_char (')');
2151 gfc_error ("Syntax error in CHARACTER declaration at %C");
2153 gfc_free_expr (len);
2159 gfc_free_expr (len);
2163 /* Do some final massaging of the length values. */
2164 cl = gfc_get_charlen ();
2165 cl->next = gfc_current_ns->cl_list;
2166 gfc_current_ns->cl_list = cl;
2168 if (seen_length == 0)
2169 cl->length = gfc_int_expr (1);
2174 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2176 /* We have to know if it was a c interoperable kind so we can
2177 do accurate type checking of bind(c) procs, etc. */
2179 /* Mark this as c interoperable if being declared with one
2180 of the named constants from iso_c_binding. */
2181 ts->is_c_interop = is_iso_c;
2182 else if (len != NULL)
2183 /* Here, we might have parsed something such as: character(c_char)
2184 In this case, the parsing code above grabs the c_char when
2185 looking for the length (line 1690, roughly). it's the last
2186 testcase for parsing the kind params of a character variable.
2187 However, it's not actually the length. this seems like it
2189 To see if the user used a C interop kind, test the expr
2190 of the so called length, and see if it's C interoperable. */
2191 ts->is_c_interop = len->ts.is_iso_c;
2197 /* Matches a type specification. If successful, sets the ts structure
2198 to the matched specification. This is necessary for FUNCTION and
2199 IMPLICIT statements.
2201 If implicit_flag is nonzero, then we don't check for the optional
2202 kind specification. Not doing so is needed for matching an IMPLICIT
2203 statement correctly. */
2206 gfc_match_type_spec (gfc_typespec *ts, int implicit_flag)
2208 char name[GFC_MAX_SYMBOL_LEN + 1];
2212 locus loc = gfc_current_locus;
2216 /* Clear the current binding label, in case one is given. */
2217 curr_binding_label[0] = '\0';
2219 if (gfc_match (" byte") == MATCH_YES)
2221 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
2225 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2227 gfc_error ("BYTE type used at %C "
2228 "is not available on the target machine");
2232 ts->type = BT_INTEGER;
2237 if (gfc_match (" integer") == MATCH_YES)
2239 ts->type = BT_INTEGER;
2240 ts->kind = gfc_default_integer_kind;
2244 if (gfc_match (" character") == MATCH_YES)
2246 ts->type = BT_CHARACTER;
2247 if (implicit_flag == 0)
2248 return match_char_spec (ts);
2253 if (gfc_match (" real") == MATCH_YES)
2256 ts->kind = gfc_default_real_kind;
2260 if (gfc_match (" double precision") == MATCH_YES)
2263 ts->kind = gfc_default_double_kind;
2267 if (gfc_match (" complex") == MATCH_YES)
2269 ts->type = BT_COMPLEX;
2270 ts->kind = gfc_default_complex_kind;
2274 if (gfc_match (" double complex") == MATCH_YES)
2276 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2277 "conform to the Fortran 95 standard") == FAILURE)
2280 ts->type = BT_COMPLEX;
2281 ts->kind = gfc_default_double_kind;
2285 if (gfc_match (" logical") == MATCH_YES)
2287 ts->type = BT_LOGICAL;
2288 ts->kind = gfc_default_logical_kind;
2292 m = gfc_match (" type ( %n )", name);
2296 if (gfc_current_state () == COMP_INTERFACE
2297 || gfc_current_state () == COMP_NONE)
2299 gfc_function_type_locus = loc;
2300 ts->type = BT_UNKNOWN;
2305 /* Search for the name but allow the components to be defined later. If
2306 type = -1, this typespec has been seen in a function declaration but
2307 the type could not legally be accessed at that point. */
2308 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2310 gfc_error ("Type name '%s' at %C is ambiguous", name);
2313 else if (ts->kind == -1)
2315 if (gfc_find_symbol (name, NULL, 0, &sym))
2317 gfc_error ("Type name '%s' at %C is ambiguous", name);
2325 if (sym->attr.flavor != FL_DERIVED
2326 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2329 ts->type = BT_DERIVED;
2336 /* For all types except double, derived and character, look for an
2337 optional kind specifier. MATCH_NO is actually OK at this point. */
2338 if (implicit_flag == 1)
2341 if (gfc_current_form == FORM_FREE)
2343 c = gfc_peek_char();
2344 if (!gfc_is_whitespace(c) && c != '*' && c != '('
2345 && c != ':' && c != ',')
2349 m = gfc_match_kind_spec (ts, false);
2350 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2351 m = gfc_match_old_kind_spec (ts);
2354 m = MATCH_YES; /* No kind specifier found. */
2360 /* Match an IMPLICIT NONE statement. Actually, this statement is
2361 already matched in parse.c, or we would not end up here in the
2362 first place. So the only thing we need to check, is if there is
2363 trailing garbage. If not, the match is successful. */
2366 gfc_match_implicit_none (void)
2368 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2372 /* Match the letter range(s) of an IMPLICIT statement. */
2375 match_implicit_range (void)
2377 int c, c1, c2, inner;
2380 cur_loc = gfc_current_locus;
2382 gfc_gobble_whitespace ();
2383 c = gfc_next_char ();
2386 gfc_error ("Missing character range in IMPLICIT at %C");
2393 gfc_gobble_whitespace ();
2394 c1 = gfc_next_char ();
2398 gfc_gobble_whitespace ();
2399 c = gfc_next_char ();
2404 inner = 0; /* Fall through. */
2411 gfc_gobble_whitespace ();
2412 c2 = gfc_next_char ();
2416 gfc_gobble_whitespace ();
2417 c = gfc_next_char ();
2419 if ((c != ',') && (c != ')'))
2432 gfc_error ("Letters must be in alphabetic order in "
2433 "IMPLICIT statement at %C");
2437 /* See if we can add the newly matched range to the pending
2438 implicits from this IMPLICIT statement. We do not check for
2439 conflicts with whatever earlier IMPLICIT statements may have
2440 set. This is done when we've successfully finished matching
2442 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2449 gfc_syntax_error (ST_IMPLICIT);
2451 gfc_current_locus = cur_loc;
2456 /* Match an IMPLICIT statement, storing the types for
2457 gfc_set_implicit() if the statement is accepted by the parser.
2458 There is a strange looking, but legal syntactic construction
2459 possible. It looks like:
2461 IMPLICIT INTEGER (a-b) (c-d)
2463 This is legal if "a-b" is a constant expression that happens to
2464 equal one of the legal kinds for integers. The real problem
2465 happens with an implicit specification that looks like:
2467 IMPLICIT INTEGER (a-b)
2469 In this case, a typespec matcher that is "greedy" (as most of the
2470 matchers are) gobbles the character range as a kindspec, leaving
2471 nothing left. We therefore have to go a bit more slowly in the
2472 matching process by inhibiting the kindspec checking during
2473 typespec matching and checking for a kind later. */
2476 gfc_match_implicit (void)
2483 /* We don't allow empty implicit statements. */
2484 if (gfc_match_eos () == MATCH_YES)
2486 gfc_error ("Empty IMPLICIT statement at %C");
2492 /* First cleanup. */
2493 gfc_clear_new_implicit ();
2495 /* A basic type is mandatory here. */
2496 m = gfc_match_type_spec (&ts, 1);
2497 if (m == MATCH_ERROR)
2502 cur_loc = gfc_current_locus;
2503 m = match_implicit_range ();
2507 /* We may have <TYPE> (<RANGE>). */
2508 gfc_gobble_whitespace ();
2509 c = gfc_next_char ();
2510 if ((c == '\n') || (c == ','))
2512 /* Check for CHARACTER with no length parameter. */
2513 if (ts.type == BT_CHARACTER && !ts.cl)
2515 ts.kind = gfc_default_character_kind;
2516 ts.cl = gfc_get_charlen ();
2517 ts.cl->next = gfc_current_ns->cl_list;
2518 gfc_current_ns->cl_list = ts.cl;
2519 ts.cl->length = gfc_int_expr (1);
2522 /* Record the Successful match. */
2523 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2528 gfc_current_locus = cur_loc;
2531 /* Discard the (incorrectly) matched range. */
2532 gfc_clear_new_implicit ();
2534 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2535 if (ts.type == BT_CHARACTER)
2536 m = match_char_spec (&ts);
2539 m = gfc_match_kind_spec (&ts, false);
2542 m = gfc_match_old_kind_spec (&ts);
2543 if (m == MATCH_ERROR)
2549 if (m == MATCH_ERROR)
2552 m = match_implicit_range ();
2553 if (m == MATCH_ERROR)
2558 gfc_gobble_whitespace ();
2559 c = gfc_next_char ();
2560 if ((c != '\n') && (c != ','))
2563 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2571 gfc_syntax_error (ST_IMPLICIT);
2579 gfc_match_import (void)
2581 char name[GFC_MAX_SYMBOL_LEN + 1];
2586 if (gfc_current_ns->proc_name == NULL
2587 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2589 gfc_error ("IMPORT statement at %C only permitted in "
2590 "an INTERFACE body");
2594 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2598 if (gfc_match_eos () == MATCH_YES)
2600 /* All host variables should be imported. */
2601 gfc_current_ns->has_import_set = 1;
2605 if (gfc_match (" ::") == MATCH_YES)
2607 if (gfc_match_eos () == MATCH_YES)
2609 gfc_error ("Expecting list of named entities at %C");
2616 m = gfc_match (" %n", name);
2620 if (gfc_current_ns->parent != NULL
2621 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2623 gfc_error ("Type name '%s' at %C is ambiguous", name);
2626 else if (gfc_current_ns->proc_name->ns->parent != NULL
2627 && gfc_find_symbol (name,
2628 gfc_current_ns->proc_name->ns->parent,
2631 gfc_error ("Type name '%s' at %C is ambiguous", name);
2637 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2638 "at %C - does not exist.", name);
2642 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2644 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2649 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2652 sym->attr.imported = 1;
2664 if (gfc_match_eos () == MATCH_YES)
2666 if (gfc_match_char (',') != MATCH_YES)
2673 gfc_error ("Syntax error in IMPORT statement at %C");
2678 /* A minimal implementation of gfc_match without whitespace, escape
2679 characters or variable arguments. Returns true if the next
2680 characters match the TARGET template exactly. */
2683 match_string_p (const char *target)
2687 for (p = target; *p; p++)
2688 if (gfc_next_char () != *p)
2693 /* Matches an attribute specification including array specs. If
2694 successful, leaves the variables current_attr and current_as
2695 holding the specification. Also sets the colon_seen variable for
2696 later use by matchers associated with initializations.
2698 This subroutine is a little tricky in the sense that we don't know
2699 if we really have an attr-spec until we hit the double colon.
2700 Until that time, we can only return MATCH_NO. This forces us to
2701 check for duplicate specification at this level. */
2704 match_attr_spec (void)
2706 /* Modifiers that can exist in a type statement. */
2708 { GFC_DECL_BEGIN = 0,
2709 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2710 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2711 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2712 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2713 DECL_IS_BIND_C, DECL_NONE,
2714 GFC_DECL_END /* Sentinel */
2718 /* GFC_DECL_END is the sentinel, index starts at 0. */
2719 #define NUM_DECL GFC_DECL_END
2721 locus start, seen_at[NUM_DECL];
2728 gfc_clear_attr (¤t_attr);
2729 start = gfc_current_locus;
2734 /* See if we get all of the keywords up to the final double colon. */
2735 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2743 gfc_gobble_whitespace ();
2745 ch = gfc_next_char ();
2748 /* This is the successful exit condition for the loop. */
2749 if (gfc_next_char () == ':')
2754 gfc_gobble_whitespace ();
2755 switch (gfc_peek_char ())
2758 if (match_string_p ("allocatable"))
2759 d = DECL_ALLOCATABLE;
2763 /* Try and match the bind(c). */
2764 m = gfc_match_bind_c (NULL, true);
2767 else if (m == MATCH_ERROR)
2772 if (match_string_p ("dimension"))
2777 if (match_string_p ("external"))
2782 if (match_string_p ("int"))
2784 ch = gfc_next_char ();
2787 if (match_string_p ("nt"))
2789 /* Matched "intent". */
2790 /* TODO: Call match_intent_spec from here. */
2791 if (gfc_match (" ( in out )") == MATCH_YES)
2793 else if (gfc_match (" ( in )") == MATCH_YES)
2795 else if (gfc_match (" ( out )") == MATCH_YES)
2801 if (match_string_p ("insic"))
2803 /* Matched "intrinsic". */
2811 if (match_string_p ("optional"))
2817 switch (gfc_next_char ())
2820 if (match_string_p ("rameter"))
2822 /* Matched "parameter". */
2828 if (match_string_p ("inter"))
2830 /* Matched "pointer". */
2836 ch = gfc_next_char ();
2839 if (match_string_p ("vate"))
2841 /* Matched "private". */
2847 if (match_string_p ("tected"))
2849 /* Matched "protected". */
2856 if (match_string_p ("blic"))
2858 /* Matched "public". */
2866 if (match_string_p ("save"))
2871 if (match_string_p ("target"))
2877 ch = gfc_next_char ();
2880 if (match_string_p ("lue"))
2882 /* Matched "value". */
2888 if (match_string_p ("latile"))
2890 /* Matched "volatile". */
2898 /* No double colon and no recognizable decl_type, so assume that
2899 we've been looking at something else the whole time. */
2907 seen_at[d] = gfc_current_locus;
2909 if (d == DECL_DIMENSION)
2911 m = gfc_match_array_spec (¤t_as);
2915 gfc_error ("Missing dimension specification at %C");
2919 if (m == MATCH_ERROR)
2924 /* Since we've seen a double colon, we have to be looking at an
2925 attr-spec. This means that we can now issue errors. */
2926 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2931 case DECL_ALLOCATABLE:
2932 attr = "ALLOCATABLE";
2934 case DECL_DIMENSION:
2941 attr = "INTENT (IN)";
2944 attr = "INTENT (OUT)";
2947 attr = "INTENT (IN OUT)";
2949 case DECL_INTRINSIC:
2955 case DECL_PARAMETER:
2961 case DECL_PROTECTED:
2976 case DECL_IS_BIND_C:
2986 attr = NULL; /* This shouldn't happen. */
2989 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2994 /* Now that we've dealt with duplicate attributes, add the attributes
2995 to the current attribute. */
2996 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3001 if (gfc_current_state () == COMP_DERIVED
3002 && d != DECL_DIMENSION && d != DECL_POINTER
3003 && d != DECL_PRIVATE && d != DECL_PUBLIC
3006 if (d == DECL_ALLOCATABLE)
3008 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3009 "attribute at %C in a TYPE definition")
3018 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3025 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3026 && gfc_current_state () != COMP_MODULE)
3028 if (d == DECL_PRIVATE)
3032 if (gfc_current_state () == COMP_DERIVED
3033 && gfc_state_stack->previous
3034 && gfc_state_stack->previous->state == COMP_MODULE)
3036 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3037 "at %L in a TYPE definition", attr,
3047 gfc_error ("%s attribute at %L is not allowed outside of the "
3048 "specification part of a module", attr, &seen_at[d]);
3056 case DECL_ALLOCATABLE:
3057 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
3060 case DECL_DIMENSION:
3061 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
3065 t = gfc_add_external (¤t_attr, &seen_at[d]);
3069 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
3073 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
3077 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
3080 case DECL_INTRINSIC:
3081 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
3085 t = gfc_add_optional (¤t_attr, &seen_at[d]);
3088 case DECL_PARAMETER:
3089 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
3093 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
3096 case DECL_PROTECTED:
3097 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3099 gfc_error ("PROTECTED at %C only allowed in specification "
3100 "part of a module");
3105 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3110 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
3114 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
3119 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
3124 t = gfc_add_save (¤t_attr, NULL, &seen_at[d]);
3128 t = gfc_add_target (¤t_attr, &seen_at[d]);
3131 case DECL_IS_BIND_C:
3132 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
3136 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3141 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
3145 if (gfc_notify_std (GFC_STD_F2003,
3146 "Fortran 2003: VOLATILE attribute at %C")
3150 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
3154 gfc_internal_error ("match_attr_spec(): Bad attribute");
3168 gfc_current_locus = start;
3169 gfc_free_array_spec (current_as);
3175 /* Set the binding label, dest_label, either with the binding label
3176 stored in the given gfc_typespec, ts, or if none was provided, it
3177 will be the symbol name in all lower case, as required by the draft
3178 (J3/04-007, section 15.4.1). If a binding label was given and
3179 there is more than one argument (num_idents), it is an error. */
3182 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3184 if (num_idents > 1 && has_name_equals)
3186 gfc_error ("Multiple identifiers provided with "
3187 "single NAME= specifier at %C");
3191 if (curr_binding_label[0] != '\0')
3193 /* Binding label given; store in temp holder til have sym. */
3194 strcpy (dest_label, curr_binding_label);
3198 /* No binding label given, and the NAME= specifier did not exist,
3199 which means there was no NAME="". */
3200 if (sym_name != NULL && has_name_equals == 0)
3201 strcpy (dest_label, sym_name);
3208 /* Set the status of the given common block as being BIND(C) or not,
3209 depending on the given parameter, is_bind_c. */
3212 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3214 com_block->is_bind_c = is_bind_c;
3219 /* Verify that the given gfc_typespec is for a C interoperable type. */
3222 verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
3226 /* Make sure the kind used is appropriate for the type.
3227 The f90_type is unknown if an integer constant was
3228 used (e.g., real(4), bind(c) :: myFloat). */
3229 if (ts->f90_type != BT_UNKNOWN)
3231 t = gfc_validate_c_kind (ts);
3234 /* Print an error, but continue parsing line. */
3235 gfc_error_now ("C kind parameter is for type %s but "
3236 "symbol '%s' at %L is of type %s",
3237 gfc_basic_typename (ts->f90_type),
3239 gfc_basic_typename (ts->type));
3243 /* Make sure the kind is C interoperable. This does not care about the
3244 possible error above. */
3245 if (ts->type == BT_DERIVED && ts->derived != NULL)
3246 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
3247 else if (ts->is_c_interop != 1)
3254 /* Verify that the variables of a given common block, which has been
3255 defined with the attribute specifier bind(c), to be of a C
3256 interoperable type. Errors will be reported here, if
3260 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3262 gfc_symbol *curr_sym = NULL;
3263 try retval = SUCCESS;
3265 curr_sym = com_block->head;
3267 /* Make sure we have at least one symbol. */
3268 if (curr_sym == NULL)
3271 /* Here we know we have a symbol, so we'll execute this loop
3275 /* The second to last param, 1, says this is in a common block. */
3276 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3277 curr_sym = curr_sym->common_next;
3278 } while (curr_sym != NULL);
3284 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3285 an appropriate error message is reported. */
3288 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3289 int is_in_common, gfc_common_head *com_block)
3291 try retval = SUCCESS;
3293 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3295 tmp_sym = tmp_sym->result;
3296 /* Make sure it wasn't an implicitly typed result. */
3297 if (tmp_sym->attr.implicit_type)
3299 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3300 "%L may not be C interoperable", tmp_sym->name,
3301 &tmp_sym->declared_at);
3302 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3303 /* Mark it as C interoperable to prevent duplicate warnings. */
3304 tmp_sym->ts.is_c_interop = 1;
3305 tmp_sym->attr.is_c_interop = 1;
3309 /* Here, we know we have the bind(c) attribute, so if we have
3310 enough type info, then verify that it's a C interop kind.
3311 The info could be in the symbol already, or possibly still in
3312 the given ts (current_ts), so look in both. */
3313 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3315 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
3316 &(tmp_sym->declared_at)) != SUCCESS)
3318 /* See if we're dealing with a sym in a common block or not. */
3319 if (is_in_common == 1)
3321 gfc_warning ("Variable '%s' in common block '%s' at %L "
3322 "may not be a C interoperable "
3323 "kind though common block '%s' is BIND(C)",
3324 tmp_sym->name, com_block->name,
3325 &(tmp_sym->declared_at), com_block->name);
3329 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3330 gfc_error ("Type declaration '%s' at %L is not C "
3331 "interoperable but it is BIND(C)",
3332 tmp_sym->name, &(tmp_sym->declared_at));
3334 gfc_warning ("Variable '%s' at %L "
3335 "may not be a C interoperable "
3336 "kind but it is bind(c)",
3337 tmp_sym->name, &(tmp_sym->declared_at));
3341 /* Variables declared w/in a common block can't be bind(c)
3342 since there's no way for C to see these variables, so there's
3343 semantically no reason for the attribute. */
3344 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3346 gfc_error ("Variable '%s' in common block '%s' at "
3347 "%L cannot be declared with BIND(C) "
3348 "since it is not a global",
3349 tmp_sym->name, com_block->name,
3350 &(tmp_sym->declared_at));
3354 /* Scalar variables that are bind(c) can not have the pointer
3355 or allocatable attributes. */
3356 if (tmp_sym->attr.is_bind_c == 1)
3358 if (tmp_sym->attr.pointer == 1)
3360 gfc_error ("Variable '%s' at %L cannot have both the "
3361 "POINTER and BIND(C) attributes",
3362 tmp_sym->name, &(tmp_sym->declared_at));
3366 if (tmp_sym->attr.allocatable == 1)
3368 gfc_error ("Variable '%s' at %L cannot have both the "
3369 "ALLOCATABLE and BIND(C) attributes",
3370 tmp_sym->name, &(tmp_sym->declared_at));
3374 /* If it is a BIND(C) function, make sure the return value is a
3375 scalar value. The previous tests in this function made sure
3376 the type is interoperable. */
3377 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
3378 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3379 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3381 /* BIND(C) functions can not return a character string. */
3382 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
3383 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3384 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3385 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3386 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3387 "be a character string", tmp_sym->name,
3388 &(tmp_sym->declared_at));
3392 /* See if the symbol has been marked as private. If it has, make sure
3393 there is no binding label and warn the user if there is one. */
3394 if (tmp_sym->attr.access == ACCESS_PRIVATE
3395 && tmp_sym->binding_label[0] != '\0')
3396 /* Use gfc_warning_now because we won't say that the symbol fails
3397 just because of this. */
3398 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3399 "given the binding label '%s'", tmp_sym->name,
3400 &(tmp_sym->declared_at), tmp_sym->binding_label);
3406 /* Set the appropriate fields for a symbol that's been declared as
3407 BIND(C) (the is_bind_c flag and the binding label), and verify that
3408 the type is C interoperable. Errors are reported by the functions
3409 used to set/test these fields. */
3412 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3414 try retval = SUCCESS;
3416 /* TODO: Do we need to make sure the vars aren't marked private? */
3418 /* Set the is_bind_c bit in symbol_attribute. */
3419 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3421 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3422 num_idents) != SUCCESS)
3429 /* Set the fields marking the given common block as BIND(C), including
3430 a binding label, and report any errors encountered. */
3433 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3435 try retval = SUCCESS;
3437 /* destLabel, common name, typespec (which may have binding label). */
3438 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3442 /* Set the given common block (com_block) to being bind(c) (1). */
3443 set_com_block_bind_c (com_block, 1);
3449 /* Retrieve the list of one or more identifiers that the given bind(c)
3450 attribute applies to. */
3453 get_bind_c_idents (void)
3455 char name[GFC_MAX_SYMBOL_LEN + 1];
3457 gfc_symbol *tmp_sym = NULL;
3459 gfc_common_head *com_block = NULL;
3461 if (gfc_match_name (name) == MATCH_YES)
3463 found_id = MATCH_YES;
3464 gfc_get_ha_symbol (name, &tmp_sym);
3466 else if (match_common_name (name) == MATCH_YES)
3468 found_id = MATCH_YES;
3469 com_block = gfc_get_common (name, 0);
3473 gfc_error ("Need either entity or common block name for "
3474 "attribute specification statement at %C");
3478 /* Save the current identifier and look for more. */
3481 /* Increment the number of identifiers found for this spec stmt. */
3484 /* Make sure we have a sym or com block, and verify that it can
3485 be bind(c). Set the appropriate field(s) and look for more
3487 if (tmp_sym != NULL || com_block != NULL)
3489 if (tmp_sym != NULL)
3491 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3497 if (set_verify_bind_c_com_block(com_block, num_idents)
3502 /* Look to see if we have another identifier. */
3504 if (gfc_match_eos () == MATCH_YES)
3505 found_id = MATCH_NO;
3506 else if (gfc_match_char (',') != MATCH_YES)
3507 found_id = MATCH_NO;
3508 else if (gfc_match_name (name) == MATCH_YES)
3510 found_id = MATCH_YES;
3511 gfc_get_ha_symbol (name, &tmp_sym);
3513 else if (match_common_name (name) == MATCH_YES)
3515 found_id = MATCH_YES;
3516 com_block = gfc_get_common (name, 0);
3520 gfc_error ("Missing entity or common block name for "
3521 "attribute specification statement at %C");
3527 gfc_internal_error ("Missing symbol");
3529 } while (found_id == MATCH_YES);
3531 /* if we get here we were successful */
3536 /* Try and match a BIND(C) attribute specification statement. */
3539 gfc_match_bind_c_stmt (void)
3541 match found_match = MATCH_NO;
3546 /* This may not be necessary. */
3548 /* Clear the temporary binding label holder. */
3549 curr_binding_label[0] = '\0';
3551 /* Look for the bind(c). */
3552 found_match = gfc_match_bind_c (NULL, true);
3554 if (found_match == MATCH_YES)
3556 /* Look for the :: now, but it is not required. */
3559 /* Get the identifier(s) that needs to be updated. This may need to
3560 change to hand the flag(s) for the attr specified so all identifiers
3561 found can have all appropriate parts updated (assuming that the same
3562 spec stmt can have multiple attrs, such as both bind(c) and
3564 if (get_bind_c_idents () != SUCCESS)
3565 /* Error message should have printed already. */
3573 /* Match a data declaration statement. */
3576 gfc_match_data_decl (void)
3582 num_idents_on_line = 0;
3584 m = gfc_match_type_spec (¤t_ts, 0);
3588 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3590 sym = gfc_use_derived (current_ts.derived);
3598 current_ts.derived = sym;
3601 m = match_attr_spec ();
3602 if (m == MATCH_ERROR)
3608 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL
3609 && !current_ts.derived->attr.zero_comp)
3612 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3615 gfc_find_symbol (current_ts.derived->name,
3616 current_ts.derived->ns->parent, 1, &sym);
3618 /* Any symbol that we find had better be a type definition
3619 which has its components defined. */
3620 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3621 && (current_ts.derived->components != NULL
3622 || current_ts.derived->attr.zero_comp))
3625 /* Now we have an error, which we signal, and then fix up
3626 because the knock-on is plain and simple confusing. */
3627 gfc_error_now ("Derived type at %C has not been previously defined "
3628 "and so cannot appear in a derived type definition");
3629 current_attr.pointer = 1;
3634 /* If we have an old-style character declaration, and no new-style
3635 attribute specifications, then there a comma is optional between
3636 the type specification and the variable list. */
3637 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3638 gfc_match_char (',');
3640 /* Give the types/attributes to symbols that follow. Give the element
3641 a number so that repeat character length expressions can be copied. */
3645 num_idents_on_line++;
3646 m = variable_decl (elem++);
3647 if (m == MATCH_ERROR)
3652 if (gfc_match_eos () == MATCH_YES)
3654 if (gfc_match_char (',') != MATCH_YES)
3658 if (gfc_error_flag_test () == 0)
3659 gfc_error ("Syntax error in data declaration at %C");
3662 gfc_free_data_all (gfc_current_ns);
3665 gfc_free_array_spec (current_as);
3671 /* Match a prefix associated with a function or subroutine
3672 declaration. If the typespec pointer is nonnull, then a typespec
3673 can be matched. Note that if nothing matches, MATCH_YES is
3674 returned (the null string was matched). */
3677 match_prefix (gfc_typespec *ts)
3681 gfc_clear_attr (¤t_attr);
3685 if (!seen_type && ts != NULL
3686 && gfc_match_type_spec (ts, 0) == MATCH_YES
3687 && gfc_match_space () == MATCH_YES)
3694 if (gfc_match ("elemental% ") == MATCH_YES)
3696 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
3702 if (gfc_match ("pure% ") == MATCH_YES)
3704 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
3710 if (gfc_match ("recursive% ") == MATCH_YES)
3712 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
3718 /* At this point, the next item is not a prefix. */
3723 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
3726 copy_prefix (symbol_attribute *dest, locus *where)
3728 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3731 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3734 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3741 /* Match a formal argument list. */
3744 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3746 gfc_formal_arglist *head, *tail, *p, *q;
3747 char name[GFC_MAX_SYMBOL_LEN + 1];
3753 if (gfc_match_char ('(') != MATCH_YES)
3760 if (gfc_match_char (')') == MATCH_YES)
3765 if (gfc_match_char ('*') == MATCH_YES)
3769 m = gfc_match_name (name);
3773 if (gfc_get_symbol (name, NULL, &sym))
3777 p = gfc_get_formal_arglist ();
3789 /* We don't add the VARIABLE flavor because the name could be a
3790 dummy procedure. We don't apply these attributes to formal
3791 arguments of statement functions. */
3792 if (sym != NULL && !st_flag
3793 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3794 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3800 /* The name of a program unit can be in a different namespace,
3801 so check for it explicitly. After the statement is accepted,
3802 the name is checked for especially in gfc_get_symbol(). */
3803 if (gfc_new_block != NULL && sym != NULL
3804 && strcmp (sym->name, gfc_new_block->name) == 0)
3806 gfc_error ("Name '%s' at %C is the name of the procedure",
3812 if (gfc_match_char (')') == MATCH_YES)
3815 m = gfc_match_char (',');
3818 gfc_error ("Unexpected junk in formal argument list at %C");
3824 /* Check for duplicate symbols in the formal argument list. */
3827 for (p = head; p->next; p = p->next)
3832 for (q = p->next; q; q = q->next)
3833 if (p->sym == q->sym)
3835 gfc_error ("Duplicate symbol '%s' in formal argument list "
3836 "at %C", p->sym->name);
3844 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3854 gfc_free_formal_arglist (head);
3859 /* Match a RESULT specification following a function declaration or
3860 ENTRY statement. Also matches the end-of-statement. */
3863 match_result (gfc_symbol *function, gfc_symbol **result)
3865 char name[GFC_MAX_SYMBOL_LEN + 1];
3869 if (gfc_match (" result (") != MATCH_YES)
3872 m = gfc_match_name (name);
3876 /* Get the right paren, and that's it because there could be the
3877 bind(c) attribute after the result clause. */
3878 if (gfc_match_char(')') != MATCH_YES)
3880 /* TODO: should report the missing right paren here. */
3884 if (strcmp (function->name, name) == 0)
3886 gfc_error ("RESULT variable at %C must be different than function name");
3890 if (gfc_get_symbol (name, NULL, &r))
3893 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3894 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
3903 /* Match a function suffix, which could be a combination of a result
3904 clause and BIND(C), either one, or neither. The draft does not
3905 require them to come in a specific order. */
3908 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3910 match is_bind_c; /* Found bind(c). */
3911 match is_result; /* Found result clause. */
3912 match found_match; /* Status of whether we've found a good match. */
3913 int peek_char; /* Character we're going to peek at. */
3914 bool allow_binding_name;
3916 /* Initialize to having found nothing. */
3917 found_match = MATCH_NO;
3918 is_bind_c = MATCH_NO;
3919 is_result = MATCH_NO;
3921 /* Get the next char to narrow between result and bind(c). */
3922 gfc_gobble_whitespace ();
3923 peek_char = gfc_peek_char ();
3925 /* C binding names are not allowed for internal procedures. */
3926 if (gfc_current_state () == COMP_CONTAINS
3927 && sym->ns->proc_name->attr.flavor != FL_MODULE)
3928 allow_binding_name = false;
3930 allow_binding_name = true;
3935 /* Look for result clause. */
3936 is_result = match_result (sym, result);
3937 if (is_result == MATCH_YES)
3939 /* Now see if there is a bind(c) after it. */
3940 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
3941 /* We've found the result clause and possibly bind(c). */
3942 found_match = MATCH_YES;
3945 /* This should only be MATCH_ERROR. */
3946 found_match = is_result;
3949 /* Look for bind(c) first. */
3950 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
3951 if (is_bind_c == MATCH_YES)
3953 /* Now see if a result clause followed it. */
3954 is_result = match_result (sym, result);
3955 found_match = MATCH_YES;
3959 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3960 found_match = MATCH_ERROR;
3964 gfc_error ("Unexpected junk after function declaration at %C");
3965 found_match = MATCH_ERROR;
3969 if (is_bind_c == MATCH_YES)
3971 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
3972 if (gfc_current_state () == COMP_CONTAINS
3973 && sym->ns->proc_name->attr.flavor != FL_MODULE
3974 && gfc_notify_std (GFC_STD_GNU, "Extension: BIND(C) attribute at %L "
3975 "may not be specified for an internal procedure",
3980 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
3989 /* Match a PROCEDURE declaration (R1211). */
3992 match_procedure_decl (void)
3995 locus old_loc, entry_loc;
3996 gfc_symbol *sym, *proc_if = NULL;
3999 old_loc = entry_loc = gfc_current_locus;
4001 gfc_clear_ts (¤t_ts);
4003 if (gfc_match (" (") != MATCH_YES)
4005 gfc_current_locus = entry_loc;
4009 /* Get the type spec. for the procedure interface. */
4010 old_loc = gfc_current_locus;
4011 m = gfc_match_type_spec (¤t_ts, 0);
4012 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_char () == ')'))
4015 if (m == MATCH_ERROR)
4018 gfc_current_locus = old_loc;
4020 /* Get the name of the procedure or abstract interface
4021 to inherit the interface from. */
4022 m = gfc_match_symbol (&proc_if, 1);
4026 else if (m == MATCH_ERROR)
4029 /* Various interface checks. */
4032 /* Resolve interface if possible. That way, attr.procedure is only set
4033 if it is declared by a later procedure-declaration-stmt, which is
4034 invalid per C1212. */
4035 while (proc_if->interface)
4036 proc_if = proc_if->interface;
4038 if (proc_if->generic)
4040 gfc_error ("Interface '%s' at %C may not be generic", proc_if->name);
4043 if (proc_if->attr.proc == PROC_ST_FUNCTION)
4045 gfc_error ("Interface '%s' at %C may not be a statement function",
4049 /* Handle intrinsic procedures. */
4050 if (gfc_intrinsic_name (proc_if->name, 0)
4051 || gfc_intrinsic_name (proc_if->name, 1))
4052 proc_if->attr.intrinsic = 1;
4053 if (proc_if->attr.intrinsic
4054 && !gfc_intrinsic_actual_ok (proc_if->name, 0))
4056 gfc_error ("Intrinsic procedure '%s' not allowed "
4057 "in PROCEDURE statement at %C", proc_if->name);
4063 if (gfc_match (" )") != MATCH_YES)
4065 gfc_current_locus = entry_loc;
4069 /* Parse attributes. */
4070 m = match_attr_spec();
4071 if (m == MATCH_ERROR)
4074 /* Get procedure symbols. */
4077 m = gfc_match_symbol (&sym, 0);
4080 else if (m == MATCH_ERROR)
4083 /* Add current_attr to the symbol attributes. */
4084 if (gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
4087 if (sym->attr.is_bind_c)
4089 /* Check for C1218. */
4090 if (!proc_if || !proc_if->attr.is_bind_c)
4092 gfc_error ("BIND(C) attribute at %C requires "
4093 "an interface with BIND(C)");
4096 /* Check for C1217. */
4097 if (has_name_equals && sym->attr.pointer)
4099 gfc_error ("BIND(C) procedure with NAME may not have "
4100 "POINTER attribute at %C");
4103 if (has_name_equals && sym->attr.dummy)
4105 gfc_error ("Dummy procedure at %C may not have "
4106 "BIND(C) attribute with NAME");
4109 /* Set binding label for BIND(C). */
4110 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4114 if (!sym->attr.pointer && gfc_add_external (&sym->attr, NULL) == FAILURE)
4116 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4119 /* Set interface. */
4120 if (proc_if != NULL)
4122 sym->interface = proc_if;
4123 sym->attr.untyped = 1;
4125 else if (current_ts.type != BT_UNKNOWN)
4127 sym->interface = gfc_new_symbol ("", gfc_current_ns);
4128 sym->interface->ts = current_ts;
4129 sym->interface->attr.function = 1;
4130 sym->ts = sym->interface->ts;
4131 sym->attr.function = sym->interface->attr.function;
4134 if (gfc_match_eos () == MATCH_YES)
4136 if (gfc_match_char (',') != MATCH_YES)
4141 gfc_error ("Syntax error in PROCEDURE statement at %C");
4146 /* Match a PROCEDURE declaration inside an interface (R1206). */
4149 match_procedure_in_interface (void)
4153 char name[GFC_MAX_SYMBOL_LEN + 1];
4155 if (current_interface.type == INTERFACE_NAMELESS
4156 || current_interface.type == INTERFACE_ABSTRACT)
4158 gfc_error ("PROCEDURE at %C must be in a generic interface");
4164 m = gfc_match_name (name);
4167 else if (m == MATCH_ERROR)
4169 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4172 if (gfc_add_interface (sym) == FAILURE)
4175 if (gfc_match_eos () == MATCH_YES)
4177 if (gfc_match_char (',') != MATCH_YES)
4184 gfc_error ("Syntax error in PROCEDURE statement at %C");
4189 /* General matcher for PROCEDURE declarations. */
4192 gfc_match_procedure (void)
4196 switch (gfc_current_state ())
4201 case COMP_SUBROUTINE:
4203 m = match_procedure_decl ();
4205 case COMP_INTERFACE:
4206 m = match_procedure_in_interface ();
4209 gfc_error ("Fortran 2003: Procedure components at %C are "
4210 "not yet implemented in gfortran");
4219 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4227 /* Match a function declaration. */
4230 gfc_match_function_decl (void)
4232 char name[GFC_MAX_SYMBOL_LEN + 1];
4233 gfc_symbol *sym, *result;
4237 match found_match; /* Status returned by match func. */
4239 if (gfc_current_state () != COMP_NONE
4240 && gfc_current_state () != COMP_INTERFACE
4241 && gfc_current_state () != COMP_CONTAINS)
4244 gfc_clear_ts (¤t_ts);
4246 old_loc = gfc_current_locus;
4248 m = match_prefix (¤t_ts);
4251 gfc_current_locus = old_loc;
4255 if (gfc_match ("function% %n", name) != MATCH_YES)
4257 gfc_current_locus = old_loc;
4260 if (get_proc_name (name, &sym, false))
4262 gfc_new_block = sym;
4264 m = gfc_match_formal_arglist (sym, 0, 0);
4267 gfc_error ("Expected formal argument list in function "
4268 "definition at %C");
4272 else if (m == MATCH_ERROR)
4277 /* According to the draft, the bind(c) and result clause can
4278 come in either order after the formal_arg_list (i.e., either
4279 can be first, both can exist together or by themselves or neither
4280 one). Therefore, the match_result can't match the end of the
4281 string, and check for the bind(c) or result clause in either order. */
4282 found_match = gfc_match_eos ();
4284 /* Make sure that it isn't already declared as BIND(C). If it is, it
4285 must have been marked BIND(C) with a BIND(C) attribute and that is
4286 not allowed for procedures. */
4287 if (sym->attr.is_bind_c == 1)
4289 sym->attr.is_bind_c = 0;
4290 if (sym->old_symbol != NULL)
4291 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4292 "variables or common blocks",
4293 &(sym->old_symbol->declared_at));
4295 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4296 "variables or common blocks", &gfc_current_locus);
4299 if (found_match != MATCH_YES)
4301 /* If we haven't found the end-of-statement, look for a suffix. */
4302 suffix_match = gfc_match_suffix (sym, &result);
4303 if (suffix_match == MATCH_YES)
4304 /* Need to get the eos now. */
4305 found_match = gfc_match_eos ();
4307 found_match = suffix_match;
4310 if(found_match != MATCH_YES)
4314 /* Make changes to the symbol. */
4317 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4320 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4321 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4324 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
4325 && !sym->attr.implicit_type)
4327 gfc_error ("Function '%s' at %C already has a type of %s", name,
4328 gfc_basic_typename (sym->ts.type));
4334 sym->ts = current_ts;
4339 result->ts = current_ts;
4340 sym->result = result;
4347 gfc_current_locus = old_loc;
4352 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4353 pass the name of the entry, rather than the gfc_current_block name, and
4354 to return false upon finding an existing global entry. */
4357 add_global_entry (const char *name, int sub)
4362 s = gfc_get_gsymbol(name);
4363 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4366 || (s->type != GSYM_UNKNOWN
4367 && s->type != type))
4368 gfc_global_used(s, NULL);
4372 s->where = gfc_current_locus;
4380 /* Match an ENTRY statement. */
4383 gfc_match_entry (void)
4388 char name[GFC_MAX_SYMBOL_LEN + 1];
4389 gfc_compile_state state;
4393 bool module_procedure;
4397 m = gfc_match_name (name);
4401 state = gfc_current_state ();
4402 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
4407 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4410 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4412 case COMP_BLOCK_DATA:
4413 gfc_error ("ENTRY statement at %C cannot appear within "
4416 case COMP_INTERFACE:
4417 gfc_error ("ENTRY statement at %C cannot appear within "
4421 gfc_error ("ENTRY statement at %C cannot appear within "
4422 "a DERIVED TYPE block");
4425 gfc_error ("ENTRY statement at %C cannot appear within "
4426 "an IF-THEN block");
4429 gfc_error ("ENTRY statement at %C cannot appear within "
4433 gfc_error ("ENTRY statement at %C cannot appear within "
4437 gfc_error ("ENTRY statement at %C cannot appear within "
4441 gfc_error ("ENTRY statement at %C cannot appear within "
4445 gfc_error ("ENTRY statement at %C cannot appear within "
4446 "a contained subprogram");
4449 gfc_internal_error ("gfc_match_entry(): Bad state");
4454 module_procedure = gfc_current_ns->parent != NULL
4455 && gfc_current_ns->parent->proc_name
4456 && gfc_current_ns->parent->proc_name->attr.flavor
4459 if (gfc_current_ns->parent != NULL
4460 && gfc_current_ns->parent->proc_name
4461 && !module_procedure)
4463 gfc_error("ENTRY statement at %C cannot appear in a "
4464 "contained procedure");
4468 /* Module function entries need special care in get_proc_name
4469 because previous references within the function will have
4470 created symbols attached to the current namespace. */
4471 if (get_proc_name (name, &entry,
4472 gfc_current_ns->parent != NULL
4474 && gfc_current_ns->proc_name->attr.function))
4477 proc = gfc_current_block ();
4479 /* Make sure that it isn't already declared as BIND(C). If it is, it
4480 must have been marked BIND(C) with a BIND(C) attribute and that is
4481 not allowed for procedures. */
4482 if (entry->attr.is_bind_c == 1)
4484 entry->attr.is_bind_c = 0;
4485 if (entry->old_symbol != NULL)
4486 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4487 "variables or common blocks",
4488 &(entry->old_symbol->declared_at));
4490 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4491 "variables or common blocks", &gfc_current_locus);
4494 /* Check what next non-whitespace character is so we can tell if there
4495 is the required parens if we have a BIND(C). */
4496 gfc_gobble_whitespace ();
4497 peek_char = gfc_peek_char ();
4499 if (state == COMP_SUBROUTINE)
4501 /* An entry in a subroutine. */
4502 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
4505 m = gfc_match_formal_arglist (entry, 0, 1);
4509 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
4510 never be an internal procedure. */
4511 is_bind_c = gfc_match_bind_c (entry, true);
4512 if (is_bind_c == MATCH_ERROR)
4514 if (is_bind_c == MATCH_YES)
4516 if (peek_char != '(')
4518 gfc_error ("Missing required parentheses before BIND(C) at %C");
4521 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
4526 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4527 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4532 /* An entry in a function.
4533 We need to take special care because writing
4538 ENTRY f() RESULT (r)
4540 ENTRY f RESULT (r). */
4541 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
4544 old_loc = gfc_current_locus;
4545 if (gfc_match_eos () == MATCH_YES)
4547 gfc_current_locus = old_loc;
4548 /* Match the empty argument list, and add the interface to
4550 m = gfc_match_formal_arglist (entry, 0, 1);
4553 m = gfc_match_formal_arglist (entry, 0, 0);
4560 if (gfc_match_eos () == MATCH_YES)
4562 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4563 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4566 entry->result = entry;
4570 m = gfc_match_suffix (entry, &result);
4572 gfc_syntax_error (ST_ENTRY);
4578 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4579 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
4580 || gfc_add_function (&entry->attr, result->name, NULL)
4583 entry->result = result;
4587 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4588 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4590 entry->result = entry;
4595 if (gfc_match_eos () != MATCH_YES)
4597 gfc_syntax_error (ST_ENTRY);
4601 entry->attr.recursive = proc->attr.recursive;
4602 entry->attr.elemental = proc->attr.elemental;
4603 entry->attr.pure = proc->attr.pure;
4605 el = gfc_get_entry_list ();
4607 el->next = gfc_current_ns->entries;
4608 gfc_current_ns->entries = el;
4610 el->id = el->next->id + 1;
4614 new_st.op = EXEC_ENTRY;
4615 new_st.ext.entry = el;
4621 /* Match a subroutine statement, including optional prefixes. */
4624 gfc_match_subroutine (void)
4626 char name[GFC_MAX_SYMBOL_LEN + 1];
4631 bool allow_binding_name;
4633 if (gfc_current_state () != COMP_NONE
4634 && gfc_current_state () != COMP_INTERFACE
4635 && gfc_current_state () != COMP_CONTAINS)
4638 m = match_prefix (NULL);
4642 m = gfc_match ("subroutine% %n", name);
4646 if (get_proc_name (name, &sym, false))
4648 gfc_new_block = sym;
4650 /* Check what next non-whitespace character is so we can tell if there
4651 is the required parens if we have a BIND(C). */
4652 gfc_gobble_whitespace ();
4653 peek_char = gfc_peek_char ();
4655 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
4658 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4661 /* Make sure that it isn't already declared as BIND(C). If it is, it
4662 must have been marked BIND(C) with a BIND(C) attribute and that is
4663 not allowed for procedures. */
4664 if (sym->attr.is_bind_c == 1)
4666 sym->attr.is_bind_c = 0;
4667 if (sym->old_symbol != NULL)
4668 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4669 "variables or common blocks",
4670 &(sym->old_symbol->declared_at));
4672 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4673 "variables or common blocks", &gfc_current_locus);
4676 /* C binding names are not allowed for internal procedures. */
4677 if (gfc_current_state () == COMP_CONTAINS
4678 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4679 allow_binding_name = false;
4681 allow_binding_name = true;
4683 /* Here, we are just checking if it has the bind(c) attribute, and if
4684 so, then we need to make sure it's all correct. If it doesn't,
4685 we still need to continue matching the rest of the subroutine line. */
4686 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4687 if (is_bind_c == MATCH_ERROR)
4689 /* There was an attempt at the bind(c), but it was wrong. An
4690 error message should have been printed w/in the gfc_match_bind_c
4691 so here we'll just return the MATCH_ERROR. */
4695 if (is_bind_c == MATCH_YES)
4697 /* The following is allowed in the Fortran 2008 draft. */
4698 if (gfc_current_state () == COMP_CONTAINS
4699 && sym->ns->proc_name->attr.flavor != FL_MODULE
4700 && gfc_notify_std (GFC_STD_GNU, "Extension: BIND(C) attribute at "
4701 "%L may not be specified for an internal procedure",
4706 if (peek_char != '(')
4708 gfc_error ("Missing required parentheses before BIND(C) at %C");
4711 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4716 if (gfc_match_eos () != MATCH_YES)
4718 gfc_syntax_error (ST_SUBROUTINE);
4722 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4729 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4730 given, and set the binding label in either the given symbol (if not
4731 NULL), or in the current_ts. The symbol may be NULL because we may
4732 encounter the BIND(C) before the declaration itself. Return
4733 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4734 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4735 or MATCH_YES if the specifier was correct and the binding label and
4736 bind(c) fields were set correctly for the given symbol or the
4737 current_ts. If allow_binding_name is false, no binding name may be
4741 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
4743 /* binding label, if exists */
4744 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4748 /* Initialize the flag that specifies whether we encountered a NAME=
4749 specifier or not. */
4750 has_name_equals = 0;
4752 /* Init the first char to nil so we can catch if we don't have
4753 the label (name attr) or the symbol name yet. */
4754 binding_label[0] = '\0';
4756 /* This much we have to be able to match, in this order, if
4757 there is a bind(c) label. */
4758 if (gfc_match (" bind ( c ") != MATCH_YES)
4761 /* Now see if there is a binding label, or if we've reached the
4762 end of the bind(c) attribute without one. */
4763 if (gfc_match_char (',') == MATCH_YES)
4765 if (gfc_match (" name = ") != MATCH_YES)
4767 gfc_error ("Syntax error in NAME= specifier for binding label "
4769 /* should give an error message here */
4773 has_name_equals = 1;
4775 /* Get the opening quote. */
4776 double_quote = MATCH_YES;
4777 single_quote = MATCH_YES;
4778 double_quote = gfc_match_char ('"');
4779 if (double_quote != MATCH_YES)
4780 single_quote = gfc_match_char ('\'');
4781 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4783 gfc_error ("Syntax error in NAME= specifier for binding label "
4788 /* Grab the binding label, using functions that will not lower
4789 case the names automatically. */
4790 if (gfc_match_name_C (binding_label) != MATCH_YES)
4793 /* Get the closing quotation. */
4794 if (double_quote == MATCH_YES)
4796 if (gfc_match_char ('"') != MATCH_YES)
4798 gfc_error ("Missing closing quote '\"' for binding label at %C");
4799 /* User started string with '"' so looked to match it. */
4805 if (gfc_match_char ('\'') != MATCH_YES)
4807 gfc_error ("Missing closing quote '\'' for binding label at %C");
4808 /* User started string with "'" char. */
4814 /* Get the required right paren. */
4815 if (gfc_match_char (')') != MATCH_YES)
4817 gfc_error ("Missing closing paren for binding label at %C");
4821 if (has_name_equals && !allow_binding_name)
4823 gfc_error ("No binding name is allowed in BIND(C) at %C");
4827 if (has_name_equals && sym != NULL && sym->attr.dummy)
4829 gfc_error ("For dummy procedure %s, no binding name is "
4830 "allowed in BIND(C) at %C", sym->name);
4835 /* Save the binding label to the symbol. If sym is null, we're
4836 probably matching the typespec attributes of a declaration and
4837 haven't gotten the name yet, and therefore, no symbol yet. */
4838 if (binding_label[0] != '\0')
4842 strcpy (sym->binding_label, binding_label);
4845 strcpy (curr_binding_label, binding_label);
4847 else if (allow_binding_name)
4849 /* No binding label, but if symbol isn't null, we
4850 can set the label for it here.
4851 If name="" or allow_binding_name is false, no C binding name is
4853 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4854 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4857 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
4858 && current_interface.type == INTERFACE_ABSTRACT)
4860 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
4868 /* Return nonzero if we're currently compiling a contained procedure. */
4871 contained_procedure (void)
4875 for (s=gfc_state_stack; s; s=s->previous)
4876 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
4877 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
4883 /* Set the kind of each enumerator. The kind is selected such that it is
4884 interoperable with the corresponding C enumeration type, making
4885 sure that -fshort-enums is honored. */
4890 enumerator_history *current_history = NULL;
4894 if (max_enum == NULL || enum_history == NULL)
4897 if (!gfc_option.fshort_enums)
4903 kind = gfc_integer_kinds[i++].kind;
4905 while (kind < gfc_c_int_kind
4906 && gfc_check_integer_range (max_enum->initializer->value.integer,
4909 current_history = enum_history;
4910 while (current_history != NULL)
4912 current_history->sym->ts.kind = kind;
4913 current_history = current_history->next;
4918 /* Match any of the various end-block statements. Returns the type of
4919 END to the caller. The END INTERFACE, END IF, END DO and END
4920 SELECT statements cannot be replaced by a single END statement. */
4923 gfc_match_end (gfc_statement *st)
4925 char name[GFC_MAX_SYMBOL_LEN + 1];
4926 gfc_compile_state state;
4928 const char *block_name;
4933 old_loc = gfc_current_locus;
4934 if (gfc_match ("end") != MATCH_YES)
4937 state = gfc_current_state ();
4938 block_name = gfc_current_block () == NULL
4939 ? NULL : gfc_current_block ()->name;
4941 if (state == COMP_CONTAINS)
4943 state = gfc_state_stack->previous->state;
4944 block_name = gfc_state_stack->previous->sym == NULL
4945 ? NULL : gfc_state_stack->previous->sym->name;
4952 *st = ST_END_PROGRAM;
4953 target = " program";
4957 case COMP_SUBROUTINE:
4958 *st = ST_END_SUBROUTINE;
4959 target = " subroutine";
4960 eos_ok = !contained_procedure ();
4964 *st = ST_END_FUNCTION;
4965 target = " function";
4966 eos_ok = !contained_procedure ();
4969 case COMP_BLOCK_DATA:
4970 *st = ST_END_BLOCK_DATA;
4971 target = " block data";
4976 *st = ST_END_MODULE;
4981 case COMP_INTERFACE:
4982 *st = ST_END_INTERFACE;
4983 target = " interface";
5006 *st = ST_END_SELECT;
5012 *st = ST_END_FORALL;
5027 last_initializer = NULL;
5029 gfc_free_enum_history ();
5033 gfc_error ("Unexpected END statement at %C");
5037 if (gfc_match_eos () == MATCH_YES)
5041 /* We would have required END [something]. */
5042 gfc_error ("%s statement expected at %L",
5043 gfc_ascii_statement (*st), &old_loc);
5050 /* Verify that we've got the sort of end-block that we're expecting. */
5051 if (gfc_match (target) != MATCH_YES)
5053 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5057 /* If we're at the end, make sure a block name wasn't required. */
5058 if (gfc_match_eos () == MATCH_YES)
5061 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5062 && *st != ST_END_FORALL && *st != ST_END_WHERE)
5065 if (gfc_current_block () == NULL)
5068 gfc_error ("Expected block name of '%s' in %s statement at %C",
5069 block_name, gfc_ascii_statement (*st));
5074 /* END INTERFACE has a special handler for its several possible endings. */
5075 if (*st == ST_END_INTERFACE)
5076 return gfc_match_end_interface ();
5078 /* We haven't hit the end of statement, so what is left must be an
5080 m = gfc_match_space ();
5082 m = gfc_match_name (name);
5085 gfc_error ("Expected terminating name at %C");
5089 if (block_name == NULL)
5092 if (strcmp (name, block_name) != 0)
5094 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5095 gfc_ascii_statement (*st));
5099 if (gfc_match_eos () == MATCH_YES)
5103 gfc_syntax_error (*st);
5106 gfc_current_locus = old_loc;
5112 /***************** Attribute declaration statements ****************/
5114 /* Set the attribute of a single variable. */
5119 char name[GFC_MAX_SYMBOL_LEN + 1];
5127 m = gfc_match_name (name);
5131 if (find_special (name, &sym))
5134 var_locus = gfc_current_locus;
5136 /* Deal with possible array specification for certain attributes. */
5137 if (current_attr.dimension
5138 || current_attr.allocatable
5139 || current_attr.pointer
5140 || current_attr.target)
5142 m = gfc_match_array_spec (&as);
5143 if (m == MATCH_ERROR)
5146 if (current_attr.dimension && m == MATCH_NO)
5148 gfc_error ("Missing array specification at %L in DIMENSION "
5149 "statement", &var_locus);
5154 if (current_attr.dimension && sym->value)
5156 gfc_error ("Dimensions specified for %s at %L after its "
5157 "initialisation", sym->name, &var_locus);
5162 if ((current_attr.allocatable || current_attr.pointer)
5163 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5165 gfc_error ("Array specification must be deferred at %L", &var_locus);
5171 /* Update symbol table. DIMENSION attribute is set
5172 in gfc_set_array_spec(). */
5173 if (current_attr.dimension == 0
5174 && gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
5180 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5186 if (sym->attr.cray_pointee && sym->as != NULL)
5188 /* Fix the array spec. */
5189 m = gfc_mod_pointee_as (sym->as);
5190 if (m == MATCH_ERROR)
5194 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
5200 if ((current_attr.external || current_attr.intrinsic)
5201 && sym->attr.flavor != FL_PROCEDURE
5202 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
5211 gfc_free_array_spec (as);
5216 /* Generic attribute declaration subroutine. Used for attributes that
5217 just have a list of names. */
5224 /* Gobble the optional double colon, by simply ignoring the result
5234 if (gfc_match_eos () == MATCH_YES)
5240 if (gfc_match_char (',') != MATCH_YES)
5242 gfc_error ("Unexpected character in variable list at %C");
5252 /* This routine matches Cray Pointer declarations of the form:
5253 pointer ( <pointer>, <pointee> )
5255 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5256 The pointer, if already declared, should be an integer. Otherwise, we
5257 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5258 be either a scalar, or an array declaration. No space is allocated for
5259 the pointee. For the statement
5260 pointer (ipt, ar(10))
5261 any subsequent uses of ar will be translated (in C-notation) as
5262 ar(i) => ((<type> *) ipt)(i)
5263 After gimplification, pointee variable will disappear in the code. */
5266 cray_pointer_decl (void)
5270 gfc_symbol *cptr; /* Pointer symbol. */
5271 gfc_symbol *cpte; /* Pointee symbol. */
5277 if (gfc_match_char ('(') != MATCH_YES)
5279 gfc_error ("Expected '(' at %C");
5283 /* Match pointer. */
5284 var_locus = gfc_current_locus;
5285 gfc_clear_attr (¤t_attr);
5286 gfc_add_cray_pointer (¤t_attr, &var_locus);
5287 current_ts.type = BT_INTEGER;
5288 current_ts.kind = gfc_index_integer_kind;
5290 m = gfc_match_symbol (&cptr, 0);
5293 gfc_error ("Expected variable name at %C");
5297 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5300 gfc_set_sym_referenced (cptr);
5302 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5304 cptr->ts.type = BT_INTEGER;
5305 cptr->ts.kind = gfc_index_integer_kind;
5307 else if (cptr->ts.type != BT_INTEGER)
5309 gfc_error ("Cray pointer at %C must be an integer");
5312 else if (cptr->ts.kind < gfc_index_integer_kind)
5313 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5314 " memory addresses require %d bytes",
5315 cptr->ts.kind, gfc_index_integer_kind);
5317 if (gfc_match_char (',') != MATCH_YES)
5319 gfc_error ("Expected \",\" at %C");
5323 /* Match Pointee. */
5324 var_locus = gfc_current_locus;
5325 gfc_clear_attr (¤t_attr);
5326 gfc_add_cray_pointee (¤t_attr, &var_locus);
5327 current_ts.type = BT_UNKNOWN;
5328 current_ts.kind = 0;
5330 m = gfc_match_symbol (&cpte, 0);
5333 gfc_error ("Expected variable name at %C");
5337 /* Check for an optional array spec. */
5338 m = gfc_match_array_spec (&as);
5339 if (m == MATCH_ERROR)
5341 gfc_free_array_spec (as);
5344 else if (m == MATCH_NO)
5346 gfc_free_array_spec (as);
5350 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5353 gfc_set_sym_referenced (cpte);
5355 if (cpte->as == NULL)
5357 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5358 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5360 else if (as != NULL)
5362 gfc_error ("Duplicate array spec for Cray pointee at %C");
5363 gfc_free_array_spec (as);
5369 if (cpte->as != NULL)
5371 /* Fix array spec. */
5372 m = gfc_mod_pointee_as (cpte->as);
5373 if (m == MATCH_ERROR)
5377 /* Point the Pointee at the Pointer. */
5378 cpte->cp_pointer = cptr;
5380 if (gfc_match_char (')') != MATCH_YES)
5382 gfc_error ("Expected \")\" at %C");
5385 m = gfc_match_char (',');
5387 done = true; /* Stop searching for more declarations. */
5391 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5392 || gfc_match_eos () != MATCH_YES)
5394 gfc_error ("Expected \",\" or end of statement at %C");
5402 gfc_match_external (void)
5405 gfc_clear_attr (¤t_attr);
5406 current_attr.external = 1;
5408 return attr_decl ();
5413 gfc_match_intent (void)
5417 intent = match_intent_spec ();
5418 if (intent == INTENT_UNKNOWN)
5421 gfc_clear_attr (¤t_attr);
5422 current_attr.intent = intent;
5424 return attr_decl ();
5429 gfc_match_intrinsic (void)
5432 gfc_clear_attr (¤t_attr);
5433 current_attr.intrinsic = 1;
5435 return attr_decl ();
5440 gfc_match_optional (void)
5443 gfc_clear_attr (¤t_attr);
5444 current_attr.optional = 1;
5446 return attr_decl ();
5451 gfc_match_pointer (void)
5453 gfc_gobble_whitespace ();
5454 if (gfc_peek_char () == '(')
5456 if (!gfc_option.flag_cray_pointer)
5458 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5462 return cray_pointer_decl ();
5466 gfc_clear_attr (¤t_attr);
5467 current_attr.pointer = 1;
5469 return attr_decl ();
5475 gfc_match_allocatable (void)
5477 gfc_clear_attr (¤t_attr);
5478 current_attr.allocatable = 1;
5480 return attr_decl ();
5485 gfc_match_dimension (void)
5487 gfc_clear_attr (¤t_attr);
5488 current_attr.dimension = 1;
5490 return attr_decl ();
5495 gfc_match_target (void)
5497 gfc_clear_attr (¤t_attr);
5498 current_attr.target = 1;
5500 return attr_decl ();
5504 /* Match the list of entities being specified in a PUBLIC or PRIVATE
5508 access_attr_decl (gfc_statement st)
5510 char name[GFC_MAX_SYMBOL_LEN + 1];
5511 interface_type type;
5514 gfc_intrinsic_op operator;
5517 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5522 m = gfc_match_generic_spec (&type, name, &operator);
5525 if (m == MATCH_ERROR)
5530 case INTERFACE_NAMELESS:
5531 case INTERFACE_ABSTRACT:
5534 case INTERFACE_GENERIC:
5535 if (gfc_get_symbol (name, NULL, &sym))
5538 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5539 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
5540 sym->name, NULL) == FAILURE)
5545 case INTERFACE_INTRINSIC_OP:
5546 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
5548 gfc_current_ns->operator_access[operator] =
5549 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5553 gfc_error ("Access specification of the %s operator at %C has "
5554 "already been specified", gfc_op2string (operator));
5560 case INTERFACE_USER_OP:
5561 uop = gfc_get_uop (name);
5563 if (uop->access == ACCESS_UNKNOWN)
5565 uop->access = (st == ST_PUBLIC)
5566 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5570 gfc_error ("Access specification of the .%s. operator at %C "
5571 "has already been specified", sym->name);
5578 if (gfc_match_char (',') == MATCH_NO)
5582 if (gfc_match_eos () != MATCH_YES)
5587 gfc_syntax_error (st);
5595 gfc_match_protected (void)
5600 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5602 gfc_error ("PROTECTED at %C only allowed in specification "
5603 "part of a module");
5608 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED 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_protected (&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 PROTECTED statement at %C");
5653 /* The PRIVATE statement is a bit weird in that it can be an attribute
5654 declaration, but also works as a standlone statement inside of a
5655 type declaration or a module. */
5658 gfc_match_private (gfc_statement *st)
5661 if (gfc_match ("private") != MATCH_YES)
5664 if (gfc_current_state () != COMP_MODULE
5665 && (gfc_current_state () != COMP_DERIVED
5666 || !gfc_state_stack->previous
5667 || gfc_state_stack->previous->state != COMP_MODULE))
5669 gfc_error ("PRIVATE statement at %C is only allowed in the "
5670 "specification part of a module");
5674 if (gfc_current_state () == COMP_DERIVED)
5676 if (gfc_match_eos () == MATCH_YES)
5682 gfc_syntax_error (ST_PRIVATE);
5686 if (gfc_match_eos () == MATCH_YES)
5693 return access_attr_decl (ST_PRIVATE);
5698 gfc_match_public (gfc_statement *st)
5701 if (gfc_match ("public") != MATCH_YES)
5704 if (gfc_current_state () != COMP_MODULE)
5706 gfc_error ("PUBLIC statement at %C is only allowed in the "
5707 "specification part of a module");
5711 if (gfc_match_eos () == MATCH_YES)
5718 return access_attr_decl (ST_PUBLIC);
5722 /* Workhorse for gfc_match_parameter. */
5731 m = gfc_match_symbol (&sym, 0);
5733 gfc_error ("Expected variable name at %C in PARAMETER statement");
5738 if (gfc_match_char ('=') == MATCH_NO)
5740 gfc_error ("Expected = sign in PARAMETER statement at %C");
5744 m = gfc_match_init_expr (&init);
5746 gfc_error ("Expected expression at %C in PARAMETER statement");
5750 if (sym->ts.type == BT_UNKNOWN
5751 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5757 if (gfc_check_assign_symbol (sym, init) == FAILURE
5758 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
5766 gfc_error ("Initializing already initialized variable at %C");
5771 if (sym->ts.type == BT_CHARACTER
5772 && sym->ts.cl != NULL
5773 && sym->ts.cl->length != NULL
5774 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5775 && init->expr_type == EXPR_CONSTANT
5776 && init->ts.type == BT_CHARACTER
5777 && init->ts.kind == 1)
5778 gfc_set_constant_character_len (
5779 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
5785 gfc_free_expr (init);
5790 /* Match a parameter statement, with the weird syntax that these have. */
5793 gfc_match_parameter (void)
5797 if (gfc_match_char ('(') == MATCH_NO)
5806 if (gfc_match (" )%t") == MATCH_YES)
5809 if (gfc_match_char (',') != MATCH_YES)
5811 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5821 /* Save statements have a special syntax. */
5824 gfc_match_save (void)
5826 char n[GFC_MAX_SYMBOL_LEN+1];
5831 if (gfc_match_eos () == MATCH_YES)
5833 if (gfc_current_ns->seen_save)
5835 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5836 "follows previous SAVE statement")
5841 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5845 if (gfc_current_ns->save_all)
5847 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5848 "blanket SAVE statement")
5857 m = gfc_match_symbol (&sym, 0);
5861 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5873 m = gfc_match (" / %n /", &n);
5874 if (m == MATCH_ERROR)
5879 c = gfc_get_common (n, 0);
5882 gfc_current_ns->seen_save = 1;
5885 if (gfc_match_eos () == MATCH_YES)
5887 if (gfc_match_char (',') != MATCH_YES)
5894 gfc_error ("Syntax error in SAVE statement at %C");
5900 gfc_match_value (void)
5905 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
5909 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5914 if (gfc_match_eos () == MATCH_YES)
5919 m = gfc_match_symbol (&sym, 0);
5923 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5936 if (gfc_match_eos () == MATCH_YES)
5938 if (gfc_match_char (',') != MATCH_YES)
5945 gfc_error ("Syntax error in VALUE statement at %C");
5951 gfc_match_volatile (void)
5956 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
5960 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5965 if (gfc_match_eos () == MATCH_YES)
5970 /* VOLATILE is special because it can be added to host-associated
5972 m = gfc_match_symbol (&sym, 1);
5976 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
5989 if (gfc_match_eos () == MATCH_YES)
5991 if (gfc_match_char (',') != MATCH_YES)
5998 gfc_error ("Syntax error in VOLATILE statement at %C");
6003 /* Match a module procedure statement. Note that we have to modify
6004 symbols in the parent's namespace because the current one was there
6005 to receive symbols that are in an interface's formal argument list. */
6008 gfc_match_modproc (void)
6010 char name[GFC_MAX_SYMBOL_LEN + 1];
6013 gfc_namespace *module_ns;
6014 gfc_interface *old_interface_head, *interface;
6016 if (gfc_state_stack->state != COMP_INTERFACE
6017 || gfc_state_stack->previous == NULL
6018 || current_interface.type == INTERFACE_NAMELESS
6019 || current_interface.type == INTERFACE_ABSTRACT)
6021 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6026 module_ns = gfc_current_ns->parent;
6027 for (; module_ns; module_ns = module_ns->parent)
6028 if (module_ns->proc_name->attr.flavor == FL_MODULE)
6031 if (module_ns == NULL)
6034 /* Store the current state of the interface. We will need it if we
6035 end up with a syntax error and need to recover. */
6036 old_interface_head = gfc_current_interface_head ();
6042 m = gfc_match_name (name);
6048 /* Check for syntax error before starting to add symbols to the
6049 current namespace. */
6050 if (gfc_match_eos () == MATCH_YES)
6052 if (!last && gfc_match_char (',') != MATCH_YES)
6055 /* Now we're sure the syntax is valid, we process this item
6057 if (gfc_get_symbol (name, module_ns, &sym))
6060 if (sym->attr.proc != PROC_MODULE
6061 && gfc_add_procedure (&sym->attr, PROC_MODULE,
6062 sym->name, NULL) == FAILURE)
6065 if (gfc_add_interface (sym) == FAILURE)
6068 sym->attr.mod_proc = 1;
6077 /* Restore the previous state of the interface. */
6078 interface = gfc_current_interface_head ();
6079 gfc_set_current_interface_head (old_interface_head);
6081 /* Free the new interfaces. */
6082 while (interface != old_interface_head)
6084 gfc_interface *i = interface->next;
6085 gfc_free (interface);
6089 /* And issue a syntax error. */
6090 gfc_syntax_error (ST_MODULE_PROC);
6095 /* Match the optional attribute specifiers for a type declaration.
6096 Return MATCH_ERROR if an error is encountered in one of the handled
6097 attributes (public, private, bind(c)), MATCH_NO if what's found is
6098 not a handled attribute, and MATCH_YES otherwise. TODO: More error
6099 checking on attribute conflicts needs to be done. */
6102 gfc_get_type_attr_spec (symbol_attribute *attr)
6104 /* See if the derived type is marked as private. */
6105 if (gfc_match (" , private") == MATCH_YES)
6107 if (gfc_current_state () != COMP_MODULE)
6109 gfc_error ("Derived type at %C can only be PRIVATE in the "
6110 "specification part of a module");
6114 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6117 else if (gfc_match (" , public") == MATCH_YES)
6119 if (gfc_current_state () != COMP_MODULE)
6121 gfc_error ("Derived type at %C can only be PUBLIC in the "
6122 "specification part of a module");
6126 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6129 else if (gfc_match(" , bind ( c )") == MATCH_YES)
6131 /* If the type is defined to be bind(c) it then needs to make
6132 sure that all fields are interoperable. This will
6133 need to be a semantic check on the finished derived type.
6134 See 15.2.3 (lines 9-12) of F2003 draft. */
6135 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
6138 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
6143 /* If we get here, something matched. */
6148 /* Match the beginning of a derived type declaration. If a type name
6149 was the result of a function, then it is possible to have a symbol
6150 already to be known as a derived type yet have no components. */
6153 gfc_match_derived_decl (void)
6155 char name[GFC_MAX_SYMBOL_LEN + 1];
6156 symbol_attribute attr;
6159 match is_type_attr_spec = MATCH_NO;
6160 bool seen_attr = false;
6162 if (gfc_current_state () == COMP_DERIVED)
6165 gfc_clear_attr (&attr);
6169 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
6170 if (is_type_attr_spec == MATCH_ERROR)
6172 if (is_type_attr_spec == MATCH_YES)
6174 } while (is_type_attr_spec == MATCH_YES);
6176 if (gfc_match (" ::") != MATCH_YES && seen_attr)
6178 gfc_error ("Expected :: in TYPE definition at %C");
6182 m = gfc_match (" %n%t", name);
6186 /* Make sure the name is not the name of an intrinsic type. */
6187 if (gfc_is_intrinsic_typename (name))
6189 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
6194 if (gfc_get_symbol (name, NULL, &sym))
6197 if (sym->ts.type != BT_UNKNOWN)
6199 gfc_error ("Derived type name '%s' at %C already has a basic type "
6200 "of %s", sym->name, gfc_typename (&sym->ts));
6204 /* The symbol may already have the derived attribute without the
6205 components. The ways this can happen is via a function
6206 definition, an INTRINSIC statement or a subtype in another
6207 derived type that is a pointer. The first part of the AND clause
6208 is true if a the symbol is not the return value of a function. */
6209 if (sym->attr.flavor != FL_DERIVED
6210 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6213 if (sym->components != NULL || sym->attr.zero_comp)
6215 gfc_error ("Derived type definition of '%s' at %C has already been "
6216 "defined", sym->name);
6220 if (attr.access != ACCESS_UNKNOWN
6221 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6224 /* See if the derived type was labeled as bind(c). */
6225 if (attr.is_bind_c != 0)
6226 sym->attr.is_bind_c = attr.is_bind_c;
6228 gfc_new_block = sym;
6234 /* Cray Pointees can be declared as:
6235 pointer (ipt, a (n,m,...,*))
6236 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6237 cheat and set a constant bound of 1 for the last dimension, if this
6238 is the case. Since there is no bounds-checking for Cray Pointees,
6239 this will be okay. */
6242 gfc_mod_pointee_as (gfc_array_spec *as)
6244 as->cray_pointee = true; /* This will be useful to know later. */
6245 if (as->type == AS_ASSUMED_SIZE)
6247 as->type = AS_EXPLICIT;
6248 as->upper[as->rank - 1] = gfc_int_expr (1);
6249 as->cp_was_assumed = true;
6251 else if (as->type == AS_ASSUMED_SHAPE)
6253 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6260 /* Match the enum definition statement, here we are trying to match
6261 the first line of enum definition statement.
6262 Returns MATCH_YES if match is found. */
6265 gfc_match_enum (void)
6269 m = gfc_match_eos ();
6273 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
6281 /* Match a variable name with an optional initializer. When this
6282 subroutine is called, a variable is expected to be parsed next.
6283 Depending on what is happening at the moment, updates either the
6284 symbol table or the current interface. */
6287 enumerator_decl (void)
6289 char name[GFC_MAX_SYMBOL_LEN + 1];
6290 gfc_expr *initializer;
6291 gfc_array_spec *as = NULL;
6299 old_locus = gfc_current_locus;
6301 /* When we get here, we've just matched a list of attributes and
6302 maybe a type and a double colon. The next thing we expect to see
6303 is the name of the symbol. */
6304 m = gfc_match_name (name);
6308 var_locus = gfc_current_locus;
6310 /* OK, we've successfully matched the declaration. Now put the
6311 symbol in the current namespace. If we fail to create the symbol,
6313 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6319 /* The double colon must be present in order to have initializers.
6320 Otherwise the statement is ambiguous with an assignment statement. */
6323 if (gfc_match_char ('=') == MATCH_YES)
6325 m = gfc_match_init_expr (&initializer);
6328 gfc_error ("Expected an initialization expression at %C");
6337 /* If we do not have an initializer, the initialization value of the
6338 previous enumerator (stored in last_initializer) is incremented
6339 by 1 and is used to initialize the current enumerator. */
6340 if (initializer == NULL)
6341 initializer = gfc_enum_initializer (last_initializer, old_locus);
6343 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6345 gfc_error("ENUMERATOR %L not initialized with integer expression",
6348 gfc_free_enum_history ();
6352 /* Store this current initializer, for the next enumerator variable
6353 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6354 use last_initializer below. */
6355 last_initializer = initializer;
6356 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6358 /* Maintain enumerator history. */
6359 gfc_find_symbol (name, NULL, 0, &sym);
6360 create_enum_history (sym, last_initializer);
6362 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6365 /* Free stuff up and return. */
6366 gfc_free_expr (initializer);
6372 /* Match the enumerator definition statement. */
6375 gfc_match_enumerator_def (void)
6380 gfc_clear_ts (¤t_ts);
6382 m = gfc_match (" enumerator");
6386 m = gfc_match (" :: ");
6387 if (m == MATCH_ERROR)
6390 colon_seen = (m == MATCH_YES);
6392 if (gfc_current_state () != COMP_ENUM)
6394 gfc_error ("ENUM definition statement expected before %C");
6395 gfc_free_enum_history ();
6399 (¤t_ts)->type = BT_INTEGER;
6400 (¤t_ts)->kind = gfc_c_int_kind;
6402 gfc_clear_attr (¤t_attr);
6403 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
6412 m = enumerator_decl ();
6413 if (m == MATCH_ERROR)
6418 if (gfc_match_eos () == MATCH_YES)
6420 if (gfc_match_char (',') != MATCH_YES)
6424 if (gfc_current_state () == COMP_ENUM)
6426 gfc_free_enum_history ();
6427 gfc_error ("Syntax error in ENUMERATOR definition at %C");
6432 gfc_free_array_spec (current_as);