1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008
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 bool gfc_matching_function;
92 /********************* DATA statement subroutines *********************/
94 static bool in_match_data = false;
97 gfc_in_match_data (void)
103 set_in_match_data (bool set_value)
105 in_match_data = set_value;
108 /* Free a gfc_data_variable structure and everything beneath it. */
111 free_variable (gfc_data_variable *p)
113 gfc_data_variable *q;
118 gfc_free_expr (p->expr);
119 gfc_free_iterator (&p->iter, 0);
120 free_variable (p->list);
126 /* Free a gfc_data_value structure and everything beneath it. */
129 free_value (gfc_data_value *p)
136 gfc_free_expr (p->expr);
142 /* Free a list of gfc_data structures. */
145 gfc_free_data (gfc_data *p)
152 free_variable (p->var);
153 free_value (p->value);
159 /* Free all data in a namespace. */
162 gfc_free_data_all (gfc_namespace *ns)
175 static match var_element (gfc_data_variable *);
177 /* Match a list of variables terminated by an iterator and a right
181 var_list (gfc_data_variable *parent)
183 gfc_data_variable *tail, var;
186 m = var_element (&var);
187 if (m == MATCH_ERROR)
192 tail = gfc_get_data_variable ();
199 if (gfc_match_char (',') != MATCH_YES)
202 m = gfc_match_iterator (&parent->iter, 1);
205 if (m == MATCH_ERROR)
208 m = var_element (&var);
209 if (m == MATCH_ERROR)
214 tail->next = gfc_get_data_variable ();
220 if (gfc_match_char (')') != MATCH_YES)
225 gfc_syntax_error (ST_DATA);
230 /* Match a single element in a data variable list, which can be a
231 variable-iterator list. */
234 var_element (gfc_data_variable *new)
239 memset (new, 0, sizeof (gfc_data_variable));
241 if (gfc_match_char ('(') == MATCH_YES)
242 return var_list (new);
244 m = gfc_match_variable (&new->expr, 0);
248 sym = new->expr->symtree->n.sym;
250 if (!sym->attr.function && gfc_current_ns->parent
251 && gfc_current_ns->parent == sym->ns)
253 gfc_error ("Host associated variable '%s' may not be in the DATA "
254 "statement at %C", sym->name);
258 if (gfc_current_state () != COMP_BLOCK_DATA
259 && sym->attr.in_common
260 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
261 "common block variable '%s' in DATA statement at %C",
262 sym->name) == FAILURE)
265 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
272 /* Match the top-level list of data variables. */
275 top_var_list (gfc_data *d)
277 gfc_data_variable var, *tail, *new;
284 m = var_element (&var);
287 if (m == MATCH_ERROR)
290 new = gfc_get_data_variable ();
300 if (gfc_match_char ('/') == MATCH_YES)
302 if (gfc_match_char (',') != MATCH_YES)
309 gfc_syntax_error (ST_DATA);
310 gfc_free_data_all (gfc_current_ns);
316 match_data_constant (gfc_expr **result)
318 char name[GFC_MAX_SYMBOL_LEN + 1];
324 m = gfc_match_literal_constant (&expr, 1);
331 if (m == MATCH_ERROR)
334 m = gfc_match_null (result);
338 old_loc = gfc_current_locus;
340 /* Should this be a structure component, try to match it
341 before matching a name. */
342 m = gfc_match_rvalue (result);
343 if (m == MATCH_ERROR)
346 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
348 if (gfc_simplify_expr (*result, 0) == FAILURE)
353 gfc_current_locus = old_loc;
355 m = gfc_match_name (name);
359 if (gfc_find_symbol (name, NULL, 1, &sym))
363 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
365 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
369 else if (sym->attr.flavor == FL_DERIVED)
370 return gfc_match_structure_constructor (sym, result);
372 /* Check to see if the value is an initialization array expression. */
373 if (sym->value->expr_type == EXPR_ARRAY)
375 gfc_current_locus = old_loc;
377 m = gfc_match_init_expr (result);
378 if (m == MATCH_ERROR)
383 if (gfc_simplify_expr (*result, 0) == FAILURE)
386 if ((*result)->expr_type == EXPR_CONSTANT)
390 gfc_error ("Invalid initializer %s in Data statement at %C", name);
396 *result = gfc_copy_expr (sym->value);
401 /* Match a list of values in a DATA statement. The leading '/' has
402 already been seen at this point. */
405 top_val_list (gfc_data *data)
407 gfc_data_value *new, *tail;
415 m = match_data_constant (&expr);
418 if (m == MATCH_ERROR)
421 new = gfc_get_data_value ();
422 mpz_init (new->repeat);
431 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
434 mpz_set_ui (tail->repeat, 1);
438 if (expr->ts.type == BT_INTEGER)
439 mpz_set (tail->repeat, expr->value.integer);
440 gfc_free_expr (expr);
442 m = match_data_constant (&tail->expr);
445 if (m == MATCH_ERROR)
449 if (gfc_match_char ('/') == MATCH_YES)
451 if (gfc_match_char (',') == MATCH_NO)
458 gfc_syntax_error (ST_DATA);
459 gfc_free_data_all (gfc_current_ns);
464 /* Matches an old style initialization. */
467 match_old_style_init (const char *name)
474 /* Set up data structure to hold initializers. */
475 gfc_find_sym_tree (name, NULL, 0, &st);
478 newdata = gfc_get_data ();
479 newdata->var = gfc_get_data_variable ();
480 newdata->var->expr = gfc_get_variable_expr (st);
481 newdata->where = gfc_current_locus;
483 /* Match initial value list. This also eats the terminal '/'. */
484 m = top_val_list (newdata);
493 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
498 /* Mark the variable as having appeared in a data statement. */
499 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
505 /* Chain in namespace list of DATA initializers. */
506 newdata->next = gfc_current_ns->data;
507 gfc_current_ns->data = newdata;
513 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
514 we are matching a DATA statement and are therefore issuing an error
515 if we encounter something unexpected, if not, we're trying to match
516 an old-style initialization expression of the form INTEGER I /2/. */
519 gfc_match_data (void)
524 set_in_match_data (true);
528 new = gfc_get_data ();
529 new->where = gfc_current_locus;
531 m = top_var_list (new);
535 m = top_val_list (new);
539 new->next = gfc_current_ns->data;
540 gfc_current_ns->data = new;
542 if (gfc_match_eos () == MATCH_YES)
545 gfc_match_char (','); /* Optional comma */
548 set_in_match_data (false);
552 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
559 set_in_match_data (false);
565 /************************ Declaration statements *********************/
567 /* Match an intent specification. Since this can only happen after an
568 INTENT word, a legal intent-spec must follow. */
571 match_intent_spec (void)
574 if (gfc_match (" ( in out )") == MATCH_YES)
576 if (gfc_match (" ( in )") == MATCH_YES)
578 if (gfc_match (" ( out )") == MATCH_YES)
581 gfc_error ("Bad INTENT specification at %C");
582 return INTENT_UNKNOWN;
586 /* Matches a character length specification, which is either a
587 specification expression or a '*'. */
590 char_len_param_value (gfc_expr **expr)
594 if (gfc_match_char ('*') == MATCH_YES)
600 m = gfc_match_expr (expr);
601 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
603 if ((*expr)->value.function.actual
604 && (*expr)->value.function.actual->expr->symtree)
607 e = (*expr)->value.function.actual->expr;
608 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
609 && e->expr_type == EXPR_VARIABLE)
611 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
613 if (e->symtree->n.sym->ts.type == BT_CHARACTER
614 && e->symtree->n.sym->ts.cl
615 && e->symtree->n.sym->ts.cl->length->ts.type == BT_UNKNOWN)
623 gfc_error ("Conflict in attributes of function argument at %C");
628 /* A character length is a '*' followed by a literal integer or a
629 char_len_param_value in parenthesis. */
632 match_char_length (gfc_expr **expr)
637 m = gfc_match_char ('*');
641 m = gfc_match_small_literal_int (&length, NULL);
642 if (m == MATCH_ERROR)
647 *expr = gfc_int_expr (length);
651 if (gfc_match_char ('(') == MATCH_NO)
654 m = char_len_param_value (expr);
655 if (m != MATCH_YES && gfc_matching_function)
661 if (m == MATCH_ERROR)
666 if (gfc_match_char (')') == MATCH_NO)
668 gfc_free_expr (*expr);
676 gfc_error ("Syntax error in character length specification at %C");
681 /* Special subroutine for finding a symbol. Check if the name is found
682 in the current name space. If not, and we're compiling a function or
683 subroutine and the parent compilation unit is an interface, then check
684 to see if the name we've been given is the name of the interface
685 (located in another namespace). */
688 find_special (const char *name, gfc_symbol **result)
693 i = gfc_get_symbol (name, NULL, result);
697 if (gfc_current_state () != COMP_SUBROUTINE
698 && gfc_current_state () != COMP_FUNCTION)
701 s = gfc_state_stack->previous;
705 if (s->state != COMP_INTERFACE)
708 goto end; /* Nameless interface. */
710 if (strcmp (name, s->sym->name) == 0)
721 /* Special subroutine for getting a symbol node associated with a
722 procedure name, used in SUBROUTINE and FUNCTION statements. The
723 symbol is created in the parent using with symtree node in the
724 child unit pointing to the symbol. If the current namespace has no
725 parent, then the symbol is just created in the current unit. */
728 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
734 /* Module functions have to be left in their own namespace because
735 they have potentially (almost certainly!) already been referenced.
736 In this sense, they are rather like external functions. This is
737 fixed up in resolve.c(resolve_entries), where the symbol name-
738 space is set to point to the master function, so that the fake
739 result mechanism can work. */
740 if (module_fcn_entry)
742 /* Present if entry is declared to be a module procedure. */
743 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
746 rc = gfc_get_symbol (name, NULL, result);
747 else if (!gfc_get_symbol (name, NULL, &sym) && sym
748 && (*result)->ts.type == BT_UNKNOWN
749 && sym->attr.flavor == FL_UNKNOWN)
750 /* Pick up the typespec for the entry, if declared in the function
751 body. Note that this symbol is FL_UNKNOWN because it will
752 only have appeared in a type declaration. The local symtree
753 is set to point to the module symbol and a unique symtree
754 to the local version. This latter ensures a correct clearing
757 /* If the ENTRY proceeds its specification, we need to ensure
758 that this does not raise a "has no IMPLICIT type" error. */
759 if (sym->ts.type == BT_UNKNOWN)
760 sym->attr.untyped = 1;
762 (*result)->ts = sym->ts;
764 /* Put the symbol in the procedure namespace so that, should
765 the ENTRY preceed its specification, the specification
767 (*result)->ns = gfc_current_ns;
769 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
771 st = gfc_get_unique_symtree (gfc_current_ns);
776 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
782 gfc_current_ns->refs++;
784 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
786 /* Trap another encompassed procedure with the same name. All
787 these conditions are necessary to avoid picking up an entry
788 whose name clashes with that of the encompassing procedure;
789 this is handled using gsymbols to register unique,globally
791 if (sym->attr.flavor != 0
792 && sym->attr.proc != 0
793 && (sym->attr.subroutine || sym->attr.function)
794 && sym->attr.if_source != IFSRC_UNKNOWN)
795 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
796 name, &sym->declared_at);
798 /* Trap a procedure with a name the same as interface in the
799 encompassing scope. */
800 if (sym->attr.generic != 0
801 && (sym->attr.subroutine || sym->attr.function)
802 && !sym->attr.mod_proc)
803 gfc_error_now ("Name '%s' at %C is already defined"
804 " as a generic interface at %L",
805 name, &sym->declared_at);
807 /* Trap declarations of attributes in encompassing scope. The
808 signature for this is that ts.kind is set. Legitimate
809 references only set ts.type. */
810 if (sym->ts.kind != 0
811 && !sym->attr.implicit_type
812 && sym->attr.proc == 0
813 && gfc_current_ns->parent != NULL
814 && sym->attr.access == 0
815 && !module_fcn_entry)
816 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
817 "and must not have attributes declared at %L",
818 name, &sym->declared_at);
821 if (gfc_current_ns->parent == NULL || *result == NULL)
824 /* Module function entries will already have a symtree in
825 the current namespace but will need one at module level. */
826 if (module_fcn_entry)
828 /* Present if entry is declared to be a module procedure. */
829 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
831 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
834 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
839 /* See if the procedure should be a module procedure. */
841 if (((sym->ns->proc_name != NULL
842 && sym->ns->proc_name->attr.flavor == FL_MODULE
843 && sym->attr.proc != PROC_MODULE)
844 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
845 && gfc_add_procedure (&sym->attr, PROC_MODULE,
846 sym->name, NULL) == FAILURE)
853 /* Verify that the given symbol representing a parameter is C
854 interoperable, by checking to see if it was marked as such after
855 its declaration. If the given symbol is not interoperable, a
856 warning is reported, thus removing the need to return the status to
857 the calling function. The standard does not require the user use
858 one of the iso_c_binding named constants to declare an
859 interoperable parameter, but we can't be sure if the param is C
860 interop or not if the user doesn't. For example, integer(4) may be
861 legal Fortran, but doesn't have meaning in C. It may interop with
862 a number of the C types, which causes a problem because the
863 compiler can't know which one. This code is almost certainly not
864 portable, and the user will get what they deserve if the C type
865 across platforms isn't always interoperable with integer(4). If
866 the user had used something like integer(c_int) or integer(c_long),
867 the compiler could have automatically handled the varying sizes
871 verify_c_interop_param (gfc_symbol *sym)
873 int is_c_interop = 0;
874 try retval = SUCCESS;
876 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
877 Don't repeat the checks here. */
878 if (sym->attr.implicit_type)
881 /* For subroutines or functions that are passed to a BIND(C) procedure,
882 they're interoperable if they're BIND(C) and their params are all
884 if (sym->attr.flavor == FL_PROCEDURE)
886 if (sym->attr.is_bind_c == 0)
888 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
889 "attribute to be C interoperable", sym->name,
890 &(sym->declared_at));
896 if (sym->attr.is_c_interop == 1)
897 /* We've already checked this procedure; don't check it again. */
900 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
905 /* See if we've stored a reference to a procedure that owns sym. */
906 if (sym->ns != NULL && sym->ns->proc_name != NULL)
908 if (sym->ns->proc_name->attr.is_bind_c == 1)
911 (verify_c_interop (&(sym->ts), sym->name, &(sym->declared_at))
914 if (is_c_interop != 1)
916 /* Make personalized messages to give better feedback. */
917 if (sym->ts.type == BT_DERIVED)
918 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
919 " procedure '%s' but is not C interoperable "
920 "because derived type '%s' is not C interoperable",
921 sym->name, &(sym->declared_at),
922 sym->ns->proc_name->name,
923 sym->ts.derived->name);
925 gfc_warning ("Variable '%s' at %L is a parameter to the "
926 "BIND(C) procedure '%s' but may not be C "
928 sym->name, &(sym->declared_at),
929 sym->ns->proc_name->name);
932 /* Character strings are only C interoperable if they have a
934 if (sym->ts.type == BT_CHARACTER)
936 gfc_charlen *cl = sym->ts.cl;
937 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
938 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
940 gfc_error ("Character argument '%s' at %L "
941 "must be length 1 because "
942 "procedure '%s' is BIND(C)",
943 sym->name, &sym->declared_at,
944 sym->ns->proc_name->name);
949 /* We have to make sure that any param to a bind(c) routine does
950 not have the allocatable, pointer, or optional attributes,
951 according to J3/04-007, section 5.1. */
952 if (sym->attr.allocatable == 1)
954 gfc_error ("Variable '%s' at %L cannot have the "
955 "ALLOCATABLE attribute because procedure '%s'"
956 " is BIND(C)", sym->name, &(sym->declared_at),
957 sym->ns->proc_name->name);
961 if (sym->attr.pointer == 1)
963 gfc_error ("Variable '%s' at %L cannot have the "
964 "POINTER attribute because procedure '%s'"
965 " is BIND(C)", sym->name, &(sym->declared_at),
966 sym->ns->proc_name->name);
970 if (sym->attr.optional == 1)
972 gfc_error ("Variable '%s' at %L cannot have the "
973 "OPTIONAL attribute because procedure '%s'"
974 " is BIND(C)", sym->name, &(sym->declared_at),
975 sym->ns->proc_name->name);
979 /* Make sure that if it has the dimension attribute, that it is
980 either assumed size or explicit shape. */
983 if (sym->as->type == AS_ASSUMED_SHAPE)
985 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
986 "argument to the procedure '%s' at %L because "
987 "the procedure is BIND(C)", sym->name,
988 &(sym->declared_at), sym->ns->proc_name->name,
989 &(sym->ns->proc_name->declared_at));
993 if (sym->as->type == AS_DEFERRED)
995 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
996 "argument to the procedure '%s' at %L because "
997 "the procedure is BIND(C)", sym->name,
998 &(sym->declared_at), sym->ns->proc_name->name,
999 &(sym->ns->proc_name->declared_at));
1010 /* Function called by variable_decl() that adds a name to the symbol table. */
1013 build_sym (const char *name, gfc_charlen *cl,
1014 gfc_array_spec **as, locus *var_locus)
1016 symbol_attribute attr;
1019 if (gfc_get_symbol (name, NULL, &sym))
1022 /* Start updating the symbol table. Add basic type attribute if present. */
1023 if (current_ts.type != BT_UNKNOWN
1024 && (sym->attr.implicit_type == 0
1025 || !gfc_compare_types (&sym->ts, ¤t_ts))
1026 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
1029 if (sym->ts.type == BT_CHARACTER)
1032 /* Add dimension attribute if present. */
1033 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1037 /* Add attribute to symbol. The copy is so that we can reset the
1038 dimension attribute. */
1039 attr = current_attr;
1042 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1045 /* Finish any work that may need to be done for the binding label,
1046 if it's a bind(c). The bind(c) attr is found before the symbol
1047 is made, and before the symbol name (for data decls), so the
1048 current_ts is holding the binding label, or nothing if the
1049 name= attr wasn't given. Therefore, test here if we're dealing
1050 with a bind(c) and make sure the binding label is set correctly. */
1051 if (sym->attr.is_bind_c == 1)
1053 if (sym->binding_label[0] == '\0')
1055 /* Set the binding label and verify that if a NAME= was specified
1056 then only one identifier was in the entity-decl-list. */
1057 if (set_binding_label (sym->binding_label, sym->name,
1058 num_idents_on_line) == FAILURE)
1063 /* See if we know we're in a common block, and if it's a bind(c)
1064 common then we need to make sure we're an interoperable type. */
1065 if (sym->attr.in_common == 1)
1067 /* Test the common block object. */
1068 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1069 && sym->ts.is_c_interop != 1)
1071 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1072 "must be declared with a C interoperable "
1073 "kind since common block '%s' is BIND(C)",
1074 sym->name, sym->common_block->name,
1075 sym->common_block->name);
1080 sym->attr.implied_index = 0;
1086 /* Set character constant to the given length. The constant will be padded or
1090 gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
1095 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1096 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
1098 slen = expr->value.character.length;
1101 s = gfc_getmem (len + 1);
1102 memcpy (s, expr->value.character.string, MIN (len, slen));
1104 memset (&s[slen], ' ', len - slen);
1106 if (gfc_option.warn_character_truncation && slen > len)
1107 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1108 "(%d/%d)", &expr->where, slen, len);
1110 /* Apply the standard by 'hand' otherwise it gets cleared for
1112 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
1113 gfc_error_now ("The CHARACTER elements of the array constructor "
1114 "at %L must have the same length (%d/%d)",
1115 &expr->where, slen, len);
1118 gfc_free (expr->value.character.string);
1119 expr->value.character.string = s;
1120 expr->value.character.length = len;
1125 /* Function to create and update the enumerator history
1126 using the information passed as arguments.
1127 Pointer "max_enum" is also updated, to point to
1128 enum history node containing largest initializer.
1130 SYM points to the symbol node of enumerator.
1131 INIT points to its enumerator value. */
1134 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1136 enumerator_history *new_enum_history;
1137 gcc_assert (sym != NULL && init != NULL);
1139 new_enum_history = gfc_getmem (sizeof (enumerator_history));
1141 new_enum_history->sym = sym;
1142 new_enum_history->initializer = init;
1143 new_enum_history->next = NULL;
1145 if (enum_history == NULL)
1147 enum_history = new_enum_history;
1148 max_enum = enum_history;
1152 new_enum_history->next = enum_history;
1153 enum_history = new_enum_history;
1155 if (mpz_cmp (max_enum->initializer->value.integer,
1156 new_enum_history->initializer->value.integer) < 0)
1157 max_enum = new_enum_history;
1162 /* Function to free enum kind history. */
1165 gfc_free_enum_history (void)
1167 enumerator_history *current = enum_history;
1168 enumerator_history *next;
1170 while (current != NULL)
1172 next = current->next;
1177 enum_history = NULL;
1181 /* Function called by variable_decl() that adds an initialization
1182 expression to a symbol. */
1185 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1187 symbol_attribute attr;
1192 if (find_special (name, &sym))
1197 /* If this symbol is confirming an implicit parameter type,
1198 then an initialization expression is not allowed. */
1199 if (attr.flavor == FL_PARAMETER
1200 && sym->value != NULL
1203 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1210 /* An initializer is required for PARAMETER declarations. */
1211 if (attr.flavor == FL_PARAMETER)
1213 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1219 /* If a variable appears in a DATA block, it cannot have an
1223 gfc_error ("Variable '%s' at %C with an initializer already "
1224 "appears in a DATA statement", sym->name);
1228 /* Check if the assignment can happen. This has to be put off
1229 until later for a derived type variable. */
1230 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1231 && gfc_check_assign_symbol (sym, init) == FAILURE)
1234 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
1236 /* Update symbol character length according initializer. */
1237 if (sym->ts.cl->length == NULL)
1240 /* If there are multiple CHARACTER variables declared on the
1241 same line, we don't want them to share the same length. */
1242 sym->ts.cl = gfc_get_charlen ();
1243 sym->ts.cl->next = gfc_current_ns->cl_list;
1244 gfc_current_ns->cl_list = sym->ts.cl;
1246 if (sym->attr.flavor == FL_PARAMETER)
1248 if (init->expr_type == EXPR_CONSTANT)
1250 clen = init->value.character.length;
1251 sym->ts.cl->length = gfc_int_expr (clen);
1253 else if (init->expr_type == EXPR_ARRAY)
1255 gfc_expr *p = init->value.constructor->expr;
1256 clen = p->value.character.length;
1257 sym->ts.cl->length = gfc_int_expr (clen);
1259 else if (init->ts.cl && init->ts.cl->length)
1260 sym->ts.cl->length =
1261 gfc_copy_expr (sym->value->ts.cl->length);
1264 /* Update initializer character length according symbol. */
1265 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
1267 int len = mpz_get_si (sym->ts.cl->length->value.integer);
1268 gfc_constructor * p;
1270 if (init->expr_type == EXPR_CONSTANT)
1271 gfc_set_constant_character_len (len, init, false);
1272 else if (init->expr_type == EXPR_ARRAY)
1274 /* Build a new charlen to prevent simplification from
1275 deleting the length before it is resolved. */
1276 init->ts.cl = gfc_get_charlen ();
1277 init->ts.cl->next = gfc_current_ns->cl_list;
1278 gfc_current_ns->cl_list = sym->ts.cl;
1279 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
1281 for (p = init->value.constructor; p; p = p->next)
1282 gfc_set_constant_character_len (len, p->expr, false);
1287 /* Need to check if the expression we initialized this
1288 to was one of the iso_c_binding named constants. If so,
1289 and we're a parameter (constant), let it be iso_c.
1291 integer(c_int), parameter :: my_int = c_int
1292 integer(my_int) :: my_int_2
1293 If we mark my_int as iso_c (since we can see it's value
1294 is equal to one of the named constants), then my_int_2
1295 will be considered C interoperable. */
1296 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1298 sym->ts.is_iso_c |= init->ts.is_iso_c;
1299 sym->ts.is_c_interop |= init->ts.is_c_interop;
1300 /* attr bits needed for module files. */
1301 sym->attr.is_iso_c |= init->ts.is_iso_c;
1302 sym->attr.is_c_interop |= init->ts.is_c_interop;
1303 if (init->ts.is_iso_c)
1304 sym->ts.f90_type = init->ts.f90_type;
1307 /* Add initializer. Make sure we keep the ranks sane. */
1308 if (sym->attr.dimension && init->rank == 0)
1314 if (sym->attr.flavor == FL_PARAMETER
1315 && init->expr_type == EXPR_CONSTANT
1316 && spec_size (sym->as, &size) == SUCCESS
1317 && mpz_cmp_si (size, 0) > 0)
1319 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1322 array->value.constructor = c = NULL;
1323 for (n = 0; n < (int)mpz_get_si (size); n++)
1325 if (array->value.constructor == NULL)
1327 array->value.constructor = c = gfc_get_constructor ();
1332 c->next = gfc_get_constructor ();
1334 c->expr = gfc_copy_expr (init);
1338 array->shape = gfc_get_shape (sym->as->rank);
1339 for (n = 0; n < sym->as->rank; n++)
1340 spec_dimen_size (sym->as, n, &array->shape[n]);
1345 init->rank = sym->as->rank;
1349 if (sym->attr.save == SAVE_NONE)
1350 sym->attr.save = SAVE_IMPLICIT;
1358 /* Function called by variable_decl() that adds a name to a structure
1362 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1363 gfc_array_spec **as)
1367 /* If the current symbol is of the same derived type that we're
1368 constructing, it must have the pointer attribute. */
1369 if (current_ts.type == BT_DERIVED
1370 && current_ts.derived == gfc_current_block ()
1371 && current_attr.pointer == 0)
1373 gfc_error ("Component at %C must have the POINTER attribute");
1377 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1379 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1381 gfc_error ("Array component of structure at %C must have explicit "
1382 "or deferred shape");
1387 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1392 gfc_set_component_attr (c, ¤t_attr);
1394 c->initializer = *init;
1402 /* Should this ever get more complicated, combine with similar section
1403 in add_init_expr_to_sym into a separate function. */
1404 if (c->ts.type == BT_CHARACTER && !c->pointer && c->initializer)
1406 int len = mpz_get_si (c->ts.cl->length->value.integer);
1408 if (c->initializer->expr_type == EXPR_CONSTANT)
1409 gfc_set_constant_character_len (len, c->initializer, false);
1410 else if (mpz_cmp (c->ts.cl->length->value.integer,
1411 c->initializer->ts.cl->length->value.integer))
1413 gfc_constructor *ctor = c->initializer->value.constructor;
1414 for (;ctor ; ctor = ctor->next)
1415 if (ctor->expr->expr_type == EXPR_CONSTANT)
1416 gfc_set_constant_character_len (len, ctor->expr, true);
1420 /* Check array components. */
1425 gfc_error ("Allocatable component at %C must be an array");
1434 if (c->as->type != AS_DEFERRED)
1436 gfc_error ("Pointer array component of structure at %C must have a "
1441 else if (c->allocatable)
1443 if (c->as->type != AS_DEFERRED)
1445 gfc_error ("Allocatable component of structure at %C must have a "
1452 if (c->as->type != AS_EXPLICIT)
1454 gfc_error ("Array component of structure at %C must have an "
1464 /* Match a 'NULL()', and possibly take care of some side effects. */
1467 gfc_match_null (gfc_expr **result)
1473 m = gfc_match (" null ( )");
1477 /* The NULL symbol now has to be/become an intrinsic function. */
1478 if (gfc_get_symbol ("null", NULL, &sym))
1480 gfc_error ("NULL() initialization at %C is ambiguous");
1484 gfc_intrinsic_symbol (sym);
1486 if (sym->attr.proc != PROC_INTRINSIC
1487 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1488 sym->name, NULL) == FAILURE
1489 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1492 e = gfc_get_expr ();
1493 e->where = gfc_current_locus;
1494 e->expr_type = EXPR_NULL;
1495 e->ts.type = BT_UNKNOWN;
1503 /* Match a variable name with an optional initializer. When this
1504 subroutine is called, a variable is expected to be parsed next.
1505 Depending on what is happening at the moment, updates either the
1506 symbol table or the current interface. */
1509 variable_decl (int elem)
1511 char name[GFC_MAX_SYMBOL_LEN + 1];
1512 gfc_expr *initializer, *char_len;
1514 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1525 old_locus = gfc_current_locus;
1527 /* When we get here, we've just matched a list of attributes and
1528 maybe a type and a double colon. The next thing we expect to see
1529 is the name of the symbol. */
1530 m = gfc_match_name (name);
1534 var_locus = gfc_current_locus;
1536 /* Now we could see the optional array spec. or character length. */
1537 m = gfc_match_array_spec (&as);
1538 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1539 cp_as = gfc_copy_array_spec (as);
1540 else if (m == MATCH_ERROR)
1544 as = gfc_copy_array_spec (current_as);
1549 if (current_ts.type == BT_CHARACTER)
1551 switch (match_char_length (&char_len))
1554 cl = gfc_get_charlen ();
1555 cl->next = gfc_current_ns->cl_list;
1556 gfc_current_ns->cl_list = cl;
1558 cl->length = char_len;
1561 /* Non-constant lengths need to be copied after the first
1562 element. Also copy assumed lengths. */
1565 && (current_ts.cl->length == NULL
1566 || current_ts.cl->length->expr_type != EXPR_CONSTANT))
1568 cl = gfc_get_charlen ();
1569 cl->next = gfc_current_ns->cl_list;
1570 gfc_current_ns->cl_list = cl;
1571 cl->length = gfc_copy_expr (current_ts.cl->length);
1583 /* If this symbol has already shown up in a Cray Pointer declaration,
1584 then we want to set the type & bail out. */
1585 if (gfc_option.flag_cray_pointer)
1587 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1588 if (sym != NULL && sym->attr.cray_pointee)
1590 sym->ts.type = current_ts.type;
1591 sym->ts.kind = current_ts.kind;
1593 sym->ts.derived = current_ts.derived;
1594 sym->ts.is_c_interop = current_ts.is_c_interop;
1595 sym->ts.is_iso_c = current_ts.is_iso_c;
1598 /* Check to see if we have an array specification. */
1601 if (sym->as != NULL)
1603 gfc_error ("Duplicate array spec for Cray pointee at %C");
1604 gfc_free_array_spec (cp_as);
1610 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1611 gfc_internal_error ("Couldn't set pointee array spec.");
1613 /* Fix the array spec. */
1614 m = gfc_mod_pointee_as (sym->as);
1615 if (m == MATCH_ERROR)
1623 gfc_free_array_spec (cp_as);
1628 /* OK, we've successfully matched the declaration. Now put the
1629 symbol in the current namespace, because it might be used in the
1630 optional initialization expression for this symbol, e.g. this is
1633 integer, parameter :: i = huge(i)
1635 This is only true for parameters or variables of a basic type.
1636 For components of derived types, it is not true, so we don't
1637 create a symbol for those yet. If we fail to create the symbol,
1639 if (gfc_current_state () != COMP_DERIVED
1640 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1646 /* An interface body specifies all of the procedure's
1647 characteristics and these shall be consistent with those
1648 specified in the procedure definition, except that the interface
1649 may specify a procedure that is not pure if the procedure is
1650 defined to be pure(12.3.2). */
1651 if (current_ts.type == BT_DERIVED
1652 && gfc_current_ns->proc_name
1653 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1654 && current_ts.derived->ns != gfc_current_ns)
1657 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.derived->name);
1658 if (!(current_ts.derived->attr.imported
1660 && st->n.sym == current_ts.derived)
1661 && !gfc_current_ns->has_import_set)
1663 gfc_error ("the type of '%s' at %C has not been declared within the "
1670 /* In functions that have a RESULT variable defined, the function
1671 name always refers to function calls. Therefore, the name is
1672 not allowed to appear in specification statements. */
1673 if (gfc_current_state () == COMP_FUNCTION
1674 && gfc_current_block () != NULL
1675 && gfc_current_block ()->result != NULL
1676 && gfc_current_block ()->result != gfc_current_block ()
1677 && strcmp (gfc_current_block ()->name, name) == 0)
1679 gfc_error ("Function name '%s' not allowed at %C", name);
1684 /* We allow old-style initializations of the form
1685 integer i /2/, j(4) /3*3, 1/
1686 (if no colon has been seen). These are different from data
1687 statements in that initializers are only allowed to apply to the
1688 variable immediately preceding, i.e.
1690 is not allowed. Therefore we have to do some work manually, that
1691 could otherwise be left to the matchers for DATA statements. */
1693 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1695 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1696 "initialization at %C") == FAILURE)
1699 return match_old_style_init (name);
1702 /* The double colon must be present in order to have initializers.
1703 Otherwise the statement is ambiguous with an assignment statement. */
1706 if (gfc_match (" =>") == MATCH_YES)
1708 if (!current_attr.pointer)
1710 gfc_error ("Initialization at %C isn't for a pointer variable");
1715 m = gfc_match_null (&initializer);
1718 gfc_error ("Pointer initialization requires a NULL() at %C");
1722 if (gfc_pure (NULL))
1724 gfc_error ("Initialization of pointer at %C is not allowed in "
1725 "a PURE procedure");
1733 else if (gfc_match_char ('=') == MATCH_YES)
1735 if (current_attr.pointer)
1737 gfc_error ("Pointer initialization at %C requires '=>', "
1743 m = gfc_match_init_expr (&initializer);
1746 gfc_error ("Expected an initialization expression at %C");
1750 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1752 gfc_error ("Initialization of variable at %C is not allowed in "
1753 "a PURE procedure");
1762 if (initializer != NULL && current_attr.allocatable
1763 && gfc_current_state () == COMP_DERIVED)
1765 gfc_error ("Initialization of allocatable component at %C is not "
1771 /* Add the initializer. Note that it is fine if initializer is
1772 NULL here, because we sometimes also need to check if a
1773 declaration *must* have an initialization expression. */
1774 if (gfc_current_state () != COMP_DERIVED)
1775 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1778 if (current_ts.type == BT_DERIVED
1779 && !current_attr.pointer && !initializer)
1780 initializer = gfc_default_initializer (¤t_ts);
1781 t = build_struct (name, cl, &initializer, &as);
1784 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1787 /* Free stuff up and return. */
1788 gfc_free_expr (initializer);
1789 gfc_free_array_spec (as);
1795 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1796 This assumes that the byte size is equal to the kind number for
1797 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1800 gfc_match_old_kind_spec (gfc_typespec *ts)
1805 if (gfc_match_char ('*') != MATCH_YES)
1808 m = gfc_match_small_literal_int (&ts->kind, NULL);
1812 original_kind = ts->kind;
1814 /* Massage the kind numbers for complex types. */
1815 if (ts->type == BT_COMPLEX)
1819 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1820 gfc_basic_typename (ts->type), original_kind);
1826 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1828 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1829 gfc_basic_typename (ts->type), original_kind);
1833 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1834 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1841 /* Match a kind specification. Since kinds are generally optional, we
1842 usually return MATCH_NO if something goes wrong. If a "kind="
1843 string is found, then we know we have an error. */
1846 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
1858 where = loc = gfc_current_locus;
1863 if (gfc_match_char ('(') == MATCH_NO)
1866 /* Also gobbles optional text. */
1867 if (gfc_match (" kind = ") == MATCH_YES)
1870 loc = gfc_current_locus;
1873 n = gfc_match_init_expr (&e);
1877 if (gfc_matching_function)
1879 /* The function kind expression might include use associated or
1880 imported parameters and try again after the specification
1882 if (gfc_match_char (')') != MATCH_YES)
1884 gfc_error ("Missing right parenthesis at %C");
1890 gfc_undo_symbols ();
1895 /* ....or else, the match is real. */
1897 gfc_error ("Expected initialization expression at %C");
1905 gfc_error ("Expected scalar initialization expression at %C");
1910 msg = gfc_extract_int (e, &ts->kind);
1919 /* Before throwing away the expression, let's see if we had a
1920 C interoperable kind (and store the fact). */
1921 if (e->ts.is_c_interop == 1)
1923 /* Mark this as c interoperable if being declared with one
1924 of the named constants from iso_c_binding. */
1925 ts->is_c_interop = e->ts.is_iso_c;
1926 ts->f90_type = e->ts.f90_type;
1932 /* Ignore errors to this point, if we've gotten here. This means
1933 we ignore the m=MATCH_ERROR from above. */
1934 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1936 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1937 gfc_basic_typename (ts->type));
1938 gfc_current_locus = where;
1942 gfc_gobble_whitespace ();
1943 if ((c = gfc_next_char ()) != ')' && (ts->type != BT_CHARACTER || c != ','))
1945 if (ts->type == BT_CHARACTER)
1946 gfc_error ("Missing right parenthesis or comma at %C");
1948 gfc_error ("Missing right parenthesis at %C");
1952 /* All tests passed. */
1955 if(m == MATCH_ERROR)
1956 gfc_current_locus = where;
1958 /* Return what we know from the test(s). */
1963 gfc_current_locus = where;
1969 match_char_kind (int * kind, int * is_iso_c)
1978 where = gfc_current_locus;
1980 n = gfc_match_init_expr (&e);
1982 if (n != MATCH_YES && gfc_matching_function)
1984 /* The expression might include use-associated or imported
1985 parameters and try again after the specification
1988 gfc_undo_symbols ();
1993 gfc_error ("Expected initialization expression at %C");
1999 gfc_error ("Expected scalar initialization expression at %C");
2004 msg = gfc_extract_int (e, kind);
2005 *is_iso_c = e->ts.is_iso_c;
2015 /* Ignore errors to this point, if we've gotten here. This means
2016 we ignore the m=MATCH_ERROR from above. */
2017 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2019 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2023 /* All tests passed. */
2026 if (m == MATCH_ERROR)
2027 gfc_current_locus = where;
2029 /* Return what we know from the test(s). */
2034 gfc_current_locus = where;
2038 /* Match the various kind/length specifications in a CHARACTER
2039 declaration. We don't return MATCH_NO. */
2042 match_char_spec (gfc_typespec *ts)
2044 int kind, seen_length, is_iso_c;
2054 /* Try the old-style specification first. */
2055 old_char_selector = 0;
2057 m = match_char_length (&len);
2061 old_char_selector = 1;
2066 m = gfc_match_char ('(');
2069 m = MATCH_YES; /* Character without length is a single char. */
2073 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2074 if (gfc_match (" kind =") == MATCH_YES)
2076 m = match_char_kind (&kind, &is_iso_c);
2078 if (m == MATCH_ERROR)
2083 if (gfc_match (" , len =") == MATCH_NO)
2086 m = char_len_param_value (&len);
2089 if (m == MATCH_ERROR)
2096 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2097 if (gfc_match (" len =") == MATCH_YES)
2099 m = char_len_param_value (&len);
2102 if (m == MATCH_ERROR)
2106 if (gfc_match_char (')') == MATCH_YES)
2109 if (gfc_match (" , kind =") != MATCH_YES)
2112 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2118 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2119 m = char_len_param_value (&len);
2122 if (m == MATCH_ERROR)
2126 m = gfc_match_char (')');
2130 if (gfc_match_char (',') != MATCH_YES)
2133 gfc_match (" kind ="); /* Gobble optional text. */
2135 m = match_char_kind (&kind, &is_iso_c);
2136 if (m == MATCH_ERROR)
2142 /* Require a right-paren at this point. */
2143 m = gfc_match_char (')');
2148 gfc_error ("Syntax error in CHARACTER declaration at %C");
2150 gfc_free_expr (len);
2154 /* Deal with character functions after USE and IMPORT statements. */
2155 if (gfc_matching_function)
2157 gfc_free_expr (len);
2158 gfc_undo_symbols ();
2164 gfc_free_expr (len);
2168 /* Do some final massaging of the length values. */
2169 cl = gfc_get_charlen ();
2170 cl->next = gfc_current_ns->cl_list;
2171 gfc_current_ns->cl_list = cl;
2173 if (seen_length == 0)
2174 cl->length = gfc_int_expr (1);
2179 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2181 /* We have to know if it was a c interoperable kind so we can
2182 do accurate type checking of bind(c) procs, etc. */
2184 /* Mark this as c interoperable if being declared with one
2185 of the named constants from iso_c_binding. */
2186 ts->is_c_interop = is_iso_c;
2187 else if (len != NULL)
2188 /* Here, we might have parsed something such as: character(c_char)
2189 In this case, the parsing code above grabs the c_char when
2190 looking for the length (line 1690, roughly). it's the last
2191 testcase for parsing the kind params of a character variable.
2192 However, it's not actually the length. this seems like it
2194 To see if the user used a C interop kind, test the expr
2195 of the so called length, and see if it's C interoperable. */
2196 ts->is_c_interop = len->ts.is_iso_c;
2202 /* Matches a type specification. If successful, sets the ts structure
2203 to the matched specification. This is necessary for FUNCTION and
2204 IMPLICIT statements.
2206 If implicit_flag is nonzero, then we don't check for the optional
2207 kind specification. Not doing so is needed for matching an IMPLICIT
2208 statement correctly. */
2211 gfc_match_type_spec (gfc_typespec *ts, int implicit_flag)
2213 char name[GFC_MAX_SYMBOL_LEN + 1];
2217 bool seen_deferred_kind;
2219 /* A belt and braces check that the typespec is correctly being treated
2220 as a deferred characteristic association. */
2221 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2222 && (gfc_current_block ()->result->ts.kind == -1)
2223 && (ts->kind == -1);
2225 if (seen_deferred_kind)
2228 /* Clear the current binding label, in case one is given. */
2229 curr_binding_label[0] = '\0';
2231 if (gfc_match (" byte") == MATCH_YES)
2233 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
2237 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2239 gfc_error ("BYTE type used at %C "
2240 "is not available on the target machine");
2244 ts->type = BT_INTEGER;
2249 if (gfc_match (" integer") == MATCH_YES)
2251 ts->type = BT_INTEGER;
2252 ts->kind = gfc_default_integer_kind;
2256 if (gfc_match (" character") == MATCH_YES)
2258 ts->type = BT_CHARACTER;
2259 if (implicit_flag == 0)
2260 return match_char_spec (ts);
2265 if (gfc_match (" real") == MATCH_YES)
2268 ts->kind = gfc_default_real_kind;
2272 if (gfc_match (" double precision") == MATCH_YES)
2275 ts->kind = gfc_default_double_kind;
2279 if (gfc_match (" complex") == MATCH_YES)
2281 ts->type = BT_COMPLEX;
2282 ts->kind = gfc_default_complex_kind;
2286 if (gfc_match (" double complex") == MATCH_YES)
2288 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2289 "conform to the Fortran 95 standard") == FAILURE)
2292 ts->type = BT_COMPLEX;
2293 ts->kind = gfc_default_double_kind;
2297 if (gfc_match (" logical") == MATCH_YES)
2299 ts->type = BT_LOGICAL;
2300 ts->kind = gfc_default_logical_kind;
2304 m = gfc_match (" type ( %n )", name);
2308 ts->type = BT_DERIVED;
2310 /* Defer association of the derived type until the end of the
2311 specification block. However, if the derived type can be
2312 found, add it to the typespec. */
2313 if (gfc_matching_function)
2316 if (gfc_current_state () != COMP_INTERFACE
2317 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2322 /* Search for the name but allow the components to be defined later. If
2323 type = -1, this typespec has been seen in a function declaration but
2324 the type could not be accessed at that point. */
2326 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2328 gfc_error ("Type name '%s' at %C is ambiguous", name);
2331 else if (ts->kind == -1)
2333 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2334 || gfc_current_ns->has_import_set;
2335 if (gfc_find_symbol (name, NULL, iface, &sym))
2337 gfc_error ("Type name '%s' at %C is ambiguous", name);
2346 if (sym->attr.flavor != FL_DERIVED
2347 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2350 gfc_set_sym_referenced (sym);
2356 /* For all types except double, derived and character, look for an
2357 optional kind specifier. MATCH_NO is actually OK at this point. */
2358 if (implicit_flag == 1)
2361 if (gfc_current_form == FORM_FREE)
2363 c = gfc_peek_char();
2364 if (!gfc_is_whitespace(c) && c != '*' && c != '('
2365 && c != ':' && c != ',')
2369 m = gfc_match_kind_spec (ts, false);
2370 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2371 m = gfc_match_old_kind_spec (ts);
2373 /* Defer association of the KIND expression of function results
2374 until after USE and IMPORT statements. */
2375 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2376 || gfc_matching_function)
2380 m = MATCH_YES; /* No kind specifier found. */
2386 /* Match an IMPLICIT NONE statement. Actually, this statement is
2387 already matched in parse.c, or we would not end up here in the
2388 first place. So the only thing we need to check, is if there is
2389 trailing garbage. If not, the match is successful. */
2392 gfc_match_implicit_none (void)
2394 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2398 /* Match the letter range(s) of an IMPLICIT statement. */
2401 match_implicit_range (void)
2403 int c, c1, c2, inner;
2406 cur_loc = gfc_current_locus;
2408 gfc_gobble_whitespace ();
2409 c = gfc_next_char ();
2412 gfc_error ("Missing character range in IMPLICIT at %C");
2419 gfc_gobble_whitespace ();
2420 c1 = gfc_next_char ();
2424 gfc_gobble_whitespace ();
2425 c = gfc_next_char ();
2430 inner = 0; /* Fall through. */
2437 gfc_gobble_whitespace ();
2438 c2 = gfc_next_char ();
2442 gfc_gobble_whitespace ();
2443 c = gfc_next_char ();
2445 if ((c != ',') && (c != ')'))
2458 gfc_error ("Letters must be in alphabetic order in "
2459 "IMPLICIT statement at %C");
2463 /* See if we can add the newly matched range to the pending
2464 implicits from this IMPLICIT statement. We do not check for
2465 conflicts with whatever earlier IMPLICIT statements may have
2466 set. This is done when we've successfully finished matching
2468 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2475 gfc_syntax_error (ST_IMPLICIT);
2477 gfc_current_locus = cur_loc;
2482 /* Match an IMPLICIT statement, storing the types for
2483 gfc_set_implicit() if the statement is accepted by the parser.
2484 There is a strange looking, but legal syntactic construction
2485 possible. It looks like:
2487 IMPLICIT INTEGER (a-b) (c-d)
2489 This is legal if "a-b" is a constant expression that happens to
2490 equal one of the legal kinds for integers. The real problem
2491 happens with an implicit specification that looks like:
2493 IMPLICIT INTEGER (a-b)
2495 In this case, a typespec matcher that is "greedy" (as most of the
2496 matchers are) gobbles the character range as a kindspec, leaving
2497 nothing left. We therefore have to go a bit more slowly in the
2498 matching process by inhibiting the kindspec checking during
2499 typespec matching and checking for a kind later. */
2502 gfc_match_implicit (void)
2511 /* We don't allow empty implicit statements. */
2512 if (gfc_match_eos () == MATCH_YES)
2514 gfc_error ("Empty IMPLICIT statement at %C");
2520 /* First cleanup. */
2521 gfc_clear_new_implicit ();
2523 /* A basic type is mandatory here. */
2524 m = gfc_match_type_spec (&ts, 1);
2525 if (m == MATCH_ERROR)
2530 cur_loc = gfc_current_locus;
2531 m = match_implicit_range ();
2535 /* We may have <TYPE> (<RANGE>). */
2536 gfc_gobble_whitespace ();
2537 c = gfc_next_char ();
2538 if ((c == '\n') || (c == ','))
2540 /* Check for CHARACTER with no length parameter. */
2541 if (ts.type == BT_CHARACTER && !ts.cl)
2543 ts.kind = gfc_default_character_kind;
2544 ts.cl = gfc_get_charlen ();
2545 ts.cl->next = gfc_current_ns->cl_list;
2546 gfc_current_ns->cl_list = ts.cl;
2547 ts.cl->length = gfc_int_expr (1);
2550 /* Record the Successful match. */
2551 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2556 gfc_current_locus = cur_loc;
2559 /* Discard the (incorrectly) matched range. */
2560 gfc_clear_new_implicit ();
2562 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2563 if (ts.type == BT_CHARACTER)
2564 m = match_char_spec (&ts);
2567 m = gfc_match_kind_spec (&ts, false);
2570 m = gfc_match_old_kind_spec (&ts);
2571 if (m == MATCH_ERROR)
2577 if (m == MATCH_ERROR)
2580 m = match_implicit_range ();
2581 if (m == MATCH_ERROR)
2586 gfc_gobble_whitespace ();
2587 c = gfc_next_char ();
2588 if ((c != '\n') && (c != ','))
2591 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2599 gfc_syntax_error (ST_IMPLICIT);
2607 gfc_match_import (void)
2609 char name[GFC_MAX_SYMBOL_LEN + 1];
2614 if (gfc_current_ns->proc_name == NULL
2615 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2617 gfc_error ("IMPORT statement at %C only permitted in "
2618 "an INTERFACE body");
2622 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2626 if (gfc_match_eos () == MATCH_YES)
2628 /* All host variables should be imported. */
2629 gfc_current_ns->has_import_set = 1;
2633 if (gfc_match (" ::") == MATCH_YES)
2635 if (gfc_match_eos () == MATCH_YES)
2637 gfc_error ("Expecting list of named entities at %C");
2644 m = gfc_match (" %n", name);
2648 if (gfc_current_ns->parent != NULL
2649 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2651 gfc_error ("Type name '%s' at %C is ambiguous", name);
2654 else if (gfc_current_ns->proc_name->ns->parent != NULL
2655 && gfc_find_symbol (name,
2656 gfc_current_ns->proc_name->ns->parent,
2659 gfc_error ("Type name '%s' at %C is ambiguous", name);
2665 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2666 "at %C - does not exist.", name);
2670 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2672 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2677 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2680 sym->attr.imported = 1;
2692 if (gfc_match_eos () == MATCH_YES)
2694 if (gfc_match_char (',') != MATCH_YES)
2701 gfc_error ("Syntax error in IMPORT statement at %C");
2706 /* A minimal implementation of gfc_match without whitespace, escape
2707 characters or variable arguments. Returns true if the next
2708 characters match the TARGET template exactly. */
2711 match_string_p (const char *target)
2715 for (p = target; *p; p++)
2716 if (gfc_next_char () != *p)
2721 /* Matches an attribute specification including array specs. If
2722 successful, leaves the variables current_attr and current_as
2723 holding the specification. Also sets the colon_seen variable for
2724 later use by matchers associated with initializations.
2726 This subroutine is a little tricky in the sense that we don't know
2727 if we really have an attr-spec until we hit the double colon.
2728 Until that time, we can only return MATCH_NO. This forces us to
2729 check for duplicate specification at this level. */
2732 match_attr_spec (void)
2734 /* Modifiers that can exist in a type statement. */
2736 { GFC_DECL_BEGIN = 0,
2737 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2738 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2739 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2740 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2741 DECL_IS_BIND_C, DECL_NONE,
2742 GFC_DECL_END /* Sentinel */
2746 /* GFC_DECL_END is the sentinel, index starts at 0. */
2747 #define NUM_DECL GFC_DECL_END
2749 locus start, seen_at[NUM_DECL];
2756 gfc_clear_attr (¤t_attr);
2757 start = gfc_current_locus;
2762 /* See if we get all of the keywords up to the final double colon. */
2763 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2771 gfc_gobble_whitespace ();
2773 ch = gfc_next_char ();
2776 /* This is the successful exit condition for the loop. */
2777 if (gfc_next_char () == ':')
2782 gfc_gobble_whitespace ();
2783 switch (gfc_peek_char ())
2786 if (match_string_p ("allocatable"))
2787 d = DECL_ALLOCATABLE;
2791 /* Try and match the bind(c). */
2792 m = gfc_match_bind_c (NULL, true);
2795 else if (m == MATCH_ERROR)
2800 if (match_string_p ("dimension"))
2805 if (match_string_p ("external"))
2810 if (match_string_p ("int"))
2812 ch = gfc_next_char ();
2815 if (match_string_p ("nt"))
2817 /* Matched "intent". */
2818 /* TODO: Call match_intent_spec from here. */
2819 if (gfc_match (" ( in out )") == MATCH_YES)
2821 else if (gfc_match (" ( in )") == MATCH_YES)
2823 else if (gfc_match (" ( out )") == MATCH_YES)
2829 if (match_string_p ("insic"))
2831 /* Matched "intrinsic". */
2839 if (match_string_p ("optional"))
2845 switch (gfc_next_char ())
2848 if (match_string_p ("rameter"))
2850 /* Matched "parameter". */
2856 if (match_string_p ("inter"))
2858 /* Matched "pointer". */
2864 ch = gfc_next_char ();
2867 if (match_string_p ("vate"))
2869 /* Matched "private". */
2875 if (match_string_p ("tected"))
2877 /* Matched "protected". */
2884 if (match_string_p ("blic"))
2886 /* Matched "public". */
2894 if (match_string_p ("save"))
2899 if (match_string_p ("target"))
2905 ch = gfc_next_char ();
2908 if (match_string_p ("lue"))
2910 /* Matched "value". */
2916 if (match_string_p ("latile"))
2918 /* Matched "volatile". */
2926 /* No double colon and no recognizable decl_type, so assume that
2927 we've been looking at something else the whole time. */
2935 seen_at[d] = gfc_current_locus;
2937 if (d == DECL_DIMENSION)
2939 m = gfc_match_array_spec (¤t_as);
2943 gfc_error ("Missing dimension specification at %C");
2947 if (m == MATCH_ERROR)
2952 /* Since we've seen a double colon, we have to be looking at an
2953 attr-spec. This means that we can now issue errors. */
2954 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2959 case DECL_ALLOCATABLE:
2960 attr = "ALLOCATABLE";
2962 case DECL_DIMENSION:
2969 attr = "INTENT (IN)";
2972 attr = "INTENT (OUT)";
2975 attr = "INTENT (IN OUT)";
2977 case DECL_INTRINSIC:
2983 case DECL_PARAMETER:
2989 case DECL_PROTECTED:
3004 case DECL_IS_BIND_C:
3014 attr = NULL; /* This shouldn't happen. */
3017 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3022 /* Now that we've dealt with duplicate attributes, add the attributes
3023 to the current attribute. */
3024 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3029 if (gfc_current_state () == COMP_DERIVED
3030 && d != DECL_DIMENSION && d != DECL_POINTER
3031 && d != DECL_PRIVATE && d != DECL_PUBLIC
3034 if (d == DECL_ALLOCATABLE)
3036 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3037 "attribute at %C in a TYPE definition")
3046 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3053 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3054 && gfc_current_state () != COMP_MODULE)
3056 if (d == DECL_PRIVATE)
3060 if (gfc_current_state () == COMP_DERIVED
3061 && gfc_state_stack->previous
3062 && gfc_state_stack->previous->state == COMP_MODULE)
3064 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3065 "at %L in a TYPE definition", attr,
3075 gfc_error ("%s attribute at %L is not allowed outside of the "
3076 "specification part of a module", attr, &seen_at[d]);
3084 case DECL_ALLOCATABLE:
3085 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
3088 case DECL_DIMENSION:
3089 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
3093 t = gfc_add_external (¤t_attr, &seen_at[d]);
3097 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
3101 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
3105 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
3108 case DECL_INTRINSIC:
3109 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
3113 t = gfc_add_optional (¤t_attr, &seen_at[d]);
3116 case DECL_PARAMETER:
3117 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
3121 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
3124 case DECL_PROTECTED:
3125 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3127 gfc_error ("PROTECTED at %C only allowed in specification "
3128 "part of a module");
3133 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3138 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
3142 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
3147 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
3152 t = gfc_add_save (¤t_attr, NULL, &seen_at[d]);
3156 t = gfc_add_target (¤t_attr, &seen_at[d]);
3159 case DECL_IS_BIND_C:
3160 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
3164 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3169 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
3173 if (gfc_notify_std (GFC_STD_F2003,
3174 "Fortran 2003: VOLATILE attribute at %C")
3178 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
3182 gfc_internal_error ("match_attr_spec(): Bad attribute");
3196 gfc_current_locus = start;
3197 gfc_free_array_spec (current_as);
3203 /* Set the binding label, dest_label, either with the binding label
3204 stored in the given gfc_typespec, ts, or if none was provided, it
3205 will be the symbol name in all lower case, as required by the draft
3206 (J3/04-007, section 15.4.1). If a binding label was given and
3207 there is more than one argument (num_idents), it is an error. */
3210 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3212 if (num_idents > 1 && has_name_equals)
3214 gfc_error ("Multiple identifiers provided with "
3215 "single NAME= specifier at %C");
3219 if (curr_binding_label[0] != '\0')
3221 /* Binding label given; store in temp holder til have sym. */
3222 strcpy (dest_label, curr_binding_label);
3226 /* No binding label given, and the NAME= specifier did not exist,
3227 which means there was no NAME="". */
3228 if (sym_name != NULL && has_name_equals == 0)
3229 strcpy (dest_label, sym_name);
3236 /* Set the status of the given common block as being BIND(C) or not,
3237 depending on the given parameter, is_bind_c. */
3240 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3242 com_block->is_bind_c = is_bind_c;
3247 /* Verify that the given gfc_typespec is for a C interoperable type. */
3250 verify_c_interop (gfc_typespec *ts, const char *name, locus *where)
3254 /* Make sure the kind used is appropriate for the type.
3255 The f90_type is unknown if an integer constant was
3256 used (e.g., real(4), bind(c) :: myFloat). */
3257 if (ts->f90_type != BT_UNKNOWN)
3259 t = gfc_validate_c_kind (ts);
3262 /* Print an error, but continue parsing line. */
3263 gfc_error_now ("C kind parameter is for type %s but "
3264 "symbol '%s' at %L is of type %s",
3265 gfc_basic_typename (ts->f90_type),
3267 gfc_basic_typename (ts->type));
3271 /* Make sure the kind is C interoperable. This does not care about the
3272 possible error above. */
3273 if (ts->type == BT_DERIVED && ts->derived != NULL)
3274 return (ts->derived->ts.is_c_interop ? SUCCESS : FAILURE);
3275 else if (ts->is_c_interop != 1)
3282 /* Verify that the variables of a given common block, which has been
3283 defined with the attribute specifier bind(c), to be of a C
3284 interoperable type. Errors will be reported here, if
3288 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3290 gfc_symbol *curr_sym = NULL;
3291 try retval = SUCCESS;
3293 curr_sym = com_block->head;
3295 /* Make sure we have at least one symbol. */
3296 if (curr_sym == NULL)
3299 /* Here we know we have a symbol, so we'll execute this loop
3303 /* The second to last param, 1, says this is in a common block. */
3304 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3305 curr_sym = curr_sym->common_next;
3306 } while (curr_sym != NULL);
3312 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3313 an appropriate error message is reported. */
3316 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3317 int is_in_common, gfc_common_head *com_block)
3319 try retval = SUCCESS;
3321 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3323 tmp_sym = tmp_sym->result;
3324 /* Make sure it wasn't an implicitly typed result. */
3325 if (tmp_sym->attr.implicit_type)
3327 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3328 "%L may not be C interoperable", tmp_sym->name,
3329 &tmp_sym->declared_at);
3330 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3331 /* Mark it as C interoperable to prevent duplicate warnings. */
3332 tmp_sym->ts.is_c_interop = 1;
3333 tmp_sym->attr.is_c_interop = 1;
3337 /* Here, we know we have the bind(c) attribute, so if we have
3338 enough type info, then verify that it's a C interop kind.
3339 The info could be in the symbol already, or possibly still in
3340 the given ts (current_ts), so look in both. */
3341 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3343 if (verify_c_interop (&(tmp_sym->ts), tmp_sym->name,
3344 &(tmp_sym->declared_at)) != SUCCESS)
3346 /* See if we're dealing with a sym in a common block or not. */
3347 if (is_in_common == 1)
3349 gfc_warning ("Variable '%s' in common block '%s' at %L "
3350 "may not be a C interoperable "
3351 "kind though common block '%s' is BIND(C)",
3352 tmp_sym->name, com_block->name,
3353 &(tmp_sym->declared_at), com_block->name);
3357 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3358 gfc_error ("Type declaration '%s' at %L is not C "
3359 "interoperable but it is BIND(C)",
3360 tmp_sym->name, &(tmp_sym->declared_at));
3362 gfc_warning ("Variable '%s' at %L "
3363 "may not be a C interoperable "
3364 "kind but it is bind(c)",
3365 tmp_sym->name, &(tmp_sym->declared_at));
3369 /* Variables declared w/in a common block can't be bind(c)
3370 since there's no way for C to see these variables, so there's
3371 semantically no reason for the attribute. */
3372 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3374 gfc_error ("Variable '%s' in common block '%s' at "
3375 "%L cannot be declared with BIND(C) "
3376 "since it is not a global",
3377 tmp_sym->name, com_block->name,
3378 &(tmp_sym->declared_at));
3382 /* Scalar variables that are bind(c) can not have the pointer
3383 or allocatable attributes. */
3384 if (tmp_sym->attr.is_bind_c == 1)
3386 if (tmp_sym->attr.pointer == 1)
3388 gfc_error ("Variable '%s' at %L cannot have both the "
3389 "POINTER and BIND(C) attributes",
3390 tmp_sym->name, &(tmp_sym->declared_at));
3394 if (tmp_sym->attr.allocatable == 1)
3396 gfc_error ("Variable '%s' at %L cannot have both the "
3397 "ALLOCATABLE and BIND(C) attributes",
3398 tmp_sym->name, &(tmp_sym->declared_at));
3402 /* If it is a BIND(C) function, make sure the return value is a
3403 scalar value. The previous tests in this function made sure
3404 the type is interoperable. */
3405 if (tmp_sym->attr.function == 1 && tmp_sym->as != NULL)
3406 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3407 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3409 /* BIND(C) functions can not return a character string. */
3410 if (tmp_sym->attr.function == 1 && tmp_sym->ts.type == BT_CHARACTER)
3411 if (tmp_sym->ts.cl == NULL || tmp_sym->ts.cl->length == NULL
3412 || tmp_sym->ts.cl->length->expr_type != EXPR_CONSTANT
3413 || mpz_cmp_si (tmp_sym->ts.cl->length->value.integer, 1) != 0)
3414 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3415 "be a character string", tmp_sym->name,
3416 &(tmp_sym->declared_at));
3420 /* See if the symbol has been marked as private. If it has, make sure
3421 there is no binding label and warn the user if there is one. */
3422 if (tmp_sym->attr.access == ACCESS_PRIVATE
3423 && tmp_sym->binding_label[0] != '\0')
3424 /* Use gfc_warning_now because we won't say that the symbol fails
3425 just because of this. */
3426 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3427 "given the binding label '%s'", tmp_sym->name,
3428 &(tmp_sym->declared_at), tmp_sym->binding_label);
3434 /* Set the appropriate fields for a symbol that's been declared as
3435 BIND(C) (the is_bind_c flag and the binding label), and verify that
3436 the type is C interoperable. Errors are reported by the functions
3437 used to set/test these fields. */
3440 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3442 try retval = SUCCESS;
3444 /* TODO: Do we need to make sure the vars aren't marked private? */
3446 /* Set the is_bind_c bit in symbol_attribute. */
3447 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3449 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3450 num_idents) != SUCCESS)
3457 /* Set the fields marking the given common block as BIND(C), including
3458 a binding label, and report any errors encountered. */
3461 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3463 try retval = SUCCESS;
3465 /* destLabel, common name, typespec (which may have binding label). */
3466 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3470 /* Set the given common block (com_block) to being bind(c) (1). */
3471 set_com_block_bind_c (com_block, 1);
3477 /* Retrieve the list of one or more identifiers that the given bind(c)
3478 attribute applies to. */
3481 get_bind_c_idents (void)
3483 char name[GFC_MAX_SYMBOL_LEN + 1];
3485 gfc_symbol *tmp_sym = NULL;
3487 gfc_common_head *com_block = NULL;
3489 if (gfc_match_name (name) == MATCH_YES)
3491 found_id = MATCH_YES;
3492 gfc_get_ha_symbol (name, &tmp_sym);
3494 else if (match_common_name (name) == MATCH_YES)
3496 found_id = MATCH_YES;
3497 com_block = gfc_get_common (name, 0);
3501 gfc_error ("Need either entity or common block name for "
3502 "attribute specification statement at %C");
3506 /* Save the current identifier and look for more. */
3509 /* Increment the number of identifiers found for this spec stmt. */
3512 /* Make sure we have a sym or com block, and verify that it can
3513 be bind(c). Set the appropriate field(s) and look for more
3515 if (tmp_sym != NULL || com_block != NULL)
3517 if (tmp_sym != NULL)
3519 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3525 if (set_verify_bind_c_com_block(com_block, num_idents)
3530 /* Look to see if we have another identifier. */
3532 if (gfc_match_eos () == MATCH_YES)
3533 found_id = MATCH_NO;
3534 else if (gfc_match_char (',') != MATCH_YES)
3535 found_id = MATCH_NO;
3536 else if (gfc_match_name (name) == MATCH_YES)
3538 found_id = MATCH_YES;
3539 gfc_get_ha_symbol (name, &tmp_sym);
3541 else if (match_common_name (name) == MATCH_YES)
3543 found_id = MATCH_YES;
3544 com_block = gfc_get_common (name, 0);
3548 gfc_error ("Missing entity or common block name for "
3549 "attribute specification statement at %C");
3555 gfc_internal_error ("Missing symbol");
3557 } while (found_id == MATCH_YES);
3559 /* if we get here we were successful */
3564 /* Try and match a BIND(C) attribute specification statement. */
3567 gfc_match_bind_c_stmt (void)
3569 match found_match = MATCH_NO;
3574 /* This may not be necessary. */
3576 /* Clear the temporary binding label holder. */
3577 curr_binding_label[0] = '\0';
3579 /* Look for the bind(c). */
3580 found_match = gfc_match_bind_c (NULL, true);
3582 if (found_match == MATCH_YES)
3584 /* Look for the :: now, but it is not required. */
3587 /* Get the identifier(s) that needs to be updated. This may need to
3588 change to hand the flag(s) for the attr specified so all identifiers
3589 found can have all appropriate parts updated (assuming that the same
3590 spec stmt can have multiple attrs, such as both bind(c) and
3592 if (get_bind_c_idents () != SUCCESS)
3593 /* Error message should have printed already. */
3601 /* Match a data declaration statement. */
3604 gfc_match_data_decl (void)
3610 num_idents_on_line = 0;
3612 m = gfc_match_type_spec (¤t_ts, 0);
3616 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3618 sym = gfc_use_derived (current_ts.derived);
3626 current_ts.derived = sym;
3629 m = match_attr_spec ();
3630 if (m == MATCH_ERROR)
3636 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL
3637 && !current_ts.derived->attr.zero_comp)
3640 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3643 gfc_find_symbol (current_ts.derived->name,
3644 current_ts.derived->ns->parent, 1, &sym);
3646 /* Any symbol that we find had better be a type definition
3647 which has its components defined. */
3648 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3649 && (current_ts.derived->components != NULL
3650 || current_ts.derived->attr.zero_comp))
3653 /* Now we have an error, which we signal, and then fix up
3654 because the knock-on is plain and simple confusing. */
3655 gfc_error_now ("Derived type at %C has not been previously defined "
3656 "and so cannot appear in a derived type definition");
3657 current_attr.pointer = 1;
3662 /* If we have an old-style character declaration, and no new-style
3663 attribute specifications, then there a comma is optional between
3664 the type specification and the variable list. */
3665 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3666 gfc_match_char (',');
3668 /* Give the types/attributes to symbols that follow. Give the element
3669 a number so that repeat character length expressions can be copied. */
3673 num_idents_on_line++;
3674 m = variable_decl (elem++);
3675 if (m == MATCH_ERROR)
3680 if (gfc_match_eos () == MATCH_YES)
3682 if (gfc_match_char (',') != MATCH_YES)
3686 if (gfc_error_flag_test () == 0)
3687 gfc_error ("Syntax error in data declaration at %C");
3690 gfc_free_data_all (gfc_current_ns);
3693 gfc_free_array_spec (current_as);
3699 /* Match a prefix associated with a function or subroutine
3700 declaration. If the typespec pointer is nonnull, then a typespec
3701 can be matched. Note that if nothing matches, MATCH_YES is
3702 returned (the null string was matched). */
3705 gfc_match_prefix (gfc_typespec *ts)
3709 gfc_clear_attr (¤t_attr);
3713 if (!seen_type && ts != NULL
3714 && gfc_match_type_spec (ts, 0) == MATCH_YES
3715 && gfc_match_space () == MATCH_YES)
3722 if (gfc_match ("elemental% ") == MATCH_YES)
3724 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
3730 if (gfc_match ("pure% ") == MATCH_YES)
3732 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
3738 if (gfc_match ("recursive% ") == MATCH_YES)
3740 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
3746 /* At this point, the next item is not a prefix. */
3751 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
3754 copy_prefix (symbol_attribute *dest, locus *where)
3756 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3759 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3762 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3769 /* Match a formal argument list. */
3772 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3774 gfc_formal_arglist *head, *tail, *p, *q;
3775 char name[GFC_MAX_SYMBOL_LEN + 1];
3781 if (gfc_match_char ('(') != MATCH_YES)
3788 if (gfc_match_char (')') == MATCH_YES)
3793 if (gfc_match_char ('*') == MATCH_YES)
3797 m = gfc_match_name (name);
3801 if (gfc_get_symbol (name, NULL, &sym))
3805 p = gfc_get_formal_arglist ();
3817 /* We don't add the VARIABLE flavor because the name could be a
3818 dummy procedure. We don't apply these attributes to formal
3819 arguments of statement functions. */
3820 if (sym != NULL && !st_flag
3821 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3822 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3828 /* The name of a program unit can be in a different namespace,
3829 so check for it explicitly. After the statement is accepted,
3830 the name is checked for especially in gfc_get_symbol(). */
3831 if (gfc_new_block != NULL && sym != NULL
3832 && strcmp (sym->name, gfc_new_block->name) == 0)
3834 gfc_error ("Name '%s' at %C is the name of the procedure",
3840 if (gfc_match_char (')') == MATCH_YES)
3843 m = gfc_match_char (',');
3846 gfc_error ("Unexpected junk in formal argument list at %C");
3852 /* Check for duplicate symbols in the formal argument list. */
3855 for (p = head; p->next; p = p->next)
3860 for (q = p->next; q; q = q->next)
3861 if (p->sym == q->sym)
3863 gfc_error ("Duplicate symbol '%s' in formal argument list "
3864 "at %C", p->sym->name);
3872 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3882 gfc_free_formal_arglist (head);
3887 /* Match a RESULT specification following a function declaration or
3888 ENTRY statement. Also matches the end-of-statement. */
3891 match_result (gfc_symbol *function, gfc_symbol **result)
3893 char name[GFC_MAX_SYMBOL_LEN + 1];
3897 if (gfc_match (" result (") != MATCH_YES)
3900 m = gfc_match_name (name);
3904 /* Get the right paren, and that's it because there could be the
3905 bind(c) attribute after the result clause. */
3906 if (gfc_match_char(')') != MATCH_YES)
3908 /* TODO: should report the missing right paren here. */
3912 if (strcmp (function->name, name) == 0)
3914 gfc_error ("RESULT variable at %C must be different than function name");
3918 if (gfc_get_symbol (name, NULL, &r))
3921 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
3922 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
3931 /* Match a function suffix, which could be a combination of a result
3932 clause and BIND(C), either one, or neither. The draft does not
3933 require them to come in a specific order. */
3936 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
3938 match is_bind_c; /* Found bind(c). */
3939 match is_result; /* Found result clause. */
3940 match found_match; /* Status of whether we've found a good match. */
3941 int peek_char; /* Character we're going to peek at. */
3942 bool allow_binding_name;
3944 /* Initialize to having found nothing. */
3945 found_match = MATCH_NO;
3946 is_bind_c = MATCH_NO;
3947 is_result = MATCH_NO;
3949 /* Get the next char to narrow between result and bind(c). */
3950 gfc_gobble_whitespace ();
3951 peek_char = gfc_peek_char ();
3953 /* C binding names are not allowed for internal procedures. */
3954 if (gfc_current_state () == COMP_CONTAINS
3955 && sym->ns->proc_name->attr.flavor != FL_MODULE)
3956 allow_binding_name = false;
3958 allow_binding_name = true;
3963 /* Look for result clause. */
3964 is_result = match_result (sym, result);
3965 if (is_result == MATCH_YES)
3967 /* Now see if there is a bind(c) after it. */
3968 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
3969 /* We've found the result clause and possibly bind(c). */
3970 found_match = MATCH_YES;
3973 /* This should only be MATCH_ERROR. */
3974 found_match = is_result;
3977 /* Look for bind(c) first. */
3978 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
3979 if (is_bind_c == MATCH_YES)
3981 /* Now see if a result clause followed it. */
3982 is_result = match_result (sym, result);
3983 found_match = MATCH_YES;
3987 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
3988 found_match = MATCH_ERROR;
3992 gfc_error ("Unexpected junk after function declaration at %C");
3993 found_match = MATCH_ERROR;
3997 if (is_bind_c == MATCH_YES)
3999 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4000 if (gfc_current_state () == COMP_CONTAINS
4001 && sym->ns->proc_name->attr.flavor != FL_MODULE
4002 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4003 "at %L may not be specified for an internal "
4004 "procedure", &gfc_current_locus)
4008 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4017 /* Match a PROCEDURE declaration (R1211). */
4020 match_procedure_decl (void)
4023 locus old_loc, entry_loc;
4024 gfc_symbol *sym, *proc_if = NULL;
4027 old_loc = entry_loc = gfc_current_locus;
4029 gfc_clear_ts (¤t_ts);
4031 if (gfc_match (" (") != MATCH_YES)
4033 gfc_current_locus = entry_loc;
4037 /* Get the type spec. for the procedure interface. */
4038 old_loc = gfc_current_locus;
4039 m = gfc_match_type_spec (¤t_ts, 0);
4040 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_char () == ')'))
4043 if (m == MATCH_ERROR)
4046 gfc_current_locus = old_loc;
4048 /* Get the name of the procedure or abstract interface
4049 to inherit the interface from. */
4050 m = gfc_match_symbol (&proc_if, 1);
4054 else if (m == MATCH_ERROR)
4057 /* Various interface checks. */
4060 /* Resolve interface if possible. That way, attr.procedure is only set
4061 if it is declared by a later procedure-declaration-stmt, which is
4062 invalid per C1212. */
4063 while (proc_if->ts.interface)
4064 proc_if = proc_if->ts.interface;
4066 if (proc_if->generic)
4068 gfc_error ("Interface '%s' at %C may not be generic", proc_if->name);
4071 if (proc_if->attr.proc == PROC_ST_FUNCTION)
4073 gfc_error ("Interface '%s' at %C may not be a statement function",
4077 /* Handle intrinsic procedures. */
4078 if (gfc_intrinsic_name (proc_if->name, 0)
4079 || gfc_intrinsic_name (proc_if->name, 1))
4080 proc_if->attr.intrinsic = 1;
4081 if (proc_if->attr.intrinsic
4082 && !gfc_intrinsic_actual_ok (proc_if->name, 0))
4084 gfc_error ("Intrinsic procedure '%s' not allowed "
4085 "in PROCEDURE statement at %C", proc_if->name);
4091 if (gfc_match (" )") != MATCH_YES)
4093 gfc_current_locus = entry_loc;
4097 /* Parse attributes. */
4098 m = match_attr_spec();
4099 if (m == MATCH_ERROR)
4102 /* Get procedure symbols. */
4105 m = gfc_match_symbol (&sym, 0);
4108 else if (m == MATCH_ERROR)
4111 /* Add current_attr to the symbol attributes. */
4112 if (gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
4115 if (sym->attr.is_bind_c)
4117 /* Check for C1218. */
4118 if (!proc_if || !proc_if->attr.is_bind_c)
4120 gfc_error ("BIND(C) attribute at %C requires "
4121 "an interface with BIND(C)");
4124 /* Check for C1217. */
4125 if (has_name_equals && sym->attr.pointer)
4127 gfc_error ("BIND(C) procedure with NAME may not have "
4128 "POINTER attribute at %C");
4131 if (has_name_equals && sym->attr.dummy)
4133 gfc_error ("Dummy procedure at %C may not have "
4134 "BIND(C) attribute with NAME");
4137 /* Set binding label for BIND(C). */
4138 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4142 if (!sym->attr.pointer && gfc_add_external (&sym->attr, NULL) == FAILURE)
4144 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4147 /* Set interface. */
4148 if (proc_if != NULL)
4150 sym->ts.interface = proc_if;
4151 sym->attr.untyped = 1;
4153 else if (current_ts.type != BT_UNKNOWN)
4155 sym->ts = current_ts;
4156 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4157 sym->ts.interface->ts = current_ts;
4158 sym->ts.interface->attr.function = 1;
4159 sym->attr.function = sym->ts.interface->attr.function;
4162 if (gfc_match_eos () == MATCH_YES)
4164 if (gfc_match_char (',') != MATCH_YES)
4169 gfc_error ("Syntax error in PROCEDURE statement at %C");
4174 /* Match a PROCEDURE declaration inside an interface (R1206). */
4177 match_procedure_in_interface (void)
4181 char name[GFC_MAX_SYMBOL_LEN + 1];
4183 if (current_interface.type == INTERFACE_NAMELESS
4184 || current_interface.type == INTERFACE_ABSTRACT)
4186 gfc_error ("PROCEDURE at %C must be in a generic interface");
4192 m = gfc_match_name (name);
4195 else if (m == MATCH_ERROR)
4197 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4200 if (gfc_add_interface (sym) == FAILURE)
4203 if (gfc_match_eos () == MATCH_YES)
4205 if (gfc_match_char (',') != MATCH_YES)
4212 gfc_error ("Syntax error in PROCEDURE statement at %C");
4217 /* General matcher for PROCEDURE declarations. */
4220 gfc_match_procedure (void)
4224 switch (gfc_current_state ())
4229 case COMP_SUBROUTINE:
4231 m = match_procedure_decl ();
4233 case COMP_INTERFACE:
4234 m = match_procedure_in_interface ();
4237 gfc_error ("Fortran 2003: Procedure components at %C are "
4238 "not yet implemented in gfortran");
4247 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4255 /* Match a function declaration. */
4258 gfc_match_function_decl (void)
4260 char name[GFC_MAX_SYMBOL_LEN + 1];
4261 gfc_symbol *sym, *result;
4265 match found_match; /* Status returned by match func. */
4267 if (gfc_current_state () != COMP_NONE
4268 && gfc_current_state () != COMP_INTERFACE
4269 && gfc_current_state () != COMP_CONTAINS)
4272 gfc_clear_ts (¤t_ts);
4274 old_loc = gfc_current_locus;
4276 m = gfc_match_prefix (¤t_ts);
4279 gfc_current_locus = old_loc;
4283 if (gfc_match ("function% %n", name) != MATCH_YES)
4285 gfc_current_locus = old_loc;
4288 if (get_proc_name (name, &sym, false))
4290 gfc_new_block = sym;
4292 m = gfc_match_formal_arglist (sym, 0, 0);
4295 gfc_error ("Expected formal argument list in function "
4296 "definition at %C");
4300 else if (m == MATCH_ERROR)
4305 /* According to the draft, the bind(c) and result clause can
4306 come in either order after the formal_arg_list (i.e., either
4307 can be first, both can exist together or by themselves or neither
4308 one). Therefore, the match_result can't match the end of the
4309 string, and check for the bind(c) or result clause in either order. */
4310 found_match = gfc_match_eos ();
4312 /* Make sure that it isn't already declared as BIND(C). If it is, it
4313 must have been marked BIND(C) with a BIND(C) attribute and that is
4314 not allowed for procedures. */
4315 if (sym->attr.is_bind_c == 1)
4317 sym->attr.is_bind_c = 0;
4318 if (sym->old_symbol != NULL)
4319 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4320 "variables or common blocks",
4321 &(sym->old_symbol->declared_at));
4323 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4324 "variables or common blocks", &gfc_current_locus);
4327 if (found_match != MATCH_YES)
4329 /* If we haven't found the end-of-statement, look for a suffix. */
4330 suffix_match = gfc_match_suffix (sym, &result);
4331 if (suffix_match == MATCH_YES)
4332 /* Need to get the eos now. */
4333 found_match = gfc_match_eos ();
4335 found_match = suffix_match;
4338 if(found_match != MATCH_YES)
4342 /* Make changes to the symbol. */
4345 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4348 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4349 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4352 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
4353 && !sym->attr.implicit_type)
4355 gfc_error ("Function '%s' at %C already has a type of %s", name,
4356 gfc_basic_typename (sym->ts.type));
4360 /* Delay matching the function characteristics until after the
4361 specification block by signalling kind=-1. */
4362 sym->declared_at = old_loc;
4363 if (current_ts.type != BT_UNKNOWN)
4364 current_ts.kind = -1;
4366 current_ts.kind = 0;
4370 sym->ts = current_ts;
4375 result->ts = current_ts;
4376 sym->result = result;
4383 gfc_current_locus = old_loc;
4388 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4389 pass the name of the entry, rather than the gfc_current_block name, and
4390 to return false upon finding an existing global entry. */
4393 add_global_entry (const char *name, int sub)
4398 s = gfc_get_gsymbol(name);
4399 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4402 || (s->type != GSYM_UNKNOWN
4403 && s->type != type))
4404 gfc_global_used(s, NULL);
4408 s->where = gfc_current_locus;
4416 /* Match an ENTRY statement. */
4419 gfc_match_entry (void)
4424 char name[GFC_MAX_SYMBOL_LEN + 1];
4425 gfc_compile_state state;
4429 bool module_procedure;
4433 m = gfc_match_name (name);
4437 state = gfc_current_state ();
4438 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
4443 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4446 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4448 case COMP_BLOCK_DATA:
4449 gfc_error ("ENTRY statement at %C cannot appear within "
4452 case COMP_INTERFACE:
4453 gfc_error ("ENTRY statement at %C cannot appear within "
4457 gfc_error ("ENTRY statement at %C cannot appear within "
4458 "a DERIVED TYPE block");
4461 gfc_error ("ENTRY statement at %C cannot appear within "
4462 "an IF-THEN block");
4465 gfc_error ("ENTRY statement at %C cannot appear within "
4469 gfc_error ("ENTRY statement at %C cannot appear within "
4473 gfc_error ("ENTRY statement at %C cannot appear within "
4477 gfc_error ("ENTRY statement at %C cannot appear within "
4481 gfc_error ("ENTRY statement at %C cannot appear within "
4482 "a contained subprogram");
4485 gfc_internal_error ("gfc_match_entry(): Bad state");
4490 module_procedure = gfc_current_ns->parent != NULL
4491 && gfc_current_ns->parent->proc_name
4492 && gfc_current_ns->parent->proc_name->attr.flavor
4495 if (gfc_current_ns->parent != NULL
4496 && gfc_current_ns->parent->proc_name
4497 && !module_procedure)
4499 gfc_error("ENTRY statement at %C cannot appear in a "
4500 "contained procedure");
4504 /* Module function entries need special care in get_proc_name
4505 because previous references within the function will have
4506 created symbols attached to the current namespace. */
4507 if (get_proc_name (name, &entry,
4508 gfc_current_ns->parent != NULL
4510 && gfc_current_ns->proc_name->attr.function))
4513 proc = gfc_current_block ();
4515 /* Make sure that it isn't already declared as BIND(C). If it is, it
4516 must have been marked BIND(C) with a BIND(C) attribute and that is
4517 not allowed for procedures. */
4518 if (entry->attr.is_bind_c == 1)
4520 entry->attr.is_bind_c = 0;
4521 if (entry->old_symbol != NULL)
4522 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4523 "variables or common blocks",
4524 &(entry->old_symbol->declared_at));
4526 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4527 "variables or common blocks", &gfc_current_locus);
4530 /* Check what next non-whitespace character is so we can tell if there
4531 is the required parens if we have a BIND(C). */
4532 gfc_gobble_whitespace ();
4533 peek_char = gfc_peek_char ();
4535 if (state == COMP_SUBROUTINE)
4537 /* An entry in a subroutine. */
4538 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
4541 m = gfc_match_formal_arglist (entry, 0, 1);
4545 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
4546 never be an internal procedure. */
4547 is_bind_c = gfc_match_bind_c (entry, true);
4548 if (is_bind_c == MATCH_ERROR)
4550 if (is_bind_c == MATCH_YES)
4552 if (peek_char != '(')
4554 gfc_error ("Missing required parentheses before BIND(C) at %C");
4557 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
4562 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4563 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4568 /* An entry in a function.
4569 We need to take special care because writing
4574 ENTRY f() RESULT (r)
4576 ENTRY f RESULT (r). */
4577 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
4580 old_loc = gfc_current_locus;
4581 if (gfc_match_eos () == MATCH_YES)
4583 gfc_current_locus = old_loc;
4584 /* Match the empty argument list, and add the interface to
4586 m = gfc_match_formal_arglist (entry, 0, 1);
4589 m = gfc_match_formal_arglist (entry, 0, 0);
4596 if (gfc_match_eos () == MATCH_YES)
4598 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4599 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4602 entry->result = entry;
4606 m = gfc_match_suffix (entry, &result);
4608 gfc_syntax_error (ST_ENTRY);
4614 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
4615 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
4616 || gfc_add_function (&entry->attr, result->name, NULL)
4619 entry->result = result;
4623 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4624 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4626 entry->result = entry;
4631 if (gfc_match_eos () != MATCH_YES)
4633 gfc_syntax_error (ST_ENTRY);
4637 entry->attr.recursive = proc->attr.recursive;
4638 entry->attr.elemental = proc->attr.elemental;
4639 entry->attr.pure = proc->attr.pure;
4641 el = gfc_get_entry_list ();
4643 el->next = gfc_current_ns->entries;
4644 gfc_current_ns->entries = el;
4646 el->id = el->next->id + 1;
4650 new_st.op = EXEC_ENTRY;
4651 new_st.ext.entry = el;
4657 /* Match a subroutine statement, including optional prefixes. */
4660 gfc_match_subroutine (void)
4662 char name[GFC_MAX_SYMBOL_LEN + 1];
4667 bool allow_binding_name;
4669 if (gfc_current_state () != COMP_NONE
4670 && gfc_current_state () != COMP_INTERFACE
4671 && gfc_current_state () != COMP_CONTAINS)
4674 m = gfc_match_prefix (NULL);
4678 m = gfc_match ("subroutine% %n", name);
4682 if (get_proc_name (name, &sym, false))
4684 gfc_new_block = sym;
4686 /* Check what next non-whitespace character is so we can tell if there
4687 is the required parens if we have a BIND(C). */
4688 gfc_gobble_whitespace ();
4689 peek_char = gfc_peek_char ();
4691 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
4694 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
4697 /* Make sure that it isn't already declared as BIND(C). If it is, it
4698 must have been marked BIND(C) with a BIND(C) attribute and that is
4699 not allowed for procedures. */
4700 if (sym->attr.is_bind_c == 1)
4702 sym->attr.is_bind_c = 0;
4703 if (sym->old_symbol != NULL)
4704 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4705 "variables or common blocks",
4706 &(sym->old_symbol->declared_at));
4708 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4709 "variables or common blocks", &gfc_current_locus);
4712 /* C binding names are not allowed for internal procedures. */
4713 if (gfc_current_state () == COMP_CONTAINS
4714 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4715 allow_binding_name = false;
4717 allow_binding_name = true;
4719 /* Here, we are just checking if it has the bind(c) attribute, and if
4720 so, then we need to make sure it's all correct. If it doesn't,
4721 we still need to continue matching the rest of the subroutine line. */
4722 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4723 if (is_bind_c == MATCH_ERROR)
4725 /* There was an attempt at the bind(c), but it was wrong. An
4726 error message should have been printed w/in the gfc_match_bind_c
4727 so here we'll just return the MATCH_ERROR. */
4731 if (is_bind_c == MATCH_YES)
4733 /* The following is allowed in the Fortran 2008 draft. */
4734 if (gfc_current_state () == COMP_CONTAINS
4735 && sym->ns->proc_name->attr.flavor != FL_MODULE
4736 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4737 "at %L may not be specified for an internal "
4738 "procedure", &gfc_current_locus)
4742 if (peek_char != '(')
4744 gfc_error ("Missing required parentheses before BIND(C) at %C");
4747 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
4752 if (gfc_match_eos () != MATCH_YES)
4754 gfc_syntax_error (ST_SUBROUTINE);
4758 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4765 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4766 given, and set the binding label in either the given symbol (if not
4767 NULL), or in the current_ts. The symbol may be NULL because we may
4768 encounter the BIND(C) before the declaration itself. Return
4769 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4770 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4771 or MATCH_YES if the specifier was correct and the binding label and
4772 bind(c) fields were set correctly for the given symbol or the
4773 current_ts. If allow_binding_name is false, no binding name may be
4777 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
4779 /* binding label, if exists */
4780 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
4784 /* Initialize the flag that specifies whether we encountered a NAME=
4785 specifier or not. */
4786 has_name_equals = 0;
4788 /* Init the first char to nil so we can catch if we don't have
4789 the label (name attr) or the symbol name yet. */
4790 binding_label[0] = '\0';
4792 /* This much we have to be able to match, in this order, if
4793 there is a bind(c) label. */
4794 if (gfc_match (" bind ( c ") != MATCH_YES)
4797 /* Now see if there is a binding label, or if we've reached the
4798 end of the bind(c) attribute without one. */
4799 if (gfc_match_char (',') == MATCH_YES)
4801 if (gfc_match (" name = ") != MATCH_YES)
4803 gfc_error ("Syntax error in NAME= specifier for binding label "
4805 /* should give an error message here */
4809 has_name_equals = 1;
4811 /* Get the opening quote. */
4812 double_quote = MATCH_YES;
4813 single_quote = MATCH_YES;
4814 double_quote = gfc_match_char ('"');
4815 if (double_quote != MATCH_YES)
4816 single_quote = gfc_match_char ('\'');
4817 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
4819 gfc_error ("Syntax error in NAME= specifier for binding label "
4824 /* Grab the binding label, using functions that will not lower
4825 case the names automatically. */
4826 if (gfc_match_name_C (binding_label) != MATCH_YES)
4829 /* Get the closing quotation. */
4830 if (double_quote == MATCH_YES)
4832 if (gfc_match_char ('"') != MATCH_YES)
4834 gfc_error ("Missing closing quote '\"' for binding label at %C");
4835 /* User started string with '"' so looked to match it. */
4841 if (gfc_match_char ('\'') != MATCH_YES)
4843 gfc_error ("Missing closing quote '\'' for binding label at %C");
4844 /* User started string with "'" char. */
4850 /* Get the required right paren. */
4851 if (gfc_match_char (')') != MATCH_YES)
4853 gfc_error ("Missing closing paren for binding label at %C");
4857 if (has_name_equals && !allow_binding_name)
4859 gfc_error ("No binding name is allowed in BIND(C) at %C");
4863 if (has_name_equals && sym != NULL && sym->attr.dummy)
4865 gfc_error ("For dummy procedure %s, no binding name is "
4866 "allowed in BIND(C) at %C", sym->name);
4871 /* Save the binding label to the symbol. If sym is null, we're
4872 probably matching the typespec attributes of a declaration and
4873 haven't gotten the name yet, and therefore, no symbol yet. */
4874 if (binding_label[0] != '\0')
4878 strcpy (sym->binding_label, binding_label);
4881 strcpy (curr_binding_label, binding_label);
4883 else if (allow_binding_name)
4885 /* No binding label, but if symbol isn't null, we
4886 can set the label for it here.
4887 If name="" or allow_binding_name is false, no C binding name is
4889 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
4890 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
4893 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
4894 && current_interface.type == INTERFACE_ABSTRACT)
4896 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
4904 /* Return nonzero if we're currently compiling a contained procedure. */
4907 contained_procedure (void)
4909 gfc_state_data *s = gfc_state_stack;
4911 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
4912 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
4918 /* Set the kind of each enumerator. The kind is selected such that it is
4919 interoperable with the corresponding C enumeration type, making
4920 sure that -fshort-enums is honored. */
4925 enumerator_history *current_history = NULL;
4929 if (max_enum == NULL || enum_history == NULL)
4932 if (!gfc_option.fshort_enums)
4938 kind = gfc_integer_kinds[i++].kind;
4940 while (kind < gfc_c_int_kind
4941 && gfc_check_integer_range (max_enum->initializer->value.integer,
4944 current_history = enum_history;
4945 while (current_history != NULL)
4947 current_history->sym->ts.kind = kind;
4948 current_history = current_history->next;
4953 /* Match any of the various end-block statements. Returns the type of
4954 END to the caller. The END INTERFACE, END IF, END DO and END
4955 SELECT statements cannot be replaced by a single END statement. */
4958 gfc_match_end (gfc_statement *st)
4960 char name[GFC_MAX_SYMBOL_LEN + 1];
4961 gfc_compile_state state;
4963 const char *block_name;
4968 old_loc = gfc_current_locus;
4969 if (gfc_match ("end") != MATCH_YES)
4972 state = gfc_current_state ();
4973 block_name = gfc_current_block () == NULL
4974 ? NULL : gfc_current_block ()->name;
4976 if (state == COMP_CONTAINS)
4978 state = gfc_state_stack->previous->state;
4979 block_name = gfc_state_stack->previous->sym == NULL
4980 ? NULL : gfc_state_stack->previous->sym->name;
4987 *st = ST_END_PROGRAM;
4988 target = " program";
4992 case COMP_SUBROUTINE:
4993 *st = ST_END_SUBROUTINE;
4994 target = " subroutine";
4995 eos_ok = !contained_procedure ();
4999 *st = ST_END_FUNCTION;
5000 target = " function";
5001 eos_ok = !contained_procedure ();
5004 case COMP_BLOCK_DATA:
5005 *st = ST_END_BLOCK_DATA;
5006 target = " block data";
5011 *st = ST_END_MODULE;
5016 case COMP_INTERFACE:
5017 *st = ST_END_INTERFACE;
5018 target = " interface";
5041 *st = ST_END_SELECT;
5047 *st = ST_END_FORALL;
5062 last_initializer = NULL;
5064 gfc_free_enum_history ();
5068 gfc_error ("Unexpected END statement at %C");
5072 if (gfc_match_eos () == MATCH_YES)
5076 /* We would have required END [something]. */
5077 gfc_error ("%s statement expected at %L",
5078 gfc_ascii_statement (*st), &old_loc);
5085 /* Verify that we've got the sort of end-block that we're expecting. */
5086 if (gfc_match (target) != MATCH_YES)
5088 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5092 /* If we're at the end, make sure a block name wasn't required. */
5093 if (gfc_match_eos () == MATCH_YES)
5096 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5097 && *st != ST_END_FORALL && *st != ST_END_WHERE)
5100 if (gfc_current_block () == NULL)
5103 gfc_error ("Expected block name of '%s' in %s statement at %C",
5104 block_name, gfc_ascii_statement (*st));
5109 /* END INTERFACE has a special handler for its several possible endings. */
5110 if (*st == ST_END_INTERFACE)
5111 return gfc_match_end_interface ();
5113 /* We haven't hit the end of statement, so what is left must be an
5115 m = gfc_match_space ();
5117 m = gfc_match_name (name);
5120 gfc_error ("Expected terminating name at %C");
5124 if (block_name == NULL)
5127 if (strcmp (name, block_name) != 0)
5129 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5130 gfc_ascii_statement (*st));
5134 if (gfc_match_eos () == MATCH_YES)
5138 gfc_syntax_error (*st);
5141 gfc_current_locus = old_loc;
5147 /***************** Attribute declaration statements ****************/
5149 /* Set the attribute of a single variable. */
5154 char name[GFC_MAX_SYMBOL_LEN + 1];
5162 m = gfc_match_name (name);
5166 if (find_special (name, &sym))
5169 var_locus = gfc_current_locus;
5171 /* Deal with possible array specification for certain attributes. */
5172 if (current_attr.dimension
5173 || current_attr.allocatable
5174 || current_attr.pointer
5175 || current_attr.target)
5177 m = gfc_match_array_spec (&as);
5178 if (m == MATCH_ERROR)
5181 if (current_attr.dimension && m == MATCH_NO)
5183 gfc_error ("Missing array specification at %L in DIMENSION "
5184 "statement", &var_locus);
5189 if (current_attr.dimension && sym->value)
5191 gfc_error ("Dimensions specified for %s at %L after its "
5192 "initialisation", sym->name, &var_locus);
5197 if ((current_attr.allocatable || current_attr.pointer)
5198 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5200 gfc_error ("Array specification must be deferred at %L", &var_locus);
5206 /* Update symbol table. DIMENSION attribute is set
5207 in gfc_set_array_spec(). */
5208 if (current_attr.dimension == 0
5209 && gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
5215 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5221 if (sym->attr.cray_pointee && sym->as != NULL)
5223 /* Fix the array spec. */
5224 m = gfc_mod_pointee_as (sym->as);
5225 if (m == MATCH_ERROR)
5229 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
5235 if ((current_attr.external || current_attr.intrinsic)
5236 && sym->attr.flavor != FL_PROCEDURE
5237 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
5246 gfc_free_array_spec (as);
5251 /* Generic attribute declaration subroutine. Used for attributes that
5252 just have a list of names. */
5259 /* Gobble the optional double colon, by simply ignoring the result
5269 if (gfc_match_eos () == MATCH_YES)
5275 if (gfc_match_char (',') != MATCH_YES)
5277 gfc_error ("Unexpected character in variable list at %C");
5287 /* This routine matches Cray Pointer declarations of the form:
5288 pointer ( <pointer>, <pointee> )
5290 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5291 The pointer, if already declared, should be an integer. Otherwise, we
5292 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5293 be either a scalar, or an array declaration. No space is allocated for
5294 the pointee. For the statement
5295 pointer (ipt, ar(10))
5296 any subsequent uses of ar will be translated (in C-notation) as
5297 ar(i) => ((<type> *) ipt)(i)
5298 After gimplification, pointee variable will disappear in the code. */
5301 cray_pointer_decl (void)
5305 gfc_symbol *cptr; /* Pointer symbol. */
5306 gfc_symbol *cpte; /* Pointee symbol. */
5312 if (gfc_match_char ('(') != MATCH_YES)
5314 gfc_error ("Expected '(' at %C");
5318 /* Match pointer. */
5319 var_locus = gfc_current_locus;
5320 gfc_clear_attr (¤t_attr);
5321 gfc_add_cray_pointer (¤t_attr, &var_locus);
5322 current_ts.type = BT_INTEGER;
5323 current_ts.kind = gfc_index_integer_kind;
5325 m = gfc_match_symbol (&cptr, 0);
5328 gfc_error ("Expected variable name at %C");
5332 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5335 gfc_set_sym_referenced (cptr);
5337 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5339 cptr->ts.type = BT_INTEGER;
5340 cptr->ts.kind = gfc_index_integer_kind;
5342 else if (cptr->ts.type != BT_INTEGER)
5344 gfc_error ("Cray pointer at %C must be an integer");
5347 else if (cptr->ts.kind < gfc_index_integer_kind)
5348 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5349 " memory addresses require %d bytes",
5350 cptr->ts.kind, gfc_index_integer_kind);
5352 if (gfc_match_char (',') != MATCH_YES)
5354 gfc_error ("Expected \",\" at %C");
5358 /* Match Pointee. */
5359 var_locus = gfc_current_locus;
5360 gfc_clear_attr (¤t_attr);
5361 gfc_add_cray_pointee (¤t_attr, &var_locus);
5362 current_ts.type = BT_UNKNOWN;
5363 current_ts.kind = 0;
5365 m = gfc_match_symbol (&cpte, 0);
5368 gfc_error ("Expected variable name at %C");
5372 /* Check for an optional array spec. */
5373 m = gfc_match_array_spec (&as);
5374 if (m == MATCH_ERROR)
5376 gfc_free_array_spec (as);
5379 else if (m == MATCH_NO)
5381 gfc_free_array_spec (as);
5385 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5388 gfc_set_sym_referenced (cpte);
5390 if (cpte->as == NULL)
5392 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5393 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5395 else if (as != NULL)
5397 gfc_error ("Duplicate array spec for Cray pointee at %C");
5398 gfc_free_array_spec (as);
5404 if (cpte->as != NULL)
5406 /* Fix array spec. */
5407 m = gfc_mod_pointee_as (cpte->as);
5408 if (m == MATCH_ERROR)
5412 /* Point the Pointee at the Pointer. */
5413 cpte->cp_pointer = cptr;
5415 if (gfc_match_char (')') != MATCH_YES)
5417 gfc_error ("Expected \")\" at %C");
5420 m = gfc_match_char (',');
5422 done = true; /* Stop searching for more declarations. */
5426 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5427 || gfc_match_eos () != MATCH_YES)
5429 gfc_error ("Expected \",\" or end of statement at %C");
5437 gfc_match_external (void)
5440 gfc_clear_attr (¤t_attr);
5441 current_attr.external = 1;
5443 return attr_decl ();
5448 gfc_match_intent (void)
5452 intent = match_intent_spec ();
5453 if (intent == INTENT_UNKNOWN)
5456 gfc_clear_attr (¤t_attr);
5457 current_attr.intent = intent;
5459 return attr_decl ();
5464 gfc_match_intrinsic (void)
5467 gfc_clear_attr (¤t_attr);
5468 current_attr.intrinsic = 1;
5470 return attr_decl ();
5475 gfc_match_optional (void)
5478 gfc_clear_attr (¤t_attr);
5479 current_attr.optional = 1;
5481 return attr_decl ();
5486 gfc_match_pointer (void)
5488 gfc_gobble_whitespace ();
5489 if (gfc_peek_char () == '(')
5491 if (!gfc_option.flag_cray_pointer)
5493 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5497 return cray_pointer_decl ();
5501 gfc_clear_attr (¤t_attr);
5502 current_attr.pointer = 1;
5504 return attr_decl ();
5510 gfc_match_allocatable (void)
5512 gfc_clear_attr (¤t_attr);
5513 current_attr.allocatable = 1;
5515 return attr_decl ();
5520 gfc_match_dimension (void)
5522 gfc_clear_attr (¤t_attr);
5523 current_attr.dimension = 1;
5525 return attr_decl ();
5530 gfc_match_target (void)
5532 gfc_clear_attr (¤t_attr);
5533 current_attr.target = 1;
5535 return attr_decl ();
5539 /* Match the list of entities being specified in a PUBLIC or PRIVATE
5543 access_attr_decl (gfc_statement st)
5545 char name[GFC_MAX_SYMBOL_LEN + 1];
5546 interface_type type;
5549 gfc_intrinsic_op operator;
5552 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5557 m = gfc_match_generic_spec (&type, name, &operator);
5560 if (m == MATCH_ERROR)
5565 case INTERFACE_NAMELESS:
5566 case INTERFACE_ABSTRACT:
5569 case INTERFACE_GENERIC:
5570 if (gfc_get_symbol (name, NULL, &sym))
5573 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5574 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
5575 sym->name, NULL) == FAILURE)
5580 case INTERFACE_INTRINSIC_OP:
5581 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
5583 gfc_current_ns->operator_access[operator] =
5584 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5588 gfc_error ("Access specification of the %s operator at %C has "
5589 "already been specified", gfc_op2string (operator));
5595 case INTERFACE_USER_OP:
5596 uop = gfc_get_uop (name);
5598 if (uop->access == ACCESS_UNKNOWN)
5600 uop->access = (st == ST_PUBLIC)
5601 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5605 gfc_error ("Access specification of the .%s. operator at %C "
5606 "has already been specified", sym->name);
5613 if (gfc_match_char (',') == MATCH_NO)
5617 if (gfc_match_eos () != MATCH_YES)
5622 gfc_syntax_error (st);
5630 gfc_match_protected (void)
5635 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
5637 gfc_error ("PROTECTED at %C only allowed in specification "
5638 "part of a module");
5643 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
5647 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5652 if (gfc_match_eos () == MATCH_YES)
5657 m = gfc_match_symbol (&sym, 0);
5661 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
5674 if (gfc_match_eos () == MATCH_YES)
5676 if (gfc_match_char (',') != MATCH_YES)
5683 gfc_error ("Syntax error in PROTECTED statement at %C");
5688 /* The PRIVATE statement is a bit weird in that it can be an attribute
5689 declaration, but also works as a standlone statement inside of a
5690 type declaration or a module. */
5693 gfc_match_private (gfc_statement *st)
5696 if (gfc_match ("private") != MATCH_YES)
5699 if (gfc_current_state () != COMP_MODULE
5700 && (gfc_current_state () != COMP_DERIVED
5701 || !gfc_state_stack->previous
5702 || gfc_state_stack->previous->state != COMP_MODULE))
5704 gfc_error ("PRIVATE statement at %C is only allowed in the "
5705 "specification part of a module");
5709 if (gfc_current_state () == COMP_DERIVED)
5711 if (gfc_match_eos () == MATCH_YES)
5717 gfc_syntax_error (ST_PRIVATE);
5721 if (gfc_match_eos () == MATCH_YES)
5728 return access_attr_decl (ST_PRIVATE);
5733 gfc_match_public (gfc_statement *st)
5736 if (gfc_match ("public") != MATCH_YES)
5739 if (gfc_current_state () != COMP_MODULE)
5741 gfc_error ("PUBLIC statement at %C is only allowed in the "
5742 "specification part of a module");
5746 if (gfc_match_eos () == MATCH_YES)
5753 return access_attr_decl (ST_PUBLIC);
5757 /* Workhorse for gfc_match_parameter. */
5766 m = gfc_match_symbol (&sym, 0);
5768 gfc_error ("Expected variable name at %C in PARAMETER statement");
5773 if (gfc_match_char ('=') == MATCH_NO)
5775 gfc_error ("Expected = sign in PARAMETER statement at %C");
5779 m = gfc_match_init_expr (&init);
5781 gfc_error ("Expected expression at %C in PARAMETER statement");
5785 if (sym->ts.type == BT_UNKNOWN
5786 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
5792 if (gfc_check_assign_symbol (sym, init) == FAILURE
5793 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
5801 gfc_error ("Initializing already initialized variable at %C");
5806 if (sym->ts.type == BT_CHARACTER
5807 && sym->ts.cl != NULL
5808 && sym->ts.cl->length != NULL
5809 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
5810 && init->expr_type == EXPR_CONSTANT
5811 && init->ts.type == BT_CHARACTER
5812 && init->ts.kind == 1)
5813 gfc_set_constant_character_len (
5814 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
5820 gfc_free_expr (init);
5825 /* Match a parameter statement, with the weird syntax that these have. */
5828 gfc_match_parameter (void)
5832 if (gfc_match_char ('(') == MATCH_NO)
5841 if (gfc_match (" )%t") == MATCH_YES)
5844 if (gfc_match_char (',') != MATCH_YES)
5846 gfc_error ("Unexpected characters in PARAMETER statement at %C");
5856 /* Save statements have a special syntax. */
5859 gfc_match_save (void)
5861 char n[GFC_MAX_SYMBOL_LEN+1];
5866 if (gfc_match_eos () == MATCH_YES)
5868 if (gfc_current_ns->seen_save)
5870 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
5871 "follows previous SAVE statement")
5876 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
5880 if (gfc_current_ns->save_all)
5882 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
5883 "blanket SAVE statement")
5892 m = gfc_match_symbol (&sym, 0);
5896 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
5908 m = gfc_match (" / %n /", &n);
5909 if (m == MATCH_ERROR)
5914 c = gfc_get_common (n, 0);
5917 gfc_current_ns->seen_save = 1;
5920 if (gfc_match_eos () == MATCH_YES)
5922 if (gfc_match_char (',') != MATCH_YES)
5929 gfc_error ("Syntax error in SAVE statement at %C");
5935 gfc_match_value (void)
5940 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
5944 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5949 if (gfc_match_eos () == MATCH_YES)
5954 m = gfc_match_symbol (&sym, 0);
5958 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
5971 if (gfc_match_eos () == MATCH_YES)
5973 if (gfc_match_char (',') != MATCH_YES)
5980 gfc_error ("Syntax error in VALUE statement at %C");
5986 gfc_match_volatile (void)
5991 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
5995 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6000 if (gfc_match_eos () == MATCH_YES)
6005 /* VOLATILE is special because it can be added to host-associated
6007 m = gfc_match_symbol (&sym, 1);
6011 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6024 if (gfc_match_eos () == MATCH_YES)
6026 if (gfc_match_char (',') != MATCH_YES)
6033 gfc_error ("Syntax error in VOLATILE statement at %C");
6038 /* Match a module procedure statement. Note that we have to modify
6039 symbols in the parent's namespace because the current one was there
6040 to receive symbols that are in an interface's formal argument list. */
6043 gfc_match_modproc (void)
6045 char name[GFC_MAX_SYMBOL_LEN + 1];
6048 gfc_namespace *module_ns;
6049 gfc_interface *old_interface_head, *interface;
6051 if (gfc_state_stack->state != COMP_INTERFACE
6052 || gfc_state_stack->previous == NULL
6053 || current_interface.type == INTERFACE_NAMELESS
6054 || current_interface.type == INTERFACE_ABSTRACT)
6056 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6061 module_ns = gfc_current_ns->parent;
6062 for (; module_ns; module_ns = module_ns->parent)
6063 if (module_ns->proc_name->attr.flavor == FL_MODULE)
6066 if (module_ns == NULL)
6069 /* Store the current state of the interface. We will need it if we
6070 end up with a syntax error and need to recover. */
6071 old_interface_head = gfc_current_interface_head ();
6077 m = gfc_match_name (name);
6083 /* Check for syntax error before starting to add symbols to the
6084 current namespace. */
6085 if (gfc_match_eos () == MATCH_YES)
6087 if (!last && gfc_match_char (',') != MATCH_YES)
6090 /* Now we're sure the syntax is valid, we process this item
6092 if (gfc_get_symbol (name, module_ns, &sym))
6095 if (sym->attr.proc != PROC_MODULE
6096 && gfc_add_procedure (&sym->attr, PROC_MODULE,
6097 sym->name, NULL) == FAILURE)
6100 if (gfc_add_interface (sym) == FAILURE)
6103 sym->attr.mod_proc = 1;
6112 /* Restore the previous state of the interface. */
6113 interface = gfc_current_interface_head ();
6114 gfc_set_current_interface_head (old_interface_head);
6116 /* Free the new interfaces. */
6117 while (interface != old_interface_head)
6119 gfc_interface *i = interface->next;
6120 gfc_free (interface);
6124 /* And issue a syntax error. */
6125 gfc_syntax_error (ST_MODULE_PROC);
6130 /* Match the optional attribute specifiers for a type declaration.
6131 Return MATCH_ERROR if an error is encountered in one of the handled
6132 attributes (public, private, bind(c)), MATCH_NO if what's found is
6133 not a handled attribute, and MATCH_YES otherwise. TODO: More error
6134 checking on attribute conflicts needs to be done. */
6137 gfc_get_type_attr_spec (symbol_attribute *attr)
6139 /* See if the derived type is marked as private. */
6140 if (gfc_match (" , private") == MATCH_YES)
6142 if (gfc_current_state () != COMP_MODULE)
6144 gfc_error ("Derived type at %C can only be PRIVATE in the "
6145 "specification part of a module");
6149 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6152 else if (gfc_match (" , public") == MATCH_YES)
6154 if (gfc_current_state () != COMP_MODULE)
6156 gfc_error ("Derived type at %C can only be PUBLIC in the "
6157 "specification part of a module");
6161 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6164 else if (gfc_match(" , bind ( c )") == MATCH_YES)
6166 /* If the type is defined to be bind(c) it then needs to make
6167 sure that all fields are interoperable. This will
6168 need to be a semantic check on the finished derived type.
6169 See 15.2.3 (lines 9-12) of F2003 draft. */
6170 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
6173 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
6178 /* If we get here, something matched. */
6183 /* Match the beginning of a derived type declaration. If a type name
6184 was the result of a function, then it is possible to have a symbol
6185 already to be known as a derived type yet have no components. */
6188 gfc_match_derived_decl (void)
6190 char name[GFC_MAX_SYMBOL_LEN + 1];
6191 symbol_attribute attr;
6194 match is_type_attr_spec = MATCH_NO;
6195 bool seen_attr = false;
6197 if (gfc_current_state () == COMP_DERIVED)
6200 gfc_clear_attr (&attr);
6204 is_type_attr_spec = gfc_get_type_attr_spec (&attr);
6205 if (is_type_attr_spec == MATCH_ERROR)
6207 if (is_type_attr_spec == MATCH_YES)
6209 } while (is_type_attr_spec == MATCH_YES);
6211 if (gfc_match (" ::") != MATCH_YES && seen_attr)
6213 gfc_error ("Expected :: in TYPE definition at %C");
6217 m = gfc_match (" %n%t", name);
6221 /* Make sure the name is not the name of an intrinsic type. */
6222 if (gfc_is_intrinsic_typename (name))
6224 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
6229 if (gfc_get_symbol (name, NULL, &sym))
6232 if (sym->ts.type != BT_UNKNOWN)
6234 gfc_error ("Derived type name '%s' at %C already has a basic type "
6235 "of %s", sym->name, gfc_typename (&sym->ts));
6239 /* The symbol may already have the derived attribute without the
6240 components. The ways this can happen is via a function
6241 definition, an INTRINSIC statement or a subtype in another
6242 derived type that is a pointer. The first part of the AND clause
6243 is true if a the symbol is not the return value of a function. */
6244 if (sym->attr.flavor != FL_DERIVED
6245 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6248 if (sym->components != NULL || sym->attr.zero_comp)
6250 gfc_error ("Derived type definition of '%s' at %C has already been "
6251 "defined", sym->name);
6255 if (attr.access != ACCESS_UNKNOWN
6256 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6259 /* See if the derived type was labeled as bind(c). */
6260 if (attr.is_bind_c != 0)
6261 sym->attr.is_bind_c = attr.is_bind_c;
6263 gfc_new_block = sym;
6269 /* Cray Pointees can be declared as:
6270 pointer (ipt, a (n,m,...,*))
6271 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6272 cheat and set a constant bound of 1 for the last dimension, if this
6273 is the case. Since there is no bounds-checking for Cray Pointees,
6274 this will be okay. */
6277 gfc_mod_pointee_as (gfc_array_spec *as)
6279 as->cray_pointee = true; /* This will be useful to know later. */
6280 if (as->type == AS_ASSUMED_SIZE)
6282 as->type = AS_EXPLICIT;
6283 as->upper[as->rank - 1] = gfc_int_expr (1);
6284 as->cp_was_assumed = true;
6286 else if (as->type == AS_ASSUMED_SHAPE)
6288 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6295 /* Match the enum definition statement, here we are trying to match
6296 the first line of enum definition statement.
6297 Returns MATCH_YES if match is found. */
6300 gfc_match_enum (void)
6304 m = gfc_match_eos ();
6308 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
6316 /* Match a variable name with an optional initializer. When this
6317 subroutine is called, a variable is expected to be parsed next.
6318 Depending on what is happening at the moment, updates either the
6319 symbol table or the current interface. */
6322 enumerator_decl (void)
6324 char name[GFC_MAX_SYMBOL_LEN + 1];
6325 gfc_expr *initializer;
6326 gfc_array_spec *as = NULL;
6334 old_locus = gfc_current_locus;
6336 /* When we get here, we've just matched a list of attributes and
6337 maybe a type and a double colon. The next thing we expect to see
6338 is the name of the symbol. */
6339 m = gfc_match_name (name);
6343 var_locus = gfc_current_locus;
6345 /* OK, we've successfully matched the declaration. Now put the
6346 symbol in the current namespace. If we fail to create the symbol,
6348 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6354 /* The double colon must be present in order to have initializers.
6355 Otherwise the statement is ambiguous with an assignment statement. */
6358 if (gfc_match_char ('=') == MATCH_YES)
6360 m = gfc_match_init_expr (&initializer);
6363 gfc_error ("Expected an initialization expression at %C");
6372 /* If we do not have an initializer, the initialization value of the
6373 previous enumerator (stored in last_initializer) is incremented
6374 by 1 and is used to initialize the current enumerator. */
6375 if (initializer == NULL)
6376 initializer = gfc_enum_initializer (last_initializer, old_locus);
6378 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6380 gfc_error("ENUMERATOR %L not initialized with integer expression",
6383 gfc_free_enum_history ();
6387 /* Store this current initializer, for the next enumerator variable
6388 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6389 use last_initializer below. */
6390 last_initializer = initializer;
6391 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6393 /* Maintain enumerator history. */
6394 gfc_find_symbol (name, NULL, 0, &sym);
6395 create_enum_history (sym, last_initializer);
6397 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6400 /* Free stuff up and return. */
6401 gfc_free_expr (initializer);
6407 /* Match the enumerator definition statement. */
6410 gfc_match_enumerator_def (void)
6415 gfc_clear_ts (¤t_ts);
6417 m = gfc_match (" enumerator");
6421 m = gfc_match (" :: ");
6422 if (m == MATCH_ERROR)
6425 colon_seen = (m == MATCH_YES);
6427 if (gfc_current_state () != COMP_ENUM)
6429 gfc_error ("ENUM definition statement expected before %C");
6430 gfc_free_enum_history ();
6434 (¤t_ts)->type = BT_INTEGER;
6435 (¤t_ts)->kind = gfc_c_int_kind;
6437 gfc_clear_attr (¤t_attr);
6438 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
6447 m = enumerator_decl ();
6448 if (m == MATCH_ERROR)
6453 if (gfc_match_eos () == MATCH_YES)
6455 if (gfc_match_char (',') != MATCH_YES)
6459 if (gfc_current_state () == COMP_ENUM)
6461 gfc_free_enum_history ();
6462 gfc_error ("Syntax error in ENUMERATOR definition at %C");
6467 gfc_free_array_spec (current_as);