1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
29 /* Macros to access allocate memory for gfc_data_variable,
30 gfc_data_value and gfc_data. */
31 #define gfc_get_data_variable() gfc_getmem (sizeof (gfc_data_variable))
32 #define gfc_get_data_value() gfc_getmem (sizeof (gfc_data_value))
33 #define gfc_get_data() gfc_getmem( sizeof (gfc_data))
36 /* This flag is set if an old-style length selector is matched
37 during a type-declaration statement. */
39 static int old_char_selector;
41 /* When variables acquire types and attributes from a declaration
42 statement, they get them from the following static variables. The
43 first part of a declaration sets these variables and the second
44 part copies these into symbol structures. */
46 static gfc_typespec current_ts;
48 static symbol_attribute current_attr;
49 static gfc_array_spec *current_as;
50 static int colon_seen;
52 /* The current binding label (if any). */
53 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
54 /* Need to know how many identifiers are on the current data declaration
55 line in case we're given the BIND(C) attribute with a NAME= specifier. */
56 static int num_idents_on_line;
57 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
58 can supply a name if the curr_binding_label is nil and NAME= was not. */
59 static int has_name_equals = 0;
61 /* Initializer of the previous enumerator. */
63 static gfc_expr *last_initializer;
65 /* History of all the enumerators is maintained, so that
66 kind values of all the enumerators could be updated depending
67 upon the maximum initialized value. */
69 typedef struct enumerator_history
72 gfc_expr *initializer;
73 struct enumerator_history *next;
77 /* Header of enum history chain. */
79 static enumerator_history *enum_history = NULL;
81 /* Pointer of enum history node containing largest initializer. */
83 static enumerator_history *max_enum = NULL;
85 /* gfc_new_block points to the symbol of a newly matched block. */
87 gfc_symbol *gfc_new_block;
89 locus gfc_function_kind_locus;
90 locus gfc_function_type_locus;
93 /********************* DATA statement subroutines *********************/
95 static bool in_match_data = false;
98 gfc_in_match_data (void)
100 return in_match_data;
104 set_in_match_data (bool set_value)
106 in_match_data = set_value;
109 /* Free a gfc_data_variable structure and everything beneath it. */
112 free_variable (gfc_data_variable *p)
114 gfc_data_variable *q;
119 gfc_free_expr (p->expr);
120 gfc_free_iterator (&p->iter, 0);
121 free_variable (p->list);
127 /* Free a gfc_data_value structure and everything beneath it. */
130 free_value (gfc_data_value *p)
137 gfc_free_expr (p->expr);
143 /* Free a list of gfc_data structures. */
146 gfc_free_data (gfc_data *p)
153 free_variable (p->var);
154 free_value (p->value);
160 /* Free all data in a namespace. */
163 gfc_free_data_all (gfc_namespace *ns)
176 static match var_element (gfc_data_variable *);
178 /* Match a list of variables terminated by an iterator and a right
182 var_list (gfc_data_variable *parent)
184 gfc_data_variable *tail, var;
187 m = var_element (&var);
188 if (m == MATCH_ERROR)
193 tail = gfc_get_data_variable ();
200 if (gfc_match_char (',') != MATCH_YES)
203 m = gfc_match_iterator (&parent->iter, 1);
206 if (m == MATCH_ERROR)
209 m = var_element (&var);
210 if (m == MATCH_ERROR)
215 tail->next = gfc_get_data_variable ();
221 if (gfc_match_char (')') != MATCH_YES)
226 gfc_syntax_error (ST_DATA);
231 /* Match a single element in a data variable list, which can be a
232 variable-iterator list. */
235 var_element (gfc_data_variable *new)
240 memset (new, 0, sizeof (gfc_data_variable));
242 if (gfc_match_char ('(') == MATCH_YES)
243 return var_list (new);
245 m = gfc_match_variable (&new->expr, 0);
249 sym = new->expr->symtree->n.sym;
251 if (!sym->attr.function && gfc_current_ns->parent
252 && gfc_current_ns->parent == sym->ns)
254 gfc_error ("Host associated variable '%s' may not be in the DATA "
255 "statement at %C", sym->name);
259 if (gfc_current_state () != COMP_BLOCK_DATA
260 && sym->attr.in_common
261 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
262 "common block variable '%s' in DATA statement at %C",
263 sym->name) == FAILURE)
266 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
273 /* Match the top-level list of data variables. */
276 top_var_list (gfc_data *d)
278 gfc_data_variable var, *tail, *new;
285 m = var_element (&var);
288 if (m == MATCH_ERROR)
291 new = gfc_get_data_variable ();
301 if (gfc_match_char ('/') == MATCH_YES)
303 if (gfc_match_char (',') != MATCH_YES)
310 gfc_syntax_error (ST_DATA);
311 gfc_free_data_all (gfc_current_ns);
317 match_data_constant (gfc_expr **result)
319 char name[GFC_MAX_SYMBOL_LEN + 1];
325 m = gfc_match_literal_constant (&expr, 1);
332 if (m == MATCH_ERROR)
335 m = gfc_match_null (result);
339 old_loc = gfc_current_locus;
341 /* Should this be a structure component, try to match it
342 before matching a name. */
343 m = gfc_match_rvalue (result);
344 if (m == MATCH_ERROR)
347 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
349 if (gfc_simplify_expr (*result, 0) == FAILURE)
354 gfc_current_locus = old_loc;
356 m = gfc_match_name (name);
360 if (gfc_find_symbol (name, NULL, 1, &sym))
364 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
366 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
370 else if (sym->attr.flavor == FL_DERIVED)
371 return gfc_match_structure_constructor (sym, result);
373 /* Check to see if the value is an initialization array expression. */
374 if (sym->value->expr_type == EXPR_ARRAY)
376 gfc_current_locus = old_loc;
378 m = gfc_match_init_expr (result);
379 if (m == MATCH_ERROR)
384 if (gfc_simplify_expr (*result, 0) == FAILURE)
387 if ((*result)->expr_type == EXPR_CONSTANT)
391 gfc_error ("Invalid initializer %s in Data statement at %C", name);
397 *result = gfc_copy_expr (sym->value);
402 /* Match a list of values in a DATA statement. The leading '/' has
403 already been seen at this point. */
406 top_val_list (gfc_data *data)
408 gfc_data_value *new, *tail;
416 m = match_data_constant (&expr);
419 if (m == MATCH_ERROR)
422 new = gfc_get_data_value ();
423 mpz_init (new->repeat);
432 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
435 mpz_set_ui (tail->repeat, 1);
439 if (expr->ts.type == BT_INTEGER)
440 mpz_set (tail->repeat, expr->value.integer);
441 gfc_free_expr (expr);
443 m = match_data_constant (&tail->expr);
446 if (m == MATCH_ERROR)
450 if (gfc_match_char ('/') == MATCH_YES)
452 if (gfc_match_char (',') == MATCH_NO)
459 gfc_syntax_error (ST_DATA);
460 gfc_free_data_all (gfc_current_ns);
465 /* Matches an old style initialization. */
468 match_old_style_init (const char *name)
475 /* Set up data structure to hold initializers. */
476 gfc_find_sym_tree (name, NULL, 0, &st);
479 newdata = gfc_get_data ();
480 newdata->var = gfc_get_data_variable ();
481 newdata->var->expr = gfc_get_variable_expr (st);
482 newdata->where = gfc_current_locus;
484 /* Match initial value list. This also eats the terminal '/'. */
485 m = top_val_list (newdata);
494 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
499 /* Mark the variable as having appeared in a data statement. */
500 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
506 /* Chain in namespace list of DATA initializers. */
507 newdata->next = gfc_current_ns->data;
508 gfc_current_ns->data = newdata;
514 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
515 we are matching a DATA statement and are therefore issuing an error
516 if we encounter something unexpected, if not, we're trying to match
517 an old-style initialization expression of the form INTEGER I /2/. */
520 gfc_match_data (void)
525 set_in_match_data (true);
529 new = gfc_get_data ();
530 new->where = gfc_current_locus;
532 m = top_var_list (new);
536 m = top_val_list (new);
540 new->next = gfc_current_ns->data;
541 gfc_current_ns->data = new;
543 if (gfc_match_eos () == MATCH_YES)
546 gfc_match_char (','); /* Optional comma */
549 set_in_match_data (false);
553 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
560 set_in_match_data (false);
566 /************************ Declaration statements *********************/
568 /* Match an intent specification. Since this can only happen after an
569 INTENT word, a legal intent-spec must follow. */
572 match_intent_spec (void)
575 if (gfc_match (" ( in out )") == MATCH_YES)
577 if (gfc_match (" ( in )") == MATCH_YES)
579 if (gfc_match (" ( out )") == MATCH_YES)
582 gfc_error ("Bad INTENT specification at %C");
583 return INTENT_UNKNOWN;
587 /* Matches a character length specification, which is either a
588 specification expression or a '*'. */
591 char_len_param_value (gfc_expr **expr)
595 if (gfc_match_char ('*') == MATCH_YES)
601 m = gfc_match_expr (expr);
602 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
604 if ((*expr)->value.function.actual
605 && (*expr)->value.function.actual->expr->symtree)
608 e = (*expr)->value.function.actual->expr;
609 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
610 && e->expr_type == EXPR_VARIABLE)
612 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
614 if (e->symtree->n.sym->ts.type == BT_CHARACTER
615 && e->symtree->n.sym->ts.cl
616 && e->symtree->n.sym->ts.cl->length->ts.type == BT_UNKNOWN)
624 gfc_error ("Conflict in attributes of function argument at %C");
629 /* A character length is a '*' followed by a literal integer or a
630 char_len_param_value in parenthesis. */
633 match_char_length (gfc_expr **expr)
638 m = gfc_match_char ('*');
642 m = gfc_match_small_literal_int (&length, NULL);
643 if (m == MATCH_ERROR)
648 *expr = gfc_int_expr (length);
652 if (gfc_match_char ('(') == MATCH_NO)
655 m = char_len_param_value (expr);
656 if (m == MATCH_ERROR)
661 if (gfc_match_char (')') == MATCH_NO)
663 gfc_free_expr (*expr);
671 gfc_error ("Syntax error in character length specification at %C");
676 /* Special subroutine for finding a symbol. Check if the name is found
677 in the current name space. If not, and we're compiling a function or
678 subroutine and the parent compilation unit is an interface, then check
679 to see if the name we've been given is the name of the interface
680 (located in another namespace). */
683 find_special (const char *name, gfc_symbol **result)
688 i = gfc_get_symbol (name, NULL, result);
692 if (gfc_current_state () != COMP_SUBROUTINE
693 && gfc_current_state () != COMP_FUNCTION)
696 s = gfc_state_stack->previous;
700 if (s->state != COMP_INTERFACE)
703 goto end; /* Nameless interface. */
705 if (strcmp (name, s->sym->name) == 0)
716 /* Special subroutine for getting a symbol node associated with a
717 procedure name, used in SUBROUTINE and FUNCTION statements. The
718 symbol is created in the parent using with symtree node in the
719 child unit pointing to the symbol. If the current namespace has no
720 parent, then the symbol is just created in the current unit. */
723 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
729 /* Module functions have to be left in their own namespace because
730 they have potentially (almost certainly!) already been referenced.
731 In this sense, they are rather like external functions. This is
732 fixed up in resolve.c(resolve_entries), where the symbol name-
733 space is set to point to the master function, so that the fake
734 result mechanism can work. */
735 if (module_fcn_entry)
737 /* Present if entry is declared to be a module procedure. */
738 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
741 rc = gfc_get_symbol (name, NULL, result);
742 else if (!gfc_get_symbol (name, NULL, &sym) && sym
743 && (*result)->ts.type == BT_UNKNOWN
744 && sym->attr.flavor == FL_UNKNOWN)
745 /* Pick up the typespec for the entry, if declared in the function
746 body. Note that this symbol is FL_UNKNOWN because it will
747 only have appeared in a type declaration. The local symtree
748 is set to point to the module symbol and a unique symtree
749 to the local version. This latter ensures a correct clearing
752 /* If the ENTRY proceeds its specification, we need to ensure
753 that this does not raise a "has no IMPLICIT type" error. */
754 if (sym->ts.type == BT_UNKNOWN)
755 sym->attr.untyped = 1;
757 (*result)->ts = sym->ts;
759 /* Put the symbol in the procedure namespace so that, should
760 the ENTRY preceed its specification, the specification
762 (*result)->ns = gfc_current_ns;
764 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
766 st = gfc_get_unique_symtree (gfc_current_ns);
771 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
777 gfc_current_ns->refs++;
779 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
781 /* Trap another encompassed procedure with the same name. All
782 these conditions are necessary to avoid picking up an entry
783 whose name clashes with that of the encompassing procedure;
784 this is handled using gsymbols to register unique,globally
786 if (sym->attr.flavor != 0
787 && sym->attr.proc != 0
788 && (sym->attr.subroutine || sym->attr.function)
789 && sym->attr.if_source != IFSRC_UNKNOWN)
790 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
791 name, &sym->declared_at);
793 /* Trap a procedure with a name the same as interface in the
794 encompassing scope. */
795 if (sym->attr.generic != 0
796 && (sym->attr.subroutine || sym->attr.function)
797 && !sym->attr.mod_proc)
798 gfc_error_now ("Name '%s' at %C is already defined"
799 " as a generic interface at %L",
800 name, &sym->declared_at);
802 /* Trap declarations of attributes in encompassing scope. The
803 signature for this is that ts.kind is set. Legitimate
804 references only set ts.type. */
805 if (sym->ts.kind != 0
806 && !sym->attr.implicit_type
807 && sym->attr.proc == 0
808 && gfc_current_ns->parent != NULL
809 && sym->attr.access == 0
810 && !module_fcn_entry)
811 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
812 "and must not have attributes declared at %L",
813 name, &sym->declared_at);
816 if (gfc_current_ns->parent == NULL || *result == NULL)
819 /* Module function entries will already have a symtree in
820 the current namespace but will need one at module level. */
821 if (module_fcn_entry)
823 /* Present if entry is declared to be a module procedure. */
824 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
826 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
829 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
834 /* See if the procedure should be a module procedure. */
836 if (((sym->ns->proc_name != NULL
837 && sym->ns->proc_name->attr.flavor == FL_MODULE
838 && sym->attr.proc != PROC_MODULE)
839 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
840 && gfc_add_procedure (&sym->attr, PROC_MODULE,
841 sym->name, NULL) == FAILURE)
848 /* Verify that the given symbol representing a parameter is C
849 interoperable, by checking to see if it was marked as such after
850 its declaration. If the given symbol is not interoperable, a
851 warning is reported, thus removing the need to return the status to
852 the calling function. The standard does not require the user use
853 one of the iso_c_binding named constants to declare an
854 interoperable parameter, but we can't be sure if the param is C
855 interop or not if the user doesn't. For example, integer(4) may be
856 legal Fortran, but doesn't have meaning in C. It may interop with
857 a number of the C types, which causes a problem because the
858 compiler can't know which one. This code is almost certainly not
859 portable, and the user will get what they deserve if the C type
860 across platforms isn't always interoperable with integer(4). If
861 the user had used something like integer(c_int) or integer(c_long),
862 the compiler could have automatically handled the varying sizes
866 verify_c_interop_param (gfc_symbol *sym)
868 int is_c_interop = 0;
869 try retval = SUCCESS;
871 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
872 Don't repeat the checks here. */
873 if (sym->attr.implicit_type)
876 /* For subroutines or functions that are passed to a BIND(C) procedure,
877 they're interoperable if they're BIND(C) and their params are all
879 if (sym->attr.flavor == FL_PROCEDURE)
881 if (sym->attr.is_bind_c == 0)
883 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
884 "attribute to be C interoperable", sym->name,
885 &(sym->declared_at));
891 if (sym->attr.is_c_interop == 1)
892 /* We've already checked this procedure; don't check it again. */
895 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
900 /* See if we've stored a reference to a procedure that owns sym. */
901 if (sym->ns != NULL && sym->ns->proc_name != NULL)
903 if (sym->ns->proc_name->attr.is_bind_c == 1)
906 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
909 if (is_c_interop != 1)
911 /* Make personalized messages to give better feedback. */
912 if (sym->ts.type == BT_DERIVED)
913 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
914 " procedure '%s' but is not C interoperable "
915 "because derived type '%s' is not C interoperable",
916 sym->name, &(sym->declared_at),
917 sym->ns->proc_name->name,
918 sym->ts.derived->name);
920 gfc_warning ("Variable '%s' at %L is a parameter to the "
921 "BIND(C) procedure '%s' but may not be C "
923 sym->name, &(sym->declared_at),
924 sym->ns->proc_name->name);
927 /* Character strings are only C interoperable if they have a
929 if (sym->ts.type == BT_CHARACTER)
931 gfc_charlen *cl = sym->ts.cl;
932 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
933 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
935 gfc_error ("Character argument '%s' at %L "
936 "must be length 1 because "
937 "procedure '%s' is BIND(C)",
938 sym->name, &sym->declared_at,
939 sym->ns->proc_name->name);
944 /* We have to make sure that any param to a bind(c) routine does
945 not have the allocatable, pointer, or optional attributes,
946 according to J3/04-007, section 5.1. */
947 if (sym->attr.allocatable == 1)
949 gfc_error ("Variable '%s' at %L cannot have the "
950 "ALLOCATABLE attribute because procedure '%s'"
951 " is BIND(C)", sym->name, &(sym->declared_at),
952 sym->ns->proc_name->name);
956 if (sym->attr.pointer == 1)
958 gfc_error ("Variable '%s' at %L cannot have the "
959 "POINTER attribute because procedure '%s'"
960 " is BIND(C)", sym->name, &(sym->declared_at),
961 sym->ns->proc_name->name);
965 if (sym->attr.optional == 1)
967 gfc_error ("Variable '%s' at %L cannot have the "
968 "OPTIONAL attribute because procedure '%s'"
969 " is BIND(C)", sym->name, &(sym->declared_at),
970 sym->ns->proc_name->name);
974 /* Make sure that if it has the dimension attribute, that it is
975 either assumed size or explicit shape. */
978 if (sym->as->type == AS_ASSUMED_SHAPE)
980 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
981 "argument to the procedure '%s' at %L because "
982 "the procedure is BIND(C)", sym->name,
983 &(sym->declared_at), sym->ns->proc_name->name,
984 &(sym->ns->proc_name->declared_at));
988 if (sym->as->type == AS_DEFERRED)
990 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
991 "argument to the procedure '%s' at %L because "
992 "the procedure is BIND(C)", sym->name,
993 &(sym->declared_at), sym->ns->proc_name->name,
994 &(sym->ns->proc_name->declared_at));
1005 /* Function called by variable_decl() that adds a name to the symbol table. */
1008 build_sym (const char *name, gfc_charlen *cl,
1009 gfc_array_spec **as, locus *var_locus)
1011 symbol_attribute attr;
1014 if (gfc_get_symbol (name, NULL, &sym))
1017 /* Start updating the symbol table. Add basic type attribute if present. */
1018 if (current_ts.type != BT_UNKNOWN
1019 && (sym->attr.implicit_type == 0
1020 || !gfc_compare_types (&sym->ts, ¤t_ts))
1021 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
1024 if (sym->ts.type == BT_CHARACTER)
1027 /* Add dimension attribute if present. */
1028 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1032 /* Add attribute to symbol. The copy is so that we can reset the
1033 dimension attribute. */
1034 attr = current_attr;
1037 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1040 /* Finish any work that may need to be done for the binding label,
1041 if it's a bind(c). The bind(c) attr is found before the symbol
1042 is made, and before the symbol name (for data decls), so the
1043 current_ts is holding the binding label, or nothing if the
1044 name= attr wasn't given. Therefore, test here if we're dealing
1045 with a bind(c) and make sure the binding label is set correctly. */
1046 if (sym->attr.is_bind_c == 1)
1048 if (sym->binding_label[0] == '\0')
1050 /* Set the binding label and verify that if a NAME= was specified
1051 then only one identifier was in the entity-decl-list. */
1052 if (set_binding_label (sym->binding_label, sym->name,
1053 num_idents_on_line) == FAILURE)
1058 /* See if we know we're in a common block, and if it's a bind(c)
1059 common then we need to make sure we're an interoperable type. */
1060 if (sym->attr.in_common == 1)
1062 /* Test the common block object. */
1063 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1064 && sym->ts.is_c_interop != 1)
1066 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1067 "must be declared with a C interoperable "
1068 "kind since common block '%s' is BIND(C)",
1069 sym->name, sym->common_block->name,
1070 sym->common_block->name);
1075 sym->attr.implied_index = 0;
1081 /* Set character constant to the given length. The constant will be padded or
1085 gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
1090 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1091 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
1093 slen = expr->value.character.length;
1096 s = gfc_getmem (len + 1);
1097 memcpy (s, expr->value.character.string, MIN (len, slen));
1099 memset (&s[slen], ' ', len - slen);
1101 if (gfc_option.warn_character_truncation && slen > len)
1102 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1103 "(%d/%d)", &expr->where, slen, len);
1105 /* Apply the standard by 'hand' otherwise it gets cleared for
1107 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
1108 gfc_error_now ("The CHARACTER elements of the array constructor "
1109 "at %L must have the same length (%d/%d)",
1110 &expr->where, slen, len);
1113 gfc_free (expr->value.character.string);
1114 expr->value.character.string = s;
1115 expr->value.character.length = len;
1120 /* Function to create and update the enumerator history
1121 using the information passed as arguments.
1122 Pointer "max_enum" is also updated, to point to
1123 enum history node containing largest initializer.
1125 SYM points to the symbol node of enumerator.
1126 INIT points to its enumerator value. */
1129 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1131 enumerator_history *new_enum_history;
1132 gcc_assert (sym != NULL && init != NULL);
1134 new_enum_history = gfc_getmem (sizeof (enumerator_history));
1136 new_enum_history->sym = sym;
1137 new_enum_history->initializer = init;
1138 new_enum_history->next = NULL;
1140 if (enum_history == NULL)
1142 enum_history = new_enum_history;
1143 max_enum = enum_history;
1147 new_enum_history->next = enum_history;
1148 enum_history = new_enum_history;
1150 if (mpz_cmp (max_enum->initializer->value.integer,
1151 new_enum_history->initializer->value.integer) < 0)
1152 max_enum = new_enum_history;
1157 /* Function to free enum kind history. */
1160 gfc_free_enum_history (void)
1162 enumerator_history *current = enum_history;
1163 enumerator_history *next;
1165 while (current != NULL)
1167 next = current->next;
1172 enum_history = NULL;
1176 /* Function called by variable_decl() that adds an initialization
1177 expression to a symbol. */
1180 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1182 symbol_attribute attr;
1187 if (find_special (name, &sym))
1192 /* If this symbol is confirming an implicit parameter type,
1193 then an initialization expression is not allowed. */
1194 if (attr.flavor == FL_PARAMETER
1195 && sym->value != NULL
1198 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1205 /* An initializer is required for PARAMETER declarations. */
1206 if (attr.flavor == FL_PARAMETER)
1208 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1214 /* If a variable appears in a DATA block, it cannot have an
1218 gfc_error ("Variable '%s' at %C with an initializer already "
1219 "appears in a DATA statement", sym->name);
1223 /* Check if the assignment can happen. This has to be put off
1224 until later for a derived type variable. */
1225 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1226 && gfc_check_assign_symbol (sym, init) == FAILURE)
1229 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1231 /* Update symbol character length according initializer. */
1232 if (sym->ts.cl->length == NULL)
1235 /* If there are multiple CHARACTER variables declared on the
1236 same line, we don't want them to share the same length. */
1237 sym->ts.cl = gfc_get_charlen ();
1238 sym->ts.cl->next = gfc_current_ns->cl_list;
1239 gfc_current_ns->cl_list = sym->ts.cl;
1241 if (sym->attr.flavor == FL_PARAMETER)
1243 if (init->expr_type == EXPR_CONSTANT)
1245 clen = init->value.character.length;
1246 sym->ts.cl->length = gfc_int_expr (clen);
1248 else if (init->expr_type == EXPR_ARRAY)
1250 gfc_expr *p = init->value.constructor->expr;
1251 clen = p->value.character.length;
1252 sym->ts.cl->length = gfc_int_expr (clen);
1254 else if (init->ts.cl && init->ts.cl->length)
1255 sym->ts.cl->length =
1256 gfc_copy_expr (sym->value->ts.cl->length);
1259 /* Update initializer character length according symbol. */
1260 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1262 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1263 gfc_constructor * p;
1265 if (init->expr_type == EXPR_CONSTANT)
1266 gfc_set_constant_character_len (len, init, false);
1267 else if (init->expr_type == EXPR_ARRAY)
1269 /* Build a new charlen to prevent simplification from
1270 deleting the length before it is resolved. */
1271 init->ts.cl = gfc_get_charlen ();
1272 init->ts.cl->next = gfc_current_ns->cl_list;
1273 gfc_current_ns->cl_list = sym->ts.cl;
1274 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
1276 for (p = init->value.constructor; p; p = p->next)
1277 gfc_set_constant_character_len (len, p->expr, false);
1282 /* Need to check if the expression we initialized this
1283 to was one of the iso_c_binding named constants. If so,
1284 and we're a parameter (constant), let it be iso_c.
1286 integer(c_int), parameter :: my_int = c_int
1287 integer(my_int) :: my_int_2
1288 If we mark my_int as iso_c (since we can see it's value
1289 is equal to one of the named constants), then my_int_2
1290 will be considered C interoperable. */
1291 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1293 sym->ts.is_iso_c |= init->ts.is_iso_c;
1294 sym->ts.is_c_interop |= init->ts.is_c_interop;
1295 /* attr bits needed for module files. */
1296 sym->attr.is_iso_c |= init->ts.is_iso_c;
1297 sym->attr.is_c_interop |= init->ts.is_c_interop;
1298 if (init->ts.is_iso_c)
1299 sym->ts.f90_type = init->ts.f90_type;
1302 /* Add initializer. Make sure we keep the ranks sane. */
1303 if (sym->attr.dimension && init->rank == 0)
1309 if (sym->attr.flavor == FL_PARAMETER
1310 && init->expr_type == EXPR_CONSTANT
1311 && spec_size (sym->as, &size) == SUCCESS
1312 && mpz_cmp_si (size, 0) > 0)
1314 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1317 array->value.constructor = c = NULL;
1318 for (n = 0; n < (int)mpz_get_si (size); n++)
1320 if (array->value.constructor == NULL)
1322 array->value.constructor = c = gfc_get_constructor ();
1327 c->next = gfc_get_constructor ();
1329 c->expr = gfc_copy_expr (init);
1333 array->shape = gfc_get_shape (sym->as->rank);
1334 for (n = 0; n < sym->as->rank; n++)
1335 spec_dimen_size (sym->as, n, &array->shape[n]);
1340 init->rank = sym->as->rank;
1344 if (sym->attr.save == SAVE_NONE)
1345 sym->attr.save = SAVE_IMPLICIT;
1353 /* Function called by variable_decl() that adds a name to a structure
1357 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1358 gfc_array_spec **as)
1362 /* If the current symbol is of the same derived type that we're
1363 constructing, it must have the pointer attribute. */
1364 if (current_ts.type == BT_DERIVED
1365 && current_ts.derived == gfc_current_block ()
1366 && current_attr.pointer == 0)
1368 gfc_error ("Component at %C must have the POINTER attribute");
1372 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1374 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1376 gfc_error ("Array component of structure at %C must have explicit "
1377 "or deferred shape");
1382 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1387 gfc_set_component_attr (c, ¤t_attr);
1389 c->initializer = *init;
1397 /* Check array components. */
1402 gfc_error ("Allocatable component at %C must be an array");
1411 if (c->as->type != AS_DEFERRED)
1413 gfc_error ("Pointer array component of structure at %C must have a "
1418 else if (c->allocatable)
1420 if (c->as->type != AS_DEFERRED)
1422 gfc_error ("Allocatable component of structure at %C must have a "
1429 if (c->as->type != AS_EXPLICIT)
1431 gfc_error ("Array component of structure at %C must have an "
1441 /* Match a 'NULL()', and possibly take care of some side effects. */
1444 gfc_match_null (gfc_expr **result)
1450 m = gfc_match (" null ( )");
1454 /* The NULL symbol now has to be/become an intrinsic function. */
1455 if (gfc_get_symbol ("null", NULL, &sym))
1457 gfc_error ("NULL() initialization at %C is ambiguous");
1461 gfc_intrinsic_symbol (sym);
1463 if (sym->attr.proc != PROC_INTRINSIC
1464 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1465 sym->name, NULL) == FAILURE
1466 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1469 e = gfc_get_expr ();
1470 e->where = gfc_current_locus;
1471 e->expr_type = EXPR_NULL;
1472 e->ts.type = BT_UNKNOWN;
1480 /* Match a variable name with an optional initializer. When this
1481 subroutine is called, a variable is expected to be parsed next.
1482 Depending on what is happening at the moment, updates either the
1483 symbol table or the current interface. */
1486 variable_decl (int elem)
1488 char name[GFC_MAX_SYMBOL_LEN + 1];
1489 gfc_expr *initializer, *char_len;
1491 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1502 old_locus = gfc_current_locus;
1504 /* When we get here, we've just matched a list of attributes and
1505 maybe a type and a double colon. The next thing we expect to see
1506 is the name of the symbol. */
1507 m = gfc_match_name (name);
1511 var_locus = gfc_current_locus;
1513 /* Now we could see the optional array spec. or character length. */
1514 m = gfc_match_array_spec (&as);
1515 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1516 cp_as = gfc_copy_array_spec (as);
1517 else if (m == MATCH_ERROR)
1521 as = gfc_copy_array_spec (current_as);
1526 if (current_ts.type == BT_CHARACTER)
1528 switch (match_char_length (&char_len))
1531 cl = gfc_get_charlen ();
1532 cl->next = gfc_current_ns->cl_list;
1533 gfc_current_ns->cl_list = cl;
1535 cl->length = char_len;
1538 /* Non-constant lengths need to be copied after the first
1539 element. Also copy assumed lengths. */
1542 && (current_ts.cl->length == NULL
1543 || current_ts.cl->length->expr_type != EXPR_CONSTANT))
1545 cl = gfc_get_charlen ();
1546 cl->next = gfc_current_ns->cl_list;
1547 gfc_current_ns->cl_list = cl;
1548 cl->length = gfc_copy_expr (current_ts.cl->length);
1560 /* If this symbol has already shown up in a Cray Pointer declaration,
1561 then we want to set the type & bail out. */
1562 if (gfc_option.flag_cray_pointer)
1564 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1565 if (sym != NULL && sym->attr.cray_pointee)
1567 sym->ts.type = current_ts.type;
1568 sym->ts.kind = current_ts.kind;
1570 sym->ts.derived = current_ts.derived;
1571 sym->ts.is_c_interop = current_ts.is_c_interop;
1572 sym->ts.is_iso_c = current_ts.is_iso_c;
1575 /* Check to see if we have an array specification. */
1578 if (sym->as != NULL)
1580 gfc_error ("Duplicate array spec for Cray pointee at %C");
1581 gfc_free_array_spec (cp_as);
1587 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1588 gfc_internal_error ("Couldn't set pointee array spec.");
1590 /* Fix the array spec. */
1591 m = gfc_mod_pointee_as (sym->as);
1592 if (m == MATCH_ERROR)
1600 gfc_free_array_spec (cp_as);
1605 /* OK, we've successfully matched the declaration. Now put the
1606 symbol in the current namespace, because it might be used in the
1607 optional initialization expression for this symbol, e.g. this is
1610 integer, parameter :: i = huge(i)
1612 This is only true for parameters or variables of a basic type.
1613 For components of derived types, it is not true, so we don't
1614 create a symbol for those yet. If we fail to create the symbol,
1616 if (gfc_current_state () != COMP_DERIVED
1617 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1623 /* An interface body specifies all of the procedure's
1624 characteristics and these shall be consistent with those
1625 specified in the procedure definition, except that the interface
1626 may specify a procedure that is not pure if the procedure is
1627 defined to be pure(12.3.2). */
1628 if (current_ts.type == BT_DERIVED
1629 && gfc_current_ns->proc_name
1630 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1631 && current_ts.derived->ns != gfc_current_ns)
1634 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.derived->name);
1635 if (!(current_ts.derived->attr.imported
1637 && st->n.sym == current_ts.derived)
1638 && !gfc_current_ns->has_import_set)
1640 gfc_error ("the type of '%s' at %C has not been declared within the "
1647 /* In functions that have a RESULT variable defined, the function
1648 name always refers to function calls. Therefore, the name is
1649 not allowed to appear in specification statements. */
1650 if (gfc_current_state () == COMP_FUNCTION
1651 && gfc_current_block () != NULL
1652 && gfc_current_block ()->result != NULL
1653 && gfc_current_block ()->result != gfc_current_block ()
1654 && strcmp (gfc_current_block ()->name, name) == 0)
1656 gfc_error ("Function name '%s' not allowed at %C", name);
1661 /* We allow old-style initializations of the form
1662 integer i /2/, j(4) /3*3, 1/
1663 (if no colon has been seen). These are different from data
1664 statements in that initializers are only allowed to apply to the
1665 variable immediately preceding, i.e.
1667 is not allowed. Therefore we have to do some work manually, that
1668 could otherwise be left to the matchers for DATA statements. */
1670 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1672 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1673 "initialization at %C") == FAILURE)
1676 return match_old_style_init (name);
1679 /* The double colon must be present in order to have initializers.
1680 Otherwise the statement is ambiguous with an assignment statement. */
1683 if (gfc_match (" =>") == MATCH_YES)
1685 if (!current_attr.pointer)
1687 gfc_error ("Initialization at %C isn't for a pointer variable");
1692 m = gfc_match_null (&initializer);
1695 gfc_error ("Pointer initialization requires a NULL() at %C");
1699 if (gfc_pure (NULL))
1701 gfc_error ("Initialization of pointer at %C is not allowed in "
1702 "a PURE procedure");
1710 else if (gfc_match_char ('=') == MATCH_YES)
1712 if (current_attr.pointer)
1714 gfc_error ("Pointer initialization at %C requires '=>', "
1720 m = gfc_match_init_expr (&initializer);
1723 gfc_error ("Expected an initialization expression at %C");
1727 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1729 gfc_error ("Initialization of variable at %C is not allowed in "
1730 "a PURE procedure");
1739 if (initializer != NULL && current_attr.allocatable
1740 && gfc_current_state () == COMP_DERIVED)
1742 gfc_error ("Initialization of allocatable component at %C is not "
1748 /* Add the initializer. Note that it is fine if initializer is
1749 NULL here, because we sometimes also need to check if a
1750 declaration *must* have an initialization expression. */
1751 if (gfc_current_state () != COMP_DERIVED)
1752 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1755 if (current_ts.type == BT_DERIVED
1756 && !current_attr.pointer && !initializer)
1757 initializer = gfc_default_initializer (¤t_ts);
1758 t = build_struct (name, cl, &initializer, &as);
1761 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1764 /* Free stuff up and return. */
1765 gfc_free_expr (initializer);
1766 gfc_free_array_spec (as);
1772 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1773 This assumes that the byte size is equal to the kind number for
1774 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1777 gfc_match_old_kind_spec (gfc_typespec *ts)
1782 if (gfc_match_char ('*') != MATCH_YES)
1785 m = gfc_match_small_literal_int (&ts->kind, NULL);
1789 original_kind = ts->kind;
1791 /* Massage the kind numbers for complex types. */
1792 if (ts->type == BT_COMPLEX)
1796 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1797 gfc_basic_typename (ts->type), original_kind);
1803 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1805 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1806 gfc_basic_typename (ts->type), original_kind);
1810 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1811 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1818 /* Match a kind specification. Since kinds are generally optional, we
1819 usually return MATCH_NO if something goes wrong. If a "kind="
1820 string is found, then we know we have an error. */
1823 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
1834 where = loc = gfc_current_locus;
1839 if (gfc_match_char ('(') == MATCH_NO)
1842 /* Also gobbles optional text. */
1843 if (gfc_match (" kind = ") == MATCH_YES)
1846 loc = gfc_current_locus;
1849 n = gfc_match_init_expr (&e);
1853 if (gfc_current_state () == COMP_INTERFACE
1854 || gfc_current_state () == COMP_NONE
1855 || gfc_current_state () == COMP_CONTAINS)
1857 /* Signal using kind = -1 that the expression might include
1858 use associated or imported parameters and try again after
1859 the specification expressions..... */
1860 if (gfc_match_char (')') != MATCH_YES)
1862 gfc_error ("Missing right parenthesis at %C");
1869 gfc_function_kind_locus = loc;
1870 gfc_undo_symbols ();
1875 /* ....or else, the match is real. */
1877 gfc_error ("Expected initialization expression at %C");
1885 gfc_error ("Expected scalar initialization expression at %C");
1890 msg = gfc_extract_int (e, &ts->kind);
1898 /* Before throwing away the expression, let's see if we had a
1899 C interoperable kind (and store the fact). */
1900 if (e->ts.is_c_interop == 1)
1902 /* Mark this as c interoperable if being declared with one
1903 of the named constants from iso_c_binding. */
1904 ts->is_c_interop = e->ts.is_iso_c;
1905 ts->f90_type = e->ts.f90_type;
1911 /* Ignore errors to this point, if we've gotten here. This means
1912 we ignore the m=MATCH_ERROR from above. */
1913 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1915 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1916 gfc_basic_typename (ts->type));
1919 else if (gfc_match_char (')') != MATCH_YES)
1921 gfc_error ("Missing right parenthesis at %C");
1925 /* All tests passed. */
1928 if(m == MATCH_ERROR)
1929 gfc_current_locus = where;
1931 /* Return what we know from the test(s). */
1936 gfc_current_locus = where;
1942 match_char_kind (int * kind, int * is_iso_c)
1951 where = gfc_current_locus;
1953 n = gfc_match_init_expr (&e);
1955 gfc_error ("Expected initialization expression at %C");
1961 gfc_error ("Expected scalar initialization expression at %C");
1966 msg = gfc_extract_int (e, kind);
1967 *is_iso_c = e->ts.is_iso_c;
1977 /* Ignore errors to this point, if we've gotten here. This means
1978 we ignore the m=MATCH_ERROR from above. */
1979 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
1981 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
1985 /* All tests passed. */
1988 if (m == MATCH_ERROR)
1989 gfc_current_locus = where;
1991 /* Return what we know from the test(s). */
1996 gfc_current_locus = where;
2000 /* Match the various kind/length specifications in a CHARACTER
2001 declaration. We don't return MATCH_NO. */
2004 match_char_spec (gfc_typespec *ts)
2006 int kind, seen_length, is_iso_c;
2016 /* Try the old-style specification first. */
2017 old_char_selector = 0;
2019 m = match_char_length (&len);
2023 old_char_selector = 1;
2028 m = gfc_match_char ('(');
2031 m = MATCH_YES; /* Character without length is a single char. */
2035 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2036 if (gfc_match (" kind =") == MATCH_YES)
2038 m = match_char_kind (&kind, &is_iso_c);
2040 if (m == MATCH_ERROR)
2045 if (gfc_match (" , len =") == MATCH_NO)
2048 m = char_len_param_value (&len);
2051 if (m == MATCH_ERROR)
2058 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2059 if (gfc_match (" len =") == MATCH_YES)
2061 m = char_len_param_value (&len);
2064 if (m == MATCH_ERROR)
2068 if (gfc_match_char (')') == MATCH_YES)
2071 if (gfc_match (" , kind =") != MATCH_YES)
2074 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2080 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2081 m = char_len_param_value (&len);
2084 if (m == MATCH_ERROR)
2088 m = gfc_match_char (')');
2092 if (gfc_match_char (',') != MATCH_YES)
2095 gfc_match (" kind ="); /* Gobble optional text. */
2097 m = match_char_kind (&kind, &is_iso_c);
2098 if (m == MATCH_ERROR)
2104 /* Require a right-paren at this point. */
2105 m = gfc_match_char (')');
2110 gfc_error ("Syntax error in CHARACTER declaration at %C");
2112 gfc_free_expr (len);
2118 gfc_free_expr (len);
2122 /* Do some final massaging of the length values. */
2123 cl = gfc_get_charlen ();
2124 cl->next = gfc_current_ns->cl_list;
2125 gfc_current_ns->cl_list = cl;
2127 if (seen_length == 0)
2128 cl->length = gfc_int_expr (1);
2133 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2135 /* We have to know if it was a c interoperable kind so we can
2136 do accurate type checking of bind(c) procs, etc. */
2138 /* Mark this as c interoperable if being declared with one
2139 of the named constants from iso_c_binding. */
2140 ts->is_c_interop = is_iso_c;
2141 else if (len != NULL)
2142 /* Here, we might have parsed something such as: character(c_char)
2143 In this case, the parsing code above grabs the c_char when
2144 looking for the length (line 1690, roughly). it's the last
2145 testcase for parsing the kind params of a character variable.
2146 However, it's not actually the length. this seems like it
2148 To see if the user used a C interop kind, test the expr
2149 of the so called length, and see if it's C interoperable. */
2150 ts->is_c_interop = len->ts.is_iso_c;
2156 /* Matches a type specification. If successful, sets the ts structure
2157 to the matched specification. This is necessary for FUNCTION and
2158 IMPLICIT statements.
2160 If implicit_flag is nonzero, then we don't check for the optional
2161 kind specification. Not doing so is needed for matching an IMPLICIT
2162 statement correctly. */
2165 gfc_match_type_spec (gfc_typespec *ts, int implicit_flag)
2167 char name[GFC_MAX_SYMBOL_LEN + 1];
2171 locus loc = gfc_current_locus;
2175 /* Clear the current binding label, in case one is given. */
2176 curr_binding_label[0] = '\0';
2178 if (gfc_match (" byte") == MATCH_YES)
2180 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
2184 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2186 gfc_error ("BYTE type used at %C "
2187 "is not available on the target machine");
2191 ts->type = BT_INTEGER;
2196 if (gfc_match (" integer") == MATCH_YES)
2198 ts->type = BT_INTEGER;
2199 ts->kind = gfc_default_integer_kind;
2203 if (gfc_match (" character") == MATCH_YES)
2205 ts->type = BT_CHARACTER;
2206 if (implicit_flag == 0)
2207 return match_char_spec (ts);
2212 if (gfc_match (" real") == MATCH_YES)
2215 ts->kind = gfc_default_real_kind;
2219 if (gfc_match (" double precision") == MATCH_YES)
2222 ts->kind = gfc_default_double_kind;
2226 if (gfc_match (" complex") == MATCH_YES)
2228 ts->type = BT_COMPLEX;
2229 ts->kind = gfc_default_complex_kind;
2233 if (gfc_match (" double complex") == MATCH_YES)
2235 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2236 "conform to the Fortran 95 standard") == FAILURE)
2239 ts->type = BT_COMPLEX;
2240 ts->kind = gfc_default_double_kind;
2244 if (gfc_match (" logical") == MATCH_YES)
2246 ts->type = BT_LOGICAL;
2247 ts->kind = gfc_default_logical_kind;
2251 m = gfc_match (" type ( %n )", name);
2255 if (gfc_current_state () == COMP_INTERFACE
2256 || gfc_current_state () == COMP_NONE)
2258 gfc_function_type_locus = loc;
2259 ts->type = BT_UNKNOWN;
2264 /* Search for the name but allow the components to be defined later. If
2265 type = -1, this typespec has been seen in a function declaration but
2266 the type could not legally be accessed at that point. */
2267 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2269 gfc_error ("Type name '%s' at %C is ambiguous", name);
2272 else if (ts->kind == -1)
2274 if (gfc_find_symbol (name, NULL, 0, &sym))
2276 gfc_error ("Type name '%s' at %C is ambiguous", name);
2284 if (sym->attr.flavor != FL_DERIVED
2285 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2288 ts->type = BT_DERIVED;
2295 /* For all types except double, derived and character, look for an
2296 optional kind specifier. MATCH_NO is actually OK at this point. */
2297 if (implicit_flag == 1)
2300 if (gfc_current_form == FORM_FREE)
2302 c = gfc_peek_char();
2303 if (!gfc_is_whitespace(c) && c != '*' && c != '('
2304 && c != ':' && c != ',')
2308 m = gfc_match_kind_spec (ts, false);
2309 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2310 m = gfc_match_old_kind_spec (ts);
2313 m = MATCH_YES; /* No kind specifier found. */
2319 /* Match an IMPLICIT NONE statement. Actually, this statement is
2320 already matched in parse.c, or we would not end up here in the
2321 first place. So the only thing we need to check, is if there is
2322 trailing garbage. If not, the match is successful. */
2325 gfc_match_implicit_none (void)
2327 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2331 /* Match the letter range(s) of an IMPLICIT statement. */
2334 match_implicit_range (void)
2336 int c, c1, c2, inner;
2339 cur_loc = gfc_current_locus;
2341 gfc_gobble_whitespace ();
2342 c = gfc_next_char ();
2345 gfc_error ("Missing character range in IMPLICIT at %C");
2352 gfc_gobble_whitespace ();
2353 c1 = gfc_next_char ();
2357 gfc_gobble_whitespace ();
2358 c = gfc_next_char ();
2363 inner = 0; /* Fall through. */
2370 gfc_gobble_whitespace ();
2371 c2 = gfc_next_char ();
2375 gfc_gobble_whitespace ();
2376 c = gfc_next_char ();
2378 if ((c != ',') && (c != ')'))
2391 gfc_error ("Letters must be in alphabetic order in "
2392 "IMPLICIT statement at %C");
2396 /* See if we can add the newly matched range to the pending
2397 implicits from this IMPLICIT statement. We do not check for
2398 conflicts with whatever earlier IMPLICIT statements may have
2399 set. This is done when we've successfully finished matching
2401 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2408 gfc_syntax_error (ST_IMPLICIT);
2410 gfc_current_locus = cur_loc;
2415 /* Match an IMPLICIT statement, storing the types for
2416 gfc_set_implicit() if the statement is accepted by the parser.
2417 There is a strange looking, but legal syntactic construction
2418 possible. It looks like:
2420 IMPLICIT INTEGER (a-b) (c-d)
2422 This is legal if "a-b" is a constant expression that happens to
2423 equal one of the legal kinds for integers. The real problem
2424 happens with an implicit specification that looks like:
2426 IMPLICIT INTEGER (a-b)
2428 In this case, a typespec matcher that is "greedy" (as most of the
2429 matchers are) gobbles the character range as a kindspec, leaving
2430 nothing left. We therefore have to go a bit more slowly in the
2431 matching process by inhibiting the kindspec checking during
2432 typespec matching and checking for a kind later. */
2435 gfc_match_implicit (void)
2442 /* We don't allow empty implicit statements. */
2443 if (gfc_match_eos () == MATCH_YES)
2445 gfc_error ("Empty IMPLICIT statement at %C");
2451 /* First cleanup. */
2452 gfc_clear_new_implicit ();
2454 /* A basic type is mandatory here. */
2455 m = gfc_match_type_spec (&ts, 1);
2456 if (m == MATCH_ERROR)
2461 cur_loc = gfc_current_locus;
2462 m = match_implicit_range ();
2466 /* We may have <TYPE> (<RANGE>). */
2467 gfc_gobble_whitespace ();
2468 c = gfc_next_char ();
2469 if ((c == '\n') || (c == ','))
2471 /* Check for CHARACTER with no length parameter. */
2472 if (ts.type == BT_CHARACTER && !ts.cl)
2474 ts.kind = gfc_default_character_kind;
2475 ts.cl = gfc_get_charlen ();
2476 ts.cl->next = gfc_current_ns->cl_list;
2477 gfc_current_ns->cl_list = ts.cl;
2478 ts.cl->length = gfc_int_expr (1);
2481 /* Record the Successful match. */
2482 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2487 gfc_current_locus = cur_loc;
2490 /* Discard the (incorrectly) matched range. */
2491 gfc_clear_new_implicit ();
2493 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2494 if (ts.type == BT_CHARACTER)
2495 m = match_char_spec (&ts);
2498 m = gfc_match_kind_spec (&ts, false);
2501 m = gfc_match_old_kind_spec (&ts);
2502 if (m == MATCH_ERROR)
2508 if (m == MATCH_ERROR)
2511 m = match_implicit_range ();
2512 if (m == MATCH_ERROR)
2517 gfc_gobble_whitespace ();
2518 c = gfc_next_char ();
2519 if ((c != '\n') && (c != ','))
2522 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2530 gfc_syntax_error (ST_IMPLICIT);
2538 gfc_match_import (void)
2540 char name[GFC_MAX_SYMBOL_LEN + 1];
2545 if (gfc_current_ns->proc_name == NULL
2546 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2548 gfc_error ("IMPORT statement at %C only permitted in "
2549 "an INTERFACE body");
2553 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2557 if (gfc_match_eos () == MATCH_YES)
2559 /* All host variables should be imported. */
2560 gfc_current_ns->has_import_set = 1;
2564 if (gfc_match (" ::") == MATCH_YES)
2566 if (gfc_match_eos () == MATCH_YES)
2568 gfc_error ("Expecting list of named entities at %C");
2575 m = gfc_match (" %n", name);
2579 if (gfc_current_ns->parent != NULL
2580 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2582 gfc_error ("Type name '%s' at %C is ambiguous", name);
2585 else if (gfc_current_ns->proc_name->ns->parent != NULL
2586 && gfc_find_symbol (name,
2587 gfc_current_ns->proc_name->ns->parent,
2590 gfc_error ("Type name '%s' at %C is ambiguous", name);
2596 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2597 "at %C - does not exist.", name);
2601 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2603 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2608 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2611 sym->attr.imported = 1;
2623 if (gfc_match_eos () == MATCH_YES)
2625 if (gfc_match_char (',') != MATCH_YES)
2632 gfc_error ("Syntax error in IMPORT statement at %C");
2637 /* A minimal implementation of gfc_match without whitespace, escape
2638 characters or variable arguments. Returns true if the next
2639 characters match the TARGET template exactly. */
2642 match_string_p (const char *target)
2646 for (p = target; *p; p++)
2647 if (gfc_next_char () != *p)
2652 /* Matches an attribute specification including array specs. If
2653 successful, leaves the variables current_attr and current_as
2654 holding the specification. Also sets the colon_seen variable for
2655 later use by matchers associated with initializations.
2657 This subroutine is a little tricky in the sense that we don't know
2658 if we really have an attr-spec until we hit the double colon.
2659 Until that time, we can only return MATCH_NO. This forces us to
2660 check for duplicate specification at this level. */
2663 match_attr_spec (void)
2665 /* Modifiers that can exist in a type statement. */
2667 { GFC_DECL_BEGIN = 0,
2668 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2669 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2670 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2671 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2672 DECL_IS_BIND_C, DECL_NONE,
2673 GFC_DECL_END /* Sentinel */
2677 /* GFC_DECL_END is the sentinel, index starts at 0. */
2678 #define NUM_DECL GFC_DECL_END
2680 locus start, seen_at[NUM_DECL];
2687 gfc_clear_attr (¤t_attr);
2688 start = gfc_current_locus;
2693 /* See if we get all of the keywords up to the final double colon. */
2694 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2702 gfc_gobble_whitespace ();
2704 ch = gfc_next_char ();
2707 /* This is the successful exit condition for the loop. */
2708 if (gfc_next_char () == ':')
2713 gfc_gobble_whitespace ();
2714 switch (gfc_peek_char ())
2717 if (match_string_p ("allocatable"))
2718 d = DECL_ALLOCATABLE;
2722 /* Try and match the bind(c). */
2723 m = gfc_match_bind_c (NULL, true);
2726 else if (m == MATCH_ERROR)
2731 if (match_string_p ("dimension"))
2736 if (match_string_p ("external"))
2741 if (match_string_p ("int"))
2743 ch = gfc_next_char ();
2746 if (match_string_p ("nt"))
2748 /* Matched "intent". */
2749 /* TODO: Call match_intent_spec from here. */
2750 if (gfc_match (" ( in out )") == MATCH_YES)
2752 else if (gfc_match (" ( in )") == MATCH_YES)
2754 else if (gfc_match (" ( out )") == MATCH_YES)
2760 if (match_string_p ("insic"))
2762 /* Matched "intrinsic". */
2770 if (match_string_p ("optional"))
2776 switch (gfc_next_char ())
2779 if (match_string_p ("rameter"))
2781 /* Matched "parameter". */
2787 if (match_string_p ("inter"))
2789 /* Matched "pointer". */
2795 ch = gfc_next_char ();
2798 if (match_string_p ("vate"))
2800 /* Matched "private". */
2806 if (match_string_p ("tected"))
2808 /* Matched "protected". */
2815 if (match_string_p ("blic"))
2817 /* Matched "public". */
2825 if (match_string_p ("save"))
2830 if (match_string_p ("target"))
2836 ch = gfc_next_char ();
2839 if (match_string_p ("lue"))
2841 /* Matched "value". */
2847 if (match_string_p ("latile"))
2849 /* Matched "volatile". */
2857 /* No double colon and no recognizable decl_type, so assume that
2858 we've been looking at something else the whole time. */
2866 seen_at[d] = gfc_current_locus;
2868 if (d == DECL_DIMENSION)
2870 m = gfc_match_array_spec (¤t_as);
2874 gfc_error ("Missing dimension specification at %C");
2878 if (m == MATCH_ERROR)
2883 /* Since we've seen a double colon, we have to be looking at an
2884 attr-spec. This means that we can now issue errors. */
2885 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2890 case DECL_ALLOCATABLE:
2891 attr = "ALLOCATABLE";
2893 case DECL_DIMENSION:
2900 attr = "INTENT (IN)";
2903 attr = "INTENT (OUT)";
2906 attr = "INTENT (IN OUT)";
2908 case DECL_INTRINSIC:
2914 case DECL_PARAMETER:
2920 case DECL_PROTECTED:
2935 case DECL_IS_BIND_C:
2945 attr = NULL; /* This shouldn't happen. */
2948 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2953 /* Now that we've dealt with duplicate attributes, add the attributes
2954 to the current attribute. */
2955 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2960 if (gfc_current_state () == COMP_DERIVED
2961 && d != DECL_DIMENSION && d != DECL_POINTER
2962 && d != DECL_PRIVATE && d != DECL_PUBLIC
2965 if (d == DECL_ALLOCATABLE)
2967 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
2968 "attribute at %C in a TYPE definition")
2977 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2984 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2985 && gfc_current_state () != COMP_MODULE)
2987 if (d == DECL_PRIVATE)
2991 if (gfc_current_state () == COMP_DERIVED
2992 && gfc_state_stack->previous
2993 && gfc_state_stack->previous->state == COMP_MODULE)
2995 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
2996 "at %L in a TYPE definition", attr,
3006 gfc_error ("%s attribute at %L is not allowed outside of the "
3007 "specification part of a module", attr, &seen_at[d]);
3015 case DECL_ALLOCATABLE:
3016 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
3019 case DECL_DIMENSION:
3020 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
3024 t = gfc_add_external (¤t_attr, &seen_at[d]);
3028 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
3032 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
3036 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
3039 case DECL_INTRINSIC:
3040 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
3044 t = gfc_add_optional (¤t_attr, &seen_at[d]);
3047 case DECL_PARAMETER:
3048 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
3052 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
3055 case DECL_PROTECTED:
3056 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3058 gfc_error ("PROTECTED at %C only allowed in specification "
3059 "part of a module");
3064 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3069 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
3073 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
3078 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
3083 t = gfc_add_save (¤t_attr, NULL, &seen_at[d]);
3087 t = gfc_add_target (¤t_attr, &seen_at[d]);
3090 case DECL_IS_BIND_C:
3091 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
3095 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3100 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
3104 if (gfc_notify_std (GFC_STD_F2003,
3105 "Fortran 2003: VOLATILE attribute at %C")
3109 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
3113 gfc_internal_error ("match_attr_spec(): Bad attribute");
3127 gfc_current_locus = start;
3128 gfc_free_array_spec (current_as);
3134 /* Set the binding label, dest_label, either with the binding label
3135 stored in the given gfc_typespec, ts, or if none was provided, it
3136 will be the symbol name in all lower case, as required by the draft
3137 (J3/04-007, section 15.4.1). If a binding label was given and
3138 there is more than one argument (num_idents), it is an error. */
3141 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3143 if (num_idents > 1 && has_name_equals)
3145 gfc_error ("Multiple identifiers provided with "
3146 "single NAME= specifier at %C");
3150 if (curr_binding_label[0] != '\0')
3152 /* Binding label given; store in temp holder til have sym. */
3153 strcpy (dest_label, curr_binding_label);
3157 /* No binding label given, and the NAME= specifier did not exist,
3158 which means there was no NAME="". */
3159 if (sym_name != NULL && has_name_equals == 0)
3160 strcpy (dest_label, sym_name);
3167 /* Set the status of the given common block as being BIND(C) or not,
3168 depending on the given parameter, is_bind_c. */
3171 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3173 com_block->is_bind_c = is_bind_c;
3178 /* Verify that the given gfc_typespec is for a C interoperable type. */
3181 verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
3185 /* Make sure the kind used is appropriate for the type.
3186 The f90_type is unknown if an integer constant was
3187 used (e.g., real(4), bind(c) :: myFloat). */
3188 if (ts->f90_type != BT_UNKNOWN)
3190 t = gfc_validate_c_kind (ts);
3193 /* Print an error, but continue parsing line. */
3194 gfc_error_now ("C kind parameter is for type %s but "
3195 "symbol '%s' at %L is of type %s",
3196 gfc_basic_typename (ts->f90_type),
3198 gfc_basic_typename (ts->type));
3202 /* Make sure the kind is C interoperable. This does not care about the
3203 possible error above. */
3204 if (ts->type == BT_DERIVED && ts->derived != NULL)
3205 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
3206 else if (ts->is_c_interop != 1)
3213 /* Verify that the variables of a given common block, which has been
3214 defined with the attribute specifier bind(c), to be of a C
3215 interoperable type. Errors will be reported here, if
3219 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3221 gfc_symbol *curr_sym = NULL;
3222 try retval = SUCCESS;
3224 curr_sym = com_block->head;
3226 /* Make sure we have at least one symbol. */
3227 if (curr_sym == NULL)
3230 /* Here we know we have a symbol, so we'll execute this loop
3234 /* The second to last param, 1, says this is in a common block. */
3235 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3236 curr_sym = curr_sym->common_next;
3237 } while (curr_sym != NULL);
3243 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3244 an appropriate error message is reported. */
3247 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3248 int is_in_common, gfc_common_head *com_block)
3250 try retval = SUCCESS;
3252 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3254 tmp_sym = tmp_sym->result;
3255 /* Make sure it wasn't an implicitly typed result. */
3256 if (tmp_sym->attr.implicit_type)
3258 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3259 "%L may not be C interoperable", tmp_sym->name,
3260 &tmp_sym->declared_at);
3261 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3262 /* Mark it as C interoperable to prevent duplicate warnings. */
3263 tmp_sym->ts.is_c_interop = 1;
3264 tmp_sym->attr.is_c_interop = 1;
3268 /* Here, we know we have the bind(c) attribute, so if we have
3269 enough type info, then verify that it's a C interop kind.
3270 The info could be in the symbol already, or possibly still in
3271 the given ts (current_ts), so look in both. */
3272 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3274 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
3275 &(tmp_sym->declared_at)) != SUCCESS)
3277 /* See if we're dealing with a sym in a common block or not. */
3278 if (is_in_common == 1)
3280 gfc_warning ("Variable '%s' in common block '%s' at %L "
3281 "may not be a C interoperable "
3282 "kind though common block '%s' is BIND(C)",
3283 tmp_sym->name, com_block->name,
3284 &(tmp_sym->declared_at), com_block->name);
3288 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3289 gfc_error ("Type declaration '%s' at %L is not C "
3290 "interoperable but it is BIND(C)",
3291 tmp_sym->name, &(tmp_sym->declared_at));
3293 gfc_warning ("Variable '%s' at %L "
3294 "may not be a C interoperable "
3295 "kind but it is bind(c)",
3296 tmp_sym->name, &(tmp_sym->declared_at));
3300 /* Variables declared w/in a common block can't be bind(c)
3301 since there's no way for C to see these variables, so there's
3302 semantically no reason for the attribute. */
3303 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3305 gfc_error ("Variable '%s' in common block '%s' at "
3306 "%L cannot be declared with BIND(C) "
3307 "since it is not a global",
3308 tmp_sym->name, com_block->name,
3309 &(tmp_sym->declared_at));
3313 /* Scalar variables that are bind(c) can not have the pointer
3314 or allocatable attributes. */
3315 if (tmp_sym->attr.is_bind_c == 1)
3317 if (tmp_sym->attr.pointer == 1)
3319 gfc_error ("Variable '%s' at %L cannot have both the "
3320 "POINTER and BIND(C) attributes",
3321 tmp_sym->name, &(tmp_sym->declared_at));
3325 if (tmp_sym->attr.allocatable == 1)
3327 gfc_error ("Variable '%s' at %L cannot have both the "
3328 "ALLOCATABLE and BIND(C) attributes",
3329 tmp_sym->name, &(tmp_sym->declared_at));
3333 /* If it is a BIND(C) function, make sure the return value is a
3334 scalar value. The previous tests in this function made sure
3335 the type is interoperable. */
3336 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
3337 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3338 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3340 /* BIND(C) functions can not return a character string. */
3341 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
3342 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3343 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3344 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3345 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3346 "be a character string", tmp_sym->name,
3347 &(tmp_sym->declared_at));
3351 /* See if the symbol has been marked as private. If it has, make sure
3352 there is no binding label and warn the user if there is one. */
3353 if (tmp_sym->attr.access == ACCESS_PRIVATE
3354 && tmp_sym->binding_label[0] != '\0')
3355 /* Use gfc_warning_now because we won't say that the symbol fails
3356 just because of this. */
3357 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3358 "given the binding label '%s'", tmp_sym->name,
3359 &(tmp_sym->declared_at), tmp_sym->binding_label);
3365 /* Set the appropriate fields for a symbol that's been declared as
3366 BIND(C) (the is_bind_c flag and the binding label), and verify that
3367 the type is C interoperable. Errors are reported by the functions
3368 used to set/test these fields. */
3371 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3373 try retval = SUCCESS;
3375 /* TODO: Do we need to make sure the vars aren't marked private? */
3377 /* Set the is_bind_c bit in symbol_attribute. */
3378 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3380 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3381 num_idents) != SUCCESS)
3388 /* Set the fields marking the given common block as BIND(C), including
3389 a binding label, and report any errors encountered. */
3392 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3394 try retval = SUCCESS;
3396 /* destLabel, common name, typespec (which may have binding label). */
3397 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3401 /* Set the given common block (com_block) to being bind(c) (1). */
3402 set_com_block_bind_c (com_block, 1);
3408 /* Retrieve the list of one or more identifiers that the given bind(c)
3409 attribute applies to. */
3412 get_bind_c_idents (void)
3414 char name[GFC_MAX_SYMBOL_LEN + 1];
3416 gfc_symbol *tmp_sym = NULL;
3418 gfc_common_head *com_block = NULL;
3420 if (gfc_match_name (name) == MATCH_YES)
3422 found_id = MATCH_YES;
3423 gfc_get_ha_symbol (name, &tmp_sym);
3425 else if (match_common_name (name) == MATCH_YES)
3427 found_id = MATCH_YES;
3428 com_block = gfc_get_common (name, 0);
3432 gfc_error ("Need either entity or common block name for "
3433 "attribute specification statement at %C");
3437 /* Save the current identifier and look for more. */
3440 /* Increment the number of identifiers found for this spec stmt. */
3443 /* Make sure we have a sym or com block, and verify that it can
3444 be bind(c). Set the appropriate field(s) and look for more
3446 if (tmp_sym != NULL || com_block != NULL)
3448 if (tmp_sym != NULL)
3450 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3456 if (set_verify_bind_c_com_block(com_block, num_idents)
3461 /* Look to see if we have another identifier. */
3463 if (gfc_match_eos () == MATCH_YES)
3464 found_id = MATCH_NO;
3465 else if (gfc_match_char (',') != MATCH_YES)
3466 found_id = MATCH_NO;
3467 else if (gfc_match_name (name) == MATCH_YES)
3469 found_id = MATCH_YES;
3470 gfc_get_ha_symbol (name, &tmp_sym);
3472 else if (match_common_name (name) == MATCH_YES)
3474 found_id = MATCH_YES;
3475 com_block = gfc_get_common (name, 0);
3479 gfc_error ("Missing entity or common block name for "
3480 "attribute specification statement at %C");
3486 gfc_internal_error ("Missing symbol");
3488 } while (found_id == MATCH_YES);
3490 /* if we get here we were successful */
3495 /* Try and match a BIND(C) attribute specification statement. */
3498 gfc_match_bind_c_stmt (void)
3500 match found_match = MATCH_NO;
3505 /* This may not be necessary. */
3507 /* Clear the temporary binding label holder. */
3508 curr_binding_label[0] = '\0';
3510 /* Look for the bind(c). */
3511 found_match = gfc_match_bind_c (NULL, true);
3513 if (found_match == MATCH_YES)
3515 /* Look for the :: now, but it is not required. */
3518 /* Get the identifier(s) that needs to be updated. This may need to
3519 change to hand the flag(s) for the attr specified so all identifiers
3520 found can have all appropriate parts updated (assuming that the same
3521 spec stmt can have multiple attrs, such as both bind(c) and
3523 if (get_bind_c_idents () != SUCCESS)
3524 /* Error message should have printed already. */
3532 /* Match a data declaration statement. */
3535 gfc_match_data_decl (void)
3541 num_idents_on_line = 0;
3543 m = gfc_match_type_spec (¤t_ts, 0);
3547 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3549 sym = gfc_use_derived (current_ts.derived);
3557 current_ts.derived = sym;
3560 m = match_attr_spec ();
3561 if (m == MATCH_ERROR)
3567 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL
3568 && !current_ts.derived->attr.zero_comp)
3571 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3574 gfc_find_symbol (current_ts.derived->name,
3575 current_ts.derived->ns->parent, 1, &sym);
3577 /* Any symbol that we find had better be a type definition
3578 which has its components defined. */
3579 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3580 && (current_ts.derived->components != NULL
3581 || current_ts.derived->attr.zero_comp))
3584 /* Now we have an error, which we signal, and then fix up
3585 because the knock-on is plain and simple confusing. */
3586 gfc_error_now ("Derived type at %C has not been previously defined "
3587 "and so cannot appear in a derived type definition");
3588 current_attr.pointer = 1;
3593 /* If we have an old-style character declaration, and no new-style
3594 attribute specifications, then there a comma is optional between
3595 the type specification and the variable list. */
3596 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3597 gfc_match_char (',');
3599 /* Give the types/attributes to symbols that follow. Give the element
3600 a number so that repeat character length expressions can be copied. */
3604 num_idents_on_line++;
3605 m = variable_decl (elem++);
3606 if (m == MATCH_ERROR)
3611 if (gfc_match_eos () == MATCH_YES)
3613 if (gfc_match_char (',') != MATCH_YES)
3617 if (gfc_error_flag_test () == 0)
3618 gfc_error ("Syntax error in data declaration at %C");
3621 gfc_free_data_all (gfc_current_ns);
3624 gfc_free_array_spec (current_as);
3630 /* Match a prefix associated with a function or subroutine
3631 declaration. If the typespec pointer is nonnull, then a typespec
3632 can be matched. Note that if nothing matches, MATCH_YES is
3633 returned (the null string was matched). */
3636 match_prefix (gfc_typespec *ts)
3640 gfc_clear_attr (¤t_attr);
3644 if (!seen_type && ts != NULL
3645 && gfc_match_type_spec (ts, 0) == MATCH_YES
3646 && gfc_match_space () == MATCH_YES)
3653 if (gfc_match ("elemental% ") == MATCH_YES)
3655 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
3661 if (gfc_match ("pure% ") == MATCH_YES)
3663 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
3669 if (gfc_match ("recursive% ") == MATCH_YES)
3671 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
3677 /* At this point, the next item is not a prefix. */
3682 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
3685 copy_prefix (symbol_attribute *dest, locus *where)
3687 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3690 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3693 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3700 /* Match a formal argument list. */
3703 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3705 gfc_formal_arglist *head, *tail, *p, *q;
3706 char name[GFC_MAX_SYMBOL_LEN + 1];
3712 if (gfc_match_char ('(') != MATCH_YES)
3719 if (gfc_match_char (')') == MATCH_YES)
3724 if (gfc_match_char ('*') == MATCH_YES)
3728 m = gfc_match_name (name);
3732 if (gfc_get_symbol (name, NULL, &sym))
3736 p = gfc_get_formal_arglist ();
3748 /* We don't add the VARIABLE flavor because the name could be a
3749 dummy procedure. We don't apply these attributes to formal
3750 arguments of statement functions. */
3751 if (sym != NULL && !st_flag
3752 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3753 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3759 /* The name of a program unit can be in a different namespace,
3760 so check for it explicitly. After the statement is accepted,
3761 the name is checked for especially in gfc_get_symbol(). */
3762 if (gfc_new_block != NULL && sym != NULL
3763 && strcmp (sym->name, gfc_new_block->name) == 0)
3765 gfc_error ("Name '%s' at %C is the name of the procedure",
3771 if (gfc_match_char (')') == MATCH_YES)
3774 m = gfc_match_char (',');
3777 gfc_error ("Unexpected junk in formal argument list at %C");
3783 /* Check for duplicate symbols in the formal argument list. */
3786 for (p = head; p->next; p = p->next)
3791 for (q = p->next; q; q = q->next)
3792 if (p->sym == q->sym)
3794 gfc_error ("Duplicate symbol '%s' in formal argument list "
3795 "at %C", p->sym->name);
3803 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3813 gfc_free_formal_arglist (head);
3818 /* Match a RESULT specification following a function declaration or
3819 ENTRY statement. Also matches the end-of-statement. */
3822 match_result (gfc_symbol *function, gfc_symbol **result)
3824 char name[GFC_MAX_SYMBOL_LEN + 1];
3828 if (gfc_match (" result (") != MATCH_YES)
3831 m = gfc_match_name (name);
3835 /* Get the right paren, and that's it because there could be the
3836 bind(c) attribute after the result clause. */
3837 if (gfc_match_char(')') != MATCH_YES)
3839 /* TODO: should report the missing right paren here. */
3843 if (strcmp (function->name, name) == 0)
3845 gfc_error ("RESULT variable at %C must be different than function name");
3849 if (gfc_get_symbol (name, NULL, &r))
3852 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3853 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
3862 /* Match a function suffix, which could be a combination of a result
3863 clause and BIND(C), either one, or neither. The draft does not
3864 require them to come in a specific order. */
3867 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3869 match is_bind_c; /* Found bind(c). */
3870 match is_result; /* Found result clause. */
3871 match found_match; /* Status of whether we've found a good match. */
3872 int peek_char; /* Character we're going to peek at. */
3873 bool allow_binding_name;
3875 /* Initialize to having found nothing. */
3876 found_match = MATCH_NO;
3877 is_bind_c = MATCH_NO;
3878 is_result = MATCH_NO;
3880 /* Get the next char to narrow between result and bind(c). */
3881 gfc_gobble_whitespace ();
3882 peek_char = gfc_peek_char ();
3884 /* C binding names are not allowed for internal procedures. */
3885 if (gfc_current_state () == COMP_CONTAINS
3886 && sym->ns->proc_name->attr.flavor != FL_MODULE)
3887 allow_binding_name = false;
3889 allow_binding_name = true;
3894 /* Look for result clause. */
3895 is_result = match_result (sym, result);
3896 if (is_result == MATCH_YES)
3898 /* Now see if there is a bind(c) after it. */
3899 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
3900 /* We've found the result clause and possibly bind(c). */
3901 found_match = MATCH_YES;
3904 /* This should only be MATCH_ERROR. */
3905 found_match = is_result;
3908 /* Look for bind(c) first. */
3909 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
3910 if (is_bind_c == MATCH_YES)
3912 /* Now see if a result clause followed it. */
3913 is_result = match_result (sym, result);
3914 found_match = MATCH_YES;
3918 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3919 found_match = MATCH_ERROR;
3923 gfc_error ("Unexpected junk after function declaration at %C");
3924 found_match = MATCH_ERROR;
3928 if (is_bind_c == MATCH_YES)
3930 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
3931 if (gfc_current_state () == COMP_CONTAINS
3932 && sym->ns->proc_name->attr.flavor != FL_MODULE
3933 && gfc_notify_std (GFC_STD_GNU, "Extension: BIND(C) attribute at %L "
3934 "may not be specified for an internal procedure",
3939 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
3948 /* Match a PROCEDURE declaration (R1211). */
3951 match_procedure_decl (void)
3954 locus old_loc, entry_loc;
3955 gfc_symbol *sym, *proc_if = NULL;
3958 old_loc = entry_loc = gfc_current_locus;
3960 gfc_clear_ts (¤t_ts);
3962 if (gfc_match (" (") != MATCH_YES)
3964 gfc_current_locus = entry_loc;
3968 /* Get the type spec. for the procedure interface. */
3969 old_loc = gfc_current_locus;
3970 m = gfc_match_type_spec (¤t_ts, 0);
3971 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_char () == ')'))
3974 if (m == MATCH_ERROR)
3977 gfc_current_locus = old_loc;
3979 /* Get the name of the procedure or abstract interface
3980 to inherit the interface from. */
3981 m = gfc_match_symbol (&proc_if, 1);
3985 else if (m == MATCH_ERROR)
3988 /* Various interface checks. */
3991 /* Resolve interface if possible. That way, attr.procedure is only set
3992 if it is declared by a later procedure-declaration-stmt, which is
3993 invalid per C1212. */
3994 while (proc_if->interface)
3995 proc_if = proc_if->interface;
3997 if (proc_if->generic)
3999 gfc_error ("Interface '%s' at %C may not be generic", proc_if->name);
4002 if (proc_if->attr.proc == PROC_ST_FUNCTION)
4004 gfc_error ("Interface '%s' at %C may not be a statement function",
4008 /* Handle intrinsic procedures. */
4009 if (gfc_intrinsic_name (proc_if->name, 0)
4010 || gfc_intrinsic_name (proc_if->name, 1))
4011 proc_if->attr.intrinsic = 1;
4012 if (proc_if->attr.intrinsic
4013 && !gfc_intrinsic_actual_ok (proc_if->name, 0))
4015 gfc_error ("Intrinsic procedure '%s' not allowed "
4016 "in PROCEDURE statement at %C", proc_if->name);
4022 if (gfc_match (" )") != MATCH_YES)
4024 gfc_current_locus = entry_loc;
4028 /* Parse attributes. */
4029 m = match_attr_spec();
4030 if (m == MATCH_ERROR)
4033 /* Get procedure symbols. */
4036 m = gfc_match_symbol (&sym, 0);
4039 else if (m == MATCH_ERROR)
4042 /* Add current_attr to the symbol attributes. */
4043 if (gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
4046 if (sym->attr.is_bind_c)
4048 /* Check for C1218. */
4049 if (!proc_if || !proc_if->attr.is_bind_c)
4051 gfc_error ("BIND(C) attribute at %C requires "
4052 "an interface with BIND(C)");
4055 /* Check for C1217. */
4056 if (has_name_equals && sym->attr.pointer)
4058 gfc_error ("BIND(C) procedure with NAME may not have "
4059 "POINTER attribute at %C");
4062 if (has_name_equals && sym->attr.dummy)
4064 gfc_error ("Dummy procedure at %C may not have "
4065 "BIND(C) attribute with NAME");
4068 /* Set binding label for BIND(C). */
4069 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4073 if (!sym->attr.pointer && gfc_add_external (&sym->attr, NULL) == FAILURE)
4075 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4078 /* Set interface. */
4079 if (proc_if != NULL)
4081 sym->interface = proc_if;
4082 sym->attr.untyped = 1;
4084 else if (current_ts.type != BT_UNKNOWN)
4086 sym->interface = gfc_new_symbol ("", gfc_current_ns);
4087 sym->interface->ts = current_ts;
4088 sym->interface->attr.function = 1;
4089 sym->ts = sym->interface->ts;
4090 sym->attr.function = sym->interface->attr.function;
4093 if (gfc_match_eos () == MATCH_YES)
4095 if (gfc_match_char (',') != MATCH_YES)
4100 gfc_error ("Syntax error in PROCEDURE statement at %C");
4105 /* Match a PROCEDURE declaration inside an interface (R1206). */
4108 match_procedure_in_interface (void)
4112 char name[GFC_MAX_SYMBOL_LEN + 1];
4114 if (current_interface.type == INTERFACE_NAMELESS
4115 || current_interface.type == INTERFACE_ABSTRACT)
4117 gfc_error ("PROCEDURE at %C must be in a generic interface");
4123 m = gfc_match_name (name);
4126 else if (m == MATCH_ERROR)
4128 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4131 if (gfc_add_interface (sym) == FAILURE)
4134 if (gfc_match_eos () == MATCH_YES)
4136 if (gfc_match_char (',') != MATCH_YES)
4143 gfc_error ("Syntax error in PROCEDURE statement at %C");
4148 /* General matcher for PROCEDURE declarations. */
4151 gfc_match_procedure (void)
4155 switch (gfc_current_state ())
4160 case COMP_SUBROUTINE:
4162 m = match_procedure_decl ();
4164 case COMP_INTERFACE:
4165 m = match_procedure_in_interface ();
4168 gfc_error ("Fortran 2003: Procedure components at %C are "
4169 "not yet implemented in gfortran");
4178 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4186 /* Match a function declaration. */
4189 gfc_match_function_decl (void)
4191 char name[GFC_MAX_SYMBOL_LEN + 1];
4192 gfc_symbol *sym, *result;
4196 match found_match; /* Status returned by match func. */
4198 if (gfc_current_state () != COMP_NONE
4199 && gfc_current_state () != COMP_INTERFACE
4200 && gfc_current_state () != COMP_CONTAINS)
4203 gfc_clear_ts (¤t_ts);
4205 old_loc = gfc_current_locus;
4207 m = match_prefix (¤t_ts);
4210 gfc_current_locus = old_loc;
4214 if (gfc_match ("function% %n", name) != MATCH_YES)
4216 gfc_current_locus = old_loc;
4219 if (get_proc_name (name, &sym, false))
4221 gfc_new_block = sym;
4223 m = gfc_match_formal_arglist (sym, 0, 0);
4226 gfc_error ("Expected formal argument list in function "
4227 "definition at %C");
4231 else if (m == MATCH_ERROR)
4236 /* According to the draft, the bind(c) and result clause can
4237 come in either order after the formal_arg_list (i.e., either
4238 can be first, both can exist together or by themselves or neither
4239 one). Therefore, the match_result can't match the end of the
4240 string, and check for the bind(c) or result clause in either order. */
4241 found_match = gfc_match_eos ();
4243 /* Make sure that it isn't already declared as BIND(C). If it is, it
4244 must have been marked BIND(C) with a BIND(C) attribute and that is
4245 not allowed for procedures. */
4246 if (sym->attr.is_bind_c == 1)
4248 sym->attr.is_bind_c = 0;
4249 if (sym->old_symbol != NULL)
4250 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4251 "variables or common blocks",
4252 &(sym->old_symbol->declared_at));
4254 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4255 "variables or common blocks", &gfc_current_locus);
4258 if (found_match != MATCH_YES)
4260 /* If we haven't found the end-of-statement, look for a suffix. */
4261 suffix_match = gfc_match_suffix (sym, &result);
4262 if (suffix_match == MATCH_YES)
4263 /* Need to get the eos now. */
4264 found_match = gfc_match_eos ();
4266 found_match = suffix_match;
4269 if(found_match != MATCH_YES)
4273 /* Make changes to the symbol. */
4276 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4279 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4280 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4283 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
4284 && !sym->attr.implicit_type)
4286 gfc_error ("Function '%s' at %C already has a type of %s", name,
4287 gfc_basic_typename (sym->ts.type));
4293 sym->ts = current_ts;
4298 result->ts = current_ts;
4299 sym->result = result;
4306 gfc_current_locus = old_loc;
4311 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4312 pass the name of the entry, rather than the gfc_current_block name, and
4313 to return false upon finding an existing global entry. */
4316 add_global_entry (const char *name, int sub)
4320 s = gfc_get_gsymbol(name);
4323 || (s->type != GSYM_UNKNOWN
4324 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
4325 gfc_global_used(s, NULL);
4328 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4329 s->where = gfc_current_locus;
4337 /* Match an ENTRY statement. */
4340 gfc_match_entry (void)
4345 char name[GFC_MAX_SYMBOL_LEN + 1];
4346 gfc_compile_state state;
4350 bool module_procedure;
4354 m = gfc_match_name (name);
4358 state = gfc_current_state ();
4359 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
4364 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4367 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4369 case COMP_BLOCK_DATA:
4370 gfc_error ("ENTRY statement at %C cannot appear within "
4373 case COMP_INTERFACE:
4374 gfc_error ("ENTRY statement at %C cannot appear within "
4378 gfc_error ("ENTRY statement at %C cannot appear within "
4379 "a DERIVED TYPE block");
4382 gfc_error ("ENTRY statement at %C cannot appear within "
4383 "an IF-THEN block");
4386 gfc_error ("ENTRY statement at %C cannot appear within "
4390 gfc_error ("ENTRY statement at %C cannot appear within "
4394 gfc_error ("ENTRY statement at %C cannot appear within "
4398 gfc_error ("ENTRY statement at %C cannot appear within "
4402 gfc_error ("ENTRY statement at %C cannot appear within "
4403 "a contained subprogram");
4406 gfc_internal_error ("gfc_match_entry(): Bad state");
4411 module_procedure = gfc_current_ns->parent != NULL
4412 && gfc_current_ns->parent->proc_name
4413 && gfc_current_ns->parent->proc_name->attr.flavor
4416 if (gfc_current_ns->parent != NULL
4417 && gfc_current_ns->parent->proc_name
4418 && !module_procedure)
4420 gfc_error("ENTRY statement at %C cannot appear in a "
4421 "contained procedure");
4425 /* Module function entries need special care in get_proc_name
4426 because previous references within the function will have
4427 created symbols attached to the current namespace. */
4428 if (get_proc_name (name, &entry,
4429 gfc_current_ns->parent != NULL
4431 && gfc_current_ns->proc_name->attr.function))
4434 proc = gfc_current_block ();
4436 /* Make sure that it isn't already declared as BIND(C). If it is, it
4437 must have been marked BIND(C) with a BIND(C) attribute and that is
4438 not allowed for procedures. */
4439 if (entry->attr.is_bind_c == 1)
4441 entry->attr.is_bind_c = 0;
4442 if (entry->old_symbol != NULL)
4443 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4444 "variables or common blocks",
4445 &(entry->old_symbol->declared_at));
4447 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4448 "variables or common blocks", &gfc_current_locus);
4451 /* Check what next non-whitespace character is so we can tell if there
4452 is the required parens if we have a BIND(C). */
4453 gfc_gobble_whitespace ();
4454 peek_char = gfc_peek_char ();
4456 if (state == COMP_SUBROUTINE)
4458 /* An entry in a subroutine. */
4459 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
4462 m = gfc_match_formal_arglist (entry, 0, 1);
4466 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
4467 never be an internal procedure. */
4468 is_bind_c = gfc_match_bind_c (entry, true);
4469 if (is_bind_c == MATCH_ERROR)
4471 if (is_bind_c == MATCH_YES)
4473 if (peek_char != '(')
4475 gfc_error ("Missing required parentheses before BIND(C) at %C");
4478 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
4483 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4484 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4489 /* An entry in a function.
4490 We need to take special care because writing
4495 ENTRY f() RESULT (r)
4497 ENTRY f RESULT (r). */
4498 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
4501 old_loc = gfc_current_locus;
4502 if (gfc_match_eos () == MATCH_YES)
4504 gfc_current_locus = old_loc;
4505 /* Match the empty argument list, and add the interface to
4507 m = gfc_match_formal_arglist (entry, 0, 1);
4510 m = gfc_match_formal_arglist (entry, 0, 0);
4517 if (gfc_match_eos () == MATCH_YES)
4519 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4520 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4523 entry->result = entry;
4527 m = gfc_match_suffix (entry, &result);
4529 gfc_syntax_error (ST_ENTRY);
4535 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4536 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
4537 || gfc_add_function (&entry->attr, result->name, NULL)
4540 entry->result = result;
4544 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4545 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4547 entry->result = entry;
4552 if (gfc_match_eos () != MATCH_YES)
4554 gfc_syntax_error (ST_ENTRY);
4558 entry->attr.recursive = proc->attr.recursive;
4559 entry->attr.elemental = proc->attr.elemental;
4560 entry->attr.pure = proc->attr.pure;
4562 el = gfc_get_entry_list ();
4564 el->next = gfc_current_ns->entries;
4565 gfc_current_ns->entries = el;
4567 el->id = el->next->id + 1;
4571 new_st.op = EXEC_ENTRY;
4572 new_st.ext.entry = el;
4578 /* Match a subroutine statement, including optional prefixes. */
4581 gfc_match_subroutine (void)
4583 char name[GFC_MAX_SYMBOL_LEN + 1];
4588 bool allow_binding_name;
4590 if (gfc_current_state () != COMP_NONE
4591 && gfc_current_state () != COMP_INTERFACE
4592 && gfc_current_state () != COMP_CONTAINS)
4595 m = match_prefix (NULL);
4599 m = gfc_match ("subroutine% %n", name);
4603 if (get_proc_name (name, &sym, false))
4605 gfc_new_block = sym;
4607 /* Check what next non-whitespace character is so we can tell if there
4608 is the required parens if we have a BIND(C). */
4609 gfc_gobble_whitespace ();
4610 peek_char = gfc_peek_char ();
4612 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
4615 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4618 /* Make sure that it isn't already declared as BIND(C). If it is, it
4619 must have been marked BIND(C) with a BIND(C) attribute and that is
4620 not allowed for procedures. */
4621 if (sym->attr.is_bind_c == 1)
4623 sym->attr.is_bind_c = 0;
4624 if (sym->old_symbol != NULL)
4625 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4626 "variables or common blocks",
4627 &(sym->old_symbol->declared_at));
4629 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4630 "variables or common blocks", &gfc_current_locus);
4633 /* C binding names are not allowed for internal procedures. */
4634 if (gfc_current_state () == COMP_CONTAINS
4635 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4636 allow_binding_name = false;
4638 allow_binding_name = true;
4640 /* Here, we are just checking if it has the bind(c) attribute, and if
4641 so, then we need to make sure it's all correct. If it doesn't,
4642 we still need to continue matching the rest of the subroutine line. */
4643 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4644 if (is_bind_c == MATCH_ERROR)
4646 /* There was an attempt at the bind(c), but it was wrong. An
4647 error message should have been printed w/in the gfc_match_bind_c
4648 so here we'll just return the MATCH_ERROR. */
4652 if (is_bind_c == MATCH_YES)
4654 /* The following is allowed in the Fortran 2008 draft. */
4655 if (gfc_current_state () == COMP_CONTAINS
4656 && sym->ns->proc_name->attr.flavor != FL_MODULE
4657 && gfc_notify_std (GFC_STD_GNU, "Extension: BIND(C) attribute at "
4658 "%L may not be specified for an internal procedure",
4663 if (peek_char != '(')
4665 gfc_error ("Missing required parentheses before BIND(C) at %C");
4668 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4673 if (gfc_match_eos () != MATCH_YES)
4675 gfc_syntax_error (ST_SUBROUTINE);
4679 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4686 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4687 given, and set the binding label in either the given symbol (if not
4688 NULL), or in the current_ts. The symbol may be NULL because we may
4689 encounter the BIND(C) before the declaration itself. Return
4690 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4691 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4692 or MATCH_YES if the specifier was correct and the binding label and
4693 bind(c) fields were set correctly for the given symbol or the
4694 current_ts. If allow_binding_name is false, no binding name may be
4698 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
4700 /* binding label, if exists */
4701 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4705 /* Initialize the flag that specifies whether we encountered a NAME=
4706 specifier or not. */
4707 has_name_equals = 0;
4709 /* Init the first char to nil so we can catch if we don't have
4710 the label (name attr) or the symbol name yet. */
4711 binding_label[0] = '\0';
4713 /* This much we have to be able to match, in this order, if
4714 there is a bind(c) label. */
4715 if (gfc_match (" bind ( c ") != MATCH_YES)
4718 /* Now see if there is a binding label, or if we've reached the
4719 end of the bind(c) attribute without one. */
4720 if (gfc_match_char (',') == MATCH_YES)
4722 if (gfc_match (" name = ") != MATCH_YES)
4724 gfc_error ("Syntax error in NAME= specifier for binding label "
4726 /* should give an error message here */
4730 has_name_equals = 1;
4732 /* Get the opening quote. */
4733 double_quote = MATCH_YES;
4734 single_quote = MATCH_YES;
4735 double_quote = gfc_match_char ('"');
4736 if (double_quote != MATCH_YES)
4737 single_quote = gfc_match_char ('\'');
4738 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4740 gfc_error ("Syntax error in NAME= specifier for binding label "
4745 /* Grab the binding label, using functions that will not lower
4746 case the names automatically. */
4747 if (gfc_match_name_C (binding_label) != MATCH_YES)
4750 /* Get the closing quotation. */
4751 if (double_quote == MATCH_YES)
4753 if (gfc_match_char ('"') != MATCH_YES)
4755 gfc_error ("Missing closing quote '\"' for binding label at %C");
4756 /* User started string with '"' so looked to match it. */
4762 if (gfc_match_char ('\'') != MATCH_YES)
4764 gfc_error ("Missing closing quote '\'' for binding label at %C");
4765 /* User started string with "'" char. */
4771 /* Get the required right paren. */
4772 if (gfc_match_char (')') != MATCH_YES)
4774 gfc_error ("Missing closing paren for binding label at %C");
4778 if (has_name_equals && !allow_binding_name)
4780 gfc_error ("No binding name is allowed in BIND(C) at %C");
4784 if (has_name_equals && sym != NULL && sym->attr.dummy)
4786 gfc_error ("For dummy procedure %s, no binding name is "
4787 "allowed in BIND(C) at %C", sym->name);
4792 /* Save the binding label to the symbol. If sym is null, we're
4793 probably matching the typespec attributes of a declaration and
4794 haven't gotten the name yet, and therefore, no symbol yet. */
4795 if (binding_label[0] != '\0')
4799 strcpy (sym->binding_label, binding_label);
4802 strcpy (curr_binding_label, binding_label);
4804 else if (allow_binding_name)
4806 /* No binding label, but if symbol isn't null, we
4807 can set the label for it here.
4808 If name="" or allow_binding_name is false, no C binding name is
4810 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4811 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4814 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
4815 && current_interface.type == INTERFACE_ABSTRACT)
4817 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
4825 /* Return nonzero if we're currently compiling a contained procedure. */
4828 contained_procedure (void)
4832 for (s=gfc_state_stack; s; s=s->previous)
4833 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
4834 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
4840 /* Set the kind of each enumerator. The kind is selected such that it is
4841 interoperable with the corresponding C enumeration type, making
4842 sure that -fshort-enums is honored. */
4847 enumerator_history *current_history = NULL;
4851 if (max_enum == NULL || enum_history == NULL)
4854 if (!gfc_option.fshort_enums)
4860 kind = gfc_integer_kinds[i++].kind;
4862 while (kind < gfc_c_int_kind
4863 && gfc_check_integer_range (max_enum->initializer->value.integer,
4866 current_history = enum_history;
4867 while (current_history != NULL)
4869 current_history->sym->ts.kind = kind;
4870 current_history = current_history->next;
4875 /* Match any of the various end-block statements. Returns the type of
4876 END to the caller. The END INTERFACE, END IF, END DO and END
4877 SELECT statements cannot be replaced by a single END statement. */
4880 gfc_match_end (gfc_statement *st)
4882 char name[GFC_MAX_SYMBOL_LEN + 1];
4883 gfc_compile_state state;
4885 const char *block_name;
4890 old_loc = gfc_current_locus;
4891 if (gfc_match ("end") != MATCH_YES)
4894 state = gfc_current_state ();
4895 block_name = gfc_current_block () == NULL
4896 ? NULL : gfc_current_block ()->name;
4898 if (state == COMP_CONTAINS)
4900 state = gfc_state_stack->previous->state;
4901 block_name = gfc_state_stack->previous->sym == NULL
4902 ? NULL : gfc_state_stack->previous->sym->name;
4909 *st = ST_END_PROGRAM;
4910 target = " program";
4914 case COMP_SUBROUTINE:
4915 *st = ST_END_SUBROUTINE;
4916 target = " subroutine";
4917 eos_ok = !contained_procedure ();
4921 *st = ST_END_FUNCTION;
4922 target = " function";
4923 eos_ok = !contained_procedure ();
4926 case COMP_BLOCK_DATA:
4927 *st = ST_END_BLOCK_DATA;
4928 target = " block data";
4933 *st = ST_END_MODULE;
4938 case COMP_INTERFACE:
4939 *st = ST_END_INTERFACE;
4940 target = " interface";
4963 *st = ST_END_SELECT;
4969 *st = ST_END_FORALL;
4984 last_initializer = NULL;
4986 gfc_free_enum_history ();
4990 gfc_error ("Unexpected END statement at %C");
4994 if (gfc_match_eos () == MATCH_YES)
4998 /* We would have required END [something]. */
4999 gfc_error ("%s statement expected at %L",
5000 gfc_ascii_statement (*st), &old_loc);
5007 /* Verify that we've got the sort of end-block that we're expecting. */
5008 if (gfc_match (target) != MATCH_YES)
5010 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5014 /* If we're at the end, make sure a block name wasn't required. */
5015 if (gfc_match_eos () == MATCH_YES)
5018 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5019 && *st != ST_END_FORALL && *st != ST_END_WHERE)
5022 if (gfc_current_block () == NULL)
5025 gfc_error ("Expected block name of '%s' in %s statement at %C",
5026 block_name, gfc_ascii_statement (*st));
5031 /* END INTERFACE has a special handler for its several possible endings. */
5032 if (*st == ST_END_INTERFACE)
5033 return gfc_match_end_interface ();
5035 /* We haven't hit the end of statement, so what is left must be an
5037 m = gfc_match_space ();
5039 m = gfc_match_name (name);
5042 gfc_error ("Expected terminating name at %C");
5046 if (block_name == NULL)
5049 if (strcmp (name, block_name) != 0)
5051 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5052 gfc_ascii_statement (*st));
5056 if (gfc_match_eos () == MATCH_YES)
5060 gfc_syntax_error (*st);
5063 gfc_current_locus = old_loc;
5069 /***************** Attribute declaration statements ****************/
5071 /* Set the attribute of a single variable. */
5076 char name[GFC_MAX_SYMBOL_LEN + 1];
5084 m = gfc_match_name (name);
5088 if (find_special (name, &sym))
5091 var_locus = gfc_current_locus;
5093 /* Deal with possible array specification for certain attributes. */
5094 if (current_attr.dimension
5095 || current_attr.allocatable
5096 || current_attr.pointer
5097 || current_attr.target)
5099 m = gfc_match_array_spec (&as);
5100 if (m == MATCH_ERROR)
5103 if (current_attr.dimension && m == MATCH_NO)
5105 gfc_error ("Missing array specification at %L in DIMENSION "
5106 "statement", &var_locus);
5111 if ((current_attr.allocatable || current_attr.pointer)
5112 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5114 gfc_error ("Array specification must be deferred at %L", &var_locus);
5120 /* Update symbol table. DIMENSION attribute is set
5121 in gfc_set_array_spec(). */
5122 if (current_attr.dimension == 0
5123 && gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
5129 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5135 if (sym->attr.cray_pointee && sym->as != NULL)
5137 /* Fix the array spec. */
5138 m = gfc_mod_pointee_as (sym->as);
5139 if (m == MATCH_ERROR)
5143 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
5149 if ((current_attr.external || current_attr.intrinsic)
5150 && sym->attr.flavor != FL_PROCEDURE
5151 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
5160 gfc_free_array_spec (as);
5165 /* Generic attribute declaration subroutine. Used for attributes that
5166 just have a list of names. */
5173 /* Gobble the optional double colon, by simply ignoring the result
5183 if (gfc_match_eos () == MATCH_YES)
5189 if (gfc_match_char (',') != MATCH_YES)
5191 gfc_error ("Unexpected character in variable list at %C");
5201 /* This routine matches Cray Pointer declarations of the form:
5202 pointer ( <pointer>, <pointee> )
5204 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5205 The pointer, if already declared, should be an integer. Otherwise, we
5206 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5207 be either a scalar, or an array declaration. No space is allocated for
5208 the pointee. For the statement
5209 pointer (ipt, ar(10))
5210 any subsequent uses of ar will be translated (in C-notation) as
5211 ar(i) => ((<type> *) ipt)(i)
5212 After gimplification, pointee variable will disappear in the code. */
5215 cray_pointer_decl (void)
5219 gfc_symbol *cptr; /* Pointer symbol. */
5220 gfc_symbol *cpte; /* Pointee symbol. */
5226 if (gfc_match_char ('(') != MATCH_YES)
5228 gfc_error ("Expected '(' at %C");
5232 /* Match pointer. */
5233 var_locus = gfc_current_locus;
5234 gfc_clear_attr (¤t_attr);
5235 gfc_add_cray_pointer (¤t_attr, &var_locus);
5236 current_ts.type = BT_INTEGER;
5237 current_ts.kind = gfc_index_integer_kind;
5239 m = gfc_match_symbol (&cptr, 0);
5242 gfc_error ("Expected variable name at %C");
5246 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5249 gfc_set_sym_referenced (cptr);
5251 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5253 cptr->ts.type = BT_INTEGER;
5254 cptr->ts.kind = gfc_index_integer_kind;
5256 else if (cptr->ts.type != BT_INTEGER)
5258 gfc_error ("Cray pointer at %C must be an integer");
5261 else if (cptr->ts.kind < gfc_index_integer_kind)
5262 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5263 " memory addresses require %d bytes",
5264 cptr->ts.kind, gfc_index_integer_kind);
5266 if (gfc_match_char (',') != MATCH_YES)
5268 gfc_error ("Expected \",\" at %C");
5272 /* Match Pointee. */
5273 var_locus = gfc_current_locus;
5274 gfc_clear_attr (¤t_attr);
5275 gfc_add_cray_pointee (¤t_attr, &var_locus);
5276 current_ts.type = BT_UNKNOWN;
5277 current_ts.kind = 0;
5279 m = gfc_match_symbol (&cpte, 0);
5282 gfc_error ("Expected variable name at %C");
5286 /* Check for an optional array spec. */
5287 m = gfc_match_array_spec (&as);
5288 if (m == MATCH_ERROR)
5290 gfc_free_array_spec (as);
5293 else if (m == MATCH_NO)
5295 gfc_free_array_spec (as);
5299 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5302 gfc_set_sym_referenced (cpte);
5304 if (cpte->as == NULL)
5306 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5307 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5309 else if (as != NULL)
5311 gfc_error ("Duplicate array spec for Cray pointee at %C");
5312 gfc_free_array_spec (as);
5318 if (cpte->as != NULL)
5320 /* Fix array spec. */
5321 m = gfc_mod_pointee_as (cpte->as);
5322 if (m == MATCH_ERROR)
5326 /* Point the Pointee at the Pointer. */
5327 cpte->cp_pointer = cptr;
5329 if (gfc_match_char (')') != MATCH_YES)
5331 gfc_error ("Expected \")\" at %C");
5334 m = gfc_match_char (',');
5336 done = true; /* Stop searching for more declarations. */
5340 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5341 || gfc_match_eos () != MATCH_YES)
5343 gfc_error ("Expected \",\" or end of statement at %C");
5351 gfc_match_external (void)
5354 gfc_clear_attr (¤t_attr);
5355 current_attr.external = 1;
5357 return attr_decl ();
5362 gfc_match_intent (void)
5366 intent = match_intent_spec ();
5367 if (intent == INTENT_UNKNOWN)
5370 gfc_clear_attr (¤t_attr);
5371 current_attr.intent = intent;
5373 return attr_decl ();
5378 gfc_match_intrinsic (void)
5381 gfc_clear_attr (¤t_attr);
5382 current_attr.intrinsic = 1;
5384 return attr_decl ();
5389 gfc_match_optional (void)
5392 gfc_clear_attr (¤t_attr);
5393 current_attr.optional = 1;
5395 return attr_decl ();
5400 gfc_match_pointer (void)
5402 gfc_gobble_whitespace ();
5403 if (gfc_peek_char () == '(')
5405 if (!gfc_option.flag_cray_pointer)
5407 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5411 return cray_pointer_decl ();
5415 gfc_clear_attr (¤t_attr);
5416 current_attr.pointer = 1;
5418 return attr_decl ();
5424 gfc_match_allocatable (void)
5426 gfc_clear_attr (¤t_attr);
5427 current_attr.allocatable = 1;
5429 return attr_decl ();
5434 gfc_match_dimension (void)
5436 gfc_clear_attr (¤t_attr);
5437 current_attr.dimension = 1;
5439 return attr_decl ();
5444 gfc_match_target (void)
5446 gfc_clear_attr (¤t_attr);
5447 current_attr.target = 1;
5449 return attr_decl ();
5453 /* Match the list of entities being specified in a PUBLIC or PRIVATE
5457 access_attr_decl (gfc_statement st)
5459 char name[GFC_MAX_SYMBOL_LEN + 1];
5460 interface_type type;
5463 gfc_intrinsic_op operator;
5466 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5471 m = gfc_match_generic_spec (&type, name, &operator);
5474 if (m == MATCH_ERROR)
5479 case INTERFACE_NAMELESS:
5480 case INTERFACE_ABSTRACT:
5483 case INTERFACE_GENERIC:
5484 if (gfc_get_symbol (name, NULL, &sym))
5487 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5488 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
5489 sym->name, NULL) == FAILURE)
5494 case INTERFACE_INTRINSIC_OP:
5495 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
5497 gfc_current_ns->operator_access[operator] =
5498 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5502 gfc_error ("Access specification of the %s operator at %C has "
5503 "already been specified", gfc_op2string (operator));
5509 case INTERFACE_USER_OP:
5510 uop = gfc_get_uop (name);
5512 if (uop->access == ACCESS_UNKNOWN)
5514 uop->access = (st == ST_PUBLIC)
5515 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5519 gfc_error ("Access specification of the .%s. operator at %C "
5520 "has already been specified", sym->name);
5527 if (gfc_match_char (',') == MATCH_NO)
5531 if (gfc_match_eos () != MATCH_YES)
5536 gfc_syntax_error (st);
5544 gfc_match_protected (void)
5549 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5551 gfc_error ("PROTECTED at %C only allowed in specification "
5552 "part of a module");
5557 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
5561 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5566 if (gfc_match_eos () == MATCH_YES)
5571 m = gfc_match_symbol (&sym, 0);
5575 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
5588 if (gfc_match_eos () == MATCH_YES)
5590 if (gfc_match_char (',') != MATCH_YES)
5597 gfc_error ("Syntax error in PROTECTED statement at %C");
5602 /* The PRIVATE statement is a bit weird in that it can be an attribute
5603 declaration, but also works as a standlone statement inside of a
5604 type declaration or a module. */
5607 gfc_match_private (gfc_statement *st)
5610 if (gfc_match ("private") != MATCH_YES)
5613 if (gfc_current_state () != COMP_MODULE
5614 && (gfc_current_state () != COMP_DERIVED
5615 || !gfc_state_stack->previous
5616 || gfc_state_stack->previous->state != COMP_MODULE))
5618 gfc_error ("PRIVATE statement at %C is only allowed in the "
5619 "specification part of a module");
5623 if (gfc_current_state () == COMP_DERIVED)
5625 if (gfc_match_eos () == MATCH_YES)
5631 gfc_syntax_error (ST_PRIVATE);
5635 if (gfc_match_eos () == MATCH_YES)
5642 return access_attr_decl (ST_PRIVATE);
5647 gfc_match_public (gfc_statement *st)
5650 if (gfc_match ("public") != MATCH_YES)
5653 if (gfc_current_state () != COMP_MODULE)
5655 gfc_error ("PUBLIC statement at %C is only allowed in the "
5656 "specification part of a module");
5660 if (gfc_match_eos () == MATCH_YES)
5667 return access_attr_decl (ST_PUBLIC);
5671 /* Workhorse for gfc_match_parameter. */
5680 m = gfc_match_symbol (&sym, 0);
5682 gfc_error ("Expected variable name at %C in PARAMETER statement");
5687 if (gfc_match_char ('=') == MATCH_NO)
5689 gfc_error ("Expected = sign in PARAMETER statement at %C");
5693 m = gfc_match_init_expr (&init);
5695 gfc_error ("Expected expression at %C in PARAMETER statement");
5699 if (sym->ts.type == BT_UNKNOWN
5700 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5706 if (gfc_check_assign_symbol (sym, init) == FAILURE
5707 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
5713 if (sym->ts.type == BT_CHARACTER
5714 && sym->ts.cl != NULL
5715 && sym->ts.cl->length != NULL
5716 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5717 && init->expr_type == EXPR_CONSTANT
5718 && init->ts.type == BT_CHARACTER
5719 && init->ts.kind == 1)
5720 gfc_set_constant_character_len (
5721 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
5727 gfc_free_expr (init);
5732 /* Match a parameter statement, with the weird syntax that these have. */
5735 gfc_match_parameter (void)
5739 if (gfc_match_char ('(') == MATCH_NO)
5748 if (gfc_match (" )%t") == MATCH_YES)
5751 if (gfc_match_char (',') != MATCH_YES)
5753 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5763 /* Save statements have a special syntax. */
5766 gfc_match_save (void)
5768 char n[GFC_MAX_SYMBOL_LEN+1];
5773 if (gfc_match_eos () == MATCH_YES)
5775 if (gfc_current_ns->seen_save)
5777 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5778 "follows previous SAVE statement")
5783 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5787 if (gfc_current_ns->save_all)
5789 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5790 "blanket SAVE statement")
5799 m = gfc_match_symbol (&sym, 0);
5803 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5815 m = gfc_match (" / %n /", &n);
5816 if (m == MATCH_ERROR)
5821 c = gfc_get_common (n, 0);
5824 gfc_current_ns->seen_save = 1;
5827 if (gfc_match_eos () == MATCH_YES)
5829 if (gfc_match_char (',') != MATCH_YES)
5836 gfc_error ("Syntax error in SAVE statement at %C");
5842 gfc_match_value (void)
5847 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
5851 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5856 if (gfc_match_eos () == MATCH_YES)
5861 m = gfc_match_symbol (&sym, 0);
5865 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5878 if (gfc_match_eos () == MATCH_YES)
5880 if (gfc_match_char (',') != MATCH_YES)
5887 gfc_error ("Syntax error in VALUE statement at %C");
5893 gfc_match_volatile (void)
5898 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
5902 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5907 if (gfc_match_eos () == MATCH_YES)
5912 /* VOLATILE is special because it can be added to host-associated
5914 m = gfc_match_symbol (&sym, 1);
5918 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
5931 if (gfc_match_eos () == MATCH_YES)
5933 if (gfc_match_char (',') != MATCH_YES)
5940 gfc_error ("Syntax error in VOLATILE statement at %C");
5945 /* Match a module procedure statement. Note that we have to modify
5946 symbols in the parent's namespace because the current one was there
5947 to receive symbols that are in an interface's formal argument list. */
5950 gfc_match_modproc (void)
5952 char name[GFC_MAX_SYMBOL_LEN + 1];
5955 gfc_namespace *module_ns;
5956 gfc_interface *old_interface_head, *interface;
5958 if (gfc_state_stack->state != COMP_INTERFACE
5959 || gfc_state_stack->previous == NULL
5960 || current_interface.type == INTERFACE_NAMELESS
5961 || current_interface.type == INTERFACE_ABSTRACT)
5963 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5968 module_ns = gfc_current_ns->parent;
5969 for (; module_ns; module_ns = module_ns->parent)
5970 if (module_ns->proc_name->attr.flavor == FL_MODULE)
5973 if (module_ns == NULL)
5976 /* Store the current state of the interface. We will need it if we
5977 end up with a syntax error and need to recover. */
5978 old_interface_head = gfc_current_interface_head ();
5984 m = gfc_match_name (name);
5990 /* Check for syntax error before starting to add symbols to the
5991 current namespace. */
5992 if (gfc_match_eos () == MATCH_YES)
5994 if (!last && gfc_match_char (',') != MATCH_YES)
5997 /* Now we're sure the syntax is valid, we process this item
5999 if (gfc_get_symbol (name, module_ns, &sym))
6002 if (sym->attr.proc != PROC_MODULE
6003 && gfc_add_procedure (&sym->attr, PROC_MODULE,
6004 sym->name, NULL) == FAILURE)
6007 if (gfc_add_interface (sym) == FAILURE)
6010 sym->attr.mod_proc = 1;
6019 /* Restore the previous state of the interface. */
6020 interface = gfc_current_interface_head ();
6021 gfc_set_current_interface_head (old_interface_head);
6023 /* Free the new interfaces. */
6024 while (interface != old_interface_head)
6026 gfc_interface *i = interface->next;
6027 gfc_free (interface);
6031 /* And issue a syntax error. */
6032 gfc_syntax_error (ST_MODULE_PROC);
6037 /* Match the optional attribute specifiers for a type declaration.
6038 Return MATCH_ERROR if an error is encountered in one of the handled
6039 attributes (public, private, bind(c)), MATCH_NO if what's found is
6040 not a handled attribute, and MATCH_YES otherwise. TODO: More error
6041 checking on attribute conflicts needs to be done. */
6044 gfc_get_type_attr_spec (symbol_attribute *attr)
6046 /* See if the derived type is marked as private. */
6047 if (gfc_match (" , private") == MATCH_YES)
6049 if (gfc_current_state () != COMP_MODULE)
6051 gfc_error ("Derived type at %C can only be PRIVATE in the "
6052 "specification part of a module");
6056 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6059 else if (gfc_match (" , public") == MATCH_YES)
6061 if (gfc_current_state () != COMP_MODULE)
6063 gfc_error ("Derived type at %C can only be PUBLIC in the "
6064 "specification part of a module");
6068 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6071 else if (gfc_match(" , bind ( c )") == MATCH_YES)
6073 /* If the type is defined to be bind(c) it then needs to make
6074 sure that all fields are interoperable. This will
6075 need to be a semantic check on the finished derived type.
6076 See 15.2.3 (lines 9-12) of F2003 draft. */
6077 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
6080 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
6085 /* If we get here, something matched. */
6090 /* Match the beginning of a derived type declaration. If a type name
6091 was the result of a function, then it is possible to have a symbol
6092 already to be known as a derived type yet have no components. */
6095 gfc_match_derived_decl (void)
6097 char name[GFC_MAX_SYMBOL_LEN + 1];
6098 symbol_attribute attr;
6101 match is_type_attr_spec = MATCH_NO;
6102 bool seen_attr = false;
6104 if (gfc_current_state () == COMP_DERIVED)
6107 gfc_clear_attr (&attr);
6111 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
6112 if (is_type_attr_spec == MATCH_ERROR)
6114 if (is_type_attr_spec == MATCH_YES)
6116 } while (is_type_attr_spec == MATCH_YES);
6118 if (gfc_match (" ::") != MATCH_YES && seen_attr)
6120 gfc_error ("Expected :: in TYPE definition at %C");
6124 m = gfc_match (" %n%t", name);
6128 /* Make sure the name is not the name of an intrinsic type. */
6129 if (gfc_is_intrinsic_typename (name))
6131 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
6136 if (gfc_get_symbol (name, NULL, &sym))
6139 if (sym->ts.type != BT_UNKNOWN)
6141 gfc_error ("Derived type name '%s' at %C already has a basic type "
6142 "of %s", sym->name, gfc_typename (&sym->ts));
6146 /* The symbol may already have the derived attribute without the
6147 components. The ways this can happen is via a function
6148 definition, an INTRINSIC statement or a subtype in another
6149 derived type that is a pointer. The first part of the AND clause
6150 is true if a the symbol is not the return value of a function. */
6151 if (sym->attr.flavor != FL_DERIVED
6152 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6155 if (sym->components != NULL || sym->attr.zero_comp)
6157 gfc_error ("Derived type definition of '%s' at %C has already been "
6158 "defined", sym->name);
6162 if (attr.access != ACCESS_UNKNOWN
6163 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6166 /* See if the derived type was labeled as bind(c). */
6167 if (attr.is_bind_c != 0)
6168 sym->attr.is_bind_c = attr.is_bind_c;
6170 gfc_new_block = sym;
6176 /* Cray Pointees can be declared as:
6177 pointer (ipt, a (n,m,...,*))
6178 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6179 cheat and set a constant bound of 1 for the last dimension, if this
6180 is the case. Since there is no bounds-checking for Cray Pointees,
6181 this will be okay. */
6184 gfc_mod_pointee_as (gfc_array_spec *as)
6186 as->cray_pointee = true; /* This will be useful to know later. */
6187 if (as->type == AS_ASSUMED_SIZE)
6189 as->type = AS_EXPLICIT;
6190 as->upper[as->rank - 1] = gfc_int_expr (1);
6191 as->cp_was_assumed = true;
6193 else if (as->type == AS_ASSUMED_SHAPE)
6195 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6202 /* Match the enum definition statement, here we are trying to match
6203 the first line of enum definition statement.
6204 Returns MATCH_YES if match is found. */
6207 gfc_match_enum (void)
6211 m = gfc_match_eos ();
6215 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
6223 /* Match a variable name with an optional initializer. When this
6224 subroutine is called, a variable is expected to be parsed next.
6225 Depending on what is happening at the moment, updates either the
6226 symbol table or the current interface. */
6229 enumerator_decl (void)
6231 char name[GFC_MAX_SYMBOL_LEN + 1];
6232 gfc_expr *initializer;
6233 gfc_array_spec *as = NULL;
6241 old_locus = gfc_current_locus;
6243 /* When we get here, we've just matched a list of attributes and
6244 maybe a type and a double colon. The next thing we expect to see
6245 is the name of the symbol. */
6246 m = gfc_match_name (name);
6250 var_locus = gfc_current_locus;
6252 /* OK, we've successfully matched the declaration. Now put the
6253 symbol in the current namespace. If we fail to create the symbol,
6255 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6261 /* The double colon must be present in order to have initializers.
6262 Otherwise the statement is ambiguous with an assignment statement. */
6265 if (gfc_match_char ('=') == MATCH_YES)
6267 m = gfc_match_init_expr (&initializer);
6270 gfc_error ("Expected an initialization expression at %C");
6279 /* If we do not have an initializer, the initialization value of the
6280 previous enumerator (stored in last_initializer) is incremented
6281 by 1 and is used to initialize the current enumerator. */
6282 if (initializer == NULL)
6283 initializer = gfc_enum_initializer (last_initializer, old_locus);
6285 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6287 gfc_error("ENUMERATOR %L not initialized with integer expression",
6290 gfc_free_enum_history ();
6294 /* Store this current initializer, for the next enumerator variable
6295 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6296 use last_initializer below. */
6297 last_initializer = initializer;
6298 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6300 /* Maintain enumerator history. */
6301 gfc_find_symbol (name, NULL, 0, &sym);
6302 create_enum_history (sym, last_initializer);
6304 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6307 /* Free stuff up and return. */
6308 gfc_free_expr (initializer);
6314 /* Match the enumerator definition statement. */
6317 gfc_match_enumerator_def (void)
6322 gfc_clear_ts (¤t_ts);
6324 m = gfc_match (" enumerator");
6328 m = gfc_match (" :: ");
6329 if (m == MATCH_ERROR)
6332 colon_seen = (m == MATCH_YES);
6334 if (gfc_current_state () != COMP_ENUM)
6336 gfc_error ("ENUM definition statement expected before %C");
6337 gfc_free_enum_history ();
6341 (¤t_ts)->type = BT_INTEGER;
6342 (¤t_ts)->kind = gfc_c_int_kind;
6344 gfc_clear_attr (¤t_attr);
6345 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
6354 m = enumerator_decl ();
6355 if (m == MATCH_ERROR)
6360 if (gfc_match_eos () == MATCH_YES)
6362 if (gfc_match_char (',') != MATCH_YES)
6366 if (gfc_current_state () == COMP_ENUM)
6368 gfc_free_enum_history ();
6369 gfc_error ("Syntax error in ENUMERATOR definition at %C");
6374 gfc_free_array_spec (current_as);