1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009
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/>. */
30 /* Macros to access allocate memory for gfc_data_variable,
31 gfc_data_value and gfc_data. */
32 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
33 #define gfc_get_data_value() XCNEW (gfc_data_value)
34 #define gfc_get_data() XCNEW (gfc_data)
37 /* This flag is set if an old-style length selector is matched
38 during a type-declaration statement. */
40 static int old_char_selector;
42 /* When variables acquire types and attributes from a declaration
43 statement, they get them from the following static variables. The
44 first part of a declaration sets these variables and the second
45 part copies these into symbol structures. */
47 static gfc_typespec current_ts;
49 static symbol_attribute current_attr;
50 static gfc_array_spec *current_as;
51 static int colon_seen;
53 /* The current binding label (if any). */
54 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
55 /* Need to know how many identifiers are on the current data declaration
56 line in case we're given the BIND(C) attribute with a NAME= specifier. */
57 static int num_idents_on_line;
58 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
59 can supply a name if the curr_binding_label is nil and NAME= was not. */
60 static int has_name_equals = 0;
62 /* Initializer of the previous enumerator. */
64 static gfc_expr *last_initializer;
66 /* History of all the enumerators is maintained, so that
67 kind values of all the enumerators could be updated depending
68 upon the maximum initialized value. */
70 typedef struct enumerator_history
73 gfc_expr *initializer;
74 struct enumerator_history *next;
78 /* Header of enum history chain. */
80 static enumerator_history *enum_history = NULL;
82 /* Pointer of enum history node containing largest initializer. */
84 static enumerator_history *max_enum = NULL;
86 /* gfc_new_block points to the symbol of a newly matched block. */
88 gfc_symbol *gfc_new_block;
90 bool gfc_matching_function;
93 /********************* DATA statement subroutines *********************/
95 static bool in_match_data = false;
98 gfc_in_match_data (void)
100 return in_match_data;
104 set_in_match_data (bool set_value)
106 in_match_data = set_value;
109 /* Free a gfc_data_variable structure and everything beneath it. */
112 free_variable (gfc_data_variable *p)
114 gfc_data_variable *q;
119 gfc_free_expr (p->expr);
120 gfc_free_iterator (&p->iter, 0);
121 free_variable (p->list);
127 /* Free a gfc_data_value structure and everything beneath it. */
130 free_value (gfc_data_value *p)
137 gfc_free_expr (p->expr);
143 /* Free a list of gfc_data structures. */
146 gfc_free_data (gfc_data *p)
153 free_variable (p->var);
154 free_value (p->value);
160 /* Free all data in a namespace. */
163 gfc_free_data_all (gfc_namespace *ns)
176 static match var_element (gfc_data_variable *);
178 /* Match a list of variables terminated by an iterator and a right
182 var_list (gfc_data_variable *parent)
184 gfc_data_variable *tail, var;
187 m = var_element (&var);
188 if (m == MATCH_ERROR)
193 tail = gfc_get_data_variable ();
200 if (gfc_match_char (',') != MATCH_YES)
203 m = gfc_match_iterator (&parent->iter, 1);
206 if (m == MATCH_ERROR)
209 m = var_element (&var);
210 if (m == MATCH_ERROR)
215 tail->next = gfc_get_data_variable ();
221 if (gfc_match_char (')') != MATCH_YES)
226 gfc_syntax_error (ST_DATA);
231 /* Match a single element in a data variable list, which can be a
232 variable-iterator list. */
235 var_element (gfc_data_variable *new_var)
240 memset (new_var, 0, sizeof (gfc_data_variable));
242 if (gfc_match_char ('(') == MATCH_YES)
243 return var_list (new_var);
245 m = gfc_match_variable (&new_var->expr, 0);
249 sym = new_var->expr->symtree->n.sym;
251 /* Symbol should already have an associated type. */
252 if (gfc_check_symbol_typed (sym, gfc_current_ns,
253 false, gfc_current_locus) == FAILURE)
256 if (!sym->attr.function && gfc_current_ns->parent
257 && gfc_current_ns->parent == sym->ns)
259 gfc_error ("Host associated variable '%s' may not be in the DATA "
260 "statement at %C", sym->name);
264 if (gfc_current_state () != COMP_BLOCK_DATA
265 && sym->attr.in_common
266 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
267 "common block variable '%s' in DATA statement at %C",
268 sym->name) == FAILURE)
271 if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
278 /* Match the top-level list of data variables. */
281 top_var_list (gfc_data *d)
283 gfc_data_variable var, *tail, *new_var;
290 m = var_element (&var);
293 if (m == MATCH_ERROR)
296 new_var = gfc_get_data_variable ();
302 tail->next = new_var;
306 if (gfc_match_char ('/') == MATCH_YES)
308 if (gfc_match_char (',') != MATCH_YES)
315 gfc_syntax_error (ST_DATA);
316 gfc_free_data_all (gfc_current_ns);
322 match_data_constant (gfc_expr **result)
324 char name[GFC_MAX_SYMBOL_LEN + 1];
330 m = gfc_match_literal_constant (&expr, 1);
337 if (m == MATCH_ERROR)
340 m = gfc_match_null (result);
344 old_loc = gfc_current_locus;
346 /* Should this be a structure component, try to match it
347 before matching a name. */
348 m = gfc_match_rvalue (result);
349 if (m == MATCH_ERROR)
352 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
354 if (gfc_simplify_expr (*result, 0) == FAILURE)
359 gfc_current_locus = old_loc;
361 m = gfc_match_name (name);
365 if (gfc_find_symbol (name, NULL, 1, &sym))
369 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
371 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
375 else if (sym->attr.flavor == FL_DERIVED)
376 return gfc_match_structure_constructor (sym, result, false);
378 /* Check to see if the value is an initialization array expression. */
379 if (sym->value->expr_type == EXPR_ARRAY)
381 gfc_current_locus = old_loc;
383 m = gfc_match_init_expr (result);
384 if (m == MATCH_ERROR)
389 if (gfc_simplify_expr (*result, 0) == FAILURE)
392 if ((*result)->expr_type == EXPR_CONSTANT)
396 gfc_error ("Invalid initializer %s in Data statement at %C", name);
402 *result = gfc_copy_expr (sym->value);
407 /* Match a list of values in a DATA statement. The leading '/' has
408 already been seen at this point. */
411 top_val_list (gfc_data *data)
413 gfc_data_value *new_val, *tail;
421 m = match_data_constant (&expr);
424 if (m == MATCH_ERROR)
427 new_val = gfc_get_data_value ();
428 mpz_init (new_val->repeat);
431 data->value = new_val;
433 tail->next = new_val;
437 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
440 mpz_set_ui (tail->repeat, 1);
444 if (expr->ts.type == BT_INTEGER)
445 mpz_set (tail->repeat, expr->value.integer);
446 gfc_free_expr (expr);
448 m = match_data_constant (&tail->expr);
451 if (m == MATCH_ERROR)
455 if (gfc_match_char ('/') == MATCH_YES)
457 if (gfc_match_char (',') == MATCH_NO)
464 gfc_syntax_error (ST_DATA);
465 gfc_free_data_all (gfc_current_ns);
470 /* Matches an old style initialization. */
473 match_old_style_init (const char *name)
480 /* Set up data structure to hold initializers. */
481 gfc_find_sym_tree (name, NULL, 0, &st);
484 newdata = gfc_get_data ();
485 newdata->var = gfc_get_data_variable ();
486 newdata->var->expr = gfc_get_variable_expr (st);
487 newdata->where = gfc_current_locus;
489 /* Match initial value list. This also eats the terminal '/'. */
490 m = top_val_list (newdata);
499 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
504 /* Mark the variable as having appeared in a data statement. */
505 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
511 /* Chain in namespace list of DATA initializers. */
512 newdata->next = gfc_current_ns->data;
513 gfc_current_ns->data = newdata;
519 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
520 we are matching a DATA statement and are therefore issuing an error
521 if we encounter something unexpected, if not, we're trying to match
522 an old-style initialization expression of the form INTEGER I /2/. */
525 gfc_match_data (void)
530 set_in_match_data (true);
534 new_data = gfc_get_data ();
535 new_data->where = gfc_current_locus;
537 m = top_var_list (new_data);
541 m = top_val_list (new_data);
545 new_data->next = gfc_current_ns->data;
546 gfc_current_ns->data = new_data;
548 if (gfc_match_eos () == MATCH_YES)
551 gfc_match_char (','); /* Optional comma */
554 set_in_match_data (false);
558 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
565 set_in_match_data (false);
566 gfc_free_data (new_data);
571 /************************ Declaration statements *********************/
573 /* Match an intent specification. Since this can only happen after an
574 INTENT word, a legal intent-spec must follow. */
577 match_intent_spec (void)
580 if (gfc_match (" ( in out )") == MATCH_YES)
582 if (gfc_match (" ( in )") == MATCH_YES)
584 if (gfc_match (" ( out )") == MATCH_YES)
587 gfc_error ("Bad INTENT specification at %C");
588 return INTENT_UNKNOWN;
592 /* Matches a character length specification, which is either a
593 specification expression or a '*'. */
596 char_len_param_value (gfc_expr **expr)
600 if (gfc_match_char ('*') == MATCH_YES)
606 m = gfc_match_expr (expr);
609 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
612 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
614 if ((*expr)->value.function.actual
615 && (*expr)->value.function.actual->expr->symtree)
618 e = (*expr)->value.function.actual->expr;
619 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
620 && e->expr_type == EXPR_VARIABLE)
622 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
624 if (e->symtree->n.sym->ts.type == BT_CHARACTER
625 && e->symtree->n.sym->ts.u.cl
626 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
634 gfc_error ("Conflict in attributes of function argument at %C");
639 /* A character length is a '*' followed by a literal integer or a
640 char_len_param_value in parenthesis. */
643 match_char_length (gfc_expr **expr)
648 m = gfc_match_char ('*');
652 m = gfc_match_small_literal_int (&length, NULL);
653 if (m == MATCH_ERROR)
658 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
659 "Old-style character length at %C") == FAILURE)
661 *expr = gfc_int_expr (length);
665 if (gfc_match_char ('(') == MATCH_NO)
668 m = char_len_param_value (expr);
669 if (m != MATCH_YES && gfc_matching_function)
675 if (m == MATCH_ERROR)
680 if (gfc_match_char (')') == MATCH_NO)
682 gfc_free_expr (*expr);
690 gfc_error ("Syntax error in character length specification at %C");
695 /* Special subroutine for finding a symbol. Check if the name is found
696 in the current name space. If not, and we're compiling a function or
697 subroutine and the parent compilation unit is an interface, then check
698 to see if the name we've been given is the name of the interface
699 (located in another namespace). */
702 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
708 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
711 *result = st ? st->n.sym : NULL;
715 if (gfc_current_state () != COMP_SUBROUTINE
716 && gfc_current_state () != COMP_FUNCTION)
719 s = gfc_state_stack->previous;
723 if (s->state != COMP_INTERFACE)
726 goto end; /* Nameless interface. */
728 if (strcmp (name, s->sym->name) == 0)
739 /* Special subroutine for getting a symbol node associated with a
740 procedure name, used in SUBROUTINE and FUNCTION statements. The
741 symbol is created in the parent using with symtree node in the
742 child unit pointing to the symbol. If the current namespace has no
743 parent, then the symbol is just created in the current unit. */
746 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
752 /* Module functions have to be left in their own namespace because
753 they have potentially (almost certainly!) already been referenced.
754 In this sense, they are rather like external functions. This is
755 fixed up in resolve.c(resolve_entries), where the symbol name-
756 space is set to point to the master function, so that the fake
757 result mechanism can work. */
758 if (module_fcn_entry)
760 /* Present if entry is declared to be a module procedure. */
761 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
764 rc = gfc_get_symbol (name, NULL, result);
765 else if (!gfc_get_symbol (name, NULL, &sym) && sym
766 && (*result)->ts.type == BT_UNKNOWN
767 && sym->attr.flavor == FL_UNKNOWN)
768 /* Pick up the typespec for the entry, if declared in the function
769 body. Note that this symbol is FL_UNKNOWN because it will
770 only have appeared in a type declaration. The local symtree
771 is set to point to the module symbol and a unique symtree
772 to the local version. This latter ensures a correct clearing
775 /* If the ENTRY proceeds its specification, we need to ensure
776 that this does not raise a "has no IMPLICIT type" error. */
777 if (sym->ts.type == BT_UNKNOWN)
778 sym->attr.untyped = 1;
780 (*result)->ts = sym->ts;
782 /* Put the symbol in the procedure namespace so that, should
783 the ENTRY precede its specification, the specification
785 (*result)->ns = gfc_current_ns;
787 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
789 st = gfc_get_unique_symtree (gfc_current_ns);
794 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
800 gfc_current_ns->refs++;
802 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
804 /* Trap another encompassed procedure with the same name. All
805 these conditions are necessary to avoid picking up an entry
806 whose name clashes with that of the encompassing procedure;
807 this is handled using gsymbols to register unique,globally
809 if (sym->attr.flavor != 0
810 && sym->attr.proc != 0
811 && (sym->attr.subroutine || sym->attr.function)
812 && sym->attr.if_source != IFSRC_UNKNOWN)
813 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
814 name, &sym->declared_at);
816 /* Trap a procedure with a name the same as interface in the
817 encompassing scope. */
818 if (sym->attr.generic != 0
819 && (sym->attr.subroutine || sym->attr.function)
820 && !sym->attr.mod_proc)
821 gfc_error_now ("Name '%s' at %C is already defined"
822 " as a generic interface at %L",
823 name, &sym->declared_at);
825 /* Trap declarations of attributes in encompassing scope. The
826 signature for this is that ts.kind is set. Legitimate
827 references only set ts.type. */
828 if (sym->ts.kind != 0
829 && !sym->attr.implicit_type
830 && sym->attr.proc == 0
831 && gfc_current_ns->parent != NULL
832 && sym->attr.access == 0
833 && !module_fcn_entry)
834 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
835 "and must not have attributes declared at %L",
836 name, &sym->declared_at);
839 if (gfc_current_ns->parent == NULL || *result == NULL)
842 /* Module function entries will already have a symtree in
843 the current namespace but will need one at module level. */
844 if (module_fcn_entry)
846 /* Present if entry is declared to be a module procedure. */
847 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
849 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
852 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
857 /* See if the procedure should be a module procedure. */
859 if (((sym->ns->proc_name != NULL
860 && sym->ns->proc_name->attr.flavor == FL_MODULE
861 && sym->attr.proc != PROC_MODULE)
862 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
863 && gfc_add_procedure (&sym->attr, PROC_MODULE,
864 sym->name, NULL) == FAILURE)
871 /* Verify that the given symbol representing a parameter is C
872 interoperable, by checking to see if it was marked as such after
873 its declaration. If the given symbol is not interoperable, a
874 warning is reported, thus removing the need to return the status to
875 the calling function. The standard does not require the user use
876 one of the iso_c_binding named constants to declare an
877 interoperable parameter, but we can't be sure if the param is C
878 interop or not if the user doesn't. For example, integer(4) may be
879 legal Fortran, but doesn't have meaning in C. It may interop with
880 a number of the C types, which causes a problem because the
881 compiler can't know which one. This code is almost certainly not
882 portable, and the user will get what they deserve if the C type
883 across platforms isn't always interoperable with integer(4). If
884 the user had used something like integer(c_int) or integer(c_long),
885 the compiler could have automatically handled the varying sizes
889 verify_c_interop_param (gfc_symbol *sym)
891 int is_c_interop = 0;
892 gfc_try retval = SUCCESS;
894 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
895 Don't repeat the checks here. */
896 if (sym->attr.implicit_type)
899 /* For subroutines or functions that are passed to a BIND(C) procedure,
900 they're interoperable if they're BIND(C) and their params are all
902 if (sym->attr.flavor == FL_PROCEDURE)
904 if (sym->attr.is_bind_c == 0)
906 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
907 "attribute to be C interoperable", sym->name,
908 &(sym->declared_at));
914 if (sym->attr.is_c_interop == 1)
915 /* We've already checked this procedure; don't check it again. */
918 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
923 /* See if we've stored a reference to a procedure that owns sym. */
924 if (sym->ns != NULL && sym->ns->proc_name != NULL)
926 if (sym->ns->proc_name->attr.is_bind_c == 1)
929 (verify_c_interop (&(sym->ts))
932 if (is_c_interop != 1)
934 /* Make personalized messages to give better feedback. */
935 if (sym->ts.type == BT_DERIVED)
936 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
937 " procedure '%s' but is not C interoperable "
938 "because derived type '%s' is not C interoperable",
939 sym->name, &(sym->declared_at),
940 sym->ns->proc_name->name,
941 sym->ts.u.derived->name);
943 gfc_warning ("Variable '%s' at %L is a parameter to the "
944 "BIND(C) procedure '%s' but may not be C "
946 sym->name, &(sym->declared_at),
947 sym->ns->proc_name->name);
950 /* Character strings are only C interoperable if they have a
952 if (sym->ts.type == BT_CHARACTER)
954 gfc_charlen *cl = sym->ts.u.cl;
955 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
956 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
958 gfc_error ("Character argument '%s' at %L "
959 "must be length 1 because "
960 "procedure '%s' is BIND(C)",
961 sym->name, &sym->declared_at,
962 sym->ns->proc_name->name);
967 /* We have to make sure that any param to a bind(c) routine does
968 not have the allocatable, pointer, or optional attributes,
969 according to J3/04-007, section 5.1. */
970 if (sym->attr.allocatable == 1)
972 gfc_error ("Variable '%s' at %L cannot have the "
973 "ALLOCATABLE attribute because procedure '%s'"
974 " is BIND(C)", sym->name, &(sym->declared_at),
975 sym->ns->proc_name->name);
979 if (sym->attr.pointer == 1)
981 gfc_error ("Variable '%s' at %L cannot have the "
982 "POINTER attribute because procedure '%s'"
983 " is BIND(C)", sym->name, &(sym->declared_at),
984 sym->ns->proc_name->name);
988 if (sym->attr.optional == 1)
990 gfc_error ("Variable '%s' at %L cannot have the "
991 "OPTIONAL attribute because procedure '%s'"
992 " is BIND(C)", sym->name, &(sym->declared_at),
993 sym->ns->proc_name->name);
997 /* Make sure that if it has the dimension attribute, that it is
998 either assumed size or explicit shape. */
1001 if (sym->as->type == AS_ASSUMED_SHAPE)
1003 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1004 "argument to the procedure '%s' at %L because "
1005 "the procedure is BIND(C)", sym->name,
1006 &(sym->declared_at), sym->ns->proc_name->name,
1007 &(sym->ns->proc_name->declared_at));
1011 if (sym->as->type == AS_DEFERRED)
1013 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1014 "argument to the procedure '%s' at %L because "
1015 "the procedure is BIND(C)", sym->name,
1016 &(sym->declared_at), sym->ns->proc_name->name,
1017 &(sym->ns->proc_name->declared_at));
1028 /* Function called by variable_decl() that adds a name to the symbol table. */
1031 build_sym (const char *name, gfc_charlen *cl,
1032 gfc_array_spec **as, locus *var_locus)
1034 symbol_attribute attr;
1037 if (gfc_get_symbol (name, NULL, &sym))
1040 /* Start updating the symbol table. Add basic type attribute if present. */
1041 if (current_ts.type != BT_UNKNOWN
1042 && (sym->attr.implicit_type == 0
1043 || !gfc_compare_types (&sym->ts, ¤t_ts))
1044 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
1047 if (sym->ts.type == BT_CHARACTER)
1050 /* Add dimension attribute if present. */
1051 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1055 /* Add attribute to symbol. The copy is so that we can reset the
1056 dimension attribute. */
1057 attr = current_attr;
1060 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1063 /* Finish any work that may need to be done for the binding label,
1064 if it's a bind(c). The bind(c) attr is found before the symbol
1065 is made, and before the symbol name (for data decls), so the
1066 current_ts is holding the binding label, or nothing if the
1067 name= attr wasn't given. Therefore, test here if we're dealing
1068 with a bind(c) and make sure the binding label is set correctly. */
1069 if (sym->attr.is_bind_c == 1)
1071 if (sym->binding_label[0] == '\0')
1073 /* Set the binding label and verify that if a NAME= was specified
1074 then only one identifier was in the entity-decl-list. */
1075 if (set_binding_label (sym->binding_label, sym->name,
1076 num_idents_on_line) == FAILURE)
1081 /* See if we know we're in a common block, and if it's a bind(c)
1082 common then we need to make sure we're an interoperable type. */
1083 if (sym->attr.in_common == 1)
1085 /* Test the common block object. */
1086 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1087 && sym->ts.is_c_interop != 1)
1089 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1090 "must be declared with a C interoperable "
1091 "kind since common block '%s' is BIND(C)",
1092 sym->name, sym->common_block->name,
1093 sym->common_block->name);
1098 sym->attr.implied_index = 0;
1104 /* Set character constant to the given length. The constant will be padded or
1105 truncated. If we're inside an array constructor without a typespec, we
1106 additionally check that all elements have the same length; check_len -1
1107 means no checking. */
1110 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1115 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1116 gcc_assert (expr->ts.type == BT_CHARACTER);
1118 slen = expr->value.character.length;
1121 s = gfc_get_wide_string (len + 1);
1122 memcpy (s, expr->value.character.string,
1123 MIN (len, slen) * sizeof (gfc_char_t));
1125 gfc_wide_memset (&s[slen], ' ', len - slen);
1127 if (gfc_option.warn_character_truncation && slen > len)
1128 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1129 "(%d/%d)", &expr->where, slen, len);
1131 /* Apply the standard by 'hand' otherwise it gets cleared for
1133 if (check_len != -1 && slen != check_len
1134 && !(gfc_option.allow_std & GFC_STD_GNU))
1135 gfc_error_now ("The CHARACTER elements of the array constructor "
1136 "at %L must have the same length (%d/%d)",
1137 &expr->where, slen, check_len);
1140 gfc_free (expr->value.character.string);
1141 expr->value.character.string = s;
1142 expr->value.character.length = len;
1147 /* Function to create and update the enumerator history
1148 using the information passed as arguments.
1149 Pointer "max_enum" is also updated, to point to
1150 enum history node containing largest initializer.
1152 SYM points to the symbol node of enumerator.
1153 INIT points to its enumerator value. */
1156 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1158 enumerator_history *new_enum_history;
1159 gcc_assert (sym != NULL && init != NULL);
1161 new_enum_history = XCNEW (enumerator_history);
1163 new_enum_history->sym = sym;
1164 new_enum_history->initializer = init;
1165 new_enum_history->next = NULL;
1167 if (enum_history == NULL)
1169 enum_history = new_enum_history;
1170 max_enum = enum_history;
1174 new_enum_history->next = enum_history;
1175 enum_history = new_enum_history;
1177 if (mpz_cmp (max_enum->initializer->value.integer,
1178 new_enum_history->initializer->value.integer) < 0)
1179 max_enum = new_enum_history;
1184 /* Function to free enum kind history. */
1187 gfc_free_enum_history (void)
1189 enumerator_history *current = enum_history;
1190 enumerator_history *next;
1192 while (current != NULL)
1194 next = current->next;
1199 enum_history = NULL;
1203 /* Function called by variable_decl() that adds an initialization
1204 expression to a symbol. */
1207 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1209 symbol_attribute attr;
1214 if (find_special (name, &sym, false))
1219 /* If this symbol is confirming an implicit parameter type,
1220 then an initialization expression is not allowed. */
1221 if (attr.flavor == FL_PARAMETER
1222 && sym->value != NULL
1225 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1232 /* An initializer is required for PARAMETER declarations. */
1233 if (attr.flavor == FL_PARAMETER)
1235 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1241 /* If a variable appears in a DATA block, it cannot have an
1245 gfc_error ("Variable '%s' at %C with an initializer already "
1246 "appears in a DATA statement", sym->name);
1250 /* Check if the assignment can happen. This has to be put off
1251 until later for a derived type variable. */
1252 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1253 && gfc_check_assign_symbol (sym, init) == FAILURE)
1256 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1257 && init->ts.type == BT_CHARACTER)
1259 /* Update symbol character length according initializer. */
1260 if (gfc_check_assign_symbol (sym, init) == FAILURE)
1263 if (sym->ts.u.cl->length == NULL)
1266 /* If there are multiple CHARACTER variables declared on the
1267 same line, we don't want them to share the same length. */
1268 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1270 if (sym->attr.flavor == FL_PARAMETER)
1272 if (init->expr_type == EXPR_CONSTANT)
1274 clen = init->value.character.length;
1275 sym->ts.u.cl->length = gfc_int_expr (clen);
1277 else if (init->expr_type == EXPR_ARRAY)
1279 gfc_expr *p = init->value.constructor->expr;
1280 clen = p->value.character.length;
1281 sym->ts.u.cl->length = gfc_int_expr (clen);
1283 else if (init->ts.u.cl && init->ts.u.cl->length)
1284 sym->ts.u.cl->length =
1285 gfc_copy_expr (sym->value->ts.u.cl->length);
1288 /* Update initializer character length according symbol. */
1289 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1291 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1292 gfc_constructor * p;
1294 if (init->expr_type == EXPR_CONSTANT)
1295 gfc_set_constant_character_len (len, init, -1);
1296 else if (init->expr_type == EXPR_ARRAY)
1298 /* Build a new charlen to prevent simplification from
1299 deleting the length before it is resolved. */
1300 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1301 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1303 for (p = init->value.constructor; p; p = p->next)
1304 gfc_set_constant_character_len (len, p->expr, -1);
1309 /* Need to check if the expression we initialized this
1310 to was one of the iso_c_binding named constants. If so,
1311 and we're a parameter (constant), let it be iso_c.
1313 integer(c_int), parameter :: my_int = c_int
1314 integer(my_int) :: my_int_2
1315 If we mark my_int as iso_c (since we can see it's value
1316 is equal to one of the named constants), then my_int_2
1317 will be considered C interoperable. */
1318 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1320 sym->ts.is_iso_c |= init->ts.is_iso_c;
1321 sym->ts.is_c_interop |= init->ts.is_c_interop;
1322 /* attr bits needed for module files. */
1323 sym->attr.is_iso_c |= init->ts.is_iso_c;
1324 sym->attr.is_c_interop |= init->ts.is_c_interop;
1325 if (init->ts.is_iso_c)
1326 sym->ts.f90_type = init->ts.f90_type;
1329 /* Add initializer. Make sure we keep the ranks sane. */
1330 if (sym->attr.dimension && init->rank == 0)
1336 if (sym->attr.flavor == FL_PARAMETER
1337 && init->expr_type == EXPR_CONSTANT
1338 && spec_size (sym->as, &size) == SUCCESS
1339 && mpz_cmp_si (size, 0) > 0)
1341 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1344 array->value.constructor = c = NULL;
1345 for (n = 0; n < (int)mpz_get_si (size); n++)
1347 if (array->value.constructor == NULL)
1349 array->value.constructor = c = gfc_get_constructor ();
1354 c->next = gfc_get_constructor ();
1356 c->expr = gfc_copy_expr (init);
1360 array->shape = gfc_get_shape (sym->as->rank);
1361 for (n = 0; n < sym->as->rank; n++)
1362 spec_dimen_size (sym->as, n, &array->shape[n]);
1367 init->rank = sym->as->rank;
1371 if (sym->attr.save == SAVE_NONE)
1372 sym->attr.save = SAVE_IMPLICIT;
1380 /* Function called by variable_decl() that adds a name to a structure
1384 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1385 gfc_array_spec **as)
1389 /* If the current symbol is of the same derived type that we're
1390 constructing, it must have the pointer attribute. */
1391 if (current_ts.type == BT_DERIVED
1392 && current_ts.u.derived == gfc_current_block ()
1393 && current_attr.pointer == 0)
1395 gfc_error ("Component at %C must have the POINTER attribute");
1399 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1401 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1403 gfc_error ("Array component of structure at %C must have explicit "
1404 "or deferred shape");
1409 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1413 if (c->ts.type == BT_CHARACTER)
1415 c->attr = current_attr;
1417 c->initializer = *init;
1422 c->attr.dimension = 1;
1425 /* Should this ever get more complicated, combine with similar section
1426 in add_init_expr_to_sym into a separate function. */
1427 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer && c->ts.u.cl
1428 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1432 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1433 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1434 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1436 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1438 if (c->initializer->expr_type == EXPR_CONSTANT)
1439 gfc_set_constant_character_len (len, c->initializer, -1);
1440 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1441 c->initializer->ts.u.cl->length->value.integer))
1444 gfc_constructor *ctor = c->initializer->value.constructor;
1446 has_ts = (c->initializer->ts.u.cl
1447 && c->initializer->ts.u.cl->length_from_typespec);
1453 /* Remember the length of the first element for checking
1454 that all elements *in the constructor* have the same
1455 length. This need not be the length of the LHS! */
1456 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1457 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1458 first_len = ctor->expr->value.character.length;
1460 for (; ctor; ctor = ctor->next)
1462 if (ctor->expr->expr_type == EXPR_CONSTANT)
1463 gfc_set_constant_character_len (len, ctor->expr,
1464 has_ts ? -1 : first_len);
1470 /* Check array components. */
1471 if (!c->attr.dimension)
1473 if (c->attr.allocatable)
1475 gfc_error ("Allocatable component at %C must be an array");
1482 if (c->attr.pointer)
1484 if (c->as->type != AS_DEFERRED)
1486 gfc_error ("Pointer array component of structure at %C must have a "
1491 else if (c->attr.allocatable)
1493 if (c->as->type != AS_DEFERRED)
1495 gfc_error ("Allocatable component of structure at %C must have a "
1502 if (c->as->type != AS_EXPLICIT)
1504 gfc_error ("Array component of structure at %C must have an "
1514 /* Match a 'NULL()', and possibly take care of some side effects. */
1517 gfc_match_null (gfc_expr **result)
1523 m = gfc_match (" null ( )");
1527 /* The NULL symbol now has to be/become an intrinsic function. */
1528 if (gfc_get_symbol ("null", NULL, &sym))
1530 gfc_error ("NULL() initialization at %C is ambiguous");
1534 gfc_intrinsic_symbol (sym);
1536 if (sym->attr.proc != PROC_INTRINSIC
1537 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1538 sym->name, NULL) == FAILURE
1539 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1542 e = gfc_get_expr ();
1543 e->where = gfc_current_locus;
1544 e->expr_type = EXPR_NULL;
1545 e->ts.type = BT_UNKNOWN;
1553 /* Match a variable name with an optional initializer. When this
1554 subroutine is called, a variable is expected to be parsed next.
1555 Depending on what is happening at the moment, updates either the
1556 symbol table or the current interface. */
1559 variable_decl (int elem)
1561 char name[GFC_MAX_SYMBOL_LEN + 1];
1562 gfc_expr *initializer, *char_len;
1564 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1575 old_locus = gfc_current_locus;
1577 /* When we get here, we've just matched a list of attributes and
1578 maybe a type and a double colon. The next thing we expect to see
1579 is the name of the symbol. */
1580 m = gfc_match_name (name);
1584 var_locus = gfc_current_locus;
1586 /* Now we could see the optional array spec. or character length. */
1587 m = gfc_match_array_spec (&as);
1588 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1589 cp_as = gfc_copy_array_spec (as);
1590 else if (m == MATCH_ERROR)
1594 as = gfc_copy_array_spec (current_as);
1599 if (current_ts.type == BT_CHARACTER)
1601 switch (match_char_length (&char_len))
1604 cl = gfc_new_charlen (gfc_current_ns, NULL);
1606 cl->length = char_len;
1609 /* Non-constant lengths need to be copied after the first
1610 element. Also copy assumed lengths. */
1613 && (current_ts.u.cl->length == NULL
1614 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1616 cl = gfc_new_charlen (gfc_current_ns, NULL);
1617 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1620 cl = current_ts.u.cl;
1629 /* If this symbol has already shown up in a Cray Pointer declaration,
1630 then we want to set the type & bail out. */
1631 if (gfc_option.flag_cray_pointer)
1633 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1634 if (sym != NULL && sym->attr.cray_pointee)
1636 sym->ts.type = current_ts.type;
1637 sym->ts.kind = current_ts.kind;
1639 sym->ts.u.derived = current_ts.u.derived;
1640 sym->ts.is_c_interop = current_ts.is_c_interop;
1641 sym->ts.is_iso_c = current_ts.is_iso_c;
1644 /* Check to see if we have an array specification. */
1647 if (sym->as != NULL)
1649 gfc_error ("Duplicate array spec for Cray pointee at %C");
1650 gfc_free_array_spec (cp_as);
1656 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1657 gfc_internal_error ("Couldn't set pointee array spec.");
1659 /* Fix the array spec. */
1660 m = gfc_mod_pointee_as (sym->as);
1661 if (m == MATCH_ERROR)
1669 gfc_free_array_spec (cp_as);
1673 /* Procedure pointer as function result. */
1674 if (gfc_current_state () == COMP_FUNCTION
1675 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1676 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1677 strcpy (name, "ppr@");
1679 if (gfc_current_state () == COMP_FUNCTION
1680 && strcmp (name, gfc_current_block ()->name) == 0
1681 && gfc_current_block ()->result
1682 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1683 strcpy (name, "ppr@");
1685 /* OK, we've successfully matched the declaration. Now put the
1686 symbol in the current namespace, because it might be used in the
1687 optional initialization expression for this symbol, e.g. this is
1690 integer, parameter :: i = huge(i)
1692 This is only true for parameters or variables of a basic type.
1693 For components of derived types, it is not true, so we don't
1694 create a symbol for those yet. If we fail to create the symbol,
1696 if (gfc_current_state () != COMP_DERIVED
1697 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1703 /* An interface body specifies all of the procedure's
1704 characteristics and these shall be consistent with those
1705 specified in the procedure definition, except that the interface
1706 may specify a procedure that is not pure if the procedure is
1707 defined to be pure(12.3.2). */
1708 if (current_ts.type == BT_DERIVED
1709 && gfc_current_ns->proc_name
1710 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1711 && current_ts.u.derived->ns != gfc_current_ns)
1714 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.u.derived->name);
1715 if (!(current_ts.u.derived->attr.imported
1717 && st->n.sym == current_ts.u.derived)
1718 && !gfc_current_ns->has_import_set)
1720 gfc_error ("the type of '%s' at %C has not been declared within the "
1727 /* In functions that have a RESULT variable defined, the function
1728 name always refers to function calls. Therefore, the name is
1729 not allowed to appear in specification statements. */
1730 if (gfc_current_state () == COMP_FUNCTION
1731 && gfc_current_block () != NULL
1732 && gfc_current_block ()->result != NULL
1733 && gfc_current_block ()->result != gfc_current_block ()
1734 && strcmp (gfc_current_block ()->name, name) == 0)
1736 gfc_error ("Function name '%s' not allowed at %C", name);
1741 /* We allow old-style initializations of the form
1742 integer i /2/, j(4) /3*3, 1/
1743 (if no colon has been seen). These are different from data
1744 statements in that initializers are only allowed to apply to the
1745 variable immediately preceding, i.e.
1747 is not allowed. Therefore we have to do some work manually, that
1748 could otherwise be left to the matchers for DATA statements. */
1750 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1752 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1753 "initialization at %C") == FAILURE)
1756 return match_old_style_init (name);
1759 /* The double colon must be present in order to have initializers.
1760 Otherwise the statement is ambiguous with an assignment statement. */
1763 if (gfc_match (" =>") == MATCH_YES)
1765 if (!current_attr.pointer)
1767 gfc_error ("Initialization at %C isn't for a pointer variable");
1772 m = gfc_match_null (&initializer);
1775 gfc_error ("Pointer initialization requires a NULL() at %C");
1779 if (gfc_pure (NULL))
1781 gfc_error ("Initialization of pointer at %C is not allowed in "
1782 "a PURE procedure");
1790 else if (gfc_match_char ('=') == MATCH_YES)
1792 if (current_attr.pointer)
1794 gfc_error ("Pointer initialization at %C requires '=>', "
1800 m = gfc_match_init_expr (&initializer);
1803 gfc_error ("Expected an initialization expression at %C");
1807 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1809 gfc_error ("Initialization of variable at %C is not allowed in "
1810 "a PURE procedure");
1819 if (initializer != NULL && current_attr.allocatable
1820 && gfc_current_state () == COMP_DERIVED)
1822 gfc_error ("Initialization of allocatable component at %C is not "
1828 /* Add the initializer. Note that it is fine if initializer is
1829 NULL here, because we sometimes also need to check if a
1830 declaration *must* have an initialization expression. */
1831 if (gfc_current_state () != COMP_DERIVED)
1832 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1835 if (current_ts.type == BT_DERIVED
1836 && !current_attr.pointer && !initializer)
1837 initializer = gfc_default_initializer (¤t_ts);
1838 t = build_struct (name, cl, &initializer, &as);
1841 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1844 /* Free stuff up and return. */
1845 gfc_free_expr (initializer);
1846 gfc_free_array_spec (as);
1852 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1853 This assumes that the byte size is equal to the kind number for
1854 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1857 gfc_match_old_kind_spec (gfc_typespec *ts)
1862 if (gfc_match_char ('*') != MATCH_YES)
1865 m = gfc_match_small_literal_int (&ts->kind, NULL);
1869 original_kind = ts->kind;
1871 /* Massage the kind numbers for complex types. */
1872 if (ts->type == BT_COMPLEX)
1876 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1877 gfc_basic_typename (ts->type), original_kind);
1883 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1885 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1886 gfc_basic_typename (ts->type), original_kind);
1890 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1891 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1898 /* Match a kind specification. Since kinds are generally optional, we
1899 usually return MATCH_NO if something goes wrong. If a "kind="
1900 string is found, then we know we have an error. */
1903 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
1915 where = loc = gfc_current_locus;
1920 if (gfc_match_char ('(') == MATCH_NO)
1923 /* Also gobbles optional text. */
1924 if (gfc_match (" kind = ") == MATCH_YES)
1927 loc = gfc_current_locus;
1930 n = gfc_match_init_expr (&e);
1934 if (gfc_matching_function)
1936 /* The function kind expression might include use associated or
1937 imported parameters and try again after the specification
1939 if (gfc_match_char (')') != MATCH_YES)
1941 gfc_error ("Missing right parenthesis at %C");
1947 gfc_undo_symbols ();
1952 /* ....or else, the match is real. */
1954 gfc_error ("Expected initialization expression at %C");
1962 gfc_error ("Expected scalar initialization expression at %C");
1967 msg = gfc_extract_int (e, &ts->kind);
1976 /* Before throwing away the expression, let's see if we had a
1977 C interoperable kind (and store the fact). */
1978 if (e->ts.is_c_interop == 1)
1980 /* Mark this as c interoperable if being declared with one
1981 of the named constants from iso_c_binding. */
1982 ts->is_c_interop = e->ts.is_iso_c;
1983 ts->f90_type = e->ts.f90_type;
1989 /* Ignore errors to this point, if we've gotten here. This means
1990 we ignore the m=MATCH_ERROR from above. */
1991 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1993 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1994 gfc_basic_typename (ts->type));
1995 gfc_current_locus = where;
1999 /* Warn if, e.g., c_int is used for a REAL variable, but not
2000 if, e.g., c_double is used for COMPLEX as the standard
2001 explicitly says that the kind type parameter for complex and real
2002 variable is the same, i.e. c_float == c_float_complex. */
2003 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2004 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2005 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2006 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2007 "is %s", gfc_basic_typename (ts->f90_type), &where,
2008 gfc_basic_typename (ts->type));
2010 gfc_gobble_whitespace ();
2011 if ((c = gfc_next_ascii_char ()) != ')'
2012 && (ts->type != BT_CHARACTER || c != ','))
2014 if (ts->type == BT_CHARACTER)
2015 gfc_error ("Missing right parenthesis or comma at %C");
2017 gfc_error ("Missing right parenthesis at %C");
2021 /* All tests passed. */
2024 if(m == MATCH_ERROR)
2025 gfc_current_locus = where;
2027 /* Return what we know from the test(s). */
2032 gfc_current_locus = where;
2038 match_char_kind (int * kind, int * is_iso_c)
2047 where = gfc_current_locus;
2049 n = gfc_match_init_expr (&e);
2051 if (n != MATCH_YES && gfc_matching_function)
2053 /* The expression might include use-associated or imported
2054 parameters and try again after the specification
2057 gfc_undo_symbols ();
2062 gfc_error ("Expected initialization expression at %C");
2068 gfc_error ("Expected scalar initialization expression at %C");
2073 msg = gfc_extract_int (e, kind);
2074 *is_iso_c = e->ts.is_iso_c;
2084 /* Ignore errors to this point, if we've gotten here. This means
2085 we ignore the m=MATCH_ERROR from above. */
2086 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2088 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2092 /* All tests passed. */
2095 if (m == MATCH_ERROR)
2096 gfc_current_locus = where;
2098 /* Return what we know from the test(s). */
2103 gfc_current_locus = where;
2108 /* Match the various kind/length specifications in a CHARACTER
2109 declaration. We don't return MATCH_NO. */
2112 gfc_match_char_spec (gfc_typespec *ts)
2114 int kind, seen_length, is_iso_c;
2124 /* Try the old-style specification first. */
2125 old_char_selector = 0;
2127 m = match_char_length (&len);
2131 old_char_selector = 1;
2136 m = gfc_match_char ('(');
2139 m = MATCH_YES; /* Character without length is a single char. */
2143 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2144 if (gfc_match (" kind =") == MATCH_YES)
2146 m = match_char_kind (&kind, &is_iso_c);
2148 if (m == MATCH_ERROR)
2153 if (gfc_match (" , len =") == MATCH_NO)
2156 m = char_len_param_value (&len);
2159 if (m == MATCH_ERROR)
2166 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2167 if (gfc_match (" len =") == MATCH_YES)
2169 m = char_len_param_value (&len);
2172 if (m == MATCH_ERROR)
2176 if (gfc_match_char (')') == MATCH_YES)
2179 if (gfc_match (" , kind =") != MATCH_YES)
2182 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2188 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2189 m = char_len_param_value (&len);
2192 if (m == MATCH_ERROR)
2196 m = gfc_match_char (')');
2200 if (gfc_match_char (',') != MATCH_YES)
2203 gfc_match (" kind ="); /* Gobble optional text. */
2205 m = match_char_kind (&kind, &is_iso_c);
2206 if (m == MATCH_ERROR)
2212 /* Require a right-paren at this point. */
2213 m = gfc_match_char (')');
2218 gfc_error ("Syntax error in CHARACTER declaration at %C");
2220 gfc_free_expr (len);
2224 /* Deal with character functions after USE and IMPORT statements. */
2225 if (gfc_matching_function)
2227 gfc_free_expr (len);
2228 gfc_undo_symbols ();
2234 gfc_free_expr (len);
2238 /* Do some final massaging of the length values. */
2239 cl = gfc_new_charlen (gfc_current_ns, NULL);
2241 if (seen_length == 0)
2242 cl->length = gfc_int_expr (1);
2247 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2249 /* We have to know if it was a c interoperable kind so we can
2250 do accurate type checking of bind(c) procs, etc. */
2252 /* Mark this as c interoperable if being declared with one
2253 of the named constants from iso_c_binding. */
2254 ts->is_c_interop = is_iso_c;
2255 else if (len != NULL)
2256 /* Here, we might have parsed something such as: character(c_char)
2257 In this case, the parsing code above grabs the c_char when
2258 looking for the length (line 1690, roughly). it's the last
2259 testcase for parsing the kind params of a character variable.
2260 However, it's not actually the length. this seems like it
2262 To see if the user used a C interop kind, test the expr
2263 of the so called length, and see if it's C interoperable. */
2264 ts->is_c_interop = len->ts.is_iso_c;
2270 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2271 structure to the matched specification. This is necessary for FUNCTION and
2272 IMPLICIT statements.
2274 If implicit_flag is nonzero, then we don't check for the optional
2275 kind specification. Not doing so is needed for matching an IMPLICIT
2276 statement correctly. */
2279 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2281 char name[GFC_MAX_SYMBOL_LEN + 1];
2285 bool seen_deferred_kind;
2287 /* A belt and braces check that the typespec is correctly being treated
2288 as a deferred characteristic association. */
2289 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2290 && (gfc_current_block ()->result->ts.kind == -1)
2291 && (ts->kind == -1);
2293 if (seen_deferred_kind)
2296 /* Clear the current binding label, in case one is given. */
2297 curr_binding_label[0] = '\0';
2299 if (gfc_match (" byte") == MATCH_YES)
2301 if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2305 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2307 gfc_error ("BYTE type used at %C "
2308 "is not available on the target machine");
2312 ts->type = BT_INTEGER;
2317 if (gfc_match (" integer") == MATCH_YES)
2319 ts->type = BT_INTEGER;
2320 ts->kind = gfc_default_integer_kind;
2324 if (gfc_match (" character") == MATCH_YES)
2326 ts->type = BT_CHARACTER;
2327 if (implicit_flag == 0)
2328 return gfc_match_char_spec (ts);
2333 if (gfc_match (" real") == MATCH_YES)
2336 ts->kind = gfc_default_real_kind;
2340 if (gfc_match (" double precision") == MATCH_YES)
2343 ts->kind = gfc_default_double_kind;
2347 if (gfc_match (" complex") == MATCH_YES)
2349 ts->type = BT_COMPLEX;
2350 ts->kind = gfc_default_complex_kind;
2354 if (gfc_match (" double complex") == MATCH_YES)
2356 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2357 "conform to the Fortran 95 standard") == FAILURE)
2360 ts->type = BT_COMPLEX;
2361 ts->kind = gfc_default_double_kind;
2365 if (gfc_match (" logical") == MATCH_YES)
2367 ts->type = BT_LOGICAL;
2368 ts->kind = gfc_default_logical_kind;
2372 m = gfc_match (" type ( %n )", name);
2375 m = gfc_match (" class ( %n )", name);
2380 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2384 /* TODO: Implement Polymorphism. */
2385 gfc_warning ("Polymorphic entities are not yet implemented. "
2386 "CLASS will be treated like TYPE at %C");
2389 ts->type = BT_DERIVED;
2391 /* Defer association of the derived type until the end of the
2392 specification block. However, if the derived type can be
2393 found, add it to the typespec. */
2394 if (gfc_matching_function)
2396 ts->u.derived = NULL;
2397 if (gfc_current_state () != COMP_INTERFACE
2398 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2399 ts->u.derived = sym;
2403 /* Search for the name but allow the components to be defined later. If
2404 type = -1, this typespec has been seen in a function declaration but
2405 the type could not be accessed at that point. */
2407 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2409 gfc_error ("Type name '%s' at %C is ambiguous", name);
2412 else if (ts->kind == -1)
2414 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2415 || gfc_current_ns->has_import_set;
2416 if (gfc_find_symbol (name, NULL, iface, &sym))
2418 gfc_error ("Type name '%s' at %C is ambiguous", name);
2427 if (sym->attr.flavor != FL_DERIVED
2428 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2431 gfc_set_sym_referenced (sym);
2432 ts->u.derived = sym;
2437 /* For all types except double, derived and character, look for an
2438 optional kind specifier. MATCH_NO is actually OK at this point. */
2439 if (implicit_flag == 1)
2442 if (gfc_current_form == FORM_FREE)
2444 c = gfc_peek_ascii_char ();
2445 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2446 && c != ':' && c != ',')
2450 m = gfc_match_kind_spec (ts, false);
2451 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2452 m = gfc_match_old_kind_spec (ts);
2454 /* Defer association of the KIND expression of function results
2455 until after USE and IMPORT statements. */
2456 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2457 || gfc_matching_function)
2461 m = MATCH_YES; /* No kind specifier found. */
2467 /* Match an IMPLICIT NONE statement. Actually, this statement is
2468 already matched in parse.c, or we would not end up here in the
2469 first place. So the only thing we need to check, is if there is
2470 trailing garbage. If not, the match is successful. */
2473 gfc_match_implicit_none (void)
2475 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2479 /* Match the letter range(s) of an IMPLICIT statement. */
2482 match_implicit_range (void)
2488 cur_loc = gfc_current_locus;
2490 gfc_gobble_whitespace ();
2491 c = gfc_next_ascii_char ();
2494 gfc_error ("Missing character range in IMPLICIT at %C");
2501 gfc_gobble_whitespace ();
2502 c1 = gfc_next_ascii_char ();
2506 gfc_gobble_whitespace ();
2507 c = gfc_next_ascii_char ();
2512 inner = 0; /* Fall through. */
2519 gfc_gobble_whitespace ();
2520 c2 = gfc_next_ascii_char ();
2524 gfc_gobble_whitespace ();
2525 c = gfc_next_ascii_char ();
2527 if ((c != ',') && (c != ')'))
2540 gfc_error ("Letters must be in alphabetic order in "
2541 "IMPLICIT statement at %C");
2545 /* See if we can add the newly matched range to the pending
2546 implicits from this IMPLICIT statement. We do not check for
2547 conflicts with whatever earlier IMPLICIT statements may have
2548 set. This is done when we've successfully finished matching
2550 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2557 gfc_syntax_error (ST_IMPLICIT);
2559 gfc_current_locus = cur_loc;
2564 /* Match an IMPLICIT statement, storing the types for
2565 gfc_set_implicit() if the statement is accepted by the parser.
2566 There is a strange looking, but legal syntactic construction
2567 possible. It looks like:
2569 IMPLICIT INTEGER (a-b) (c-d)
2571 This is legal if "a-b" is a constant expression that happens to
2572 equal one of the legal kinds for integers. The real problem
2573 happens with an implicit specification that looks like:
2575 IMPLICIT INTEGER (a-b)
2577 In this case, a typespec matcher that is "greedy" (as most of the
2578 matchers are) gobbles the character range as a kindspec, leaving
2579 nothing left. We therefore have to go a bit more slowly in the
2580 matching process by inhibiting the kindspec checking during
2581 typespec matching and checking for a kind later. */
2584 gfc_match_implicit (void)
2593 /* We don't allow empty implicit statements. */
2594 if (gfc_match_eos () == MATCH_YES)
2596 gfc_error ("Empty IMPLICIT statement at %C");
2602 /* First cleanup. */
2603 gfc_clear_new_implicit ();
2605 /* A basic type is mandatory here. */
2606 m = gfc_match_decl_type_spec (&ts, 1);
2607 if (m == MATCH_ERROR)
2612 cur_loc = gfc_current_locus;
2613 m = match_implicit_range ();
2617 /* We may have <TYPE> (<RANGE>). */
2618 gfc_gobble_whitespace ();
2619 c = gfc_next_ascii_char ();
2620 if ((c == '\n') || (c == ','))
2622 /* Check for CHARACTER with no length parameter. */
2623 if (ts.type == BT_CHARACTER && !ts.u.cl)
2625 ts.kind = gfc_default_character_kind;
2626 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2627 ts.u.cl->length = gfc_int_expr (1);
2630 /* Record the Successful match. */
2631 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2636 gfc_current_locus = cur_loc;
2639 /* Discard the (incorrectly) matched range. */
2640 gfc_clear_new_implicit ();
2642 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2643 if (ts.type == BT_CHARACTER)
2644 m = gfc_match_char_spec (&ts);
2647 m = gfc_match_kind_spec (&ts, false);
2650 m = gfc_match_old_kind_spec (&ts);
2651 if (m == MATCH_ERROR)
2657 if (m == MATCH_ERROR)
2660 m = match_implicit_range ();
2661 if (m == MATCH_ERROR)
2666 gfc_gobble_whitespace ();
2667 c = gfc_next_ascii_char ();
2668 if ((c != '\n') && (c != ','))
2671 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2679 gfc_syntax_error (ST_IMPLICIT);
2687 gfc_match_import (void)
2689 char name[GFC_MAX_SYMBOL_LEN + 1];
2694 if (gfc_current_ns->proc_name == NULL
2695 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2697 gfc_error ("IMPORT statement at %C only permitted in "
2698 "an INTERFACE body");
2702 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2706 if (gfc_match_eos () == MATCH_YES)
2708 /* All host variables should be imported. */
2709 gfc_current_ns->has_import_set = 1;
2713 if (gfc_match (" ::") == MATCH_YES)
2715 if (gfc_match_eos () == MATCH_YES)
2717 gfc_error ("Expecting list of named entities at %C");
2724 m = gfc_match (" %n", name);
2728 if (gfc_current_ns->parent != NULL
2729 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2731 gfc_error ("Type name '%s' at %C is ambiguous", name);
2734 else if (gfc_current_ns->proc_name->ns->parent != NULL
2735 && gfc_find_symbol (name,
2736 gfc_current_ns->proc_name->ns->parent,
2739 gfc_error ("Type name '%s' at %C is ambiguous", name);
2745 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2746 "at %C - does not exist.", name);
2750 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2752 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2757 st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
2760 sym->attr.imported = 1;
2772 if (gfc_match_eos () == MATCH_YES)
2774 if (gfc_match_char (',') != MATCH_YES)
2781 gfc_error ("Syntax error in IMPORT statement at %C");
2786 /* A minimal implementation of gfc_match without whitespace, escape
2787 characters or variable arguments. Returns true if the next
2788 characters match the TARGET template exactly. */
2791 match_string_p (const char *target)
2795 for (p = target; *p; p++)
2796 if ((char) gfc_next_ascii_char () != *p)
2801 /* Matches an attribute specification including array specs. If
2802 successful, leaves the variables current_attr and current_as
2803 holding the specification. Also sets the colon_seen variable for
2804 later use by matchers associated with initializations.
2806 This subroutine is a little tricky in the sense that we don't know
2807 if we really have an attr-spec until we hit the double colon.
2808 Until that time, we can only return MATCH_NO. This forces us to
2809 check for duplicate specification at this level. */
2812 match_attr_spec (void)
2814 /* Modifiers that can exist in a type statement. */
2816 { GFC_DECL_BEGIN = 0,
2817 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2818 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2819 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2820 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2821 DECL_IS_BIND_C, DECL_NONE,
2822 GFC_DECL_END /* Sentinel */
2826 /* GFC_DECL_END is the sentinel, index starts at 0. */
2827 #define NUM_DECL GFC_DECL_END
2829 locus start, seen_at[NUM_DECL];
2836 gfc_clear_attr (¤t_attr);
2837 start = gfc_current_locus;
2842 /* See if we get all of the keywords up to the final double colon. */
2843 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2851 gfc_gobble_whitespace ();
2853 ch = gfc_next_ascii_char ();
2856 /* This is the successful exit condition for the loop. */
2857 if (gfc_next_ascii_char () == ':')
2862 gfc_gobble_whitespace ();
2863 switch (gfc_peek_ascii_char ())
2866 if (match_string_p ("allocatable"))
2867 d = DECL_ALLOCATABLE;
2871 /* Try and match the bind(c). */
2872 m = gfc_match_bind_c (NULL, true);
2875 else if (m == MATCH_ERROR)
2880 if (match_string_p ("dimension"))
2885 if (match_string_p ("external"))
2890 if (match_string_p ("int"))
2892 ch = gfc_next_ascii_char ();
2895 if (match_string_p ("nt"))
2897 /* Matched "intent". */
2898 /* TODO: Call match_intent_spec from here. */
2899 if (gfc_match (" ( in out )") == MATCH_YES)
2901 else if (gfc_match (" ( in )") == MATCH_YES)
2903 else if (gfc_match (" ( out )") == MATCH_YES)
2909 if (match_string_p ("insic"))
2911 /* Matched "intrinsic". */
2919 if (match_string_p ("optional"))
2924 gfc_next_ascii_char ();
2925 switch (gfc_next_ascii_char ())
2928 if (match_string_p ("rameter"))
2930 /* Matched "parameter". */
2936 if (match_string_p ("inter"))
2938 /* Matched "pointer". */
2944 ch = gfc_next_ascii_char ();
2947 if (match_string_p ("vate"))
2949 /* Matched "private". */
2955 if (match_string_p ("tected"))
2957 /* Matched "protected". */
2964 if (match_string_p ("blic"))
2966 /* Matched "public". */
2974 if (match_string_p ("save"))
2979 if (match_string_p ("target"))
2984 gfc_next_ascii_char ();
2985 ch = gfc_next_ascii_char ();
2988 if (match_string_p ("lue"))
2990 /* Matched "value". */
2996 if (match_string_p ("latile"))
2998 /* Matched "volatile". */
3006 /* No double colon and no recognizable decl_type, so assume that
3007 we've been looking at something else the whole time. */
3014 /* Check to make sure any parens are paired up correctly. */
3015 if (gfc_match_parens () == MATCH_ERROR)
3022 seen_at[d] = gfc_current_locus;
3024 if (d == DECL_DIMENSION)
3026 m = gfc_match_array_spec (¤t_as);
3030 gfc_error ("Missing dimension specification at %C");
3034 if (m == MATCH_ERROR)
3039 /* Since we've seen a double colon, we have to be looking at an
3040 attr-spec. This means that we can now issue errors. */
3041 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3046 case DECL_ALLOCATABLE:
3047 attr = "ALLOCATABLE";
3049 case DECL_DIMENSION:
3056 attr = "INTENT (IN)";
3059 attr = "INTENT (OUT)";
3062 attr = "INTENT (IN OUT)";
3064 case DECL_INTRINSIC:
3070 case DECL_PARAMETER:
3076 case DECL_PROTECTED:
3091 case DECL_IS_BIND_C:
3101 attr = NULL; /* This shouldn't happen. */
3104 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3109 /* Now that we've dealt with duplicate attributes, add the attributes
3110 to the current attribute. */
3111 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3116 if (gfc_current_state () == COMP_DERIVED
3117 && d != DECL_DIMENSION && d != DECL_POINTER
3118 && d != DECL_PRIVATE && d != DECL_PUBLIC
3121 if (d == DECL_ALLOCATABLE)
3123 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3124 "attribute at %C in a TYPE definition")
3133 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3140 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3141 && gfc_current_state () != COMP_MODULE)
3143 if (d == DECL_PRIVATE)
3147 if (gfc_current_state () == COMP_DERIVED
3148 && gfc_state_stack->previous
3149 && gfc_state_stack->previous->state == COMP_MODULE)
3151 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3152 "at %L in a TYPE definition", attr,
3162 gfc_error ("%s attribute at %L is not allowed outside of the "
3163 "specification part of a module", attr, &seen_at[d]);
3171 case DECL_ALLOCATABLE:
3172 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
3175 case DECL_DIMENSION:
3176 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
3180 t = gfc_add_external (¤t_attr, &seen_at[d]);
3184 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
3188 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
3192 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
3195 case DECL_INTRINSIC:
3196 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
3200 t = gfc_add_optional (¤t_attr, &seen_at[d]);
3203 case DECL_PARAMETER:
3204 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
3208 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
3211 case DECL_PROTECTED:
3212 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3214 gfc_error ("PROTECTED at %C only allowed in specification "
3215 "part of a module");
3220 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3225 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
3229 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
3234 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
3239 t = gfc_add_save (¤t_attr, NULL, &seen_at[d]);
3243 t = gfc_add_target (¤t_attr, &seen_at[d]);
3246 case DECL_IS_BIND_C:
3247 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
3251 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3256 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
3260 if (gfc_notify_std (GFC_STD_F2003,
3261 "Fortran 2003: VOLATILE attribute at %C")
3265 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
3269 gfc_internal_error ("match_attr_spec(): Bad attribute");
3283 gfc_current_locus = start;
3284 gfc_free_array_spec (current_as);
3290 /* Set the binding label, dest_label, either with the binding label
3291 stored in the given gfc_typespec, ts, or if none was provided, it
3292 will be the symbol name in all lower case, as required by the draft
3293 (J3/04-007, section 15.4.1). If a binding label was given and
3294 there is more than one argument (num_idents), it is an error. */
3297 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3299 if (num_idents > 1 && has_name_equals)
3301 gfc_error ("Multiple identifiers provided with "
3302 "single NAME= specifier at %C");
3306 if (curr_binding_label[0] != '\0')
3308 /* Binding label given; store in temp holder til have sym. */
3309 strcpy (dest_label, curr_binding_label);
3313 /* No binding label given, and the NAME= specifier did not exist,
3314 which means there was no NAME="". */
3315 if (sym_name != NULL && has_name_equals == 0)
3316 strcpy (dest_label, sym_name);
3323 /* Set the status of the given common block as being BIND(C) or not,
3324 depending on the given parameter, is_bind_c. */
3327 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3329 com_block->is_bind_c = is_bind_c;
3334 /* Verify that the given gfc_typespec is for a C interoperable type. */
3337 verify_c_interop (gfc_typespec *ts)
3339 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3340 return (ts->u.derived->ts.is_c_interop ? SUCCESS : FAILURE);
3341 else if (ts->is_c_interop != 1)
3348 /* Verify that the variables of a given common block, which has been
3349 defined with the attribute specifier bind(c), to be of a C
3350 interoperable type. Errors will be reported here, if
3354 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3356 gfc_symbol *curr_sym = NULL;
3357 gfc_try retval = SUCCESS;
3359 curr_sym = com_block->head;
3361 /* Make sure we have at least one symbol. */
3362 if (curr_sym == NULL)
3365 /* Here we know we have a symbol, so we'll execute this loop
3369 /* The second to last param, 1, says this is in a common block. */
3370 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3371 curr_sym = curr_sym->common_next;
3372 } while (curr_sym != NULL);
3378 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3379 an appropriate error message is reported. */
3382 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3383 int is_in_common, gfc_common_head *com_block)
3385 bool bind_c_function = false;
3386 gfc_try retval = SUCCESS;
3388 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3389 bind_c_function = true;
3391 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3393 tmp_sym = tmp_sym->result;
3394 /* Make sure it wasn't an implicitly typed result. */
3395 if (tmp_sym->attr.implicit_type)
3397 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3398 "%L may not be C interoperable", tmp_sym->name,
3399 &tmp_sym->declared_at);
3400 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3401 /* Mark it as C interoperable to prevent duplicate warnings. */
3402 tmp_sym->ts.is_c_interop = 1;
3403 tmp_sym->attr.is_c_interop = 1;
3407 /* Here, we know we have the bind(c) attribute, so if we have
3408 enough type info, then verify that it's a C interop kind.
3409 The info could be in the symbol already, or possibly still in
3410 the given ts (current_ts), so look in both. */
3411 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3413 if (verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3415 /* See if we're dealing with a sym in a common block or not. */
3416 if (is_in_common == 1)
3418 gfc_warning ("Variable '%s' in common block '%s' at %L "
3419 "may not be a C interoperable "
3420 "kind though common block '%s' is BIND(C)",
3421 tmp_sym->name, com_block->name,
3422 &(tmp_sym->declared_at), com_block->name);
3426 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3427 gfc_error ("Type declaration '%s' at %L is not C "
3428 "interoperable but it is BIND(C)",
3429 tmp_sym->name, &(tmp_sym->declared_at));
3431 gfc_warning ("Variable '%s' at %L "
3432 "may not be a C interoperable "
3433 "kind but it is bind(c)",
3434 tmp_sym->name, &(tmp_sym->declared_at));
3438 /* Variables declared w/in a common block can't be bind(c)
3439 since there's no way for C to see these variables, so there's
3440 semantically no reason for the attribute. */
3441 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3443 gfc_error ("Variable '%s' in common block '%s' at "
3444 "%L cannot be declared with BIND(C) "
3445 "since it is not a global",
3446 tmp_sym->name, com_block->name,
3447 &(tmp_sym->declared_at));
3451 /* Scalar variables that are bind(c) can not have the pointer
3452 or allocatable attributes. */
3453 if (tmp_sym->attr.is_bind_c == 1)
3455 if (tmp_sym->attr.pointer == 1)
3457 gfc_error ("Variable '%s' at %L cannot have both the "
3458 "POINTER and BIND(C) attributes",
3459 tmp_sym->name, &(tmp_sym->declared_at));
3463 if (tmp_sym->attr.allocatable == 1)
3465 gfc_error ("Variable '%s' at %L cannot have both the "
3466 "ALLOCATABLE and BIND(C) attributes",
3467 tmp_sym->name, &(tmp_sym->declared_at));
3473 /* If it is a BIND(C) function, make sure the return value is a
3474 scalar value. The previous tests in this function made sure
3475 the type is interoperable. */
3476 if (bind_c_function && tmp_sym->as != NULL)
3477 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3478 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3480 /* BIND(C) functions can not return a character string. */
3481 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3482 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
3483 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
3484 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
3485 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3486 "be a character string", tmp_sym->name,
3487 &(tmp_sym->declared_at));
3490 /* See if the symbol has been marked as private. If it has, make sure
3491 there is no binding label and warn the user if there is one. */
3492 if (tmp_sym->attr.access == ACCESS_PRIVATE
3493 && tmp_sym->binding_label[0] != '\0')
3494 /* Use gfc_warning_now because we won't say that the symbol fails
3495 just because of this. */
3496 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3497 "given the binding label '%s'", tmp_sym->name,
3498 &(tmp_sym->declared_at), tmp_sym->binding_label);
3504 /* Set the appropriate fields for a symbol that's been declared as
3505 BIND(C) (the is_bind_c flag and the binding label), and verify that
3506 the type is C interoperable. Errors are reported by the functions
3507 used to set/test these fields. */
3510 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3512 gfc_try retval = SUCCESS;
3514 /* TODO: Do we need to make sure the vars aren't marked private? */
3516 /* Set the is_bind_c bit in symbol_attribute. */
3517 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3519 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3520 num_idents) != SUCCESS)
3527 /* Set the fields marking the given common block as BIND(C), including
3528 a binding label, and report any errors encountered. */
3531 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3533 gfc_try retval = SUCCESS;
3535 /* destLabel, common name, typespec (which may have binding label). */
3536 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3540 /* Set the given common block (com_block) to being bind(c) (1). */
3541 set_com_block_bind_c (com_block, 1);
3547 /* Retrieve the list of one or more identifiers that the given bind(c)
3548 attribute applies to. */
3551 get_bind_c_idents (void)
3553 char name[GFC_MAX_SYMBOL_LEN + 1];
3555 gfc_symbol *tmp_sym = NULL;
3557 gfc_common_head *com_block = NULL;
3559 if (gfc_match_name (name) == MATCH_YES)
3561 found_id = MATCH_YES;
3562 gfc_get_ha_symbol (name, &tmp_sym);
3564 else if (match_common_name (name) == MATCH_YES)
3566 found_id = MATCH_YES;
3567 com_block = gfc_get_common (name, 0);
3571 gfc_error ("Need either entity or common block name for "
3572 "attribute specification statement at %C");
3576 /* Save the current identifier and look for more. */
3579 /* Increment the number of identifiers found for this spec stmt. */
3582 /* Make sure we have a sym or com block, and verify that it can
3583 be bind(c). Set the appropriate field(s) and look for more
3585 if (tmp_sym != NULL || com_block != NULL)
3587 if (tmp_sym != NULL)
3589 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3595 if (set_verify_bind_c_com_block(com_block, num_idents)
3600 /* Look to see if we have another identifier. */
3602 if (gfc_match_eos () == MATCH_YES)
3603 found_id = MATCH_NO;
3604 else if (gfc_match_char (',') != MATCH_YES)
3605 found_id = MATCH_NO;
3606 else if (gfc_match_name (name) == MATCH_YES)
3608 found_id = MATCH_YES;
3609 gfc_get_ha_symbol (name, &tmp_sym);
3611 else if (match_common_name (name) == MATCH_YES)
3613 found_id = MATCH_YES;
3614 com_block = gfc_get_common (name, 0);
3618 gfc_error ("Missing entity or common block name for "
3619 "attribute specification statement at %C");
3625 gfc_internal_error ("Missing symbol");
3627 } while (found_id == MATCH_YES);
3629 /* if we get here we were successful */
3634 /* Try and match a BIND(C) attribute specification statement. */
3637 gfc_match_bind_c_stmt (void)
3639 match found_match = MATCH_NO;
3644 /* This may not be necessary. */
3646 /* Clear the temporary binding label holder. */
3647 curr_binding_label[0] = '\0';
3649 /* Look for the bind(c). */
3650 found_match = gfc_match_bind_c (NULL, true);
3652 if (found_match == MATCH_YES)
3654 /* Look for the :: now, but it is not required. */
3657 /* Get the identifier(s) that needs to be updated. This may need to
3658 change to hand the flag(s) for the attr specified so all identifiers
3659 found can have all appropriate parts updated (assuming that the same
3660 spec stmt can have multiple attrs, such as both bind(c) and
3662 if (get_bind_c_idents () != SUCCESS)
3663 /* Error message should have printed already. */
3671 /* Match a data declaration statement. */
3674 gfc_match_data_decl (void)
3680 num_idents_on_line = 0;
3682 m = gfc_match_decl_type_spec (¤t_ts, 0);
3686 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3688 sym = gfc_use_derived (current_ts.u.derived);
3696 current_ts.u.derived = sym;
3699 m = match_attr_spec ();
3700 if (m == MATCH_ERROR)
3706 if (current_ts.type == BT_DERIVED && current_ts.u.derived->components == NULL
3707 && !current_ts.u.derived->attr.zero_comp)
3710 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3713 gfc_find_symbol (current_ts.u.derived->name,
3714 current_ts.u.derived->ns->parent, 1, &sym);
3716 /* Any symbol that we find had better be a type definition
3717 which has its components defined. */
3718 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3719 && (current_ts.u.derived->components != NULL
3720 || current_ts.u.derived->attr.zero_comp))
3723 /* Now we have an error, which we signal, and then fix up
3724 because the knock-on is plain and simple confusing. */
3725 gfc_error_now ("Derived type at %C has not been previously defined "
3726 "and so cannot appear in a derived type definition");
3727 current_attr.pointer = 1;
3732 /* If we have an old-style character declaration, and no new-style
3733 attribute specifications, then there a comma is optional between
3734 the type specification and the variable list. */
3735 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3736 gfc_match_char (',');
3738 /* Give the types/attributes to symbols that follow. Give the element
3739 a number so that repeat character length expressions can be copied. */
3743 num_idents_on_line++;
3744 m = variable_decl (elem++);
3745 if (m == MATCH_ERROR)
3750 if (gfc_match_eos () == MATCH_YES)
3752 if (gfc_match_char (',') != MATCH_YES)
3756 if (gfc_error_flag_test () == 0)
3757 gfc_error ("Syntax error in data declaration at %C");
3760 gfc_free_data_all (gfc_current_ns);
3763 gfc_free_array_spec (current_as);
3769 /* Match a prefix associated with a function or subroutine
3770 declaration. If the typespec pointer is nonnull, then a typespec
3771 can be matched. Note that if nothing matches, MATCH_YES is
3772 returned (the null string was matched). */
3775 gfc_match_prefix (gfc_typespec *ts)
3779 gfc_clear_attr (¤t_attr);
3782 gcc_assert (!gfc_matching_prefix);
3783 gfc_matching_prefix = true;
3786 if (!seen_type && ts != NULL
3787 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
3788 && gfc_match_space () == MATCH_YES)
3795 if (gfc_match ("elemental% ") == MATCH_YES)
3797 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
3803 if (gfc_match ("pure% ") == MATCH_YES)
3805 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
3811 if (gfc_match ("recursive% ") == MATCH_YES)
3813 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
3819 /* At this point, the next item is not a prefix. */
3820 gcc_assert (gfc_matching_prefix);
3821 gfc_matching_prefix = false;
3825 gcc_assert (gfc_matching_prefix);
3826 gfc_matching_prefix = false;
3831 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
3834 copy_prefix (symbol_attribute *dest, locus *where)
3836 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3839 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3842 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3849 /* Match a formal argument list. */
3852 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3854 gfc_formal_arglist *head, *tail, *p, *q;
3855 char name[GFC_MAX_SYMBOL_LEN + 1];
3861 if (gfc_match_char ('(') != MATCH_YES)
3868 if (gfc_match_char (')') == MATCH_YES)
3873 if (gfc_match_char ('*') == MATCH_YES)
3877 m = gfc_match_name (name);
3881 if (gfc_get_symbol (name, NULL, &sym))
3885 p = gfc_get_formal_arglist ();
3897 /* We don't add the VARIABLE flavor because the name could be a
3898 dummy procedure. We don't apply these attributes to formal
3899 arguments of statement functions. */
3900 if (sym != NULL && !st_flag
3901 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3902 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3908 /* The name of a program unit can be in a different namespace,
3909 so check for it explicitly. After the statement is accepted,
3910 the name is checked for especially in gfc_get_symbol(). */
3911 if (gfc_new_block != NULL && sym != NULL
3912 && strcmp (sym->name, gfc_new_block->name) == 0)
3914 gfc_error ("Name '%s' at %C is the name of the procedure",
3920 if (gfc_match_char (')') == MATCH_YES)
3923 m = gfc_match_char (',');
3926 gfc_error ("Unexpected junk in formal argument list at %C");
3932 /* Check for duplicate symbols in the formal argument list. */
3935 for (p = head; p->next; p = p->next)
3940 for (q = p->next; q; q = q->next)
3941 if (p->sym == q->sym)
3943 gfc_error ("Duplicate symbol '%s' in formal argument list "
3944 "at %C", p->sym->name);
3952 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
3962 gfc_free_formal_arglist (head);
3967 /* Match a RESULT specification following a function declaration or
3968 ENTRY statement. Also matches the end-of-statement. */
3971 match_result (gfc_symbol *function, gfc_symbol **result)
3973 char name[GFC_MAX_SYMBOL_LEN + 1];
3977 if (gfc_match (" result (") != MATCH_YES)
3980 m = gfc_match_name (name);
3984 /* Get the right paren, and that's it because there could be the
3985 bind(c) attribute after the result clause. */
3986 if (gfc_match_char(')') != MATCH_YES)
3988 /* TODO: should report the missing right paren here. */
3992 if (strcmp (function->name, name) == 0)
3994 gfc_error ("RESULT variable at %C must be different than function name");
3998 if (gfc_get_symbol (name, NULL, &r))
4001 if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4010 /* Match a function suffix, which could be a combination of a result
4011 clause and BIND(C), either one, or neither. The draft does not
4012 require them to come in a specific order. */
4015 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4017 match is_bind_c; /* Found bind(c). */
4018 match is_result; /* Found result clause. */
4019 match found_match; /* Status of whether we've found a good match. */
4020 char peek_char; /* Character we're going to peek at. */
4021 bool allow_binding_name;
4023 /* Initialize to having found nothing. */
4024 found_match = MATCH_NO;
4025 is_bind_c = MATCH_NO;
4026 is_result = MATCH_NO;
4028 /* Get the next char to narrow between result and bind(c). */
4029 gfc_gobble_whitespace ();
4030 peek_char = gfc_peek_ascii_char ();
4032 /* C binding names are not allowed for internal procedures. */
4033 if (gfc_current_state () == COMP_CONTAINS
4034 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4035 allow_binding_name = false;
4037 allow_binding_name = true;
4042 /* Look for result clause. */
4043 is_result = match_result (sym, result);
4044 if (is_result == MATCH_YES)
4046 /* Now see if there is a bind(c) after it. */
4047 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4048 /* We've found the result clause and possibly bind(c). */
4049 found_match = MATCH_YES;
4052 /* This should only be MATCH_ERROR. */
4053 found_match = is_result;
4056 /* Look for bind(c) first. */
4057 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4058 if (is_bind_c == MATCH_YES)
4060 /* Now see if a result clause followed it. */
4061 is_result = match_result (sym, result);
4062 found_match = MATCH_YES;
4066 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4067 found_match = MATCH_ERROR;
4071 gfc_error ("Unexpected junk after function declaration at %C");
4072 found_match = MATCH_ERROR;
4076 if (is_bind_c == MATCH_YES)
4078 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4079 if (gfc_current_state () == COMP_CONTAINS
4080 && sym->ns->proc_name->attr.flavor != FL_MODULE
4081 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4082 "at %L may not be specified for an internal "
4083 "procedure", &gfc_current_locus)
4087 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4096 /* Procedure pointer return value without RESULT statement:
4097 Add "hidden" result variable named "ppr@". */
4100 add_hidden_procptr_result (gfc_symbol *sym)
4104 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4107 /* First usage case: PROCEDURE and EXTERNAL statements. */
4108 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4109 && strcmp (gfc_current_block ()->name, sym->name) == 0
4110 && sym->attr.external;
4111 /* Second usage case: INTERFACE statements. */
4112 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4113 && gfc_state_stack->previous->state == COMP_FUNCTION
4114 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4120 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4124 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4125 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4126 st2->n.sym = stree->n.sym;
4128 sym->result = stree->n.sym;
4130 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4131 sym->result->attr.pointer = sym->attr.pointer;
4132 sym->result->attr.external = sym->attr.external;
4133 sym->result->attr.referenced = sym->attr.referenced;
4134 sym->result->ts = sym->ts;
4135 sym->attr.proc_pointer = 0;
4136 sym->attr.pointer = 0;
4137 sym->attr.external = 0;
4138 if (sym->result->attr.external && sym->result->attr.pointer)
4140 sym->result->attr.pointer = 0;
4141 sym->result->attr.proc_pointer = 1;
4144 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4146 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4147 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4148 && sym->result && sym->result != sym && sym->result->attr.external
4149 && sym == gfc_current_ns->proc_name
4150 && sym == sym->result->ns->proc_name
4151 && strcmp ("ppr@", sym->result->name) == 0)
4153 sym->result->attr.proc_pointer = 1;
4154 sym->attr.pointer = 0;
4162 /* Match the interface for a PROCEDURE declaration,
4163 including brackets (R1212). */
4166 match_procedure_interface (gfc_symbol **proc_if)
4170 locus old_loc, entry_loc;
4171 gfc_namespace *old_ns = gfc_current_ns;
4172 char name[GFC_MAX_SYMBOL_LEN + 1];
4174 old_loc = entry_loc = gfc_current_locus;
4175 gfc_clear_ts (¤t_ts);
4177 if (gfc_match (" (") != MATCH_YES)
4179 gfc_current_locus = entry_loc;
4183 /* Get the type spec. for the procedure interface. */
4184 old_loc = gfc_current_locus;
4185 m = gfc_match_decl_type_spec (¤t_ts, 0);
4186 gfc_gobble_whitespace ();
4187 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4190 if (m == MATCH_ERROR)
4193 /* Procedure interface is itself a procedure. */
4194 gfc_current_locus = old_loc;
4195 m = gfc_match_name (name);
4197 /* First look to see if it is already accessible in the current
4198 namespace because it is use associated or contained. */
4200 if (gfc_find_sym_tree (name, NULL, 0, &st))
4203 /* If it is still not found, then try the parent namespace, if it
4204 exists and create the symbol there if it is still not found. */
4205 if (gfc_current_ns->parent)
4206 gfc_current_ns = gfc_current_ns->parent;
4207 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4210 gfc_current_ns = old_ns;
4211 *proc_if = st->n.sym;
4213 /* Various interface checks. */
4217 /* Resolve interface if possible. That way, attr.procedure is only set
4218 if it is declared by a later procedure-declaration-stmt, which is
4219 invalid per C1212. */
4220 while ((*proc_if)->ts.interface)
4221 *proc_if = (*proc_if)->ts.interface;
4223 if ((*proc_if)->generic)
4225 gfc_error ("Interface '%s' at %C may not be generic",
4229 if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
4231 gfc_error ("Interface '%s' at %C may not be a statement function",
4235 /* Handle intrinsic procedures. */
4236 if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4237 || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4238 && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4239 || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4240 (*proc_if)->attr.intrinsic = 1;
4241 if ((*proc_if)->attr.intrinsic
4242 && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
4244 gfc_error ("Intrinsic procedure '%s' not allowed "
4245 "in PROCEDURE statement at %C", (*proc_if)->name);
4251 if (gfc_match (" )") != MATCH_YES)
4253 gfc_current_locus = entry_loc;
4261 /* Match a PROCEDURE declaration (R1211). */
4264 match_procedure_decl (void)
4267 gfc_symbol *sym, *proc_if = NULL;
4269 gfc_expr *initializer = NULL;
4271 /* Parse interface (with brackets). */
4272 m = match_procedure_interface (&proc_if);
4276 /* Parse attributes (with colons). */
4277 m = match_attr_spec();
4278 if (m == MATCH_ERROR)
4281 /* Get procedure symbols. */
4284 m = gfc_match_symbol (&sym, 0);
4287 else if (m == MATCH_ERROR)
4290 /* Add current_attr to the symbol attributes. */
4291 if (gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
4294 if (sym->attr.is_bind_c)
4296 /* Check for C1218. */
4297 if (!proc_if || !proc_if->attr.is_bind_c)
4299 gfc_error ("BIND(C) attribute at %C requires "
4300 "an interface with BIND(C)");
4303 /* Check for C1217. */
4304 if (has_name_equals && sym->attr.pointer)
4306 gfc_error ("BIND(C) procedure with NAME may not have "
4307 "POINTER attribute at %C");
4310 if (has_name_equals && sym->attr.dummy)
4312 gfc_error ("Dummy procedure at %C may not have "
4313 "BIND(C) attribute with NAME");
4316 /* Set binding label for BIND(C). */
4317 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4321 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4324 if (add_hidden_procptr_result (sym) == SUCCESS)
4327 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4330 /* Set interface. */
4331 if (proc_if != NULL)
4333 if (sym->ts.type != BT_UNKNOWN)
4335 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4336 sym->name, &gfc_current_locus,
4337 gfc_basic_typename (sym->ts.type));
4340 sym->ts.interface = proc_if;
4341 sym->attr.untyped = 1;
4342 sym->attr.if_source = IFSRC_IFBODY;
4344 else if (current_ts.type != BT_UNKNOWN)
4346 if (gfc_add_type (sym, ¤t_ts, &gfc_current_locus) == FAILURE)
4348 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4349 sym->ts.interface->ts = current_ts;
4350 sym->ts.interface->attr.function = 1;
4351 sym->attr.function = sym->ts.interface->attr.function;
4352 sym->attr.if_source = IFSRC_UNKNOWN;
4355 if (gfc_match (" =>") == MATCH_YES)
4357 if (!current_attr.pointer)
4359 gfc_error ("Initialization at %C isn't for a pointer variable");
4364 m = gfc_match_null (&initializer);
4367 gfc_error ("Pointer initialization requires a NULL() at %C");
4371 if (gfc_pure (NULL))
4373 gfc_error ("Initialization of pointer at %C is not allowed in "
4374 "a PURE procedure");
4381 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4387 gfc_set_sym_referenced (sym);
4389 if (gfc_match_eos () == MATCH_YES)
4391 if (gfc_match_char (',') != MATCH_YES)
4396 gfc_error ("Syntax error in PROCEDURE statement at %C");
4400 /* Free stuff up and return. */
4401 gfc_free_expr (initializer);
4407 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4410 /* Match a procedure pointer component declaration (R445). */
4413 match_ppc_decl (void)
4416 gfc_symbol *proc_if = NULL;
4420 gfc_expr *initializer = NULL;
4421 gfc_typebound_proc* tb;
4422 char name[GFC_MAX_SYMBOL_LEN + 1];
4424 /* Parse interface (with brackets). */
4425 m = match_procedure_interface (&proc_if);
4429 /* Parse attributes. */
4430 tb = XCNEW (gfc_typebound_proc);
4431 tb->where = gfc_current_locus;
4432 m = match_binding_attributes (tb, false, true);
4433 if (m == MATCH_ERROR)
4436 gfc_clear_attr (¤t_attr);
4437 current_attr.procedure = 1;
4438 current_attr.proc_pointer = 1;
4439 current_attr.access = tb->access;
4440 current_attr.flavor = FL_PROCEDURE;
4442 /* Match the colons (required). */
4443 if (gfc_match (" ::") != MATCH_YES)
4445 gfc_error ("Expected '::' after binding-attributes at %C");
4449 /* Check for C450. */
4450 if (!tb->nopass && proc_if == NULL)
4452 gfc_error("NOPASS or explicit interface required at %C");
4456 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure pointer "
4457 "component at %C") == FAILURE)
4460 /* Match PPC names. */
4464 m = gfc_match_name (name);
4467 else if (m == MATCH_ERROR)
4470 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
4473 /* Add current_attr to the symbol attributes. */
4474 if (gfc_copy_attr (&c->attr, ¤t_attr, NULL) == FAILURE)
4477 if (gfc_add_external (&c->attr, NULL) == FAILURE)
4480 if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
4485 /* Set interface. */
4486 if (proc_if != NULL)
4488 c->ts.interface = proc_if;
4489 c->attr.untyped = 1;
4490 c->attr.if_source = IFSRC_IFBODY;
4492 else if (ts.type != BT_UNKNOWN)
4495 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4496 c->ts.interface->ts = ts;
4497 c->ts.interface->attr.function = 1;
4498 c->attr.function = c->ts.interface->attr.function;
4499 c->attr.if_source = IFSRC_UNKNOWN;
4502 if (gfc_match (" =>") == MATCH_YES)
4504 m = gfc_match_null (&initializer);
4507 gfc_error ("Pointer initialization requires a NULL() at %C");
4510 if (gfc_pure (NULL))
4512 gfc_error ("Initialization of pointer at %C is not allowed in "
4513 "a PURE procedure");
4518 gfc_free_expr (initializer);
4521 c->initializer = initializer;
4524 if (gfc_match_eos () == MATCH_YES)
4526 if (gfc_match_char (',') != MATCH_YES)
4531 gfc_error ("Syntax error in procedure pointer component at %C");
4536 /* Match a PROCEDURE declaration inside an interface (R1206). */
4539 match_procedure_in_interface (void)
4543 char name[GFC_MAX_SYMBOL_LEN + 1];
4545 if (current_interface.type == INTERFACE_NAMELESS
4546 || current_interface.type == INTERFACE_ABSTRACT)
4548 gfc_error ("PROCEDURE at %C must be in a generic interface");
4554 m = gfc_match_name (name);
4557 else if (m == MATCH_ERROR)
4559 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4562 if (gfc_add_interface (sym) == FAILURE)
4565 if (gfc_match_eos () == MATCH_YES)
4567 if (gfc_match_char (',') != MATCH_YES)
4574 gfc_error ("Syntax error in PROCEDURE statement at %C");
4579 /* General matcher for PROCEDURE declarations. */
4581 static match match_procedure_in_type (void);
4584 gfc_match_procedure (void)
4588 switch (gfc_current_state ())
4593 case COMP_SUBROUTINE:
4595 m = match_procedure_decl ();
4597 case COMP_INTERFACE:
4598 m = match_procedure_in_interface ();
4601 m = match_ppc_decl ();
4603 case COMP_DERIVED_CONTAINS:
4604 m = match_procedure_in_type ();
4613 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4621 /* Warn if a matched procedure has the same name as an intrinsic; this is
4622 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4623 parser-state-stack to find out whether we're in a module. */
4626 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
4630 in_module = (gfc_state_stack->previous
4631 && gfc_state_stack->previous->state == COMP_MODULE);
4633 gfc_warn_intrinsic_shadow (sym, in_module, func);
4637 /* Match a function declaration. */
4640 gfc_match_function_decl (void)
4642 char name[GFC_MAX_SYMBOL_LEN + 1];
4643 gfc_symbol *sym, *result;
4647 match found_match; /* Status returned by match func. */
4649 if (gfc_current_state () != COMP_NONE
4650 && gfc_current_state () != COMP_INTERFACE
4651 && gfc_current_state () != COMP_CONTAINS)
4654 gfc_clear_ts (¤t_ts);
4656 old_loc = gfc_current_locus;
4658 m = gfc_match_prefix (¤t_ts);
4661 gfc_current_locus = old_loc;
4665 if (gfc_match ("function% %n", name) != MATCH_YES)
4667 gfc_current_locus = old_loc;
4670 if (get_proc_name (name, &sym, false))
4673 if (add_hidden_procptr_result (sym) == SUCCESS)
4676 gfc_new_block = sym;
4678 m = gfc_match_formal_arglist (sym, 0, 0);
4681 gfc_error ("Expected formal argument list in function "
4682 "definition at %C");
4686 else if (m == MATCH_ERROR)
4691 /* According to the draft, the bind(c) and result clause can
4692 come in either order after the formal_arg_list (i.e., either
4693 can be first, both can exist together or by themselves or neither
4694 one). Therefore, the match_result can't match the end of the
4695 string, and check for the bind(c) or result clause in either order. */
4696 found_match = gfc_match_eos ();
4698 /* Make sure that it isn't already declared as BIND(C). If it is, it
4699 must have been marked BIND(C) with a BIND(C) attribute and that is
4700 not allowed for procedures. */
4701 if (sym->attr.is_bind_c == 1)
4703 sym->attr.is_bind_c = 0;
4704 if (sym->old_symbol != NULL)
4705 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4706 "variables or common blocks",
4707 &(sym->old_symbol->declared_at));
4709 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4710 "variables or common blocks", &gfc_current_locus);
4713 if (found_match != MATCH_YES)
4715 /* If we haven't found the end-of-statement, look for a suffix. */
4716 suffix_match = gfc_match_suffix (sym, &result);
4717 if (suffix_match == MATCH_YES)
4718 /* Need to get the eos now. */
4719 found_match = gfc_match_eos ();
4721 found_match = suffix_match;
4724 if(found_match != MATCH_YES)
4728 /* Make changes to the symbol. */
4731 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4734 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4735 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4738 /* Delay matching the function characteristics until after the
4739 specification block by signalling kind=-1. */
4740 sym->declared_at = old_loc;
4741 if (current_ts.type != BT_UNKNOWN)
4742 current_ts.kind = -1;
4744 current_ts.kind = 0;
4748 if (current_ts.type != BT_UNKNOWN
4749 && gfc_add_type (sym, ¤t_ts, &gfc_current_locus) == FAILURE)
4755 if (current_ts.type != BT_UNKNOWN
4756 && gfc_add_type (result, ¤t_ts, &gfc_current_locus)
4759 sym->result = result;
4762 /* Warn if this procedure has the same name as an intrinsic. */
4763 warn_intrinsic_shadow (sym, true);
4769 gfc_current_locus = old_loc;
4774 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4775 pass the name of the entry, rather than the gfc_current_block name, and
4776 to return false upon finding an existing global entry. */
4779 add_global_entry (const char *name, int sub)
4782 enum gfc_symbol_type type;
4784 s = gfc_get_gsymbol(name);
4785 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4788 || (s->type != GSYM_UNKNOWN
4789 && s->type != type))
4790 gfc_global_used(s, NULL);
4794 s->where = gfc_current_locus;
4796 s->ns = gfc_current_ns;
4803 /* Match an ENTRY statement. */
4806 gfc_match_entry (void)
4811 char name[GFC_MAX_SYMBOL_LEN + 1];
4812 gfc_compile_state state;
4816 bool module_procedure;
4820 m = gfc_match_name (name);
4824 state = gfc_current_state ();
4825 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
4830 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4833 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4835 case COMP_BLOCK_DATA:
4836 gfc_error ("ENTRY statement at %C cannot appear within "
4839 case COMP_INTERFACE:
4840 gfc_error ("ENTRY statement at %C cannot appear within "
4844 gfc_error ("ENTRY statement at %C cannot appear within "
4845 "a DERIVED TYPE block");
4848 gfc_error ("ENTRY statement at %C cannot appear within "
4849 "an IF-THEN block");
4852 gfc_error ("ENTRY statement at %C cannot appear within "
4856 gfc_error ("ENTRY statement at %C cannot appear within "
4860 gfc_error ("ENTRY statement at %C cannot appear within "
4864 gfc_error ("ENTRY statement at %C cannot appear within "
4868 gfc_error ("ENTRY statement at %C cannot appear within "
4869 "a contained subprogram");
4872 gfc_internal_error ("gfc_match_entry(): Bad state");
4877 module_procedure = gfc_current_ns->parent != NULL
4878 && gfc_current_ns->parent->proc_name
4879 && gfc_current_ns->parent->proc_name->attr.flavor
4882 if (gfc_current_ns->parent != NULL
4883 && gfc_current_ns->parent->proc_name
4884 && !module_procedure)
4886 gfc_error("ENTRY statement at %C cannot appear in a "
4887 "contained procedure");
4891 /* Module function entries need special care in get_proc_name
4892 because previous references within the function will have
4893 created symbols attached to the current namespace. */
4894 if (get_proc_name (name, &entry,
4895 gfc_current_ns->parent != NULL
4896 && module_procedure))
4899 proc = gfc_current_block ();
4901 /* Make sure that it isn't already declared as BIND(C). If it is, it
4902 must have been marked BIND(C) with a BIND(C) attribute and that is
4903 not allowed for procedures. */
4904 if (entry->attr.is_bind_c == 1)
4906 entry->attr.is_bind_c = 0;
4907 if (entry->old_symbol != NULL)
4908 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4909 "variables or common blocks",
4910 &(entry->old_symbol->declared_at));
4912 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4913 "variables or common blocks", &gfc_current_locus);
4916 /* Check what next non-whitespace character is so we can tell if there
4917 is the required parens if we have a BIND(C). */
4918 gfc_gobble_whitespace ();
4919 peek_char = gfc_peek_ascii_char ();
4921 if (state == COMP_SUBROUTINE)
4923 /* An entry in a subroutine. */
4924 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
4927 m = gfc_match_formal_arglist (entry, 0, 1);
4931 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
4932 never be an internal procedure. */
4933 is_bind_c = gfc_match_bind_c (entry, true);
4934 if (is_bind_c == MATCH_ERROR)
4936 if (is_bind_c == MATCH_YES)
4938 if (peek_char != '(')
4940 gfc_error ("Missing required parentheses before BIND(C) at %C");
4943 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
4948 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4949 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
4954 /* An entry in a function.
4955 We need to take special care because writing
4960 ENTRY f() RESULT (r)
4962 ENTRY f RESULT (r). */
4963 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
4966 old_loc = gfc_current_locus;
4967 if (gfc_match_eos () == MATCH_YES)
4969 gfc_current_locus = old_loc;
4970 /* Match the empty argument list, and add the interface to
4972 m = gfc_match_formal_arglist (entry, 0, 1);
4975 m = gfc_match_formal_arglist (entry, 0, 0);
4982 if (gfc_match_eos () == MATCH_YES)
4984 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
4985 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
4988 entry->result = entry;
4992 m = gfc_match_suffix (entry, &result);
4994 gfc_syntax_error (ST_ENTRY);
5000 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
5001 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
5002 || gfc_add_function (&entry->attr, result->name, NULL)
5005 entry->result = result;
5009 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5010 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5012 entry->result = entry;
5017 if (gfc_match_eos () != MATCH_YES)
5019 gfc_syntax_error (ST_ENTRY);
5023 entry->attr.recursive = proc->attr.recursive;
5024 entry->attr.elemental = proc->attr.elemental;
5025 entry->attr.pure = proc->attr.pure;
5027 el = gfc_get_entry_list ();
5029 el->next = gfc_current_ns->entries;
5030 gfc_current_ns->entries = el;
5032 el->id = el->next->id + 1;
5036 new_st.op = EXEC_ENTRY;
5037 new_st.ext.entry = el;
5043 /* Match a subroutine statement, including optional prefixes. */
5046 gfc_match_subroutine (void)
5048 char name[GFC_MAX_SYMBOL_LEN + 1];
5053 bool allow_binding_name;
5055 if (gfc_current_state () != COMP_NONE
5056 && gfc_current_state () != COMP_INTERFACE
5057 && gfc_current_state () != COMP_CONTAINS)
5060 m = gfc_match_prefix (NULL);
5064 m = gfc_match ("subroutine% %n", name);
5068 if (get_proc_name (name, &sym, false))
5071 if (add_hidden_procptr_result (sym) == SUCCESS)
5074 gfc_new_block = sym;
5076 /* Check what next non-whitespace character is so we can tell if there
5077 is the required parens if we have a BIND(C). */
5078 gfc_gobble_whitespace ();
5079 peek_char = gfc_peek_ascii_char ();
5081 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5084 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5087 /* Make sure that it isn't already declared as BIND(C). If it is, it
5088 must have been marked BIND(C) with a BIND(C) attribute and that is
5089 not allowed for procedures. */
5090 if (sym->attr.is_bind_c == 1)
5092 sym->attr.is_bind_c = 0;
5093 if (sym->old_symbol != NULL)
5094 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5095 "variables or common blocks",
5096 &(sym->old_symbol->declared_at));
5098 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5099 "variables or common blocks", &gfc_current_locus);
5102 /* C binding names are not allowed for internal procedures. */
5103 if (gfc_current_state () == COMP_CONTAINS
5104 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5105 allow_binding_name = false;
5107 allow_binding_name = true;
5109 /* Here, we are just checking if it has the bind(c) attribute, and if
5110 so, then we need to make sure it's all correct. If it doesn't,
5111 we still need to continue matching the rest of the subroutine line. */
5112 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5113 if (is_bind_c == MATCH_ERROR)
5115 /* There was an attempt at the bind(c), but it was wrong. An
5116 error message should have been printed w/in the gfc_match_bind_c
5117 so here we'll just return the MATCH_ERROR. */
5121 if (is_bind_c == MATCH_YES)
5123 /* The following is allowed in the Fortran 2008 draft. */
5124 if (gfc_current_state () == COMP_CONTAINS
5125 && sym->ns->proc_name->attr.flavor != FL_MODULE
5126 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5127 "at %L may not be specified for an internal "
5128 "procedure", &gfc_current_locus)
5132 if (peek_char != '(')
5134 gfc_error ("Missing required parentheses before BIND(C) at %C");
5137 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5142 if (gfc_match_eos () != MATCH_YES)
5144 gfc_syntax_error (ST_SUBROUTINE);
5148 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5151 /* Warn if it has the same name as an intrinsic. */
5152 warn_intrinsic_shadow (sym, false);
5158 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5159 given, and set the binding label in either the given symbol (if not
5160 NULL), or in the current_ts. The symbol may be NULL because we may
5161 encounter the BIND(C) before the declaration itself. Return
5162 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5163 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5164 or MATCH_YES if the specifier was correct and the binding label and
5165 bind(c) fields were set correctly for the given symbol or the
5166 current_ts. If allow_binding_name is false, no binding name may be
5170 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5172 /* binding label, if exists */
5173 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
5177 /* Initialize the flag that specifies whether we encountered a NAME=
5178 specifier or not. */
5179 has_name_equals = 0;
5181 /* Init the first char to nil so we can catch if we don't have
5182 the label (name attr) or the symbol name yet. */
5183 binding_label[0] = '\0';
5185 /* This much we have to be able to match, in this order, if
5186 there is a bind(c) label. */
5187 if (gfc_match (" bind ( c ") != MATCH_YES)
5190 /* Now see if there is a binding label, or if we've reached the
5191 end of the bind(c) attribute without one. */
5192 if (gfc_match_char (',') == MATCH_YES)
5194 if (gfc_match (" name = ") != MATCH_YES)
5196 gfc_error ("Syntax error in NAME= specifier for binding label "
5198 /* should give an error message here */
5202 has_name_equals = 1;
5204 /* Get the opening quote. */
5205 double_quote = MATCH_YES;
5206 single_quote = MATCH_YES;
5207 double_quote = gfc_match_char ('"');
5208 if (double_quote != MATCH_YES)
5209 single_quote = gfc_match_char ('\'');
5210 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5212 gfc_error ("Syntax error in NAME= specifier for binding label "
5217 /* Grab the binding label, using functions that will not lower
5218 case the names automatically. */
5219 if (gfc_match_name_C (binding_label) != MATCH_YES)
5222 /* Get the closing quotation. */
5223 if (double_quote == MATCH_YES)
5225 if (gfc_match_char ('"') != MATCH_YES)
5227 gfc_error ("Missing closing quote '\"' for binding label at %C");
5228 /* User started string with '"' so looked to match it. */
5234 if (gfc_match_char ('\'') != MATCH_YES)
5236 gfc_error ("Missing closing quote '\'' for binding label at %C");
5237 /* User started string with "'" char. */
5243 /* Get the required right paren. */
5244 if (gfc_match_char (')') != MATCH_YES)
5246 gfc_error ("Missing closing paren for binding label at %C");
5250 if (has_name_equals && !allow_binding_name)
5252 gfc_error ("No binding name is allowed in BIND(C) at %C");
5256 if (has_name_equals && sym != NULL && sym->attr.dummy)
5258 gfc_error ("For dummy procedure %s, no binding name is "
5259 "allowed in BIND(C) at %C", sym->name);
5264 /* Save the binding label to the symbol. If sym is null, we're
5265 probably matching the typespec attributes of a declaration and
5266 haven't gotten the name yet, and therefore, no symbol yet. */
5267 if (binding_label[0] != '\0')
5271 strcpy (sym->binding_label, binding_label);
5274 strcpy (curr_binding_label, binding_label);
5276 else if (allow_binding_name)
5278 /* No binding label, but if symbol isn't null, we
5279 can set the label for it here.
5280 If name="" or allow_binding_name is false, no C binding name is
5282 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5283 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
5286 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5287 && current_interface.type == INTERFACE_ABSTRACT)
5289 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5297 /* Return nonzero if we're currently compiling a contained procedure. */
5300 contained_procedure (void)
5302 gfc_state_data *s = gfc_state_stack;
5304 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5305 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5311 /* Set the kind of each enumerator. The kind is selected such that it is
5312 interoperable with the corresponding C enumeration type, making
5313 sure that -fshort-enums is honored. */
5318 enumerator_history *current_history = NULL;
5322 if (max_enum == NULL || enum_history == NULL)
5325 if (!flag_short_enums)
5331 kind = gfc_integer_kinds[i++].kind;
5333 while (kind < gfc_c_int_kind
5334 && gfc_check_integer_range (max_enum->initializer->value.integer,
5337 current_history = enum_history;
5338 while (current_history != NULL)
5340 current_history->sym->ts.kind = kind;
5341 current_history = current_history->next;
5346 /* Match any of the various end-block statements. Returns the type of
5347 END to the caller. The END INTERFACE, END IF, END DO and END
5348 SELECT statements cannot be replaced by a single END statement. */
5351 gfc_match_end (gfc_statement *st)
5353 char name[GFC_MAX_SYMBOL_LEN + 1];
5354 gfc_compile_state state;
5356 const char *block_name;
5361 old_loc = gfc_current_locus;
5362 if (gfc_match ("end") != MATCH_YES)
5365 state = gfc_current_state ();
5366 block_name = gfc_current_block () == NULL
5367 ? NULL : gfc_current_block ()->name;
5369 if (state == COMP_CONTAINS || state == COMP_DERIVED_CONTAINS)
5371 state = gfc_state_stack->previous->state;
5372 block_name = gfc_state_stack->previous->sym == NULL
5373 ? NULL : gfc_state_stack->previous->sym->name;
5380 *st = ST_END_PROGRAM;
5381 target = " program";
5385 case COMP_SUBROUTINE:
5386 *st = ST_END_SUBROUTINE;
5387 target = " subroutine";
5388 eos_ok = !contained_procedure ();
5392 *st = ST_END_FUNCTION;
5393 target = " function";
5394 eos_ok = !contained_procedure ();
5397 case COMP_BLOCK_DATA:
5398 *st = ST_END_BLOCK_DATA;
5399 target = " block data";
5404 *st = ST_END_MODULE;
5409 case COMP_INTERFACE:
5410 *st = ST_END_INTERFACE;
5411 target = " interface";
5416 case COMP_DERIVED_CONTAINS:
5435 *st = ST_END_SELECT;
5441 *st = ST_END_FORALL;
5456 last_initializer = NULL;
5458 gfc_free_enum_history ();
5462 gfc_error ("Unexpected END statement at %C");
5466 if (gfc_match_eos () == MATCH_YES)
5470 /* We would have required END [something]. */
5471 gfc_error ("%s statement expected at %L",
5472 gfc_ascii_statement (*st), &old_loc);
5479 /* Verify that we've got the sort of end-block that we're expecting. */
5480 if (gfc_match (target) != MATCH_YES)
5482 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5486 /* If we're at the end, make sure a block name wasn't required. */
5487 if (gfc_match_eos () == MATCH_YES)
5490 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5491 && *st != ST_END_FORALL && *st != ST_END_WHERE)
5494 if (gfc_current_block () == NULL)
5497 gfc_error ("Expected block name of '%s' in %s statement at %C",
5498 block_name, gfc_ascii_statement (*st));
5503 /* END INTERFACE has a special handler for its several possible endings. */
5504 if (*st == ST_END_INTERFACE)
5505 return gfc_match_end_interface ();
5507 /* We haven't hit the end of statement, so what is left must be an
5509 m = gfc_match_space ();
5511 m = gfc_match_name (name);
5514 gfc_error ("Expected terminating name at %C");
5518 if (block_name == NULL)
5521 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
5523 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5524 gfc_ascii_statement (*st));
5527 /* Procedure pointer as function result. */
5528 else if (strcmp (block_name, "ppr@") == 0
5529 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
5531 gfc_error ("Expected label '%s' for %s statement at %C",
5532 gfc_current_block ()->ns->proc_name->name,
5533 gfc_ascii_statement (*st));
5537 if (gfc_match_eos () == MATCH_YES)
5541 gfc_syntax_error (*st);
5544 gfc_current_locus = old_loc;
5550 /***************** Attribute declaration statements ****************/
5552 /* Set the attribute of a single variable. */
5557 char name[GFC_MAX_SYMBOL_LEN + 1];
5565 m = gfc_match_name (name);
5569 if (find_special (name, &sym, false))
5572 var_locus = gfc_current_locus;
5574 /* Deal with possible array specification for certain attributes. */
5575 if (current_attr.dimension
5576 || current_attr.allocatable
5577 || current_attr.pointer
5578 || current_attr.target)
5580 m = gfc_match_array_spec (&as);
5581 if (m == MATCH_ERROR)
5584 if (current_attr.dimension && m == MATCH_NO)
5586 gfc_error ("Missing array specification at %L in DIMENSION "
5587 "statement", &var_locus);
5592 if (current_attr.dimension && sym->value)
5594 gfc_error ("Dimensions specified for %s at %L after its "
5595 "initialisation", sym->name, &var_locus);
5600 if ((current_attr.allocatable || current_attr.pointer)
5601 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5603 gfc_error ("Array specification must be deferred at %L", &var_locus);
5609 /* Update symbol table. DIMENSION attribute is set
5610 in gfc_set_array_spec(). */
5611 if (current_attr.dimension == 0
5612 && gfc_copy_attr (&sym->attr, ¤t_attr, &var_locus) == FAILURE)
5618 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5624 if (sym->attr.cray_pointee && sym->as != NULL)
5626 /* Fix the array spec. */
5627 m = gfc_mod_pointee_as (sym->as);
5628 if (m == MATCH_ERROR)
5632 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
5638 if ((current_attr.external || current_attr.intrinsic)
5639 && sym->attr.flavor != FL_PROCEDURE
5640 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
5646 add_hidden_procptr_result (sym);
5651 gfc_free_array_spec (as);
5656 /* Generic attribute declaration subroutine. Used for attributes that
5657 just have a list of names. */
5664 /* Gobble the optional double colon, by simply ignoring the result
5674 if (gfc_match_eos () == MATCH_YES)
5680 if (gfc_match_char (',') != MATCH_YES)
5682 gfc_error ("Unexpected character in variable list at %C");
5692 /* This routine matches Cray Pointer declarations of the form:
5693 pointer ( <pointer>, <pointee> )
5695 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5696 The pointer, if already declared, should be an integer. Otherwise, we
5697 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5698 be either a scalar, or an array declaration. No space is allocated for
5699 the pointee. For the statement
5700 pointer (ipt, ar(10))
5701 any subsequent uses of ar will be translated (in C-notation) as
5702 ar(i) => ((<type> *) ipt)(i)
5703 After gimplification, pointee variable will disappear in the code. */
5706 cray_pointer_decl (void)
5710 gfc_symbol *cptr; /* Pointer symbol. */
5711 gfc_symbol *cpte; /* Pointee symbol. */
5717 if (gfc_match_char ('(') != MATCH_YES)
5719 gfc_error ("Expected '(' at %C");
5723 /* Match pointer. */
5724 var_locus = gfc_current_locus;
5725 gfc_clear_attr (¤t_attr);
5726 gfc_add_cray_pointer (¤t_attr, &var_locus);
5727 current_ts.type = BT_INTEGER;
5728 current_ts.kind = gfc_index_integer_kind;
5730 m = gfc_match_symbol (&cptr, 0);
5733 gfc_error ("Expected variable name at %C");
5737 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5740 gfc_set_sym_referenced (cptr);
5742 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5744 cptr->ts.type = BT_INTEGER;
5745 cptr->ts.kind = gfc_index_integer_kind;
5747 else if (cptr->ts.type != BT_INTEGER)
5749 gfc_error ("Cray pointer at %C must be an integer");
5752 else if (cptr->ts.kind < gfc_index_integer_kind)
5753 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5754 " memory addresses require %d bytes",
5755 cptr->ts.kind, gfc_index_integer_kind);
5757 if (gfc_match_char (',') != MATCH_YES)
5759 gfc_error ("Expected \",\" at %C");
5763 /* Match Pointee. */
5764 var_locus = gfc_current_locus;
5765 gfc_clear_attr (¤t_attr);
5766 gfc_add_cray_pointee (¤t_attr, &var_locus);
5767 current_ts.type = BT_UNKNOWN;
5768 current_ts.kind = 0;
5770 m = gfc_match_symbol (&cpte, 0);
5773 gfc_error ("Expected variable name at %C");
5777 /* Check for an optional array spec. */
5778 m = gfc_match_array_spec (&as);
5779 if (m == MATCH_ERROR)
5781 gfc_free_array_spec (as);
5784 else if (m == MATCH_NO)
5786 gfc_free_array_spec (as);
5790 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5793 gfc_set_sym_referenced (cpte);
5795 if (cpte->as == NULL)
5797 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5798 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5800 else if (as != NULL)
5802 gfc_error ("Duplicate array spec for Cray pointee at %C");
5803 gfc_free_array_spec (as);
5809 if (cpte->as != NULL)
5811 /* Fix array spec. */
5812 m = gfc_mod_pointee_as (cpte->as);
5813 if (m == MATCH_ERROR)
5817 /* Point the Pointee at the Pointer. */
5818 cpte->cp_pointer = cptr;
5820 if (gfc_match_char (')') != MATCH_YES)
5822 gfc_error ("Expected \")\" at %C");
5825 m = gfc_match_char (',');
5827 done = true; /* Stop searching for more declarations. */
5831 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5832 || gfc_match_eos () != MATCH_YES)
5834 gfc_error ("Expected \",\" or end of statement at %C");
5842 gfc_match_external (void)
5845 gfc_clear_attr (¤t_attr);
5846 current_attr.external = 1;
5848 return attr_decl ();
5853 gfc_match_intent (void)
5857 intent = match_intent_spec ();
5858 if (intent == INTENT_UNKNOWN)
5861 gfc_clear_attr (¤t_attr);
5862 current_attr.intent = intent;
5864 return attr_decl ();
5869 gfc_match_intrinsic (void)
5872 gfc_clear_attr (¤t_attr);
5873 current_attr.intrinsic = 1;
5875 return attr_decl ();
5880 gfc_match_optional (void)
5883 gfc_clear_attr (¤t_attr);
5884 current_attr.optional = 1;
5886 return attr_decl ();
5891 gfc_match_pointer (void)
5893 gfc_gobble_whitespace ();
5894 if (gfc_peek_ascii_char () == '(')
5896 if (!gfc_option.flag_cray_pointer)
5898 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5902 return cray_pointer_decl ();
5906 gfc_clear_attr (¤t_attr);
5907 current_attr.pointer = 1;
5909 return attr_decl ();
5915 gfc_match_allocatable (void)
5917 gfc_clear_attr (¤t_attr);
5918 current_attr.allocatable = 1;
5920 return attr_decl ();
5925 gfc_match_dimension (void)
5927 gfc_clear_attr (¤t_attr);
5928 current_attr.dimension = 1;
5930 return attr_decl ();
5935 gfc_match_target (void)
5937 gfc_clear_attr (¤t_attr);
5938 current_attr.target = 1;
5940 return attr_decl ();
5944 /* Match the list of entities being specified in a PUBLIC or PRIVATE
5948 access_attr_decl (gfc_statement st)
5950 char name[GFC_MAX_SYMBOL_LEN + 1];
5951 interface_type type;
5954 gfc_intrinsic_op op;
5957 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
5962 m = gfc_match_generic_spec (&type, name, &op);
5965 if (m == MATCH_ERROR)
5970 case INTERFACE_NAMELESS:
5971 case INTERFACE_ABSTRACT:
5974 case INTERFACE_GENERIC:
5975 if (gfc_get_symbol (name, NULL, &sym))
5978 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
5979 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
5980 sym->name, NULL) == FAILURE)
5985 case INTERFACE_INTRINSIC_OP:
5986 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
5988 gfc_current_ns->operator_access[op] =
5989 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
5993 gfc_error ("Access specification of the %s operator at %C has "
5994 "already been specified", gfc_op2string (op));
6000 case INTERFACE_USER_OP:
6001 uop = gfc_get_uop (name);
6003 if (uop->access == ACCESS_UNKNOWN)
6005 uop->access = (st == ST_PUBLIC)
6006 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6010 gfc_error ("Access specification of the .%s. operator at %C "
6011 "has already been specified", sym->name);
6018 if (gfc_match_char (',') == MATCH_NO)
6022 if (gfc_match_eos () != MATCH_YES)
6027 gfc_syntax_error (st);
6035 gfc_match_protected (void)
6040 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6042 gfc_error ("PROTECTED at %C only allowed in specification "
6043 "part of a module");
6048 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
6052 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6057 if (gfc_match_eos () == MATCH_YES)
6062 m = gfc_match_symbol (&sym, 0);
6066 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6079 if (gfc_match_eos () == MATCH_YES)
6081 if (gfc_match_char (',') != MATCH_YES)
6088 gfc_error ("Syntax error in PROTECTED statement at %C");
6093 /* The PRIVATE statement is a bit weird in that it can be an attribute
6094 declaration, but also works as a standalone statement inside of a
6095 type declaration or a module. */
6098 gfc_match_private (gfc_statement *st)
6101 if (gfc_match ("private") != MATCH_YES)
6104 if (gfc_current_state () != COMP_MODULE
6105 && !(gfc_current_state () == COMP_DERIVED
6106 && gfc_state_stack->previous
6107 && gfc_state_stack->previous->state == COMP_MODULE)
6108 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6109 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6110 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6112 gfc_error ("PRIVATE statement at %C is only allowed in the "
6113 "specification part of a module");
6117 if (gfc_current_state () == COMP_DERIVED)
6119 if (gfc_match_eos () == MATCH_YES)
6125 gfc_syntax_error (ST_PRIVATE);
6129 if (gfc_match_eos () == MATCH_YES)
6136 return access_attr_decl (ST_PRIVATE);
6141 gfc_match_public (gfc_statement *st)
6144 if (gfc_match ("public") != MATCH_YES)
6147 if (gfc_current_state () != COMP_MODULE)
6149 gfc_error ("PUBLIC statement at %C is only allowed in the "
6150 "specification part of a module");
6154 if (gfc_match_eos () == MATCH_YES)
6161 return access_attr_decl (ST_PUBLIC);
6165 /* Workhorse for gfc_match_parameter. */
6174 m = gfc_match_symbol (&sym, 0);
6176 gfc_error ("Expected variable name at %C in PARAMETER statement");
6181 if (gfc_match_char ('=') == MATCH_NO)
6183 gfc_error ("Expected = sign in PARAMETER statement at %C");
6187 m = gfc_match_init_expr (&init);
6189 gfc_error ("Expected expression at %C in PARAMETER statement");
6193 if (sym->ts.type == BT_UNKNOWN
6194 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6200 if (gfc_check_assign_symbol (sym, init) == FAILURE
6201 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6209 gfc_error ("Initializing already initialized variable at %C");
6214 if (sym->ts.type == BT_CHARACTER
6215 && sym->ts.u.cl != NULL
6216 && sym->ts.u.cl->length != NULL
6217 && sym->ts.u.cl->length->expr_type == EXPR_CONSTANT
6218 && init->expr_type == EXPR_CONSTANT
6219 && init->ts.type == BT_CHARACTER)
6220 gfc_set_constant_character_len (
6221 mpz_get_si (sym->ts.u.cl->length->value.integer), init, -1);
6222 else if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl != NULL
6223 && sym->ts.u.cl->length == NULL)
6226 if (init->expr_type == EXPR_CONSTANT)
6228 clen = init->value.character.length;
6229 sym->ts.u.cl->length = gfc_int_expr (clen);
6231 else if (init->expr_type == EXPR_ARRAY)
6233 gfc_expr *p = init->value.constructor->expr;
6234 clen = p->value.character.length;
6235 sym->ts.u.cl->length = gfc_int_expr (clen);
6237 else if (init->ts.u.cl && init->ts.u.cl->length)
6238 sym->ts.u.cl->length = gfc_copy_expr (sym->value->ts.u.cl->length);
6245 gfc_free_expr (init);
6250 /* Match a parameter statement, with the weird syntax that these have. */
6253 gfc_match_parameter (void)
6257 if (gfc_match_char ('(') == MATCH_NO)
6266 if (gfc_match (" )%t") == MATCH_YES)
6269 if (gfc_match_char (',') != MATCH_YES)
6271 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6281 /* Save statements have a special syntax. */
6284 gfc_match_save (void)
6286 char n[GFC_MAX_SYMBOL_LEN+1];
6291 if (gfc_match_eos () == MATCH_YES)
6293 if (gfc_current_ns->seen_save)
6295 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6296 "follows previous SAVE statement")
6301 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6305 if (gfc_current_ns->save_all)
6307 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6308 "blanket SAVE statement")
6317 m = gfc_match_symbol (&sym, 0);
6321 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
6333 m = gfc_match (" / %n /", &n);
6334 if (m == MATCH_ERROR)
6339 c = gfc_get_common (n, 0);
6342 gfc_current_ns->seen_save = 1;
6345 if (gfc_match_eos () == MATCH_YES)
6347 if (gfc_match_char (',') != MATCH_YES)
6354 gfc_error ("Syntax error in SAVE statement at %C");
6360 gfc_match_value (void)
6365 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
6369 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6374 if (gfc_match_eos () == MATCH_YES)
6379 m = gfc_match_symbol (&sym, 0);
6383 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
6396 if (gfc_match_eos () == MATCH_YES)
6398 if (gfc_match_char (',') != MATCH_YES)
6405 gfc_error ("Syntax error in VALUE statement at %C");
6411 gfc_match_volatile (void)
6416 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
6420 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6425 if (gfc_match_eos () == MATCH_YES)
6430 /* VOLATILE is special because it can be added to host-associated
6432 m = gfc_match_symbol (&sym, 1);
6436 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6449 if (gfc_match_eos () == MATCH_YES)
6451 if (gfc_match_char (',') != MATCH_YES)
6458 gfc_error ("Syntax error in VOLATILE statement at %C");
6463 /* Match a module procedure statement. Note that we have to modify
6464 symbols in the parent's namespace because the current one was there
6465 to receive symbols that are in an interface's formal argument list. */
6468 gfc_match_modproc (void)
6470 char name[GFC_MAX_SYMBOL_LEN + 1];
6473 gfc_namespace *module_ns;
6474 gfc_interface *old_interface_head, *interface;
6476 if (gfc_state_stack->state != COMP_INTERFACE
6477 || gfc_state_stack->previous == NULL
6478 || current_interface.type == INTERFACE_NAMELESS
6479 || current_interface.type == INTERFACE_ABSTRACT)
6481 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6486 module_ns = gfc_current_ns->parent;
6487 for (; module_ns; module_ns = module_ns->parent)
6488 if (module_ns->proc_name->attr.flavor == FL_MODULE
6489 || module_ns->proc_name->attr.flavor == FL_PROGRAM
6490 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
6491 && !module_ns->proc_name->attr.contained))
6494 if (module_ns == NULL)
6497 /* Store the current state of the interface. We will need it if we
6498 end up with a syntax error and need to recover. */
6499 old_interface_head = gfc_current_interface_head ();
6503 locus old_locus = gfc_current_locus;
6506 m = gfc_match_name (name);
6512 /* Check for syntax error before starting to add symbols to the
6513 current namespace. */
6514 if (gfc_match_eos () == MATCH_YES)
6516 if (!last && gfc_match_char (',') != MATCH_YES)
6519 /* Now we're sure the syntax is valid, we process this item
6521 if (gfc_get_symbol (name, module_ns, &sym))
6524 if (sym->attr.intrinsic)
6526 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
6527 "PROCEDURE", &old_locus);
6531 if (sym->attr.proc != PROC_MODULE
6532 && gfc_add_procedure (&sym->attr, PROC_MODULE,
6533 sym->name, NULL) == FAILURE)
6536 if (gfc_add_interface (sym) == FAILURE)
6539 sym->attr.mod_proc = 1;
6540 sym->declared_at = old_locus;
6549 /* Restore the previous state of the interface. */
6550 interface = gfc_current_interface_head ();
6551 gfc_set_current_interface_head (old_interface_head);
6553 /* Free the new interfaces. */
6554 while (interface != old_interface_head)
6556 gfc_interface *i = interface->next;
6557 gfc_free (interface);
6561 /* And issue a syntax error. */
6562 gfc_syntax_error (ST_MODULE_PROC);
6567 /* Check a derived type that is being extended. */
6569 check_extended_derived_type (char *name)
6571 gfc_symbol *extended;
6573 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
6575 gfc_error ("Ambiguous symbol in TYPE definition at %C");
6581 gfc_error ("No such symbol in TYPE definition at %C");
6585 if (extended->attr.flavor != FL_DERIVED)
6587 gfc_error ("'%s' in EXTENDS expression at %C is not a "
6588 "derived type", name);
6592 if (extended->attr.is_bind_c)
6594 gfc_error ("'%s' cannot be extended at %C because it "
6595 "is BIND(C)", extended->name);
6599 if (extended->attr.sequence)
6601 gfc_error ("'%s' cannot be extended at %C because it "
6602 "is a SEQUENCE type", extended->name);
6610 /* Match the optional attribute specifiers for a type declaration.
6611 Return MATCH_ERROR if an error is encountered in one of the handled
6612 attributes (public, private, bind(c)), MATCH_NO if what's found is
6613 not a handled attribute, and MATCH_YES otherwise. TODO: More error
6614 checking on attribute conflicts needs to be done. */
6617 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
6619 /* See if the derived type is marked as private. */
6620 if (gfc_match (" , private") == MATCH_YES)
6622 if (gfc_current_state () != COMP_MODULE)
6624 gfc_error ("Derived type at %C can only be PRIVATE in the "
6625 "specification part of a module");
6629 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6632 else if (gfc_match (" , public") == MATCH_YES)
6634 if (gfc_current_state () != COMP_MODULE)
6636 gfc_error ("Derived type at %C can only be PUBLIC in the "
6637 "specification part of a module");
6641 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6644 else if (gfc_match (" , bind ( c )") == MATCH_YES)
6646 /* If the type is defined to be bind(c) it then needs to make
6647 sure that all fields are interoperable. This will
6648 need to be a semantic check on the finished derived type.
6649 See 15.2.3 (lines 9-12) of F2003 draft. */
6650 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
6653 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
6655 else if (gfc_match (" , abstract") == MATCH_YES)
6657 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
6661 if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
6664 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
6666 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
6672 /* If we get here, something matched. */
6677 /* Match the beginning of a derived type declaration. If a type name
6678 was the result of a function, then it is possible to have a symbol
6679 already to be known as a derived type yet have no components. */
6682 gfc_match_derived_decl (void)
6684 char name[GFC_MAX_SYMBOL_LEN + 1];
6685 char parent[GFC_MAX_SYMBOL_LEN + 1];
6686 symbol_attribute attr;
6688 gfc_symbol *extended;
6690 match is_type_attr_spec = MATCH_NO;
6691 bool seen_attr = false;
6693 if (gfc_current_state () == COMP_DERIVED)
6698 gfc_clear_attr (&attr);
6703 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
6704 if (is_type_attr_spec == MATCH_ERROR)
6706 if (is_type_attr_spec == MATCH_YES)
6708 } while (is_type_attr_spec == MATCH_YES);
6710 /* Deal with derived type extensions. The extension attribute has
6711 been added to 'attr' but now the parent type must be found and
6714 extended = check_extended_derived_type (parent);
6716 if (parent[0] && !extended)
6719 if (gfc_match (" ::") != MATCH_YES && seen_attr)
6721 gfc_error ("Expected :: in TYPE definition at %C");
6725 m = gfc_match (" %n%t", name);
6729 /* Make sure the name is not the name of an intrinsic type. */
6730 if (gfc_is_intrinsic_typename (name))
6732 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
6737 if (gfc_get_symbol (name, NULL, &sym))
6740 if (sym->ts.type != BT_UNKNOWN)
6742 gfc_error ("Derived type name '%s' at %C already has a basic type "
6743 "of %s", sym->name, gfc_typename (&sym->ts));
6747 /* The symbol may already have the derived attribute without the
6748 components. The ways this can happen is via a function
6749 definition, an INTRINSIC statement or a subtype in another
6750 derived type that is a pointer. The first part of the AND clause
6751 is true if the symbol is not the return value of a function. */
6752 if (sym->attr.flavor != FL_DERIVED
6753 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6756 if (sym->components != NULL || sym->attr.zero_comp)
6758 gfc_error ("Derived type definition of '%s' at %C has already been "
6759 "defined", sym->name);
6763 if (attr.access != ACCESS_UNKNOWN
6764 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6767 /* See if the derived type was labeled as bind(c). */
6768 if (attr.is_bind_c != 0)
6769 sym->attr.is_bind_c = attr.is_bind_c;
6771 /* Construct the f2k_derived namespace if it is not yet there. */
6772 if (!sym->f2k_derived)
6773 sym->f2k_derived = gfc_get_namespace (NULL, 0);
6775 if (extended && !sym->components)
6780 /* Add the extended derived type as the first component. */
6781 gfc_add_component (sym, parent, &p);
6782 sym->attr.extension = attr.extension;
6784 gfc_set_sym_referenced (extended);
6786 p->ts.type = BT_DERIVED;
6787 p->ts.u.derived = extended;
6788 p->initializer = gfc_default_initializer (&p->ts);
6790 /* Provide the links between the extended type and its extension. */
6791 if (!extended->f2k_derived)
6792 extended->f2k_derived = gfc_get_namespace (NULL, 0);
6793 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
6797 /* Take over the ABSTRACT attribute. */
6798 sym->attr.abstract = attr.abstract;
6800 gfc_new_block = sym;
6806 /* Cray Pointees can be declared as:
6807 pointer (ipt, a (n,m,...,*))
6808 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6809 cheat and set a constant bound of 1 for the last dimension, if this
6810 is the case. Since there is no bounds-checking for Cray Pointees,
6811 this will be okay. */
6814 gfc_mod_pointee_as (gfc_array_spec *as)
6816 as->cray_pointee = true; /* This will be useful to know later. */
6817 if (as->type == AS_ASSUMED_SIZE)
6819 as->type = AS_EXPLICIT;
6820 as->upper[as->rank - 1] = gfc_int_expr (1);
6821 as->cp_was_assumed = true;
6823 else if (as->type == AS_ASSUMED_SHAPE)
6825 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6832 /* Match the enum definition statement, here we are trying to match
6833 the first line of enum definition statement.
6834 Returns MATCH_YES if match is found. */
6837 gfc_match_enum (void)
6841 m = gfc_match_eos ();
6845 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
6853 /* Returns an initializer whose value is one higher than the value of the
6854 LAST_INITIALIZER argument. If the argument is NULL, the
6855 initializers value will be set to zero. The initializer's kind
6856 will be set to gfc_c_int_kind.
6858 If -fshort-enums is given, the appropriate kind will be selected
6859 later after all enumerators have been parsed. A warning is issued
6860 here if an initializer exceeds gfc_c_int_kind. */
6863 enum_initializer (gfc_expr *last_initializer, locus where)
6867 result = gfc_get_expr ();
6868 result->expr_type = EXPR_CONSTANT;
6869 result->ts.type = BT_INTEGER;
6870 result->ts.kind = gfc_c_int_kind;
6871 result->where = where;
6873 mpz_init (result->value.integer);
6875 if (last_initializer != NULL)
6877 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
6878 result->where = last_initializer->where;
6880 if (gfc_check_integer_range (result->value.integer,
6881 gfc_c_int_kind) != ARITH_OK)
6883 gfc_error ("Enumerator exceeds the C integer type at %C");
6889 /* Control comes here, if it's the very first enumerator and no
6890 initializer has been given. It will be initialized to zero. */
6891 mpz_set_si (result->value.integer, 0);
6898 /* Match a variable name with an optional initializer. When this
6899 subroutine is called, a variable is expected to be parsed next.
6900 Depending on what is happening at the moment, updates either the
6901 symbol table or the current interface. */
6904 enumerator_decl (void)
6906 char name[GFC_MAX_SYMBOL_LEN + 1];
6907 gfc_expr *initializer;
6908 gfc_array_spec *as = NULL;
6916 old_locus = gfc_current_locus;
6918 /* When we get here, we've just matched a list of attributes and
6919 maybe a type and a double colon. The next thing we expect to see
6920 is the name of the symbol. */
6921 m = gfc_match_name (name);
6925 var_locus = gfc_current_locus;
6927 /* OK, we've successfully matched the declaration. Now put the
6928 symbol in the current namespace. If we fail to create the symbol,
6930 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
6936 /* The double colon must be present in order to have initializers.
6937 Otherwise the statement is ambiguous with an assignment statement. */
6940 if (gfc_match_char ('=') == MATCH_YES)
6942 m = gfc_match_init_expr (&initializer);
6945 gfc_error ("Expected an initialization expression at %C");
6954 /* If we do not have an initializer, the initialization value of the
6955 previous enumerator (stored in last_initializer) is incremented
6956 by 1 and is used to initialize the current enumerator. */
6957 if (initializer == NULL)
6958 initializer = enum_initializer (last_initializer, old_locus);
6960 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
6962 gfc_error("ENUMERATOR %L not initialized with integer expression",
6965 gfc_free_enum_history ();
6969 /* Store this current initializer, for the next enumerator variable
6970 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6971 use last_initializer below. */
6972 last_initializer = initializer;
6973 t = add_init_expr_to_sym (name, &initializer, &var_locus);
6975 /* Maintain enumerator history. */
6976 gfc_find_symbol (name, NULL, 0, &sym);
6977 create_enum_history (sym, last_initializer);
6979 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6982 /* Free stuff up and return. */
6983 gfc_free_expr (initializer);
6989 /* Match the enumerator definition statement. */
6992 gfc_match_enumerator_def (void)
6997 gfc_clear_ts (¤t_ts);
6999 m = gfc_match (" enumerator");
7003 m = gfc_match (" :: ");
7004 if (m == MATCH_ERROR)
7007 colon_seen = (m == MATCH_YES);
7009 if (gfc_current_state () != COMP_ENUM)
7011 gfc_error ("ENUM definition statement expected before %C");
7012 gfc_free_enum_history ();
7016 (¤t_ts)->type = BT_INTEGER;
7017 (¤t_ts)->kind = gfc_c_int_kind;
7019 gfc_clear_attr (¤t_attr);
7020 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
7029 m = enumerator_decl ();
7030 if (m == MATCH_ERROR)
7035 if (gfc_match_eos () == MATCH_YES)
7037 if (gfc_match_char (',') != MATCH_YES)
7041 if (gfc_current_state () == COMP_ENUM)
7043 gfc_free_enum_history ();
7044 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7049 gfc_free_array_spec (current_as);
7056 /* Match binding attributes. */
7059 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7061 bool found_passing = false;
7062 bool seen_ptr = false;
7063 match m = MATCH_YES;
7065 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7066 this case the defaults are in there. */
7067 ba->access = ACCESS_UNKNOWN;
7068 ba->pass_arg = NULL;
7069 ba->pass_arg_num = 0;
7071 ba->non_overridable = 0;
7075 /* If we find a comma, we believe there are binding attributes. */
7076 m = gfc_match_char (',');
7082 /* Access specifier. */
7084 m = gfc_match (" public");
7085 if (m == MATCH_ERROR)
7089 if (ba->access != ACCESS_UNKNOWN)
7091 gfc_error ("Duplicate access-specifier at %C");
7095 ba->access = ACCESS_PUBLIC;
7099 m = gfc_match (" private");
7100 if (m == MATCH_ERROR)
7104 if (ba->access != ACCESS_UNKNOWN)
7106 gfc_error ("Duplicate access-specifier at %C");
7110 ba->access = ACCESS_PRIVATE;
7114 /* If inside GENERIC, the following is not allowed. */
7119 m = gfc_match (" nopass");
7120 if (m == MATCH_ERROR)
7126 gfc_error ("Binding attributes already specify passing,"
7127 " illegal NOPASS at %C");
7131 found_passing = true;
7136 /* PASS possibly including argument. */
7137 m = gfc_match (" pass");
7138 if (m == MATCH_ERROR)
7142 char arg[GFC_MAX_SYMBOL_LEN + 1];
7146 gfc_error ("Binding attributes already specify passing,"
7147 " illegal PASS at %C");
7151 m = gfc_match (" ( %n )", arg);
7152 if (m == MATCH_ERROR)
7155 ba->pass_arg = gfc_get_string (arg);
7156 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7158 found_passing = true;
7166 m = gfc_match (" pointer");
7167 if (m == MATCH_ERROR)
7173 gfc_error ("Duplicate POINTER attribute at %C");
7183 /* NON_OVERRIDABLE flag. */
7184 m = gfc_match (" non_overridable");
7185 if (m == MATCH_ERROR)
7189 if (ba->non_overridable)
7191 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7195 ba->non_overridable = 1;
7199 /* DEFERRED flag. */
7200 m = gfc_match (" deferred");
7201 if (m == MATCH_ERROR)
7207 gfc_error ("Duplicate DEFERRED at %C");
7218 /* Nothing matching found. */
7220 gfc_error ("Expected access-specifier at %C");
7222 gfc_error ("Expected binding attribute at %C");
7225 while (gfc_match_char (',') == MATCH_YES);
7227 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
7228 if (ba->non_overridable && ba->deferred)
7230 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7237 if (ba->access == ACCESS_UNKNOWN)
7238 ba->access = gfc_typebound_default_access;
7240 if (ppc && !seen_ptr)
7242 gfc_error ("POINTER attribute is required for procedure pointer component"
7254 /* Match a PROCEDURE specific binding inside a derived type. */
7257 match_procedure_in_type (void)
7259 char name[GFC_MAX_SYMBOL_LEN + 1];
7260 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
7261 char* target = NULL;
7262 gfc_typebound_proc* tb;
7270 /* Check current state. */
7271 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
7272 block = gfc_state_stack->previous->sym;
7275 /* Try to match PROCEDURE(interface). */
7276 if (gfc_match (" (") == MATCH_YES)
7278 m = gfc_match_name (target_buf);
7279 if (m == MATCH_ERROR)
7283 gfc_error ("Interface-name expected after '(' at %C");
7287 if (gfc_match (" )") != MATCH_YES)
7289 gfc_error ("')' expected at %C");
7293 target = target_buf;
7296 /* Construct the data structure. */
7297 tb = gfc_get_typebound_proc ();
7298 tb->where = gfc_current_locus;
7301 /* Match binding attributes. */
7302 m = match_binding_attributes (tb, false, false);
7303 if (m == MATCH_ERROR)
7305 seen_attrs = (m == MATCH_YES);
7307 /* Check that attribute DEFERRED is given iff an interface is specified, which
7308 means target != NULL. */
7309 if (tb->deferred && !target)
7311 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7314 if (target && !tb->deferred)
7316 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7320 /* Match the colons. */
7321 m = gfc_match (" ::");
7322 if (m == MATCH_ERROR)
7324 seen_colons = (m == MATCH_YES);
7325 if (seen_attrs && !seen_colons)
7327 gfc_error ("Expected '::' after binding-attributes at %C");
7331 /* Match the binding name. */
7332 m = gfc_match_name (name);
7333 if (m == MATCH_ERROR)
7337 gfc_error ("Expected binding name at %C");
7341 /* Try to match the '=> target', if it's there. */
7342 m = gfc_match (" =>");
7343 if (m == MATCH_ERROR)
7349 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7355 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7360 m = gfc_match_name (target_buf);
7361 if (m == MATCH_ERROR)
7365 gfc_error ("Expected binding target after '=>' at %C");
7368 target = target_buf;
7371 /* Now we should have the end. */
7372 m = gfc_match_eos ();
7373 if (m == MATCH_ERROR)
7377 gfc_error ("Junk after PROCEDURE declaration at %C");
7381 /* If no target was found, it has the same name as the binding. */
7385 /* Get the namespace to insert the symbols into. */
7386 ns = block->f2k_derived;
7389 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7390 if (tb->deferred && !block->attr.abstract)
7392 gfc_error ("Type '%s' containing DEFERRED binding at %C is not ABSTRACT",
7397 /* See if we already have a binding with this name in the symtree which would
7398 be an error. If a GENERIC already targetted this binding, it may be
7399 already there but then typebound is still NULL. */
7400 stree = gfc_find_symtree (ns->tb_sym_root, name);
7401 if (stree && stree->n.tb)
7403 gfc_error ("There's already a procedure with binding name '%s' for the"
7404 " derived type '%s' at %C", name, block->name);
7408 /* Insert it and set attributes. */
7412 stree = gfc_new_symtree (&ns->tb_sym_root, name);
7417 if (gfc_get_sym_tree (target, gfc_current_ns, &tb->u.specific, false))
7419 gfc_set_sym_referenced (tb->u.specific->n.sym);
7425 /* Match a GENERIC procedure binding inside a derived type. */
7428 gfc_match_generic (void)
7430 char name[GFC_MAX_SYMBOL_LEN + 1];
7431 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
7433 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
7434 gfc_typebound_proc* tb;
7436 interface_type op_type;
7437 gfc_intrinsic_op op;
7440 /* Check current state. */
7441 if (gfc_current_state () == COMP_DERIVED)
7443 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
7446 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
7448 block = gfc_state_stack->previous->sym;
7449 ns = block->f2k_derived;
7450 gcc_assert (block && ns);
7452 /* See if we get an access-specifier. */
7453 m = match_binding_attributes (&tbattr, true, false);
7454 if (m == MATCH_ERROR)
7457 /* Now the colons, those are required. */
7458 if (gfc_match (" ::") != MATCH_YES)
7460 gfc_error ("Expected '::' at %C");
7464 /* Match the binding name; depending on type (operator / generic) format
7465 it for future error messages into bind_name. */
7467 m = gfc_match_generic_spec (&op_type, name, &op);
7468 if (m == MATCH_ERROR)
7472 gfc_error ("Expected generic name or operator descriptor at %C");
7478 case INTERFACE_GENERIC:
7479 snprintf (bind_name, sizeof (bind_name), "%s", name);
7482 case INTERFACE_USER_OP:
7483 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
7486 case INTERFACE_INTRINSIC_OP:
7487 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
7488 gfc_op2string (op));
7495 /* Match the required =>. */
7496 if (gfc_match (" =>") != MATCH_YES)
7498 gfc_error ("Expected '=>' at %C");
7502 /* Try to find existing GENERIC binding with this name / for this operator;
7503 if there is something, check that it is another GENERIC and then extend
7504 it rather than building a new node. Otherwise, create it and put it
7505 at the right position. */
7509 case INTERFACE_USER_OP:
7510 case INTERFACE_GENERIC:
7512 const bool is_op = (op_type == INTERFACE_USER_OP);
7515 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
7527 case INTERFACE_INTRINSIC_OP:
7537 if (!tb->is_generic)
7539 gcc_assert (op_type == INTERFACE_GENERIC);
7540 gfc_error ("There's already a non-generic procedure with binding name"
7541 " '%s' for the derived type '%s' at %C",
7542 bind_name, block->name);
7546 if (tb->access != tbattr.access)
7548 gfc_error ("Binding at %C must have the same access as already"
7549 " defined binding '%s'", bind_name);
7555 tb = gfc_get_typebound_proc ();
7556 tb->where = gfc_current_locus;
7557 tb->access = tbattr.access;
7559 tb->u.generic = NULL;
7563 case INTERFACE_GENERIC:
7564 case INTERFACE_USER_OP:
7566 const bool is_op = (op_type == INTERFACE_USER_OP);
7569 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
7577 case INTERFACE_INTRINSIC_OP:
7586 /* Now, match all following names as specific targets. */
7589 gfc_symtree* target_st;
7590 gfc_tbp_generic* target;
7592 m = gfc_match_name (name);
7593 if (m == MATCH_ERROR)
7597 gfc_error ("Expected specific binding name at %C");
7601 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
7603 /* See if this is a duplicate specification. */
7604 for (target = tb->u.generic; target; target = target->next)
7605 if (target_st == target->specific_st)
7607 gfc_error ("'%s' already defined as specific binding for the"
7608 " generic '%s' at %C", name, bind_name);
7612 target = gfc_get_tbp_generic ();
7613 target->specific_st = target_st;
7614 target->specific = NULL;
7615 target->next = tb->u.generic;
7616 tb->u.generic = target;
7618 while (gfc_match (" ,") == MATCH_YES);
7620 /* Here should be the end. */
7621 if (gfc_match_eos () != MATCH_YES)
7623 gfc_error ("Junk after GENERIC binding at %C");
7634 /* Match a FINAL declaration inside a derived type. */
7637 gfc_match_final_decl (void)
7639 char name[GFC_MAX_SYMBOL_LEN + 1];
7642 gfc_namespace* module_ns;
7646 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
7648 gfc_error ("FINAL declaration at %C must be inside a derived type "
7649 "CONTAINS section");
7653 block = gfc_state_stack->previous->sym;
7656 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
7657 || gfc_state_stack->previous->previous->state != COMP_MODULE)
7659 gfc_error ("Derived type declaration with FINAL at %C must be in the"
7660 " specification part of a MODULE");
7664 module_ns = gfc_current_ns;
7665 gcc_assert (module_ns);
7666 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
7668 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
7669 if (gfc_match (" ::") == MATCH_ERROR)
7672 /* Match the sequence of procedure names. */
7679 if (first && gfc_match_eos () == MATCH_YES)
7681 gfc_error ("Empty FINAL at %C");
7685 m = gfc_match_name (name);
7688 gfc_error ("Expected module procedure name at %C");
7691 else if (m != MATCH_YES)
7694 if (gfc_match_eos () == MATCH_YES)
7696 if (!last && gfc_match_char (',') != MATCH_YES)
7698 gfc_error ("Expected ',' at %C");
7702 if (gfc_get_symbol (name, module_ns, &sym))
7704 gfc_error ("Unknown procedure name \"%s\" at %C", name);
7708 /* Mark the symbol as module procedure. */
7709 if (sym->attr.proc != PROC_MODULE
7710 && gfc_add_procedure (&sym->attr, PROC_MODULE,
7711 sym->name, NULL) == FAILURE)
7714 /* Check if we already have this symbol in the list, this is an error. */
7715 for (f = block->f2k_derived->finalizers; f; f = f->next)
7716 if (f->proc_sym == sym)
7718 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
7723 /* Add this symbol to the list of finalizers. */
7724 gcc_assert (block->f2k_derived);
7726 f = XCNEW (gfc_finalizer);
7728 f->proc_tree = NULL;
7729 f->where = gfc_current_locus;
7730 f->next = block->f2k_derived->finalizers;
7731 block->f2k_derived->finalizers = f;
7741 const ext_attr_t ext_attr_list[] = {
7742 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
7743 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
7744 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
7745 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
7746 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
7747 { NULL, EXT_ATTR_LAST, NULL }
7750 /* Match a !GCC$ ATTRIBUTES statement of the form:
7751 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
7752 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
7754 TODO: We should support all GCC attributes using the same syntax for
7755 the attribute list, i.e. the list in C
7756 __attributes(( attribute-list ))
7758 !GCC$ ATTRIBUTES attribute-list ::
7759 Cf. c-parser.c's c_parser_attributes; the data can then directly be
7762 As there is absolutely no risk of confusion, we should never return
7765 gfc_match_gcc_attributes (void)
7767 symbol_attribute attr;
7768 char name[GFC_MAX_SYMBOL_LEN + 1];
7773 gfc_clear_attr (&attr);
7778 if (gfc_match_name (name) != MATCH_YES)
7781 for (id = 0; id < EXT_ATTR_LAST; id++)
7782 if (strcmp (name, ext_attr_list[id].name) == 0)
7785 if (id == EXT_ATTR_LAST)
7787 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
7791 if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
7795 gfc_gobble_whitespace ();
7796 ch = gfc_next_ascii_char ();
7799 /* This is the successful exit condition for the loop. */
7800 if (gfc_next_ascii_char () == ':')
7810 if (gfc_match_eos () == MATCH_YES)
7815 m = gfc_match_name (name);
7819 if (find_special (name, &sym, true))
7822 sym->attr.ext_attr |= attr.ext_attr;
7824 if (gfc_match_eos () == MATCH_YES)
7827 if (gfc_match_char (',') != MATCH_YES)
7834 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");