1 /* Declaration statement matcher
2 Copyright (C) 2002-2013 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
28 #include "constructor.h"
31 /* Macros to access allocate memory for gfc_data_variable,
32 gfc_data_value and gfc_data. */
33 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
34 #define gfc_get_data_value() XCNEW (gfc_data_value)
35 #define gfc_get_data() XCNEW (gfc_data)
38 static gfc_try set_binding_label (const char **, const char *, int);
41 /* This flag is set if an old-style length selector is matched
42 during a type-declaration statement. */
44 static int old_char_selector;
46 /* When variables acquire types and attributes from a declaration
47 statement, they get them from the following static variables. The
48 first part of a declaration sets these variables and the second
49 part copies these into symbol structures. */
51 static gfc_typespec current_ts;
53 static symbol_attribute current_attr;
54 static gfc_array_spec *current_as;
55 static int colon_seen;
57 /* The current binding label (if any). */
58 static const char* curr_binding_label;
59 /* Need to know how many identifiers are on the current data declaration
60 line in case we're given the BIND(C) attribute with a NAME= specifier. */
61 static int num_idents_on_line;
62 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
63 can supply a name if the curr_binding_label is nil and NAME= was not. */
64 static int has_name_equals = 0;
66 /* Initializer of the previous enumerator. */
68 static gfc_expr *last_initializer;
70 /* History of all the enumerators is maintained, so that
71 kind values of all the enumerators could be updated depending
72 upon the maximum initialized value. */
74 typedef struct enumerator_history
77 gfc_expr *initializer;
78 struct enumerator_history *next;
82 /* Header of enum history chain. */
84 static enumerator_history *enum_history = NULL;
86 /* Pointer of enum history node containing largest initializer. */
88 static enumerator_history *max_enum = NULL;
90 /* gfc_new_block points to the symbol of a newly matched block. */
92 gfc_symbol *gfc_new_block;
94 bool gfc_matching_function;
97 /********************* DATA statement subroutines *********************/
99 static bool in_match_data = false;
102 gfc_in_match_data (void)
104 return in_match_data;
108 set_in_match_data (bool set_value)
110 in_match_data = set_value;
113 /* Free a gfc_data_variable structure and everything beneath it. */
116 free_variable (gfc_data_variable *p)
118 gfc_data_variable *q;
123 gfc_free_expr (p->expr);
124 gfc_free_iterator (&p->iter, 0);
125 free_variable (p->list);
131 /* Free a gfc_data_value structure and everything beneath it. */
134 free_value (gfc_data_value *p)
141 mpz_clear (p->repeat);
142 gfc_free_expr (p->expr);
148 /* Free a list of gfc_data structures. */
151 gfc_free_data (gfc_data *p)
158 free_variable (p->var);
159 free_value (p->value);
165 /* Free all data in a namespace. */
168 gfc_free_data_all (gfc_namespace *ns)
181 static match var_element (gfc_data_variable *);
183 /* Match a list of variables terminated by an iterator and a right
187 var_list (gfc_data_variable *parent)
189 gfc_data_variable *tail, var;
192 m = var_element (&var);
193 if (m == MATCH_ERROR)
198 tail = gfc_get_data_variable ();
205 if (gfc_match_char (',') != MATCH_YES)
208 m = gfc_match_iterator (&parent->iter, 1);
211 if (m == MATCH_ERROR)
214 m = var_element (&var);
215 if (m == MATCH_ERROR)
220 tail->next = gfc_get_data_variable ();
226 if (gfc_match_char (')') != MATCH_YES)
231 gfc_syntax_error (ST_DATA);
236 /* Match a single element in a data variable list, which can be a
237 variable-iterator list. */
240 var_element (gfc_data_variable *new_var)
245 memset (new_var, 0, sizeof (gfc_data_variable));
247 if (gfc_match_char ('(') == MATCH_YES)
248 return var_list (new_var);
250 m = gfc_match_variable (&new_var->expr, 0);
254 sym = new_var->expr->symtree->n.sym;
256 /* Symbol should already have an associated type. */
257 if (gfc_check_symbol_typed (sym, gfc_current_ns,
258 false, gfc_current_locus) == FAILURE)
261 if (!sym->attr.function && gfc_current_ns->parent
262 && gfc_current_ns->parent == sym->ns)
264 gfc_error ("Host associated variable '%s' may not be in the DATA "
265 "statement at %C", sym->name);
269 if (gfc_current_state () != COMP_BLOCK_DATA
270 && sym->attr.in_common
271 && gfc_notify_std (GFC_STD_GNU, "initialization of "
272 "common block variable '%s' in DATA statement at %C",
273 sym->name) == FAILURE)
276 if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
283 /* Match the top-level list of data variables. */
286 top_var_list (gfc_data *d)
288 gfc_data_variable var, *tail, *new_var;
295 m = var_element (&var);
298 if (m == MATCH_ERROR)
301 new_var = gfc_get_data_variable ();
307 tail->next = new_var;
311 if (gfc_match_char ('/') == MATCH_YES)
313 if (gfc_match_char (',') != MATCH_YES)
320 gfc_syntax_error (ST_DATA);
321 gfc_free_data_all (gfc_current_ns);
327 match_data_constant (gfc_expr **result)
329 char name[GFC_MAX_SYMBOL_LEN + 1];
330 gfc_symbol *sym, *dt_sym = NULL;
335 m = gfc_match_literal_constant (&expr, 1);
342 if (m == MATCH_ERROR)
345 m = gfc_match_null (result);
349 old_loc = gfc_current_locus;
351 /* Should this be a structure component, try to match it
352 before matching a name. */
353 m = gfc_match_rvalue (result);
354 if (m == MATCH_ERROR)
357 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
359 if (gfc_simplify_expr (*result, 0) == FAILURE)
363 else if (m == MATCH_YES)
364 gfc_free_expr (*result);
366 gfc_current_locus = old_loc;
368 m = gfc_match_name (name);
372 if (gfc_find_symbol (name, NULL, 1, &sym))
375 if (sym && sym->attr.generic)
376 dt_sym = gfc_find_dt_in_generic (sym);
379 || (sym->attr.flavor != FL_PARAMETER
380 && (!dt_sym || dt_sym->attr.flavor != FL_DERIVED)))
382 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
386 else if (dt_sym && dt_sym->attr.flavor == FL_DERIVED)
387 return gfc_match_structure_constructor (dt_sym, result);
389 /* Check to see if the value is an initialization array expression. */
390 if (sym->value->expr_type == EXPR_ARRAY)
392 gfc_current_locus = old_loc;
394 m = gfc_match_init_expr (result);
395 if (m == MATCH_ERROR)
400 if (gfc_simplify_expr (*result, 0) == FAILURE)
403 if ((*result)->expr_type == EXPR_CONSTANT)
407 gfc_error ("Invalid initializer %s in Data statement at %C", name);
413 *result = gfc_copy_expr (sym->value);
418 /* Match a list of values in a DATA statement. The leading '/' has
419 already been seen at this point. */
422 top_val_list (gfc_data *data)
424 gfc_data_value *new_val, *tail;
432 m = match_data_constant (&expr);
435 if (m == MATCH_ERROR)
438 new_val = gfc_get_data_value ();
439 mpz_init (new_val->repeat);
442 data->value = new_val;
444 tail->next = new_val;
448 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
451 mpz_set_ui (tail->repeat, 1);
455 mpz_set (tail->repeat, expr->value.integer);
456 gfc_free_expr (expr);
458 m = match_data_constant (&tail->expr);
461 if (m == MATCH_ERROR)
465 if (gfc_match_char ('/') == MATCH_YES)
467 if (gfc_match_char (',') == MATCH_NO)
474 gfc_syntax_error (ST_DATA);
475 gfc_free_data_all (gfc_current_ns);
480 /* Matches an old style initialization. */
483 match_old_style_init (const char *name)
490 /* Set up data structure to hold initializers. */
491 gfc_find_sym_tree (name, NULL, 0, &st);
494 newdata = gfc_get_data ();
495 newdata->var = gfc_get_data_variable ();
496 newdata->var->expr = gfc_get_variable_expr (st);
497 newdata->where = gfc_current_locus;
499 /* Match initial value list. This also eats the terminal '/'. */
500 m = top_val_list (newdata);
509 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
514 if (gfc_implicit_pure (NULL))
515 gfc_current_ns->proc_name->attr.implicit_pure = 0;
517 /* Mark the variable as having appeared in a data statement. */
518 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
524 /* Chain in namespace list of DATA initializers. */
525 newdata->next = gfc_current_ns->data;
526 gfc_current_ns->data = newdata;
532 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
533 we are matching a DATA statement and are therefore issuing an error
534 if we encounter something unexpected, if not, we're trying to match
535 an old-style initialization expression of the form INTEGER I /2/. */
538 gfc_match_data (void)
543 set_in_match_data (true);
547 new_data = gfc_get_data ();
548 new_data->where = gfc_current_locus;
550 m = top_var_list (new_data);
554 m = top_val_list (new_data);
558 new_data->next = gfc_current_ns->data;
559 gfc_current_ns->data = new_data;
561 if (gfc_match_eos () == MATCH_YES)
564 gfc_match_char (','); /* Optional comma */
567 set_in_match_data (false);
571 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
575 if (gfc_implicit_pure (NULL))
576 gfc_current_ns->proc_name->attr.implicit_pure = 0;
581 set_in_match_data (false);
582 gfc_free_data (new_data);
587 /************************ Declaration statements *********************/
590 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
593 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
597 if ((from->type == AS_ASSUMED_RANK && to->corank)
598 || (to->type == AS_ASSUMED_RANK && from->corank))
600 gfc_error ("The assumed-rank array at %C shall not have a codimension");
604 if (to->rank == 0 && from->rank > 0)
606 to->rank = from->rank;
607 to->type = from->type;
608 to->cray_pointee = from->cray_pointee;
609 to->cp_was_assumed = from->cp_was_assumed;
611 for (i = 0; i < to->corank; i++)
613 to->lower[from->rank + i] = to->lower[i];
614 to->upper[from->rank + i] = to->upper[i];
616 for (i = 0; i < from->rank; i++)
620 to->lower[i] = gfc_copy_expr (from->lower[i]);
621 to->upper[i] = gfc_copy_expr (from->upper[i]);
625 to->lower[i] = from->lower[i];
626 to->upper[i] = from->upper[i];
630 else if (to->corank == 0 && from->corank > 0)
632 to->corank = from->corank;
633 to->cotype = from->cotype;
635 for (i = 0; i < from->corank; i++)
639 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
640 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
644 to->lower[to->rank + i] = from->lower[i];
645 to->upper[to->rank + i] = from->upper[i];
654 /* Match an intent specification. Since this can only happen after an
655 INTENT word, a legal intent-spec must follow. */
658 match_intent_spec (void)
661 if (gfc_match (" ( in out )") == MATCH_YES)
663 if (gfc_match (" ( in )") == MATCH_YES)
665 if (gfc_match (" ( out )") == MATCH_YES)
668 gfc_error ("Bad INTENT specification at %C");
669 return INTENT_UNKNOWN;
673 /* Matches a character length specification, which is either a
674 specification expression, '*', or ':'. */
677 char_len_param_value (gfc_expr **expr, bool *deferred)
684 if (gfc_match_char ('*') == MATCH_YES)
687 if (gfc_match_char (':') == MATCH_YES)
689 if (gfc_notify_std (GFC_STD_F2003, "deferred type "
690 "parameter at %C") == FAILURE)
698 m = gfc_match_expr (expr);
701 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
704 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
706 if ((*expr)->value.function.actual
707 && (*expr)->value.function.actual->expr->symtree)
710 e = (*expr)->value.function.actual->expr;
711 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
712 && e->expr_type == EXPR_VARIABLE)
714 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
716 if (e->symtree->n.sym->ts.type == BT_CHARACTER
717 && e->symtree->n.sym->ts.u.cl
718 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
726 gfc_error ("Conflict in attributes of function argument at %C");
731 /* A character length is a '*' followed by a literal integer or a
732 char_len_param_value in parenthesis. */
735 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
741 m = gfc_match_char ('*');
745 m = gfc_match_small_literal_int (&length, NULL);
746 if (m == MATCH_ERROR)
751 if (obsolescent_check
752 && gfc_notify_std (GFC_STD_F95_OBS,
753 "Old-style character length at %C") == FAILURE)
755 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
759 if (gfc_match_char ('(') == MATCH_NO)
762 m = char_len_param_value (expr, deferred);
763 if (m != MATCH_YES && gfc_matching_function)
769 if (m == MATCH_ERROR)
774 if (gfc_match_char (')') == MATCH_NO)
776 gfc_free_expr (*expr);
784 gfc_error ("Syntax error in character length specification at %C");
789 /* Special subroutine for finding a symbol. Check if the name is found
790 in the current name space. If not, and we're compiling a function or
791 subroutine and the parent compilation unit is an interface, then check
792 to see if the name we've been given is the name of the interface
793 (located in another namespace). */
796 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
802 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
805 *result = st ? st->n.sym : NULL;
809 if (gfc_current_state () != COMP_SUBROUTINE
810 && gfc_current_state () != COMP_FUNCTION)
813 s = gfc_state_stack->previous;
817 if (s->state != COMP_INTERFACE)
820 goto end; /* Nameless interface. */
822 if (strcmp (name, s->sym->name) == 0)
833 /* Special subroutine for getting a symbol node associated with a
834 procedure name, used in SUBROUTINE and FUNCTION statements. The
835 symbol is created in the parent using with symtree node in the
836 child unit pointing to the symbol. If the current namespace has no
837 parent, then the symbol is just created in the current unit. */
840 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
846 /* Module functions have to be left in their own namespace because
847 they have potentially (almost certainly!) already been referenced.
848 In this sense, they are rather like external functions. This is
849 fixed up in resolve.c(resolve_entries), where the symbol name-
850 space is set to point to the master function, so that the fake
851 result mechanism can work. */
852 if (module_fcn_entry)
854 /* Present if entry is declared to be a module procedure. */
855 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
858 rc = gfc_get_symbol (name, NULL, result);
859 else if (!gfc_get_symbol (name, NULL, &sym) && sym
860 && (*result)->ts.type == BT_UNKNOWN
861 && sym->attr.flavor == FL_UNKNOWN)
862 /* Pick up the typespec for the entry, if declared in the function
863 body. Note that this symbol is FL_UNKNOWN because it will
864 only have appeared in a type declaration. The local symtree
865 is set to point to the module symbol and a unique symtree
866 to the local version. This latter ensures a correct clearing
869 /* If the ENTRY proceeds its specification, we need to ensure
870 that this does not raise a "has no IMPLICIT type" error. */
871 if (sym->ts.type == BT_UNKNOWN)
872 sym->attr.untyped = 1;
874 (*result)->ts = sym->ts;
876 /* Put the symbol in the procedure namespace so that, should
877 the ENTRY precede its specification, the specification
879 (*result)->ns = gfc_current_ns;
881 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
883 st = gfc_get_unique_symtree (gfc_current_ns);
888 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
895 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
897 /* Trap another encompassed procedure with the same name. All
898 these conditions are necessary to avoid picking up an entry
899 whose name clashes with that of the encompassing procedure;
900 this is handled using gsymbols to register unique,globally
902 if (sym->attr.flavor != 0
903 && sym->attr.proc != 0
904 && (sym->attr.subroutine || sym->attr.function)
905 && sym->attr.if_source != IFSRC_UNKNOWN)
906 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
907 name, &sym->declared_at);
909 /* Trap a procedure with a name the same as interface in the
910 encompassing scope. */
911 if (sym->attr.generic != 0
912 && (sym->attr.subroutine || sym->attr.function)
913 && !sym->attr.mod_proc)
914 gfc_error_now ("Name '%s' at %C is already defined"
915 " as a generic interface at %L",
916 name, &sym->declared_at);
918 /* Trap declarations of attributes in encompassing scope. The
919 signature for this is that ts.kind is set. Legitimate
920 references only set ts.type. */
921 if (sym->ts.kind != 0
922 && !sym->attr.implicit_type
923 && sym->attr.proc == 0
924 && gfc_current_ns->parent != NULL
925 && sym->attr.access == 0
926 && !module_fcn_entry)
927 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
928 "and must not have attributes declared at %L",
929 name, &sym->declared_at);
932 if (gfc_current_ns->parent == NULL || *result == NULL)
935 /* Module function entries will already have a symtree in
936 the current namespace but will need one at module level. */
937 if (module_fcn_entry)
939 /* Present if entry is declared to be a module procedure. */
940 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
942 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
945 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
950 /* See if the procedure should be a module procedure. */
952 if (((sym->ns->proc_name != NULL
953 && sym->ns->proc_name->attr.flavor == FL_MODULE
954 && sym->attr.proc != PROC_MODULE)
955 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
956 && gfc_add_procedure (&sym->attr, PROC_MODULE,
957 sym->name, NULL) == FAILURE)
964 /* Verify that the given symbol representing a parameter is C
965 interoperable, by checking to see if it was marked as such after
966 its declaration. If the given symbol is not interoperable, a
967 warning is reported, thus removing the need to return the status to
968 the calling function. The standard does not require the user use
969 one of the iso_c_binding named constants to declare an
970 interoperable parameter, but we can't be sure if the param is C
971 interop or not if the user doesn't. For example, integer(4) may be
972 legal Fortran, but doesn't have meaning in C. It may interop with
973 a number of the C types, which causes a problem because the
974 compiler can't know which one. This code is almost certainly not
975 portable, and the user will get what they deserve if the C type
976 across platforms isn't always interoperable with integer(4). If
977 the user had used something like integer(c_int) or integer(c_long),
978 the compiler could have automatically handled the varying sizes
982 gfc_verify_c_interop_param (gfc_symbol *sym)
984 int is_c_interop = 0;
985 gfc_try retval = SUCCESS;
987 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
988 Don't repeat the checks here. */
989 if (sym->attr.implicit_type)
992 /* For subroutines or functions that are passed to a BIND(C) procedure,
993 they're interoperable if they're BIND(C) and their params are all
995 if (sym->attr.flavor == FL_PROCEDURE)
997 if (sym->attr.is_bind_c == 0)
999 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
1000 "attribute to be C interoperable", sym->name,
1001 &(sym->declared_at));
1007 if (sym->attr.is_c_interop == 1)
1008 /* We've already checked this procedure; don't check it again. */
1011 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1016 /* See if we've stored a reference to a procedure that owns sym. */
1017 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1019 if (sym->ns->proc_name->attr.is_bind_c == 1)
1021 is_c_interop = (gfc_verify_c_interop (&(sym->ts)) == SUCCESS ? 1 : 0);
1023 if (is_c_interop != 1)
1025 /* Make personalized messages to give better feedback. */
1026 if (sym->ts.type == BT_DERIVED)
1027 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1028 "BIND(C) procedure '%s' but is not C interoperable "
1029 "because derived type '%s' is not C interoperable",
1030 sym->name, &(sym->declared_at),
1031 sym->ns->proc_name->name,
1032 sym->ts.u.derived->name);
1033 else if (sym->ts.type == BT_CLASS)
1034 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1035 "BIND(C) procedure '%s' but is not C interoperable "
1036 "because it is polymorphic",
1037 sym->name, &(sym->declared_at),
1038 sym->ns->proc_name->name);
1039 else if (gfc_option.warn_c_binding_type)
1040 gfc_warning ("Variable '%s' at %L is a dummy argument of the "
1041 "BIND(C) procedure '%s' but may not be C "
1043 sym->name, &(sym->declared_at),
1044 sym->ns->proc_name->name);
1047 /* Character strings are only C interoperable if they have a
1049 if (sym->ts.type == BT_CHARACTER)
1051 gfc_charlen *cl = sym->ts.u.cl;
1052 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1053 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1055 gfc_error ("Character argument '%s' at %L "
1056 "must be length 1 because "
1057 "procedure '%s' is BIND(C)",
1058 sym->name, &sym->declared_at,
1059 sym->ns->proc_name->name);
1064 /* We have to make sure that any param to a bind(c) routine does
1065 not have the allocatable, pointer, or optional attributes,
1066 according to J3/04-007, section 5.1. */
1067 if (sym->attr.allocatable == 1)
1069 gfc_error ("Variable '%s' at %L cannot have the "
1070 "ALLOCATABLE attribute because procedure '%s'"
1071 " is BIND(C)", sym->name, &(sym->declared_at),
1072 sym->ns->proc_name->name);
1076 if (sym->attr.pointer == 1)
1078 gfc_error ("Variable '%s' at %L cannot have the "
1079 "POINTER attribute because procedure '%s'"
1080 " is BIND(C)", sym->name, &(sym->declared_at),
1081 sym->ns->proc_name->name);
1085 if (sym->attr.optional == 1 && sym->attr.value)
1087 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1088 "and the VALUE attribute because procedure '%s' "
1089 "is BIND(C)", sym->name, &(sym->declared_at),
1090 sym->ns->proc_name->name);
1093 else if (sym->attr.optional == 1
1094 && gfc_notify_std (GFC_STD_F2008_TS, "Variable '%s' "
1095 "at %L with OPTIONAL attribute in "
1096 "procedure '%s' which is BIND(C)",
1097 sym->name, &(sym->declared_at),
1098 sym->ns->proc_name->name)
1102 /* Make sure that if it has the dimension attribute, that it is
1103 either assumed size or explicit shape. Deferred shape is already
1104 covered by the pointer/allocatable attribute. */
1105 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1106 && gfc_notify_std (GFC_STD_F2008_TS, "Assumed-shape array '%s' "
1107 "at %L as dummy argument to the BIND(C) "
1108 "procedure '%s' at %L", sym->name,
1109 &(sym->declared_at), sym->ns->proc_name->name,
1110 &(sym->ns->proc_name->declared_at)) == FAILURE)
1120 /* Function called by variable_decl() that adds a name to the symbol table. */
1123 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1124 gfc_array_spec **as, locus *var_locus)
1126 symbol_attribute attr;
1129 if (gfc_get_symbol (name, NULL, &sym))
1132 /* Start updating the symbol table. Add basic type attribute if present. */
1133 if (current_ts.type != BT_UNKNOWN
1134 && (sym->attr.implicit_type == 0
1135 || !gfc_compare_types (&sym->ts, ¤t_ts))
1136 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
1139 if (sym->ts.type == BT_CHARACTER)
1142 sym->ts.deferred = cl_deferred;
1145 /* Add dimension attribute if present. */
1146 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1150 /* Add attribute to symbol. The copy is so that we can reset the
1151 dimension attribute. */
1152 attr = current_attr;
1154 attr.codimension = 0;
1156 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1159 /* Finish any work that may need to be done for the binding label,
1160 if it's a bind(c). The bind(c) attr is found before the symbol
1161 is made, and before the symbol name (for data decls), so the
1162 current_ts is holding the binding label, or nothing if the
1163 name= attr wasn't given. Therefore, test here if we're dealing
1164 with a bind(c) and make sure the binding label is set correctly. */
1165 if (sym->attr.is_bind_c == 1)
1167 if (!sym->binding_label)
1169 /* Set the binding label and verify that if a NAME= was specified
1170 then only one identifier was in the entity-decl-list. */
1171 if (set_binding_label (&sym->binding_label, sym->name,
1172 num_idents_on_line) == FAILURE)
1177 /* See if we know we're in a common block, and if it's a bind(c)
1178 common then we need to make sure we're an interoperable type. */
1179 if (sym->attr.in_common == 1)
1181 /* Test the common block object. */
1182 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1183 && sym->ts.is_c_interop != 1)
1185 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1186 "must be declared with a C interoperable "
1187 "kind since common block '%s' is BIND(C)",
1188 sym->name, sym->common_block->name,
1189 sym->common_block->name);
1194 sym->attr.implied_index = 0;
1196 if (sym->ts.type == BT_CLASS)
1197 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1203 /* Set character constant to the given length. The constant will be padded or
1204 truncated. If we're inside an array constructor without a typespec, we
1205 additionally check that all elements have the same length; check_len -1
1206 means no checking. */
1209 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1214 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1215 gcc_assert (expr->ts.type == BT_CHARACTER);
1217 slen = expr->value.character.length;
1220 s = gfc_get_wide_string (len + 1);
1221 memcpy (s, expr->value.character.string,
1222 MIN (len, slen) * sizeof (gfc_char_t));
1224 gfc_wide_memset (&s[slen], ' ', len - slen);
1226 if (gfc_option.warn_character_truncation && slen > len)
1227 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1228 "(%d/%d)", &expr->where, slen, len);
1230 /* Apply the standard by 'hand' otherwise it gets cleared for
1232 if (check_len != -1 && slen != check_len
1233 && !(gfc_option.allow_std & GFC_STD_GNU))
1234 gfc_error_now ("The CHARACTER elements of the array constructor "
1235 "at %L must have the same length (%d/%d)",
1236 &expr->where, slen, check_len);
1239 free (expr->value.character.string);
1240 expr->value.character.string = s;
1241 expr->value.character.length = len;
1246 /* Function to create and update the enumerator history
1247 using the information passed as arguments.
1248 Pointer "max_enum" is also updated, to point to
1249 enum history node containing largest initializer.
1251 SYM points to the symbol node of enumerator.
1252 INIT points to its enumerator value. */
1255 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1257 enumerator_history *new_enum_history;
1258 gcc_assert (sym != NULL && init != NULL);
1260 new_enum_history = XCNEW (enumerator_history);
1262 new_enum_history->sym = sym;
1263 new_enum_history->initializer = init;
1264 new_enum_history->next = NULL;
1266 if (enum_history == NULL)
1268 enum_history = new_enum_history;
1269 max_enum = enum_history;
1273 new_enum_history->next = enum_history;
1274 enum_history = new_enum_history;
1276 if (mpz_cmp (max_enum->initializer->value.integer,
1277 new_enum_history->initializer->value.integer) < 0)
1278 max_enum = new_enum_history;
1283 /* Function to free enum kind history. */
1286 gfc_free_enum_history (void)
1288 enumerator_history *current = enum_history;
1289 enumerator_history *next;
1291 while (current != NULL)
1293 next = current->next;
1298 enum_history = NULL;
1302 /* Function called by variable_decl() that adds an initialization
1303 expression to a symbol. */
1306 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1308 symbol_attribute attr;
1313 if (find_special (name, &sym, false))
1318 /* If this symbol is confirming an implicit parameter type,
1319 then an initialization expression is not allowed. */
1320 if (attr.flavor == FL_PARAMETER
1321 && sym->value != NULL
1324 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1331 /* An initializer is required for PARAMETER declarations. */
1332 if (attr.flavor == FL_PARAMETER)
1334 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1340 /* If a variable appears in a DATA block, it cannot have an
1344 gfc_error ("Variable '%s' at %C with an initializer already "
1345 "appears in a DATA statement", sym->name);
1349 /* Check if the assignment can happen. This has to be put off
1350 until later for derived type variables and procedure pointers. */
1351 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1352 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1353 && !sym->attr.proc_pointer
1354 && gfc_check_assign_symbol (sym, NULL, init) == FAILURE)
1357 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1358 && init->ts.type == BT_CHARACTER)
1360 /* Update symbol character length according initializer. */
1361 if (gfc_check_assign_symbol (sym, NULL, init) == FAILURE)
1364 if (sym->ts.u.cl->length == NULL)
1367 /* If there are multiple CHARACTER variables declared on the
1368 same line, we don't want them to share the same length. */
1369 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1371 if (sym->attr.flavor == FL_PARAMETER)
1373 if (init->expr_type == EXPR_CONSTANT)
1375 clen = init->value.character.length;
1376 sym->ts.u.cl->length
1377 = gfc_get_int_expr (gfc_default_integer_kind,
1380 else if (init->expr_type == EXPR_ARRAY)
1383 c = gfc_constructor_first (init->value.constructor);
1384 clen = c->expr->value.character.length;
1385 sym->ts.u.cl->length
1386 = gfc_get_int_expr (gfc_default_integer_kind,
1389 else if (init->ts.u.cl && init->ts.u.cl->length)
1390 sym->ts.u.cl->length =
1391 gfc_copy_expr (sym->value->ts.u.cl->length);
1394 /* Update initializer character length according symbol. */
1395 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1397 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1399 if (init->expr_type == EXPR_CONSTANT)
1400 gfc_set_constant_character_len (len, init, -1);
1401 else if (init->expr_type == EXPR_ARRAY)
1405 /* Build a new charlen to prevent simplification from
1406 deleting the length before it is resolved. */
1407 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1408 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1410 for (c = gfc_constructor_first (init->value.constructor);
1411 c; c = gfc_constructor_next (c))
1412 gfc_set_constant_character_len (len, c->expr, -1);
1417 /* If sym is implied-shape, set its upper bounds from init. */
1418 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1419 && sym->as->type == AS_IMPLIED_SHAPE)
1423 if (init->rank == 0)
1425 gfc_error ("Can't initialize implied-shape array at %L"
1426 " with scalar", &sym->declared_at);
1429 gcc_assert (sym->as->rank == init->rank);
1431 /* Shape should be present, we get an initialization expression. */
1432 gcc_assert (init->shape);
1434 for (dim = 0; dim < sym->as->rank; ++dim)
1440 lower = sym->as->lower[dim];
1441 if (lower->expr_type != EXPR_CONSTANT)
1443 gfc_error ("Non-constant lower bound in implied-shape"
1444 " declaration at %L", &lower->where);
1448 /* All dimensions must be without upper bound. */
1449 gcc_assert (!sym->as->upper[dim]);
1452 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1453 mpz_add (e->value.integer,
1454 lower->value.integer, init->shape[dim]);
1455 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1456 sym->as->upper[dim] = e;
1459 sym->as->type = AS_EXPLICIT;
1462 /* Need to check if the expression we initialized this
1463 to was one of the iso_c_binding named constants. If so,
1464 and we're a parameter (constant), let it be iso_c.
1466 integer(c_int), parameter :: my_int = c_int
1467 integer(my_int) :: my_int_2
1468 If we mark my_int as iso_c (since we can see it's value
1469 is equal to one of the named constants), then my_int_2
1470 will be considered C interoperable. */
1471 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1473 sym->ts.is_iso_c |= init->ts.is_iso_c;
1474 sym->ts.is_c_interop |= init->ts.is_c_interop;
1475 /* attr bits needed for module files. */
1476 sym->attr.is_iso_c |= init->ts.is_iso_c;
1477 sym->attr.is_c_interop |= init->ts.is_c_interop;
1478 if (init->ts.is_iso_c)
1479 sym->ts.f90_type = init->ts.f90_type;
1482 /* Add initializer. Make sure we keep the ranks sane. */
1483 if (sym->attr.dimension && init->rank == 0)
1488 if (sym->attr.flavor == FL_PARAMETER
1489 && init->expr_type == EXPR_CONSTANT
1490 && spec_size (sym->as, &size) == SUCCESS
1491 && mpz_cmp_si (size, 0) > 0)
1493 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1495 for (n = 0; n < (int)mpz_get_si (size); n++)
1496 gfc_constructor_append_expr (&array->value.constructor,
1499 : gfc_copy_expr (init),
1502 array->shape = gfc_get_shape (sym->as->rank);
1503 for (n = 0; n < sym->as->rank; n++)
1504 spec_dimen_size (sym->as, n, &array->shape[n]);
1509 init->rank = sym->as->rank;
1513 if (sym->attr.save == SAVE_NONE)
1514 sym->attr.save = SAVE_IMPLICIT;
1522 /* Function called by variable_decl() that adds a name to a structure
1526 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1527 gfc_array_spec **as)
1530 gfc_try t = SUCCESS;
1532 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1533 constructing, it must have the pointer attribute. */
1534 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1535 && current_ts.u.derived == gfc_current_block ()
1536 && current_attr.pointer == 0)
1538 gfc_error ("Component at %C must have the POINTER attribute");
1542 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1544 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1546 gfc_error ("Array component of structure at %C must have explicit "
1547 "or deferred shape");
1552 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1556 if (c->ts.type == BT_CHARACTER)
1558 c->attr = current_attr;
1560 c->initializer = *init;
1567 c->attr.codimension = 1;
1569 c->attr.dimension = 1;
1573 /* Should this ever get more complicated, combine with similar section
1574 in add_init_expr_to_sym into a separate function. */
1575 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1577 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1581 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1582 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1583 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1585 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1587 if (c->initializer->expr_type == EXPR_CONSTANT)
1588 gfc_set_constant_character_len (len, c->initializer, -1);
1589 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1590 c->initializer->ts.u.cl->length->value.integer))
1592 gfc_constructor *ctor;
1593 ctor = gfc_constructor_first (c->initializer->value.constructor);
1598 bool has_ts = (c->initializer->ts.u.cl
1599 && c->initializer->ts.u.cl->length_from_typespec);
1601 /* Remember the length of the first element for checking
1602 that all elements *in the constructor* have the same
1603 length. This need not be the length of the LHS! */
1604 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1605 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1606 first_len = ctor->expr->value.character.length;
1608 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1609 if (ctor->expr->expr_type == EXPR_CONSTANT)
1611 gfc_set_constant_character_len (len, ctor->expr,
1612 has_ts ? -1 : first_len);
1613 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1619 /* Check array components. */
1620 if (!c->attr.dimension)
1623 if (c->attr.pointer)
1625 if (c->as->type != AS_DEFERRED)
1627 gfc_error ("Pointer array component of structure at %C must have a "
1632 else if (c->attr.allocatable)
1634 if (c->as->type != AS_DEFERRED)
1636 gfc_error ("Allocatable component of structure at %C must have a "
1643 if (c->as->type != AS_EXPLICIT)
1645 gfc_error ("Array component of structure at %C must have an "
1652 if (c->ts.type == BT_CLASS)
1654 bool delayed = (gfc_state_stack->sym == c->ts.u.derived)
1655 || (!c->ts.u.derived->components
1656 && !c->ts.u.derived->attr.zero_comp);
1657 gfc_try t2 = gfc_build_class_symbol (&c->ts, &c->attr, &c->as, delayed);
1667 /* Match a 'NULL()', and possibly take care of some side effects. */
1670 gfc_match_null (gfc_expr **result)
1673 match m, m2 = MATCH_NO;
1675 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
1681 char name[GFC_MAX_SYMBOL_LEN + 1];
1683 if ((m2 = gfc_match (" null (")) != MATCH_YES)
1686 old_loc = gfc_current_locus;
1687 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
1690 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
1694 gfc_current_locus = old_loc;
1699 /* The NULL symbol now has to be/become an intrinsic function. */
1700 if (gfc_get_symbol ("null", NULL, &sym))
1702 gfc_error ("NULL() initialization at %C is ambiguous");
1706 gfc_intrinsic_symbol (sym);
1708 if (sym->attr.proc != PROC_INTRINSIC
1709 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1710 sym->name, NULL) == FAILURE
1711 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1714 *result = gfc_get_null_expr (&gfc_current_locus);
1716 /* Invalid per F2008, C512. */
1717 if (m2 == MATCH_YES)
1719 gfc_error ("NULL() initialization at %C may not have MOLD");
1727 /* Match the initialization expr for a data pointer or procedure pointer. */
1730 match_pointer_init (gfc_expr **init, int procptr)
1734 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1736 gfc_error ("Initialization of pointer at %C is not allowed in "
1737 "a PURE procedure");
1741 /* Match NULL() initialization. */
1742 m = gfc_match_null (init);
1746 /* Match non-NULL initialization. */
1747 gfc_matching_ptr_assignment = !procptr;
1748 gfc_matching_procptr_assignment = procptr;
1749 m = gfc_match_rvalue (init);
1750 gfc_matching_ptr_assignment = 0;
1751 gfc_matching_procptr_assignment = 0;
1752 if (m == MATCH_ERROR)
1754 else if (m == MATCH_NO)
1756 gfc_error ("Error in pointer initialization at %C");
1761 gfc_resolve_expr (*init);
1763 if (gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
1764 "initialization at %C") == FAILURE)
1772 check_function_name (char *name)
1774 /* In functions that have a RESULT variable defined, the function name always
1775 refers to function calls. Therefore, the name is not allowed to appear in
1776 specification statements. When checking this, be careful about
1777 'hidden' procedure pointer results ('ppr@'). */
1779 if (gfc_current_state () == COMP_FUNCTION)
1781 gfc_symbol *block = gfc_current_block ();
1782 if (block && block->result && block->result != block
1783 && strcmp (block->result->name, "ppr@") != 0
1784 && strcmp (block->name, name) == 0)
1786 gfc_error ("Function name '%s' not allowed at %C", name);
1795 /* Match a variable name with an optional initializer. When this
1796 subroutine is called, a variable is expected to be parsed next.
1797 Depending on what is happening at the moment, updates either the
1798 symbol table or the current interface. */
1801 variable_decl (int elem)
1803 char name[GFC_MAX_SYMBOL_LEN + 1];
1804 gfc_expr *initializer, *char_len;
1806 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1818 /* When we get here, we've just matched a list of attributes and
1819 maybe a type and a double colon. The next thing we expect to see
1820 is the name of the symbol. */
1821 m = gfc_match_name (name);
1825 var_locus = gfc_current_locus;
1827 /* Now we could see the optional array spec. or character length. */
1828 m = gfc_match_array_spec (&as, true, true);
1829 if (m == MATCH_ERROR)
1833 as = gfc_copy_array_spec (current_as);
1835 && merge_array_spec (current_as, as, true) == FAILURE)
1841 if (gfc_option.flag_cray_pointer)
1842 cp_as = gfc_copy_array_spec (as);
1844 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1845 determine (and check) whether it can be implied-shape. If it
1846 was parsed as assumed-size, change it because PARAMETERs can not
1850 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1853 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1858 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1859 && current_attr.flavor == FL_PARAMETER)
1860 as->type = AS_IMPLIED_SHAPE;
1862 if (as->type == AS_IMPLIED_SHAPE
1863 && gfc_notify_std (GFC_STD_F2008,
1864 "Implied-shape array at %L",
1865 &var_locus) == FAILURE)
1874 cl_deferred = false;
1876 if (current_ts.type == BT_CHARACTER)
1878 switch (match_char_length (&char_len, &cl_deferred, false))
1881 cl = gfc_new_charlen (gfc_current_ns, NULL);
1883 cl->length = char_len;
1886 /* Non-constant lengths need to be copied after the first
1887 element. Also copy assumed lengths. */
1890 && (current_ts.u.cl->length == NULL
1891 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1893 cl = gfc_new_charlen (gfc_current_ns, NULL);
1894 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1897 cl = current_ts.u.cl;
1899 cl_deferred = current_ts.deferred;
1908 /* If this symbol has already shown up in a Cray Pointer declaration,
1909 then we want to set the type & bail out. */
1910 if (gfc_option.flag_cray_pointer)
1912 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1913 if (sym != NULL && sym->attr.cray_pointee)
1915 sym->ts.type = current_ts.type;
1916 sym->ts.kind = current_ts.kind;
1918 sym->ts.u.derived = current_ts.u.derived;
1919 sym->ts.is_c_interop = current_ts.is_c_interop;
1920 sym->ts.is_iso_c = current_ts.is_iso_c;
1923 /* Check to see if we have an array specification. */
1926 if (sym->as != NULL)
1928 gfc_error ("Duplicate array spec for Cray pointee at %C");
1929 gfc_free_array_spec (cp_as);
1935 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1936 gfc_internal_error ("Couldn't set pointee array spec.");
1938 /* Fix the array spec. */
1939 m = gfc_mod_pointee_as (sym->as);
1940 if (m == MATCH_ERROR)
1948 gfc_free_array_spec (cp_as);
1952 /* Procedure pointer as function result. */
1953 if (gfc_current_state () == COMP_FUNCTION
1954 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1955 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1956 strcpy (name, "ppr@");
1958 if (gfc_current_state () == COMP_FUNCTION
1959 && strcmp (name, gfc_current_block ()->name) == 0
1960 && gfc_current_block ()->result
1961 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1962 strcpy (name, "ppr@");
1964 /* OK, we've successfully matched the declaration. Now put the
1965 symbol in the current namespace, because it might be used in the
1966 optional initialization expression for this symbol, e.g. this is
1969 integer, parameter :: i = huge(i)
1971 This is only true for parameters or variables of a basic type.
1972 For components of derived types, it is not true, so we don't
1973 create a symbol for those yet. If we fail to create the symbol,
1975 if (gfc_current_state () != COMP_DERIVED
1976 && build_sym (name, cl, cl_deferred, &as, &var_locus) == FAILURE)
1982 if (check_function_name (name) == FAILURE)
1988 /* We allow old-style initializations of the form
1989 integer i /2/, j(4) /3*3, 1/
1990 (if no colon has been seen). These are different from data
1991 statements in that initializers are only allowed to apply to the
1992 variable immediately preceding, i.e.
1994 is not allowed. Therefore we have to do some work manually, that
1995 could otherwise be left to the matchers for DATA statements. */
1997 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1999 if (gfc_notify_std (GFC_STD_GNU, "Old-style "
2000 "initialization at %C") == FAILURE)
2003 return match_old_style_init (name);
2006 /* The double colon must be present in order to have initializers.
2007 Otherwise the statement is ambiguous with an assignment statement. */
2010 if (gfc_match (" =>") == MATCH_YES)
2012 if (!current_attr.pointer)
2014 gfc_error ("Initialization at %C isn't for a pointer variable");
2019 m = match_pointer_init (&initializer, 0);
2023 else if (gfc_match_char ('=') == MATCH_YES)
2025 if (current_attr.pointer)
2027 gfc_error ("Pointer initialization at %C requires '=>', "
2033 m = gfc_match_init_expr (&initializer);
2036 gfc_error ("Expected an initialization expression at %C");
2040 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2041 && gfc_state_stack->state != COMP_DERIVED)
2043 gfc_error ("Initialization of variable at %C is not allowed in "
2044 "a PURE procedure");
2053 if (initializer != NULL && current_attr.allocatable
2054 && gfc_current_state () == COMP_DERIVED)
2056 gfc_error ("Initialization of allocatable component at %C is not "
2062 /* Add the initializer. Note that it is fine if initializer is
2063 NULL here, because we sometimes also need to check if a
2064 declaration *must* have an initialization expression. */
2065 if (gfc_current_state () != COMP_DERIVED)
2066 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2069 if (current_ts.type == BT_DERIVED
2070 && !current_attr.pointer && !initializer)
2071 initializer = gfc_default_initializer (¤t_ts);
2072 t = build_struct (name, cl, &initializer, &as);
2075 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
2078 /* Free stuff up and return. */
2079 gfc_free_expr (initializer);
2080 gfc_free_array_spec (as);
2086 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2087 This assumes that the byte size is equal to the kind number for
2088 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2091 gfc_match_old_kind_spec (gfc_typespec *ts)
2096 if (gfc_match_char ('*') != MATCH_YES)
2099 m = gfc_match_small_literal_int (&ts->kind, NULL);
2103 original_kind = ts->kind;
2105 /* Massage the kind numbers for complex types. */
2106 if (ts->type == BT_COMPLEX)
2110 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2111 gfc_basic_typename (ts->type), original_kind);
2118 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2121 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2125 if (gfc_option.flag_real4_kind == 8)
2127 if (gfc_option.flag_real4_kind == 10)
2129 if (gfc_option.flag_real4_kind == 16)
2135 if (gfc_option.flag_real8_kind == 4)
2137 if (gfc_option.flag_real8_kind == 10)
2139 if (gfc_option.flag_real8_kind == 16)
2144 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2146 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2147 gfc_basic_typename (ts->type), original_kind);
2151 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2152 gfc_basic_typename (ts->type), original_kind) == FAILURE)
2159 /* Match a kind specification. Since kinds are generally optional, we
2160 usually return MATCH_NO if something goes wrong. If a "kind="
2161 string is found, then we know we have an error. */
2164 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2176 where = loc = gfc_current_locus;
2181 if (gfc_match_char ('(') == MATCH_NO)
2184 /* Also gobbles optional text. */
2185 if (gfc_match (" kind = ") == MATCH_YES)
2188 loc = gfc_current_locus;
2191 n = gfc_match_init_expr (&e);
2195 if (gfc_matching_function)
2197 /* The function kind expression might include use associated or
2198 imported parameters and try again after the specification
2200 if (gfc_match_char (')') != MATCH_YES)
2202 gfc_error ("Missing right parenthesis at %C");
2208 gfc_undo_symbols ();
2213 /* ....or else, the match is real. */
2215 gfc_error ("Expected initialization expression at %C");
2223 gfc_error ("Expected scalar initialization expression at %C");
2228 msg = gfc_extract_int (e, &ts->kind);
2237 /* Before throwing away the expression, let's see if we had a
2238 C interoperable kind (and store the fact). */
2239 if (e->ts.is_c_interop == 1)
2241 /* Mark this as C interoperable if being declared with one
2242 of the named constants from iso_c_binding. */
2243 ts->is_c_interop = e->ts.is_iso_c;
2244 ts->f90_type = e->ts.f90_type;
2250 /* Ignore errors to this point, if we've gotten here. This means
2251 we ignore the m=MATCH_ERROR from above. */
2252 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2254 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2255 gfc_basic_typename (ts->type));
2256 gfc_current_locus = where;
2260 /* Warn if, e.g., c_int is used for a REAL variable, but not
2261 if, e.g., c_double is used for COMPLEX as the standard
2262 explicitly says that the kind type parameter for complex and real
2263 variable is the same, i.e. c_float == c_float_complex. */
2264 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2265 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2266 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2267 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2268 "is %s", gfc_basic_typename (ts->f90_type), &where,
2269 gfc_basic_typename (ts->type));
2271 gfc_gobble_whitespace ();
2272 if ((c = gfc_next_ascii_char ()) != ')'
2273 && (ts->type != BT_CHARACTER || c != ','))
2275 if (ts->type == BT_CHARACTER)
2276 gfc_error ("Missing right parenthesis or comma at %C");
2278 gfc_error ("Missing right parenthesis at %C");
2282 /* All tests passed. */
2285 if(m == MATCH_ERROR)
2286 gfc_current_locus = where;
2288 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2291 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2295 if (gfc_option.flag_real4_kind == 8)
2297 if (gfc_option.flag_real4_kind == 10)
2299 if (gfc_option.flag_real4_kind == 16)
2305 if (gfc_option.flag_real8_kind == 4)
2307 if (gfc_option.flag_real8_kind == 10)
2309 if (gfc_option.flag_real8_kind == 16)
2314 /* Return what we know from the test(s). */
2319 gfc_current_locus = where;
2325 match_char_kind (int * kind, int * is_iso_c)
2334 where = gfc_current_locus;
2336 n = gfc_match_init_expr (&e);
2338 if (n != MATCH_YES && gfc_matching_function)
2340 /* The expression might include use-associated or imported
2341 parameters and try again after the specification
2344 gfc_undo_symbols ();
2349 gfc_error ("Expected initialization expression at %C");
2355 gfc_error ("Expected scalar initialization expression at %C");
2360 msg = gfc_extract_int (e, kind);
2361 *is_iso_c = e->ts.is_iso_c;
2371 /* Ignore errors to this point, if we've gotten here. This means
2372 we ignore the m=MATCH_ERROR from above. */
2373 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2375 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2379 /* All tests passed. */
2382 if (m == MATCH_ERROR)
2383 gfc_current_locus = where;
2385 /* Return what we know from the test(s). */
2390 gfc_current_locus = where;
2395 /* Match the various kind/length specifications in a CHARACTER
2396 declaration. We don't return MATCH_NO. */
2399 gfc_match_char_spec (gfc_typespec *ts)
2401 int kind, seen_length, is_iso_c;
2413 /* Try the old-style specification first. */
2414 old_char_selector = 0;
2416 m = match_char_length (&len, &deferred, true);
2420 old_char_selector = 1;
2425 m = gfc_match_char ('(');
2428 m = MATCH_YES; /* Character without length is a single char. */
2432 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2433 if (gfc_match (" kind =") == MATCH_YES)
2435 m = match_char_kind (&kind, &is_iso_c);
2437 if (m == MATCH_ERROR)
2442 if (gfc_match (" , len =") == MATCH_NO)
2445 m = char_len_param_value (&len, &deferred);
2448 if (m == MATCH_ERROR)
2455 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2456 if (gfc_match (" len =") == MATCH_YES)
2458 m = char_len_param_value (&len, &deferred);
2461 if (m == MATCH_ERROR)
2465 if (gfc_match_char (')') == MATCH_YES)
2468 if (gfc_match (" , kind =") != MATCH_YES)
2471 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2477 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2478 m = char_len_param_value (&len, &deferred);
2481 if (m == MATCH_ERROR)
2485 m = gfc_match_char (')');
2489 if (gfc_match_char (',') != MATCH_YES)
2492 gfc_match (" kind ="); /* Gobble optional text. */
2494 m = match_char_kind (&kind, &is_iso_c);
2495 if (m == MATCH_ERROR)
2501 /* Require a right-paren at this point. */
2502 m = gfc_match_char (')');
2507 gfc_error ("Syntax error in CHARACTER declaration at %C");
2509 gfc_free_expr (len);
2513 /* Deal with character functions after USE and IMPORT statements. */
2514 if (gfc_matching_function)
2516 gfc_free_expr (len);
2517 gfc_undo_symbols ();
2523 gfc_free_expr (len);
2527 /* Do some final massaging of the length values. */
2528 cl = gfc_new_charlen (gfc_current_ns, NULL);
2530 if (seen_length == 0)
2531 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2536 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2537 ts->deferred = deferred;
2539 /* We have to know if it was a C interoperable kind so we can
2540 do accurate type checking of bind(c) procs, etc. */
2542 /* Mark this as C interoperable if being declared with one
2543 of the named constants from iso_c_binding. */
2544 ts->is_c_interop = is_iso_c;
2545 else if (len != NULL)
2546 /* Here, we might have parsed something such as: character(c_char)
2547 In this case, the parsing code above grabs the c_char when
2548 looking for the length (line 1690, roughly). it's the last
2549 testcase for parsing the kind params of a character variable.
2550 However, it's not actually the length. this seems like it
2552 To see if the user used a C interop kind, test the expr
2553 of the so called length, and see if it's C interoperable. */
2554 ts->is_c_interop = len->ts.is_iso_c;
2560 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2561 structure to the matched specification. This is necessary for FUNCTION and
2562 IMPLICIT statements.
2564 If implicit_flag is nonzero, then we don't check for the optional
2565 kind specification. Not doing so is needed for matching an IMPLICIT
2566 statement correctly. */
2569 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2571 char name[GFC_MAX_SYMBOL_LEN + 1];
2572 gfc_symbol *sym, *dt_sym;
2575 bool seen_deferred_kind, matched_type;
2576 const char *dt_name;
2578 /* A belt and braces check that the typespec is correctly being treated
2579 as a deferred characteristic association. */
2580 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2581 && (gfc_current_block ()->result->ts.kind == -1)
2582 && (ts->kind == -1);
2584 if (seen_deferred_kind)
2587 /* Clear the current binding label, in case one is given. */
2588 curr_binding_label = NULL;
2590 if (gfc_match (" byte") == MATCH_YES)
2592 if (gfc_notify_std (GFC_STD_GNU, "BYTE type at %C")
2596 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2598 gfc_error ("BYTE type used at %C "
2599 "is not available on the target machine");
2603 ts->type = BT_INTEGER;
2609 m = gfc_match (" type (");
2610 matched_type = (m == MATCH_YES);
2613 gfc_gobble_whitespace ();
2614 if (gfc_peek_ascii_char () == '*')
2616 if ((m = gfc_match ("*)")) != MATCH_YES)
2618 if (gfc_current_state () == COMP_DERIVED)
2620 gfc_error ("Assumed type at %C is not allowed for components");
2623 if (gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
2624 "at %C") == FAILURE)
2626 ts->type = BT_ASSUMED;
2630 m = gfc_match ("%n", name);
2631 matched_type = (m == MATCH_YES);
2634 if ((matched_type && strcmp ("integer", name) == 0)
2635 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2637 ts->type = BT_INTEGER;
2638 ts->kind = gfc_default_integer_kind;
2642 if ((matched_type && strcmp ("character", name) == 0)
2643 || (!matched_type && gfc_match (" character") == MATCH_YES))
2646 && gfc_notify_std (GFC_STD_F2008, "TYPE with "
2647 "intrinsic-type-spec at %C") == FAILURE)
2650 ts->type = BT_CHARACTER;
2651 if (implicit_flag == 0)
2652 m = gfc_match_char_spec (ts);
2656 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2662 if ((matched_type && strcmp ("real", name) == 0)
2663 || (!matched_type && gfc_match (" real") == MATCH_YES))
2666 ts->kind = gfc_default_real_kind;
2671 && (strcmp ("doubleprecision", name) == 0
2672 || (strcmp ("double", name) == 0
2673 && gfc_match (" precision") == MATCH_YES)))
2674 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2677 && gfc_notify_std (GFC_STD_F2008, "TYPE with "
2678 "intrinsic-type-spec at %C") == FAILURE)
2680 if (matched_type && gfc_match_char (')') != MATCH_YES)
2684 ts->kind = gfc_default_double_kind;
2688 if ((matched_type && strcmp ("complex", name) == 0)
2689 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2691 ts->type = BT_COMPLEX;
2692 ts->kind = gfc_default_complex_kind;
2697 && (strcmp ("doublecomplex", name) == 0
2698 || (strcmp ("double", name) == 0
2699 && gfc_match (" complex") == MATCH_YES)))
2700 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2702 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C")
2707 && gfc_notify_std (GFC_STD_F2008, "TYPE with "
2708 "intrinsic-type-spec at %C") == FAILURE)
2711 if (matched_type && gfc_match_char (')') != MATCH_YES)
2714 ts->type = BT_COMPLEX;
2715 ts->kind = gfc_default_double_kind;
2719 if ((matched_type && strcmp ("logical", name) == 0)
2720 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2722 ts->type = BT_LOGICAL;
2723 ts->kind = gfc_default_logical_kind;
2728 m = gfc_match_char (')');
2731 ts->type = BT_DERIVED;
2734 /* Match CLASS declarations. */
2735 m = gfc_match (" class ( * )");
2736 if (m == MATCH_ERROR)
2738 else if (m == MATCH_YES)
2742 ts->type = BT_CLASS;
2743 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
2746 upe = gfc_new_symbol ("STAR", gfc_current_ns);
2747 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2749 gfc_set_sym_referenced (upe);
2751 upe->ts.type = BT_VOID;
2752 upe->attr.unlimited_polymorphic = 1;
2753 /* This is essential to force the construction of
2754 unlimited polymorphic component class containers. */
2755 upe->attr.zero_comp = 1;
2756 if (gfc_add_flavor (&upe->attr, FL_DERIVED,
2757 NULL, &gfc_current_locus) == FAILURE)
2762 st = gfc_find_symtree (gfc_current_ns->sym_root, "STAR");
2764 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2768 ts->u.derived = upe;
2772 m = gfc_match (" class ( %n )", name);
2775 ts->type = BT_CLASS;
2777 if (gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C")
2782 /* Defer association of the derived type until the end of the
2783 specification block. However, if the derived type can be
2784 found, add it to the typespec. */
2785 if (gfc_matching_function)
2787 ts->u.derived = NULL;
2788 if (gfc_current_state () != COMP_INTERFACE
2789 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2791 sym = gfc_find_dt_in_generic (sym);
2792 ts->u.derived = sym;
2797 /* Search for the name but allow the components to be defined later. If
2798 type = -1, this typespec has been seen in a function declaration but
2799 the type could not be accessed at that point. The actual derived type is
2800 stored in a symtree with the first letter of the name capitalized; the
2801 symtree with the all lower-case name contains the associated
2802 generic function. */
2803 dt_name = gfc_get_string ("%c%s",
2804 (char) TOUPPER ((unsigned char) name[0]),
2805 (const char*)&name[1]);
2810 gfc_get_ha_symbol (name, &sym);
2811 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2813 gfc_error ("Type name '%s' at %C is ambiguous", name);
2816 if (sym->generic && !dt_sym)
2817 dt_sym = gfc_find_dt_in_generic (sym);
2819 else if (ts->kind == -1)
2821 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2822 || gfc_current_ns->has_import_set;
2823 gfc_find_symbol (name, NULL, iface, &sym);
2824 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2826 gfc_error ("Type name '%s' at %C is ambiguous", name);
2829 if (sym && sym->generic && !dt_sym)
2830 dt_sym = gfc_find_dt_in_generic (sym);
2837 if ((sym->attr.flavor != FL_UNKNOWN
2838 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2839 || sym->attr.subroutine)
2841 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2842 "entity at %L, which has the same name", name,
2847 gfc_set_sym_referenced (sym);
2848 if (!sym->attr.generic
2849 && gfc_add_generic (&sym->attr, sym->name, NULL) == FAILURE)
2852 if (!sym->attr.function
2853 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2858 gfc_interface *intr, *head;
2860 /* Use upper case to save the actual derived-type symbol. */
2861 gfc_get_symbol (dt_name, NULL, &dt_sym);
2862 dt_sym->name = gfc_get_string (sym->name);
2863 head = sym->generic;
2864 intr = gfc_get_interface ();
2866 intr->where = gfc_current_locus;
2868 sym->generic = intr;
2869 sym->attr.if_source = IFSRC_DECL;
2872 gfc_set_sym_referenced (dt_sym);
2874 if (dt_sym->attr.flavor != FL_DERIVED
2875 && gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL)
2879 ts->u.derived = dt_sym;
2885 && gfc_notify_std (GFC_STD_F2008, "TYPE with "
2886 "intrinsic-type-spec at %C") == FAILURE)
2889 /* For all types except double, derived and character, look for an
2890 optional kind specifier. MATCH_NO is actually OK at this point. */
2891 if (implicit_flag == 1)
2893 if (matched_type && gfc_match_char (')') != MATCH_YES)
2899 if (gfc_current_form == FORM_FREE)
2901 c = gfc_peek_ascii_char ();
2902 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2903 && c != ':' && c != ',')
2905 if (matched_type && c == ')')
2907 gfc_next_ascii_char ();
2914 m = gfc_match_kind_spec (ts, false);
2915 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2916 m = gfc_match_old_kind_spec (ts);
2918 if (matched_type && gfc_match_char (')') != MATCH_YES)
2921 /* Defer association of the KIND expression of function results
2922 until after USE and IMPORT statements. */
2923 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2924 || gfc_matching_function)
2928 m = MATCH_YES; /* No kind specifier found. */
2934 /* Match an IMPLICIT NONE statement. Actually, this statement is
2935 already matched in parse.c, or we would not end up here in the
2936 first place. So the only thing we need to check, is if there is
2937 trailing garbage. If not, the match is successful. */
2940 gfc_match_implicit_none (void)
2942 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2946 /* Match the letter range(s) of an IMPLICIT statement. */
2949 match_implicit_range (void)
2955 cur_loc = gfc_current_locus;
2957 gfc_gobble_whitespace ();
2958 c = gfc_next_ascii_char ();
2961 gfc_error ("Missing character range in IMPLICIT at %C");
2968 gfc_gobble_whitespace ();
2969 c1 = gfc_next_ascii_char ();
2973 gfc_gobble_whitespace ();
2974 c = gfc_next_ascii_char ();
2979 inner = 0; /* Fall through. */
2986 gfc_gobble_whitespace ();
2987 c2 = gfc_next_ascii_char ();
2991 gfc_gobble_whitespace ();
2992 c = gfc_next_ascii_char ();
2994 if ((c != ',') && (c != ')'))
3007 gfc_error ("Letters must be in alphabetic order in "
3008 "IMPLICIT statement at %C");
3012 /* See if we can add the newly matched range to the pending
3013 implicits from this IMPLICIT statement. We do not check for
3014 conflicts with whatever earlier IMPLICIT statements may have
3015 set. This is done when we've successfully finished matching
3017 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
3024 gfc_syntax_error (ST_IMPLICIT);
3026 gfc_current_locus = cur_loc;
3031 /* Match an IMPLICIT statement, storing the types for
3032 gfc_set_implicit() if the statement is accepted by the parser.
3033 There is a strange looking, but legal syntactic construction
3034 possible. It looks like:
3036 IMPLICIT INTEGER (a-b) (c-d)
3038 This is legal if "a-b" is a constant expression that happens to
3039 equal one of the legal kinds for integers. The real problem
3040 happens with an implicit specification that looks like:
3042 IMPLICIT INTEGER (a-b)
3044 In this case, a typespec matcher that is "greedy" (as most of the
3045 matchers are) gobbles the character range as a kindspec, leaving
3046 nothing left. We therefore have to go a bit more slowly in the
3047 matching process by inhibiting the kindspec checking during
3048 typespec matching and checking for a kind later. */
3051 gfc_match_implicit (void)
3060 /* We don't allow empty implicit statements. */
3061 if (gfc_match_eos () == MATCH_YES)
3063 gfc_error ("Empty IMPLICIT statement at %C");
3069 /* First cleanup. */
3070 gfc_clear_new_implicit ();
3072 /* A basic type is mandatory here. */
3073 m = gfc_match_decl_type_spec (&ts, 1);
3074 if (m == MATCH_ERROR)
3079 cur_loc = gfc_current_locus;
3080 m = match_implicit_range ();
3084 /* We may have <TYPE> (<RANGE>). */
3085 gfc_gobble_whitespace ();
3086 c = gfc_next_ascii_char ();
3087 if ((c == '\n') || (c == ','))
3089 /* Check for CHARACTER with no length parameter. */
3090 if (ts.type == BT_CHARACTER && !ts.u.cl)
3092 ts.kind = gfc_default_character_kind;
3093 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3094 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3098 /* Record the Successful match. */
3099 if (gfc_merge_new_implicit (&ts) != SUCCESS)
3104 gfc_current_locus = cur_loc;
3107 /* Discard the (incorrectly) matched range. */
3108 gfc_clear_new_implicit ();
3110 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3111 if (ts.type == BT_CHARACTER)
3112 m = gfc_match_char_spec (&ts);
3115 m = gfc_match_kind_spec (&ts, false);
3118 m = gfc_match_old_kind_spec (&ts);
3119 if (m == MATCH_ERROR)
3125 if (m == MATCH_ERROR)
3128 m = match_implicit_range ();
3129 if (m == MATCH_ERROR)
3134 gfc_gobble_whitespace ();
3135 c = gfc_next_ascii_char ();
3136 if ((c != '\n') && (c != ','))
3139 if (gfc_merge_new_implicit (&ts) != SUCCESS)
3147 gfc_syntax_error (ST_IMPLICIT);
3155 gfc_match_import (void)
3157 char name[GFC_MAX_SYMBOL_LEN + 1];
3162 if (gfc_current_ns->proc_name == NULL
3163 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3165 gfc_error ("IMPORT statement at %C only permitted in "
3166 "an INTERFACE body");
3170 if (gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C")
3174 if (gfc_match_eos () == MATCH_YES)
3176 /* All host variables should be imported. */
3177 gfc_current_ns->has_import_set = 1;
3181 if (gfc_match (" ::") == MATCH_YES)
3183 if (gfc_match_eos () == MATCH_YES)
3185 gfc_error ("Expecting list of named entities at %C");
3193 m = gfc_match (" %n", name);
3197 if (gfc_current_ns->parent != NULL
3198 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3200 gfc_error ("Type name '%s' at %C is ambiguous", name);
3203 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3204 && gfc_find_symbol (name,
3205 gfc_current_ns->proc_name->ns->parent,
3208 gfc_error ("Type name '%s' at %C is ambiguous", name);
3214 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3215 "at %C - does not exist.", name);
3219 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3221 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3226 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3229 sym->attr.imported = 1;
3231 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3233 /* The actual derived type is stored in a symtree with the first
3234 letter of the name capitalized; the symtree with the all
3235 lower-case name contains the associated generic function. */
3236 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3237 gfc_get_string ("%c%s",
3238 (char) TOUPPER ((unsigned char) name[0]),
3242 sym->attr.imported = 1;
3255 if (gfc_match_eos () == MATCH_YES)
3257 if (gfc_match_char (',') != MATCH_YES)
3264 gfc_error ("Syntax error in IMPORT statement at %C");
3269 /* A minimal implementation of gfc_match without whitespace, escape
3270 characters or variable arguments. Returns true if the next
3271 characters match the TARGET template exactly. */
3274 match_string_p (const char *target)
3278 for (p = target; *p; p++)
3279 if ((char) gfc_next_ascii_char () != *p)
3284 /* Matches an attribute specification including array specs. If
3285 successful, leaves the variables current_attr and current_as
3286 holding the specification. Also sets the colon_seen variable for
3287 later use by matchers associated with initializations.
3289 This subroutine is a little tricky in the sense that we don't know
3290 if we really have an attr-spec until we hit the double colon.
3291 Until that time, we can only return MATCH_NO. This forces us to
3292 check for duplicate specification at this level. */
3295 match_attr_spec (void)
3297 /* Modifiers that can exist in a type statement. */
3299 { GFC_DECL_BEGIN = 0,
3300 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3301 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3302 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3303 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3304 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3305 DECL_NONE, GFC_DECL_END /* Sentinel */
3308 /* GFC_DECL_END is the sentinel, index starts at 0. */
3309 #define NUM_DECL GFC_DECL_END
3311 locus start, seen_at[NUM_DECL];
3318 gfc_clear_attr (¤t_attr);
3319 start = gfc_current_locus;
3324 /* See if we get all of the keywords up to the final double colon. */
3325 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3333 gfc_gobble_whitespace ();
3335 ch = gfc_next_ascii_char ();
3338 /* This is the successful exit condition for the loop. */
3339 if (gfc_next_ascii_char () == ':')
3344 gfc_gobble_whitespace ();
3345 switch (gfc_peek_ascii_char ())
3348 gfc_next_ascii_char ();
3349 switch (gfc_next_ascii_char ())
3352 if (match_string_p ("locatable"))
3354 /* Matched "allocatable". */
3355 d = DECL_ALLOCATABLE;
3360 if (match_string_p ("ynchronous"))
3362 /* Matched "asynchronous". */
3363 d = DECL_ASYNCHRONOUS;
3370 /* Try and match the bind(c). */
3371 m = gfc_match_bind_c (NULL, true);
3374 else if (m == MATCH_ERROR)
3379 gfc_next_ascii_char ();
3380 if ('o' != gfc_next_ascii_char ())
3382 switch (gfc_next_ascii_char ())
3385 if (match_string_p ("imension"))
3387 d = DECL_CODIMENSION;
3391 if (match_string_p ("tiguous"))
3393 d = DECL_CONTIGUOUS;
3400 if (match_string_p ("dimension"))
3405 if (match_string_p ("external"))
3410 if (match_string_p ("int"))
3412 ch = gfc_next_ascii_char ();
3415 if (match_string_p ("nt"))
3417 /* Matched "intent". */
3418 /* TODO: Call match_intent_spec from here. */
3419 if (gfc_match (" ( in out )") == MATCH_YES)
3421 else if (gfc_match (" ( in )") == MATCH_YES)
3423 else if (gfc_match (" ( out )") == MATCH_YES)
3429 if (match_string_p ("insic"))
3431 /* Matched "intrinsic". */
3439 if (match_string_p ("optional"))
3444 gfc_next_ascii_char ();
3445 switch (gfc_next_ascii_char ())
3448 if (match_string_p ("rameter"))
3450 /* Matched "parameter". */
3456 if (match_string_p ("inter"))
3458 /* Matched "pointer". */
3464 ch = gfc_next_ascii_char ();
3467 if (match_string_p ("vate"))
3469 /* Matched "private". */
3475 if (match_string_p ("tected"))
3477 /* Matched "protected". */
3484 if (match_string_p ("blic"))
3486 /* Matched "public". */
3494 if (match_string_p ("save"))
3499 if (match_string_p ("target"))
3504 gfc_next_ascii_char ();
3505 ch = gfc_next_ascii_char ();
3508 if (match_string_p ("lue"))
3510 /* Matched "value". */
3516 if (match_string_p ("latile"))
3518 /* Matched "volatile". */
3526 /* No double colon and no recognizable decl_type, so assume that
3527 we've been looking at something else the whole time. */
3534 /* Check to make sure any parens are paired up correctly. */
3535 if (gfc_match_parens () == MATCH_ERROR)
3542 seen_at[d] = gfc_current_locus;
3544 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3546 gfc_array_spec *as = NULL;
3548 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3549 d == DECL_CODIMENSION);
3551 if (current_as == NULL)
3553 else if (m == MATCH_YES)
3555 if (merge_array_spec (as, current_as, false) == FAILURE)
3562 if (d == DECL_CODIMENSION)
3563 gfc_error ("Missing codimension specification at %C");
3565 gfc_error ("Missing dimension specification at %C");
3569 if (m == MATCH_ERROR)
3574 /* Since we've seen a double colon, we have to be looking at an
3575 attr-spec. This means that we can now issue errors. */
3576 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3581 case DECL_ALLOCATABLE:
3582 attr = "ALLOCATABLE";
3584 case DECL_ASYNCHRONOUS:
3585 attr = "ASYNCHRONOUS";
3587 case DECL_CODIMENSION:
3588 attr = "CODIMENSION";
3590 case DECL_CONTIGUOUS:
3591 attr = "CONTIGUOUS";
3593 case DECL_DIMENSION:
3600 attr = "INTENT (IN)";
3603 attr = "INTENT (OUT)";
3606 attr = "INTENT (IN OUT)";
3608 case DECL_INTRINSIC:
3614 case DECL_PARAMETER:
3620 case DECL_PROTECTED:
3635 case DECL_IS_BIND_C:
3645 attr = NULL; /* This shouldn't happen. */
3648 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3653 /* Now that we've dealt with duplicate attributes, add the attributes
3654 to the current attribute. */
3655 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3660 if (gfc_current_state () == COMP_DERIVED
3661 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3662 && d != DECL_POINTER && d != DECL_PRIVATE
3663 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3665 if (d == DECL_ALLOCATABLE)
3667 if (gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
3668 "attribute at %C in a TYPE definition")
3677 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3684 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3685 && gfc_current_state () != COMP_MODULE)
3687 if (d == DECL_PRIVATE)
3691 if (gfc_current_state () == COMP_DERIVED
3692 && gfc_state_stack->previous
3693 && gfc_state_stack->previous->state == COMP_MODULE)
3695 if (gfc_notify_std (GFC_STD_F2003, "Attribute %s "
3696 "at %L in a TYPE definition", attr,
3706 gfc_error ("%s attribute at %L is not allowed outside of the "
3707 "specification part of a module", attr, &seen_at[d]);
3715 case DECL_ALLOCATABLE:
3716 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
3719 case DECL_ASYNCHRONOUS:
3720 if (gfc_notify_std (GFC_STD_F2003,
3721 "ASYNCHRONOUS attribute at %C")
3725 t = gfc_add_asynchronous (¤t_attr, NULL, &seen_at[d]);
3728 case DECL_CODIMENSION:
3729 t = gfc_add_codimension (¤t_attr, NULL, &seen_at[d]);
3732 case DECL_CONTIGUOUS:
3733 if (gfc_notify_std (GFC_STD_F2008,
3734 "CONTIGUOUS attribute at %C")
3738 t = gfc_add_contiguous (¤t_attr, NULL, &seen_at[d]);
3741 case DECL_DIMENSION:
3742 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
3746 t = gfc_add_external (¤t_attr, &seen_at[d]);
3750 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
3754 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
3758 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
3761 case DECL_INTRINSIC:
3762 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
3766 t = gfc_add_optional (¤t_attr, &seen_at[d]);
3769 case DECL_PARAMETER:
3770 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
3774 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
3777 case DECL_PROTECTED:
3778 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3780 gfc_error ("PROTECTED at %C only allowed in specification "
3781 "part of a module");
3786 if (gfc_notify_std (GFC_STD_F2003, "PROTECTED "
3791 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
3795 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
3800 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
3805 t = gfc_add_save (¤t_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3809 t = gfc_add_target (¤t_attr, &seen_at[d]);
3812 case DECL_IS_BIND_C:
3813 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
3817 if (gfc_notify_std (GFC_STD_F2003, "VALUE attribute "
3822 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
3826 if (gfc_notify_std (GFC_STD_F2003,
3827 "VOLATILE attribute at %C")
3831 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
3835 gfc_internal_error ("match_attr_spec(): Bad attribute");
3845 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3846 if (gfc_current_state () == COMP_MODULE && !current_attr.save
3847 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
3848 current_attr.save = SAVE_IMPLICIT;
3854 gfc_current_locus = start;
3855 gfc_free_array_spec (current_as);
3861 /* Set the binding label, dest_label, either with the binding label
3862 stored in the given gfc_typespec, ts, or if none was provided, it
3863 will be the symbol name in all lower case, as required by the draft
3864 (J3/04-007, section 15.4.1). If a binding label was given and
3865 there is more than one argument (num_idents), it is an error. */
3868 set_binding_label (const char **dest_label, const char *sym_name,
3871 if (num_idents > 1 && has_name_equals)
3873 gfc_error ("Multiple identifiers provided with "
3874 "single NAME= specifier at %C");
3878 if (curr_binding_label)
3879 /* Binding label given; store in temp holder till have sym. */
3880 *dest_label = curr_binding_label;
3883 /* No binding label given, and the NAME= specifier did not exist,
3884 which means there was no NAME="". */
3885 if (sym_name != NULL && has_name_equals == 0)
3886 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
3893 /* Set the status of the given common block as being BIND(C) or not,
3894 depending on the given parameter, is_bind_c. */
3897 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3899 com_block->is_bind_c = is_bind_c;
3904 /* Verify that the given gfc_typespec is for a C interoperable type. */
3907 gfc_verify_c_interop (gfc_typespec *ts)
3909 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3910 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3911 ? SUCCESS : FAILURE;
3912 else if (ts->type == BT_CLASS)
3914 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
3921 /* Verify that the variables of a given common block, which has been
3922 defined with the attribute specifier bind(c), to be of a C
3923 interoperable type. Errors will be reported here, if
3927 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3929 gfc_symbol *curr_sym = NULL;
3930 gfc_try retval = SUCCESS;
3932 curr_sym = com_block->head;
3934 /* Make sure we have at least one symbol. */
3935 if (curr_sym == NULL)
3938 /* Here we know we have a symbol, so we'll execute this loop
3942 /* The second to last param, 1, says this is in a common block. */
3943 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3944 curr_sym = curr_sym->common_next;
3945 } while (curr_sym != NULL);
3951 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3952 an appropriate error message is reported. */
3955 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3956 int is_in_common, gfc_common_head *com_block)
3958 bool bind_c_function = false;
3959 gfc_try retval = SUCCESS;
3961 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3962 bind_c_function = true;
3964 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3966 tmp_sym = tmp_sym->result;
3967 /* Make sure it wasn't an implicitly typed result. */
3968 if (tmp_sym->attr.implicit_type && gfc_option.warn_c_binding_type)
3970 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3971 "%L may not be C interoperable", tmp_sym->name,
3972 &tmp_sym->declared_at);
3973 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3974 /* Mark it as C interoperable to prevent duplicate warnings. */
3975 tmp_sym->ts.is_c_interop = 1;
3976 tmp_sym->attr.is_c_interop = 1;
3980 /* Here, we know we have the bind(c) attribute, so if we have
3981 enough type info, then verify that it's a C interop kind.
3982 The info could be in the symbol already, or possibly still in
3983 the given ts (current_ts), so look in both. */
3984 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3986 if (gfc_verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3988 /* See if we're dealing with a sym in a common block or not. */
3989 if (is_in_common == 1 && gfc_option.warn_c_binding_type)
3991 gfc_warning ("Variable '%s' in common block '%s' at %L "
3992 "may not be a C interoperable "
3993 "kind though common block '%s' is BIND(C)",
3994 tmp_sym->name, com_block->name,
3995 &(tmp_sym->declared_at), com_block->name);
3999 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
4000 gfc_error ("Type declaration '%s' at %L is not C "
4001 "interoperable but it is BIND(C)",
4002 tmp_sym->name, &(tmp_sym->declared_at));
4003 else if (gfc_option.warn_c_binding_type)
4004 gfc_warning ("Variable '%s' at %L "
4005 "may not be a C interoperable "
4006 "kind but it is bind(c)",
4007 tmp_sym->name, &(tmp_sym->declared_at));
4011 /* Variables declared w/in a common block can't be bind(c)
4012 since there's no way for C to see these variables, so there's
4013 semantically no reason for the attribute. */
4014 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
4016 gfc_error ("Variable '%s' in common block '%s' at "
4017 "%L cannot be declared with BIND(C) "
4018 "since it is not a global",
4019 tmp_sym->name, com_block->name,
4020 &(tmp_sym->declared_at));
4024 /* Scalar variables that are bind(c) can not have the pointer
4025 or allocatable attributes. */
4026 if (tmp_sym->attr.is_bind_c == 1)
4028 if (tmp_sym->attr.pointer == 1)
4030 gfc_error ("Variable '%s' at %L cannot have both the "
4031 "POINTER and BIND(C) attributes",
4032 tmp_sym->name, &(tmp_sym->declared_at));
4036 if (tmp_sym->attr.allocatable == 1)
4038 gfc_error ("Variable '%s' at %L cannot have both the "
4039 "ALLOCATABLE and BIND(C) attributes",
4040 tmp_sym->name, &(tmp_sym->declared_at));
4046 /* If it is a BIND(C) function, make sure the return value is a
4047 scalar value. The previous tests in this function made sure
4048 the type is interoperable. */
4049 if (bind_c_function && tmp_sym->as != NULL)
4050 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4051 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
4053 /* BIND(C) functions can not return a character string. */
4054 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
4055 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
4056 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
4057 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4058 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4059 "be a character string", tmp_sym->name,
4060 &(tmp_sym->declared_at));
4063 /* See if the symbol has been marked as private. If it has, make sure
4064 there is no binding label and warn the user if there is one. */
4065 if (tmp_sym->attr.access == ACCESS_PRIVATE
4066 && tmp_sym->binding_label)
4067 /* Use gfc_warning_now because we won't say that the symbol fails
4068 just because of this. */
4069 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
4070 "given the binding label '%s'", tmp_sym->name,
4071 &(tmp_sym->declared_at), tmp_sym->binding_label);
4077 /* Set the appropriate fields for a symbol that's been declared as
4078 BIND(C) (the is_bind_c flag and the binding label), and verify that
4079 the type is C interoperable. Errors are reported by the functions
4080 used to set/test these fields. */
4083 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4085 gfc_try retval = SUCCESS;
4087 /* TODO: Do we need to make sure the vars aren't marked private? */
4089 /* Set the is_bind_c bit in symbol_attribute. */
4090 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4092 if (set_binding_label (&tmp_sym->binding_label, tmp_sym->name,
4093 num_idents) != SUCCESS)
4100 /* Set the fields marking the given common block as BIND(C), including
4101 a binding label, and report any errors encountered. */
4104 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4106 gfc_try retval = SUCCESS;
4108 /* destLabel, common name, typespec (which may have binding label). */
4109 if (set_binding_label (&com_block->binding_label, com_block->name,
4114 /* Set the given common block (com_block) to being bind(c) (1). */
4115 set_com_block_bind_c (com_block, 1);
4121 /* Retrieve the list of one or more identifiers that the given bind(c)
4122 attribute applies to. */
4125 get_bind_c_idents (void)
4127 char name[GFC_MAX_SYMBOL_LEN + 1];
4129 gfc_symbol *tmp_sym = NULL;
4131 gfc_common_head *com_block = NULL;
4133 if (gfc_match_name (name) == MATCH_YES)
4135 found_id = MATCH_YES;
4136 gfc_get_ha_symbol (name, &tmp_sym);
4138 else if (match_common_name (name) == MATCH_YES)
4140 found_id = MATCH_YES;
4141 com_block = gfc_get_common (name, 0);
4145 gfc_error ("Need either entity or common block name for "
4146 "attribute specification statement at %C");
4150 /* Save the current identifier and look for more. */
4153 /* Increment the number of identifiers found for this spec stmt. */
4156 /* Make sure we have a sym or com block, and verify that it can
4157 be bind(c). Set the appropriate field(s) and look for more
4159 if (tmp_sym != NULL || com_block != NULL)
4161 if (tmp_sym != NULL)
4163 if (set_verify_bind_c_sym (tmp_sym, num_idents)
4169 if (set_verify_bind_c_com_block(com_block, num_idents)
4174 /* Look to see if we have another identifier. */
4176 if (gfc_match_eos () == MATCH_YES)
4177 found_id = MATCH_NO;
4178 else if (gfc_match_char (',') != MATCH_YES)
4179 found_id = MATCH_NO;
4180 else if (gfc_match_name (name) == MATCH_YES)
4182 found_id = MATCH_YES;
4183 gfc_get_ha_symbol (name, &tmp_sym);
4185 else if (match_common_name (name) == MATCH_YES)
4187 found_id = MATCH_YES;
4188 com_block = gfc_get_common (name, 0);
4192 gfc_error ("Missing entity or common block name for "
4193 "attribute specification statement at %C");
4199 gfc_internal_error ("Missing symbol");
4201 } while (found_id == MATCH_YES);
4203 /* if we get here we were successful */
4208 /* Try and match a BIND(C) attribute specification statement. */
4211 gfc_match_bind_c_stmt (void)
4213 match found_match = MATCH_NO;
4218 /* This may not be necessary. */
4220 /* Clear the temporary binding label holder. */
4221 curr_binding_label = NULL;
4223 /* Look for the bind(c). */
4224 found_match = gfc_match_bind_c (NULL, true);
4226 if (found_match == MATCH_YES)
4228 /* Look for the :: now, but it is not required. */
4231 /* Get the identifier(s) that needs to be updated. This may need to
4232 change to hand the flag(s) for the attr specified so all identifiers
4233 found can have all appropriate parts updated (assuming that the same
4234 spec stmt can have multiple attrs, such as both bind(c) and
4236 if (get_bind_c_idents () != SUCCESS)
4237 /* Error message should have printed already. */
4245 /* Match a data declaration statement. */
4248 gfc_match_data_decl (void)
4254 num_idents_on_line = 0;
4256 m = gfc_match_decl_type_spec (¤t_ts, 0);
4260 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4261 && gfc_current_state () != COMP_DERIVED)
4263 sym = gfc_use_derived (current_ts.u.derived);
4271 current_ts.u.derived = sym;
4274 m = match_attr_spec ();
4275 if (m == MATCH_ERROR)
4281 if (current_ts.type == BT_CLASS
4282 && current_ts.u.derived->attr.unlimited_polymorphic)
4285 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4286 && current_ts.u.derived->components == NULL
4287 && !current_ts.u.derived->attr.zero_comp)
4290 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4293 gfc_find_symbol (current_ts.u.derived->name,
4294 current_ts.u.derived->ns, 1, &sym);
4296 /* Any symbol that we find had better be a type definition
4297 which has its components defined. */
4298 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4299 && (current_ts.u.derived->components != NULL
4300 || current_ts.u.derived->attr.zero_comp))
4303 /* Now we have an error, which we signal, and then fix up
4304 because the knock-on is plain and simple confusing. */
4305 gfc_error_now ("Derived type at %C has not been previously defined "
4306 "and so cannot appear in a derived type definition");
4307 current_attr.pointer = 1;
4312 /* If we have an old-style character declaration, and no new-style
4313 attribute specifications, then there a comma is optional between
4314 the type specification and the variable list. */
4315 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4316 gfc_match_char (',');
4318 /* Give the types/attributes to symbols that follow. Give the element
4319 a number so that repeat character length expressions can be copied. */
4323 num_idents_on_line++;
4324 m = variable_decl (elem++);
4325 if (m == MATCH_ERROR)
4330 if (gfc_match_eos () == MATCH_YES)
4332 if (gfc_match_char (',') != MATCH_YES)
4336 if (gfc_error_flag_test () == 0)
4337 gfc_error ("Syntax error in data declaration at %C");
4340 gfc_free_data_all (gfc_current_ns);
4343 gfc_free_array_spec (current_as);
4349 /* Match a prefix associated with a function or subroutine
4350 declaration. If the typespec pointer is nonnull, then a typespec
4351 can be matched. Note that if nothing matches, MATCH_YES is
4352 returned (the null string was matched). */
4355 gfc_match_prefix (gfc_typespec *ts)
4361 gfc_clear_attr (¤t_attr);
4363 seen_impure = false;
4365 gcc_assert (!gfc_matching_prefix);
4366 gfc_matching_prefix = true;
4370 found_prefix = false;
4372 if (!seen_type && ts != NULL
4373 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4374 && gfc_match_space () == MATCH_YES)
4378 found_prefix = true;
4381 if (gfc_match ("elemental% ") == MATCH_YES)
4383 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
4386 found_prefix = true;
4389 if (gfc_match ("pure% ") == MATCH_YES)
4391 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
4394 found_prefix = true;
4397 if (gfc_match ("recursive% ") == MATCH_YES)
4399 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
4402 found_prefix = true;
4405 /* IMPURE is a somewhat special case, as it needs not set an actual
4406 attribute but rather only prevents ELEMENTAL routines from being
4407 automatically PURE. */
4408 if (gfc_match ("impure% ") == MATCH_YES)
4410 if (gfc_notify_std (GFC_STD_F2008,
4411 "IMPURE procedure at %C")
4416 found_prefix = true;
4419 while (found_prefix);
4421 /* IMPURE and PURE must not both appear, of course. */
4422 if (seen_impure && current_attr.pure)
4424 gfc_error ("PURE and IMPURE must not appear both at %C");
4428 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4429 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4431 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
4435 /* At this point, the next item is not a prefix. */
4436 gcc_assert (gfc_matching_prefix);
4437 gfc_matching_prefix = false;
4441 gcc_assert (gfc_matching_prefix);
4442 gfc_matching_prefix = false;
4447 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4450 copy_prefix (symbol_attribute *dest, locus *where)
4452 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
4455 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
4458 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
4465 /* Match a formal argument list. */
4468 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4470 gfc_formal_arglist *head, *tail, *p, *q;
4471 char name[GFC_MAX_SYMBOL_LEN + 1];
4477 if (gfc_match_char ('(') != MATCH_YES)
4484 if (gfc_match_char (')') == MATCH_YES)
4489 if (gfc_match_char ('*') == MATCH_YES)
4493 m = gfc_match_name (name);
4497 if (gfc_get_symbol (name, NULL, &sym))
4501 p = gfc_get_formal_arglist ();
4513 /* We don't add the VARIABLE flavor because the name could be a
4514 dummy procedure. We don't apply these attributes to formal
4515 arguments of statement functions. */
4516 if (sym != NULL && !st_flag
4517 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
4518 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
4524 /* The name of a program unit can be in a different namespace,
4525 so check for it explicitly. After the statement is accepted,
4526 the name is checked for especially in gfc_get_symbol(). */
4527 if (gfc_new_block != NULL && sym != NULL
4528 && strcmp (sym->name, gfc_new_block->name) == 0)
4530 gfc_error ("Name '%s' at %C is the name of the procedure",
4536 if (gfc_match_char (')') == MATCH_YES)
4539 m = gfc_match_char (',');
4542 gfc_error ("Unexpected junk in formal argument list at %C");
4548 /* Check for duplicate symbols in the formal argument list. */
4551 for (p = head; p->next; p = p->next)
4556 for (q = p->next; q; q = q->next)
4557 if (p->sym == q->sym)
4559 gfc_error ("Duplicate symbol '%s' in formal argument list "
4560 "at %C", p->sym->name);
4568 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
4578 gfc_free_formal_arglist (head);
4583 /* Match a RESULT specification following a function declaration or
4584 ENTRY statement. Also matches the end-of-statement. */
4587 match_result (gfc_symbol *function, gfc_symbol **result)
4589 char name[GFC_MAX_SYMBOL_LEN + 1];
4593 if (gfc_match (" result (") != MATCH_YES)
4596 m = gfc_match_name (name);
4600 /* Get the right paren, and that's it because there could be the
4601 bind(c) attribute after the result clause. */
4602 if (gfc_match_char(')') != MATCH_YES)
4604 /* TODO: should report the missing right paren here. */
4608 if (strcmp (function->name, name) == 0)
4610 gfc_error ("RESULT variable at %C must be different than function name");
4614 if (gfc_get_symbol (name, NULL, &r))
4617 if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4626 /* Match a function suffix, which could be a combination of a result
4627 clause and BIND(C), either one, or neither. The draft does not
4628 require them to come in a specific order. */
4631 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4633 match is_bind_c; /* Found bind(c). */
4634 match is_result; /* Found result clause. */
4635 match found_match; /* Status of whether we've found a good match. */
4636 char peek_char; /* Character we're going to peek at. */
4637 bool allow_binding_name;
4639 /* Initialize to having found nothing. */
4640 found_match = MATCH_NO;
4641 is_bind_c = MATCH_NO;
4642 is_result = MATCH_NO;
4644 /* Get the next char to narrow between result and bind(c). */
4645 gfc_gobble_whitespace ();
4646 peek_char = gfc_peek_ascii_char ();
4648 /* C binding names are not allowed for internal procedures. */
4649 if (gfc_current_state () == COMP_CONTAINS
4650 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4651 allow_binding_name = false;
4653 allow_binding_name = true;
4658 /* Look for result clause. */
4659 is_result = match_result (sym, result);
4660 if (is_result == MATCH_YES)
4662 /* Now see if there is a bind(c) after it. */
4663 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4664 /* We've found the result clause and possibly bind(c). */
4665 found_match = MATCH_YES;
4668 /* This should only be MATCH_ERROR. */
4669 found_match = is_result;
4672 /* Look for bind(c) first. */
4673 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4674 if (is_bind_c == MATCH_YES)
4676 /* Now see if a result clause followed it. */
4677 is_result = match_result (sym, result);
4678 found_match = MATCH_YES;
4682 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4683 found_match = MATCH_ERROR;
4687 gfc_error ("Unexpected junk after function declaration at %C");
4688 found_match = MATCH_ERROR;
4692 if (is_bind_c == MATCH_YES)
4694 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4695 if (gfc_current_state () == COMP_CONTAINS
4696 && sym->ns->proc_name->attr.flavor != FL_MODULE
4697 && gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
4698 "at %L may not be specified for an internal "
4699 "procedure", &gfc_current_locus)
4703 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4712 /* Procedure pointer return value without RESULT statement:
4713 Add "hidden" result variable named "ppr@". */
4716 add_hidden_procptr_result (gfc_symbol *sym)
4720 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4723 /* First usage case: PROCEDURE and EXTERNAL statements. */
4724 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4725 && strcmp (gfc_current_block ()->name, sym->name) == 0
4726 && sym->attr.external;
4727 /* Second usage case: INTERFACE statements. */
4728 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4729 && gfc_state_stack->previous->state == COMP_FUNCTION
4730 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4736 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4740 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4741 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4742 st2->n.sym = stree->n.sym;
4744 sym->result = stree->n.sym;
4746 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4747 sym->result->attr.pointer = sym->attr.pointer;
4748 sym->result->attr.external = sym->attr.external;
4749 sym->result->attr.referenced = sym->attr.referenced;
4750 sym->result->ts = sym->ts;
4751 sym->attr.proc_pointer = 0;
4752 sym->attr.pointer = 0;
4753 sym->attr.external = 0;
4754 if (sym->result->attr.external && sym->result->attr.pointer)
4756 sym->result->attr.pointer = 0;
4757 sym->result->attr.proc_pointer = 1;
4760 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4762 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4763 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4764 && sym->result && sym->result != sym && sym->result->attr.external
4765 && sym == gfc_current_ns->proc_name
4766 && sym == sym->result->ns->proc_name
4767 && strcmp ("ppr@", sym->result->name) == 0)
4769 sym->result->attr.proc_pointer = 1;
4770 sym->attr.pointer = 0;
4778 /* Match the interface for a PROCEDURE declaration,
4779 including brackets (R1212). */
4782 match_procedure_interface (gfc_symbol **proc_if)
4786 locus old_loc, entry_loc;
4787 gfc_namespace *old_ns = gfc_current_ns;
4788 char name[GFC_MAX_SYMBOL_LEN + 1];
4790 old_loc = entry_loc = gfc_current_locus;
4791 gfc_clear_ts (¤t_ts);
4793 if (gfc_match (" (") != MATCH_YES)
4795 gfc_current_locus = entry_loc;
4799 /* Get the type spec. for the procedure interface. */
4800 old_loc = gfc_current_locus;
4801 m = gfc_match_decl_type_spec (¤t_ts, 0);
4802 gfc_gobble_whitespace ();
4803 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4806 if (m == MATCH_ERROR)
4809 /* Procedure interface is itself a procedure. */
4810 gfc_current_locus = old_loc;
4811 m = gfc_match_name (name);
4813 /* First look to see if it is already accessible in the current
4814 namespace because it is use associated or contained. */
4816 if (gfc_find_sym_tree (name, NULL, 0, &st))
4819 /* If it is still not found, then try the parent namespace, if it
4820 exists and create the symbol there if it is still not found. */
4821 if (gfc_current_ns->parent)
4822 gfc_current_ns = gfc_current_ns->parent;
4823 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4826 gfc_current_ns = old_ns;
4827 *proc_if = st->n.sym;
4832 /* Resolve interface if possible. That way, attr.procedure is only set
4833 if it is declared by a later procedure-declaration-stmt, which is
4834 invalid per F08:C1216 (cf. resolve_procedure_interface). */
4835 while ((*proc_if)->ts.interface)
4836 *proc_if = (*proc_if)->ts.interface;
4838 if ((*proc_if)->attr.flavor == FL_UNKNOWN
4839 && (*proc_if)->ts.type == BT_UNKNOWN
4840 && gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
4841 (*proc_if)->name, NULL) == FAILURE)
4846 if (gfc_match (" )") != MATCH_YES)
4848 gfc_current_locus = entry_loc;
4856 /* Match a PROCEDURE declaration (R1211). */
4859 match_procedure_decl (void)
4862 gfc_symbol *sym, *proc_if = NULL;
4864 gfc_expr *initializer = NULL;
4866 /* Parse interface (with brackets). */
4867 m = match_procedure_interface (&proc_if);
4871 /* Parse attributes (with colons). */
4872 m = match_attr_spec();
4873 if (m == MATCH_ERROR)
4876 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
4878 current_attr.is_bind_c = 1;
4879 has_name_equals = 0;
4880 curr_binding_label = NULL;
4883 /* Get procedure symbols. */
4886 m = gfc_match_symbol (&sym, 0);
4889 else if (m == MATCH_ERROR)
4892 /* Add current_attr to the symbol attributes. */
4893 if (gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
4896 if (sym->attr.is_bind_c)
4898 /* Check for C1218. */
4899 if (!proc_if || !proc_if->attr.is_bind_c)
4901 gfc_error ("BIND(C) attribute at %C requires "
4902 "an interface with BIND(C)");
4905 /* Check for C1217. */
4906 if (has_name_equals && sym->attr.pointer)
4908 gfc_error ("BIND(C) procedure with NAME may not have "
4909 "POINTER attribute at %C");
4912 if (has_name_equals && sym->attr.dummy)
4914 gfc_error ("Dummy procedure at %C may not have "
4915 "BIND(C) attribute with NAME");
4918 /* Set binding label for BIND(C). */
4919 if (set_binding_label (&sym->binding_label, sym->name, num)
4924 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4927 if (add_hidden_procptr_result (sym) == SUCCESS)
4930 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4933 /* Set interface. */
4934 if (proc_if != NULL)
4936 if (sym->ts.type != BT_UNKNOWN)
4938 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4939 sym->name, &gfc_current_locus,
4940 gfc_basic_typename (sym->ts.type));
4943 sym->ts.interface = proc_if;
4944 sym->attr.untyped = 1;
4945 sym->attr.if_source = IFSRC_IFBODY;
4947 else if (current_ts.type != BT_UNKNOWN)
4949 if (gfc_add_type (sym, ¤t_ts, &gfc_current_locus) == FAILURE)
4951 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4952 sym->ts.interface->ts = current_ts;
4953 sym->ts.interface->attr.flavor = FL_PROCEDURE;
4954 sym->ts.interface->attr.function = 1;
4955 sym->attr.function = 1;
4956 sym->attr.if_source = IFSRC_UNKNOWN;
4959 if (gfc_match (" =>") == MATCH_YES)
4961 if (!current_attr.pointer)
4963 gfc_error ("Initialization at %C isn't for a pointer variable");
4968 m = match_pointer_init (&initializer, 1);
4972 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4978 if (gfc_match_eos () == MATCH_YES)
4980 if (gfc_match_char (',') != MATCH_YES)
4985 gfc_error ("Syntax error in PROCEDURE statement at %C");
4989 /* Free stuff up and return. */
4990 gfc_free_expr (initializer);
4996 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4999 /* Match a procedure pointer component declaration (R445). */
5002 match_ppc_decl (void)
5005 gfc_symbol *proc_if = NULL;
5009 gfc_expr *initializer = NULL;
5010 gfc_typebound_proc* tb;
5011 char name[GFC_MAX_SYMBOL_LEN + 1];
5013 /* Parse interface (with brackets). */
5014 m = match_procedure_interface (&proc_if);
5018 /* Parse attributes. */
5019 tb = XCNEW (gfc_typebound_proc);
5020 tb->where = gfc_current_locus;
5021 m = match_binding_attributes (tb, false, true);
5022 if (m == MATCH_ERROR)
5025 gfc_clear_attr (¤t_attr);
5026 current_attr.procedure = 1;
5027 current_attr.proc_pointer = 1;
5028 current_attr.access = tb->access;
5029 current_attr.flavor = FL_PROCEDURE;
5031 /* Match the colons (required). */
5032 if (gfc_match (" ::") != MATCH_YES)
5034 gfc_error ("Expected '::' after binding-attributes at %C");
5038 /* Check for C450. */
5039 if (!tb->nopass && proc_if == NULL)
5041 gfc_error("NOPASS or explicit interface required at %C");
5045 if (gfc_notify_std (GFC_STD_F2003, "Procedure pointer "
5046 "component at %C") == FAILURE)
5049 /* Match PPC names. */
5053 m = gfc_match_name (name);
5056 else if (m == MATCH_ERROR)
5059 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
5062 /* Add current_attr to the symbol attributes. */
5063 if (gfc_copy_attr (&c->attr, ¤t_attr, NULL) == FAILURE)
5066 if (gfc_add_external (&c->attr, NULL) == FAILURE)
5069 if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
5074 /* Set interface. */
5075 if (proc_if != NULL)
5077 c->ts.interface = proc_if;
5078 c->attr.untyped = 1;
5079 c->attr.if_source = IFSRC_IFBODY;
5081 else if (ts.type != BT_UNKNOWN)
5084 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5085 c->ts.interface->result = c->ts.interface;
5086 c->ts.interface->ts = ts;
5087 c->ts.interface->attr.flavor = FL_PROCEDURE;
5088 c->ts.interface->attr.function = 1;
5089 c->attr.function = 1;
5090 c->attr.if_source = IFSRC_UNKNOWN;
5093 if (gfc_match (" =>") == MATCH_YES)
5095 m = match_pointer_init (&initializer, 1);
5098 gfc_free_expr (initializer);
5101 c->initializer = initializer;
5104 if (gfc_match_eos () == MATCH_YES)
5106 if (gfc_match_char (',') != MATCH_YES)
5111 gfc_error ("Syntax error in procedure pointer component at %C");
5116 /* Match a PROCEDURE declaration inside an interface (R1206). */
5119 match_procedure_in_interface (void)
5123 char name[GFC_MAX_SYMBOL_LEN + 1];
5126 if (current_interface.type == INTERFACE_NAMELESS
5127 || current_interface.type == INTERFACE_ABSTRACT)
5129 gfc_error ("PROCEDURE at %C must be in a generic interface");
5133 /* Check if the F2008 optional double colon appears. */
5134 gfc_gobble_whitespace ();
5135 old_locus = gfc_current_locus;
5136 if (gfc_match ("::") == MATCH_YES)
5138 if (gfc_notify_std (GFC_STD_F2008, "double colon in "
5139 "MODULE PROCEDURE statement at %L", &old_locus)
5144 gfc_current_locus = old_locus;
5148 m = gfc_match_name (name);
5151 else if (m == MATCH_ERROR)
5153 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5156 if (gfc_add_interface (sym) == FAILURE)
5159 if (gfc_match_eos () == MATCH_YES)
5161 if (gfc_match_char (',') != MATCH_YES)
5168 gfc_error ("Syntax error in PROCEDURE statement at %C");
5173 /* General matcher for PROCEDURE declarations. */
5175 static match match_procedure_in_type (void);
5178 gfc_match_procedure (void)
5182 switch (gfc_current_state ())
5187 case COMP_SUBROUTINE:
5190 m = match_procedure_decl ();
5192 case COMP_INTERFACE:
5193 m = match_procedure_in_interface ();
5196 m = match_ppc_decl ();
5198 case COMP_DERIVED_CONTAINS:
5199 m = match_procedure_in_type ();
5208 if (gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C")
5216 /* Warn if a matched procedure has the same name as an intrinsic; this is
5217 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5218 parser-state-stack to find out whether we're in a module. */
5221 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5225 in_module = (gfc_state_stack->previous
5226 && gfc_state_stack->previous->state == COMP_MODULE);
5228 gfc_warn_intrinsic_shadow (sym, in_module, func);
5232 /* Match a function declaration. */
5235 gfc_match_function_decl (void)
5237 char name[GFC_MAX_SYMBOL_LEN + 1];
5238 gfc_symbol *sym, *result;
5242 match found_match; /* Status returned by match func. */
5244 if (gfc_current_state () != COMP_NONE
5245 && gfc_current_state () != COMP_INTERFACE
5246 && gfc_current_state () != COMP_CONTAINS)
5249 gfc_clear_ts (¤t_ts);
5251 old_loc = gfc_current_locus;
5253 m = gfc_match_prefix (¤t_ts);
5256 gfc_current_locus = old_loc;
5260 if (gfc_match ("function% %n", name) != MATCH_YES)
5262 gfc_current_locus = old_loc;
5265 if (get_proc_name (name, &sym, false))
5268 if (add_hidden_procptr_result (sym) == SUCCESS)
5271 gfc_new_block = sym;
5273 m = gfc_match_formal_arglist (sym, 0, 0);
5276 gfc_error ("Expected formal argument list in function "
5277 "definition at %C");
5281 else if (m == MATCH_ERROR)
5286 /* According to the draft, the bind(c) and result clause can
5287 come in either order after the formal_arg_list (i.e., either
5288 can be first, both can exist together or by themselves or neither
5289 one). Therefore, the match_result can't match the end of the
5290 string, and check for the bind(c) or result clause in either order. */
5291 found_match = gfc_match_eos ();
5293 /* Make sure that it isn't already declared as BIND(C). If it is, it
5294 must have been marked BIND(C) with a BIND(C) attribute and that is
5295 not allowed for procedures. */
5296 if (sym->attr.is_bind_c == 1)
5298 sym->attr.is_bind_c = 0;
5299 if (sym->old_symbol != NULL)
5300 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5301 "variables or common blocks",
5302 &(sym->old_symbol->declared_at));
5304 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5305 "variables or common blocks", &gfc_current_locus);
5308 if (found_match != MATCH_YES)
5310 /* If we haven't found the end-of-statement, look for a suffix. */
5311 suffix_match = gfc_match_suffix (sym, &result);
5312 if (suffix_match == MATCH_YES)
5313 /* Need to get the eos now. */
5314 found_match = gfc_match_eos ();
5316 found_match = suffix_match;
5319 if(found_match != MATCH_YES)
5323 /* Make changes to the symbol. */
5326 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
5329 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
5330 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5333 /* Delay matching the function characteristics until after the
5334 specification block by signalling kind=-1. */
5335 sym->declared_at = old_loc;
5336 if (current_ts.type != BT_UNKNOWN)
5337 current_ts.kind = -1;
5339 current_ts.kind = 0;
5343 if (current_ts.type != BT_UNKNOWN
5344 && gfc_add_type (sym, ¤t_ts, &gfc_current_locus) == FAILURE)
5350 if (current_ts.type != BT_UNKNOWN
5351 && gfc_add_type (result, ¤t_ts, &gfc_current_locus)
5354 sym->result = result;
5357 /* Warn if this procedure has the same name as an intrinsic. */
5358 warn_intrinsic_shadow (sym, true);
5364 gfc_current_locus = old_loc;
5369 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5370 pass the name of the entry, rather than the gfc_current_block name, and
5371 to return false upon finding an existing global entry. */
5374 add_global_entry (const char *name, int sub)
5377 enum gfc_symbol_type type;
5379 s = gfc_get_gsymbol(name);
5380 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5383 || (s->type != GSYM_UNKNOWN
5384 && s->type != type))
5385 gfc_global_used(s, NULL);
5389 s->where = gfc_current_locus;
5391 s->ns = gfc_current_ns;
5398 /* Match an ENTRY statement. */
5401 gfc_match_entry (void)
5406 char name[GFC_MAX_SYMBOL_LEN + 1];
5407 gfc_compile_state state;
5411 bool module_procedure;
5415 m = gfc_match_name (name);
5419 if (gfc_notify_std (GFC_STD_F2008_OBS,
5420 "ENTRY statement at %C") == FAILURE)
5423 state = gfc_current_state ();
5424 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5429 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5432 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5434 case COMP_BLOCK_DATA:
5435 gfc_error ("ENTRY statement at %C cannot appear within "
5438 case COMP_INTERFACE:
5439 gfc_error ("ENTRY statement at %C cannot appear within "
5443 gfc_error ("ENTRY statement at %C cannot appear within "
5444 "a DERIVED TYPE block");
5447 gfc_error ("ENTRY statement at %C cannot appear within "
5448 "an IF-THEN block");
5451 case COMP_DO_CONCURRENT:
5452 gfc_error ("ENTRY statement at %C cannot appear within "
5456 gfc_error ("ENTRY statement at %C cannot appear within "
5460 gfc_error ("ENTRY statement at %C cannot appear within "
5464 gfc_error ("ENTRY statement at %C cannot appear within "
5468 gfc_error ("ENTRY statement at %C cannot appear within "
5469 "a contained subprogram");
5472 gfc_internal_error ("gfc_match_entry(): Bad state");
5477 module_procedure = gfc_current_ns->parent != NULL
5478 && gfc_current_ns->parent->proc_name
5479 && gfc_current_ns->parent->proc_name->attr.flavor
5482 if (gfc_current_ns->parent != NULL
5483 && gfc_current_ns->parent->proc_name
5484 && !module_procedure)
5486 gfc_error("ENTRY statement at %C cannot appear in a "
5487 "contained procedure");
5491 /* Module function entries need special care in get_proc_name
5492 because previous references within the function will have
5493 created symbols attached to the current namespace. */
5494 if (get_proc_name (name, &entry,
5495 gfc_current_ns->parent != NULL
5496 && module_procedure))
5499 proc = gfc_current_block ();
5501 /* Make sure that it isn't already declared as BIND(C). If it is, it
5502 must have been marked BIND(C) with a BIND(C) attribute and that is
5503 not allowed for procedures. */
5504 if (entry->attr.is_bind_c == 1)
5506 entry->attr.is_bind_c = 0;
5507 if (entry->old_symbol != NULL)
5508 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5509 "variables or common blocks",
5510 &(entry->old_symbol->declared_at));
5512 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5513 "variables or common blocks", &gfc_current_locus);
5516 /* Check what next non-whitespace character is so we can tell if there
5517 is the required parens if we have a BIND(C). */
5518 gfc_gobble_whitespace ();
5519 peek_char = gfc_peek_ascii_char ();
5521 if (state == COMP_SUBROUTINE)
5523 /* An entry in a subroutine. */
5524 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
5527 m = gfc_match_formal_arglist (entry, 0, 1);
5531 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5532 never be an internal procedure. */
5533 is_bind_c = gfc_match_bind_c (entry, true);
5534 if (is_bind_c == MATCH_ERROR)
5536 if (is_bind_c == MATCH_YES)
5538 if (peek_char != '(')
5540 gfc_error ("Missing required parentheses before BIND(C) at %C");
5543 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
5548 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5549 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
5554 /* An entry in a function.
5555 We need to take special care because writing
5560 ENTRY f() RESULT (r)
5562 ENTRY f RESULT (r). */
5563 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
5566 old_loc = gfc_current_locus;
5567 if (gfc_match_eos () == MATCH_YES)
5569 gfc_current_locus = old_loc;
5570 /* Match the empty argument list, and add the interface to
5572 m = gfc_match_formal_arglist (entry, 0, 1);
5575 m = gfc_match_formal_arglist (entry, 0, 0);
5582 if (gfc_match_eos () == MATCH_YES)
5584 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5585 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5588 entry->result = entry;
5592 m = gfc_match_suffix (entry, &result);
5594 gfc_syntax_error (ST_ENTRY);
5600 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
5601 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
5602 || gfc_add_function (&entry->attr, result->name, NULL)
5605 entry->result = result;
5609 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5610 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5612 entry->result = entry;
5617 if (gfc_match_eos () != MATCH_YES)
5619 gfc_syntax_error (ST_ENTRY);
5623 entry->attr.recursive = proc->attr.recursive;
5624 entry->attr.elemental = proc->attr.elemental;
5625 entry->attr.pure = proc->attr.pure;
5627 el = gfc_get_entry_list ();
5629 el->next = gfc_current_ns->entries;
5630 gfc_current_ns->entries = el;
5632 el->id = el->next->id + 1;
5636 new_st.op = EXEC_ENTRY;
5637 new_st.ext.entry = el;
5643 /* Match a subroutine statement, including optional prefixes. */
5646 gfc_match_subroutine (void)
5648 char name[GFC_MAX_SYMBOL_LEN + 1];
5653 bool allow_binding_name;
5655 if (gfc_current_state () != COMP_NONE
5656 && gfc_current_state () != COMP_INTERFACE
5657 && gfc_current_state () != COMP_CONTAINS)
5660 m = gfc_match_prefix (NULL);
5664 m = gfc_match ("subroutine% %n", name);
5668 if (get_proc_name (name, &sym, false))
5671 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5672 the symbol existed before. */
5673 sym->declared_at = gfc_current_locus;
5675 if (add_hidden_procptr_result (sym) == SUCCESS)
5678 gfc_new_block = sym;
5680 /* Check what next non-whitespace character is so we can tell if there
5681 is the required parens if we have a BIND(C). */
5682 gfc_gobble_whitespace ();
5683 peek_char = gfc_peek_ascii_char ();
5685 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5688 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5691 /* Make sure that it isn't already declared as BIND(C). If it is, it
5692 must have been marked BIND(C) with a BIND(C) attribute and that is
5693 not allowed for procedures. */
5694 if (sym->attr.is_bind_c == 1)
5696 sym->attr.is_bind_c = 0;
5697 if (sym->old_symbol != NULL)
5698 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5699 "variables or common blocks",
5700 &(sym->old_symbol->declared_at));
5702 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5703 "variables or common blocks", &gfc_current_locus);
5706 /* C binding names are not allowed for internal procedures. */
5707 if (gfc_current_state () == COMP_CONTAINS
5708 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5709 allow_binding_name = false;
5711 allow_binding_name = true;
5713 /* Here, we are just checking if it has the bind(c) attribute, and if
5714 so, then we need to make sure it's all correct. If it doesn't,
5715 we still need to continue matching the rest of the subroutine line. */
5716 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5717 if (is_bind_c == MATCH_ERROR)
5719 /* There was an attempt at the bind(c), but it was wrong. An
5720 error message should have been printed w/in the gfc_match_bind_c
5721 so here we'll just return the MATCH_ERROR. */
5725 if (is_bind_c == MATCH_YES)
5727 /* The following is allowed in the Fortran 2008 draft. */
5728 if (gfc_current_state () == COMP_CONTAINS
5729 && sym->ns->proc_name->attr.flavor != FL_MODULE
5730 && gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
5731 "at %L may not be specified for an internal "
5732 "procedure", &gfc_current_locus)
5736 if (peek_char != '(')
5738 gfc_error ("Missing required parentheses before BIND(C) at %C");
5741 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5746 if (gfc_match_eos () != MATCH_YES)
5748 gfc_syntax_error (ST_SUBROUTINE);
5752 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5755 /* Warn if it has the same name as an intrinsic. */
5756 warn_intrinsic_shadow (sym, false);
5762 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5763 given, and set the binding label in either the given symbol (if not
5764 NULL), or in the current_ts. The symbol may be NULL because we may
5765 encounter the BIND(C) before the declaration itself. Return
5766 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5767 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5768 or MATCH_YES if the specifier was correct and the binding label and
5769 bind(c) fields were set correctly for the given symbol or the
5770 current_ts. If allow_binding_name is false, no binding name may be
5774 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5776 /* binding label, if exists */
5777 const char* binding_label = NULL;
5781 /* Initialize the flag that specifies whether we encountered a NAME=
5782 specifier or not. */
5783 has_name_equals = 0;
5785 /* This much we have to be able to match, in this order, if
5786 there is a bind(c) label. */
5787 if (gfc_match (" bind ( c ") != MATCH_YES)
5790 /* Now see if there is a binding label, or if we've reached the
5791 end of the bind(c) attribute without one. */
5792 if (gfc_match_char (',') == MATCH_YES)
5794 if (gfc_match (" name = ") != MATCH_YES)
5796 gfc_error ("Syntax error in NAME= specifier for binding label "
5798 /* should give an error message here */
5802 has_name_equals = 1;
5804 /* Get the opening quote. */
5805 double_quote = MATCH_YES;
5806 single_quote = MATCH_YES;
5807 double_quote = gfc_match_char ('"');
5808 if (double_quote != MATCH_YES)
5809 single_quote = gfc_match_char ('\'');
5810 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5812 gfc_error ("Syntax error in NAME= specifier for binding label "
5817 /* Grab the binding label, using functions that will not lower
5818 case the names automatically. */
5819 if (gfc_match_name_C (&binding_label) != MATCH_YES)
5822 /* Get the closing quotation. */
5823 if (double_quote == MATCH_YES)
5825 if (gfc_match_char ('"') != MATCH_YES)
5827 gfc_error ("Missing closing quote '\"' for binding label at %C");
5828 /* User started string with '"' so looked to match it. */
5834 if (gfc_match_char ('\'') != MATCH_YES)
5836 gfc_error ("Missing closing quote '\'' for binding label at %C");
5837 /* User started string with "'" char. */
5843 /* Get the required right paren. */
5844 if (gfc_match_char (')') != MATCH_YES)
5846 gfc_error ("Missing closing paren for binding label at %C");
5850 if (has_name_equals && !allow_binding_name)
5852 gfc_error ("No binding name is allowed in BIND(C) at %C");
5856 if (has_name_equals && sym != NULL && sym->attr.dummy)
5858 gfc_error ("For dummy procedure %s, no binding name is "
5859 "allowed in BIND(C) at %C", sym->name);
5864 /* Save the binding label to the symbol. If sym is null, we're
5865 probably matching the typespec attributes of a declaration and
5866 haven't gotten the name yet, and therefore, no symbol yet. */
5870 sym->binding_label = binding_label;
5872 curr_binding_label = binding_label;
5874 else if (allow_binding_name)
5876 /* No binding label, but if symbol isn't null, we
5877 can set the label for it here.
5878 If name="" or allow_binding_name is false, no C binding name is
5880 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5881 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
5884 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5885 && current_interface.type == INTERFACE_ABSTRACT)
5887 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5895 /* Return nonzero if we're currently compiling a contained procedure. */
5898 contained_procedure (void)
5900 gfc_state_data *s = gfc_state_stack;
5902 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5903 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5909 /* Set the kind of each enumerator. The kind is selected such that it is
5910 interoperable with the corresponding C enumeration type, making
5911 sure that -fshort-enums is honored. */
5916 enumerator_history *current_history = NULL;
5920 if (max_enum == NULL || enum_history == NULL)
5923 if (!flag_short_enums)
5929 kind = gfc_integer_kinds[i++].kind;
5931 while (kind < gfc_c_int_kind
5932 && gfc_check_integer_range (max_enum->initializer->value.integer,
5935 current_history = enum_history;
5936 while (current_history != NULL)
5938 current_history->sym->ts.kind = kind;
5939 current_history = current_history->next;
5944 /* Match any of the various end-block statements. Returns the type of
5945 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5946 and END BLOCK statements cannot be replaced by a single END statement. */
5949 gfc_match_end (gfc_statement *st)
5951 char name[GFC_MAX_SYMBOL_LEN + 1];
5952 gfc_compile_state state;
5954 const char *block_name;
5958 gfc_namespace *parent_ns, *ns, *prev_ns;
5959 gfc_namespace **nsp;
5961 old_loc = gfc_current_locus;
5962 if (gfc_match ("end") != MATCH_YES)
5965 state = gfc_current_state ();
5966 block_name = gfc_current_block () == NULL
5967 ? NULL : gfc_current_block ()->name;
5971 case COMP_ASSOCIATE:
5973 if (!strncmp (block_name, "block@", strlen("block@")))
5978 case COMP_DERIVED_CONTAINS:
5979 state = gfc_state_stack->previous->state;
5980 block_name = gfc_state_stack->previous->sym == NULL
5981 ? NULL : gfc_state_stack->previous->sym->name;
5992 *st = ST_END_PROGRAM;
5993 target = " program";
5997 case COMP_SUBROUTINE:
5998 *st = ST_END_SUBROUTINE;
5999 target = " subroutine";
6000 eos_ok = !contained_procedure ();
6004 *st = ST_END_FUNCTION;
6005 target = " function";
6006 eos_ok = !contained_procedure ();
6009 case COMP_BLOCK_DATA:
6010 *st = ST_END_BLOCK_DATA;
6011 target = " block data";
6016 *st = ST_END_MODULE;
6021 case COMP_INTERFACE:
6022 *st = ST_END_INTERFACE;
6023 target = " interface";
6028 case COMP_DERIVED_CONTAINS:
6034 case COMP_ASSOCIATE:
6035 *st = ST_END_ASSOCIATE;
6036 target = " associate";
6053 case COMP_DO_CONCURRENT:
6060 *st = ST_END_CRITICAL;
6061 target = " critical";
6066 case COMP_SELECT_TYPE:
6067 *st = ST_END_SELECT;
6073 *st = ST_END_FORALL;
6088 last_initializer = NULL;
6090 gfc_free_enum_history ();
6094 gfc_error ("Unexpected END statement at %C");
6098 if (gfc_match_eos () == MATCH_YES)
6100 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6102 if (gfc_notify_std (GFC_STD_F2008, "END statement "
6103 "instead of %s statement at %L",
6104 gfc_ascii_statement (*st), &old_loc) == FAILURE)
6109 /* We would have required END [something]. */
6110 gfc_error ("%s statement expected at %L",
6111 gfc_ascii_statement (*st), &old_loc);
6118 /* Verify that we've got the sort of end-block that we're expecting. */
6119 if (gfc_match (target) != MATCH_YES)
6121 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
6125 /* If we're at the end, make sure a block name wasn't required. */
6126 if (gfc_match_eos () == MATCH_YES)
6129 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6130 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6131 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6137 gfc_error ("Expected block name of '%s' in %s statement at %C",
6138 block_name, gfc_ascii_statement (*st));
6143 /* END INTERFACE has a special handler for its several possible endings. */
6144 if (*st == ST_END_INTERFACE)
6145 return gfc_match_end_interface ();
6147 /* We haven't hit the end of statement, so what is left must be an
6149 m = gfc_match_space ();
6151 m = gfc_match_name (name);
6154 gfc_error ("Expected terminating name at %C");
6158 if (block_name == NULL)
6161 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6163 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
6164 gfc_ascii_statement (*st));
6167 /* Procedure pointer as function result. */
6168 else if (strcmp (block_name, "ppr@") == 0
6169 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6171 gfc_error ("Expected label '%s' for %s statement at %C",
6172 gfc_current_block ()->ns->proc_name->name,
6173 gfc_ascii_statement (*st));
6177 if (gfc_match_eos () == MATCH_YES)
6181 gfc_syntax_error (*st);
6184 gfc_current_locus = old_loc;
6186 /* If we are missing an END BLOCK, we created a half-ready namespace.
6187 Remove it from the parent namespace's sibling list. */
6189 if (state == COMP_BLOCK)
6191 parent_ns = gfc_current_ns->parent;
6193 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
6199 if (ns == gfc_current_ns)
6201 if (prev_ns == NULL)
6204 prev_ns->sibling = ns->sibling;
6210 gfc_free_namespace (gfc_current_ns);
6211 gfc_current_ns = parent_ns;
6219 /***************** Attribute declaration statements ****************/
6221 /* Set the attribute of a single variable. */
6226 char name[GFC_MAX_SYMBOL_LEN + 1];
6234 m = gfc_match_name (name);
6238 if (find_special (name, &sym, false))
6241 if (check_function_name (name) == FAILURE)
6247 var_locus = gfc_current_locus;
6249 /* Deal with possible array specification for certain attributes. */
6250 if (current_attr.dimension
6251 || current_attr.codimension
6252 || current_attr.allocatable
6253 || current_attr.pointer
6254 || current_attr.target)
6256 m = gfc_match_array_spec (&as, !current_attr.codimension,
6257 !current_attr.dimension
6258 && !current_attr.pointer
6259 && !current_attr.target);
6260 if (m == MATCH_ERROR)
6263 if (current_attr.dimension && m == MATCH_NO)
6265 gfc_error ("Missing array specification at %L in DIMENSION "
6266 "statement", &var_locus);
6271 if (current_attr.dimension && sym->value)
6273 gfc_error ("Dimensions specified for %s at %L after its "
6274 "initialisation", sym->name, &var_locus);
6279 if (current_attr.codimension && m == MATCH_NO)
6281 gfc_error ("Missing array specification at %L in CODIMENSION "
6282 "statement", &var_locus);
6287 if ((current_attr.allocatable || current_attr.pointer)
6288 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6290 gfc_error ("Array specification must be deferred at %L", &var_locus);
6296 /* Update symbol table. DIMENSION attribute is set in
6297 gfc_set_array_spec(). For CLASS variables, this must be applied
6298 to the first component, or '_data' field. */
6299 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6301 if (gfc_copy_attr (&CLASS_DATA (sym)->attr, ¤t_attr, &var_locus)
6310 if (current_attr.dimension == 0 && current_attr.codimension == 0
6311 && gfc_copy_attr (&sym->attr, ¤t_attr, &var_locus) == FAILURE)
6318 if (sym->ts.type == BT_CLASS
6319 && gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false) == FAILURE)
6325 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
6331 if (sym->attr.cray_pointee && sym->as != NULL)
6333 /* Fix the array spec. */
6334 m = gfc_mod_pointee_as (sym->as);
6335 if (m == MATCH_ERROR)
6339 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
6345 if ((current_attr.external || current_attr.intrinsic)
6346 && sym->attr.flavor != FL_PROCEDURE
6347 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6353 add_hidden_procptr_result (sym);
6358 gfc_free_array_spec (as);
6363 /* Generic attribute declaration subroutine. Used for attributes that
6364 just have a list of names. */
6371 /* Gobble the optional double colon, by simply ignoring the result
6381 if (gfc_match_eos () == MATCH_YES)
6387 if (gfc_match_char (',') != MATCH_YES)
6389 gfc_error ("Unexpected character in variable list at %C");
6399 /* This routine matches Cray Pointer declarations of the form:
6400 pointer ( <pointer>, <pointee> )
6402 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6403 The pointer, if already declared, should be an integer. Otherwise, we
6404 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6405 be either a scalar, or an array declaration. No space is allocated for
6406 the pointee. For the statement
6407 pointer (ipt, ar(10))
6408 any subsequent uses of ar will be translated (in C-notation) as
6409 ar(i) => ((<type> *) ipt)(i)
6410 After gimplification, pointee variable will disappear in the code. */
6413 cray_pointer_decl (void)
6416 gfc_array_spec *as = NULL;
6417 gfc_symbol *cptr; /* Pointer symbol. */
6418 gfc_symbol *cpte; /* Pointee symbol. */
6424 if (gfc_match_char ('(') != MATCH_YES)
6426 gfc_error ("Expected '(' at %C");
6430 /* Match pointer. */
6431 var_locus = gfc_current_locus;
6432 gfc_clear_attr (¤t_attr);
6433 gfc_add_cray_pointer (¤t_attr, &var_locus);
6434 current_ts.type = BT_INTEGER;
6435 current_ts.kind = gfc_index_integer_kind;
6437 m = gfc_match_symbol (&cptr, 0);
6440 gfc_error ("Expected variable name at %C");
6444 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
6447 gfc_set_sym_referenced (cptr);
6449 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6451 cptr->ts.type = BT_INTEGER;
6452 cptr->ts.kind = gfc_index_integer_kind;
6454 else if (cptr->ts.type != BT_INTEGER)
6456 gfc_error ("Cray pointer at %C must be an integer");
6459 else if (cptr->ts.kind < gfc_index_integer_kind)
6460 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6461 " memory addresses require %d bytes",
6462 cptr->ts.kind, gfc_index_integer_kind);
6464 if (gfc_match_char (',') != MATCH_YES)
6466 gfc_error ("Expected \",\" at %C");
6470 /* Match Pointee. */
6471 var_locus = gfc_current_locus;
6472 gfc_clear_attr (¤t_attr);
6473 gfc_add_cray_pointee (¤t_attr, &var_locus);
6474 current_ts.type = BT_UNKNOWN;
6475 current_ts.kind = 0;
6477 m = gfc_match_symbol (&cpte, 0);
6480 gfc_error ("Expected variable name at %C");
6484 /* Check for an optional array spec. */
6485 m = gfc_match_array_spec (&as, true, false);
6486 if (m == MATCH_ERROR)
6488 gfc_free_array_spec (as);
6491 else if (m == MATCH_NO)
6493 gfc_free_array_spec (as);
6497 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
6500 gfc_set_sym_referenced (cpte);
6502 if (cpte->as == NULL)
6504 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
6505 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6507 else if (as != NULL)
6509 gfc_error ("Duplicate array spec for Cray pointee at %C");
6510 gfc_free_array_spec (as);
6516 if (cpte->as != NULL)
6518 /* Fix array spec. */
6519 m = gfc_mod_pointee_as (cpte->as);
6520 if (m == MATCH_ERROR)
6524 /* Point the Pointee at the Pointer. */
6525 cpte->cp_pointer = cptr;
6527 if (gfc_match_char (')') != MATCH_YES)
6529 gfc_error ("Expected \")\" at %C");
6532 m = gfc_match_char (',');
6534 done = true; /* Stop searching for more declarations. */
6538 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6539 || gfc_match_eos () != MATCH_YES)
6541 gfc_error ("Expected \",\" or end of statement at %C");
6549 gfc_match_external (void)
6552 gfc_clear_attr (¤t_attr);
6553 current_attr.external = 1;
6555 return attr_decl ();
6560 gfc_match_intent (void)
6564 /* This is not allowed within a BLOCK construct! */
6565 if (gfc_current_state () == COMP_BLOCK)
6567 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6571 intent = match_intent_spec ();
6572 if (intent == INTENT_UNKNOWN)
6575 gfc_clear_attr (¤t_attr);
6576 current_attr.intent = intent;
6578 return attr_decl ();
6583 gfc_match_intrinsic (void)
6586 gfc_clear_attr (¤t_attr);
6587 current_attr.intrinsic = 1;
6589 return attr_decl ();
6594 gfc_match_optional (void)
6596 /* This is not allowed within a BLOCK construct! */
6597 if (gfc_current_state () == COMP_BLOCK)
6599 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6603 gfc_clear_attr (¤t_attr);
6604 current_attr.optional = 1;
6606 return attr_decl ();
6611 gfc_match_pointer (void)
6613 gfc_gobble_whitespace ();
6614 if (gfc_peek_ascii_char () == '(')
6616 if (!gfc_option.flag_cray_pointer)
6618 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6622 return cray_pointer_decl ();
6626 gfc_clear_attr (¤t_attr);
6627 current_attr.pointer = 1;
6629 return attr_decl ();
6635 gfc_match_allocatable (void)
6637 gfc_clear_attr (¤t_attr);
6638 current_attr.allocatable = 1;
6640 return attr_decl ();
6645 gfc_match_codimension (void)
6647 gfc_clear_attr (¤t_attr);
6648 current_attr.codimension = 1;
6650 return attr_decl ();
6655 gfc_match_contiguous (void)
6657 if (gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C")
6661 gfc_clear_attr (¤t_attr);
6662 current_attr.contiguous = 1;
6664 return attr_decl ();
6669 gfc_match_dimension (void)
6671 gfc_clear_attr (¤t_attr);
6672 current_attr.dimension = 1;
6674 return attr_decl ();
6679 gfc_match_target (void)
6681 gfc_clear_attr (¤t_attr);
6682 current_attr.target = 1;
6684 return attr_decl ();
6688 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6692 access_attr_decl (gfc_statement st)
6694 char name[GFC_MAX_SYMBOL_LEN + 1];
6695 interface_type type;
6697 gfc_symbol *sym, *dt_sym;
6698 gfc_intrinsic_op op;
6701 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6706 m = gfc_match_generic_spec (&type, name, &op);
6709 if (m == MATCH_ERROR)
6714 case INTERFACE_NAMELESS:
6715 case INTERFACE_ABSTRACT:
6718 case INTERFACE_GENERIC:
6719 if (gfc_get_symbol (name, NULL, &sym))
6722 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
6723 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6724 sym->name, NULL) == FAILURE)
6727 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
6728 && gfc_add_access (&dt_sym->attr,
6729 (st == ST_PUBLIC) ? ACCESS_PUBLIC
6731 sym->name, NULL) == FAILURE)
6736 case INTERFACE_INTRINSIC_OP:
6737 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6739 gfc_intrinsic_op other_op;
6741 gfc_current_ns->operator_access[op] =
6742 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6744 /* Handle the case if there is another op with the same
6745 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6746 other_op = gfc_equivalent_op (op);
6748 if (other_op != INTRINSIC_NONE)
6749 gfc_current_ns->operator_access[other_op] =
6750 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6755 gfc_error ("Access specification of the %s operator at %C has "
6756 "already been specified", gfc_op2string (op));
6762 case INTERFACE_USER_OP:
6763 uop = gfc_get_uop (name);
6765 if (uop->access == ACCESS_UNKNOWN)
6767 uop->access = (st == ST_PUBLIC)
6768 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6772 gfc_error ("Access specification of the .%s. operator at %C "
6773 "has already been specified", sym->name);
6780 if (gfc_match_char (',') == MATCH_NO)
6784 if (gfc_match_eos () != MATCH_YES)
6789 gfc_syntax_error (st);
6797 gfc_match_protected (void)
6802 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6804 gfc_error ("PROTECTED at %C only allowed in specification "
6805 "part of a module");
6810 if (gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C")
6814 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6819 if (gfc_match_eos () == MATCH_YES)
6824 m = gfc_match_symbol (&sym, 0);
6828 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6841 if (gfc_match_eos () == MATCH_YES)
6843 if (gfc_match_char (',') != MATCH_YES)
6850 gfc_error ("Syntax error in PROTECTED statement at %C");
6855 /* The PRIVATE statement is a bit weird in that it can be an attribute
6856 declaration, but also works as a standalone statement inside of a
6857 type declaration or a module. */
6860 gfc_match_private (gfc_statement *st)
6863 if (gfc_match ("private") != MATCH_YES)
6866 if (gfc_current_state () != COMP_MODULE
6867 && !(gfc_current_state () == COMP_DERIVED
6868 && gfc_state_stack->previous
6869 && gfc_state_stack->previous->state == COMP_MODULE)
6870 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6871 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6872 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6874 gfc_error ("PRIVATE statement at %C is only allowed in the "
6875 "specification part of a module");
6879 if (gfc_current_state () == COMP_DERIVED)
6881 if (gfc_match_eos () == MATCH_YES)
6887 gfc_syntax_error (ST_PRIVATE);
6891 if (gfc_match_eos () == MATCH_YES)
6898 return access_attr_decl (ST_PRIVATE);
6903 gfc_match_public (gfc_statement *st)
6906 if (gfc_match ("public") != MATCH_YES)
6909 if (gfc_current_state () != COMP_MODULE)
6911 gfc_error ("PUBLIC statement at %C is only allowed in the "
6912 "specification part of a module");
6916 if (gfc_match_eos () == MATCH_YES)
6923 return access_attr_decl (ST_PUBLIC);
6927 /* Workhorse for gfc_match_parameter. */
6937 m = gfc_match_symbol (&sym, 0);
6939 gfc_error ("Expected variable name at %C in PARAMETER statement");
6944 if (gfc_match_char ('=') == MATCH_NO)
6946 gfc_error ("Expected = sign in PARAMETER statement at %C");
6950 m = gfc_match_init_expr (&init);
6952 gfc_error ("Expected expression at %C in PARAMETER statement");
6956 if (sym->ts.type == BT_UNKNOWN
6957 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6963 if (gfc_check_assign_symbol (sym, NULL, init) == FAILURE
6964 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6972 gfc_error ("Initializing already initialized variable at %C");
6977 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
6978 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6981 gfc_free_expr (init);
6986 /* Match a parameter statement, with the weird syntax that these have. */
6989 gfc_match_parameter (void)
6993 if (gfc_match_char ('(') == MATCH_NO)
7002 if (gfc_match (" )%t") == MATCH_YES)
7005 if (gfc_match_char (',') != MATCH_YES)
7007 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7017 /* Save statements have a special syntax. */
7020 gfc_match_save (void)
7022 char n[GFC_MAX_SYMBOL_LEN+1];
7027 if (gfc_match_eos () == MATCH_YES)
7029 if (gfc_current_ns->seen_save)
7031 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
7032 "follows previous SAVE statement")
7037 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
7041 if (gfc_current_ns->save_all)
7043 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
7044 "blanket SAVE statement")
7053 m = gfc_match_symbol (&sym, 0);
7057 if (gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
7058 &gfc_current_locus) == FAILURE)
7069 m = gfc_match (" / %n /", &n);
7070 if (m == MATCH_ERROR)
7075 c = gfc_get_common (n, 0);
7078 gfc_current_ns->seen_save = 1;
7081 if (gfc_match_eos () == MATCH_YES)
7083 if (gfc_match_char (',') != MATCH_YES)
7090 gfc_error ("Syntax error in SAVE statement at %C");
7096 gfc_match_value (void)
7101 /* This is not allowed within a BLOCK construct! */
7102 if (gfc_current_state () == COMP_BLOCK)
7104 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7108 if (gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C")
7112 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7117 if (gfc_match_eos () == MATCH_YES)
7122 m = gfc_match_symbol (&sym, 0);
7126 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
7139 if (gfc_match_eos () == MATCH_YES)
7141 if (gfc_match_char (',') != MATCH_YES)
7148 gfc_error ("Syntax error in VALUE statement at %C");
7154 gfc_match_volatile (void)
7159 if (gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C")
7163 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7168 if (gfc_match_eos () == MATCH_YES)
7173 /* VOLATILE is special because it can be added to host-associated
7174 symbols locally. Except for coarrays. */
7175 m = gfc_match_symbol (&sym, 1);
7179 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7180 for variable in a BLOCK which is defined outside of the BLOCK. */
7181 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7183 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
7184 "%C, which is use-/host-associated", sym->name);
7187 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
7200 if (gfc_match_eos () == MATCH_YES)
7202 if (gfc_match_char (',') != MATCH_YES)
7209 gfc_error ("Syntax error in VOLATILE statement at %C");
7215 gfc_match_asynchronous (void)
7220 if (gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C")
7224 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7229 if (gfc_match_eos () == MATCH_YES)
7234 /* ASYNCHRONOUS is special because it can be added to host-associated
7236 m = gfc_match_symbol (&sym, 1);
7240 if (gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus)
7253 if (gfc_match_eos () == MATCH_YES)
7255 if (gfc_match_char (',') != MATCH_YES)
7262 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7267 /* Match a module procedure statement. Note that we have to modify
7268 symbols in the parent's namespace because the current one was there
7269 to receive symbols that are in an interface's formal argument list. */
7272 gfc_match_modproc (void)
7274 char name[GFC_MAX_SYMBOL_LEN + 1];
7278 gfc_namespace *module_ns;
7279 gfc_interface *old_interface_head, *interface;
7281 if (gfc_state_stack->state != COMP_INTERFACE
7282 || gfc_state_stack->previous == NULL
7283 || current_interface.type == INTERFACE_NAMELESS
7284 || current_interface.type == INTERFACE_ABSTRACT)
7286 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7291 module_ns = gfc_current_ns->parent;
7292 for (; module_ns; module_ns = module_ns->parent)
7293 if (module_ns->proc_name->attr.flavor == FL_MODULE
7294 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7295 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7296 && !module_ns->proc_name->attr.contained))
7299 if (module_ns == NULL)
7302 /* Store the current state of the interface. We will need it if we
7303 end up with a syntax error and need to recover. */
7304 old_interface_head = gfc_current_interface_head ();
7306 /* Check if the F2008 optional double colon appears. */
7307 gfc_gobble_whitespace ();
7308 old_locus = gfc_current_locus;
7309 if (gfc_match ("::") == MATCH_YES)
7311 if (gfc_notify_std (GFC_STD_F2008, "double colon in "
7312 "MODULE PROCEDURE statement at %L", &old_locus)
7317 gfc_current_locus = old_locus;
7322 old_locus = gfc_current_locus;
7324 m = gfc_match_name (name);
7330 /* Check for syntax error before starting to add symbols to the
7331 current namespace. */
7332 if (gfc_match_eos () == MATCH_YES)
7335 if (!last && gfc_match_char (',') != MATCH_YES)
7338 /* Now we're sure the syntax is valid, we process this item
7340 if (gfc_get_symbol (name, module_ns, &sym))
7343 if (sym->attr.intrinsic)
7345 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7346 "PROCEDURE", &old_locus);
7350 if (sym->attr.proc != PROC_MODULE
7351 && gfc_add_procedure (&sym->attr, PROC_MODULE,
7352 sym->name, NULL) == FAILURE)
7355 if (gfc_add_interface (sym) == FAILURE)
7358 sym->attr.mod_proc = 1;
7359 sym->declared_at = old_locus;
7368 /* Restore the previous state of the interface. */
7369 interface = gfc_current_interface_head ();
7370 gfc_set_current_interface_head (old_interface_head);
7372 /* Free the new interfaces. */
7373 while (interface != old_interface_head)
7375 gfc_interface *i = interface->next;
7380 /* And issue a syntax error. */
7381 gfc_syntax_error (ST_MODULE_PROC);
7386 /* Check a derived type that is being extended. */
7388 check_extended_derived_type (char *name)
7390 gfc_symbol *extended;
7392 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7394 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7400 gfc_error ("No such symbol in TYPE definition at %C");
7404 extended = gfc_find_dt_in_generic (extended);
7406 if (extended->attr.flavor != FL_DERIVED)
7408 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7409 "derived type", name);
7413 if (extended->attr.is_bind_c)
7415 gfc_error ("'%s' cannot be extended at %C because it "
7416 "is BIND(C)", extended->name);
7420 if (extended->attr.sequence)
7422 gfc_error ("'%s' cannot be extended at %C because it "
7423 "is a SEQUENCE type", extended->name);
7431 /* Match the optional attribute specifiers for a type declaration.
7432 Return MATCH_ERROR if an error is encountered in one of the handled
7433 attributes (public, private, bind(c)), MATCH_NO if what's found is
7434 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7435 checking on attribute conflicts needs to be done. */
7438 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7440 /* See if the derived type is marked as private. */
7441 if (gfc_match (" , private") == MATCH_YES)
7443 if (gfc_current_state () != COMP_MODULE)
7445 gfc_error ("Derived type at %C can only be PRIVATE in the "
7446 "specification part of a module");
7450 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
7453 else if (gfc_match (" , public") == MATCH_YES)
7455 if (gfc_current_state () != COMP_MODULE)
7457 gfc_error ("Derived type at %C can only be PUBLIC in the "
7458 "specification part of a module");
7462 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
7465 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7467 /* If the type is defined to be bind(c) it then needs to make
7468 sure that all fields are interoperable. This will
7469 need to be a semantic check on the finished derived type.
7470 See 15.2.3 (lines 9-12) of F2003 draft. */
7471 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
7474 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7476 else if (gfc_match (" , abstract") == MATCH_YES)
7478 if (gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C")
7482 if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
7485 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
7487 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
7493 /* If we get here, something matched. */
7498 /* Match the beginning of a derived type declaration. If a type name
7499 was the result of a function, then it is possible to have a symbol
7500 already to be known as a derived type yet have no components. */
7503 gfc_match_derived_decl (void)
7505 char name[GFC_MAX_SYMBOL_LEN + 1];
7506 char parent[GFC_MAX_SYMBOL_LEN + 1];
7507 symbol_attribute attr;
7508 gfc_symbol *sym, *gensym;
7509 gfc_symbol *extended;
7511 match is_type_attr_spec = MATCH_NO;
7512 bool seen_attr = false;
7513 gfc_interface *intr = NULL, *head;
7515 if (gfc_current_state () == COMP_DERIVED)
7520 gfc_clear_attr (&attr);
7525 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7526 if (is_type_attr_spec == MATCH_ERROR)
7528 if (is_type_attr_spec == MATCH_YES)
7530 } while (is_type_attr_spec == MATCH_YES);
7532 /* Deal with derived type extensions. The extension attribute has
7533 been added to 'attr' but now the parent type must be found and
7536 extended = check_extended_derived_type (parent);
7538 if (parent[0] && !extended)
7541 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7543 gfc_error ("Expected :: in TYPE definition at %C");
7547 m = gfc_match (" %n%t", name);
7551 /* Make sure the name is not the name of an intrinsic type. */
7552 if (gfc_is_intrinsic_typename (name))
7554 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7559 if (gfc_get_symbol (name, NULL, &gensym))
7562 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
7564 gfc_error ("Derived type name '%s' at %C already has a basic type "
7565 "of %s", gensym->name, gfc_typename (&gensym->ts));
7569 if (!gensym->attr.generic
7570 && gfc_add_generic (&gensym->attr, gensym->name, NULL) == FAILURE)
7573 if (!gensym->attr.function
7574 && gfc_add_function (&gensym->attr, gensym->name, NULL) == FAILURE)
7577 sym = gfc_find_dt_in_generic (gensym);
7579 if (sym && (sym->components != NULL || sym->attr.zero_comp))
7581 gfc_error ("Derived type definition of '%s' at %C has already been "
7582 "defined", sym->name);
7588 /* Use upper case to save the actual derived-type symbol. */
7589 gfc_get_symbol (gfc_get_string ("%c%s",
7590 (char) TOUPPER ((unsigned char) gensym->name[0]),
7591 &gensym->name[1]), NULL, &sym);
7592 sym->name = gfc_get_string (gensym->name);
7593 head = gensym->generic;
7594 intr = gfc_get_interface ();
7596 intr->where = gfc_current_locus;
7597 intr->sym->declared_at = gfc_current_locus;
7599 gensym->generic = intr;
7600 gensym->attr.if_source = IFSRC_DECL;
7603 /* The symbol may already have the derived attribute without the
7604 components. The ways this can happen is via a function
7605 definition, an INTRINSIC statement or a subtype in another
7606 derived type that is a pointer. The first part of the AND clause
7607 is true if the symbol is not the return value of a function. */
7608 if (sym->attr.flavor != FL_DERIVED
7609 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
7612 if (attr.access != ACCESS_UNKNOWN
7613 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
7615 else if (sym->attr.access == ACCESS_UNKNOWN
7616 && gensym->attr.access != ACCESS_UNKNOWN
7617 && gfc_add_access (&sym->attr, gensym->attr.access, sym->name, NULL)
7621 if (sym->attr.access != ACCESS_UNKNOWN
7622 && gensym->attr.access == ACCESS_UNKNOWN)
7623 gensym->attr.access = sym->attr.access;
7625 /* See if the derived type was labeled as bind(c). */
7626 if (attr.is_bind_c != 0)
7627 sym->attr.is_bind_c = attr.is_bind_c;
7629 /* Construct the f2k_derived namespace if it is not yet there. */
7630 if (!sym->f2k_derived)
7631 sym->f2k_derived = gfc_get_namespace (NULL, 0);
7633 if (extended && !sym->components)
7638 /* Add the extended derived type as the first component. */
7639 gfc_add_component (sym, parent, &p);
7641 gfc_set_sym_referenced (extended);
7643 p->ts.type = BT_DERIVED;
7644 p->ts.u.derived = extended;
7645 p->initializer = gfc_default_initializer (&p->ts);
7647 /* Set extension level. */
7648 if (extended->attr.extension == 255)
7650 /* Since the extension field is 8 bit wide, we can only have
7651 up to 255 extension levels. */
7652 gfc_error ("Maximum extension level reached with type '%s' at %L",
7653 extended->name, &extended->declared_at);
7656 sym->attr.extension = extended->attr.extension + 1;
7658 /* Provide the links between the extended type and its extension. */
7659 if (!extended->f2k_derived)
7660 extended->f2k_derived = gfc_get_namespace (NULL, 0);
7661 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7665 if (!sym->hash_value)
7666 /* Set the hash for the compound name for this type. */
7667 sym->hash_value = gfc_hash_value (sym);
7669 /* Take over the ABSTRACT attribute. */
7670 sym->attr.abstract = attr.abstract;
7672 gfc_new_block = sym;
7678 /* Cray Pointees can be declared as:
7679 pointer (ipt, a (n,m,...,*)) */
7682 gfc_mod_pointee_as (gfc_array_spec *as)
7684 as->cray_pointee = true; /* This will be useful to know later. */
7685 if (as->type == AS_ASSUMED_SIZE)
7686 as->cp_was_assumed = true;
7687 else if (as->type == AS_ASSUMED_SHAPE)
7689 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7696 /* Match the enum definition statement, here we are trying to match
7697 the first line of enum definition statement.
7698 Returns MATCH_YES if match is found. */
7701 gfc_match_enum (void)
7705 m = gfc_match_eos ();
7709 if (gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C")
7717 /* Returns an initializer whose value is one higher than the value of the
7718 LAST_INITIALIZER argument. If the argument is NULL, the
7719 initializers value will be set to zero. The initializer's kind
7720 will be set to gfc_c_int_kind.
7722 If -fshort-enums is given, the appropriate kind will be selected
7723 later after all enumerators have been parsed. A warning is issued
7724 here if an initializer exceeds gfc_c_int_kind. */
7727 enum_initializer (gfc_expr *last_initializer, locus where)
7730 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7732 mpz_init (result->value.integer);
7734 if (last_initializer != NULL)
7736 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7737 result->where = last_initializer->where;
7739 if (gfc_check_integer_range (result->value.integer,
7740 gfc_c_int_kind) != ARITH_OK)
7742 gfc_error ("Enumerator exceeds the C integer type at %C");
7748 /* Control comes here, if it's the very first enumerator and no
7749 initializer has been given. It will be initialized to zero. */
7750 mpz_set_si (result->value.integer, 0);
7757 /* Match a variable name with an optional initializer. When this
7758 subroutine is called, a variable is expected to be parsed next.
7759 Depending on what is happening at the moment, updates either the
7760 symbol table or the current interface. */
7763 enumerator_decl (void)
7765 char name[GFC_MAX_SYMBOL_LEN + 1];
7766 gfc_expr *initializer;
7767 gfc_array_spec *as = NULL;
7775 old_locus = gfc_current_locus;
7777 /* When we get here, we've just matched a list of attributes and
7778 maybe a type and a double colon. The next thing we expect to see
7779 is the name of the symbol. */
7780 m = gfc_match_name (name);
7784 var_locus = gfc_current_locus;
7786 /* OK, we've successfully matched the declaration. Now put the
7787 symbol in the current namespace. If we fail to create the symbol,
7789 if (build_sym (name, NULL, false, &as, &var_locus) == FAILURE)
7795 /* The double colon must be present in order to have initializers.
7796 Otherwise the statement is ambiguous with an assignment statement. */
7799 if (gfc_match_char ('=') == MATCH_YES)
7801 m = gfc_match_init_expr (&initializer);
7804 gfc_error ("Expected an initialization expression at %C");
7813 /* If we do not have an initializer, the initialization value of the
7814 previous enumerator (stored in last_initializer) is incremented
7815 by 1 and is used to initialize the current enumerator. */
7816 if (initializer == NULL)
7817 initializer = enum_initializer (last_initializer, old_locus);
7819 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7821 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7827 /* Store this current initializer, for the next enumerator variable
7828 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7829 use last_initializer below. */
7830 last_initializer = initializer;
7831 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7833 /* Maintain enumerator history. */
7834 gfc_find_symbol (name, NULL, 0, &sym);
7835 create_enum_history (sym, last_initializer);
7837 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
7840 /* Free stuff up and return. */
7841 gfc_free_expr (initializer);
7847 /* Match the enumerator definition statement. */
7850 gfc_match_enumerator_def (void)
7855 gfc_clear_ts (¤t_ts);
7857 m = gfc_match (" enumerator");
7861 m = gfc_match (" :: ");
7862 if (m == MATCH_ERROR)
7865 colon_seen = (m == MATCH_YES);
7867 if (gfc_current_state () != COMP_ENUM)
7869 gfc_error ("ENUM definition statement expected before %C");
7870 gfc_free_enum_history ();
7874 (¤t_ts)->type = BT_INTEGER;
7875 (¤t_ts)->kind = gfc_c_int_kind;
7877 gfc_clear_attr (¤t_attr);
7878 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
7887 m = enumerator_decl ();
7888 if (m == MATCH_ERROR)
7890 gfc_free_enum_history ();
7896 if (gfc_match_eos () == MATCH_YES)
7898 if (gfc_match_char (',') != MATCH_YES)
7902 if (gfc_current_state () == COMP_ENUM)
7904 gfc_free_enum_history ();
7905 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7910 gfc_free_array_spec (current_as);
7917 /* Match binding attributes. */
7920 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7922 bool found_passing = false;
7923 bool seen_ptr = false;
7924 match m = MATCH_YES;
7926 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
7927 this case the defaults are in there. */
7928 ba->access = ACCESS_UNKNOWN;
7929 ba->pass_arg = NULL;
7930 ba->pass_arg_num = 0;
7932 ba->non_overridable = 0;
7936 /* If we find a comma, we believe there are binding attributes. */
7937 m = gfc_match_char (',');
7943 /* Access specifier. */
7945 m = gfc_match (" public");
7946 if (m == MATCH_ERROR)
7950 if (ba->access != ACCESS_UNKNOWN)
7952 gfc_error ("Duplicate access-specifier at %C");
7956 ba->access = ACCESS_PUBLIC;
7960 m = gfc_match (" private");
7961 if (m == MATCH_ERROR)
7965 if (ba->access != ACCESS_UNKNOWN)
7967 gfc_error ("Duplicate access-specifier at %C");
7971 ba->access = ACCESS_PRIVATE;
7975 /* If inside GENERIC, the following is not allowed. */
7980 m = gfc_match (" nopass");
7981 if (m == MATCH_ERROR)
7987 gfc_error ("Binding attributes already specify passing,"
7988 " illegal NOPASS at %C");
7992 found_passing = true;
7997 /* PASS possibly including argument. */
7998 m = gfc_match (" pass");
7999 if (m == MATCH_ERROR)
8003 char arg[GFC_MAX_SYMBOL_LEN + 1];
8007 gfc_error ("Binding attributes already specify passing,"
8008 " illegal PASS at %C");
8012 m = gfc_match (" ( %n )", arg);
8013 if (m == MATCH_ERROR)
8016 ba->pass_arg = gfc_get_string (arg);
8017 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
8019 found_passing = true;
8027 m = gfc_match (" pointer");
8028 if (m == MATCH_ERROR)
8034 gfc_error ("Duplicate POINTER attribute at %C");
8044 /* NON_OVERRIDABLE flag. */
8045 m = gfc_match (" non_overridable");
8046 if (m == MATCH_ERROR)
8050 if (ba->non_overridable)
8052 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8056 ba->non_overridable = 1;
8060 /* DEFERRED flag. */
8061 m = gfc_match (" deferred");
8062 if (m == MATCH_ERROR)
8068 gfc_error ("Duplicate DEFERRED at %C");
8079 /* Nothing matching found. */
8081 gfc_error ("Expected access-specifier at %C");
8083 gfc_error ("Expected binding attribute at %C");
8086 while (gfc_match_char (',') == MATCH_YES);
8088 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8089 if (ba->non_overridable && ba->deferred)
8091 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8098 if (ba->access == ACCESS_UNKNOWN)
8099 ba->access = gfc_typebound_default_access;
8101 if (ppc && !seen_ptr)
8103 gfc_error ("POINTER attribute is required for procedure pointer component"
8115 /* Match a PROCEDURE specific binding inside a derived type. */
8118 match_procedure_in_type (void)
8120 char name[GFC_MAX_SYMBOL_LEN + 1];
8121 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
8122 char* target = NULL, *ifc = NULL;
8123 gfc_typebound_proc tb;
8132 /* Check current state. */
8133 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8134 block = gfc_state_stack->previous->sym;
8137 /* Try to match PROCEDURE(interface). */
8138 if (gfc_match (" (") == MATCH_YES)
8140 m = gfc_match_name (target_buf);
8141 if (m == MATCH_ERROR)
8145 gfc_error ("Interface-name expected after '(' at %C");
8149 if (gfc_match (" )") != MATCH_YES)
8151 gfc_error ("')' expected at %C");
8158 /* Construct the data structure. */
8159 memset (&tb, 0, sizeof (tb));
8160 tb.where = gfc_current_locus;
8162 /* Match binding attributes. */
8163 m = match_binding_attributes (&tb, false, false);
8164 if (m == MATCH_ERROR)
8166 seen_attrs = (m == MATCH_YES);
8168 /* Check that attribute DEFERRED is given if an interface is specified. */
8169 if (tb.deferred && !ifc)
8171 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8174 if (ifc && !tb.deferred)
8176 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8180 /* Match the colons. */
8181 m = gfc_match (" ::");
8182 if (m == MATCH_ERROR)
8184 seen_colons = (m == MATCH_YES);
8185 if (seen_attrs && !seen_colons)
8187 gfc_error ("Expected '::' after binding-attributes at %C");
8191 /* Match the binding names. */
8194 m = gfc_match_name (name);
8195 if (m == MATCH_ERROR)
8199 gfc_error ("Expected binding name at %C");
8203 if (num>1 && gfc_notify_std (GFC_STD_F2008, "PROCEDURE list"
8204 " at %C") == FAILURE)
8207 /* Try to match the '=> target', if it's there. */
8209 m = gfc_match (" =>");
8210 if (m == MATCH_ERROR)
8216 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
8222 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
8227 m = gfc_match_name (target_buf);
8228 if (m == MATCH_ERROR)
8232 gfc_error ("Expected binding target after '=>' at %C");
8235 target = target_buf;
8238 /* If no target was found, it has the same name as the binding. */
8242 /* Get the namespace to insert the symbols into. */
8243 ns = block->f2k_derived;
8246 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8247 if (tb.deferred && !block->attr.abstract)
8249 gfc_error ("Type '%s' containing DEFERRED binding at %C "
8250 "is not ABSTRACT", block->name);
8254 /* See if we already have a binding with this name in the symtree which
8255 would be an error. If a GENERIC already targetted this binding, it may
8256 be already there but then typebound is still NULL. */
8257 stree = gfc_find_symtree (ns->tb_sym_root, name);
8258 if (stree && stree->n.tb)
8260 gfc_error ("There is already a procedure with binding name '%s' for "
8261 "the derived type '%s' at %C", name, block->name);
8265 /* Insert it and set attributes. */
8269 stree = gfc_new_symtree (&ns->tb_sym_root, name);
8272 stree->n.tb = gfc_get_typebound_proc (&tb);
8274 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8277 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
8279 if (gfc_match_eos () == MATCH_YES)
8281 if (gfc_match_char (',') != MATCH_YES)
8286 gfc_error ("Syntax error in PROCEDURE statement at %C");
8291 /* Match a GENERIC procedure binding inside a derived type. */
8294 gfc_match_generic (void)
8296 char name[GFC_MAX_SYMBOL_LEN + 1];
8297 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
8299 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8300 gfc_typebound_proc* tb;
8302 interface_type op_type;
8303 gfc_intrinsic_op op;
8306 /* Check current state. */
8307 if (gfc_current_state () == COMP_DERIVED)
8309 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8312 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8314 block = gfc_state_stack->previous->sym;
8315 ns = block->f2k_derived;
8316 gcc_assert (block && ns);
8318 memset (&tbattr, 0, sizeof (tbattr));
8319 tbattr.where = gfc_current_locus;
8321 /* See if we get an access-specifier. */
8322 m = match_binding_attributes (&tbattr, true, false);
8323 if (m == MATCH_ERROR)
8326 /* Now the colons, those are required. */
8327 if (gfc_match (" ::") != MATCH_YES)
8329 gfc_error ("Expected '::' at %C");
8333 /* Match the binding name; depending on type (operator / generic) format
8334 it for future error messages into bind_name. */
8336 m = gfc_match_generic_spec (&op_type, name, &op);
8337 if (m == MATCH_ERROR)
8341 gfc_error ("Expected generic name or operator descriptor at %C");
8347 case INTERFACE_GENERIC:
8348 snprintf (bind_name, sizeof (bind_name), "%s", name);
8351 case INTERFACE_USER_OP:
8352 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8355 case INTERFACE_INTRINSIC_OP:
8356 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8357 gfc_op2string (op));
8364 /* Match the required =>. */
8365 if (gfc_match (" =>") != MATCH_YES)
8367 gfc_error ("Expected '=>' at %C");
8371 /* Try to find existing GENERIC binding with this name / for this operator;
8372 if there is something, check that it is another GENERIC and then extend
8373 it rather than building a new node. Otherwise, create it and put it
8374 at the right position. */
8378 case INTERFACE_USER_OP:
8379 case INTERFACE_GENERIC:
8381 const bool is_op = (op_type == INTERFACE_USER_OP);
8384 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8396 case INTERFACE_INTRINSIC_OP:
8406 if (!tb->is_generic)
8408 gcc_assert (op_type == INTERFACE_GENERIC);
8409 gfc_error ("There's already a non-generic procedure with binding name"
8410 " '%s' for the derived type '%s' at %C",
8411 bind_name, block->name);
8415 if (tb->access != tbattr.access)
8417 gfc_error ("Binding at %C must have the same access as already"
8418 " defined binding '%s'", bind_name);
8424 tb = gfc_get_typebound_proc (NULL);
8425 tb->where = gfc_current_locus;
8426 tb->access = tbattr.access;
8428 tb->u.generic = NULL;
8432 case INTERFACE_GENERIC:
8433 case INTERFACE_USER_OP:
8435 const bool is_op = (op_type == INTERFACE_USER_OP);
8438 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8446 case INTERFACE_INTRINSIC_OP:
8455 /* Now, match all following names as specific targets. */
8458 gfc_symtree* target_st;
8459 gfc_tbp_generic* target;
8461 m = gfc_match_name (name);
8462 if (m == MATCH_ERROR)
8466 gfc_error ("Expected specific binding name at %C");
8470 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8472 /* See if this is a duplicate specification. */
8473 for (target = tb->u.generic; target; target = target->next)
8474 if (target_st == target->specific_st)
8476 gfc_error ("'%s' already defined as specific binding for the"
8477 " generic '%s' at %C", name, bind_name);
8481 target = gfc_get_tbp_generic ();
8482 target->specific_st = target_st;
8483 target->specific = NULL;
8484 target->next = tb->u.generic;
8485 target->is_operator = ((op_type == INTERFACE_USER_OP)
8486 || (op_type == INTERFACE_INTRINSIC_OP));
8487 tb->u.generic = target;
8489 while (gfc_match (" ,") == MATCH_YES);
8491 /* Here should be the end. */
8492 if (gfc_match_eos () != MATCH_YES)
8494 gfc_error ("Junk after GENERIC binding at %C");
8505 /* Match a FINAL declaration inside a derived type. */
8508 gfc_match_final_decl (void)
8510 char name[GFC_MAX_SYMBOL_LEN + 1];
8513 gfc_namespace* module_ns;
8517 if (gfc_current_form == FORM_FREE)
8519 char c = gfc_peek_ascii_char ();
8520 if (!gfc_is_whitespace (c) && c != ':')
8524 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8526 if (gfc_current_form == FORM_FIXED)
8529 gfc_error ("FINAL declaration at %C must be inside a derived type "
8530 "CONTAINS section");
8534 block = gfc_state_stack->previous->sym;
8537 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8538 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8540 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8541 " specification part of a MODULE");
8545 module_ns = gfc_current_ns;
8546 gcc_assert (module_ns);
8547 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8549 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8550 if (gfc_match (" ::") == MATCH_ERROR)
8553 /* Match the sequence of procedure names. */
8560 if (first && gfc_match_eos () == MATCH_YES)
8562 gfc_error ("Empty FINAL at %C");
8566 m = gfc_match_name (name);
8569 gfc_error ("Expected module procedure name at %C");
8572 else if (m != MATCH_YES)
8575 if (gfc_match_eos () == MATCH_YES)
8577 if (!last && gfc_match_char (',') != MATCH_YES)
8579 gfc_error ("Expected ',' at %C");
8583 if (gfc_get_symbol (name, module_ns, &sym))
8585 gfc_error ("Unknown procedure name \"%s\" at %C", name);
8589 /* Mark the symbol as module procedure. */
8590 if (sym->attr.proc != PROC_MODULE
8591 && gfc_add_procedure (&sym->attr, PROC_MODULE,
8592 sym->name, NULL) == FAILURE)
8595 /* Check if we already have this symbol in the list, this is an error. */
8596 for (f = block->f2k_derived->finalizers; f; f = f->next)
8597 if (f->proc_sym == sym)
8599 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8604 /* Add this symbol to the list of finalizers. */
8605 gcc_assert (block->f2k_derived);
8607 f = XCNEW (gfc_finalizer);
8609 f->proc_tree = NULL;
8610 f->where = gfc_current_locus;
8611 f->next = block->f2k_derived->finalizers;
8612 block->f2k_derived->finalizers = f;
8622 const ext_attr_t ext_attr_list[] = {
8623 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8624 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8625 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
8626 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
8627 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
8628 { NULL, EXT_ATTR_LAST, NULL }
8631 /* Match a !GCC$ ATTRIBUTES statement of the form:
8632 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8633 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8635 TODO: We should support all GCC attributes using the same syntax for
8636 the attribute list, i.e. the list in C
8637 __attributes(( attribute-list ))
8639 !GCC$ ATTRIBUTES attribute-list ::
8640 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8643 As there is absolutely no risk of confusion, we should never return
8646 gfc_match_gcc_attributes (void)
8648 symbol_attribute attr;
8649 char name[GFC_MAX_SYMBOL_LEN + 1];
8654 gfc_clear_attr (&attr);
8659 if (gfc_match_name (name) != MATCH_YES)
8662 for (id = 0; id < EXT_ATTR_LAST; id++)
8663 if (strcmp (name, ext_attr_list[id].name) == 0)
8666 if (id == EXT_ATTR_LAST)
8668 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8672 if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
8676 gfc_gobble_whitespace ();
8677 ch = gfc_next_ascii_char ();
8680 /* This is the successful exit condition for the loop. */
8681 if (gfc_next_ascii_char () == ':')
8691 if (gfc_match_eos () == MATCH_YES)
8696 m = gfc_match_name (name);
8700 if (find_special (name, &sym, true))
8703 sym->attr.ext_attr |= attr.ext_attr;
8705 if (gfc_match_eos () == MATCH_YES)
8708 if (gfc_match_char (',') != MATCH_YES)
8715 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");