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 *result = gfc_copy_expr (sym->value);
378 /* Match a list of values in a DATA statement. The leading '/' has
379 already been seen at this point. */
382 top_val_list (gfc_data *data)
384 gfc_data_value *new, *tail;
392 m = match_data_constant (&expr);
395 if (m == MATCH_ERROR)
398 new = gfc_get_data_value ();
399 mpz_init (new->repeat);
408 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
411 mpz_set_ui (tail->repeat, 1);
415 if (expr->ts.type == BT_INTEGER)
416 mpz_set (tail->repeat, expr->value.integer);
417 gfc_free_expr (expr);
419 m = match_data_constant (&tail->expr);
422 if (m == MATCH_ERROR)
426 if (gfc_match_char ('/') == MATCH_YES)
428 if (gfc_match_char (',') == MATCH_NO)
435 gfc_syntax_error (ST_DATA);
436 gfc_free_data_all (gfc_current_ns);
441 /* Matches an old style initialization. */
444 match_old_style_init (const char *name)
451 /* Set up data structure to hold initializers. */
452 gfc_find_sym_tree (name, NULL, 0, &st);
455 newdata = gfc_get_data ();
456 newdata->var = gfc_get_data_variable ();
457 newdata->var->expr = gfc_get_variable_expr (st);
458 newdata->where = gfc_current_locus;
460 /* Match initial value list. This also eats the terminal '/'. */
461 m = top_val_list (newdata);
470 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
475 /* Mark the variable as having appeared in a data statement. */
476 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
482 /* Chain in namespace list of DATA initializers. */
483 newdata->next = gfc_current_ns->data;
484 gfc_current_ns->data = newdata;
490 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
491 we are matching a DATA statement and are therefore issuing an error
492 if we encounter something unexpected, if not, we're trying to match
493 an old-style initialization expression of the form INTEGER I /2/. */
496 gfc_match_data (void)
501 set_in_match_data (true);
505 new = gfc_get_data ();
506 new->where = gfc_current_locus;
508 m = top_var_list (new);
512 m = top_val_list (new);
516 new->next = gfc_current_ns->data;
517 gfc_current_ns->data = new;
519 if (gfc_match_eos () == MATCH_YES)
522 gfc_match_char (','); /* Optional comma */
525 set_in_match_data (false);
529 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
536 set_in_match_data (false);
542 /************************ Declaration statements *********************/
544 /* Match an intent specification. Since this can only happen after an
545 INTENT word, a legal intent-spec must follow. */
548 match_intent_spec (void)
551 if (gfc_match (" ( in out )") == MATCH_YES)
553 if (gfc_match (" ( in )") == MATCH_YES)
555 if (gfc_match (" ( out )") == MATCH_YES)
558 gfc_error ("Bad INTENT specification at %C");
559 return INTENT_UNKNOWN;
563 /* Matches a character length specification, which is either a
564 specification expression or a '*'. */
567 char_len_param_value (gfc_expr **expr)
571 if (gfc_match_char ('*') == MATCH_YES)
577 m = gfc_match_expr (expr);
578 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
580 if ((*expr)->value.function.actual
581 && (*expr)->value.function.actual->expr->symtree)
584 e = (*expr)->value.function.actual->expr;
585 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
586 && e->expr_type == EXPR_VARIABLE)
588 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
590 if (e->symtree->n.sym->ts.type == BT_CHARACTER
591 && e->symtree->n.sym->ts.cl
592 && e->symtree->n.sym->ts.cl->length->ts.type == BT_UNKNOWN)
600 gfc_error ("Conflict in attributes of function argument at %C");
605 /* A character length is a '*' followed by a literal integer or a
606 char_len_param_value in parenthesis. */
609 match_char_length (gfc_expr **expr)
614 m = gfc_match_char ('*');
618 m = gfc_match_small_literal_int (&length, NULL);
619 if (m == MATCH_ERROR)
624 *expr = gfc_int_expr (length);
628 if (gfc_match_char ('(') == MATCH_NO)
631 m = char_len_param_value (expr);
632 if (m == MATCH_ERROR)
637 if (gfc_match_char (')') == MATCH_NO)
639 gfc_free_expr (*expr);
647 gfc_error ("Syntax error in character length specification at %C");
652 /* Special subroutine for finding a symbol. Check if the name is found
653 in the current name space. If not, and we're compiling a function or
654 subroutine and the parent compilation unit is an interface, then check
655 to see if the name we've been given is the name of the interface
656 (located in another namespace). */
659 find_special (const char *name, gfc_symbol **result)
664 i = gfc_get_symbol (name, NULL, result);
668 if (gfc_current_state () != COMP_SUBROUTINE
669 && gfc_current_state () != COMP_FUNCTION)
672 s = gfc_state_stack->previous;
676 if (s->state != COMP_INTERFACE)
679 goto end; /* Nameless interface. */
681 if (strcmp (name, s->sym->name) == 0)
692 /* Special subroutine for getting a symbol node associated with a
693 procedure name, used in SUBROUTINE and FUNCTION statements. The
694 symbol is created in the parent using with symtree node in the
695 child unit pointing to the symbol. If the current namespace has no
696 parent, then the symbol is just created in the current unit. */
699 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
705 /* Module functions have to be left in their own namespace because
706 they have potentially (almost certainly!) already been referenced.
707 In this sense, they are rather like external functions. This is
708 fixed up in resolve.c(resolve_entries), where the symbol name-
709 space is set to point to the master function, so that the fake
710 result mechanism can work. */
711 if (module_fcn_entry)
713 /* Present if entry is declared to be a module procedure. */
714 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
717 rc = gfc_get_symbol (name, NULL, result);
718 else if (!gfc_get_symbol (name, NULL, &sym) && sym
719 && (*result)->ts.type == BT_UNKNOWN
720 && sym->attr.flavor == FL_UNKNOWN)
721 /* Pick up the typespec for the entry, if declared in the function
722 body. Note that this symbol is FL_UNKNOWN because it will
723 only have appeared in a type declaration. The local symtree
724 is set to point to the module symbol and a unique symtree
725 to the local version. This latter ensures a correct clearing
728 /* If the ENTRY proceeds its specification, we need to ensure
729 that this does not raise a "has no IMPLICIT type" error. */
730 if (sym->ts.type == BT_UNKNOWN)
731 sym->attr.untyped = 1;
733 (*result)->ts = sym->ts;
735 /* Put the symbol in the procedure namespace so that, should
736 the ENTRY preceed its specification, the specification
738 (*result)->ns = gfc_current_ns;
740 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
742 st = gfc_get_unique_symtree (gfc_current_ns);
747 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
753 gfc_current_ns->refs++;
755 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
757 /* Trap another encompassed procedure with the same name. All
758 these conditions are necessary to avoid picking up an entry
759 whose name clashes with that of the encompassing procedure;
760 this is handled using gsymbols to register unique,globally
762 if (sym->attr.flavor != 0
763 && sym->attr.proc != 0
764 && (sym->attr.subroutine || sym->attr.function)
765 && sym->attr.if_source != IFSRC_UNKNOWN)
766 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
767 name, &sym->declared_at);
769 /* Trap a procedure with a name the same as interface in the
770 encompassing scope. */
771 if (sym->attr.generic != 0
772 && (sym->attr.subroutine || sym->attr.function)
773 && !sym->attr.mod_proc)
774 gfc_error_now ("Name '%s' at %C is already defined"
775 " as a generic interface at %L",
776 name, &sym->declared_at);
778 /* Trap declarations of attributes in encompassing scope. The
779 signature for this is that ts.kind is set. Legitimate
780 references only set ts.type. */
781 if (sym->ts.kind != 0
782 && !sym->attr.implicit_type
783 && sym->attr.proc == 0
784 && gfc_current_ns->parent != NULL
785 && sym->attr.access == 0
786 && !module_fcn_entry)
787 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
788 "and must not have attributes declared at %L",
789 name, &sym->declared_at);
792 if (gfc_current_ns->parent == NULL || *result == NULL)
795 /* Module function entries will already have a symtree in
796 the current namespace but will need one at module level. */
797 if (module_fcn_entry)
799 /* Present if entry is declared to be a module procedure. */
800 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
802 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
805 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
810 /* See if the procedure should be a module procedure. */
812 if (((sym->ns->proc_name != NULL
813 && sym->ns->proc_name->attr.flavor == FL_MODULE
814 && sym->attr.proc != PROC_MODULE)
815 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
816 && gfc_add_procedure (&sym->attr, PROC_MODULE,
817 sym->name, NULL) == FAILURE)
824 /* Verify that the given symbol representing a parameter is C
825 interoperable, by checking to see if it was marked as such after
826 its declaration. If the given symbol is not interoperable, a
827 warning is reported, thus removing the need to return the status to
828 the calling function. The standard does not require the user use
829 one of the iso_c_binding named constants to declare an
830 interoperable parameter, but we can't be sure if the param is C
831 interop or not if the user doesn't. For example, integer(4) may be
832 legal Fortran, but doesn't have meaning in C. It may interop with
833 a number of the C types, which causes a problem because the
834 compiler can't know which one. This code is almost certainly not
835 portable, and the user will get what they deserve if the C type
836 across platforms isn't always interoperable with integer(4). If
837 the user had used something like integer(c_int) or integer(c_long),
838 the compiler could have automatically handled the varying sizes
842 verify_c_interop_param (gfc_symbol *sym)
844 int is_c_interop = 0;
845 try retval = SUCCESS;
847 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
848 Don't repeat the checks here. */
849 if (sym->attr.implicit_type)
852 /* For subroutines or functions that are passed to a BIND(C) procedure,
853 they're interoperable if they're BIND(C) and their params are all
855 if (sym->attr.flavor == FL_PROCEDURE)
857 if (sym->attr.is_bind_c == 0)
859 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
860 "attribute to be C interoperable", sym->name,
861 &(sym->declared_at));
867 if (sym->attr.is_c_interop == 1)
868 /* We've already checked this procedure; don't check it again. */
871 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
876 /* See if we've stored a reference to a procedure that owns sym. */
877 if (sym->ns != NULL && sym->ns->proc_name != NULL)
879 if (sym->ns->proc_name->attr.is_bind_c == 1)
882 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
885 if (is_c_interop != 1)
887 /* Make personalized messages to give better feedback. */
888 if (sym->ts.type == BT_DERIVED)
889 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
890 " procedure '%s' but is not C interoperable "
891 "because derived type '%s' is not C interoperable",
892 sym->name, &(sym->declared_at),
893 sym->ns->proc_name->name,
894 sym->ts.derived->name);
896 gfc_warning ("Variable '%s' at %L is a parameter to the "
897 "BIND(C) procedure '%s' but may not be C "
899 sym->name, &(sym->declared_at),
900 sym->ns->proc_name->name);
903 /* Character strings are only C interoperable if they have a
905 if (sym->ts.type == BT_CHARACTER)
907 gfc_charlen *cl = sym->ts.cl;
908 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
909 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
911 gfc_error ("Character argument '%s' at %L "
912 "must be length 1 because "
913 "procedure '%s' is BIND(C)",
914 sym->name, &sym->declared_at,
915 sym->ns->proc_name->name);
920 /* We have to make sure that any param to a bind(c) routine does
921 not have the allocatable, pointer, or optional attributes,
922 according to J3/04-007, section 5.1. */
923 if (sym->attr.allocatable == 1)
925 gfc_error ("Variable '%s' at %L cannot have the "
926 "ALLOCATABLE attribute because procedure '%s'"
927 " is BIND(C)", sym->name, &(sym->declared_at),
928 sym->ns->proc_name->name);
932 if (sym->attr.pointer == 1)
934 gfc_error ("Variable '%s' at %L cannot have the "
935 "POINTER attribute because procedure '%s'"
936 " is BIND(C)", sym->name, &(sym->declared_at),
937 sym->ns->proc_name->name);
941 if (sym->attr.optional == 1)
943 gfc_error ("Variable '%s' at %L cannot have the "
944 "OPTIONAL attribute because procedure '%s'"
945 " is BIND(C)", sym->name, &(sym->declared_at),
946 sym->ns->proc_name->name);
950 /* Make sure that if it has the dimension attribute, that it is
951 either assumed size or explicit shape. */
954 if (sym->as->type == AS_ASSUMED_SHAPE)
956 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
957 "argument to the procedure '%s' at %L because "
958 "the procedure is BIND(C)", sym->name,
959 &(sym->declared_at), sym->ns->proc_name->name,
960 &(sym->ns->proc_name->declared_at));
964 if (sym->as->type == AS_DEFERRED)
966 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
967 "argument to the procedure '%s' at %L because "
968 "the procedure is BIND(C)", sym->name,
969 &(sym->declared_at), sym->ns->proc_name->name,
970 &(sym->ns->proc_name->declared_at));
981 /* Function called by variable_decl() that adds a name to the symbol table. */
984 build_sym (const char *name, gfc_charlen *cl,
985 gfc_array_spec **as, locus *var_locus)
987 symbol_attribute attr;
990 if (gfc_get_symbol (name, NULL, &sym))
993 /* Start updating the symbol table. Add basic type attribute if present. */
994 if (current_ts.type != BT_UNKNOWN
995 && (sym->attr.implicit_type == 0
996 || !gfc_compare_types (&sym->ts, ¤t_ts))
997 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
1000 if (sym->ts.type == BT_CHARACTER)
1003 /* Add dimension attribute if present. */
1004 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1008 /* Add attribute to symbol. The copy is so that we can reset the
1009 dimension attribute. */
1010 attr = current_attr;
1013 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1016 /* Finish any work that may need to be done for the binding label,
1017 if it's a bind(c). The bind(c) attr is found before the symbol
1018 is made, and before the symbol name (for data decls), so the
1019 current_ts is holding the binding label, or nothing if the
1020 name= attr wasn't given. Therefore, test here if we're dealing
1021 with a bind(c) and make sure the binding label is set correctly. */
1022 if (sym->attr.is_bind_c == 1)
1024 if (sym->binding_label[0] == '\0')
1026 /* Set the binding label and verify that if a NAME= was specified
1027 then only one identifier was in the entity-decl-list. */
1028 if (set_binding_label (sym->binding_label, sym->name,
1029 num_idents_on_line) == FAILURE)
1034 /* See if we know we're in a common block, and if it's a bind(c)
1035 common then we need to make sure we're an interoperable type. */
1036 if (sym->attr.in_common == 1)
1038 /* Test the common block object. */
1039 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1040 && sym->ts.is_c_interop != 1)
1042 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1043 "must be declared with a C interoperable "
1044 "kind since common block '%s' is BIND(C)",
1045 sym->name, sym->common_block->name,
1046 sym->common_block->name);
1051 sym->attr.implied_index = 0;
1057 /* Set character constant to the given length. The constant will be padded or
1061 gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
1066 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1067 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
1069 slen = expr->value.character.length;
1072 s = gfc_getmem (len + 1);
1073 memcpy (s, expr->value.character.string, MIN (len, slen));
1075 memset (&s[slen], ' ', len - slen);
1077 if (gfc_option.warn_character_truncation && slen > len)
1078 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1079 "(%d/%d)", &expr->where, slen, len);
1081 /* Apply the standard by 'hand' otherwise it gets cleared for
1083 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
1084 gfc_error_now ("The CHARACTER elements of the array constructor "
1085 "at %L must have the same length (%d/%d)",
1086 &expr->where, slen, len);
1089 gfc_free (expr->value.character.string);
1090 expr->value.character.string = s;
1091 expr->value.character.length = len;
1096 /* Function to create and update the enumerator history
1097 using the information passed as arguments.
1098 Pointer "max_enum" is also updated, to point to
1099 enum history node containing largest initializer.
1101 SYM points to the symbol node of enumerator.
1102 INIT points to its enumerator value. */
1105 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1107 enumerator_history *new_enum_history;
1108 gcc_assert (sym != NULL && init != NULL);
1110 new_enum_history = gfc_getmem (sizeof (enumerator_history));
1112 new_enum_history->sym = sym;
1113 new_enum_history->initializer = init;
1114 new_enum_history->next = NULL;
1116 if (enum_history == NULL)
1118 enum_history = new_enum_history;
1119 max_enum = enum_history;
1123 new_enum_history->next = enum_history;
1124 enum_history = new_enum_history;
1126 if (mpz_cmp (max_enum->initializer->value.integer,
1127 new_enum_history->initializer->value.integer) < 0)
1128 max_enum = new_enum_history;
1133 /* Function to free enum kind history. */
1136 gfc_free_enum_history (void)
1138 enumerator_history *current = enum_history;
1139 enumerator_history *next;
1141 while (current != NULL)
1143 next = current->next;
1148 enum_history = NULL;
1152 /* Function called by variable_decl() that adds an initialization
1153 expression to a symbol. */
1156 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1158 symbol_attribute attr;
1163 if (find_special (name, &sym))
1168 /* If this symbol is confirming an implicit parameter type,
1169 then an initialization expression is not allowed. */
1170 if (attr.flavor == FL_PARAMETER
1171 && sym->value != NULL
1174 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1181 /* An initializer is required for PARAMETER declarations. */
1182 if (attr.flavor == FL_PARAMETER)
1184 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1190 /* If a variable appears in a DATA block, it cannot have an
1194 gfc_error ("Variable '%s' at %C with an initializer already "
1195 "appears in a DATA statement", sym->name);
1199 /* Check if the assignment can happen. This has to be put off
1200 until later for a derived type variable. */
1201 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1202 && gfc_check_assign_symbol (sym, init) == FAILURE)
1205 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1207 /* Update symbol character length according initializer. */
1208 if (sym->ts.cl->length == NULL)
1211 /* If there are multiple CHARACTER variables declared on the
1212 same line, we don't want them to share the same length. */
1213 sym->ts.cl = gfc_get_charlen ();
1214 sym->ts.cl->next = gfc_current_ns->cl_list;
1215 gfc_current_ns->cl_list = sym->ts.cl;
1217 if (sym->attr.flavor == FL_PARAMETER)
1219 if (init->expr_type == EXPR_CONSTANT)
1221 clen = init->value.character.length;
1222 sym->ts.cl->length = gfc_int_expr (clen);
1224 else if (init->expr_type == EXPR_ARRAY)
1226 gfc_expr *p = init->value.constructor->expr;
1227 clen = p->value.character.length;
1228 sym->ts.cl->length = gfc_int_expr (clen);
1230 else if (init->ts.cl && init->ts.cl->length)
1231 sym->ts.cl->length =
1232 gfc_copy_expr (sym->value->ts.cl->length);
1235 /* Update initializer character length according symbol. */
1236 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1238 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1239 gfc_constructor * p;
1241 if (init->expr_type == EXPR_CONSTANT)
1242 gfc_set_constant_character_len (len, init, false);
1243 else if (init->expr_type == EXPR_ARRAY)
1245 /* Build a new charlen to prevent simplification from
1246 deleting the length before it is resolved. */
1247 init->ts.cl = gfc_get_charlen ();
1248 init->ts.cl->next = gfc_current_ns->cl_list;
1249 gfc_current_ns->cl_list = sym->ts.cl;
1250 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
1252 for (p = init->value.constructor; p; p = p->next)
1253 gfc_set_constant_character_len (len, p->expr, false);
1258 /* Need to check if the expression we initialized this
1259 to was one of the iso_c_binding named constants. If so,
1260 and we're a parameter (constant), let it be iso_c.
1262 integer(c_int), parameter :: my_int = c_int
1263 integer(my_int) :: my_int_2
1264 If we mark my_int as iso_c (since we can see it's value
1265 is equal to one of the named constants), then my_int_2
1266 will be considered C interoperable. */
1267 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1269 sym->ts.is_iso_c |= init->ts.is_iso_c;
1270 sym->ts.is_c_interop |= init->ts.is_c_interop;
1271 /* attr bits needed for module files. */
1272 sym->attr.is_iso_c |= init->ts.is_iso_c;
1273 sym->attr.is_c_interop |= init->ts.is_c_interop;
1274 if (init->ts.is_iso_c)
1275 sym->ts.f90_type = init->ts.f90_type;
1278 /* Add initializer. Make sure we keep the ranks sane. */
1279 if (sym->attr.dimension && init->rank == 0)
1285 if (sym->attr.flavor == FL_PARAMETER
1286 && init->expr_type == EXPR_CONSTANT
1287 && spec_size (sym->as, &size) == SUCCESS
1288 && mpz_cmp_si (size, 0) > 0)
1290 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1293 array->value.constructor = c = NULL;
1294 for (n = 0; n < (int)mpz_get_si (size); n++)
1296 if (array->value.constructor == NULL)
1298 array->value.constructor = c = gfc_get_constructor ();
1303 c->next = gfc_get_constructor ();
1305 c->expr = gfc_copy_expr (init);
1309 array->shape = gfc_get_shape (sym->as->rank);
1310 for (n = 0; n < sym->as->rank; n++)
1311 spec_dimen_size (sym->as, n, &array->shape[n]);
1316 init->rank = sym->as->rank;
1320 if (sym->attr.save == SAVE_NONE)
1321 sym->attr.save = SAVE_IMPLICIT;
1329 /* Function called by variable_decl() that adds a name to a structure
1333 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1334 gfc_array_spec **as)
1338 /* If the current symbol is of the same derived type that we're
1339 constructing, it must have the pointer attribute. */
1340 if (current_ts.type == BT_DERIVED
1341 && current_ts.derived == gfc_current_block ()
1342 && current_attr.pointer == 0)
1344 gfc_error ("Component at %C must have the POINTER attribute");
1348 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1350 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1352 gfc_error ("Array component of structure at %C must have explicit "
1353 "or deferred shape");
1358 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1363 gfc_set_component_attr (c, ¤t_attr);
1365 c->initializer = *init;
1373 /* Check array components. */
1378 gfc_error ("Allocatable component at %C must be an array");
1387 if (c->as->type != AS_DEFERRED)
1389 gfc_error ("Pointer array component of structure at %C must have a "
1394 else if (c->allocatable)
1396 if (c->as->type != AS_DEFERRED)
1398 gfc_error ("Allocatable component of structure at %C must have a "
1405 if (c->as->type != AS_EXPLICIT)
1407 gfc_error ("Array component of structure at %C must have an "
1417 /* Match a 'NULL()', and possibly take care of some side effects. */
1420 gfc_match_null (gfc_expr **result)
1426 m = gfc_match (" null ( )");
1430 /* The NULL symbol now has to be/become an intrinsic function. */
1431 if (gfc_get_symbol ("null", NULL, &sym))
1433 gfc_error ("NULL() initialization at %C is ambiguous");
1437 gfc_intrinsic_symbol (sym);
1439 if (sym->attr.proc != PROC_INTRINSIC
1440 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1441 sym->name, NULL) == FAILURE
1442 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1445 e = gfc_get_expr ();
1446 e->where = gfc_current_locus;
1447 e->expr_type = EXPR_NULL;
1448 e->ts.type = BT_UNKNOWN;
1456 /* Match a variable name with an optional initializer. When this
1457 subroutine is called, a variable is expected to be parsed next.
1458 Depending on what is happening at the moment, updates either the
1459 symbol table or the current interface. */
1462 variable_decl (int elem)
1464 char name[GFC_MAX_SYMBOL_LEN + 1];
1465 gfc_expr *initializer, *char_len;
1467 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1478 old_locus = gfc_current_locus;
1480 /* When we get here, we've just matched a list of attributes and
1481 maybe a type and a double colon. The next thing we expect to see
1482 is the name of the symbol. */
1483 m = gfc_match_name (name);
1487 var_locus = gfc_current_locus;
1489 /* Now we could see the optional array spec. or character length. */
1490 m = gfc_match_array_spec (&as);
1491 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1492 cp_as = gfc_copy_array_spec (as);
1493 else if (m == MATCH_ERROR)
1497 as = gfc_copy_array_spec (current_as);
1502 if (current_ts.type == BT_CHARACTER)
1504 switch (match_char_length (&char_len))
1507 cl = gfc_get_charlen ();
1508 cl->next = gfc_current_ns->cl_list;
1509 gfc_current_ns->cl_list = cl;
1511 cl->length = char_len;
1514 /* Non-constant lengths need to be copied after the first
1515 element. Also copy assumed lengths. */
1518 && (current_ts.cl->length == NULL
1519 || current_ts.cl->length->expr_type != EXPR_CONSTANT))
1521 cl = gfc_get_charlen ();
1522 cl->next = gfc_current_ns->cl_list;
1523 gfc_current_ns->cl_list = cl;
1524 cl->length = gfc_copy_expr (current_ts.cl->length);
1536 /* If this symbol has already shown up in a Cray Pointer declaration,
1537 then we want to set the type & bail out. */
1538 if (gfc_option.flag_cray_pointer)
1540 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1541 if (sym != NULL && sym->attr.cray_pointee)
1543 sym->ts.type = current_ts.type;
1544 sym->ts.kind = current_ts.kind;
1546 sym->ts.derived = current_ts.derived;
1547 sym->ts.is_c_interop = current_ts.is_c_interop;
1548 sym->ts.is_iso_c = current_ts.is_iso_c;
1551 /* Check to see if we have an array specification. */
1554 if (sym->as != NULL)
1556 gfc_error ("Duplicate array spec for Cray pointee at %C");
1557 gfc_free_array_spec (cp_as);
1563 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1564 gfc_internal_error ("Couldn't set pointee array spec.");
1566 /* Fix the array spec. */
1567 m = gfc_mod_pointee_as (sym->as);
1568 if (m == MATCH_ERROR)
1576 gfc_free_array_spec (cp_as);
1581 /* OK, we've successfully matched the declaration. Now put the
1582 symbol in the current namespace, because it might be used in the
1583 optional initialization expression for this symbol, e.g. this is
1586 integer, parameter :: i = huge(i)
1588 This is only true for parameters or variables of a basic type.
1589 For components of derived types, it is not true, so we don't
1590 create a symbol for those yet. If we fail to create the symbol,
1592 if (gfc_current_state () != COMP_DERIVED
1593 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1599 /* An interface body specifies all of the procedure's
1600 characteristics and these shall be consistent with those
1601 specified in the procedure definition, except that the interface
1602 may specify a procedure that is not pure if the procedure is
1603 defined to be pure(12.3.2). */
1604 if (current_ts.type == BT_DERIVED
1605 && gfc_current_ns->proc_name
1606 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1607 && current_ts.derived->ns != gfc_current_ns)
1610 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.derived->name);
1611 if (!(current_ts.derived->attr.imported
1613 && st->n.sym == current_ts.derived)
1614 && !gfc_current_ns->has_import_set)
1616 gfc_error ("the type of '%s' at %C has not been declared within the "
1623 /* In functions that have a RESULT variable defined, the function
1624 name always refers to function calls. Therefore, the name is
1625 not allowed to appear in specification statements. */
1626 if (gfc_current_state () == COMP_FUNCTION
1627 && gfc_current_block () != NULL
1628 && gfc_current_block ()->result != NULL
1629 && gfc_current_block ()->result != gfc_current_block ()
1630 && strcmp (gfc_current_block ()->name, name) == 0)
1632 gfc_error ("Function name '%s' not allowed at %C", name);
1637 /* We allow old-style initializations of the form
1638 integer i /2/, j(4) /3*3, 1/
1639 (if no colon has been seen). These are different from data
1640 statements in that initializers are only allowed to apply to the
1641 variable immediately preceding, i.e.
1643 is not allowed. Therefore we have to do some work manually, that
1644 could otherwise be left to the matchers for DATA statements. */
1646 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1648 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1649 "initialization at %C") == FAILURE)
1652 return match_old_style_init (name);
1655 /* The double colon must be present in order to have initializers.
1656 Otherwise the statement is ambiguous with an assignment statement. */
1659 if (gfc_match (" =>") == MATCH_YES)
1661 if (!current_attr.pointer)
1663 gfc_error ("Initialization at %C isn't for a pointer variable");
1668 m = gfc_match_null (&initializer);
1671 gfc_error ("Pointer initialization requires a NULL() at %C");
1675 if (gfc_pure (NULL))
1677 gfc_error ("Initialization of pointer at %C is not allowed in "
1678 "a PURE procedure");
1686 else if (gfc_match_char ('=') == MATCH_YES)
1688 if (current_attr.pointer)
1690 gfc_error ("Pointer initialization at %C requires '=>', "
1696 m = gfc_match_init_expr (&initializer);
1699 gfc_error ("Expected an initialization expression at %C");
1703 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1705 gfc_error ("Initialization of variable at %C is not allowed in "
1706 "a PURE procedure");
1715 if (initializer != NULL && current_attr.allocatable
1716 && gfc_current_state () == COMP_DERIVED)
1718 gfc_error ("Initialization of allocatable component at %C is not "
1724 /* Add the initializer. Note that it is fine if initializer is
1725 NULL here, because we sometimes also need to check if a
1726 declaration *must* have an initialization expression. */
1727 if (gfc_current_state () != COMP_DERIVED)
1728 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1731 if (current_ts.type == BT_DERIVED
1732 && !current_attr.pointer && !initializer)
1733 initializer = gfc_default_initializer (¤t_ts);
1734 t = build_struct (name, cl, &initializer, &as);
1737 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1740 /* Free stuff up and return. */
1741 gfc_free_expr (initializer);
1742 gfc_free_array_spec (as);
1748 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1749 This assumes that the byte size is equal to the kind number for
1750 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1753 gfc_match_old_kind_spec (gfc_typespec *ts)
1758 if (gfc_match_char ('*') != MATCH_YES)
1761 m = gfc_match_small_literal_int (&ts->kind, NULL);
1765 original_kind = ts->kind;
1767 /* Massage the kind numbers for complex types. */
1768 if (ts->type == BT_COMPLEX)
1772 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1773 gfc_basic_typename (ts->type), original_kind);
1779 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1781 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1782 gfc_basic_typename (ts->type), original_kind);
1786 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1787 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1794 /* Match a kind specification. Since kinds are generally optional, we
1795 usually return MATCH_NO if something goes wrong. If a "kind="
1796 string is found, then we know we have an error. */
1799 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
1810 where = loc = gfc_current_locus;
1815 if (gfc_match_char ('(') == MATCH_NO)
1818 /* Also gobbles optional text. */
1819 if (gfc_match (" kind = ") == MATCH_YES)
1822 loc = gfc_current_locus;
1825 n = gfc_match_init_expr (&e);
1829 if (gfc_current_state () == COMP_INTERFACE
1830 || gfc_current_state () == COMP_NONE
1831 || gfc_current_state () == COMP_CONTAINS)
1833 /* Signal using kind = -1 that the expression might include
1834 use associated or imported parameters and try again after
1835 the specification expressions..... */
1836 if (gfc_match_char (')') != MATCH_YES)
1838 gfc_error ("Missing right parenthesis at %C");
1845 gfc_function_kind_locus = loc;
1846 gfc_undo_symbols ();
1851 /* ....or else, the match is real. */
1853 gfc_error ("Expected initialization expression at %C");
1861 gfc_error ("Expected scalar initialization expression at %C");
1866 msg = gfc_extract_int (e, &ts->kind);
1874 /* Before throwing away the expression, let's see if we had a
1875 C interoperable kind (and store the fact). */
1876 if (e->ts.is_c_interop == 1)
1878 /* Mark this as c interoperable if being declared with one
1879 of the named constants from iso_c_binding. */
1880 ts->is_c_interop = e->ts.is_iso_c;
1881 ts->f90_type = e->ts.f90_type;
1887 /* Ignore errors to this point, if we've gotten here. This means
1888 we ignore the m=MATCH_ERROR from above. */
1889 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1891 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1892 gfc_basic_typename (ts->type));
1895 else if (gfc_match_char (')') != MATCH_YES)
1897 gfc_error ("Missing right parenthesis at %C");
1901 /* All tests passed. */
1904 if(m == MATCH_ERROR)
1905 gfc_current_locus = where;
1907 /* Return what we know from the test(s). */
1912 gfc_current_locus = where;
1918 match_char_kind (int * kind, int * is_iso_c)
1927 where = gfc_current_locus;
1929 n = gfc_match_init_expr (&e);
1931 gfc_error ("Expected initialization expression at %C");
1937 gfc_error ("Expected scalar initialization expression at %C");
1942 msg = gfc_extract_int (e, kind);
1943 *is_iso_c = e->ts.is_iso_c;
1953 /* Ignore errors to this point, if we've gotten here. This means
1954 we ignore the m=MATCH_ERROR from above. */
1955 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
1957 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
1961 /* All tests passed. */
1964 if (m == MATCH_ERROR)
1965 gfc_current_locus = where;
1967 /* Return what we know from the test(s). */
1972 gfc_current_locus = where;
1976 /* Match the various kind/length specifications in a CHARACTER
1977 declaration. We don't return MATCH_NO. */
1980 match_char_spec (gfc_typespec *ts)
1982 int kind, seen_length, is_iso_c;
1992 /* Try the old-style specification first. */
1993 old_char_selector = 0;
1995 m = match_char_length (&len);
1999 old_char_selector = 1;
2004 m = gfc_match_char ('(');
2007 m = MATCH_YES; /* Character without length is a single char. */
2011 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2012 if (gfc_match (" kind =") == MATCH_YES)
2014 m = match_char_kind (&kind, &is_iso_c);
2016 if (m == MATCH_ERROR)
2021 if (gfc_match (" , len =") == MATCH_NO)
2024 m = char_len_param_value (&len);
2027 if (m == MATCH_ERROR)
2034 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2035 if (gfc_match (" len =") == MATCH_YES)
2037 m = char_len_param_value (&len);
2040 if (m == MATCH_ERROR)
2044 if (gfc_match_char (')') == MATCH_YES)
2047 if (gfc_match (" , kind =") != MATCH_YES)
2050 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2056 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2057 m = char_len_param_value (&len);
2060 if (m == MATCH_ERROR)
2064 m = gfc_match_char (')');
2068 if (gfc_match_char (',') != MATCH_YES)
2071 gfc_match (" kind ="); /* Gobble optional text. */
2073 m = match_char_kind (&kind, &is_iso_c);
2074 if (m == MATCH_ERROR)
2080 /* Require a right-paren at this point. */
2081 m = gfc_match_char (')');
2086 gfc_error ("Syntax error in CHARACTER declaration at %C");
2088 gfc_free_expr (len);
2094 gfc_free_expr (len);
2098 /* Do some final massaging of the length values. */
2099 cl = gfc_get_charlen ();
2100 cl->next = gfc_current_ns->cl_list;
2101 gfc_current_ns->cl_list = cl;
2103 if (seen_length == 0)
2104 cl->length = gfc_int_expr (1);
2109 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2111 /* We have to know if it was a c interoperable kind so we can
2112 do accurate type checking of bind(c) procs, etc. */
2114 /* Mark this as c interoperable if being declared with one
2115 of the named constants from iso_c_binding. */
2116 ts->is_c_interop = is_iso_c;
2117 else if (len != NULL)
2118 /* Here, we might have parsed something such as: character(c_char)
2119 In this case, the parsing code above grabs the c_char when
2120 looking for the length (line 1690, roughly). it's the last
2121 testcase for parsing the kind params of a character variable.
2122 However, it's not actually the length. this seems like it
2124 To see if the user used a C interop kind, test the expr
2125 of the so called length, and see if it's C interoperable. */
2126 ts->is_c_interop = len->ts.is_iso_c;
2132 /* Matches a type specification. If successful, sets the ts structure
2133 to the matched specification. This is necessary for FUNCTION and
2134 IMPLICIT statements.
2136 If implicit_flag is nonzero, then we don't check for the optional
2137 kind specification. Not doing so is needed for matching an IMPLICIT
2138 statement correctly. */
2141 gfc_match_type_spec (gfc_typespec *ts, int implicit_flag)
2143 char name[GFC_MAX_SYMBOL_LEN + 1];
2147 locus loc = gfc_current_locus;
2151 /* Clear the current binding label, in case one is given. */
2152 curr_binding_label[0] = '\0';
2154 if (gfc_match (" byte") == MATCH_YES)
2156 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
2160 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2162 gfc_error ("BYTE type used at %C "
2163 "is not available on the target machine");
2167 ts->type = BT_INTEGER;
2172 if (gfc_match (" integer") == MATCH_YES)
2174 ts->type = BT_INTEGER;
2175 ts->kind = gfc_default_integer_kind;
2179 if (gfc_match (" character") == MATCH_YES)
2181 ts->type = BT_CHARACTER;
2182 if (implicit_flag == 0)
2183 return match_char_spec (ts);
2188 if (gfc_match (" real") == MATCH_YES)
2191 ts->kind = gfc_default_real_kind;
2195 if (gfc_match (" double precision") == MATCH_YES)
2198 ts->kind = gfc_default_double_kind;
2202 if (gfc_match (" complex") == MATCH_YES)
2204 ts->type = BT_COMPLEX;
2205 ts->kind = gfc_default_complex_kind;
2209 if (gfc_match (" double complex") == MATCH_YES)
2211 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2212 "conform to the Fortran 95 standard") == FAILURE)
2215 ts->type = BT_COMPLEX;
2216 ts->kind = gfc_default_double_kind;
2220 if (gfc_match (" logical") == MATCH_YES)
2222 ts->type = BT_LOGICAL;
2223 ts->kind = gfc_default_logical_kind;
2227 m = gfc_match (" type ( %n )", name);
2231 if (gfc_current_state () == COMP_INTERFACE
2232 || gfc_current_state () == COMP_NONE)
2234 gfc_function_type_locus = loc;
2235 ts->type = BT_UNKNOWN;
2240 /* Search for the name but allow the components to be defined later. If
2241 type = -1, this typespec has been seen in a function declaration but
2242 the type could not legally be accessed at that point. */
2243 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2245 gfc_error ("Type name '%s' at %C is ambiguous", name);
2248 else if (ts->kind == -1)
2250 if (gfc_find_symbol (name, NULL, 0, &sym))
2252 gfc_error ("Type name '%s' at %C is ambiguous", name);
2260 if (sym->attr.flavor != FL_DERIVED
2261 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2264 ts->type = BT_DERIVED;
2271 /* For all types except double, derived and character, look for an
2272 optional kind specifier. MATCH_NO is actually OK at this point. */
2273 if (implicit_flag == 1)
2276 if (gfc_current_form == FORM_FREE)
2278 c = gfc_peek_char();
2279 if (!gfc_is_whitespace(c) && c != '*' && c != '('
2280 && c != ':' && c != ',')
2284 m = gfc_match_kind_spec (ts, false);
2285 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2286 m = gfc_match_old_kind_spec (ts);
2289 m = MATCH_YES; /* No kind specifier found. */
2295 /* Match an IMPLICIT NONE statement. Actually, this statement is
2296 already matched in parse.c, or we would not end up here in the
2297 first place. So the only thing we need to check, is if there is
2298 trailing garbage. If not, the match is successful. */
2301 gfc_match_implicit_none (void)
2303 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2307 /* Match the letter range(s) of an IMPLICIT statement. */
2310 match_implicit_range (void)
2312 int c, c1, c2, inner;
2315 cur_loc = gfc_current_locus;
2317 gfc_gobble_whitespace ();
2318 c = gfc_next_char ();
2321 gfc_error ("Missing character range in IMPLICIT at %C");
2328 gfc_gobble_whitespace ();
2329 c1 = gfc_next_char ();
2333 gfc_gobble_whitespace ();
2334 c = gfc_next_char ();
2339 inner = 0; /* Fall through. */
2346 gfc_gobble_whitespace ();
2347 c2 = gfc_next_char ();
2351 gfc_gobble_whitespace ();
2352 c = gfc_next_char ();
2354 if ((c != ',') && (c != ')'))
2367 gfc_error ("Letters must be in alphabetic order in "
2368 "IMPLICIT statement at %C");
2372 /* See if we can add the newly matched range to the pending
2373 implicits from this IMPLICIT statement. We do not check for
2374 conflicts with whatever earlier IMPLICIT statements may have
2375 set. This is done when we've successfully finished matching
2377 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2384 gfc_syntax_error (ST_IMPLICIT);
2386 gfc_current_locus = cur_loc;
2391 /* Match an IMPLICIT statement, storing the types for
2392 gfc_set_implicit() if the statement is accepted by the parser.
2393 There is a strange looking, but legal syntactic construction
2394 possible. It looks like:
2396 IMPLICIT INTEGER (a-b) (c-d)
2398 This is legal if "a-b" is a constant expression that happens to
2399 equal one of the legal kinds for integers. The real problem
2400 happens with an implicit specification that looks like:
2402 IMPLICIT INTEGER (a-b)
2404 In this case, a typespec matcher that is "greedy" (as most of the
2405 matchers are) gobbles the character range as a kindspec, leaving
2406 nothing left. We therefore have to go a bit more slowly in the
2407 matching process by inhibiting the kindspec checking during
2408 typespec matching and checking for a kind later. */
2411 gfc_match_implicit (void)
2418 /* We don't allow empty implicit statements. */
2419 if (gfc_match_eos () == MATCH_YES)
2421 gfc_error ("Empty IMPLICIT statement at %C");
2427 /* First cleanup. */
2428 gfc_clear_new_implicit ();
2430 /* A basic type is mandatory here. */
2431 m = gfc_match_type_spec (&ts, 1);
2432 if (m == MATCH_ERROR)
2437 cur_loc = gfc_current_locus;
2438 m = match_implicit_range ();
2442 /* We may have <TYPE> (<RANGE>). */
2443 gfc_gobble_whitespace ();
2444 c = gfc_next_char ();
2445 if ((c == '\n') || (c == ','))
2447 /* Check for CHARACTER with no length parameter. */
2448 if (ts.type == BT_CHARACTER && !ts.cl)
2450 ts.kind = gfc_default_character_kind;
2451 ts.cl = gfc_get_charlen ();
2452 ts.cl->next = gfc_current_ns->cl_list;
2453 gfc_current_ns->cl_list = ts.cl;
2454 ts.cl->length = gfc_int_expr (1);
2457 /* Record the Successful match. */
2458 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2463 gfc_current_locus = cur_loc;
2466 /* Discard the (incorrectly) matched range. */
2467 gfc_clear_new_implicit ();
2469 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2470 if (ts.type == BT_CHARACTER)
2471 m = match_char_spec (&ts);
2474 m = gfc_match_kind_spec (&ts, false);
2477 m = gfc_match_old_kind_spec (&ts);
2478 if (m == MATCH_ERROR)
2484 if (m == MATCH_ERROR)
2487 m = match_implicit_range ();
2488 if (m == MATCH_ERROR)
2493 gfc_gobble_whitespace ();
2494 c = gfc_next_char ();
2495 if ((c != '\n') && (c != ','))
2498 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2506 gfc_syntax_error (ST_IMPLICIT);
2514 gfc_match_import (void)
2516 char name[GFC_MAX_SYMBOL_LEN + 1];
2521 if (gfc_current_ns->proc_name == NULL
2522 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2524 gfc_error ("IMPORT statement at %C only permitted in "
2525 "an INTERFACE body");
2529 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2533 if (gfc_match_eos () == MATCH_YES)
2535 /* All host variables should be imported. */
2536 gfc_current_ns->has_import_set = 1;
2540 if (gfc_match (" ::") == MATCH_YES)
2542 if (gfc_match_eos () == MATCH_YES)
2544 gfc_error ("Expecting list of named entities at %C");
2551 m = gfc_match (" %n", name);
2555 if (gfc_current_ns->parent != NULL
2556 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2558 gfc_error ("Type name '%s' at %C is ambiguous", name);
2561 else if (gfc_current_ns->proc_name->ns->parent != NULL
2562 && gfc_find_symbol (name,
2563 gfc_current_ns->proc_name->ns->parent,
2566 gfc_error ("Type name '%s' at %C is ambiguous", name);
2572 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2573 "at %C - does not exist.", name);
2577 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2579 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2584 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2587 sym->attr.imported = 1;
2599 if (gfc_match_eos () == MATCH_YES)
2601 if (gfc_match_char (',') != MATCH_YES)
2608 gfc_error ("Syntax error in IMPORT statement at %C");
2613 /* A minimal implementation of gfc_match without whitespace, escape
2614 characters or variable arguments. Returns true if the next
2615 characters match the TARGET template exactly. */
2618 match_string_p (const char *target)
2622 for (p = target; *p; p++)
2623 if (gfc_next_char () != *p)
2628 /* Matches an attribute specification including array specs. If
2629 successful, leaves the variables current_attr and current_as
2630 holding the specification. Also sets the colon_seen variable for
2631 later use by matchers associated with initializations.
2633 This subroutine is a little tricky in the sense that we don't know
2634 if we really have an attr-spec until we hit the double colon.
2635 Until that time, we can only return MATCH_NO. This forces us to
2636 check for duplicate specification at this level. */
2639 match_attr_spec (void)
2641 /* Modifiers that can exist in a type statement. */
2643 { GFC_DECL_BEGIN = 0,
2644 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2645 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2646 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2647 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2648 DECL_IS_BIND_C, DECL_NONE,
2649 GFC_DECL_END /* Sentinel */
2653 /* GFC_DECL_END is the sentinel, index starts at 0. */
2654 #define NUM_DECL GFC_DECL_END
2656 locus start, seen_at[NUM_DECL];
2663 gfc_clear_attr (¤t_attr);
2664 start = gfc_current_locus;
2669 /* See if we get all of the keywords up to the final double colon. */
2670 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2678 gfc_gobble_whitespace ();
2680 ch = gfc_next_char ();
2683 /* This is the successful exit condition for the loop. */
2684 if (gfc_next_char () == ':')
2689 gfc_gobble_whitespace ();
2690 switch (gfc_peek_char ())
2693 if (match_string_p ("allocatable"))
2694 d = DECL_ALLOCATABLE;
2698 /* Try and match the bind(c). */
2699 m = gfc_match_bind_c (NULL);
2702 else if (m == MATCH_ERROR)
2707 if (match_string_p ("dimension"))
2712 if (match_string_p ("external"))
2717 if (match_string_p ("int"))
2719 ch = gfc_next_char ();
2722 if (match_string_p ("nt"))
2724 /* Matched "intent". */
2725 /* TODO: Call match_intent_spec from here. */
2726 if (gfc_match (" ( in out )") == MATCH_YES)
2728 else if (gfc_match (" ( in )") == MATCH_YES)
2730 else if (gfc_match (" ( out )") == MATCH_YES)
2736 if (match_string_p ("insic"))
2738 /* Matched "intrinsic". */
2746 if (match_string_p ("optional"))
2752 switch (gfc_next_char ())
2755 if (match_string_p ("rameter"))
2757 /* Matched "parameter". */
2763 if (match_string_p ("inter"))
2765 /* Matched "pointer". */
2771 ch = gfc_next_char ();
2774 if (match_string_p ("vate"))
2776 /* Matched "private". */
2782 if (match_string_p ("tected"))
2784 /* Matched "protected". */
2791 if (match_string_p ("blic"))
2793 /* Matched "public". */
2801 if (match_string_p ("save"))
2806 if (match_string_p ("target"))
2812 ch = gfc_next_char ();
2815 if (match_string_p ("lue"))
2817 /* Matched "value". */
2823 if (match_string_p ("latile"))
2825 /* Matched "volatile". */
2833 /* No double colon and no recognizable decl_type, so assume that
2834 we've been looking at something else the whole time. */
2842 seen_at[d] = gfc_current_locus;
2844 if (d == DECL_DIMENSION)
2846 m = gfc_match_array_spec (¤t_as);
2850 gfc_error ("Missing dimension specification at %C");
2854 if (m == MATCH_ERROR)
2859 /* Since we've seen a double colon, we have to be looking at an
2860 attr-spec. This means that we can now issue errors. */
2861 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2866 case DECL_ALLOCATABLE:
2867 attr = "ALLOCATABLE";
2869 case DECL_DIMENSION:
2876 attr = "INTENT (IN)";
2879 attr = "INTENT (OUT)";
2882 attr = "INTENT (IN OUT)";
2884 case DECL_INTRINSIC:
2890 case DECL_PARAMETER:
2896 case DECL_PROTECTED:
2911 case DECL_IS_BIND_C:
2921 attr = NULL; /* This shouldn't happen. */
2924 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2929 /* Now that we've dealt with duplicate attributes, add the attributes
2930 to the current attribute. */
2931 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2936 if (gfc_current_state () == COMP_DERIVED
2937 && d != DECL_DIMENSION && d != DECL_POINTER
2938 && d != DECL_PRIVATE && d != DECL_PUBLIC
2941 if (d == DECL_ALLOCATABLE)
2943 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
2944 "attribute at %C in a TYPE definition")
2953 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2960 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2961 && gfc_current_state () != COMP_MODULE)
2963 if (d == DECL_PRIVATE)
2967 if (gfc_current_state () == COMP_DERIVED
2968 && gfc_state_stack->previous
2969 && gfc_state_stack->previous->state == COMP_MODULE)
2971 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
2972 "at %L in a TYPE definition", attr,
2982 gfc_error ("%s attribute at %L is not allowed outside of the "
2983 "specification part of a module", attr, &seen_at[d]);
2991 case DECL_ALLOCATABLE:
2992 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
2995 case DECL_DIMENSION:
2996 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
3000 t = gfc_add_external (¤t_attr, &seen_at[d]);
3004 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
3008 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
3012 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
3015 case DECL_INTRINSIC:
3016 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
3020 t = gfc_add_optional (¤t_attr, &seen_at[d]);
3023 case DECL_PARAMETER:
3024 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
3028 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
3031 case DECL_PROTECTED:
3032 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3034 gfc_error ("PROTECTED at %C only allowed in specification "
3035 "part of a module");
3040 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3045 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
3049 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
3054 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
3059 t = gfc_add_save (¤t_attr, NULL, &seen_at[d]);
3063 t = gfc_add_target (¤t_attr, &seen_at[d]);
3066 case DECL_IS_BIND_C:
3067 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
3071 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3076 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
3080 if (gfc_notify_std (GFC_STD_F2003,
3081 "Fortran 2003: VOLATILE attribute at %C")
3085 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
3089 gfc_internal_error ("match_attr_spec(): Bad attribute");
3103 gfc_current_locus = start;
3104 gfc_free_array_spec (current_as);
3110 /* Set the binding label, dest_label, either with the binding label
3111 stored in the given gfc_typespec, ts, or if none was provided, it
3112 will be the symbol name in all lower case, as required by the draft
3113 (J3/04-007, section 15.4.1). If a binding label was given and
3114 there is more than one argument (num_idents), it is an error. */
3117 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3119 if (num_idents > 1 && has_name_equals)
3121 gfc_error ("Multiple identifiers provided with "
3122 "single NAME= specifier at %C");
3126 if (curr_binding_label[0] != '\0')
3128 /* Binding label given; store in temp holder til have sym. */
3129 strcpy (dest_label, curr_binding_label);
3133 /* No binding label given, and the NAME= specifier did not exist,
3134 which means there was no NAME="". */
3135 if (sym_name != NULL && has_name_equals == 0)
3136 strcpy (dest_label, sym_name);
3143 /* Set the status of the given common block as being BIND(C) or not,
3144 depending on the given parameter, is_bind_c. */
3147 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3149 com_block->is_bind_c = is_bind_c;
3154 /* Verify that the given gfc_typespec is for a C interoperable type. */
3157 verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
3161 /* Make sure the kind used is appropriate for the type.
3162 The f90_type is unknown if an integer constant was
3163 used (e.g., real(4), bind(c) :: myFloat). */
3164 if (ts->f90_type != BT_UNKNOWN)
3166 t = gfc_validate_c_kind (ts);
3169 /* Print an error, but continue parsing line. */
3170 gfc_error_now ("C kind parameter is for type %s but "
3171 "symbol '%s' at %L is of type %s",
3172 gfc_basic_typename (ts->f90_type),
3174 gfc_basic_typename (ts->type));
3178 /* Make sure the kind is C interoperable. This does not care about the
3179 possible error above. */
3180 if (ts->type == BT_DERIVED && ts->derived != NULL)
3181 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
3182 else if (ts->is_c_interop != 1)
3189 /* Verify that the variables of a given common block, which has been
3190 defined with the attribute specifier bind(c), to be of a C
3191 interoperable type. Errors will be reported here, if
3195 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3197 gfc_symbol *curr_sym = NULL;
3198 try retval = SUCCESS;
3200 curr_sym = com_block->head;
3202 /* Make sure we have at least one symbol. */
3203 if (curr_sym == NULL)
3206 /* Here we know we have a symbol, so we'll execute this loop
3210 /* The second to last param, 1, says this is in a common block. */
3211 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3212 curr_sym = curr_sym->common_next;
3213 } while (curr_sym != NULL);
3219 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3220 an appropriate error message is reported. */
3223 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3224 int is_in_common, gfc_common_head *com_block)
3226 try retval = SUCCESS;
3228 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3230 tmp_sym = tmp_sym->result;
3231 /* Make sure it wasn't an implicitly typed result. */
3232 if (tmp_sym->attr.implicit_type)
3234 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3235 "%L may not be C interoperable", tmp_sym->name,
3236 &tmp_sym->declared_at);
3237 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3238 /* Mark it as C interoperable to prevent duplicate warnings. */
3239 tmp_sym->ts.is_c_interop = 1;
3240 tmp_sym->attr.is_c_interop = 1;
3244 /* Here, we know we have the bind(c) attribute, so if we have
3245 enough type info, then verify that it's a C interop kind.
3246 The info could be in the symbol already, or possibly still in
3247 the given ts (current_ts), so look in both. */
3248 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3250 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
3251 &(tmp_sym->declared_at)) != SUCCESS)
3253 /* See if we're dealing with a sym in a common block or not. */
3254 if (is_in_common == 1)
3256 gfc_warning ("Variable '%s' in common block '%s' at %L "
3257 "may not be a C interoperable "
3258 "kind though common block '%s' is BIND(C)",
3259 tmp_sym->name, com_block->name,
3260 &(tmp_sym->declared_at), com_block->name);
3264 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3265 gfc_error ("Type declaration '%s' at %L is not C "
3266 "interoperable but it is BIND(C)",
3267 tmp_sym->name, &(tmp_sym->declared_at));
3269 gfc_warning ("Variable '%s' at %L "
3270 "may not be a C interoperable "
3271 "kind but it is bind(c)",
3272 tmp_sym->name, &(tmp_sym->declared_at));
3276 /* Variables declared w/in a common block can't be bind(c)
3277 since there's no way for C to see these variables, so there's
3278 semantically no reason for the attribute. */
3279 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3281 gfc_error ("Variable '%s' in common block '%s' at "
3282 "%L cannot be declared with BIND(C) "
3283 "since it is not a global",
3284 tmp_sym->name, com_block->name,
3285 &(tmp_sym->declared_at));
3289 /* Scalar variables that are bind(c) can not have the pointer
3290 or allocatable attributes. */
3291 if (tmp_sym->attr.is_bind_c == 1)
3293 if (tmp_sym->attr.pointer == 1)
3295 gfc_error ("Variable '%s' at %L cannot have both the "
3296 "POINTER and BIND(C) attributes",
3297 tmp_sym->name, &(tmp_sym->declared_at));
3301 if (tmp_sym->attr.allocatable == 1)
3303 gfc_error ("Variable '%s' at %L cannot have both the "
3304 "ALLOCATABLE and BIND(C) attributes",
3305 tmp_sym->name, &(tmp_sym->declared_at));
3309 /* If it is a BIND(C) function, make sure the return value is a
3310 scalar value. The previous tests in this function made sure
3311 the type is interoperable. */
3312 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
3313 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3314 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3316 /* BIND(C) functions can not return a character string. */
3317 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
3318 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3319 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3320 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3321 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3322 "be a character string", tmp_sym->name,
3323 &(tmp_sym->declared_at));
3327 /* See if the symbol has been marked as private. If it has, make sure
3328 there is no binding label and warn the user if there is one. */
3329 if (tmp_sym->attr.access == ACCESS_PRIVATE
3330 && tmp_sym->binding_label[0] != '\0')
3331 /* Use gfc_warning_now because we won't say that the symbol fails
3332 just because of this. */
3333 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3334 "given the binding label '%s'", tmp_sym->name,
3335 &(tmp_sym->declared_at), tmp_sym->binding_label);
3341 /* Set the appropriate fields for a symbol that's been declared as
3342 BIND(C) (the is_bind_c flag and the binding label), and verify that
3343 the type is C interoperable. Errors are reported by the functions
3344 used to set/test these fields. */
3347 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3349 try retval = SUCCESS;
3351 /* TODO: Do we need to make sure the vars aren't marked private? */
3353 /* Set the is_bind_c bit in symbol_attribute. */
3354 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3356 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3357 num_idents) != SUCCESS)
3364 /* Set the fields marking the given common block as BIND(C), including
3365 a binding label, and report any errors encountered. */
3368 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3370 try retval = SUCCESS;
3372 /* destLabel, common name, typespec (which may have binding label). */
3373 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3377 /* Set the given common block (com_block) to being bind(c) (1). */
3378 set_com_block_bind_c (com_block, 1);
3384 /* Retrieve the list of one or more identifiers that the given bind(c)
3385 attribute applies to. */
3388 get_bind_c_idents (void)
3390 char name[GFC_MAX_SYMBOL_LEN + 1];
3392 gfc_symbol *tmp_sym = NULL;
3394 gfc_common_head *com_block = NULL;
3396 if (gfc_match_name (name) == MATCH_YES)
3398 found_id = MATCH_YES;
3399 gfc_get_ha_symbol (name, &tmp_sym);
3401 else if (match_common_name (name) == MATCH_YES)
3403 found_id = MATCH_YES;
3404 com_block = gfc_get_common (name, 0);
3408 gfc_error ("Need either entity or common block name for "
3409 "attribute specification statement at %C");
3413 /* Save the current identifier and look for more. */
3416 /* Increment the number of identifiers found for this spec stmt. */
3419 /* Make sure we have a sym or com block, and verify that it can
3420 be bind(c). Set the appropriate field(s) and look for more
3422 if (tmp_sym != NULL || com_block != NULL)
3424 if (tmp_sym != NULL)
3426 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3432 if (set_verify_bind_c_com_block(com_block, num_idents)
3437 /* Look to see if we have another identifier. */
3439 if (gfc_match_eos () == MATCH_YES)
3440 found_id = MATCH_NO;
3441 else if (gfc_match_char (',') != MATCH_YES)
3442 found_id = MATCH_NO;
3443 else if (gfc_match_name (name) == MATCH_YES)
3445 found_id = MATCH_YES;
3446 gfc_get_ha_symbol (name, &tmp_sym);
3448 else if (match_common_name (name) == MATCH_YES)
3450 found_id = MATCH_YES;
3451 com_block = gfc_get_common (name, 0);
3455 gfc_error ("Missing entity or common block name for "
3456 "attribute specification statement at %C");
3462 gfc_internal_error ("Missing symbol");
3464 } while (found_id == MATCH_YES);
3466 /* if we get here we were successful */
3471 /* Try and match a BIND(C) attribute specification statement. */
3474 gfc_match_bind_c_stmt (void)
3476 match found_match = MATCH_NO;
3481 /* This may not be necessary. */
3483 /* Clear the temporary binding label holder. */
3484 curr_binding_label[0] = '\0';
3486 /* Look for the bind(c). */
3487 found_match = gfc_match_bind_c (NULL);
3489 if (found_match == MATCH_YES)
3491 /* Look for the :: now, but it is not required. */
3494 /* Get the identifier(s) that needs to be updated. This may need to
3495 change to hand the flag(s) for the attr specified so all identifiers
3496 found can have all appropriate parts updated (assuming that the same
3497 spec stmt can have multiple attrs, such as both bind(c) and
3499 if (get_bind_c_idents () != SUCCESS)
3500 /* Error message should have printed already. */
3508 /* Match a data declaration statement. */
3511 gfc_match_data_decl (void)
3517 num_idents_on_line = 0;
3519 m = gfc_match_type_spec (¤t_ts, 0);
3523 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3525 sym = gfc_use_derived (current_ts.derived);
3533 current_ts.derived = sym;
3536 m = match_attr_spec ();
3537 if (m == MATCH_ERROR)
3543 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL
3544 && !current_ts.derived->attr.zero_comp)
3547 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3550 gfc_find_symbol (current_ts.derived->name,
3551 current_ts.derived->ns->parent, 1, &sym);
3553 /* Any symbol that we find had better be a type definition
3554 which has its components defined. */
3555 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3556 && (current_ts.derived->components != NULL
3557 || current_ts.derived->attr.zero_comp))
3560 /* Now we have an error, which we signal, and then fix up
3561 because the knock-on is plain and simple confusing. */
3562 gfc_error_now ("Derived type at %C has not been previously defined "
3563 "and so cannot appear in a derived type definition");
3564 current_attr.pointer = 1;
3569 /* If we have an old-style character declaration, and no new-style
3570 attribute specifications, then there a comma is optional between
3571 the type specification and the variable list. */
3572 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3573 gfc_match_char (',');
3575 /* Give the types/attributes to symbols that follow. Give the element
3576 a number so that repeat character length expressions can be copied. */
3580 num_idents_on_line++;
3581 m = variable_decl (elem++);
3582 if (m == MATCH_ERROR)
3587 if (gfc_match_eos () == MATCH_YES)
3589 if (gfc_match_char (',') != MATCH_YES)
3593 if (gfc_error_flag_test () == 0)
3594 gfc_error ("Syntax error in data declaration at %C");
3597 gfc_free_data_all (gfc_current_ns);
3600 gfc_free_array_spec (current_as);
3606 /* Match a prefix associated with a function or subroutine
3607 declaration. If the typespec pointer is nonnull, then a typespec
3608 can be matched. Note that if nothing matches, MATCH_YES is
3609 returned (the null string was matched). */
3612 match_prefix (gfc_typespec *ts)
3616 gfc_clear_attr (¤t_attr);
3620 if (!seen_type && ts != NULL
3621 && gfc_match_type_spec (ts, 0) == MATCH_YES
3622 && gfc_match_space () == MATCH_YES)
3629 if (gfc_match ("elemental% ") == MATCH_YES)
3631 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
3637 if (gfc_match ("pure% ") == MATCH_YES)
3639 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
3645 if (gfc_match ("recursive% ") == MATCH_YES)
3647 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
3653 /* At this point, the next item is not a prefix. */
3658 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
3661 copy_prefix (symbol_attribute *dest, locus *where)
3663 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3666 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3669 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3676 /* Match a formal argument list. */
3679 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3681 gfc_formal_arglist *head, *tail, *p, *q;
3682 char name[GFC_MAX_SYMBOL_LEN + 1];
3688 if (gfc_match_char ('(') != MATCH_YES)
3695 if (gfc_match_char (')') == MATCH_YES)
3700 if (gfc_match_char ('*') == MATCH_YES)
3704 m = gfc_match_name (name);
3708 if (gfc_get_symbol (name, NULL, &sym))
3712 p = gfc_get_formal_arglist ();
3724 /* We don't add the VARIABLE flavor because the name could be a
3725 dummy procedure. We don't apply these attributes to formal
3726 arguments of statement functions. */
3727 if (sym != NULL && !st_flag
3728 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3729 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3735 /* The name of a program unit can be in a different namespace,
3736 so check for it explicitly. After the statement is accepted,
3737 the name is checked for especially in gfc_get_symbol(). */
3738 if (gfc_new_block != NULL && sym != NULL
3739 && strcmp (sym->name, gfc_new_block->name) == 0)
3741 gfc_error ("Name '%s' at %C is the name of the procedure",
3747 if (gfc_match_char (')') == MATCH_YES)
3750 m = gfc_match_char (',');
3753 gfc_error ("Unexpected junk in formal argument list at %C");
3759 /* Check for duplicate symbols in the formal argument list. */
3762 for (p = head; p->next; p = p->next)
3767 for (q = p->next; q; q = q->next)
3768 if (p->sym == q->sym)
3770 gfc_error ("Duplicate symbol '%s' in formal argument list "
3771 "at %C", p->sym->name);
3779 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3789 gfc_free_formal_arglist (head);
3794 /* Match a RESULT specification following a function declaration or
3795 ENTRY statement. Also matches the end-of-statement. */
3798 match_result (gfc_symbol *function, gfc_symbol **result)
3800 char name[GFC_MAX_SYMBOL_LEN + 1];
3804 if (gfc_match (" result (") != MATCH_YES)
3807 m = gfc_match_name (name);
3811 /* Get the right paren, and that's it because there could be the
3812 bind(c) attribute after the result clause. */
3813 if (gfc_match_char(')') != MATCH_YES)
3815 /* TODO: should report the missing right paren here. */
3819 if (strcmp (function->name, name) == 0)
3821 gfc_error ("RESULT variable at %C must be different than function name");
3825 if (gfc_get_symbol (name, NULL, &r))
3828 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3829 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
3838 /* Match a function suffix, which could be a combination of a result
3839 clause and BIND(C), either one, or neither. The draft does not
3840 require them to come in a specific order. */
3843 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3845 match is_bind_c; /* Found bind(c). */
3846 match is_result; /* Found result clause. */
3847 match found_match; /* Status of whether we've found a good match. */
3848 int peek_char; /* Character we're going to peek at. */
3850 /* Initialize to having found nothing. */
3851 found_match = MATCH_NO;
3852 is_bind_c = MATCH_NO;
3853 is_result = MATCH_NO;
3855 /* Get the next char to narrow between result and bind(c). */
3856 gfc_gobble_whitespace ();
3857 peek_char = gfc_peek_char ();
3862 /* Look for result clause. */
3863 is_result = match_result (sym, result);
3864 if (is_result == MATCH_YES)
3866 /* Now see if there is a bind(c) after it. */
3867 is_bind_c = gfc_match_bind_c (sym);
3868 /* We've found the result clause and possibly bind(c). */
3869 found_match = MATCH_YES;
3872 /* This should only be MATCH_ERROR. */
3873 found_match = is_result;
3876 /* Look for bind(c) first. */
3877 is_bind_c = gfc_match_bind_c (sym);
3878 if (is_bind_c == MATCH_YES)
3880 /* Now see if a result clause followed it. */
3881 is_result = match_result (sym, result);
3882 found_match = MATCH_YES;
3886 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3887 found_match = MATCH_ERROR;
3891 gfc_error ("Unexpected junk after function declaration at %C");
3892 found_match = MATCH_ERROR;
3896 if (is_bind_c == MATCH_YES)
3898 if (gfc_current_state () == COMP_CONTAINS
3899 && sym->ns->proc_name->attr.flavor != FL_MODULE)
3901 gfc_error ("BIND(C) attribute at %L may not be specified for an "
3902 "internal procedure", &gfc_current_locus);
3905 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
3914 /* Match a PROCEDURE declaration (R1211). */
3917 match_procedure_decl (void)
3920 locus old_loc, entry_loc;
3921 gfc_symbol *sym, *proc_if = NULL;
3924 old_loc = entry_loc = gfc_current_locus;
3926 gfc_clear_ts (¤t_ts);
3928 if (gfc_match (" (") != MATCH_YES)
3930 gfc_current_locus = entry_loc;
3934 /* Get the type spec. for the procedure interface. */
3935 old_loc = gfc_current_locus;
3936 m = gfc_match_type_spec (¤t_ts, 0);
3937 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_char () == ')'))
3940 if (m == MATCH_ERROR)
3943 gfc_current_locus = old_loc;
3945 /* Get the name of the procedure or abstract interface
3946 to inherit the interface from. */
3947 m = gfc_match_symbol (&proc_if, 1);
3951 else if (m == MATCH_ERROR)
3954 /* Various interface checks. */
3957 /* Resolve interface if possible. That way, attr.procedure is only set
3958 if it is declared by a later procedure-declaration-stmt, which is
3959 invalid per C1212. */
3960 while (proc_if->interface)
3961 proc_if = proc_if->interface;
3963 if (proc_if->generic)
3965 gfc_error ("Interface '%s' at %C may not be generic", proc_if->name);
3968 if (proc_if->attr.proc == PROC_ST_FUNCTION)
3970 gfc_error ("Interface '%s' at %C may not be a statement function",
3974 /* Handle intrinsic procedures. */
3975 if (gfc_intrinsic_name (proc_if->name, 0)
3976 || gfc_intrinsic_name (proc_if->name, 1))
3977 proc_if->attr.intrinsic = 1;
3978 if (proc_if->attr.intrinsic
3979 && !gfc_intrinsic_actual_ok (proc_if->name, 0))
3981 gfc_error ("Intrinsic procedure '%s' not allowed "
3982 "in PROCEDURE statement at %C", proc_if->name);
3988 if (gfc_match (" )") != MATCH_YES)
3990 gfc_current_locus = entry_loc;
3994 /* Parse attributes. */
3995 m = match_attr_spec();
3996 if (m == MATCH_ERROR)
3999 /* Get procedure symbols. */
4002 m = gfc_match_symbol (&sym, 0);
4005 else if (m == MATCH_ERROR)
4008 /* Add current_attr to the symbol attributes. */
4009 if (gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
4012 if (sym->attr.is_bind_c)
4014 /* Check for C1218. */
4015 if (!proc_if || !proc_if->attr.is_bind_c)
4017 gfc_error ("BIND(C) attribute at %C requires "
4018 "an interface with BIND(C)");
4021 /* Check for C1217. */
4022 if (has_name_equals && sym->attr.pointer)
4024 gfc_error ("BIND(C) procedure with NAME may not have "
4025 "POINTER attribute at %C");
4028 if (has_name_equals && sym->attr.dummy)
4030 gfc_error ("Dummy procedure at %C may not have "
4031 "BIND(C) attribute with NAME");
4034 /* Set binding label for BIND(C). */
4035 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4039 if (!sym->attr.pointer && gfc_add_external (&sym->attr, NULL) == FAILURE)
4041 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4044 /* Set interface. */
4045 if (proc_if != NULL)
4047 sym->interface = proc_if;
4048 sym->attr.untyped = 1;
4050 else if (current_ts.type != BT_UNKNOWN)
4052 sym->interface = gfc_new_symbol ("", gfc_current_ns);
4053 sym->interface->ts = current_ts;
4054 sym->interface->attr.function = 1;
4055 sym->ts = sym->interface->ts;
4056 sym->attr.function = sym->interface->attr.function;
4059 if (gfc_match_eos () == MATCH_YES)
4061 if (gfc_match_char (',') != MATCH_YES)
4066 gfc_error ("Syntax error in PROCEDURE statement at %C");
4071 /* Match a PROCEDURE declaration inside an interface (R1206). */
4074 match_procedure_in_interface (void)
4078 char name[GFC_MAX_SYMBOL_LEN + 1];
4080 if (current_interface.type == INTERFACE_NAMELESS
4081 || current_interface.type == INTERFACE_ABSTRACT)
4083 gfc_error ("PROCEDURE at %C must be in a generic interface");
4089 m = gfc_match_name (name);
4092 else if (m == MATCH_ERROR)
4094 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4097 if (gfc_add_interface (sym) == FAILURE)
4100 if (gfc_match_eos () == MATCH_YES)
4102 if (gfc_match_char (',') != MATCH_YES)
4109 gfc_error ("Syntax error in PROCEDURE statement at %C");
4114 /* General matcher for PROCEDURE declarations. */
4117 gfc_match_procedure (void)
4121 switch (gfc_current_state ())
4126 case COMP_SUBROUTINE:
4128 m = match_procedure_decl ();
4130 case COMP_INTERFACE:
4131 m = match_procedure_in_interface ();
4134 gfc_error ("Fortran 2003: Procedure components at %C are "
4135 "not yet implemented in gfortran");
4144 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4152 /* Match a function declaration. */
4155 gfc_match_function_decl (void)
4157 char name[GFC_MAX_SYMBOL_LEN + 1];
4158 gfc_symbol *sym, *result;
4162 match found_match; /* Status returned by match func. */
4164 if (gfc_current_state () != COMP_NONE
4165 && gfc_current_state () != COMP_INTERFACE
4166 && gfc_current_state () != COMP_CONTAINS)
4169 gfc_clear_ts (¤t_ts);
4171 old_loc = gfc_current_locus;
4173 m = match_prefix (¤t_ts);
4176 gfc_current_locus = old_loc;
4180 if (gfc_match ("function% %n", name) != MATCH_YES)
4182 gfc_current_locus = old_loc;
4185 if (get_proc_name (name, &sym, false))
4187 gfc_new_block = sym;
4189 m = gfc_match_formal_arglist (sym, 0, 0);
4192 gfc_error ("Expected formal argument list in function "
4193 "definition at %C");
4197 else if (m == MATCH_ERROR)
4202 /* According to the draft, the bind(c) and result clause can
4203 come in either order after the formal_arg_list (i.e., either
4204 can be first, both can exist together or by themselves or neither
4205 one). Therefore, the match_result can't match the end of the
4206 string, and check for the bind(c) or result clause in either order. */
4207 found_match = gfc_match_eos ();
4209 /* Make sure that it isn't already declared as BIND(C). If it is, it
4210 must have been marked BIND(C) with a BIND(C) attribute and that is
4211 not allowed for procedures. */
4212 if (sym->attr.is_bind_c == 1)
4214 sym->attr.is_bind_c = 0;
4215 if (sym->old_symbol != NULL)
4216 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4217 "variables or common blocks",
4218 &(sym->old_symbol->declared_at));
4220 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4221 "variables or common blocks", &gfc_current_locus);
4224 if (found_match != MATCH_YES)
4226 /* If we haven't found the end-of-statement, look for a suffix. */
4227 suffix_match = gfc_match_suffix (sym, &result);
4228 if (suffix_match == MATCH_YES)
4229 /* Need to get the eos now. */
4230 found_match = gfc_match_eos ();
4232 found_match = suffix_match;
4235 if(found_match != MATCH_YES)
4239 /* Make changes to the symbol. */
4242 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4245 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4246 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4249 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
4250 && !sym->attr.implicit_type)
4252 gfc_error ("Function '%s' at %C already has a type of %s", name,
4253 gfc_basic_typename (sym->ts.type));
4259 sym->ts = current_ts;
4264 result->ts = current_ts;
4265 sym->result = result;
4272 gfc_current_locus = old_loc;
4277 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4278 pass the name of the entry, rather than the gfc_current_block name, and
4279 to return false upon finding an existing global entry. */
4282 add_global_entry (const char *name, int sub)
4286 s = gfc_get_gsymbol(name);
4289 || (s->type != GSYM_UNKNOWN
4290 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
4291 gfc_global_used(s, NULL);
4294 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4295 s->where = gfc_current_locus;
4303 /* Match an ENTRY statement. */
4306 gfc_match_entry (void)
4311 char name[GFC_MAX_SYMBOL_LEN + 1];
4312 gfc_compile_state state;
4316 bool module_procedure;
4320 m = gfc_match_name (name);
4324 state = gfc_current_state ();
4325 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
4330 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4333 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4335 case COMP_BLOCK_DATA:
4336 gfc_error ("ENTRY statement at %C cannot appear within "
4339 case COMP_INTERFACE:
4340 gfc_error ("ENTRY statement at %C cannot appear within "
4344 gfc_error ("ENTRY statement at %C cannot appear within "
4345 "a DERIVED TYPE block");
4348 gfc_error ("ENTRY statement at %C cannot appear within "
4349 "an IF-THEN block");
4352 gfc_error ("ENTRY statement at %C cannot appear within "
4356 gfc_error ("ENTRY statement at %C cannot appear within "
4360 gfc_error ("ENTRY statement at %C cannot appear within "
4364 gfc_error ("ENTRY statement at %C cannot appear within "
4368 gfc_error ("ENTRY statement at %C cannot appear within "
4369 "a contained subprogram");
4372 gfc_internal_error ("gfc_match_entry(): Bad state");
4377 module_procedure = gfc_current_ns->parent != NULL
4378 && gfc_current_ns->parent->proc_name
4379 && gfc_current_ns->parent->proc_name->attr.flavor
4382 if (gfc_current_ns->parent != NULL
4383 && gfc_current_ns->parent->proc_name
4384 && !module_procedure)
4386 gfc_error("ENTRY statement at %C cannot appear in a "
4387 "contained procedure");
4391 /* Module function entries need special care in get_proc_name
4392 because previous references within the function will have
4393 created symbols attached to the current namespace. */
4394 if (get_proc_name (name, &entry,
4395 gfc_current_ns->parent != NULL
4397 && gfc_current_ns->proc_name->attr.function))
4400 proc = gfc_current_block ();
4402 /* Make sure that it isn't already declared as BIND(C). If it is, it
4403 must have been marked BIND(C) with a BIND(C) attribute and that is
4404 not allowed for procedures. */
4405 if (entry->attr.is_bind_c == 1)
4407 entry->attr.is_bind_c = 0;
4408 if (entry->old_symbol != NULL)
4409 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4410 "variables or common blocks",
4411 &(entry->old_symbol->declared_at));
4413 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4414 "variables or common blocks", &gfc_current_locus);
4417 /* Check what next non-whitespace character is so we can tell if there
4418 is the required parens if we have a BIND(C). */
4419 gfc_gobble_whitespace ();
4420 peek_char = gfc_peek_char ();
4422 if (state == COMP_SUBROUTINE)
4424 /* An entry in a subroutine. */
4425 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
4428 m = gfc_match_formal_arglist (entry, 0, 1);
4432 is_bind_c = gfc_match_bind_c (entry);
4433 if (is_bind_c == MATCH_ERROR)
4435 if (is_bind_c == MATCH_YES)
4437 if (peek_char != '(')
4439 gfc_error ("Missing required parentheses before BIND(C) at %C");
4442 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
4447 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4448 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4453 /* An entry in a function.
4454 We need to take special care because writing
4459 ENTRY f() RESULT (r)
4461 ENTRY f RESULT (r). */
4462 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
4465 old_loc = gfc_current_locus;
4466 if (gfc_match_eos () == MATCH_YES)
4468 gfc_current_locus = old_loc;
4469 /* Match the empty argument list, and add the interface to
4471 m = gfc_match_formal_arglist (entry, 0, 1);
4474 m = gfc_match_formal_arglist (entry, 0, 0);
4481 if (gfc_match_eos () == MATCH_YES)
4483 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4484 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4487 entry->result = entry;
4491 m = gfc_match_suffix (entry, &result);
4493 gfc_syntax_error (ST_ENTRY);
4499 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4500 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
4501 || gfc_add_function (&entry->attr, result->name, NULL)
4504 entry->result = result;
4508 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4509 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4511 entry->result = entry;
4516 if (gfc_match_eos () != MATCH_YES)
4518 gfc_syntax_error (ST_ENTRY);
4522 entry->attr.recursive = proc->attr.recursive;
4523 entry->attr.elemental = proc->attr.elemental;
4524 entry->attr.pure = proc->attr.pure;
4526 el = gfc_get_entry_list ();
4528 el->next = gfc_current_ns->entries;
4529 gfc_current_ns->entries = el;
4531 el->id = el->next->id + 1;
4535 new_st.op = EXEC_ENTRY;
4536 new_st.ext.entry = el;
4542 /* Match a subroutine statement, including optional prefixes. */
4545 gfc_match_subroutine (void)
4547 char name[GFC_MAX_SYMBOL_LEN + 1];
4553 if (gfc_current_state () != COMP_NONE
4554 && gfc_current_state () != COMP_INTERFACE
4555 && gfc_current_state () != COMP_CONTAINS)
4558 m = match_prefix (NULL);
4562 m = gfc_match ("subroutine% %n", name);
4566 if (get_proc_name (name, &sym, false))
4568 gfc_new_block = sym;
4570 /* Check what next non-whitespace character is so we can tell if there
4571 is the required parens if we have a BIND(C). */
4572 gfc_gobble_whitespace ();
4573 peek_char = gfc_peek_char ();
4575 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
4578 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4581 /* Make sure that it isn't already declared as BIND(C). If it is, it
4582 must have been marked BIND(C) with a BIND(C) attribute and that is
4583 not allowed for procedures. */
4584 if (sym->attr.is_bind_c == 1)
4586 sym->attr.is_bind_c = 0;
4587 if (sym->old_symbol != NULL)
4588 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4589 "variables or common blocks",
4590 &(sym->old_symbol->declared_at));
4592 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4593 "variables or common blocks", &gfc_current_locus);
4596 /* Here, we are just checking if it has the bind(c) attribute, and if
4597 so, then we need to make sure it's all correct. If it doesn't,
4598 we still need to continue matching the rest of the subroutine line. */
4599 is_bind_c = gfc_match_bind_c (sym);
4600 if (is_bind_c == MATCH_ERROR)
4602 /* There was an attempt at the bind(c), but it was wrong. An
4603 error message should have been printed w/in the gfc_match_bind_c
4604 so here we'll just return the MATCH_ERROR. */
4608 if (is_bind_c == MATCH_YES)
4610 if (gfc_current_state () == COMP_CONTAINS
4611 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4613 gfc_error ("BIND(C) attribute at %L may not be specified for an "
4614 "internal procedure", &gfc_current_locus);
4617 if (peek_char != '(')
4619 gfc_error ("Missing required parentheses before BIND(C) at %C");
4622 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4627 if (gfc_match_eos () != MATCH_YES)
4629 gfc_syntax_error (ST_SUBROUTINE);
4633 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4640 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4641 given, and set the binding label in either the given symbol (if not
4642 NULL), or in the current_ts. The symbol may be NULL because we may
4643 encounter the BIND(C) before the declaration itself. Return
4644 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4645 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4646 or MATCH_YES if the specifier was correct and the binding label and
4647 bind(c) fields were set correctly for the given symbol or the
4651 gfc_match_bind_c (gfc_symbol *sym)
4653 /* binding label, if exists */
4654 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4658 /* Initialize the flag that specifies whether we encountered a NAME=
4659 specifier or not. */
4660 has_name_equals = 0;
4662 /* Init the first char to nil so we can catch if we don't have
4663 the label (name attr) or the symbol name yet. */
4664 binding_label[0] = '\0';
4666 /* This much we have to be able to match, in this order, if
4667 there is a bind(c) label. */
4668 if (gfc_match (" bind ( c ") != MATCH_YES)
4671 /* Now see if there is a binding label, or if we've reached the
4672 end of the bind(c) attribute without one. */
4673 if (gfc_match_char (',') == MATCH_YES)
4675 if (gfc_match (" name = ") != MATCH_YES)
4677 gfc_error ("Syntax error in NAME= specifier for binding label "
4679 /* should give an error message here */
4683 has_name_equals = 1;
4685 /* Get the opening quote. */
4686 double_quote = MATCH_YES;
4687 single_quote = MATCH_YES;
4688 double_quote = gfc_match_char ('"');
4689 if (double_quote != MATCH_YES)
4690 single_quote = gfc_match_char ('\'');
4691 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4693 gfc_error ("Syntax error in NAME= specifier for binding label "
4698 /* Grab the binding label, using functions that will not lower
4699 case the names automatically. */
4700 if (gfc_match_name_C (binding_label) != MATCH_YES)
4703 /* Get the closing quotation. */
4704 if (double_quote == MATCH_YES)
4706 if (gfc_match_char ('"') != MATCH_YES)
4708 gfc_error ("Missing closing quote '\"' for binding label at %C");
4709 /* User started string with '"' so looked to match it. */
4715 if (gfc_match_char ('\'') != MATCH_YES)
4717 gfc_error ("Missing closing quote '\'' for binding label at %C");
4718 /* User started string with "'" char. */
4724 /* Get the required right paren. */
4725 if (gfc_match_char (')') != MATCH_YES)
4727 gfc_error ("Missing closing paren for binding label at %C");
4731 /* Save the binding label to the symbol. If sym is null, we're
4732 probably matching the typespec attributes of a declaration and
4733 haven't gotten the name yet, and therefore, no symbol yet. */
4734 if (binding_label[0] != '\0')
4738 strcpy (sym->binding_label, binding_label);
4741 strcpy (curr_binding_label, binding_label);
4745 /* No binding label, but if symbol isn't null, we
4746 can set the label for it here. */
4747 /* TODO: If the name= was given and no binding label (name=""), we simply
4748 will let fortran mangle the symbol name as it usually would.
4749 However, this could still let C call it if the user looked up the
4750 symbol in the object file. Should the name set during mangling in
4751 trans-decl.c be marked with characters that are invalid for C to
4753 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4754 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4757 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
4758 && current_interface.type == INTERFACE_ABSTRACT)
4760 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
4768 /* Return nonzero if we're currently compiling a contained procedure. */
4771 contained_procedure (void)
4775 for (s=gfc_state_stack; s; s=s->previous)
4776 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
4777 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
4783 /* Set the kind of each enumerator. The kind is selected such that it is
4784 interoperable with the corresponding C enumeration type, making
4785 sure that -fshort-enums is honored. */
4790 enumerator_history *current_history = NULL;
4794 if (max_enum == NULL || enum_history == NULL)
4797 if (!gfc_option.fshort_enums)
4803 kind = gfc_integer_kinds[i++].kind;
4805 while (kind < gfc_c_int_kind
4806 && gfc_check_integer_range (max_enum->initializer->value.integer,
4809 current_history = enum_history;
4810 while (current_history != NULL)
4812 current_history->sym->ts.kind = kind;
4813 current_history = current_history->next;
4818 /* Match any of the various end-block statements. Returns the type of
4819 END to the caller. The END INTERFACE, END IF, END DO and END
4820 SELECT statements cannot be replaced by a single END statement. */
4823 gfc_match_end (gfc_statement *st)
4825 char name[GFC_MAX_SYMBOL_LEN + 1];
4826 gfc_compile_state state;
4828 const char *block_name;
4833 old_loc = gfc_current_locus;
4834 if (gfc_match ("end") != MATCH_YES)
4837 state = gfc_current_state ();
4838 block_name = gfc_current_block () == NULL
4839 ? NULL : gfc_current_block ()->name;
4841 if (state == COMP_CONTAINS)
4843 state = gfc_state_stack->previous->state;
4844 block_name = gfc_state_stack->previous->sym == NULL
4845 ? NULL : gfc_state_stack->previous->sym->name;
4852 *st = ST_END_PROGRAM;
4853 target = " program";
4857 case COMP_SUBROUTINE:
4858 *st = ST_END_SUBROUTINE;
4859 target = " subroutine";
4860 eos_ok = !contained_procedure ();
4864 *st = ST_END_FUNCTION;
4865 target = " function";
4866 eos_ok = !contained_procedure ();
4869 case COMP_BLOCK_DATA:
4870 *st = ST_END_BLOCK_DATA;
4871 target = " block data";
4876 *st = ST_END_MODULE;
4881 case COMP_INTERFACE:
4882 *st = ST_END_INTERFACE;
4883 target = " interface";
4906 *st = ST_END_SELECT;
4912 *st = ST_END_FORALL;
4927 last_initializer = NULL;
4929 gfc_free_enum_history ();
4933 gfc_error ("Unexpected END statement at %C");
4937 if (gfc_match_eos () == MATCH_YES)
4941 /* We would have required END [something]. */
4942 gfc_error ("%s statement expected at %L",
4943 gfc_ascii_statement (*st), &old_loc);
4950 /* Verify that we've got the sort of end-block that we're expecting. */
4951 if (gfc_match (target) != MATCH_YES)
4953 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
4957 /* If we're at the end, make sure a block name wasn't required. */
4958 if (gfc_match_eos () == MATCH_YES)
4961 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
4962 && *st != ST_END_FORALL && *st != ST_END_WHERE)
4965 if (gfc_current_block () == NULL)
4968 gfc_error ("Expected block name of '%s' in %s statement at %C",
4969 block_name, gfc_ascii_statement (*st));
4974 /* END INTERFACE has a special handler for its several possible endings. */
4975 if (*st == ST_END_INTERFACE)
4976 return gfc_match_end_interface ();
4978 /* We haven't hit the end of statement, so what is left must be an
4980 m = gfc_match_space ();
4982 m = gfc_match_name (name);
4985 gfc_error ("Expected terminating name at %C");
4989 if (block_name == NULL)
4992 if (strcmp (name, block_name) != 0)
4994 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
4995 gfc_ascii_statement (*st));
4999 if (gfc_match_eos () == MATCH_YES)
5003 gfc_syntax_error (*st);
5006 gfc_current_locus = old_loc;
5012 /***************** Attribute declaration statements ****************/
5014 /* Set the attribute of a single variable. */
5019 char name[GFC_MAX_SYMBOL_LEN + 1];
5027 m = gfc_match_name (name);
5031 if (find_special (name, &sym))
5034 var_locus = gfc_current_locus;
5036 /* Deal with possible array specification for certain attributes. */
5037 if (current_attr.dimension
5038 || current_attr.allocatable
5039 || current_attr.pointer
5040 || current_attr.target)
5042 m = gfc_match_array_spec (&as);
5043 if (m == MATCH_ERROR)
5046 if (current_attr.dimension && m == MATCH_NO)
5048 gfc_error ("Missing array specification at %L in DIMENSION "
5049 "statement", &var_locus);
5054 if ((current_attr.allocatable || current_attr.pointer)
5055 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5057 gfc_error ("Array specification must be deferred at %L", &var_locus);
5063 /* Update symbol table. DIMENSION attribute is set
5064 in gfc_set_array_spec(). */
5065 if (current_attr.dimension == 0
5066 && gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
5072 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5078 if (sym->attr.cray_pointee && sym->as != NULL)
5080 /* Fix the array spec. */
5081 m = gfc_mod_pointee_as (sym->as);
5082 if (m == MATCH_ERROR)
5086 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
5092 if ((current_attr.external || current_attr.intrinsic)
5093 && sym->attr.flavor != FL_PROCEDURE
5094 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
5103 gfc_free_array_spec (as);
5108 /* Generic attribute declaration subroutine. Used for attributes that
5109 just have a list of names. */
5116 /* Gobble the optional double colon, by simply ignoring the result
5126 if (gfc_match_eos () == MATCH_YES)
5132 if (gfc_match_char (',') != MATCH_YES)
5134 gfc_error ("Unexpected character in variable list at %C");
5144 /* This routine matches Cray Pointer declarations of the form:
5145 pointer ( <pointer>, <pointee> )
5147 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5148 The pointer, if already declared, should be an integer. Otherwise, we
5149 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5150 be either a scalar, or an array declaration. No space is allocated for
5151 the pointee. For the statement
5152 pointer (ipt, ar(10))
5153 any subsequent uses of ar will be translated (in C-notation) as
5154 ar(i) => ((<type> *) ipt)(i)
5155 After gimplification, pointee variable will disappear in the code. */
5158 cray_pointer_decl (void)
5162 gfc_symbol *cptr; /* Pointer symbol. */
5163 gfc_symbol *cpte; /* Pointee symbol. */
5169 if (gfc_match_char ('(') != MATCH_YES)
5171 gfc_error ("Expected '(' at %C");
5175 /* Match pointer. */
5176 var_locus = gfc_current_locus;
5177 gfc_clear_attr (¤t_attr);
5178 gfc_add_cray_pointer (¤t_attr, &var_locus);
5179 current_ts.type = BT_INTEGER;
5180 current_ts.kind = gfc_index_integer_kind;
5182 m = gfc_match_symbol (&cptr, 0);
5185 gfc_error ("Expected variable name at %C");
5189 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5192 gfc_set_sym_referenced (cptr);
5194 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5196 cptr->ts.type = BT_INTEGER;
5197 cptr->ts.kind = gfc_index_integer_kind;
5199 else if (cptr->ts.type != BT_INTEGER)
5201 gfc_error ("Cray pointer at %C must be an integer");
5204 else if (cptr->ts.kind < gfc_index_integer_kind)
5205 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5206 " memory addresses require %d bytes",
5207 cptr->ts.kind, gfc_index_integer_kind);
5209 if (gfc_match_char (',') != MATCH_YES)
5211 gfc_error ("Expected \",\" at %C");
5215 /* Match Pointee. */
5216 var_locus = gfc_current_locus;
5217 gfc_clear_attr (¤t_attr);
5218 gfc_add_cray_pointee (¤t_attr, &var_locus);
5219 current_ts.type = BT_UNKNOWN;
5220 current_ts.kind = 0;
5222 m = gfc_match_symbol (&cpte, 0);
5225 gfc_error ("Expected variable name at %C");
5229 /* Check for an optional array spec. */
5230 m = gfc_match_array_spec (&as);
5231 if (m == MATCH_ERROR)
5233 gfc_free_array_spec (as);
5236 else if (m == MATCH_NO)
5238 gfc_free_array_spec (as);
5242 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5245 gfc_set_sym_referenced (cpte);
5247 if (cpte->as == NULL)
5249 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5250 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5252 else if (as != NULL)
5254 gfc_error ("Duplicate array spec for Cray pointee at %C");
5255 gfc_free_array_spec (as);
5261 if (cpte->as != NULL)
5263 /* Fix array spec. */
5264 m = gfc_mod_pointee_as (cpte->as);
5265 if (m == MATCH_ERROR)
5269 /* Point the Pointee at the Pointer. */
5270 cpte->cp_pointer = cptr;
5272 if (gfc_match_char (')') != MATCH_YES)
5274 gfc_error ("Expected \")\" at %C");
5277 m = gfc_match_char (',');
5279 done = true; /* Stop searching for more declarations. */
5283 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5284 || gfc_match_eos () != MATCH_YES)
5286 gfc_error ("Expected \",\" or end of statement at %C");
5294 gfc_match_external (void)
5297 gfc_clear_attr (¤t_attr);
5298 current_attr.external = 1;
5300 return attr_decl ();
5305 gfc_match_intent (void)
5309 intent = match_intent_spec ();
5310 if (intent == INTENT_UNKNOWN)
5313 gfc_clear_attr (¤t_attr);
5314 current_attr.intent = intent;
5316 return attr_decl ();
5321 gfc_match_intrinsic (void)
5324 gfc_clear_attr (¤t_attr);
5325 current_attr.intrinsic = 1;
5327 return attr_decl ();
5332 gfc_match_optional (void)
5335 gfc_clear_attr (¤t_attr);
5336 current_attr.optional = 1;
5338 return attr_decl ();
5343 gfc_match_pointer (void)
5345 gfc_gobble_whitespace ();
5346 if (gfc_peek_char () == '(')
5348 if (!gfc_option.flag_cray_pointer)
5350 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5354 return cray_pointer_decl ();
5358 gfc_clear_attr (¤t_attr);
5359 current_attr.pointer = 1;
5361 return attr_decl ();
5367 gfc_match_allocatable (void)
5369 gfc_clear_attr (¤t_attr);
5370 current_attr.allocatable = 1;
5372 return attr_decl ();
5377 gfc_match_dimension (void)
5379 gfc_clear_attr (¤t_attr);
5380 current_attr.dimension = 1;
5382 return attr_decl ();
5387 gfc_match_target (void)
5389 gfc_clear_attr (¤t_attr);
5390 current_attr.target = 1;
5392 return attr_decl ();
5396 /* Match the list of entities being specified in a PUBLIC or PRIVATE
5400 access_attr_decl (gfc_statement st)
5402 char name[GFC_MAX_SYMBOL_LEN + 1];
5403 interface_type type;
5406 gfc_intrinsic_op operator;
5409 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5414 m = gfc_match_generic_spec (&type, name, &operator);
5417 if (m == MATCH_ERROR)
5422 case INTERFACE_NAMELESS:
5423 case INTERFACE_ABSTRACT:
5426 case INTERFACE_GENERIC:
5427 if (gfc_get_symbol (name, NULL, &sym))
5430 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5431 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
5432 sym->name, NULL) == FAILURE)
5437 case INTERFACE_INTRINSIC_OP:
5438 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
5440 gfc_current_ns->operator_access[operator] =
5441 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5445 gfc_error ("Access specification of the %s operator at %C has "
5446 "already been specified", gfc_op2string (operator));
5452 case INTERFACE_USER_OP:
5453 uop = gfc_get_uop (name);
5455 if (uop->access == ACCESS_UNKNOWN)
5457 uop->access = (st == ST_PUBLIC)
5458 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5462 gfc_error ("Access specification of the .%s. operator at %C "
5463 "has already been specified", sym->name);
5470 if (gfc_match_char (',') == MATCH_NO)
5474 if (gfc_match_eos () != MATCH_YES)
5479 gfc_syntax_error (st);
5487 gfc_match_protected (void)
5492 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5494 gfc_error ("PROTECTED at %C only allowed in specification "
5495 "part of a module");
5500 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
5504 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5509 if (gfc_match_eos () == MATCH_YES)
5514 m = gfc_match_symbol (&sym, 0);
5518 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
5531 if (gfc_match_eos () == MATCH_YES)
5533 if (gfc_match_char (',') != MATCH_YES)
5540 gfc_error ("Syntax error in PROTECTED statement at %C");
5545 /* The PRIVATE statement is a bit weird in that it can be an attribute
5546 declaration, but also works as a standlone statement inside of a
5547 type declaration or a module. */
5550 gfc_match_private (gfc_statement *st)
5553 if (gfc_match ("private") != MATCH_YES)
5556 if (gfc_current_state () != COMP_MODULE
5557 && (gfc_current_state () != COMP_DERIVED
5558 || !gfc_state_stack->previous
5559 || gfc_state_stack->previous->state != COMP_MODULE))
5561 gfc_error ("PRIVATE statement at %C is only allowed in the "
5562 "specification part of a module");
5566 if (gfc_current_state () == COMP_DERIVED)
5568 if (gfc_match_eos () == MATCH_YES)
5574 gfc_syntax_error (ST_PRIVATE);
5578 if (gfc_match_eos () == MATCH_YES)
5585 return access_attr_decl (ST_PRIVATE);
5590 gfc_match_public (gfc_statement *st)
5593 if (gfc_match ("public") != MATCH_YES)
5596 if (gfc_current_state () != COMP_MODULE)
5598 gfc_error ("PUBLIC statement at %C is only allowed in the "
5599 "specification part of a module");
5603 if (gfc_match_eos () == MATCH_YES)
5610 return access_attr_decl (ST_PUBLIC);
5614 /* Workhorse for gfc_match_parameter. */
5623 m = gfc_match_symbol (&sym, 0);
5625 gfc_error ("Expected variable name at %C in PARAMETER statement");
5630 if (gfc_match_char ('=') == MATCH_NO)
5632 gfc_error ("Expected = sign in PARAMETER statement at %C");
5636 m = gfc_match_init_expr (&init);
5638 gfc_error ("Expected expression at %C in PARAMETER statement");
5642 if (sym->ts.type == BT_UNKNOWN
5643 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5649 if (gfc_check_assign_symbol (sym, init) == FAILURE
5650 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
5656 if (sym->ts.type == BT_CHARACTER
5657 && sym->ts.cl != NULL
5658 && sym->ts.cl->length != NULL
5659 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5660 && init->expr_type == EXPR_CONSTANT
5661 && init->ts.type == BT_CHARACTER
5662 && init->ts.kind == 1)
5663 gfc_set_constant_character_len (
5664 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
5670 gfc_free_expr (init);
5675 /* Match a parameter statement, with the weird syntax that these have. */
5678 gfc_match_parameter (void)
5682 if (gfc_match_char ('(') == MATCH_NO)
5691 if (gfc_match (" )%t") == MATCH_YES)
5694 if (gfc_match_char (',') != MATCH_YES)
5696 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5706 /* Save statements have a special syntax. */
5709 gfc_match_save (void)
5711 char n[GFC_MAX_SYMBOL_LEN+1];
5716 if (gfc_match_eos () == MATCH_YES)
5718 if (gfc_current_ns->seen_save)
5720 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5721 "follows previous SAVE statement")
5726 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5730 if (gfc_current_ns->save_all)
5732 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5733 "blanket SAVE statement")
5742 m = gfc_match_symbol (&sym, 0);
5746 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5758 m = gfc_match (" / %n /", &n);
5759 if (m == MATCH_ERROR)
5764 c = gfc_get_common (n, 0);
5767 gfc_current_ns->seen_save = 1;
5770 if (gfc_match_eos () == MATCH_YES)
5772 if (gfc_match_char (',') != MATCH_YES)
5779 gfc_error ("Syntax error in SAVE statement at %C");
5785 gfc_match_value (void)
5790 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
5794 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5799 if (gfc_match_eos () == MATCH_YES)
5804 m = gfc_match_symbol (&sym, 0);
5808 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5821 if (gfc_match_eos () == MATCH_YES)
5823 if (gfc_match_char (',') != MATCH_YES)
5830 gfc_error ("Syntax error in VALUE statement at %C");
5836 gfc_match_volatile (void)
5841 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
5845 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5850 if (gfc_match_eos () == MATCH_YES)
5855 /* VOLATILE is special because it can be added to host-associated
5857 m = gfc_match_symbol (&sym, 1);
5861 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
5874 if (gfc_match_eos () == MATCH_YES)
5876 if (gfc_match_char (',') != MATCH_YES)
5883 gfc_error ("Syntax error in VOLATILE statement at %C");
5888 /* Match a module procedure statement. Note that we have to modify
5889 symbols in the parent's namespace because the current one was there
5890 to receive symbols that are in an interface's formal argument list. */
5893 gfc_match_modproc (void)
5895 char name[GFC_MAX_SYMBOL_LEN + 1];
5898 gfc_namespace *module_ns;
5899 gfc_interface *old_interface_head, *interface;
5901 if (gfc_state_stack->state != COMP_INTERFACE
5902 || gfc_state_stack->previous == NULL
5903 || current_interface.type == INTERFACE_NAMELESS
5904 || current_interface.type == INTERFACE_ABSTRACT)
5906 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5911 module_ns = gfc_current_ns->parent;
5912 for (; module_ns; module_ns = module_ns->parent)
5913 if (module_ns->proc_name->attr.flavor == FL_MODULE)
5916 if (module_ns == NULL)
5919 /* Store the current state of the interface. We will need it if we
5920 end up with a syntax error and need to recover. */
5921 old_interface_head = gfc_current_interface_head ();
5927 m = gfc_match_name (name);
5933 /* Check for syntax error before starting to add symbols to the
5934 current namespace. */
5935 if (gfc_match_eos () == MATCH_YES)
5937 if (!last && gfc_match_char (',') != MATCH_YES)
5940 /* Now we're sure the syntax is valid, we process this item
5942 if (gfc_get_symbol (name, module_ns, &sym))
5945 if (sym->attr.proc != PROC_MODULE
5946 && gfc_add_procedure (&sym->attr, PROC_MODULE,
5947 sym->name, NULL) == FAILURE)
5950 if (gfc_add_interface (sym) == FAILURE)
5953 sym->attr.mod_proc = 1;
5962 /* Restore the previous state of the interface. */
5963 interface = gfc_current_interface_head ();
5964 gfc_set_current_interface_head (old_interface_head);
5966 /* Free the new interfaces. */
5967 while (interface != old_interface_head)
5969 gfc_interface *i = interface->next;
5970 gfc_free (interface);
5974 /* And issue a syntax error. */
5975 gfc_syntax_error (ST_MODULE_PROC);
5980 /* Match the optional attribute specifiers for a type declaration.
5981 Return MATCH_ERROR if an error is encountered in one of the handled
5982 attributes (public, private, bind(c)), MATCH_NO if what's found is
5983 not a handled attribute, and MATCH_YES otherwise. TODO: More error
5984 checking on attribute conflicts needs to be done. */
5987 gfc_get_type_attr_spec (symbol_attribute *attr)
5989 /* See if the derived type is marked as private. */
5990 if (gfc_match (" , private") == MATCH_YES)
5992 if (gfc_current_state () != COMP_MODULE)
5994 gfc_error ("Derived type at %C can only be PRIVATE in the "
5995 "specification part of a module");
5999 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6002 else if (gfc_match (" , public") == MATCH_YES)
6004 if (gfc_current_state () != COMP_MODULE)
6006 gfc_error ("Derived type at %C can only be PUBLIC in the "
6007 "specification part of a module");
6011 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6014 else if (gfc_match(" , bind ( c )") == MATCH_YES)
6016 /* If the type is defined to be bind(c) it then needs to make
6017 sure that all fields are interoperable. This will
6018 need to be a semantic check on the finished derived type.
6019 See 15.2.3 (lines 9-12) of F2003 draft. */
6020 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
6023 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
6028 /* If we get here, something matched. */
6033 /* Match the beginning of a derived type declaration. If a type name
6034 was the result of a function, then it is possible to have a symbol
6035 already to be known as a derived type yet have no components. */
6038 gfc_match_derived_decl (void)
6040 char name[GFC_MAX_SYMBOL_LEN + 1];
6041 symbol_attribute attr;
6044 match is_type_attr_spec = MATCH_NO;
6045 bool seen_attr = false;
6047 if (gfc_current_state () == COMP_DERIVED)
6050 gfc_clear_attr (&attr);
6054 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
6055 if (is_type_attr_spec == MATCH_ERROR)
6057 if (is_type_attr_spec == MATCH_YES)
6059 } while (is_type_attr_spec == MATCH_YES);
6061 if (gfc_match (" ::") != MATCH_YES && seen_attr)
6063 gfc_error ("Expected :: in TYPE definition at %C");
6067 m = gfc_match (" %n%t", name);
6071 /* Make sure the name is not the name of an intrinsic type. */
6072 if (gfc_is_intrinsic_typename (name))
6074 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
6079 if (gfc_get_symbol (name, NULL, &sym))
6082 if (sym->ts.type != BT_UNKNOWN)
6084 gfc_error ("Derived type name '%s' at %C already has a basic type "
6085 "of %s", sym->name, gfc_typename (&sym->ts));
6089 /* The symbol may already have the derived attribute without the
6090 components. The ways this can happen is via a function
6091 definition, an INTRINSIC statement or a subtype in another
6092 derived type that is a pointer. The first part of the AND clause
6093 is true if a the symbol is not the return value of a function. */
6094 if (sym->attr.flavor != FL_DERIVED
6095 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6098 if (sym->components != NULL || sym->attr.zero_comp)
6100 gfc_error ("Derived type definition of '%s' at %C has already been "
6101 "defined", sym->name);
6105 if (attr.access != ACCESS_UNKNOWN
6106 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6109 /* See if the derived type was labeled as bind(c). */
6110 if (attr.is_bind_c != 0)
6111 sym->attr.is_bind_c = attr.is_bind_c;
6113 gfc_new_block = sym;
6119 /* Cray Pointees can be declared as:
6120 pointer (ipt, a (n,m,...,*))
6121 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6122 cheat and set a constant bound of 1 for the last dimension, if this
6123 is the case. Since there is no bounds-checking for Cray Pointees,
6124 this will be okay. */
6127 gfc_mod_pointee_as (gfc_array_spec *as)
6129 as->cray_pointee = true; /* This will be useful to know later. */
6130 if (as->type == AS_ASSUMED_SIZE)
6132 as->type = AS_EXPLICIT;
6133 as->upper[as->rank - 1] = gfc_int_expr (1);
6134 as->cp_was_assumed = true;
6136 else if (as->type == AS_ASSUMED_SHAPE)
6138 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6145 /* Match the enum definition statement, here we are trying to match
6146 the first line of enum definition statement.
6147 Returns MATCH_YES if match is found. */
6150 gfc_match_enum (void)
6154 m = gfc_match_eos ();
6158 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
6166 /* Match a variable name with an optional initializer. When this
6167 subroutine is called, a variable is expected to be parsed next.
6168 Depending on what is happening at the moment, updates either the
6169 symbol table or the current interface. */
6172 enumerator_decl (void)
6174 char name[GFC_MAX_SYMBOL_LEN + 1];
6175 gfc_expr *initializer;
6176 gfc_array_spec *as = NULL;
6184 old_locus = gfc_current_locus;
6186 /* When we get here, we've just matched a list of attributes and
6187 maybe a type and a double colon. The next thing we expect to see
6188 is the name of the symbol. */
6189 m = gfc_match_name (name);
6193 var_locus = gfc_current_locus;
6195 /* OK, we've successfully matched the declaration. Now put the
6196 symbol in the current namespace. If we fail to create the symbol,
6198 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6204 /* The double colon must be present in order to have initializers.
6205 Otherwise the statement is ambiguous with an assignment statement. */
6208 if (gfc_match_char ('=') == MATCH_YES)
6210 m = gfc_match_init_expr (&initializer);
6213 gfc_error ("Expected an initialization expression at %C");
6222 /* If we do not have an initializer, the initialization value of the
6223 previous enumerator (stored in last_initializer) is incremented
6224 by 1 and is used to initialize the current enumerator. */
6225 if (initializer == NULL)
6226 initializer = gfc_enum_initializer (last_initializer, old_locus);
6228 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6230 gfc_error("ENUMERATOR %L not initialized with integer expression",
6233 gfc_free_enum_history ();
6237 /* Store this current initializer, for the next enumerator variable
6238 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6239 use last_initializer below. */
6240 last_initializer = initializer;
6241 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6243 /* Maintain enumerator history. */
6244 gfc_find_symbol (name, NULL, 0, &sym);
6245 create_enum_history (sym, last_initializer);
6247 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6250 /* Free stuff up and return. */
6251 gfc_free_expr (initializer);
6257 /* Match the enumerator definition statement. */
6260 gfc_match_enumerator_def (void)
6265 gfc_clear_ts (¤t_ts);
6267 m = gfc_match (" enumerator");
6271 m = gfc_match (" :: ");
6272 if (m == MATCH_ERROR)
6275 colon_seen = (m == MATCH_YES);
6277 if (gfc_current_state () != COMP_ENUM)
6279 gfc_error ("ENUM definition statement expected before %C");
6280 gfc_free_enum_history ();
6284 (¤t_ts)->type = BT_INTEGER;
6285 (¤t_ts)->kind = gfc_c_int_kind;
6287 gfc_clear_attr (¤t_attr);
6288 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
6297 m = enumerator_decl ();
6298 if (m == MATCH_ERROR)
6303 if (gfc_match_eos () == MATCH_YES)
6305 if (gfc_match_char (',') != MATCH_YES)
6309 if (gfc_current_state () == COMP_ENUM)
6311 gfc_free_enum_history ();
6312 gfc_error ("Syntax error in ENUMERATOR definition at %C");
6317 gfc_free_array_spec (current_as);