1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
29 /* Macros to access allocate memory for gfc_data_variable,
30 gfc_data_value and gfc_data. */
31 #define gfc_get_data_variable() gfc_getmem (sizeof (gfc_data_variable))
32 #define gfc_get_data_value() gfc_getmem (sizeof (gfc_data_value))
33 #define gfc_get_data() gfc_getmem( sizeof (gfc_data))
36 /* This flag is set if an old-style length selector is matched
37 during a type-declaration statement. */
39 static int old_char_selector;
41 /* When variables acquire types and attributes from a declaration
42 statement, they get them from the following static variables. The
43 first part of a declaration sets these variables and the second
44 part copies these into symbol structures. */
46 static gfc_typespec current_ts;
48 static symbol_attribute current_attr;
49 static gfc_array_spec *current_as;
50 static int colon_seen;
52 /* The current binding label (if any). */
53 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
54 /* Need to know how many identifiers are on the current data declaration
55 line in case we're given the BIND(C) attribute with a NAME= specifier. */
56 static int num_idents_on_line;
57 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
58 can supply a name if the curr_binding_label is nil and NAME= was not. */
59 static int has_name_equals = 0;
61 /* Initializer of the previous enumerator. */
63 static gfc_expr *last_initializer;
65 /* History of all the enumerators is maintained, so that
66 kind values of all the enumerators could be updated depending
67 upon the maximum initialized value. */
69 typedef struct enumerator_history
72 gfc_expr *initializer;
73 struct enumerator_history *next;
77 /* Header of enum history chain. */
79 static enumerator_history *enum_history = NULL;
81 /* Pointer of enum history node containing largest initializer. */
83 static enumerator_history *max_enum = NULL;
85 /* gfc_new_block points to the symbol of a newly matched block. */
87 gfc_symbol *gfc_new_block;
89 locus gfc_function_kind_locus;
90 locus gfc_function_type_locus;
93 /********************* DATA statement subroutines *********************/
95 static bool in_match_data = false;
98 gfc_in_match_data (void)
100 return in_match_data;
104 set_in_match_data (bool set_value)
106 in_match_data = set_value;
109 /* Free a gfc_data_variable structure and everything beneath it. */
112 free_variable (gfc_data_variable *p)
114 gfc_data_variable *q;
119 gfc_free_expr (p->expr);
120 gfc_free_iterator (&p->iter, 0);
121 free_variable (p->list);
127 /* Free a gfc_data_value structure and everything beneath it. */
130 free_value (gfc_data_value *p)
137 gfc_free_expr (p->expr);
143 /* Free a list of gfc_data structures. */
146 gfc_free_data (gfc_data *p)
153 free_variable (p->var);
154 free_value (p->value);
160 /* Free all data in a namespace. */
163 gfc_free_data_all (gfc_namespace *ns)
176 static match var_element (gfc_data_variable *);
178 /* Match a list of variables terminated by an iterator and a right
182 var_list (gfc_data_variable *parent)
184 gfc_data_variable *tail, var;
187 m = var_element (&var);
188 if (m == MATCH_ERROR)
193 tail = gfc_get_data_variable ();
200 if (gfc_match_char (',') != MATCH_YES)
203 m = gfc_match_iterator (&parent->iter, 1);
206 if (m == MATCH_ERROR)
209 m = var_element (&var);
210 if (m == MATCH_ERROR)
215 tail->next = gfc_get_data_variable ();
221 if (gfc_match_char (')') != MATCH_YES)
226 gfc_syntax_error (ST_DATA);
231 /* Match a single element in a data variable list, which can be a
232 variable-iterator list. */
235 var_element (gfc_data_variable *new)
240 memset (new, 0, sizeof (gfc_data_variable));
242 if (gfc_match_char ('(') == MATCH_YES)
243 return var_list (new);
245 m = gfc_match_variable (&new->expr, 0);
249 sym = new->expr->symtree->n.sym;
251 if (!sym->attr.function && gfc_current_ns->parent
252 && gfc_current_ns->parent == sym->ns)
254 gfc_error ("Host associated variable '%s' may not be in the DATA "
255 "statement at %C", sym->name);
259 if (gfc_current_state () != COMP_BLOCK_DATA
260 && sym->attr.in_common
261 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
262 "common block variable '%s' in DATA statement at %C",
263 sym->name) == FAILURE)
266 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
273 /* Match the top-level list of data variables. */
276 top_var_list (gfc_data *d)
278 gfc_data_variable var, *tail, *new;
285 m = var_element (&var);
288 if (m == MATCH_ERROR)
291 new = gfc_get_data_variable ();
301 if (gfc_match_char ('/') == MATCH_YES)
303 if (gfc_match_char (',') != MATCH_YES)
310 gfc_syntax_error (ST_DATA);
311 gfc_free_data_all (gfc_current_ns);
317 match_data_constant (gfc_expr **result)
319 char name[GFC_MAX_SYMBOL_LEN + 1];
325 m = gfc_match_literal_constant (&expr, 1);
332 if (m == MATCH_ERROR)
335 m = gfc_match_null (result);
339 old_loc = gfc_current_locus;
341 /* Should this be a structure component, try to match it
342 before matching a name. */
343 m = gfc_match_rvalue (result);
344 if (m == MATCH_ERROR)
347 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
349 if (gfc_simplify_expr (*result, 0) == FAILURE)
354 gfc_current_locus = old_loc;
356 m = gfc_match_name (name);
360 if (gfc_find_symbol (name, NULL, 1, &sym))
364 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
366 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
370 else if (sym->attr.flavor == FL_DERIVED)
371 return gfc_match_structure_constructor (sym, result);
373 *result = gfc_copy_expr (sym->value);
378 /* Match a list of values in a DATA statement. The leading '/' has
379 already been seen at this point. */
382 top_val_list (gfc_data *data)
384 gfc_data_value *new, *tail;
392 m = match_data_constant (&expr);
395 if (m == MATCH_ERROR)
398 new = gfc_get_data_value ();
399 mpz_init (new->repeat);
408 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
411 mpz_set_ui (tail->repeat, 1);
415 if (expr->ts.type == BT_INTEGER)
416 mpz_set (tail->repeat, expr->value.integer);
417 gfc_free_expr (expr);
419 m = match_data_constant (&tail->expr);
422 if (m == MATCH_ERROR)
426 if (gfc_match_char ('/') == MATCH_YES)
428 if (gfc_match_char (',') == MATCH_NO)
435 gfc_syntax_error (ST_DATA);
436 gfc_free_data_all (gfc_current_ns);
441 /* Matches an old style initialization. */
444 match_old_style_init (const char *name)
451 /* Set up data structure to hold initializers. */
452 gfc_find_sym_tree (name, NULL, 0, &st);
455 newdata = gfc_get_data ();
456 newdata->var = gfc_get_data_variable ();
457 newdata->var->expr = gfc_get_variable_expr (st);
458 newdata->where = gfc_current_locus;
460 /* Match initial value list. This also eats the terminal '/'. */
461 m = top_val_list (newdata);
470 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
475 /* Mark the variable as having appeared in a data statement. */
476 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
482 /* Chain in namespace list of DATA initializers. */
483 newdata->next = gfc_current_ns->data;
484 gfc_current_ns->data = newdata;
490 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
491 we are matching a DATA statement and are therefore issuing an error
492 if we encounter something unexpected, if not, we're trying to match
493 an old-style initialization expression of the form INTEGER I /2/. */
496 gfc_match_data (void)
501 set_in_match_data (true);
505 new = gfc_get_data ();
506 new->where = gfc_current_locus;
508 m = top_var_list (new);
512 m = top_val_list (new);
516 new->next = gfc_current_ns->data;
517 gfc_current_ns->data = new;
519 if (gfc_match_eos () == MATCH_YES)
522 gfc_match_char (','); /* Optional comma */
525 set_in_match_data (false);
529 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
536 set_in_match_data (false);
542 /************************ Declaration statements *********************/
544 /* Match an intent specification. Since this can only happen after an
545 INTENT word, a legal intent-spec must follow. */
548 match_intent_spec (void)
551 if (gfc_match (" ( in out )") == MATCH_YES)
553 if (gfc_match (" ( in )") == MATCH_YES)
555 if (gfc_match (" ( out )") == MATCH_YES)
558 gfc_error ("Bad INTENT specification at %C");
559 return INTENT_UNKNOWN;
563 /* Matches a character length specification, which is either a
564 specification expression or a '*'. */
567 char_len_param_value (gfc_expr **expr)
571 if (gfc_match_char ('*') == MATCH_YES)
577 m = gfc_match_expr (expr);
578 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
580 if ((*expr)->value.function.actual
581 && (*expr)->value.function.actual->expr->symtree)
584 e = (*expr)->value.function.actual->expr;
585 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
586 && e->expr_type == EXPR_VARIABLE)
588 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
590 if (e->symtree->n.sym->ts.type == BT_CHARACTER
591 && e->symtree->n.sym->ts.cl
592 && e->symtree->n.sym->ts.cl->length->ts.type == BT_UNKNOWN)
600 gfc_error ("Conflict in attributes of function argument at %C");
605 /* A character length is a '*' followed by a literal integer or a
606 char_len_param_value in parenthesis. */
609 match_char_length (gfc_expr **expr)
614 m = gfc_match_char ('*');
618 m = gfc_match_small_literal_int (&length, NULL);
619 if (m == MATCH_ERROR)
624 *expr = gfc_int_expr (length);
628 if (gfc_match_char ('(') == MATCH_NO)
631 m = char_len_param_value (expr);
632 if (m == MATCH_ERROR)
637 if (gfc_match_char (')') == MATCH_NO)
639 gfc_free_expr (*expr);
647 gfc_error ("Syntax error in character length specification at %C");
652 /* Special subroutine for finding a symbol. Check if the name is found
653 in the current name space. If not, and we're compiling a function or
654 subroutine and the parent compilation unit is an interface, then check
655 to see if the name we've been given is the name of the interface
656 (located in another namespace). */
659 find_special (const char *name, gfc_symbol **result)
664 i = gfc_get_symbol (name, NULL, result);
668 if (gfc_current_state () != COMP_SUBROUTINE
669 && gfc_current_state () != COMP_FUNCTION)
672 s = gfc_state_stack->previous;
676 if (s->state != COMP_INTERFACE)
679 goto end; /* Nameless interface. */
681 if (strcmp (name, s->sym->name) == 0)
692 /* Special subroutine for getting a symbol node associated with a
693 procedure name, used in SUBROUTINE and FUNCTION statements. The
694 symbol is created in the parent using with symtree node in the
695 child unit pointing to the symbol. If the current namespace has no
696 parent, then the symbol is just created in the current unit. */
699 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
705 /* Module functions have to be left in their own namespace because
706 they have potentially (almost certainly!) already been referenced.
707 In this sense, they are rather like external functions. This is
708 fixed up in resolve.c(resolve_entries), where the symbol name-
709 space is set to point to the master function, so that the fake
710 result mechanism can work. */
711 if (module_fcn_entry)
713 /* Present if entry is declared to be a module procedure. */
714 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
717 rc = gfc_get_symbol (name, NULL, result);
718 else if (gfc_get_symbol (name, NULL, &sym) == 0
720 && sym->ts.type != BT_UNKNOWN
721 && (*result)->ts.type == BT_UNKNOWN
722 && sym->attr.flavor == FL_UNKNOWN)
723 /* Pick up the typespec for the entry, if declared in the function
724 body. Note that this symbol is FL_UNKNOWN because it will
725 only have appeared in a type declaration. The local symtree
726 is set to point to the module symbol and a unique symtree
727 to the local version. This latter ensures a correct clearing
730 (*result)->ts = sym->ts;
731 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
733 st = gfc_get_unique_symtree (gfc_current_ns);
738 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
744 gfc_current_ns->refs++;
746 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
748 /* Trap another encompassed procedure with the same name. All
749 these conditions are necessary to avoid picking up an entry
750 whose name clashes with that of the encompassing procedure;
751 this is handled using gsymbols to register unique,globally
753 if (sym->attr.flavor != 0
754 && sym->attr.proc != 0
755 && (sym->attr.subroutine || sym->attr.function)
756 && sym->attr.if_source != IFSRC_UNKNOWN)
757 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
758 name, &sym->declared_at);
760 /* Trap a procedure with a name the same as interface in the
761 encompassing scope. */
762 if (sym->attr.generic != 0
763 && (sym->attr.subroutine || sym->attr.function)
764 && !sym->attr.mod_proc)
765 gfc_error_now ("Name '%s' at %C is already defined"
766 " as a generic interface at %L",
767 name, &sym->declared_at);
769 /* Trap declarations of attributes in encompassing scope. The
770 signature for this is that ts.kind is set. Legitimate
771 references only set ts.type. */
772 if (sym->ts.kind != 0
773 && !sym->attr.implicit_type
774 && sym->attr.proc == 0
775 && gfc_current_ns->parent != NULL
776 && sym->attr.access == 0
777 && !module_fcn_entry)
778 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
779 "and must not have attributes declared at %L",
780 name, &sym->declared_at);
783 if (gfc_current_ns->parent == NULL || *result == NULL)
786 /* Module function entries will already have a symtree in
787 the current namespace but will need one at module level. */
788 if (module_fcn_entry)
790 /* Present if entry is declared to be a module procedure. */
791 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
793 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
796 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
801 /* See if the procedure should be a module procedure. */
803 if (((sym->ns->proc_name != NULL
804 && sym->ns->proc_name->attr.flavor == FL_MODULE
805 && sym->attr.proc != PROC_MODULE)
806 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
807 && gfc_add_procedure (&sym->attr, PROC_MODULE,
808 sym->name, NULL) == FAILURE)
815 /* Verify that the given symbol representing a parameter is C
816 interoperable, by checking to see if it was marked as such after
817 its declaration. If the given symbol is not interoperable, a
818 warning is reported, thus removing the need to return the status to
819 the calling function. The standard does not require the user use
820 one of the iso_c_binding named constants to declare an
821 interoperable parameter, but we can't be sure if the param is C
822 interop or not if the user doesn't. For example, integer(4) may be
823 legal Fortran, but doesn't have meaning in C. It may interop with
824 a number of the C types, which causes a problem because the
825 compiler can't know which one. This code is almost certainly not
826 portable, and the user will get what they deserve if the C type
827 across platforms isn't always interoperable with integer(4). If
828 the user had used something like integer(c_int) or integer(c_long),
829 the compiler could have automatically handled the varying sizes
833 verify_c_interop_param (gfc_symbol *sym)
835 int is_c_interop = 0;
836 try retval = SUCCESS;
838 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
839 Don't repeat the checks here. */
840 if (sym->attr.implicit_type)
843 /* For subroutines or functions that are passed to a BIND(C) procedure,
844 they're interoperable if they're BIND(C) and their params are all
846 if (sym->attr.flavor == FL_PROCEDURE)
848 if (sym->attr.is_bind_c == 0)
850 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
851 "attribute to be C interoperable", sym->name,
852 &(sym->declared_at));
858 if (sym->attr.is_c_interop == 1)
859 /* We've already checked this procedure; don't check it again. */
862 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
867 /* See if we've stored a reference to a procedure that owns sym. */
868 if (sym->ns != NULL && sym->ns->proc_name != NULL)
870 if (sym->ns->proc_name->attr.is_bind_c == 1)
873 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
876 if (is_c_interop != 1)
878 /* Make personalized messages to give better feedback. */
879 if (sym->ts.type == BT_DERIVED)
880 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
881 " procedure '%s' but is not C interoperable "
882 "because derived type '%s' is not C interoperable",
883 sym->name, &(sym->declared_at),
884 sym->ns->proc_name->name,
885 sym->ts.derived->name);
887 gfc_warning ("Variable '%s' at %L is a parameter to the "
888 "BIND(C) procedure '%s' but may not be C "
890 sym->name, &(sym->declared_at),
891 sym->ns->proc_name->name);
894 /* Character strings are only C interoperable if they have a
896 if (sym->ts.type == BT_CHARACTER)
898 gfc_charlen *cl = sym->ts.cl;
899 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
900 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
902 gfc_error ("Character argument '%s' at %L "
903 "must be length 1 because "
904 "procedure '%s' is BIND(C)",
905 sym->name, &sym->declared_at,
906 sym->ns->proc_name->name);
911 /* We have to make sure that any param to a bind(c) routine does
912 not have the allocatable, pointer, or optional attributes,
913 according to J3/04-007, section 5.1. */
914 if (sym->attr.allocatable == 1)
916 gfc_error ("Variable '%s' at %L cannot have the "
917 "ALLOCATABLE attribute because procedure '%s'"
918 " is BIND(C)", sym->name, &(sym->declared_at),
919 sym->ns->proc_name->name);
923 if (sym->attr.pointer == 1)
925 gfc_error ("Variable '%s' at %L cannot have the "
926 "POINTER attribute because procedure '%s'"
927 " is BIND(C)", sym->name, &(sym->declared_at),
928 sym->ns->proc_name->name);
932 if (sym->attr.optional == 1)
934 gfc_error ("Variable '%s' at %L cannot have the "
935 "OPTIONAL attribute because procedure '%s'"
936 " is BIND(C)", sym->name, &(sym->declared_at),
937 sym->ns->proc_name->name);
941 /* Make sure that if it has the dimension attribute, that it is
942 either assumed size or explicit shape. */
945 if (sym->as->type == AS_ASSUMED_SHAPE)
947 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
948 "argument to the procedure '%s' at %L because "
949 "the procedure is BIND(C)", sym->name,
950 &(sym->declared_at), sym->ns->proc_name->name,
951 &(sym->ns->proc_name->declared_at));
955 if (sym->as->type == AS_DEFERRED)
957 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
958 "argument to the procedure '%s' at %L because "
959 "the procedure is BIND(C)", sym->name,
960 &(sym->declared_at), sym->ns->proc_name->name,
961 &(sym->ns->proc_name->declared_at));
972 /* Function called by variable_decl() that adds a name to the symbol table. */
975 build_sym (const char *name, gfc_charlen *cl,
976 gfc_array_spec **as, locus *var_locus)
978 symbol_attribute attr;
981 if (gfc_get_symbol (name, NULL, &sym))
984 /* Start updating the symbol table. Add basic type attribute if present. */
985 if (current_ts.type != BT_UNKNOWN
986 && (sym->attr.implicit_type == 0
987 || !gfc_compare_types (&sym->ts, ¤t_ts))
988 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
991 if (sym->ts.type == BT_CHARACTER)
994 /* Add dimension attribute if present. */
995 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
999 /* Add attribute to symbol. The copy is so that we can reset the
1000 dimension attribute. */
1001 attr = current_attr;
1004 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1007 /* Finish any work that may need to be done for the binding label,
1008 if it's a bind(c). The bind(c) attr is found before the symbol
1009 is made, and before the symbol name (for data decls), so the
1010 current_ts is holding the binding label, or nothing if the
1011 name= attr wasn't given. Therefore, test here if we're dealing
1012 with a bind(c) and make sure the binding label is set correctly. */
1013 if (sym->attr.is_bind_c == 1)
1015 if (sym->binding_label[0] == '\0')
1017 /* Set the binding label and verify that if a NAME= was specified
1018 then only one identifier was in the entity-decl-list. */
1019 if (set_binding_label (sym->binding_label, sym->name,
1020 num_idents_on_line) == FAILURE)
1025 /* See if we know we're in a common block, and if it's a bind(c)
1026 common then we need to make sure we're an interoperable type. */
1027 if (sym->attr.in_common == 1)
1029 /* Test the common block object. */
1030 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1031 && sym->ts.is_c_interop != 1)
1033 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1034 "must be declared with a C interoperable "
1035 "kind since common block '%s' is BIND(C)",
1036 sym->name, sym->common_block->name,
1037 sym->common_block->name);
1042 sym->attr.implied_index = 0;
1048 /* Set character constant to the given length. The constant will be padded or
1052 gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
1057 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1058 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
1060 slen = expr->value.character.length;
1063 s = gfc_getmem (len + 1);
1064 memcpy (s, expr->value.character.string, MIN (len, slen));
1066 memset (&s[slen], ' ', len - slen);
1068 if (gfc_option.warn_character_truncation && slen > len)
1069 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1070 "(%d/%d)", &expr->where, slen, len);
1072 /* Apply the standard by 'hand' otherwise it gets cleared for
1074 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
1075 gfc_error_now ("The CHARACTER elements of the array constructor "
1076 "at %L must have the same length (%d/%d)",
1077 &expr->where, slen, len);
1080 gfc_free (expr->value.character.string);
1081 expr->value.character.string = s;
1082 expr->value.character.length = len;
1087 /* Function to create and update the enumerator history
1088 using the information passed as arguments.
1089 Pointer "max_enum" is also updated, to point to
1090 enum history node containing largest initializer.
1092 SYM points to the symbol node of enumerator.
1093 INIT points to its enumerator value. */
1096 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1098 enumerator_history *new_enum_history;
1099 gcc_assert (sym != NULL && init != NULL);
1101 new_enum_history = gfc_getmem (sizeof (enumerator_history));
1103 new_enum_history->sym = sym;
1104 new_enum_history->initializer = init;
1105 new_enum_history->next = NULL;
1107 if (enum_history == NULL)
1109 enum_history = new_enum_history;
1110 max_enum = enum_history;
1114 new_enum_history->next = enum_history;
1115 enum_history = new_enum_history;
1117 if (mpz_cmp (max_enum->initializer->value.integer,
1118 new_enum_history->initializer->value.integer) < 0)
1119 max_enum = new_enum_history;
1124 /* Function to free enum kind history. */
1127 gfc_free_enum_history (void)
1129 enumerator_history *current = enum_history;
1130 enumerator_history *next;
1132 while (current != NULL)
1134 next = current->next;
1139 enum_history = NULL;
1143 /* Function called by variable_decl() that adds an initialization
1144 expression to a symbol. */
1147 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1149 symbol_attribute attr;
1154 if (find_special (name, &sym))
1159 /* If this symbol is confirming an implicit parameter type,
1160 then an initialization expression is not allowed. */
1161 if (attr.flavor == FL_PARAMETER
1162 && sym->value != NULL
1165 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1174 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
1181 /* An initializer is required for PARAMETER declarations. */
1182 if (attr.flavor == FL_PARAMETER)
1184 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1190 /* If a variable appears in a DATA block, it cannot have an
1194 gfc_error ("Variable '%s' at %C with an initializer already "
1195 "appears in a DATA statement", sym->name);
1199 /* Check if the assignment can happen. This has to be put off
1200 until later for a derived type variable. */
1201 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1202 && gfc_check_assign_symbol (sym, init) == FAILURE)
1205 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1207 /* Update symbol character length according initializer. */
1208 if (sym->ts.cl->length == NULL)
1211 /* If there are multiple CHARACTER variables declared on the
1212 same line, we don't want them to share the same length. */
1213 sym->ts.cl = gfc_get_charlen ();
1214 sym->ts.cl->next = gfc_current_ns->cl_list;
1215 gfc_current_ns->cl_list = sym->ts.cl;
1217 if (sym->attr.flavor == FL_PARAMETER)
1219 if (init->expr_type == EXPR_CONSTANT)
1221 clen = init->value.character.length;
1222 sym->ts.cl->length = gfc_int_expr (clen);
1224 else if (init->expr_type == EXPR_ARRAY)
1226 gfc_expr *p = init->value.constructor->expr;
1227 clen = p->value.character.length;
1228 sym->ts.cl->length = gfc_int_expr (clen);
1230 else if (init->ts.cl && init->ts.cl->length)
1231 sym->ts.cl->length =
1232 gfc_copy_expr (sym->value->ts.cl->length);
1235 /* Update initializer character length according symbol. */
1236 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1238 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1239 gfc_constructor * p;
1241 if (init->expr_type == EXPR_CONSTANT)
1242 gfc_set_constant_character_len (len, init, false);
1243 else if (init->expr_type == EXPR_ARRAY)
1245 /* Build a new charlen to prevent simplification from
1246 deleting the length before it is resolved. */
1247 init->ts.cl = gfc_get_charlen ();
1248 init->ts.cl->next = gfc_current_ns->cl_list;
1249 gfc_current_ns->cl_list = sym->ts.cl;
1250 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
1252 for (p = init->value.constructor; p; p = p->next)
1253 gfc_set_constant_character_len (len, p->expr, false);
1258 /* Need to check if the expression we initialized this
1259 to was one of the iso_c_binding named constants. If so,
1260 and we're a parameter (constant), let it be iso_c.
1262 integer(c_int), parameter :: my_int = c_int
1263 integer(my_int) :: my_int_2
1264 If we mark my_int as iso_c (since we can see it's value
1265 is equal to one of the named constants), then my_int_2
1266 will be considered C interoperable. */
1267 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1269 sym->ts.is_iso_c |= init->ts.is_iso_c;
1270 sym->ts.is_c_interop |= init->ts.is_c_interop;
1271 /* attr bits needed for module files. */
1272 sym->attr.is_iso_c |= init->ts.is_iso_c;
1273 sym->attr.is_c_interop |= init->ts.is_c_interop;
1274 if (init->ts.is_iso_c)
1275 sym->ts.f90_type = init->ts.f90_type;
1278 /* Add initializer. Make sure we keep the ranks sane. */
1279 if (sym->attr.dimension && init->rank == 0)
1285 if (sym->attr.flavor == FL_PARAMETER
1286 && init->expr_type == EXPR_CONSTANT
1287 && spec_size (sym->as, &size) == SUCCESS
1288 && mpz_cmp_si (size, 0) > 0)
1290 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1293 array->value.constructor = c = NULL;
1294 for (n = 0; n < (int)mpz_get_si (size); n++)
1296 if (array->value.constructor == NULL)
1298 array->value.constructor = c = gfc_get_constructor ();
1303 c->next = gfc_get_constructor ();
1305 c->expr = gfc_copy_expr (init);
1309 array->shape = gfc_get_shape (sym->as->rank);
1310 for (n = 0; n < sym->as->rank; n++)
1311 spec_dimen_size (sym->as, n, &array->shape[n]);
1316 init->rank = sym->as->rank;
1320 if (sym->attr.save == SAVE_NONE)
1321 sym->attr.save = SAVE_IMPLICIT;
1329 /* Function called by variable_decl() that adds a name to a structure
1333 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1334 gfc_array_spec **as)
1338 /* If the current symbol is of the same derived type that we're
1339 constructing, it must have the pointer attribute. */
1340 if (current_ts.type == BT_DERIVED
1341 && current_ts.derived == gfc_current_block ()
1342 && current_attr.pointer == 0)
1344 gfc_error ("Component at %C must have the POINTER attribute");
1348 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1350 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1352 gfc_error ("Array component of structure at %C must have explicit "
1353 "or deferred shape");
1358 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1363 gfc_set_component_attr (c, ¤t_attr);
1365 c->initializer = *init;
1373 /* Check array components. */
1378 gfc_error ("Allocatable component at %C must be an array");
1387 if (c->as->type != AS_DEFERRED)
1389 gfc_error ("Pointer array component of structure at %C must have a "
1394 else if (c->allocatable)
1396 if (c->as->type != AS_DEFERRED)
1398 gfc_error ("Allocatable component of structure at %C must have a "
1405 if (c->as->type != AS_EXPLICIT)
1407 gfc_error ("Array component of structure at %C must have an "
1417 /* Match a 'NULL()', and possibly take care of some side effects. */
1420 gfc_match_null (gfc_expr **result)
1426 m = gfc_match (" null ( )");
1430 /* The NULL symbol now has to be/become an intrinsic function. */
1431 if (gfc_get_symbol ("null", NULL, &sym))
1433 gfc_error ("NULL() initialization at %C is ambiguous");
1437 gfc_intrinsic_symbol (sym);
1439 if (sym->attr.proc != PROC_INTRINSIC
1440 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1441 sym->name, NULL) == FAILURE
1442 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1445 e = gfc_get_expr ();
1446 e->where = gfc_current_locus;
1447 e->expr_type = EXPR_NULL;
1448 e->ts.type = BT_UNKNOWN;
1456 /* Match a variable name with an optional initializer. When this
1457 subroutine is called, a variable is expected to be parsed next.
1458 Depending on what is happening at the moment, updates either the
1459 symbol table or the current interface. */
1462 variable_decl (int elem)
1464 char name[GFC_MAX_SYMBOL_LEN + 1];
1465 gfc_expr *initializer, *char_len;
1467 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1478 old_locus = gfc_current_locus;
1480 /* When we get here, we've just matched a list of attributes and
1481 maybe a type and a double colon. The next thing we expect to see
1482 is the name of the symbol. */
1483 m = gfc_match_name (name);
1487 var_locus = gfc_current_locus;
1489 /* Now we could see the optional array spec. or character length. */
1490 m = gfc_match_array_spec (&as);
1491 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1492 cp_as = gfc_copy_array_spec (as);
1493 else if (m == MATCH_ERROR)
1497 as = gfc_copy_array_spec (current_as);
1502 if (current_ts.type == BT_CHARACTER)
1504 switch (match_char_length (&char_len))
1507 cl = gfc_get_charlen ();
1508 cl->next = gfc_current_ns->cl_list;
1509 gfc_current_ns->cl_list = cl;
1511 cl->length = char_len;
1514 /* Non-constant lengths need to be copied after the first
1515 element. Also copy assumed lengths. */
1518 && (current_ts.cl->length == NULL
1519 || current_ts.cl->length->expr_type != EXPR_CONSTANT))
1521 cl = gfc_get_charlen ();
1522 cl->next = gfc_current_ns->cl_list;
1523 gfc_current_ns->cl_list = cl;
1524 cl->length = gfc_copy_expr (current_ts.cl->length);
1536 /* If this symbol has already shown up in a Cray Pointer declaration,
1537 then we want to set the type & bail out. */
1538 if (gfc_option.flag_cray_pointer)
1540 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1541 if (sym != NULL && sym->attr.cray_pointee)
1543 sym->ts.type = current_ts.type;
1544 sym->ts.kind = current_ts.kind;
1546 sym->ts.derived = current_ts.derived;
1547 sym->ts.is_c_interop = current_ts.is_c_interop;
1548 sym->ts.is_iso_c = current_ts.is_iso_c;
1551 /* Check to see if we have an array specification. */
1554 if (sym->as != NULL)
1556 gfc_error ("Duplicate array spec for Cray pointee at %C");
1557 gfc_free_array_spec (cp_as);
1563 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1564 gfc_internal_error ("Couldn't set pointee array spec.");
1566 /* Fix the array spec. */
1567 m = gfc_mod_pointee_as (sym->as);
1568 if (m == MATCH_ERROR)
1576 gfc_free_array_spec (cp_as);
1581 /* OK, we've successfully matched the declaration. Now put the
1582 symbol in the current namespace, because it might be used in the
1583 optional initialization expression for this symbol, e.g. this is
1586 integer, parameter :: i = huge(i)
1588 This is only true for parameters or variables of a basic type.
1589 For components of derived types, it is not true, so we don't
1590 create a symbol for those yet. If we fail to create the symbol,
1592 if (gfc_current_state () != COMP_DERIVED
1593 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1599 /* An interface body specifies all of the procedure's
1600 characteristics and these shall be consistent with those
1601 specified in the procedure definition, except that the interface
1602 may specify a procedure that is not pure if the procedure is
1603 defined to be pure(12.3.2). */
1604 if (current_ts.type == BT_DERIVED
1605 && gfc_current_ns->proc_name
1606 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1607 && current_ts.derived->ns != gfc_current_ns)
1610 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.derived->name);
1611 if (!(current_ts.derived->attr.imported
1613 && st->n.sym == current_ts.derived)
1614 && !gfc_current_ns->has_import_set)
1616 gfc_error ("the type of '%s' at %C has not been declared within the "
1623 /* In functions that have a RESULT variable defined, the function
1624 name always refers to function calls. Therefore, the name is
1625 not allowed to appear in specification statements. */
1626 if (gfc_current_state () == COMP_FUNCTION
1627 && gfc_current_block () != NULL
1628 && gfc_current_block ()->result != NULL
1629 && gfc_current_block ()->result != gfc_current_block ()
1630 && strcmp (gfc_current_block ()->name, name) == 0)
1632 gfc_error ("Function name '%s' not allowed at %C", name);
1637 /* We allow old-style initializations of the form
1638 integer i /2/, j(4) /3*3, 1/
1639 (if no colon has been seen). These are different from data
1640 statements in that initializers are only allowed to apply to the
1641 variable immediately preceding, i.e.
1643 is not allowed. Therefore we have to do some work manually, that
1644 could otherwise be left to the matchers for DATA statements. */
1646 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1648 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1649 "initialization at %C") == FAILURE)
1652 return match_old_style_init (name);
1655 /* The double colon must be present in order to have initializers.
1656 Otherwise the statement is ambiguous with an assignment statement. */
1659 if (gfc_match (" =>") == MATCH_YES)
1661 if (!current_attr.pointer)
1663 gfc_error ("Initialization at %C isn't for a pointer variable");
1668 m = gfc_match_null (&initializer);
1671 gfc_error ("Pointer initialization requires a NULL() at %C");
1675 if (gfc_pure (NULL))
1677 gfc_error ("Initialization of pointer at %C is not allowed in "
1678 "a PURE procedure");
1686 else if (gfc_match_char ('=') == MATCH_YES)
1688 if (current_attr.pointer)
1690 gfc_error ("Pointer initialization at %C requires '=>', "
1696 m = gfc_match_init_expr (&initializer);
1699 gfc_error ("Expected an initialization expression at %C");
1703 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1705 gfc_error ("Initialization of variable at %C is not allowed in "
1706 "a PURE procedure");
1715 if (initializer != NULL && current_attr.allocatable
1716 && gfc_current_state () == COMP_DERIVED)
1718 gfc_error ("Initialization of allocatable component at %C is not "
1724 /* Add the initializer. Note that it is fine if initializer is
1725 NULL here, because we sometimes also need to check if a
1726 declaration *must* have an initialization expression. */
1727 if (gfc_current_state () != COMP_DERIVED)
1728 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1731 if (current_ts.type == BT_DERIVED
1732 && !current_attr.pointer && !initializer)
1733 initializer = gfc_default_initializer (¤t_ts);
1734 t = build_struct (name, cl, &initializer, &as);
1737 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1740 /* Free stuff up and return. */
1741 gfc_free_expr (initializer);
1742 gfc_free_array_spec (as);
1748 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1749 This assumes that the byte size is equal to the kind number for
1750 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1753 gfc_match_old_kind_spec (gfc_typespec *ts)
1758 if (gfc_match_char ('*') != MATCH_YES)
1761 m = gfc_match_small_literal_int (&ts->kind, NULL);
1765 original_kind = ts->kind;
1767 /* Massage the kind numbers for complex types. */
1768 if (ts->type == BT_COMPLEX)
1772 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1773 gfc_basic_typename (ts->type), original_kind);
1779 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1781 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1782 gfc_basic_typename (ts->type), original_kind);
1786 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1787 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1794 /* Match a kind specification. Since kinds are generally optional, we
1795 usually return MATCH_NO if something goes wrong. If a "kind="
1796 string is found, then we know we have an error. */
1799 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
1810 where = loc = gfc_current_locus;
1815 if (gfc_match_char ('(') == MATCH_NO)
1818 /* Also gobbles optional text. */
1819 if (gfc_match (" kind = ") == MATCH_YES)
1822 loc = gfc_current_locus;
1825 n = gfc_match_init_expr (&e);
1829 if (gfc_current_state () == COMP_INTERFACE
1830 || gfc_current_state () == COMP_NONE
1831 || gfc_current_state () == COMP_CONTAINS)
1833 /* Signal using kind = -1 that the expression might include
1834 use associated or imported parameters and try again after
1835 the specification expressions..... */
1836 if (gfc_match_char (')') != MATCH_YES)
1838 gfc_error ("Missing right parenthesis at %C");
1845 gfc_function_kind_locus = loc;
1846 gfc_undo_symbols ();
1851 /* ....or else, the match is real. */
1853 gfc_error ("Expected initialization expression at %C");
1861 gfc_error ("Expected scalar initialization expression at %C");
1866 msg = gfc_extract_int (e, &ts->kind);
1874 /* Before throwing away the expression, let's see if we had a
1875 C interoperable kind (and store the fact). */
1876 if (e->ts.is_c_interop == 1)
1878 /* Mark this as c interoperable if being declared with one
1879 of the named constants from iso_c_binding. */
1880 ts->is_c_interop = e->ts.is_iso_c;
1881 ts->f90_type = e->ts.f90_type;
1887 /* Ignore errors to this point, if we've gotten here. This means
1888 we ignore the m=MATCH_ERROR from above. */
1889 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1891 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1892 gfc_basic_typename (ts->type));
1895 else if (gfc_match_char (')') != MATCH_YES)
1897 gfc_error ("Missing right parenthesis at %C");
1901 /* All tests passed. */
1904 if(m == MATCH_ERROR)
1905 gfc_current_locus = where;
1907 /* Return what we know from the test(s). */
1912 gfc_current_locus = where;
1918 match_char_kind (int * kind, int * is_iso_c)
1927 where = gfc_current_locus;
1929 n = gfc_match_init_expr (&e);
1931 gfc_error ("Expected initialization expression at %C");
1937 gfc_error ("Expected scalar initialization expression at %C");
1942 msg = gfc_extract_int (e, kind);
1943 *is_iso_c = e->ts.is_iso_c;
1953 /* Ignore errors to this point, if we've gotten here. This means
1954 we ignore the m=MATCH_ERROR from above. */
1955 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
1957 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
1961 /* All tests passed. */
1964 if (m == MATCH_ERROR)
1965 gfc_current_locus = where;
1967 /* Return what we know from the test(s). */
1972 gfc_current_locus = where;
1976 /* Match the various kind/length specifications in a CHARACTER
1977 declaration. We don't return MATCH_NO. */
1980 match_char_spec (gfc_typespec *ts)
1982 int kind, seen_length, is_iso_c;
1992 /* Try the old-style specification first. */
1993 old_char_selector = 0;
1995 m = match_char_length (&len);
1999 old_char_selector = 1;
2004 m = gfc_match_char ('(');
2007 m = MATCH_YES; /* Character without length is a single char. */
2011 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2012 if (gfc_match (" kind =") == MATCH_YES)
2014 m = match_char_kind (&kind, &is_iso_c);
2016 if (m == MATCH_ERROR)
2021 if (gfc_match (" , len =") == MATCH_NO)
2024 m = char_len_param_value (&len);
2027 if (m == MATCH_ERROR)
2034 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2035 if (gfc_match (" len =") == MATCH_YES)
2037 m = char_len_param_value (&len);
2040 if (m == MATCH_ERROR)
2044 if (gfc_match_char (')') == MATCH_YES)
2047 if (gfc_match (" , kind =") != MATCH_YES)
2050 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2056 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2057 m = char_len_param_value (&len);
2060 if (m == MATCH_ERROR)
2064 m = gfc_match_char (')');
2068 if (gfc_match_char (',') != MATCH_YES)
2071 gfc_match (" kind ="); /* Gobble optional text. */
2073 m = match_char_kind (&kind, &is_iso_c);
2074 if (m == MATCH_ERROR)
2080 /* Require a right-paren at this point. */
2081 m = gfc_match_char (')');
2086 gfc_error ("Syntax error in CHARACTER declaration at %C");
2088 gfc_free_expr (len);
2094 gfc_free_expr (len);
2098 /* Do some final massaging of the length values. */
2099 cl = gfc_get_charlen ();
2100 cl->next = gfc_current_ns->cl_list;
2101 gfc_current_ns->cl_list = cl;
2103 if (seen_length == 0)
2104 cl->length = gfc_int_expr (1);
2109 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2111 /* We have to know if it was a c interoperable kind so we can
2112 do accurate type checking of bind(c) procs, etc. */
2114 /* Mark this as c interoperable if being declared with one
2115 of the named constants from iso_c_binding. */
2116 ts->is_c_interop = is_iso_c;
2117 else if (len != NULL)
2118 /* Here, we might have parsed something such as: character(c_char)
2119 In this case, the parsing code above grabs the c_char when
2120 looking for the length (line 1690, roughly). it's the last
2121 testcase for parsing the kind params of a character variable.
2122 However, it's not actually the length. this seems like it
2124 To see if the user used a C interop kind, test the expr
2125 of the so called length, and see if it's C interoperable. */
2126 ts->is_c_interop = len->ts.is_iso_c;
2132 /* Matches a type specification. If successful, sets the ts structure
2133 to the matched specification. This is necessary for FUNCTION and
2134 IMPLICIT statements.
2136 If implicit_flag is nonzero, then we don't check for the optional
2137 kind specification. Not doing so is needed for matching an IMPLICIT
2138 statement correctly. */
2141 gfc_match_type_spec (gfc_typespec *ts, int implicit_flag)
2143 char name[GFC_MAX_SYMBOL_LEN + 1];
2147 locus loc = gfc_current_locus;
2151 /* Clear the current binding label, in case one is given. */
2152 curr_binding_label[0] = '\0';
2154 if (gfc_match (" byte") == MATCH_YES)
2156 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
2160 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2162 gfc_error ("BYTE type used at %C "
2163 "is not available on the target machine");
2167 ts->type = BT_INTEGER;
2172 if (gfc_match (" integer") == MATCH_YES)
2174 ts->type = BT_INTEGER;
2175 ts->kind = gfc_default_integer_kind;
2179 if (gfc_match (" character") == MATCH_YES)
2181 ts->type = BT_CHARACTER;
2182 if (implicit_flag == 0)
2183 return match_char_spec (ts);
2188 if (gfc_match (" real") == MATCH_YES)
2191 ts->kind = gfc_default_real_kind;
2195 if (gfc_match (" double precision") == MATCH_YES)
2198 ts->kind = gfc_default_double_kind;
2202 if (gfc_match (" complex") == MATCH_YES)
2204 ts->type = BT_COMPLEX;
2205 ts->kind = gfc_default_complex_kind;
2209 if (gfc_match (" double complex") == MATCH_YES)
2211 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2212 "conform to the Fortran 95 standard") == FAILURE)
2215 ts->type = BT_COMPLEX;
2216 ts->kind = gfc_default_double_kind;
2220 if (gfc_match (" logical") == MATCH_YES)
2222 ts->type = BT_LOGICAL;
2223 ts->kind = gfc_default_logical_kind;
2227 m = gfc_match (" type ( %n )", name);
2231 if (gfc_current_state () == COMP_INTERFACE
2232 || gfc_current_state () == COMP_NONE)
2234 gfc_function_type_locus = loc;
2235 ts->type = BT_UNKNOWN;
2240 /* Search for the name but allow the components to be defined later. If
2241 type = -1, this typespec has been seen in a function declaration but
2242 the type could not legally be accessed at that point. */
2243 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2245 gfc_error ("Type name '%s' at %C is ambiguous", name);
2248 else if (ts->kind == -1)
2250 if (gfc_find_symbol (name, NULL, 0, &sym))
2252 gfc_error ("Type name '%s' at %C is ambiguous", name);
2260 if (sym->attr.flavor != FL_DERIVED
2261 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2264 ts->type = BT_DERIVED;
2271 /* For all types except double, derived and character, look for an
2272 optional kind specifier. MATCH_NO is actually OK at this point. */
2273 if (implicit_flag == 1)
2276 if (gfc_current_form == FORM_FREE)
2278 c = gfc_peek_char();
2279 if (!gfc_is_whitespace(c) && c != '*' && c != '('
2280 && c != ':' && c != ',')
2284 m = gfc_match_kind_spec (ts, false);
2285 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2286 m = gfc_match_old_kind_spec (ts);
2289 m = MATCH_YES; /* No kind specifier found. */
2295 /* Match an IMPLICIT NONE statement. Actually, this statement is
2296 already matched in parse.c, or we would not end up here in the
2297 first place. So the only thing we need to check, is if there is
2298 trailing garbage. If not, the match is successful. */
2301 gfc_match_implicit_none (void)
2303 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2307 /* Match the letter range(s) of an IMPLICIT statement. */
2310 match_implicit_range (void)
2312 int c, c1, c2, inner;
2315 cur_loc = gfc_current_locus;
2317 gfc_gobble_whitespace ();
2318 c = gfc_next_char ();
2321 gfc_error ("Missing character range in IMPLICIT at %C");
2328 gfc_gobble_whitespace ();
2329 c1 = gfc_next_char ();
2333 gfc_gobble_whitespace ();
2334 c = gfc_next_char ();
2339 inner = 0; /* Fall through. */
2346 gfc_gobble_whitespace ();
2347 c2 = gfc_next_char ();
2351 gfc_gobble_whitespace ();
2352 c = gfc_next_char ();
2354 if ((c != ',') && (c != ')'))
2367 gfc_error ("Letters must be in alphabetic order in "
2368 "IMPLICIT statement at %C");
2372 /* See if we can add the newly matched range to the pending
2373 implicits from this IMPLICIT statement. We do not check for
2374 conflicts with whatever earlier IMPLICIT statements may have
2375 set. This is done when we've successfully finished matching
2377 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2384 gfc_syntax_error (ST_IMPLICIT);
2386 gfc_current_locus = cur_loc;
2391 /* Match an IMPLICIT statement, storing the types for
2392 gfc_set_implicit() if the statement is accepted by the parser.
2393 There is a strange looking, but legal syntactic construction
2394 possible. It looks like:
2396 IMPLICIT INTEGER (a-b) (c-d)
2398 This is legal if "a-b" is a constant expression that happens to
2399 equal one of the legal kinds for integers. The real problem
2400 happens with an implicit specification that looks like:
2402 IMPLICIT INTEGER (a-b)
2404 In this case, a typespec matcher that is "greedy" (as most of the
2405 matchers are) gobbles the character range as a kindspec, leaving
2406 nothing left. We therefore have to go a bit more slowly in the
2407 matching process by inhibiting the kindspec checking during
2408 typespec matching and checking for a kind later. */
2411 gfc_match_implicit (void)
2418 /* We don't allow empty implicit statements. */
2419 if (gfc_match_eos () == MATCH_YES)
2421 gfc_error ("Empty IMPLICIT statement at %C");
2427 /* First cleanup. */
2428 gfc_clear_new_implicit ();
2430 /* A basic type is mandatory here. */
2431 m = gfc_match_type_spec (&ts, 1);
2432 if (m == MATCH_ERROR)
2437 cur_loc = gfc_current_locus;
2438 m = match_implicit_range ();
2442 /* We may have <TYPE> (<RANGE>). */
2443 gfc_gobble_whitespace ();
2444 c = gfc_next_char ();
2445 if ((c == '\n') || (c == ','))
2447 /* Check for CHARACTER with no length parameter. */
2448 if (ts.type == BT_CHARACTER && !ts.cl)
2450 ts.kind = gfc_default_character_kind;
2451 ts.cl = gfc_get_charlen ();
2452 ts.cl->next = gfc_current_ns->cl_list;
2453 gfc_current_ns->cl_list = ts.cl;
2454 ts.cl->length = gfc_int_expr (1);
2457 /* Record the Successful match. */
2458 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2463 gfc_current_locus = cur_loc;
2466 /* Discard the (incorrectly) matched range. */
2467 gfc_clear_new_implicit ();
2469 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2470 if (ts.type == BT_CHARACTER)
2471 m = match_char_spec (&ts);
2474 m = gfc_match_kind_spec (&ts, false);
2477 m = gfc_match_old_kind_spec (&ts);
2478 if (m == MATCH_ERROR)
2484 if (m == MATCH_ERROR)
2487 m = match_implicit_range ();
2488 if (m == MATCH_ERROR)
2493 gfc_gobble_whitespace ();
2494 c = gfc_next_char ();
2495 if ((c != '\n') && (c != ','))
2498 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2506 gfc_syntax_error (ST_IMPLICIT);
2514 gfc_match_import (void)
2516 char name[GFC_MAX_SYMBOL_LEN + 1];
2521 if (gfc_current_ns->proc_name == NULL
2522 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2524 gfc_error ("IMPORT statement at %C only permitted in "
2525 "an INTERFACE body");
2529 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2533 if (gfc_match_eos () == MATCH_YES)
2535 /* All host variables should be imported. */
2536 gfc_current_ns->has_import_set = 1;
2540 if (gfc_match (" ::") == MATCH_YES)
2542 if (gfc_match_eos () == MATCH_YES)
2544 gfc_error ("Expecting list of named entities at %C");
2551 m = gfc_match (" %n", name);
2555 if (gfc_current_ns->parent != NULL
2556 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2558 gfc_error ("Type name '%s' at %C is ambiguous", name);
2561 else if (gfc_current_ns->proc_name->ns->parent != NULL
2562 && gfc_find_symbol (name,
2563 gfc_current_ns->proc_name->ns->parent,
2566 gfc_error ("Type name '%s' at %C is ambiguous", name);
2572 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2573 "at %C - does not exist.", name);
2577 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2579 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2584 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2587 sym->attr.imported = 1;
2599 if (gfc_match_eos () == MATCH_YES)
2601 if (gfc_match_char (',') != MATCH_YES)
2608 gfc_error ("Syntax error in IMPORT statement at %C");
2613 /* A minimal implementation of gfc_match without whitespace, escape
2614 characters or variable arguments. Returns true if the next
2615 characters match the TARGET template exactly. */
2618 match_string_p (const char *target)
2622 for (p = target; *p; p++)
2623 if (gfc_next_char () != *p)
2628 /* Matches an attribute specification including array specs. If
2629 successful, leaves the variables current_attr and current_as
2630 holding the specification. Also sets the colon_seen variable for
2631 later use by matchers associated with initializations.
2633 This subroutine is a little tricky in the sense that we don't know
2634 if we really have an attr-spec until we hit the double colon.
2635 Until that time, we can only return MATCH_NO. This forces us to
2636 check for duplicate specification at this level. */
2639 match_attr_spec (void)
2641 /* Modifiers that can exist in a type statement. */
2643 { GFC_DECL_BEGIN = 0,
2644 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2645 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2646 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2647 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2648 DECL_IS_BIND_C, DECL_NONE,
2649 GFC_DECL_END /* Sentinel */
2653 /* GFC_DECL_END is the sentinel, index starts at 0. */
2654 #define NUM_DECL GFC_DECL_END
2656 locus start, seen_at[NUM_DECL];
2663 gfc_clear_attr (¤t_attr);
2664 start = gfc_current_locus;
2669 /* See if we get all of the keywords up to the final double colon. */
2670 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2678 gfc_gobble_whitespace ();
2680 ch = gfc_next_char ();
2683 /* This is the successful exit condition for the loop. */
2684 if (gfc_next_char () == ':')
2689 gfc_gobble_whitespace ();
2690 switch (gfc_peek_char ())
2693 if (match_string_p ("allocatable"))
2694 d = DECL_ALLOCATABLE;
2698 /* Try and match the bind(c). */
2699 m = gfc_match_bind_c (NULL);
2702 else if (m == MATCH_ERROR)
2707 if (match_string_p ("dimension"))
2712 if (match_string_p ("external"))
2717 if (match_string_p ("int"))
2719 ch = gfc_next_char ();
2722 if (match_string_p ("nt"))
2724 /* Matched "intent". */
2725 /* TODO: Call match_intent_spec from here. */
2726 if (gfc_match (" ( in out )") == MATCH_YES)
2728 else if (gfc_match (" ( in )") == MATCH_YES)
2730 else if (gfc_match (" ( out )") == MATCH_YES)
2736 if (match_string_p ("insic"))
2738 /* Matched "intrinsic". */
2746 if (match_string_p ("optional"))
2752 switch (gfc_next_char ())
2755 if (match_string_p ("rameter"))
2757 /* Matched "parameter". */
2763 if (match_string_p ("inter"))
2765 /* Matched "pointer". */
2771 ch = gfc_next_char ();
2774 if (match_string_p ("vate"))
2776 /* Matched "private". */
2782 if (match_string_p ("tected"))
2784 /* Matched "protected". */
2791 if (match_string_p ("blic"))
2793 /* Matched "public". */
2801 if (match_string_p ("save"))
2806 if (match_string_p ("target"))
2812 ch = gfc_next_char ();
2815 if (match_string_p ("lue"))
2817 /* Matched "value". */
2823 if (match_string_p ("latile"))
2825 /* Matched "volatile". */
2833 /* No double colon and no recognizable decl_type, so assume that
2834 we've been looking at something else the whole time. */
2842 seen_at[d] = gfc_current_locus;
2844 if (d == DECL_DIMENSION)
2846 m = gfc_match_array_spec (¤t_as);
2850 gfc_error ("Missing dimension specification at %C");
2854 if (m == MATCH_ERROR)
2859 /* Since we've seen a double colon, we have to be looking at an
2860 attr-spec. This means that we can now issue errors. */
2861 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2866 case DECL_ALLOCATABLE:
2867 attr = "ALLOCATABLE";
2869 case DECL_DIMENSION:
2876 attr = "INTENT (IN)";
2879 attr = "INTENT (OUT)";
2882 attr = "INTENT (IN OUT)";
2884 case DECL_INTRINSIC:
2890 case DECL_PARAMETER:
2896 case DECL_PROTECTED:
2911 case DECL_IS_BIND_C:
2921 attr = NULL; /* This shouldn't happen. */
2924 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2929 /* Now that we've dealt with duplicate attributes, add the attributes
2930 to the current attribute. */
2931 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2936 if (gfc_current_state () == COMP_DERIVED
2937 && d != DECL_DIMENSION && d != DECL_POINTER
2938 && d != DECL_PRIVATE && d != DECL_PUBLIC
2941 if (d == DECL_ALLOCATABLE)
2943 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
2944 "attribute at %C in a TYPE definition")
2953 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2960 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2961 && gfc_current_state () != COMP_MODULE)
2963 if (d == DECL_PRIVATE)
2967 if (gfc_current_state () == COMP_DERIVED
2968 && gfc_state_stack->previous
2969 && gfc_state_stack->previous->state == COMP_MODULE)
2971 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
2972 "at %L in a TYPE definition", attr,
2982 gfc_error ("%s attribute at %L is not allowed outside of the "
2983 "specification part of a module", attr, &seen_at[d]);
2991 case DECL_ALLOCATABLE:
2992 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
2995 case DECL_DIMENSION:
2996 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
3000 t = gfc_add_external (¤t_attr, &seen_at[d]);
3004 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
3008 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
3012 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
3015 case DECL_INTRINSIC:
3016 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
3020 t = gfc_add_optional (¤t_attr, &seen_at[d]);
3023 case DECL_PARAMETER:
3024 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
3028 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
3031 case DECL_PROTECTED:
3032 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3034 gfc_error ("PROTECTED at %C only allowed in specification "
3035 "part of a module");
3040 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3045 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
3049 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
3054 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
3059 t = gfc_add_save (¤t_attr, NULL, &seen_at[d]);
3063 t = gfc_add_target (¤t_attr, &seen_at[d]);
3066 case DECL_IS_BIND_C:
3067 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
3071 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3076 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
3080 if (gfc_notify_std (GFC_STD_F2003,
3081 "Fortran 2003: VOLATILE attribute at %C")
3085 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
3089 gfc_internal_error ("match_attr_spec(): Bad attribute");
3103 gfc_current_locus = start;
3104 gfc_free_array_spec (current_as);
3110 /* Set the binding label, dest_label, either with the binding label
3111 stored in the given gfc_typespec, ts, or if none was provided, it
3112 will be the symbol name in all lower case, as required by the draft
3113 (J3/04-007, section 15.4.1). If a binding label was given and
3114 there is more than one argument (num_idents), it is an error. */
3117 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3119 if (num_idents > 1 && has_name_equals)
3121 gfc_error ("Multiple identifiers provided with "
3122 "single NAME= specifier at %C");
3126 if (curr_binding_label[0] != '\0')
3128 /* Binding label given; store in temp holder til have sym. */
3129 strncpy (dest_label, curr_binding_label,
3130 strlen (curr_binding_label) + 1);
3134 /* No binding label given, and the NAME= specifier did not exist,
3135 which means there was no NAME="". */
3136 if (sym_name != NULL && has_name_equals == 0)
3137 strncpy (dest_label, sym_name, strlen (sym_name) + 1);
3144 /* Set the status of the given common block as being BIND(C) or not,
3145 depending on the given parameter, is_bind_c. */
3148 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3150 com_block->is_bind_c = is_bind_c;
3155 /* Verify that the given gfc_typespec is for a C interoperable type. */
3158 verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
3162 /* Make sure the kind used is appropriate for the type.
3163 The f90_type is unknown if an integer constant was
3164 used (e.g., real(4), bind(c) :: myFloat). */
3165 if (ts->f90_type != BT_UNKNOWN)
3167 t = gfc_validate_c_kind (ts);
3170 /* Print an error, but continue parsing line. */
3171 gfc_error_now ("C kind parameter is for type %s but "
3172 "symbol '%s' at %L is of type %s",
3173 gfc_basic_typename (ts->f90_type),
3175 gfc_basic_typename (ts->type));
3179 /* Make sure the kind is C interoperable. This does not care about the
3180 possible error above. */
3181 if (ts->type == BT_DERIVED && ts->derived != NULL)
3182 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
3183 else if (ts->is_c_interop != 1)
3190 /* Verify that the variables of a given common block, which has been
3191 defined with the attribute specifier bind(c), to be of a C
3192 interoperable type. Errors will be reported here, if
3196 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3198 gfc_symbol *curr_sym = NULL;
3199 try retval = SUCCESS;
3201 curr_sym = com_block->head;
3203 /* Make sure we have at least one symbol. */
3204 if (curr_sym == NULL)
3207 /* Here we know we have a symbol, so we'll execute this loop
3211 /* The second to last param, 1, says this is in a common block. */
3212 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3213 curr_sym = curr_sym->common_next;
3214 } while (curr_sym != NULL);
3220 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3221 an appropriate error message is reported. */
3224 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3225 int is_in_common, gfc_common_head *com_block)
3227 try retval = SUCCESS;
3229 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3231 tmp_sym = tmp_sym->result;
3232 /* Make sure it wasn't an implicitly typed result. */
3233 if (tmp_sym->attr.implicit_type)
3235 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3236 "%L may not be C interoperable", tmp_sym->name,
3237 &tmp_sym->declared_at);
3238 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3239 /* Mark it as C interoperable to prevent duplicate warnings. */
3240 tmp_sym->ts.is_c_interop = 1;
3241 tmp_sym->attr.is_c_interop = 1;
3245 /* Here, we know we have the bind(c) attribute, so if we have
3246 enough type info, then verify that it's a C interop kind.
3247 The info could be in the symbol already, or possibly still in
3248 the given ts (current_ts), so look in both. */
3249 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3251 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
3252 &(tmp_sym->declared_at)) != SUCCESS)
3254 /* See if we're dealing with a sym in a common block or not. */
3255 if (is_in_common == 1)
3257 gfc_warning ("Variable '%s' in common block '%s' at %L "
3258 "may not be a C interoperable "
3259 "kind though common block '%s' is BIND(C)",
3260 tmp_sym->name, com_block->name,
3261 &(tmp_sym->declared_at), com_block->name);
3265 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3266 gfc_error ("Type declaration '%s' at %L is not C "
3267 "interoperable but it is BIND(C)",
3268 tmp_sym->name, &(tmp_sym->declared_at));
3270 gfc_warning ("Variable '%s' at %L "
3271 "may not be a C interoperable "
3272 "kind but it is bind(c)",
3273 tmp_sym->name, &(tmp_sym->declared_at));
3277 /* Variables declared w/in a common block can't be bind(c)
3278 since there's no way for C to see these variables, so there's
3279 semantically no reason for the attribute. */
3280 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3282 gfc_error ("Variable '%s' in common block '%s' at "
3283 "%L cannot be declared with BIND(C) "
3284 "since it is not a global",
3285 tmp_sym->name, com_block->name,
3286 &(tmp_sym->declared_at));
3290 /* Scalar variables that are bind(c) can not have the pointer
3291 or allocatable attributes. */
3292 if (tmp_sym->attr.is_bind_c == 1)
3294 if (tmp_sym->attr.pointer == 1)
3296 gfc_error ("Variable '%s' at %L cannot have both the "
3297 "POINTER and BIND(C) attributes",
3298 tmp_sym->name, &(tmp_sym->declared_at));
3302 if (tmp_sym->attr.allocatable == 1)
3304 gfc_error ("Variable '%s' at %L cannot have both the "
3305 "ALLOCATABLE and BIND(C) attributes",
3306 tmp_sym->name, &(tmp_sym->declared_at));
3310 /* If it is a BIND(C) function, make sure the return value is a
3311 scalar value. The previous tests in this function made sure
3312 the type is interoperable. */
3313 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
3314 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3315 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3317 /* BIND(C) functions can not return a character string. */
3318 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
3319 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3320 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3321 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3322 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3323 "be a character string", tmp_sym->name,
3324 &(tmp_sym->declared_at));
3328 /* See if the symbol has been marked as private. If it has, make sure
3329 there is no binding label and warn the user if there is one. */
3330 if (tmp_sym->attr.access == ACCESS_PRIVATE
3331 && tmp_sym->binding_label[0] != '\0')
3332 /* Use gfc_warning_now because we won't say that the symbol fails
3333 just because of this. */
3334 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3335 "given the binding label '%s'", tmp_sym->name,
3336 &(tmp_sym->declared_at), tmp_sym->binding_label);
3342 /* Set the appropriate fields for a symbol that's been declared as
3343 BIND(C) (the is_bind_c flag and the binding label), and verify that
3344 the type is C interoperable. Errors are reported by the functions
3345 used to set/test these fields. */
3348 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3350 try retval = SUCCESS;
3352 /* TODO: Do we need to make sure the vars aren't marked private? */
3354 /* Set the is_bind_c bit in symbol_attribute. */
3355 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3357 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3358 num_idents) != SUCCESS)
3365 /* Set the fields marking the given common block as BIND(C), including
3366 a binding label, and report any errors encountered. */
3369 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3371 try retval = SUCCESS;
3373 /* destLabel, common name, typespec (which may have binding label). */
3374 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3378 /* Set the given common block (com_block) to being bind(c) (1). */
3379 set_com_block_bind_c (com_block, 1);
3385 /* Retrieve the list of one or more identifiers that the given bind(c)
3386 attribute applies to. */
3389 get_bind_c_idents (void)
3391 char name[GFC_MAX_SYMBOL_LEN + 1];
3393 gfc_symbol *tmp_sym = NULL;
3395 gfc_common_head *com_block = NULL;
3397 if (gfc_match_name (name) == MATCH_YES)
3399 found_id = MATCH_YES;
3400 gfc_get_ha_symbol (name, &tmp_sym);
3402 else if (match_common_name (name) == MATCH_YES)
3404 found_id = MATCH_YES;
3405 com_block = gfc_get_common (name, 0);
3409 gfc_error ("Need either entity or common block name for "
3410 "attribute specification statement at %C");
3414 /* Save the current identifier and look for more. */
3417 /* Increment the number of identifiers found for this spec stmt. */
3420 /* Make sure we have a sym or com block, and verify that it can
3421 be bind(c). Set the appropriate field(s) and look for more
3423 if (tmp_sym != NULL || com_block != NULL)
3425 if (tmp_sym != NULL)
3427 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3433 if (set_verify_bind_c_com_block(com_block, num_idents)
3438 /* Look to see if we have another identifier. */
3440 if (gfc_match_eos () == MATCH_YES)
3441 found_id = MATCH_NO;
3442 else if (gfc_match_char (',') != MATCH_YES)
3443 found_id = MATCH_NO;
3444 else if (gfc_match_name (name) == MATCH_YES)
3446 found_id = MATCH_YES;
3447 gfc_get_ha_symbol (name, &tmp_sym);
3449 else if (match_common_name (name) == MATCH_YES)
3451 found_id = MATCH_YES;
3452 com_block = gfc_get_common (name, 0);
3456 gfc_error ("Missing entity or common block name for "
3457 "attribute specification statement at %C");
3463 gfc_internal_error ("Missing symbol");
3465 } while (found_id == MATCH_YES);
3467 /* if we get here we were successful */
3472 /* Try and match a BIND(C) attribute specification statement. */
3475 gfc_match_bind_c_stmt (void)
3477 match found_match = MATCH_NO;
3482 /* This may not be necessary. */
3484 /* Clear the temporary binding label holder. */
3485 curr_binding_label[0] = '\0';
3487 /* Look for the bind(c). */
3488 found_match = gfc_match_bind_c (NULL);
3490 if (found_match == MATCH_YES)
3492 /* Look for the :: now, but it is not required. */
3495 /* Get the identifier(s) that needs to be updated. This may need to
3496 change to hand the flag(s) for the attr specified so all identifiers
3497 found can have all appropriate parts updated (assuming that the same
3498 spec stmt can have multiple attrs, such as both bind(c) and
3500 if (get_bind_c_idents () != SUCCESS)
3501 /* Error message should have printed already. */
3509 /* Match a data declaration statement. */
3512 gfc_match_data_decl (void)
3518 num_idents_on_line = 0;
3520 m = gfc_match_type_spec (¤t_ts, 0);
3524 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3526 sym = gfc_use_derived (current_ts.derived);
3534 current_ts.derived = sym;
3537 m = match_attr_spec ();
3538 if (m == MATCH_ERROR)
3544 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL
3545 && !current_ts.derived->attr.zero_comp)
3548 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3551 gfc_find_symbol (current_ts.derived->name,
3552 current_ts.derived->ns->parent, 1, &sym);
3554 /* Any symbol that we find had better be a type definition
3555 which has its components defined. */
3556 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3557 && (current_ts.derived->components != NULL
3558 || current_ts.derived->attr.zero_comp))
3561 /* Now we have an error, which we signal, and then fix up
3562 because the knock-on is plain and simple confusing. */
3563 gfc_error_now ("Derived type at %C has not been previously defined "
3564 "and so cannot appear in a derived type definition");
3565 current_attr.pointer = 1;
3570 /* If we have an old-style character declaration, and no new-style
3571 attribute specifications, then there a comma is optional between
3572 the type specification and the variable list. */
3573 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3574 gfc_match_char (',');
3576 /* Give the types/attributes to symbols that follow. Give the element
3577 a number so that repeat character length expressions can be copied. */
3581 num_idents_on_line++;
3582 m = variable_decl (elem++);
3583 if (m == MATCH_ERROR)
3588 if (gfc_match_eos () == MATCH_YES)
3590 if (gfc_match_char (',') != MATCH_YES)
3594 if (gfc_error_flag_test () == 0)
3595 gfc_error ("Syntax error in data declaration at %C");
3598 gfc_free_data_all (gfc_current_ns);
3601 gfc_free_array_spec (current_as);
3607 /* Match a prefix associated with a function or subroutine
3608 declaration. If the typespec pointer is nonnull, then a typespec
3609 can be matched. Note that if nothing matches, MATCH_YES is
3610 returned (the null string was matched). */
3613 match_prefix (gfc_typespec *ts)
3617 gfc_clear_attr (¤t_attr);
3621 if (!seen_type && ts != NULL
3622 && gfc_match_type_spec (ts, 0) == MATCH_YES
3623 && gfc_match_space () == MATCH_YES)
3630 if (gfc_match ("elemental% ") == MATCH_YES)
3632 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
3638 if (gfc_match ("pure% ") == MATCH_YES)
3640 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
3646 if (gfc_match ("recursive% ") == MATCH_YES)
3648 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
3654 /* At this point, the next item is not a prefix. */
3659 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
3662 copy_prefix (symbol_attribute *dest, locus *where)
3664 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3667 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3670 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3677 /* Match a formal argument list. */
3680 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3682 gfc_formal_arglist *head, *tail, *p, *q;
3683 char name[GFC_MAX_SYMBOL_LEN + 1];
3689 if (gfc_match_char ('(') != MATCH_YES)
3696 if (gfc_match_char (')') == MATCH_YES)
3701 if (gfc_match_char ('*') == MATCH_YES)
3705 m = gfc_match_name (name);
3709 if (gfc_get_symbol (name, NULL, &sym))
3713 p = gfc_get_formal_arglist ();
3725 /* We don't add the VARIABLE flavor because the name could be a
3726 dummy procedure. We don't apply these attributes to formal
3727 arguments of statement functions. */
3728 if (sym != NULL && !st_flag
3729 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3730 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3736 /* The name of a program unit can be in a different namespace,
3737 so check for it explicitly. After the statement is accepted,
3738 the name is checked for especially in gfc_get_symbol(). */
3739 if (gfc_new_block != NULL && sym != NULL
3740 && strcmp (sym->name, gfc_new_block->name) == 0)
3742 gfc_error ("Name '%s' at %C is the name of the procedure",
3748 if (gfc_match_char (')') == MATCH_YES)
3751 m = gfc_match_char (',');
3754 gfc_error ("Unexpected junk in formal argument list at %C");
3760 /* Check for duplicate symbols in the formal argument list. */
3763 for (p = head; p->next; p = p->next)
3768 for (q = p->next; q; q = q->next)
3769 if (p->sym == q->sym)
3771 gfc_error ("Duplicate symbol '%s' in formal argument list "
3772 "at %C", p->sym->name);
3780 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3790 gfc_free_formal_arglist (head);
3795 /* Match a RESULT specification following a function declaration or
3796 ENTRY statement. Also matches the end-of-statement. */
3799 match_result (gfc_symbol *function, gfc_symbol **result)
3801 char name[GFC_MAX_SYMBOL_LEN + 1];
3805 if (gfc_match (" result (") != MATCH_YES)
3808 m = gfc_match_name (name);
3812 /* Get the right paren, and that's it because there could be the
3813 bind(c) attribute after the result clause. */
3814 if (gfc_match_char(')') != MATCH_YES)
3816 /* TODO: should report the missing right paren here. */
3820 if (strcmp (function->name, name) == 0)
3822 gfc_error ("RESULT variable at %C must be different than function name");
3826 if (gfc_get_symbol (name, NULL, &r))
3829 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3830 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
3839 /* Match a function suffix, which could be a combination of a result
3840 clause and BIND(C), either one, or neither. The draft does not
3841 require them to come in a specific order. */
3844 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3846 match is_bind_c; /* Found bind(c). */
3847 match is_result; /* Found result clause. */
3848 match found_match; /* Status of whether we've found a good match. */
3849 int peek_char; /* Character we're going to peek at. */
3851 /* Initialize to having found nothing. */
3852 found_match = MATCH_NO;
3853 is_bind_c = MATCH_NO;
3854 is_result = MATCH_NO;
3856 /* Get the next char to narrow between result and bind(c). */
3857 gfc_gobble_whitespace ();
3858 peek_char = gfc_peek_char ();
3863 /* Look for result clause. */
3864 is_result = match_result (sym, result);
3865 if (is_result == MATCH_YES)
3867 /* Now see if there is a bind(c) after it. */
3868 is_bind_c = gfc_match_bind_c (sym);
3869 /* We've found the result clause and possibly bind(c). */
3870 found_match = MATCH_YES;
3873 /* This should only be MATCH_ERROR. */
3874 found_match = is_result;
3877 /* Look for bind(c) first. */
3878 is_bind_c = gfc_match_bind_c (sym);
3879 if (is_bind_c == MATCH_YES)
3881 /* Now see if a result clause followed it. */
3882 is_result = match_result (sym, result);
3883 found_match = MATCH_YES;
3887 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3888 found_match = MATCH_ERROR;
3892 gfc_error ("Unexpected junk after function declaration at %C");
3893 found_match = MATCH_ERROR;
3897 if (is_bind_c == MATCH_YES)
3898 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
3906 /* Match a PROCEDURE declaration (R1211). */
3909 match_procedure_decl (void)
3912 locus old_loc, entry_loc;
3913 gfc_symbol *sym, *proc_if = NULL;
3916 old_loc = entry_loc = gfc_current_locus;
3918 gfc_clear_ts (¤t_ts);
3920 if (gfc_match (" (") != MATCH_YES)
3922 gfc_current_locus = entry_loc;
3926 /* Get the type spec. for the procedure interface. */
3927 old_loc = gfc_current_locus;
3928 m = gfc_match_type_spec (¤t_ts, 0);
3929 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_char () == ')'))
3932 if (m == MATCH_ERROR)
3935 gfc_current_locus = old_loc;
3937 /* Get the name of the procedure or abstract interface
3938 to inherit the interface from. */
3939 m = gfc_match_symbol (&proc_if, 1);
3943 else if (m == MATCH_ERROR)
3946 /* Various interface checks. */
3949 if (proc_if->generic)
3951 gfc_error ("Interface '%s' at %C may not be generic", proc_if->name);
3954 if (proc_if->attr.proc == PROC_ST_FUNCTION)
3956 gfc_error ("Interface '%s' at %C may not be a statement function",
3960 /* Handle intrinsic procedures. */
3961 if (gfc_intrinsic_name (proc_if->name, 0)
3962 || gfc_intrinsic_name (proc_if->name, 1))
3963 proc_if->attr.intrinsic = 1;
3964 if (proc_if->attr.intrinsic
3965 && !gfc_intrinsic_actual_ok (proc_if->name, 0))
3967 gfc_error ("Intrinsic procedure '%s' not allowed "
3968 "in PROCEDURE statement at %C", proc_if->name);
3971 /* TODO: Allow intrinsics with gfc_intrinsic_actual_ok
3972 (proc_if->name, 0) after PR33162 is fixed. */
3973 if (proc_if->attr.intrinsic)
3975 gfc_error ("Fortran 2003: Support for intrinsic procedure '%s' "
3976 "in PROCEDURE statement at %C not yet implemented "
3977 "in gfortran", proc_if->name);
3984 if (gfc_match (" )") != MATCH_YES)
3986 gfc_current_locus = entry_loc;
3990 /* Parse attributes. */
3991 m = match_attr_spec();
3992 if (m == MATCH_ERROR)
3995 /* Get procedure symbols. */
3999 m = gfc_match_symbol (&sym, 0);
4002 else if (m == MATCH_ERROR)
4005 /* Add current_attr to the symbol attributes. */
4006 if (gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
4009 if (sym->attr.is_bind_c)
4011 /* Check for C1218. */
4012 if (!proc_if || !proc_if->attr.is_bind_c)
4014 gfc_error ("BIND(C) attribute at %C requires "
4015 "an interface with BIND(C)");
4018 /* Check for C1217. */
4019 if (has_name_equals && sym->attr.pointer)
4021 gfc_error ("BIND(C) procedure with NAME may not have "
4022 "POINTER attribute at %C");
4025 if (has_name_equals && sym->attr.dummy)
4027 gfc_error ("Dummy procedure at %C may not have "
4028 "BIND(C) attribute with NAME");
4031 /* Set binding label for BIND(C). */
4032 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4036 if (!sym->attr.pointer && gfc_add_external (&sym->attr, NULL) == FAILURE)
4038 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4041 /* Set interface. */
4042 if (proc_if != NULL)
4043 sym->interface = proc_if;
4044 else if (current_ts.type != BT_UNKNOWN)
4046 sym->interface = gfc_new_symbol ("", gfc_current_ns);
4047 sym->interface->ts = current_ts;
4048 sym->interface->attr.function = 1;
4049 sym->ts = sym->interface->ts;
4050 sym->attr.function = sym->interface->attr.function;
4053 if (gfc_match_eos () == MATCH_YES)
4055 if (gfc_match_char (',') != MATCH_YES)
4060 gfc_error ("Syntax error in PROCEDURE statement at %C");
4065 /* Match a PROCEDURE declaration inside an interface (R1206). */
4068 match_procedure_in_interface (void)
4072 char name[GFC_MAX_SYMBOL_LEN + 1];
4074 if (current_interface.type == INTERFACE_NAMELESS
4075 || current_interface.type == INTERFACE_ABSTRACT)
4077 gfc_error ("PROCEDURE at %C must be in a generic interface");
4083 m = gfc_match_name (name);
4086 else if (m == MATCH_ERROR)
4088 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4091 if (gfc_add_interface (sym) == FAILURE)
4094 sym->attr.procedure = 1;
4096 if (gfc_match_eos () == MATCH_YES)
4098 if (gfc_match_char (',') != MATCH_YES)
4105 gfc_error ("Syntax error in PROCEDURE statement at %C");
4110 /* General matcher for PROCEDURE declarations. */
4113 gfc_match_procedure (void)
4117 switch (gfc_current_state ())
4122 case COMP_SUBROUTINE:
4124 m = match_procedure_decl ();
4126 case COMP_INTERFACE:
4127 m = match_procedure_in_interface ();
4130 gfc_error ("Fortran 2003: Procedure components at %C are "
4131 "not yet implemented in gfortran");
4140 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4148 /* Match a function declaration. */
4151 gfc_match_function_decl (void)
4153 char name[GFC_MAX_SYMBOL_LEN + 1];
4154 gfc_symbol *sym, *result;
4158 match found_match; /* Status returned by match func. */
4160 if (gfc_current_state () != COMP_NONE
4161 && gfc_current_state () != COMP_INTERFACE
4162 && gfc_current_state () != COMP_CONTAINS)
4165 gfc_clear_ts (¤t_ts);
4167 old_loc = gfc_current_locus;
4169 m = match_prefix (¤t_ts);
4172 gfc_current_locus = old_loc;
4176 if (gfc_match ("function% %n", name) != MATCH_YES)
4178 gfc_current_locus = old_loc;
4181 if (get_proc_name (name, &sym, false))
4183 gfc_new_block = sym;
4185 m = gfc_match_formal_arglist (sym, 0, 0);
4188 gfc_error ("Expected formal argument list in function "
4189 "definition at %C");
4193 else if (m == MATCH_ERROR)
4198 /* According to the draft, the bind(c) and result clause can
4199 come in either order after the formal_arg_list (i.e., either
4200 can be first, both can exist together or by themselves or neither
4201 one). Therefore, the match_result can't match the end of the
4202 string, and check for the bind(c) or result clause in either order. */
4203 found_match = gfc_match_eos ();
4205 /* Make sure that it isn't already declared as BIND(C). If it is, it
4206 must have been marked BIND(C) with a BIND(C) attribute and that is
4207 not allowed for procedures. */
4208 if (sym->attr.is_bind_c == 1)
4210 sym->attr.is_bind_c = 0;
4211 if (sym->old_symbol != NULL)
4212 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4213 "variables or common blocks",
4214 &(sym->old_symbol->declared_at));
4216 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4217 "variables or common blocks", &gfc_current_locus);
4220 if (found_match != MATCH_YES)
4222 /* If we haven't found the end-of-statement, look for a suffix. */
4223 suffix_match = gfc_match_suffix (sym, &result);
4224 if (suffix_match == MATCH_YES)
4225 /* Need to get the eos now. */
4226 found_match = gfc_match_eos ();
4228 found_match = suffix_match;
4231 if(found_match != MATCH_YES)
4235 /* Make changes to the symbol. */
4238 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4241 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4242 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4245 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
4246 && !sym->attr.implicit_type)
4248 gfc_error ("Function '%s' at %C already has a type of %s", name,
4249 gfc_basic_typename (sym->ts.type));
4255 sym->ts = current_ts;
4260 result->ts = current_ts;
4261 sym->result = result;
4268 gfc_current_locus = old_loc;
4273 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4274 pass the name of the entry, rather than the gfc_current_block name, and
4275 to return false upon finding an existing global entry. */
4278 add_global_entry (const char *name, int sub)
4282 s = gfc_get_gsymbol(name);
4285 || (s->type != GSYM_UNKNOWN
4286 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
4287 gfc_global_used(s, NULL);
4290 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4291 s->where = gfc_current_locus;
4299 /* Match an ENTRY statement. */
4302 gfc_match_entry (void)
4307 char name[GFC_MAX_SYMBOL_LEN + 1];
4308 gfc_compile_state state;
4312 bool module_procedure;
4314 m = gfc_match_name (name);
4318 state = gfc_current_state ();
4319 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
4324 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4327 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4329 case COMP_BLOCK_DATA:
4330 gfc_error ("ENTRY statement at %C cannot appear within "
4333 case COMP_INTERFACE:
4334 gfc_error ("ENTRY statement at %C cannot appear within "
4338 gfc_error ("ENTRY statement at %C cannot appear within "
4339 "a DERIVED TYPE block");
4342 gfc_error ("ENTRY statement at %C cannot appear within "
4343 "an IF-THEN block");
4346 gfc_error ("ENTRY statement at %C cannot appear within "
4350 gfc_error ("ENTRY statement at %C cannot appear within "
4354 gfc_error ("ENTRY statement at %C cannot appear within "
4358 gfc_error ("ENTRY statement at %C cannot appear within "
4362 gfc_error ("ENTRY statement at %C cannot appear within "
4363 "a contained subprogram");
4366 gfc_internal_error ("gfc_match_entry(): Bad state");
4371 module_procedure = gfc_current_ns->parent != NULL
4372 && gfc_current_ns->parent->proc_name
4373 && gfc_current_ns->parent->proc_name->attr.flavor
4376 if (gfc_current_ns->parent != NULL
4377 && gfc_current_ns->parent->proc_name
4378 && !module_procedure)
4380 gfc_error("ENTRY statement at %C cannot appear in a "
4381 "contained procedure");
4385 /* Module function entries need special care in get_proc_name
4386 because previous references within the function will have
4387 created symbols attached to the current namespace. */
4388 if (get_proc_name (name, &entry,
4389 gfc_current_ns->parent != NULL
4391 && gfc_current_ns->proc_name->attr.function))
4394 proc = gfc_current_block ();
4396 if (state == COMP_SUBROUTINE)
4398 /* An entry in a subroutine. */
4399 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
4402 m = gfc_match_formal_arglist (entry, 0, 1);
4406 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4407 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4412 /* An entry in a function.
4413 We need to take special care because writing
4418 ENTRY f() RESULT (r)
4420 ENTRY f RESULT (r). */
4421 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
4424 old_loc = gfc_current_locus;
4425 if (gfc_match_eos () == MATCH_YES)
4427 gfc_current_locus = old_loc;
4428 /* Match the empty argument list, and add the interface to
4430 m = gfc_match_formal_arglist (entry, 0, 1);
4433 m = gfc_match_formal_arglist (entry, 0, 0);
4440 if (gfc_match_eos () == MATCH_YES)
4442 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4443 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4446 entry->result = entry;
4450 m = match_result (proc, &result);
4452 gfc_syntax_error (ST_ENTRY);
4456 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4457 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
4458 || gfc_add_function (&entry->attr, result->name, NULL)
4462 entry->result = result;
4466 if (gfc_match_eos () != MATCH_YES)
4468 gfc_syntax_error (ST_ENTRY);
4472 entry->attr.recursive = proc->attr.recursive;
4473 entry->attr.elemental = proc->attr.elemental;
4474 entry->attr.pure = proc->attr.pure;
4476 el = gfc_get_entry_list ();
4478 el->next = gfc_current_ns->entries;
4479 gfc_current_ns->entries = el;
4481 el->id = el->next->id + 1;
4485 new_st.op = EXEC_ENTRY;
4486 new_st.ext.entry = el;
4492 /* Match a subroutine statement, including optional prefixes. */
4495 gfc_match_subroutine (void)
4497 char name[GFC_MAX_SYMBOL_LEN + 1];
4503 if (gfc_current_state () != COMP_NONE
4504 && gfc_current_state () != COMP_INTERFACE
4505 && gfc_current_state () != COMP_CONTAINS)
4508 m = match_prefix (NULL);
4512 m = gfc_match ("subroutine% %n", name);
4516 if (get_proc_name (name, &sym, false))
4518 gfc_new_block = sym;
4520 /* Check what next non-whitespace character is so we can tell if there
4521 where the required parens if we have a BIND(C). */
4522 gfc_gobble_whitespace ();
4523 peek_char = gfc_peek_char ();
4525 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
4528 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4531 /* Make sure that it isn't already declared as BIND(C). If it is, it
4532 must have been marked BIND(C) with a BIND(C) attribute and that is
4533 not allowed for procedures. */
4534 if (sym->attr.is_bind_c == 1)
4536 sym->attr.is_bind_c = 0;
4537 if (sym->old_symbol != NULL)
4538 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4539 "variables or common blocks",
4540 &(sym->old_symbol->declared_at));
4542 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4543 "variables or common blocks", &gfc_current_locus);
4546 /* Here, we are just checking if it has the bind(c) attribute, and if
4547 so, then we need to make sure it's all correct. If it doesn't,
4548 we still need to continue matching the rest of the subroutine line. */
4549 is_bind_c = gfc_match_bind_c (sym);
4550 if (is_bind_c == MATCH_ERROR)
4552 /* There was an attempt at the bind(c), but it was wrong. An
4553 error message should have been printed w/in the gfc_match_bind_c
4554 so here we'll just return the MATCH_ERROR. */
4558 if (is_bind_c == MATCH_YES)
4560 if (peek_char != '(')
4562 gfc_error ("Missing required parentheses before BIND(C) at %C");
4565 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4570 if (gfc_match_eos () != MATCH_YES)
4572 gfc_syntax_error (ST_SUBROUTINE);
4576 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4583 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4584 given, and set the binding label in either the given symbol (if not
4585 NULL), or in the current_ts. The symbol may be NULL because we may
4586 encounter the BIND(C) before the declaration itself. Return
4587 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4588 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4589 or MATCH_YES if the specifier was correct and the binding label and
4590 bind(c) fields were set correctly for the given symbol or the
4594 gfc_match_bind_c (gfc_symbol *sym)
4596 /* binding label, if exists */
4597 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4601 /* Initialize the flag that specifies whether we encountered a NAME=
4602 specifier or not. */
4603 has_name_equals = 0;
4605 /* Init the first char to nil so we can catch if we don't have
4606 the label (name attr) or the symbol name yet. */
4607 binding_label[0] = '\0';
4609 /* This much we have to be able to match, in this order, if
4610 there is a bind(c) label. */
4611 if (gfc_match (" bind ( c ") != MATCH_YES)
4614 /* Now see if there is a binding label, or if we've reached the
4615 end of the bind(c) attribute without one. */
4616 if (gfc_match_char (',') == MATCH_YES)
4618 if (gfc_match (" name = ") != MATCH_YES)
4620 gfc_error ("Syntax error in NAME= specifier for binding label "
4622 /* should give an error message here */
4626 has_name_equals = 1;
4628 /* Get the opening quote. */
4629 double_quote = MATCH_YES;
4630 single_quote = MATCH_YES;
4631 double_quote = gfc_match_char ('"');
4632 if (double_quote != MATCH_YES)
4633 single_quote = gfc_match_char ('\'');
4634 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4636 gfc_error ("Syntax error in NAME= specifier for binding label "
4641 /* Grab the binding label, using functions that will not lower
4642 case the names automatically. */
4643 if (gfc_match_name_C (binding_label) != MATCH_YES)
4646 /* Get the closing quotation. */
4647 if (double_quote == MATCH_YES)
4649 if (gfc_match_char ('"') != MATCH_YES)
4651 gfc_error ("Missing closing quote '\"' for binding label at %C");
4652 /* User started string with '"' so looked to match it. */
4658 if (gfc_match_char ('\'') != MATCH_YES)
4660 gfc_error ("Missing closing quote '\'' for binding label at %C");
4661 /* User started string with "'" char. */
4667 /* Get the required right paren. */
4668 if (gfc_match_char (')') != MATCH_YES)
4670 gfc_error ("Missing closing paren for binding label at %C");
4674 /* Save the binding label to the symbol. If sym is null, we're
4675 probably matching the typespec attributes of a declaration and
4676 haven't gotten the name yet, and therefore, no symbol yet. */
4677 if (binding_label[0] != '\0')
4681 strncpy (sym->binding_label, binding_label,
4682 strlen (binding_label)+1);
4685 strncpy (curr_binding_label, binding_label,
4686 strlen (binding_label) + 1);
4690 /* No binding label, but if symbol isn't null, we
4691 can set the label for it here. */
4692 /* TODO: If the name= was given and no binding label (name=""), we simply
4693 will let fortran mangle the symbol name as it usually would.
4694 However, this could still let C call it if the user looked up the
4695 symbol in the object file. Should the name set during mangling in
4696 trans-decl.c be marked with characters that are invalid for C to
4698 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4699 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4702 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
4703 && current_interface.type == INTERFACE_ABSTRACT)
4705 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
4713 /* Return nonzero if we're currently compiling a contained procedure. */
4716 contained_procedure (void)
4720 for (s=gfc_state_stack; s; s=s->previous)
4721 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
4722 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
4728 /* Set the kind of each enumerator. The kind is selected such that it is
4729 interoperable with the corresponding C enumeration type, making
4730 sure that -fshort-enums is honored. */
4735 enumerator_history *current_history = NULL;
4739 if (max_enum == NULL || enum_history == NULL)
4742 if (!gfc_option.fshort_enums)
4748 kind = gfc_integer_kinds[i++].kind;
4750 while (kind < gfc_c_int_kind
4751 && gfc_check_integer_range (max_enum->initializer->value.integer,
4754 current_history = enum_history;
4755 while (current_history != NULL)
4757 current_history->sym->ts.kind = kind;
4758 current_history = current_history->next;
4763 /* Match any of the various end-block statements. Returns the type of
4764 END to the caller. The END INTERFACE, END IF, END DO and END
4765 SELECT statements cannot be replaced by a single END statement. */
4768 gfc_match_end (gfc_statement *st)
4770 char name[GFC_MAX_SYMBOL_LEN + 1];
4771 gfc_compile_state state;
4773 const char *block_name;
4778 old_loc = gfc_current_locus;
4779 if (gfc_match ("end") != MATCH_YES)
4782 state = gfc_current_state ();
4783 block_name = gfc_current_block () == NULL
4784 ? NULL : gfc_current_block ()->name;
4786 if (state == COMP_CONTAINS)
4788 state = gfc_state_stack->previous->state;
4789 block_name = gfc_state_stack->previous->sym == NULL
4790 ? NULL : gfc_state_stack->previous->sym->name;
4797 *st = ST_END_PROGRAM;
4798 target = " program";
4802 case COMP_SUBROUTINE:
4803 *st = ST_END_SUBROUTINE;
4804 target = " subroutine";
4805 eos_ok = !contained_procedure ();
4809 *st = ST_END_FUNCTION;
4810 target = " function";
4811 eos_ok = !contained_procedure ();
4814 case COMP_BLOCK_DATA:
4815 *st = ST_END_BLOCK_DATA;
4816 target = " block data";
4821 *st = ST_END_MODULE;
4826 case COMP_INTERFACE:
4827 *st = ST_END_INTERFACE;
4828 target = " interface";
4851 *st = ST_END_SELECT;
4857 *st = ST_END_FORALL;
4872 last_initializer = NULL;
4874 gfc_free_enum_history ();
4878 gfc_error ("Unexpected END statement at %C");
4882 if (gfc_match_eos () == MATCH_YES)
4886 /* We would have required END [something]. */
4887 gfc_error ("%s statement expected at %L",
4888 gfc_ascii_statement (*st), &old_loc);
4895 /* Verify that we've got the sort of end-block that we're expecting. */
4896 if (gfc_match (target) != MATCH_YES)
4898 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
4902 /* If we're at the end, make sure a block name wasn't required. */
4903 if (gfc_match_eos () == MATCH_YES)
4906 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
4907 && *st != ST_END_FORALL && *st != ST_END_WHERE)
4910 if (gfc_current_block () == NULL)
4913 gfc_error ("Expected block name of '%s' in %s statement at %C",
4914 block_name, gfc_ascii_statement (*st));
4919 /* END INTERFACE has a special handler for its several possible endings. */
4920 if (*st == ST_END_INTERFACE)
4921 return gfc_match_end_interface ();
4923 /* We haven't hit the end of statement, so what is left must be an
4925 m = gfc_match_space ();
4927 m = gfc_match_name (name);
4930 gfc_error ("Expected terminating name at %C");
4934 if (block_name == NULL)
4937 if (strcmp (name, block_name) != 0)
4939 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
4940 gfc_ascii_statement (*st));
4944 if (gfc_match_eos () == MATCH_YES)
4948 gfc_syntax_error (*st);
4951 gfc_current_locus = old_loc;
4957 /***************** Attribute declaration statements ****************/
4959 /* Set the attribute of a single variable. */
4964 char name[GFC_MAX_SYMBOL_LEN + 1];
4972 m = gfc_match_name (name);
4976 if (find_special (name, &sym))
4979 var_locus = gfc_current_locus;
4981 /* Deal with possible array specification for certain attributes. */
4982 if (current_attr.dimension
4983 || current_attr.allocatable
4984 || current_attr.pointer
4985 || current_attr.target)
4987 m = gfc_match_array_spec (&as);
4988 if (m == MATCH_ERROR)
4991 if (current_attr.dimension && m == MATCH_NO)
4993 gfc_error ("Missing array specification at %L in DIMENSION "
4994 "statement", &var_locus);
4999 if ((current_attr.allocatable || current_attr.pointer)
5000 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5002 gfc_error ("Array specification must be deferred at %L", &var_locus);
5008 /* Update symbol table. DIMENSION attribute is set
5009 in gfc_set_array_spec(). */
5010 if (current_attr.dimension == 0
5011 && gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
5017 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5023 if (sym->attr.cray_pointee && sym->as != NULL)
5025 /* Fix the array spec. */
5026 m = gfc_mod_pointee_as (sym->as);
5027 if (m == MATCH_ERROR)
5031 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
5037 if ((current_attr.external || current_attr.intrinsic)
5038 && sym->attr.flavor != FL_PROCEDURE
5039 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
5048 gfc_free_array_spec (as);
5053 /* Generic attribute declaration subroutine. Used for attributes that
5054 just have a list of names. */
5061 /* Gobble the optional double colon, by simply ignoring the result
5071 if (gfc_match_eos () == MATCH_YES)
5077 if (gfc_match_char (',') != MATCH_YES)
5079 gfc_error ("Unexpected character in variable list at %C");
5089 /* This routine matches Cray Pointer declarations of the form:
5090 pointer ( <pointer>, <pointee> )
5092 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5093 The pointer, if already declared, should be an integer. Otherwise, we
5094 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5095 be either a scalar, or an array declaration. No space is allocated for
5096 the pointee. For the statement
5097 pointer (ipt, ar(10))
5098 any subsequent uses of ar will be translated (in C-notation) as
5099 ar(i) => ((<type> *) ipt)(i)
5100 After gimplification, pointee variable will disappear in the code. */
5103 cray_pointer_decl (void)
5107 gfc_symbol *cptr; /* Pointer symbol. */
5108 gfc_symbol *cpte; /* Pointee symbol. */
5114 if (gfc_match_char ('(') != MATCH_YES)
5116 gfc_error ("Expected '(' at %C");
5120 /* Match pointer. */
5121 var_locus = gfc_current_locus;
5122 gfc_clear_attr (¤t_attr);
5123 gfc_add_cray_pointer (¤t_attr, &var_locus);
5124 current_ts.type = BT_INTEGER;
5125 current_ts.kind = gfc_index_integer_kind;
5127 m = gfc_match_symbol (&cptr, 0);
5130 gfc_error ("Expected variable name at %C");
5134 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5137 gfc_set_sym_referenced (cptr);
5139 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5141 cptr->ts.type = BT_INTEGER;
5142 cptr->ts.kind = gfc_index_integer_kind;
5144 else if (cptr->ts.type != BT_INTEGER)
5146 gfc_error ("Cray pointer at %C must be an integer");
5149 else if (cptr->ts.kind < gfc_index_integer_kind)
5150 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5151 " memory addresses require %d bytes",
5152 cptr->ts.kind, gfc_index_integer_kind);
5154 if (gfc_match_char (',') != MATCH_YES)
5156 gfc_error ("Expected \",\" at %C");
5160 /* Match Pointee. */
5161 var_locus = gfc_current_locus;
5162 gfc_clear_attr (¤t_attr);
5163 gfc_add_cray_pointee (¤t_attr, &var_locus);
5164 current_ts.type = BT_UNKNOWN;
5165 current_ts.kind = 0;
5167 m = gfc_match_symbol (&cpte, 0);
5170 gfc_error ("Expected variable name at %C");
5174 /* Check for an optional array spec. */
5175 m = gfc_match_array_spec (&as);
5176 if (m == MATCH_ERROR)
5178 gfc_free_array_spec (as);
5181 else if (m == MATCH_NO)
5183 gfc_free_array_spec (as);
5187 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5190 gfc_set_sym_referenced (cpte);
5192 if (cpte->as == NULL)
5194 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5195 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5197 else if (as != NULL)
5199 gfc_error ("Duplicate array spec for Cray pointee at %C");
5200 gfc_free_array_spec (as);
5206 if (cpte->as != NULL)
5208 /* Fix array spec. */
5209 m = gfc_mod_pointee_as (cpte->as);
5210 if (m == MATCH_ERROR)
5214 /* Point the Pointee at the Pointer. */
5215 cpte->cp_pointer = cptr;
5217 if (gfc_match_char (')') != MATCH_YES)
5219 gfc_error ("Expected \")\" at %C");
5222 m = gfc_match_char (',');
5224 done = true; /* Stop searching for more declarations. */
5228 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5229 || gfc_match_eos () != MATCH_YES)
5231 gfc_error ("Expected \",\" or end of statement at %C");
5239 gfc_match_external (void)
5242 gfc_clear_attr (¤t_attr);
5243 current_attr.external = 1;
5245 return attr_decl ();
5250 gfc_match_intent (void)
5254 intent = match_intent_spec ();
5255 if (intent == INTENT_UNKNOWN)
5258 gfc_clear_attr (¤t_attr);
5259 current_attr.intent = intent;
5261 return attr_decl ();
5266 gfc_match_intrinsic (void)
5269 gfc_clear_attr (¤t_attr);
5270 current_attr.intrinsic = 1;
5272 return attr_decl ();
5277 gfc_match_optional (void)
5280 gfc_clear_attr (¤t_attr);
5281 current_attr.optional = 1;
5283 return attr_decl ();
5288 gfc_match_pointer (void)
5290 gfc_gobble_whitespace ();
5291 if (gfc_peek_char () == '(')
5293 if (!gfc_option.flag_cray_pointer)
5295 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5299 return cray_pointer_decl ();
5303 gfc_clear_attr (¤t_attr);
5304 current_attr.pointer = 1;
5306 return attr_decl ();
5312 gfc_match_allocatable (void)
5314 gfc_clear_attr (¤t_attr);
5315 current_attr.allocatable = 1;
5317 return attr_decl ();
5322 gfc_match_dimension (void)
5324 gfc_clear_attr (¤t_attr);
5325 current_attr.dimension = 1;
5327 return attr_decl ();
5332 gfc_match_target (void)
5334 gfc_clear_attr (¤t_attr);
5335 current_attr.target = 1;
5337 return attr_decl ();
5341 /* Match the list of entities being specified in a PUBLIC or PRIVATE
5345 access_attr_decl (gfc_statement st)
5347 char name[GFC_MAX_SYMBOL_LEN + 1];
5348 interface_type type;
5351 gfc_intrinsic_op operator;
5354 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5359 m = gfc_match_generic_spec (&type, name, &operator);
5362 if (m == MATCH_ERROR)
5367 case INTERFACE_NAMELESS:
5368 case INTERFACE_ABSTRACT:
5371 case INTERFACE_GENERIC:
5372 if (gfc_get_symbol (name, NULL, &sym))
5375 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5376 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
5377 sym->name, NULL) == FAILURE)
5382 case INTERFACE_INTRINSIC_OP:
5383 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
5385 gfc_current_ns->operator_access[operator] =
5386 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5390 gfc_error ("Access specification of the %s operator at %C has "
5391 "already been specified", gfc_op2string (operator));
5397 case INTERFACE_USER_OP:
5398 uop = gfc_get_uop (name);
5400 if (uop->access == ACCESS_UNKNOWN)
5402 uop->access = (st == ST_PUBLIC)
5403 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5407 gfc_error ("Access specification of the .%s. operator at %C "
5408 "has already been specified", sym->name);
5415 if (gfc_match_char (',') == MATCH_NO)
5419 if (gfc_match_eos () != MATCH_YES)
5424 gfc_syntax_error (st);
5432 gfc_match_protected (void)
5437 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5439 gfc_error ("PROTECTED at %C only allowed in specification "
5440 "part of a module");
5445 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
5449 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5454 if (gfc_match_eos () == MATCH_YES)
5459 m = gfc_match_symbol (&sym, 0);
5463 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
5476 if (gfc_match_eos () == MATCH_YES)
5478 if (gfc_match_char (',') != MATCH_YES)
5485 gfc_error ("Syntax error in PROTECTED statement at %C");
5490 /* The PRIVATE statement is a bit weird in that it can be an attribute
5491 declaration, but also works as a standlone statement inside of a
5492 type declaration or a module. */
5495 gfc_match_private (gfc_statement *st)
5498 if (gfc_match ("private") != MATCH_YES)
5501 if (gfc_current_state () != COMP_MODULE
5502 && (gfc_current_state () != COMP_DERIVED
5503 || !gfc_state_stack->previous
5504 || gfc_state_stack->previous->state != COMP_MODULE))
5506 gfc_error ("PRIVATE statement at %C is only allowed in the "
5507 "specification part of a module");
5511 if (gfc_current_state () == COMP_DERIVED)
5513 if (gfc_match_eos () == MATCH_YES)
5519 gfc_syntax_error (ST_PRIVATE);
5523 if (gfc_match_eos () == MATCH_YES)
5530 return access_attr_decl (ST_PRIVATE);
5535 gfc_match_public (gfc_statement *st)
5538 if (gfc_match ("public") != MATCH_YES)
5541 if (gfc_current_state () != COMP_MODULE)
5543 gfc_error ("PUBLIC statement at %C is only allowed in the "
5544 "specification part of a module");
5548 if (gfc_match_eos () == MATCH_YES)
5555 return access_attr_decl (ST_PUBLIC);
5559 /* Workhorse for gfc_match_parameter. */
5568 m = gfc_match_symbol (&sym, 0);
5570 gfc_error ("Expected variable name at %C in PARAMETER statement");
5575 if (gfc_match_char ('=') == MATCH_NO)
5577 gfc_error ("Expected = sign in PARAMETER statement at %C");
5581 m = gfc_match_init_expr (&init);
5583 gfc_error ("Expected expression at %C in PARAMETER statement");
5587 if (sym->ts.type == BT_UNKNOWN
5588 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5594 if (gfc_check_assign_symbol (sym, init) == FAILURE
5595 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
5601 if (sym->ts.type == BT_CHARACTER
5602 && sym->ts.cl != NULL
5603 && sym->ts.cl->length != NULL
5604 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5605 && init->expr_type == EXPR_CONSTANT
5606 && init->ts.type == BT_CHARACTER
5607 && init->ts.kind == 1)
5608 gfc_set_constant_character_len (
5609 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
5615 gfc_free_expr (init);
5620 /* Match a parameter statement, with the weird syntax that these have. */
5623 gfc_match_parameter (void)
5627 if (gfc_match_char ('(') == MATCH_NO)
5636 if (gfc_match (" )%t") == MATCH_YES)
5639 if (gfc_match_char (',') != MATCH_YES)
5641 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5651 /* Save statements have a special syntax. */
5654 gfc_match_save (void)
5656 char n[GFC_MAX_SYMBOL_LEN+1];
5661 if (gfc_match_eos () == MATCH_YES)
5663 if (gfc_current_ns->seen_save)
5665 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5666 "follows previous SAVE statement")
5671 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5675 if (gfc_current_ns->save_all)
5677 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5678 "blanket SAVE statement")
5687 m = gfc_match_symbol (&sym, 0);
5691 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5703 m = gfc_match (" / %n /", &n);
5704 if (m == MATCH_ERROR)
5709 c = gfc_get_common (n, 0);
5712 gfc_current_ns->seen_save = 1;
5715 if (gfc_match_eos () == MATCH_YES)
5717 if (gfc_match_char (',') != MATCH_YES)
5724 gfc_error ("Syntax error in SAVE statement at %C");
5730 gfc_match_value (void)
5735 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
5739 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5744 if (gfc_match_eos () == MATCH_YES)
5749 m = gfc_match_symbol (&sym, 0);
5753 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5766 if (gfc_match_eos () == MATCH_YES)
5768 if (gfc_match_char (',') != MATCH_YES)
5775 gfc_error ("Syntax error in VALUE statement at %C");
5781 gfc_match_volatile (void)
5786 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
5790 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5795 if (gfc_match_eos () == MATCH_YES)
5800 /* VOLATILE is special because it can be added to host-associated
5802 m = gfc_match_symbol (&sym, 1);
5806 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
5819 if (gfc_match_eos () == MATCH_YES)
5821 if (gfc_match_char (',') != MATCH_YES)
5828 gfc_error ("Syntax error in VOLATILE statement at %C");
5833 /* Match a module procedure statement. Note that we have to modify
5834 symbols in the parent's namespace because the current one was there
5835 to receive symbols that are in an interface's formal argument list. */
5838 gfc_match_modproc (void)
5840 char name[GFC_MAX_SYMBOL_LEN + 1];
5843 gfc_namespace *module_ns;
5845 if (gfc_state_stack->state != COMP_INTERFACE
5846 || gfc_state_stack->previous == NULL
5847 || current_interface.type == INTERFACE_NAMELESS
5848 || current_interface.type == INTERFACE_ABSTRACT)
5850 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
5855 module_ns = gfc_current_ns->parent;
5856 for (; module_ns; module_ns = module_ns->parent)
5857 if (module_ns->proc_name->attr.flavor == FL_MODULE)
5860 if (module_ns == NULL)
5865 m = gfc_match_name (name);
5871 if (gfc_get_symbol (name, module_ns, &sym))
5874 if (sym->attr.proc != PROC_MODULE
5875 && gfc_add_procedure (&sym->attr, PROC_MODULE,
5876 sym->name, NULL) == FAILURE)
5879 if (gfc_add_interface (sym) == FAILURE)
5882 sym->attr.mod_proc = 1;
5884 if (gfc_match_eos () == MATCH_YES)
5886 if (gfc_match_char (',') != MATCH_YES)
5893 gfc_syntax_error (ST_MODULE_PROC);
5898 /* Match the optional attribute specifiers for a type declaration.
5899 Return MATCH_ERROR if an error is encountered in one of the handled
5900 attributes (public, private, bind(c)), MATCH_NO if what's found is
5901 not a handled attribute, and MATCH_YES otherwise. TODO: More error
5902 checking on attribute conflicts needs to be done. */
5905 gfc_get_type_attr_spec (symbol_attribute *attr)
5907 /* See if the derived type is marked as private. */
5908 if (gfc_match (" , private") == MATCH_YES)
5910 if (gfc_current_state () != COMP_MODULE)
5912 gfc_error ("Derived type at %C can only be PRIVATE in the "
5913 "specification part of a module");
5917 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
5920 else if (gfc_match (" , public") == MATCH_YES)
5922 if (gfc_current_state () != COMP_MODULE)
5924 gfc_error ("Derived type at %C can only be PUBLIC in the "
5925 "specification part of a module");
5929 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
5932 else if (gfc_match(" , bind ( c )") == MATCH_YES)
5934 /* If the type is defined to be bind(c) it then needs to make
5935 sure that all fields are interoperable. This will
5936 need to be a semantic check on the finished derived type.
5937 See 15.2.3 (lines 9-12) of F2003 draft. */
5938 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
5941 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
5946 /* If we get here, something matched. */
5951 /* Match the beginning of a derived type declaration. If a type name
5952 was the result of a function, then it is possible to have a symbol
5953 already to be known as a derived type yet have no components. */
5956 gfc_match_derived_decl (void)
5958 char name[GFC_MAX_SYMBOL_LEN + 1];
5959 symbol_attribute attr;
5962 match is_type_attr_spec = MATCH_NO;
5963 bool seen_attr = false;
5965 if (gfc_current_state () == COMP_DERIVED)
5968 gfc_clear_attr (&attr);
5972 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
5973 if (is_type_attr_spec == MATCH_ERROR)
5975 if (is_type_attr_spec == MATCH_YES)
5977 } while (is_type_attr_spec == MATCH_YES);
5979 if (gfc_match (" ::") != MATCH_YES && seen_attr)
5981 gfc_error ("Expected :: in TYPE definition at %C");
5985 m = gfc_match (" %n%t", name);
5989 /* Make sure the name is not the name of an intrinsic type. */
5990 if (gfc_is_intrinsic_typename (name))
5992 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
5997 if (gfc_get_symbol (name, NULL, &sym))
6000 if (sym->ts.type != BT_UNKNOWN)
6002 gfc_error ("Derived type name '%s' at %C already has a basic type "
6003 "of %s", sym->name, gfc_typename (&sym->ts));
6007 /* The symbol may already have the derived attribute without the
6008 components. The ways this can happen is via a function
6009 definition, an INTRINSIC statement or a subtype in another
6010 derived type that is a pointer. The first part of the AND clause
6011 is true if a the symbol is not the return value of a function. */
6012 if (sym->attr.flavor != FL_DERIVED
6013 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6016 if (sym->components != NULL || sym->attr.zero_comp)
6018 gfc_error ("Derived type definition of '%s' at %C has already been "
6019 "defined", sym->name);
6023 if (attr.access != ACCESS_UNKNOWN
6024 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6027 /* See if the derived type was labeled as bind(c). */
6028 if (attr.is_bind_c != 0)
6029 sym->attr.is_bind_c = attr.is_bind_c;
6031 gfc_new_block = sym;
6037 /* Cray Pointees can be declared as:
6038 pointer (ipt, a (n,m,...,*))
6039 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6040 cheat and set a constant bound of 1 for the last dimension, if this
6041 is the case. Since there is no bounds-checking for Cray Pointees,
6042 this will be okay. */
6045 gfc_mod_pointee_as (gfc_array_spec *as)
6047 as->cray_pointee = true; /* This will be useful to know later. */
6048 if (as->type == AS_ASSUMED_SIZE)
6050 as->type = AS_EXPLICIT;
6051 as->upper[as->rank - 1] = gfc_int_expr (1);
6052 as->cp_was_assumed = true;
6054 else if (as->type == AS_ASSUMED_SHAPE)
6056 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6063 /* Match the enum definition statement, here we are trying to match
6064 the first line of enum definition statement.
6065 Returns MATCH_YES if match is found. */
6068 gfc_match_enum (void)
6072 m = gfc_match_eos ();
6076 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
6084 /* Match a variable name with an optional initializer. When this
6085 subroutine is called, a variable is expected to be parsed next.
6086 Depending on what is happening at the moment, updates either the
6087 symbol table or the current interface. */
6090 enumerator_decl (void)
6092 char name[GFC_MAX_SYMBOL_LEN + 1];
6093 gfc_expr *initializer;
6094 gfc_array_spec *as = NULL;
6102 old_locus = gfc_current_locus;
6104 /* When we get here, we've just matched a list of attributes and
6105 maybe a type and a double colon. The next thing we expect to see
6106 is the name of the symbol. */
6107 m = gfc_match_name (name);
6111 var_locus = gfc_current_locus;
6113 /* OK, we've successfully matched the declaration. Now put the
6114 symbol in the current namespace. If we fail to create the symbol,
6116 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6122 /* The double colon must be present in order to have initializers.
6123 Otherwise the statement is ambiguous with an assignment statement. */
6126 if (gfc_match_char ('=') == MATCH_YES)
6128 m = gfc_match_init_expr (&initializer);
6131 gfc_error ("Expected an initialization expression at %C");
6140 /* If we do not have an initializer, the initialization value of the
6141 previous enumerator (stored in last_initializer) is incremented
6142 by 1 and is used to initialize the current enumerator. */
6143 if (initializer == NULL)
6144 initializer = gfc_enum_initializer (last_initializer, old_locus);
6146 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6148 gfc_error("ENUMERATOR %L not initialized with integer expression",
6151 gfc_free_enum_history ();
6155 /* Store this current initializer, for the next enumerator variable
6156 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6157 use last_initializer below. */
6158 last_initializer = initializer;
6159 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6161 /* Maintain enumerator history. */
6162 gfc_find_symbol (name, NULL, 0, &sym);
6163 create_enum_history (sym, last_initializer);
6165 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6168 /* Free stuff up and return. */
6169 gfc_free_expr (initializer);
6175 /* Match the enumerator definition statement. */
6178 gfc_match_enumerator_def (void)
6183 gfc_clear_ts (¤t_ts);
6185 m = gfc_match (" enumerator");
6189 m = gfc_match (" :: ");
6190 if (m == MATCH_ERROR)
6193 colon_seen = (m == MATCH_YES);
6195 if (gfc_current_state () != COMP_ENUM)
6197 gfc_error ("ENUM definition statement expected before %C");
6198 gfc_free_enum_history ();
6202 (¤t_ts)->type = BT_INTEGER;
6203 (¤t_ts)->kind = gfc_c_int_kind;
6205 gfc_clear_attr (¤t_attr);
6206 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
6215 m = enumerator_decl ();
6216 if (m == MATCH_ERROR)
6221 if (gfc_match_eos () == MATCH_YES)
6223 if (gfc_match_char (',') != MATCH_YES)
6227 if (gfc_current_state () == COMP_ENUM)
6229 gfc_free_enum_history ();
6230 gfc_error ("Syntax error in ENUMERATOR definition at %C");
6235 gfc_free_array_spec (current_as);