1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
28 #include "constructor.h"
30 /* Macros to access allocate memory for gfc_data_variable,
31 gfc_data_value and gfc_data. */
32 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
33 #define gfc_get_data_value() XCNEW (gfc_data_value)
34 #define gfc_get_data() XCNEW (gfc_data)
37 /* This flag is set if an old-style length selector is matched
38 during a type-declaration statement. */
40 static int old_char_selector;
42 /* When variables acquire types and attributes from a declaration
43 statement, they get them from the following static variables. The
44 first part of a declaration sets these variables and the second
45 part copies these into symbol structures. */
47 static gfc_typespec current_ts;
49 static symbol_attribute current_attr;
50 static gfc_array_spec *current_as;
51 static int colon_seen;
53 /* The current binding label (if any). */
54 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
55 /* Need to know how many identifiers are on the current data declaration
56 line in case we're given the BIND(C) attribute with a NAME= specifier. */
57 static int num_idents_on_line;
58 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
59 can supply a name if the curr_binding_label is nil and NAME= was not. */
60 static int has_name_equals = 0;
62 /* Initializer of the previous enumerator. */
64 static gfc_expr *last_initializer;
66 /* History of all the enumerators is maintained, so that
67 kind values of all the enumerators could be updated depending
68 upon the maximum initialized value. */
70 typedef struct enumerator_history
73 gfc_expr *initializer;
74 struct enumerator_history *next;
78 /* Header of enum history chain. */
80 static enumerator_history *enum_history = NULL;
82 /* Pointer of enum history node containing largest initializer. */
84 static enumerator_history *max_enum = NULL;
86 /* gfc_new_block points to the symbol of a newly matched block. */
88 gfc_symbol *gfc_new_block;
90 bool gfc_matching_function;
93 /********************* DATA statement subroutines *********************/
95 static bool in_match_data = false;
98 gfc_in_match_data (void)
100 return in_match_data;
104 set_in_match_data (bool set_value)
106 in_match_data = set_value;
109 /* Free a gfc_data_variable structure and everything beneath it. */
112 free_variable (gfc_data_variable *p)
114 gfc_data_variable *q;
119 gfc_free_expr (p->expr);
120 gfc_free_iterator (&p->iter, 0);
121 free_variable (p->list);
127 /* Free a gfc_data_value structure and everything beneath it. */
130 free_value (gfc_data_value *p)
137 mpz_clear (p->repeat);
138 gfc_free_expr (p->expr);
144 /* Free a list of gfc_data structures. */
147 gfc_free_data (gfc_data *p)
154 free_variable (p->var);
155 free_value (p->value);
161 /* Free all data in a namespace. */
164 gfc_free_data_all (gfc_namespace *ns)
177 static match var_element (gfc_data_variable *);
179 /* Match a list of variables terminated by an iterator and a right
183 var_list (gfc_data_variable *parent)
185 gfc_data_variable *tail, var;
188 m = var_element (&var);
189 if (m == MATCH_ERROR)
194 tail = gfc_get_data_variable ();
201 if (gfc_match_char (',') != MATCH_YES)
204 m = gfc_match_iterator (&parent->iter, 1);
207 if (m == MATCH_ERROR)
210 m = var_element (&var);
211 if (m == MATCH_ERROR)
216 tail->next = gfc_get_data_variable ();
222 if (gfc_match_char (')') != MATCH_YES)
227 gfc_syntax_error (ST_DATA);
232 /* Match a single element in a data variable list, which can be a
233 variable-iterator list. */
236 var_element (gfc_data_variable *new_var)
241 memset (new_var, 0, sizeof (gfc_data_variable));
243 if (gfc_match_char ('(') == MATCH_YES)
244 return var_list (new_var);
246 m = gfc_match_variable (&new_var->expr, 0);
250 sym = new_var->expr->symtree->n.sym;
252 /* Symbol should already have an associated type. */
253 if (gfc_check_symbol_typed (sym, gfc_current_ns,
254 false, gfc_current_locus) == FAILURE)
257 if (!sym->attr.function && gfc_current_ns->parent
258 && gfc_current_ns->parent == sym->ns)
260 gfc_error ("Host associated variable '%s' may not be in the DATA "
261 "statement at %C", sym->name);
265 if (gfc_current_state () != COMP_BLOCK_DATA
266 && sym->attr.in_common
267 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
268 "common block variable '%s' in DATA statement at %C",
269 sym->name) == FAILURE)
272 if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
279 /* Match the top-level list of data variables. */
282 top_var_list (gfc_data *d)
284 gfc_data_variable var, *tail, *new_var;
291 m = var_element (&var);
294 if (m == MATCH_ERROR)
297 new_var = gfc_get_data_variable ();
303 tail->next = new_var;
307 if (gfc_match_char ('/') == MATCH_YES)
309 if (gfc_match_char (',') != MATCH_YES)
316 gfc_syntax_error (ST_DATA);
317 gfc_free_data_all (gfc_current_ns);
323 match_data_constant (gfc_expr **result)
325 char name[GFC_MAX_SYMBOL_LEN + 1];
331 m = gfc_match_literal_constant (&expr, 1);
338 if (m == MATCH_ERROR)
341 m = gfc_match_null (result);
345 old_loc = gfc_current_locus;
347 /* Should this be a structure component, try to match it
348 before matching a name. */
349 m = gfc_match_rvalue (result);
350 if (m == MATCH_ERROR)
353 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
355 if (gfc_simplify_expr (*result, 0) == FAILURE)
360 gfc_current_locus = old_loc;
362 m = gfc_match_name (name);
366 if (gfc_find_symbol (name, NULL, 1, &sym))
370 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
372 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
376 else if (sym->attr.flavor == FL_DERIVED)
377 return gfc_match_structure_constructor (sym, result, false);
379 /* Check to see if the value is an initialization array expression. */
380 if (sym->value->expr_type == EXPR_ARRAY)
382 gfc_current_locus = old_loc;
384 m = gfc_match_init_expr (result);
385 if (m == MATCH_ERROR)
390 if (gfc_simplify_expr (*result, 0) == FAILURE)
393 if ((*result)->expr_type == EXPR_CONSTANT)
397 gfc_error ("Invalid initializer %s in Data statement at %C", name);
403 *result = gfc_copy_expr (sym->value);
408 /* Match a list of values in a DATA statement. The leading '/' has
409 already been seen at this point. */
412 top_val_list (gfc_data *data)
414 gfc_data_value *new_val, *tail;
422 m = match_data_constant (&expr);
425 if (m == MATCH_ERROR)
428 new_val = gfc_get_data_value ();
429 mpz_init (new_val->repeat);
432 data->value = new_val;
434 tail->next = new_val;
438 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
441 mpz_set_ui (tail->repeat, 1);
445 if (expr->ts.type == BT_INTEGER)
446 mpz_set (tail->repeat, expr->value.integer);
447 gfc_free_expr (expr);
449 m = match_data_constant (&tail->expr);
452 if (m == MATCH_ERROR)
456 if (gfc_match_char ('/') == MATCH_YES)
458 if (gfc_match_char (',') == MATCH_NO)
465 gfc_syntax_error (ST_DATA);
466 gfc_free_data_all (gfc_current_ns);
471 /* Matches an old style initialization. */
474 match_old_style_init (const char *name)
481 /* Set up data structure to hold initializers. */
482 gfc_find_sym_tree (name, NULL, 0, &st);
485 newdata = gfc_get_data ();
486 newdata->var = gfc_get_data_variable ();
487 newdata->var->expr = gfc_get_variable_expr (st);
488 newdata->where = gfc_current_locus;
490 /* Match initial value list. This also eats the terminal '/'. */
491 m = top_val_list (newdata);
500 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
505 if (gfc_implicit_pure (NULL))
506 gfc_current_ns->proc_name->attr.implicit_pure = 0;
508 /* Mark the variable as having appeared in a data statement. */
509 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
515 /* Chain in namespace list of DATA initializers. */
516 newdata->next = gfc_current_ns->data;
517 gfc_current_ns->data = newdata;
523 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
524 we are matching a DATA statement and are therefore issuing an error
525 if we encounter something unexpected, if not, we're trying to match
526 an old-style initialization expression of the form INTEGER I /2/. */
529 gfc_match_data (void)
534 set_in_match_data (true);
538 new_data = gfc_get_data ();
539 new_data->where = gfc_current_locus;
541 m = top_var_list (new_data);
545 m = top_val_list (new_data);
549 new_data->next = gfc_current_ns->data;
550 gfc_current_ns->data = new_data;
552 if (gfc_match_eos () == MATCH_YES)
555 gfc_match_char (','); /* Optional comma */
558 set_in_match_data (false);
562 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
566 if (gfc_implicit_pure (NULL))
567 gfc_current_ns->proc_name->attr.implicit_pure = 0;
572 set_in_match_data (false);
573 gfc_free_data (new_data);
578 /************************ Declaration statements *********************/
581 /* Auxilliary function to merge DIMENSION and CODIMENSION array specs. */
584 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
588 if (to->rank == 0 && from->rank > 0)
590 to->rank = from->rank;
591 to->type = from->type;
592 to->cray_pointee = from->cray_pointee;
593 to->cp_was_assumed = from->cp_was_assumed;
595 for (i = 0; i < to->corank; i++)
597 to->lower[from->rank + i] = to->lower[i];
598 to->upper[from->rank + i] = to->upper[i];
600 for (i = 0; i < from->rank; i++)
604 to->lower[i] = gfc_copy_expr (from->lower[i]);
605 to->upper[i] = gfc_copy_expr (from->upper[i]);
609 to->lower[i] = from->lower[i];
610 to->upper[i] = from->upper[i];
614 else if (to->corank == 0 && from->corank > 0)
616 to->corank = from->corank;
617 to->cotype = from->cotype;
619 for (i = 0; i < from->corank; i++)
623 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
624 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
628 to->lower[to->rank + i] = from->lower[i];
629 to->upper[to->rank + i] = from->upper[i];
636 /* Match an intent specification. Since this can only happen after an
637 INTENT word, a legal intent-spec must follow. */
640 match_intent_spec (void)
643 if (gfc_match (" ( in out )") == MATCH_YES)
645 if (gfc_match (" ( in )") == MATCH_YES)
647 if (gfc_match (" ( out )") == MATCH_YES)
650 gfc_error ("Bad INTENT specification at %C");
651 return INTENT_UNKNOWN;
655 /* Matches a character length specification, which is either a
656 specification expression, '*', or ':'. */
659 char_len_param_value (gfc_expr **expr, bool *deferred)
666 if (gfc_match_char ('*') == MATCH_YES)
669 if (gfc_match_char (':') == MATCH_YES)
671 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: deferred type "
672 "parameter at %C") == FAILURE)
680 m = gfc_match_expr (expr);
683 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
686 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
688 if ((*expr)->value.function.actual
689 && (*expr)->value.function.actual->expr->symtree)
692 e = (*expr)->value.function.actual->expr;
693 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
694 && e->expr_type == EXPR_VARIABLE)
696 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
698 if (e->symtree->n.sym->ts.type == BT_CHARACTER
699 && e->symtree->n.sym->ts.u.cl
700 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
708 gfc_error ("Conflict in attributes of function argument at %C");
713 /* A character length is a '*' followed by a literal integer or a
714 char_len_param_value in parenthesis. */
717 match_char_length (gfc_expr **expr, bool *deferred)
723 m = gfc_match_char ('*');
727 m = gfc_match_small_literal_int (&length, NULL);
728 if (m == MATCH_ERROR)
733 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
734 "Old-style character length at %C") == FAILURE)
736 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
740 if (gfc_match_char ('(') == MATCH_NO)
743 m = char_len_param_value (expr, deferred);
744 if (m != MATCH_YES && gfc_matching_function)
750 if (m == MATCH_ERROR)
755 if (gfc_match_char (')') == MATCH_NO)
757 gfc_free_expr (*expr);
765 gfc_error ("Syntax error in character length specification at %C");
770 /* Special subroutine for finding a symbol. Check if the name is found
771 in the current name space. If not, and we're compiling a function or
772 subroutine and the parent compilation unit is an interface, then check
773 to see if the name we've been given is the name of the interface
774 (located in another namespace). */
777 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
783 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
786 *result = st ? st->n.sym : NULL;
790 if (gfc_current_state () != COMP_SUBROUTINE
791 && gfc_current_state () != COMP_FUNCTION)
794 s = gfc_state_stack->previous;
798 if (s->state != COMP_INTERFACE)
801 goto end; /* Nameless interface. */
803 if (strcmp (name, s->sym->name) == 0)
814 /* Special subroutine for getting a symbol node associated with a
815 procedure name, used in SUBROUTINE and FUNCTION statements. The
816 symbol is created in the parent using with symtree node in the
817 child unit pointing to the symbol. If the current namespace has no
818 parent, then the symbol is just created in the current unit. */
821 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
827 /* Module functions have to be left in their own namespace because
828 they have potentially (almost certainly!) already been referenced.
829 In this sense, they are rather like external functions. This is
830 fixed up in resolve.c(resolve_entries), where the symbol name-
831 space is set to point to the master function, so that the fake
832 result mechanism can work. */
833 if (module_fcn_entry)
835 /* Present if entry is declared to be a module procedure. */
836 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
839 rc = gfc_get_symbol (name, NULL, result);
840 else if (!gfc_get_symbol (name, NULL, &sym) && sym
841 && (*result)->ts.type == BT_UNKNOWN
842 && sym->attr.flavor == FL_UNKNOWN)
843 /* Pick up the typespec for the entry, if declared in the function
844 body. Note that this symbol is FL_UNKNOWN because it will
845 only have appeared in a type declaration. The local symtree
846 is set to point to the module symbol and a unique symtree
847 to the local version. This latter ensures a correct clearing
850 /* If the ENTRY proceeds its specification, we need to ensure
851 that this does not raise a "has no IMPLICIT type" error. */
852 if (sym->ts.type == BT_UNKNOWN)
853 sym->attr.untyped = 1;
855 (*result)->ts = sym->ts;
857 /* Put the symbol in the procedure namespace so that, should
858 the ENTRY precede its specification, the specification
860 (*result)->ns = gfc_current_ns;
862 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
864 st = gfc_get_unique_symtree (gfc_current_ns);
869 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
875 gfc_current_ns->refs++;
877 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
879 /* Trap another encompassed procedure with the same name. All
880 these conditions are necessary to avoid picking up an entry
881 whose name clashes with that of the encompassing procedure;
882 this is handled using gsymbols to register unique,globally
884 if (sym->attr.flavor != 0
885 && sym->attr.proc != 0
886 && (sym->attr.subroutine || sym->attr.function)
887 && sym->attr.if_source != IFSRC_UNKNOWN)
888 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
889 name, &sym->declared_at);
891 /* Trap a procedure with a name the same as interface in the
892 encompassing scope. */
893 if (sym->attr.generic != 0
894 && (sym->attr.subroutine || sym->attr.function)
895 && !sym->attr.mod_proc)
896 gfc_error_now ("Name '%s' at %C is already defined"
897 " as a generic interface at %L",
898 name, &sym->declared_at);
900 /* Trap declarations of attributes in encompassing scope. The
901 signature for this is that ts.kind is set. Legitimate
902 references only set ts.type. */
903 if (sym->ts.kind != 0
904 && !sym->attr.implicit_type
905 && sym->attr.proc == 0
906 && gfc_current_ns->parent != NULL
907 && sym->attr.access == 0
908 && !module_fcn_entry)
909 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
910 "and must not have attributes declared at %L",
911 name, &sym->declared_at);
914 if (gfc_current_ns->parent == NULL || *result == NULL)
917 /* Module function entries will already have a symtree in
918 the current namespace but will need one at module level. */
919 if (module_fcn_entry)
921 /* Present if entry is declared to be a module procedure. */
922 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
924 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
927 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
932 /* See if the procedure should be a module procedure. */
934 if (((sym->ns->proc_name != NULL
935 && sym->ns->proc_name->attr.flavor == FL_MODULE
936 && sym->attr.proc != PROC_MODULE)
937 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
938 && gfc_add_procedure (&sym->attr, PROC_MODULE,
939 sym->name, NULL) == FAILURE)
946 /* Verify that the given symbol representing a parameter is C
947 interoperable, by checking to see if it was marked as such after
948 its declaration. If the given symbol is not interoperable, a
949 warning is reported, thus removing the need to return the status to
950 the calling function. The standard does not require the user use
951 one of the iso_c_binding named constants to declare an
952 interoperable parameter, but we can't be sure if the param is C
953 interop or not if the user doesn't. For example, integer(4) may be
954 legal Fortran, but doesn't have meaning in C. It may interop with
955 a number of the C types, which causes a problem because the
956 compiler can't know which one. This code is almost certainly not
957 portable, and the user will get what they deserve if the C type
958 across platforms isn't always interoperable with integer(4). If
959 the user had used something like integer(c_int) or integer(c_long),
960 the compiler could have automatically handled the varying sizes
964 verify_c_interop_param (gfc_symbol *sym)
966 int is_c_interop = 0;
967 gfc_try retval = SUCCESS;
969 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
970 Don't repeat the checks here. */
971 if (sym->attr.implicit_type)
974 /* For subroutines or functions that are passed to a BIND(C) procedure,
975 they're interoperable if they're BIND(C) and their params are all
977 if (sym->attr.flavor == FL_PROCEDURE)
979 if (sym->attr.is_bind_c == 0)
981 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
982 "attribute to be C interoperable", sym->name,
983 &(sym->declared_at));
989 if (sym->attr.is_c_interop == 1)
990 /* We've already checked this procedure; don't check it again. */
993 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
998 /* See if we've stored a reference to a procedure that owns sym. */
999 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1001 if (sym->ns->proc_name->attr.is_bind_c == 1)
1004 (verify_c_interop (&(sym->ts))
1005 == SUCCESS ? 1 : 0);
1007 if (is_c_interop != 1)
1009 /* Make personalized messages to give better feedback. */
1010 if (sym->ts.type == BT_DERIVED)
1011 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
1012 "procedure '%s' but is not C interoperable "
1013 "because derived type '%s' is not C interoperable",
1014 sym->name, &(sym->declared_at),
1015 sym->ns->proc_name->name,
1016 sym->ts.u.derived->name);
1018 gfc_warning ("Variable '%s' at %L is a parameter to the "
1019 "BIND(C) procedure '%s' but may not be C "
1021 sym->name, &(sym->declared_at),
1022 sym->ns->proc_name->name);
1025 /* Character strings are only C interoperable if they have a
1027 if (sym->ts.type == BT_CHARACTER)
1029 gfc_charlen *cl = sym->ts.u.cl;
1030 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1031 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1033 gfc_error ("Character argument '%s' at %L "
1034 "must be length 1 because "
1035 "procedure '%s' is BIND(C)",
1036 sym->name, &sym->declared_at,
1037 sym->ns->proc_name->name);
1042 /* We have to make sure that any param to a bind(c) routine does
1043 not have the allocatable, pointer, or optional attributes,
1044 according to J3/04-007, section 5.1. */
1045 if (sym->attr.allocatable == 1)
1047 gfc_error ("Variable '%s' at %L cannot have the "
1048 "ALLOCATABLE attribute because procedure '%s'"
1049 " is BIND(C)", sym->name, &(sym->declared_at),
1050 sym->ns->proc_name->name);
1054 if (sym->attr.pointer == 1)
1056 gfc_error ("Variable '%s' at %L cannot have the "
1057 "POINTER attribute because procedure '%s'"
1058 " is BIND(C)", sym->name, &(sym->declared_at),
1059 sym->ns->proc_name->name);
1063 if (sym->attr.optional == 1 && sym->attr.value)
1065 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1066 "and the VALUE attribute because procedure '%s' "
1067 "is BIND(C)", sym->name, &(sym->declared_at),
1068 sym->ns->proc_name->name);
1071 else if (sym->attr.optional == 1
1072 && gfc_notify_std (GFC_STD_F2008_TR, "TR29113: Variable '%s' "
1073 "at %L with OPTIONAL attribute in "
1074 "procedure '%s' which is BIND(C)",
1075 sym->name, &(sym->declared_at),
1076 sym->ns->proc_name->name)
1080 /* Make sure that if it has the dimension attribute, that it is
1081 either assumed size or explicit shape. */
1082 if (sym->as != NULL)
1084 if (sym->as->type == AS_ASSUMED_SHAPE)
1086 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1087 "argument to the procedure '%s' at %L because "
1088 "the procedure is BIND(C)", sym->name,
1089 &(sym->declared_at), sym->ns->proc_name->name,
1090 &(sym->ns->proc_name->declared_at));
1094 if (sym->as->type == AS_DEFERRED)
1096 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1097 "argument to the procedure '%s' at %L because "
1098 "the procedure is BIND(C)", sym->name,
1099 &(sym->declared_at), sym->ns->proc_name->name,
1100 &(sym->ns->proc_name->declared_at));
1112 /* Function called by variable_decl() that adds a name to the symbol table. */
1115 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1116 gfc_array_spec **as, locus *var_locus)
1118 symbol_attribute attr;
1121 if (gfc_get_symbol (name, NULL, &sym))
1124 /* Start updating the symbol table. Add basic type attribute if present. */
1125 if (current_ts.type != BT_UNKNOWN
1126 && (sym->attr.implicit_type == 0
1127 || !gfc_compare_types (&sym->ts, ¤t_ts))
1128 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
1131 if (sym->ts.type == BT_CHARACTER)
1134 sym->ts.deferred = cl_deferred;
1137 /* Add dimension attribute if present. */
1138 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1142 /* Add attribute to symbol. The copy is so that we can reset the
1143 dimension attribute. */
1144 attr = current_attr;
1146 attr.codimension = 0;
1148 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1151 /* Finish any work that may need to be done for the binding label,
1152 if it's a bind(c). The bind(c) attr is found before the symbol
1153 is made, and before the symbol name (for data decls), so the
1154 current_ts is holding the binding label, or nothing if the
1155 name= attr wasn't given. Therefore, test here if we're dealing
1156 with a bind(c) and make sure the binding label is set correctly. */
1157 if (sym->attr.is_bind_c == 1)
1159 if (sym->binding_label[0] == '\0')
1161 /* Set the binding label and verify that if a NAME= was specified
1162 then only one identifier was in the entity-decl-list. */
1163 if (set_binding_label (sym->binding_label, sym->name,
1164 num_idents_on_line) == FAILURE)
1169 /* See if we know we're in a common block, and if it's a bind(c)
1170 common then we need to make sure we're an interoperable type. */
1171 if (sym->attr.in_common == 1)
1173 /* Test the common block object. */
1174 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1175 && sym->ts.is_c_interop != 1)
1177 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1178 "must be declared with a C interoperable "
1179 "kind since common block '%s' is BIND(C)",
1180 sym->name, sym->common_block->name,
1181 sym->common_block->name);
1186 sym->attr.implied_index = 0;
1188 if (sym->ts.type == BT_CLASS)
1189 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1195 /* Set character constant to the given length. The constant will be padded or
1196 truncated. If we're inside an array constructor without a typespec, we
1197 additionally check that all elements have the same length; check_len -1
1198 means no checking. */
1201 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1206 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1207 gcc_assert (expr->ts.type == BT_CHARACTER);
1209 slen = expr->value.character.length;
1212 s = gfc_get_wide_string (len + 1);
1213 memcpy (s, expr->value.character.string,
1214 MIN (len, slen) * sizeof (gfc_char_t));
1216 gfc_wide_memset (&s[slen], ' ', len - slen);
1218 if (gfc_option.warn_character_truncation && slen > len)
1219 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1220 "(%d/%d)", &expr->where, slen, len);
1222 /* Apply the standard by 'hand' otherwise it gets cleared for
1224 if (check_len != -1 && slen != check_len
1225 && !(gfc_option.allow_std & GFC_STD_GNU))
1226 gfc_error_now ("The CHARACTER elements of the array constructor "
1227 "at %L must have the same length (%d/%d)",
1228 &expr->where, slen, check_len);
1231 free (expr->value.character.string);
1232 expr->value.character.string = s;
1233 expr->value.character.length = len;
1238 /* Function to create and update the enumerator history
1239 using the information passed as arguments.
1240 Pointer "max_enum" is also updated, to point to
1241 enum history node containing largest initializer.
1243 SYM points to the symbol node of enumerator.
1244 INIT points to its enumerator value. */
1247 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1249 enumerator_history *new_enum_history;
1250 gcc_assert (sym != NULL && init != NULL);
1252 new_enum_history = XCNEW (enumerator_history);
1254 new_enum_history->sym = sym;
1255 new_enum_history->initializer = init;
1256 new_enum_history->next = NULL;
1258 if (enum_history == NULL)
1260 enum_history = new_enum_history;
1261 max_enum = enum_history;
1265 new_enum_history->next = enum_history;
1266 enum_history = new_enum_history;
1268 if (mpz_cmp (max_enum->initializer->value.integer,
1269 new_enum_history->initializer->value.integer) < 0)
1270 max_enum = new_enum_history;
1275 /* Function to free enum kind history. */
1278 gfc_free_enum_history (void)
1280 enumerator_history *current = enum_history;
1281 enumerator_history *next;
1283 while (current != NULL)
1285 next = current->next;
1290 enum_history = NULL;
1294 /* Function called by variable_decl() that adds an initialization
1295 expression to a symbol. */
1298 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1300 symbol_attribute attr;
1305 if (find_special (name, &sym, false))
1310 /* If this symbol is confirming an implicit parameter type,
1311 then an initialization expression is not allowed. */
1312 if (attr.flavor == FL_PARAMETER
1313 && sym->value != NULL
1316 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1323 /* An initializer is required for PARAMETER declarations. */
1324 if (attr.flavor == FL_PARAMETER)
1326 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1332 /* If a variable appears in a DATA block, it cannot have an
1336 gfc_error ("Variable '%s' at %C with an initializer already "
1337 "appears in a DATA statement", sym->name);
1341 /* Check if the assignment can happen. This has to be put off
1342 until later for derived type variables and procedure pointers. */
1343 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1344 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1345 && !sym->attr.proc_pointer
1346 && gfc_check_assign_symbol (sym, init) == FAILURE)
1349 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1350 && init->ts.type == BT_CHARACTER)
1352 /* Update symbol character length according initializer. */
1353 if (gfc_check_assign_symbol (sym, init) == FAILURE)
1356 if (sym->ts.u.cl->length == NULL)
1359 /* If there are multiple CHARACTER variables declared on the
1360 same line, we don't want them to share the same length. */
1361 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1363 if (sym->attr.flavor == FL_PARAMETER)
1365 if (init->expr_type == EXPR_CONSTANT)
1367 clen = init->value.character.length;
1368 sym->ts.u.cl->length
1369 = gfc_get_int_expr (gfc_default_integer_kind,
1372 else if (init->expr_type == EXPR_ARRAY)
1375 c = gfc_constructor_first (init->value.constructor);
1376 clen = c->expr->value.character.length;
1377 sym->ts.u.cl->length
1378 = gfc_get_int_expr (gfc_default_integer_kind,
1381 else if (init->ts.u.cl && init->ts.u.cl->length)
1382 sym->ts.u.cl->length =
1383 gfc_copy_expr (sym->value->ts.u.cl->length);
1386 /* Update initializer character length according symbol. */
1387 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1389 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1391 if (init->expr_type == EXPR_CONSTANT)
1392 gfc_set_constant_character_len (len, init, -1);
1393 else if (init->expr_type == EXPR_ARRAY)
1397 /* Build a new charlen to prevent simplification from
1398 deleting the length before it is resolved. */
1399 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1400 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1402 for (c = gfc_constructor_first (init->value.constructor);
1403 c; c = gfc_constructor_next (c))
1404 gfc_set_constant_character_len (len, c->expr, -1);
1409 /* If sym is implied-shape, set its upper bounds from init. */
1410 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1411 && sym->as->type == AS_IMPLIED_SHAPE)
1415 if (init->rank == 0)
1417 gfc_error ("Can't initialize implied-shape array at %L"
1418 " with scalar", &sym->declared_at);
1421 gcc_assert (sym->as->rank == init->rank);
1423 /* Shape should be present, we get an initialization expression. */
1424 gcc_assert (init->shape);
1426 for (dim = 0; dim < sym->as->rank; ++dim)
1432 lower = sym->as->lower[dim];
1433 if (lower->expr_type != EXPR_CONSTANT)
1435 gfc_error ("Non-constant lower bound in implied-shape"
1436 " declaration at %L", &lower->where);
1440 /* All dimensions must be without upper bound. */
1441 gcc_assert (!sym->as->upper[dim]);
1444 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1445 mpz_add (e->value.integer,
1446 lower->value.integer, init->shape[dim]);
1447 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1448 sym->as->upper[dim] = e;
1451 sym->as->type = AS_EXPLICIT;
1454 /* Need to check if the expression we initialized this
1455 to was one of the iso_c_binding named constants. If so,
1456 and we're a parameter (constant), let it be iso_c.
1458 integer(c_int), parameter :: my_int = c_int
1459 integer(my_int) :: my_int_2
1460 If we mark my_int as iso_c (since we can see it's value
1461 is equal to one of the named constants), then my_int_2
1462 will be considered C interoperable. */
1463 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1465 sym->ts.is_iso_c |= init->ts.is_iso_c;
1466 sym->ts.is_c_interop |= init->ts.is_c_interop;
1467 /* attr bits needed for module files. */
1468 sym->attr.is_iso_c |= init->ts.is_iso_c;
1469 sym->attr.is_c_interop |= init->ts.is_c_interop;
1470 if (init->ts.is_iso_c)
1471 sym->ts.f90_type = init->ts.f90_type;
1474 /* Add initializer. Make sure we keep the ranks sane. */
1475 if (sym->attr.dimension && init->rank == 0)
1480 if (sym->attr.flavor == FL_PARAMETER
1481 && init->expr_type == EXPR_CONSTANT
1482 && spec_size (sym->as, &size) == SUCCESS
1483 && mpz_cmp_si (size, 0) > 0)
1485 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1487 for (n = 0; n < (int)mpz_get_si (size); n++)
1488 gfc_constructor_append_expr (&array->value.constructor,
1491 : gfc_copy_expr (init),
1494 array->shape = gfc_get_shape (sym->as->rank);
1495 for (n = 0; n < sym->as->rank; n++)
1496 spec_dimen_size (sym->as, n, &array->shape[n]);
1501 init->rank = sym->as->rank;
1505 if (sym->attr.save == SAVE_NONE)
1506 sym->attr.save = SAVE_IMPLICIT;
1514 /* Function called by variable_decl() that adds a name to a structure
1518 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1519 gfc_array_spec **as)
1522 gfc_try t = SUCCESS;
1524 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1525 constructing, it must have the pointer attribute. */
1526 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1527 && current_ts.u.derived == gfc_current_block ()
1528 && current_attr.pointer == 0)
1530 gfc_error ("Component at %C must have the POINTER attribute");
1534 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1536 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1538 gfc_error ("Array component of structure at %C must have explicit "
1539 "or deferred shape");
1544 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1548 if (c->ts.type == BT_CHARACTER)
1550 c->attr = current_attr;
1552 c->initializer = *init;
1559 c->attr.codimension = 1;
1561 c->attr.dimension = 1;
1565 /* Should this ever get more complicated, combine with similar section
1566 in add_init_expr_to_sym into a separate function. */
1567 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer && c->ts.u.cl
1568 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1572 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1573 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1574 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1576 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1578 if (c->initializer->expr_type == EXPR_CONSTANT)
1579 gfc_set_constant_character_len (len, c->initializer, -1);
1580 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1581 c->initializer->ts.u.cl->length->value.integer))
1583 gfc_constructor *ctor;
1584 ctor = gfc_constructor_first (c->initializer->value.constructor);
1589 bool has_ts = (c->initializer->ts.u.cl
1590 && c->initializer->ts.u.cl->length_from_typespec);
1592 /* Remember the length of the first element for checking
1593 that all elements *in the constructor* have the same
1594 length. This need not be the length of the LHS! */
1595 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1596 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1597 first_len = ctor->expr->value.character.length;
1599 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1600 if (ctor->expr->expr_type == EXPR_CONSTANT)
1602 gfc_set_constant_character_len (len, ctor->expr,
1603 has_ts ? -1 : first_len);
1604 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1610 /* Check array components. */
1611 if (!c->attr.dimension)
1614 if (c->attr.pointer)
1616 if (c->as->type != AS_DEFERRED)
1618 gfc_error ("Pointer array component of structure at %C must have a "
1623 else if (c->attr.allocatable)
1625 if (c->as->type != AS_DEFERRED)
1627 gfc_error ("Allocatable component of structure at %C must have a "
1634 if (c->as->type != AS_EXPLICIT)
1636 gfc_error ("Array component of structure at %C must have an "
1643 if (c->ts.type == BT_CLASS)
1645 bool delayed = (gfc_state_stack->sym == c->ts.u.derived)
1646 || (!c->ts.u.derived->components
1647 && !c->ts.u.derived->attr.zero_comp);
1648 return gfc_build_class_symbol (&c->ts, &c->attr, &c->as, delayed);
1655 /* Match a 'NULL()', and possibly take care of some side effects. */
1658 gfc_match_null (gfc_expr **result)
1663 m = gfc_match (" null ( )");
1667 /* The NULL symbol now has to be/become an intrinsic function. */
1668 if (gfc_get_symbol ("null", NULL, &sym))
1670 gfc_error ("NULL() initialization at %C is ambiguous");
1674 gfc_intrinsic_symbol (sym);
1676 if (sym->attr.proc != PROC_INTRINSIC
1677 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1678 sym->name, NULL) == FAILURE
1679 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1682 *result = gfc_get_null_expr (&gfc_current_locus);
1688 /* Match the initialization expr for a data pointer or procedure pointer. */
1691 match_pointer_init (gfc_expr **init, int procptr)
1695 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1697 gfc_error ("Initialization of pointer at %C is not allowed in "
1698 "a PURE procedure");
1702 /* Match NULL() initilization. */
1703 m = gfc_match_null (init);
1707 /* Match non-NULL initialization. */
1708 gfc_matching_ptr_assignment = !procptr;
1709 gfc_matching_procptr_assignment = procptr;
1710 m = gfc_match_rvalue (init);
1711 gfc_matching_ptr_assignment = 0;
1712 gfc_matching_procptr_assignment = 0;
1713 if (m == MATCH_ERROR)
1715 else if (m == MATCH_NO)
1717 gfc_error ("Error in pointer initialization at %C");
1722 gfc_resolve_expr (*init);
1724 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: non-NULL pointer "
1725 "initialization at %C") == FAILURE)
1732 /* Match a variable name with an optional initializer. When this
1733 subroutine is called, a variable is expected to be parsed next.
1734 Depending on what is happening at the moment, updates either the
1735 symbol table or the current interface. */
1738 variable_decl (int elem)
1740 char name[GFC_MAX_SYMBOL_LEN + 1];
1741 gfc_expr *initializer, *char_len;
1743 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1755 /* When we get here, we've just matched a list of attributes and
1756 maybe a type and a double colon. The next thing we expect to see
1757 is the name of the symbol. */
1758 m = gfc_match_name (name);
1762 var_locus = gfc_current_locus;
1764 /* Now we could see the optional array spec. or character length. */
1765 m = gfc_match_array_spec (&as, true, true);
1766 if (m == MATCH_ERROR)
1770 as = gfc_copy_array_spec (current_as);
1771 else if (current_as)
1772 merge_array_spec (current_as, as, true);
1774 if (gfc_option.flag_cray_pointer)
1775 cp_as = gfc_copy_array_spec (as);
1777 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1778 determine (and check) whether it can be implied-shape. If it
1779 was parsed as assumed-size, change it because PARAMETERs can not
1783 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1786 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1791 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1792 && current_attr.flavor == FL_PARAMETER)
1793 as->type = AS_IMPLIED_SHAPE;
1795 if (as->type == AS_IMPLIED_SHAPE
1796 && gfc_notify_std (GFC_STD_F2008,
1797 "Fortran 2008: Implied-shape array at %L",
1798 &var_locus) == FAILURE)
1807 cl_deferred = false;
1809 if (current_ts.type == BT_CHARACTER)
1811 switch (match_char_length (&char_len, &cl_deferred))
1814 cl = gfc_new_charlen (gfc_current_ns, NULL);
1816 cl->length = char_len;
1819 /* Non-constant lengths need to be copied after the first
1820 element. Also copy assumed lengths. */
1823 && (current_ts.u.cl->length == NULL
1824 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1826 cl = gfc_new_charlen (gfc_current_ns, NULL);
1827 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1830 cl = current_ts.u.cl;
1832 cl_deferred = current_ts.deferred;
1841 /* If this symbol has already shown up in a Cray Pointer declaration,
1842 then we want to set the type & bail out. */
1843 if (gfc_option.flag_cray_pointer)
1845 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1846 if (sym != NULL && sym->attr.cray_pointee)
1848 sym->ts.type = current_ts.type;
1849 sym->ts.kind = current_ts.kind;
1851 sym->ts.u.derived = current_ts.u.derived;
1852 sym->ts.is_c_interop = current_ts.is_c_interop;
1853 sym->ts.is_iso_c = current_ts.is_iso_c;
1856 /* Check to see if we have an array specification. */
1859 if (sym->as != NULL)
1861 gfc_error ("Duplicate array spec for Cray pointee at %C");
1862 gfc_free_array_spec (cp_as);
1868 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1869 gfc_internal_error ("Couldn't set pointee array spec.");
1871 /* Fix the array spec. */
1872 m = gfc_mod_pointee_as (sym->as);
1873 if (m == MATCH_ERROR)
1881 gfc_free_array_spec (cp_as);
1885 /* Procedure pointer as function result. */
1886 if (gfc_current_state () == COMP_FUNCTION
1887 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1888 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1889 strcpy (name, "ppr@");
1891 if (gfc_current_state () == COMP_FUNCTION
1892 && strcmp (name, gfc_current_block ()->name) == 0
1893 && gfc_current_block ()->result
1894 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1895 strcpy (name, "ppr@");
1897 /* OK, we've successfully matched the declaration. Now put the
1898 symbol in the current namespace, because it might be used in the
1899 optional initialization expression for this symbol, e.g. this is
1902 integer, parameter :: i = huge(i)
1904 This is only true for parameters or variables of a basic type.
1905 For components of derived types, it is not true, so we don't
1906 create a symbol for those yet. If we fail to create the symbol,
1908 if (gfc_current_state () != COMP_DERIVED
1909 && build_sym (name, cl, cl_deferred, &as, &var_locus) == FAILURE)
1915 /* An interface body specifies all of the procedure's
1916 characteristics and these shall be consistent with those
1917 specified in the procedure definition, except that the interface
1918 may specify a procedure that is not pure if the procedure is
1919 defined to be pure(12.3.2). */
1920 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1921 && gfc_current_ns->proc_name
1922 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1923 && current_ts.u.derived->ns != gfc_current_ns)
1926 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.u.derived->name);
1927 if (!(current_ts.u.derived->attr.imported
1929 && st->n.sym == current_ts.u.derived)
1930 && !gfc_current_ns->has_import_set)
1932 gfc_error ("the type of '%s' at %C has not been declared within the "
1939 /* In functions that have a RESULT variable defined, the function
1940 name always refers to function calls. Therefore, the name is
1941 not allowed to appear in specification statements. */
1942 if (gfc_current_state () == COMP_FUNCTION
1943 && gfc_current_block () != NULL
1944 && gfc_current_block ()->result != NULL
1945 && gfc_current_block ()->result != gfc_current_block ()
1946 && strcmp (gfc_current_block ()->name, name) == 0)
1948 gfc_error ("Function name '%s' not allowed at %C", name);
1953 /* We allow old-style initializations of the form
1954 integer i /2/, j(4) /3*3, 1/
1955 (if no colon has been seen). These are different from data
1956 statements in that initializers are only allowed to apply to the
1957 variable immediately preceding, i.e.
1959 is not allowed. Therefore we have to do some work manually, that
1960 could otherwise be left to the matchers for DATA statements. */
1962 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1964 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1965 "initialization at %C") == FAILURE)
1968 return match_old_style_init (name);
1971 /* The double colon must be present in order to have initializers.
1972 Otherwise the statement is ambiguous with an assignment statement. */
1975 if (gfc_match (" =>") == MATCH_YES)
1977 if (!current_attr.pointer)
1979 gfc_error ("Initialization at %C isn't for a pointer variable");
1984 m = match_pointer_init (&initializer, 0);
1988 else if (gfc_match_char ('=') == MATCH_YES)
1990 if (current_attr.pointer)
1992 gfc_error ("Pointer initialization at %C requires '=>', "
1998 m = gfc_match_init_expr (&initializer);
2001 gfc_error ("Expected an initialization expression at %C");
2005 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2006 && gfc_state_stack->state != COMP_DERIVED)
2008 gfc_error ("Initialization of variable at %C is not allowed in "
2009 "a PURE procedure");
2018 if (initializer != NULL && current_attr.allocatable
2019 && gfc_current_state () == COMP_DERIVED)
2021 gfc_error ("Initialization of allocatable component at %C is not "
2027 /* Add the initializer. Note that it is fine if initializer is
2028 NULL here, because we sometimes also need to check if a
2029 declaration *must* have an initialization expression. */
2030 if (gfc_current_state () != COMP_DERIVED)
2031 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2034 if (current_ts.type == BT_DERIVED
2035 && !current_attr.pointer && !initializer)
2036 initializer = gfc_default_initializer (¤t_ts);
2037 t = build_struct (name, cl, &initializer, &as);
2040 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
2043 /* Free stuff up and return. */
2044 gfc_free_expr (initializer);
2045 gfc_free_array_spec (as);
2051 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2052 This assumes that the byte size is equal to the kind number for
2053 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2056 gfc_match_old_kind_spec (gfc_typespec *ts)
2061 if (gfc_match_char ('*') != MATCH_YES)
2064 m = gfc_match_small_literal_int (&ts->kind, NULL);
2068 original_kind = ts->kind;
2070 /* Massage the kind numbers for complex types. */
2071 if (ts->type == BT_COMPLEX)
2075 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2076 gfc_basic_typename (ts->type), original_kind);
2082 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2084 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2085 gfc_basic_typename (ts->type), original_kind);
2089 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2090 gfc_basic_typename (ts->type), original_kind) == FAILURE)
2097 /* Match a kind specification. Since kinds are generally optional, we
2098 usually return MATCH_NO if something goes wrong. If a "kind="
2099 string is found, then we know we have an error. */
2102 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2114 where = loc = gfc_current_locus;
2119 if (gfc_match_char ('(') == MATCH_NO)
2122 /* Also gobbles optional text. */
2123 if (gfc_match (" kind = ") == MATCH_YES)
2126 loc = gfc_current_locus;
2129 n = gfc_match_init_expr (&e);
2133 if (gfc_matching_function)
2135 /* The function kind expression might include use associated or
2136 imported parameters and try again after the specification
2138 if (gfc_match_char (')') != MATCH_YES)
2140 gfc_error ("Missing right parenthesis at %C");
2146 gfc_undo_symbols ();
2151 /* ....or else, the match is real. */
2153 gfc_error ("Expected initialization expression at %C");
2161 gfc_error ("Expected scalar initialization expression at %C");
2166 msg = gfc_extract_int (e, &ts->kind);
2175 /* Before throwing away the expression, let's see if we had a
2176 C interoperable kind (and store the fact). */
2177 if (e->ts.is_c_interop == 1)
2179 /* Mark this as c interoperable if being declared with one
2180 of the named constants from iso_c_binding. */
2181 ts->is_c_interop = e->ts.is_iso_c;
2182 ts->f90_type = e->ts.f90_type;
2188 /* Ignore errors to this point, if we've gotten here. This means
2189 we ignore the m=MATCH_ERROR from above. */
2190 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2192 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2193 gfc_basic_typename (ts->type));
2194 gfc_current_locus = where;
2198 /* Warn if, e.g., c_int is used for a REAL variable, but not
2199 if, e.g., c_double is used for COMPLEX as the standard
2200 explicitly says that the kind type parameter for complex and real
2201 variable is the same, i.e. c_float == c_float_complex. */
2202 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2203 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2204 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2205 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2206 "is %s", gfc_basic_typename (ts->f90_type), &where,
2207 gfc_basic_typename (ts->type));
2209 gfc_gobble_whitespace ();
2210 if ((c = gfc_next_ascii_char ()) != ')'
2211 && (ts->type != BT_CHARACTER || c != ','))
2213 if (ts->type == BT_CHARACTER)
2214 gfc_error ("Missing right parenthesis or comma at %C");
2216 gfc_error ("Missing right parenthesis at %C");
2220 /* All tests passed. */
2223 if(m == MATCH_ERROR)
2224 gfc_current_locus = where;
2226 /* Return what we know from the test(s). */
2231 gfc_current_locus = where;
2237 match_char_kind (int * kind, int * is_iso_c)
2246 where = gfc_current_locus;
2248 n = gfc_match_init_expr (&e);
2250 if (n != MATCH_YES && gfc_matching_function)
2252 /* The expression might include use-associated or imported
2253 parameters and try again after the specification
2256 gfc_undo_symbols ();
2261 gfc_error ("Expected initialization expression at %C");
2267 gfc_error ("Expected scalar initialization expression at %C");
2272 msg = gfc_extract_int (e, kind);
2273 *is_iso_c = e->ts.is_iso_c;
2283 /* Ignore errors to this point, if we've gotten here. This means
2284 we ignore the m=MATCH_ERROR from above. */
2285 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2287 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2291 /* All tests passed. */
2294 if (m == MATCH_ERROR)
2295 gfc_current_locus = where;
2297 /* Return what we know from the test(s). */
2302 gfc_current_locus = where;
2307 /* Match the various kind/length specifications in a CHARACTER
2308 declaration. We don't return MATCH_NO. */
2311 gfc_match_char_spec (gfc_typespec *ts)
2313 int kind, seen_length, is_iso_c;
2325 /* Try the old-style specification first. */
2326 old_char_selector = 0;
2328 m = match_char_length (&len, &deferred);
2332 old_char_selector = 1;
2337 m = gfc_match_char ('(');
2340 m = MATCH_YES; /* Character without length is a single char. */
2344 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2345 if (gfc_match (" kind =") == MATCH_YES)
2347 m = match_char_kind (&kind, &is_iso_c);
2349 if (m == MATCH_ERROR)
2354 if (gfc_match (" , len =") == MATCH_NO)
2357 m = char_len_param_value (&len, &deferred);
2360 if (m == MATCH_ERROR)
2367 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2368 if (gfc_match (" len =") == MATCH_YES)
2370 m = char_len_param_value (&len, &deferred);
2373 if (m == MATCH_ERROR)
2377 if (gfc_match_char (')') == MATCH_YES)
2380 if (gfc_match (" , kind =") != MATCH_YES)
2383 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2389 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2390 m = char_len_param_value (&len, &deferred);
2393 if (m == MATCH_ERROR)
2397 m = gfc_match_char (')');
2401 if (gfc_match_char (',') != MATCH_YES)
2404 gfc_match (" kind ="); /* Gobble optional text. */
2406 m = match_char_kind (&kind, &is_iso_c);
2407 if (m == MATCH_ERROR)
2413 /* Require a right-paren at this point. */
2414 m = gfc_match_char (')');
2419 gfc_error ("Syntax error in CHARACTER declaration at %C");
2421 gfc_free_expr (len);
2425 /* Deal with character functions after USE and IMPORT statements. */
2426 if (gfc_matching_function)
2428 gfc_free_expr (len);
2429 gfc_undo_symbols ();
2435 gfc_free_expr (len);
2439 /* Do some final massaging of the length values. */
2440 cl = gfc_new_charlen (gfc_current_ns, NULL);
2442 if (seen_length == 0)
2443 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2448 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2449 ts->deferred = deferred;
2451 /* We have to know if it was a c interoperable kind so we can
2452 do accurate type checking of bind(c) procs, etc. */
2454 /* Mark this as c interoperable if being declared with one
2455 of the named constants from iso_c_binding. */
2456 ts->is_c_interop = is_iso_c;
2457 else if (len != NULL)
2458 /* Here, we might have parsed something such as: character(c_char)
2459 In this case, the parsing code above grabs the c_char when
2460 looking for the length (line 1690, roughly). it's the last
2461 testcase for parsing the kind params of a character variable.
2462 However, it's not actually the length. this seems like it
2464 To see if the user used a C interop kind, test the expr
2465 of the so called length, and see if it's C interoperable. */
2466 ts->is_c_interop = len->ts.is_iso_c;
2472 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2473 structure to the matched specification. This is necessary for FUNCTION and
2474 IMPLICIT statements.
2476 If implicit_flag is nonzero, then we don't check for the optional
2477 kind specification. Not doing so is needed for matching an IMPLICIT
2478 statement correctly. */
2481 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2483 char name[GFC_MAX_SYMBOL_LEN + 1];
2487 bool seen_deferred_kind, matched_type;
2489 /* A belt and braces check that the typespec is correctly being treated
2490 as a deferred characteristic association. */
2491 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2492 && (gfc_current_block ()->result->ts.kind == -1)
2493 && (ts->kind == -1);
2495 if (seen_deferred_kind)
2498 /* Clear the current binding label, in case one is given. */
2499 curr_binding_label[0] = '\0';
2501 if (gfc_match (" byte") == MATCH_YES)
2503 if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2507 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2509 gfc_error ("BYTE type used at %C "
2510 "is not available on the target machine");
2514 ts->type = BT_INTEGER;
2520 m = gfc_match (" type ( %n", name);
2521 matched_type = (m == MATCH_YES);
2523 if ((matched_type && strcmp ("integer", name) == 0)
2524 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2526 ts->type = BT_INTEGER;
2527 ts->kind = gfc_default_integer_kind;
2531 if ((matched_type && strcmp ("character", name) == 0)
2532 || (!matched_type && gfc_match (" character") == MATCH_YES))
2535 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2536 "intrinsic-type-spec at %C") == FAILURE)
2539 ts->type = BT_CHARACTER;
2540 if (implicit_flag == 0)
2541 m = gfc_match_char_spec (ts);
2545 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2551 if ((matched_type && strcmp ("real", name) == 0)
2552 || (!matched_type && gfc_match (" real") == MATCH_YES))
2555 ts->kind = gfc_default_real_kind;
2560 && (strcmp ("doubleprecision", name) == 0
2561 || (strcmp ("double", name) == 0
2562 && gfc_match (" precision") == MATCH_YES)))
2563 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2566 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2567 "intrinsic-type-spec at %C") == FAILURE)
2569 if (matched_type && gfc_match_char (')') != MATCH_YES)
2573 ts->kind = gfc_default_double_kind;
2577 if ((matched_type && strcmp ("complex", name) == 0)
2578 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2580 ts->type = BT_COMPLEX;
2581 ts->kind = gfc_default_complex_kind;
2586 && (strcmp ("doublecomplex", name) == 0
2587 || (strcmp ("double", name) == 0
2588 && gfc_match (" complex") == MATCH_YES)))
2589 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2591 if (gfc_notify_std (GFC_STD_GNU, "Extension: DOUBLE COMPLEX at %C")
2596 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2597 "intrinsic-type-spec at %C") == FAILURE)
2600 if (matched_type && gfc_match_char (')') != MATCH_YES)
2603 ts->type = BT_COMPLEX;
2604 ts->kind = gfc_default_double_kind;
2608 if ((matched_type && strcmp ("logical", name) == 0)
2609 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2611 ts->type = BT_LOGICAL;
2612 ts->kind = gfc_default_logical_kind;
2617 m = gfc_match_char (')');
2620 ts->type = BT_DERIVED;
2623 /* Match CLASS declarations. */
2624 m = gfc_match (" class ( * )");
2625 if (m == MATCH_ERROR)
2627 else if (m == MATCH_YES)
2629 gfc_fatal_error ("Unlimited polymorphism at %C not yet supported");
2633 m = gfc_match (" class ( %n )", name);
2636 ts->type = BT_CLASS;
2638 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2643 /* Defer association of the derived type until the end of the
2644 specification block. However, if the derived type can be
2645 found, add it to the typespec. */
2646 if (gfc_matching_function)
2648 ts->u.derived = NULL;
2649 if (gfc_current_state () != COMP_INTERFACE
2650 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2651 ts->u.derived = sym;
2655 /* Search for the name but allow the components to be defined later. If
2656 type = -1, this typespec has been seen in a function declaration but
2657 the type could not be accessed at that point. */
2659 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2661 gfc_error ("Type name '%s' at %C is ambiguous", name);
2664 else if (ts->kind == -1)
2666 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2667 || gfc_current_ns->has_import_set;
2668 if (gfc_find_symbol (name, NULL, iface, &sym))
2670 gfc_error ("Type name '%s' at %C is ambiguous", name);
2679 if (sym->attr.flavor != FL_DERIVED
2680 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2683 gfc_set_sym_referenced (sym);
2684 ts->u.derived = sym;
2690 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2691 "intrinsic-type-spec at %C") == FAILURE)
2694 /* For all types except double, derived and character, look for an
2695 optional kind specifier. MATCH_NO is actually OK at this point. */
2696 if (implicit_flag == 1)
2698 if (matched_type && gfc_match_char (')') != MATCH_YES)
2704 if (gfc_current_form == FORM_FREE)
2706 c = gfc_peek_ascii_char ();
2707 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2708 && c != ':' && c != ',')
2710 if (matched_type && c == ')')
2712 gfc_next_ascii_char ();
2719 m = gfc_match_kind_spec (ts, false);
2720 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2721 m = gfc_match_old_kind_spec (ts);
2723 if (matched_type && gfc_match_char (')') != MATCH_YES)
2726 /* Defer association of the KIND expression of function results
2727 until after USE and IMPORT statements. */
2728 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2729 || gfc_matching_function)
2733 m = MATCH_YES; /* No kind specifier found. */
2739 /* Match an IMPLICIT NONE statement. Actually, this statement is
2740 already matched in parse.c, or we would not end up here in the
2741 first place. So the only thing we need to check, is if there is
2742 trailing garbage. If not, the match is successful. */
2745 gfc_match_implicit_none (void)
2747 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2751 /* Match the letter range(s) of an IMPLICIT statement. */
2754 match_implicit_range (void)
2760 cur_loc = gfc_current_locus;
2762 gfc_gobble_whitespace ();
2763 c = gfc_next_ascii_char ();
2766 gfc_error ("Missing character range in IMPLICIT at %C");
2773 gfc_gobble_whitespace ();
2774 c1 = gfc_next_ascii_char ();
2778 gfc_gobble_whitespace ();
2779 c = gfc_next_ascii_char ();
2784 inner = 0; /* Fall through. */
2791 gfc_gobble_whitespace ();
2792 c2 = gfc_next_ascii_char ();
2796 gfc_gobble_whitespace ();
2797 c = gfc_next_ascii_char ();
2799 if ((c != ',') && (c != ')'))
2812 gfc_error ("Letters must be in alphabetic order in "
2813 "IMPLICIT statement at %C");
2817 /* See if we can add the newly matched range to the pending
2818 implicits from this IMPLICIT statement. We do not check for
2819 conflicts with whatever earlier IMPLICIT statements may have
2820 set. This is done when we've successfully finished matching
2822 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2829 gfc_syntax_error (ST_IMPLICIT);
2831 gfc_current_locus = cur_loc;
2836 /* Match an IMPLICIT statement, storing the types for
2837 gfc_set_implicit() if the statement is accepted by the parser.
2838 There is a strange looking, but legal syntactic construction
2839 possible. It looks like:
2841 IMPLICIT INTEGER (a-b) (c-d)
2843 This is legal if "a-b" is a constant expression that happens to
2844 equal one of the legal kinds for integers. The real problem
2845 happens with an implicit specification that looks like:
2847 IMPLICIT INTEGER (a-b)
2849 In this case, a typespec matcher that is "greedy" (as most of the
2850 matchers are) gobbles the character range as a kindspec, leaving
2851 nothing left. We therefore have to go a bit more slowly in the
2852 matching process by inhibiting the kindspec checking during
2853 typespec matching and checking for a kind later. */
2856 gfc_match_implicit (void)
2865 /* We don't allow empty implicit statements. */
2866 if (gfc_match_eos () == MATCH_YES)
2868 gfc_error ("Empty IMPLICIT statement at %C");
2874 /* First cleanup. */
2875 gfc_clear_new_implicit ();
2877 /* A basic type is mandatory here. */
2878 m = gfc_match_decl_type_spec (&ts, 1);
2879 if (m == MATCH_ERROR)
2884 cur_loc = gfc_current_locus;
2885 m = match_implicit_range ();
2889 /* We may have <TYPE> (<RANGE>). */
2890 gfc_gobble_whitespace ();
2891 c = gfc_next_ascii_char ();
2892 if ((c == '\n') || (c == ','))
2894 /* Check for CHARACTER with no length parameter. */
2895 if (ts.type == BT_CHARACTER && !ts.u.cl)
2897 ts.kind = gfc_default_character_kind;
2898 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2899 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
2903 /* Record the Successful match. */
2904 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2909 gfc_current_locus = cur_loc;
2912 /* Discard the (incorrectly) matched range. */
2913 gfc_clear_new_implicit ();
2915 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2916 if (ts.type == BT_CHARACTER)
2917 m = gfc_match_char_spec (&ts);
2920 m = gfc_match_kind_spec (&ts, false);
2923 m = gfc_match_old_kind_spec (&ts);
2924 if (m == MATCH_ERROR)
2930 if (m == MATCH_ERROR)
2933 m = match_implicit_range ();
2934 if (m == MATCH_ERROR)
2939 gfc_gobble_whitespace ();
2940 c = gfc_next_ascii_char ();
2941 if ((c != '\n') && (c != ','))
2944 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2952 gfc_syntax_error (ST_IMPLICIT);
2960 gfc_match_import (void)
2962 char name[GFC_MAX_SYMBOL_LEN + 1];
2967 if (gfc_current_ns->proc_name == NULL
2968 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2970 gfc_error ("IMPORT statement at %C only permitted in "
2971 "an INTERFACE body");
2975 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2979 if (gfc_match_eos () == MATCH_YES)
2981 /* All host variables should be imported. */
2982 gfc_current_ns->has_import_set = 1;
2986 if (gfc_match (" ::") == MATCH_YES)
2988 if (gfc_match_eos () == MATCH_YES)
2990 gfc_error ("Expecting list of named entities at %C");
2998 m = gfc_match (" %n", name);
3002 if (gfc_current_ns->parent != NULL
3003 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3005 gfc_error ("Type name '%s' at %C is ambiguous", name);
3008 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3009 && gfc_find_symbol (name,
3010 gfc_current_ns->proc_name->ns->parent,
3013 gfc_error ("Type name '%s' at %C is ambiguous", name);
3019 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3020 "at %C - does not exist.", name);
3024 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
3026 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3031 st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
3034 sym->attr.imported = 1;
3046 if (gfc_match_eos () == MATCH_YES)
3048 if (gfc_match_char (',') != MATCH_YES)
3055 gfc_error ("Syntax error in IMPORT statement at %C");
3060 /* A minimal implementation of gfc_match without whitespace, escape
3061 characters or variable arguments. Returns true if the next
3062 characters match the TARGET template exactly. */
3065 match_string_p (const char *target)
3069 for (p = target; *p; p++)
3070 if ((char) gfc_next_ascii_char () != *p)
3075 /* Matches an attribute specification including array specs. If
3076 successful, leaves the variables current_attr and current_as
3077 holding the specification. Also sets the colon_seen variable for
3078 later use by matchers associated with initializations.
3080 This subroutine is a little tricky in the sense that we don't know
3081 if we really have an attr-spec until we hit the double colon.
3082 Until that time, we can only return MATCH_NO. This forces us to
3083 check for duplicate specification at this level. */
3086 match_attr_spec (void)
3088 /* Modifiers that can exist in a type statement. */
3090 { GFC_DECL_BEGIN = 0,
3091 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3092 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3093 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3094 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3095 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3096 DECL_NONE, GFC_DECL_END /* Sentinel */
3100 /* GFC_DECL_END is the sentinel, index starts at 0. */
3101 #define NUM_DECL GFC_DECL_END
3103 locus start, seen_at[NUM_DECL];
3110 gfc_clear_attr (¤t_attr);
3111 start = gfc_current_locus;
3116 /* See if we get all of the keywords up to the final double colon. */
3117 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3125 gfc_gobble_whitespace ();
3127 ch = gfc_next_ascii_char ();
3130 /* This is the successful exit condition for the loop. */
3131 if (gfc_next_ascii_char () == ':')
3136 gfc_gobble_whitespace ();
3137 switch (gfc_peek_ascii_char ())
3140 gfc_next_ascii_char ();
3141 switch (gfc_next_ascii_char ())
3144 if (match_string_p ("locatable"))
3146 /* Matched "allocatable". */
3147 d = DECL_ALLOCATABLE;
3152 if (match_string_p ("ynchronous"))
3154 /* Matched "asynchronous". */
3155 d = DECL_ASYNCHRONOUS;
3162 /* Try and match the bind(c). */
3163 m = gfc_match_bind_c (NULL, true);
3166 else if (m == MATCH_ERROR)
3171 gfc_next_ascii_char ();
3172 if ('o' != gfc_next_ascii_char ())
3174 switch (gfc_next_ascii_char ())
3177 if (match_string_p ("imension"))
3179 d = DECL_CODIMENSION;
3183 if (match_string_p ("tiguous"))
3185 d = DECL_CONTIGUOUS;
3192 if (match_string_p ("dimension"))
3197 if (match_string_p ("external"))
3202 if (match_string_p ("int"))
3204 ch = gfc_next_ascii_char ();
3207 if (match_string_p ("nt"))
3209 /* Matched "intent". */
3210 /* TODO: Call match_intent_spec from here. */
3211 if (gfc_match (" ( in out )") == MATCH_YES)
3213 else if (gfc_match (" ( in )") == MATCH_YES)
3215 else if (gfc_match (" ( out )") == MATCH_YES)
3221 if (match_string_p ("insic"))
3223 /* Matched "intrinsic". */
3231 if (match_string_p ("optional"))
3236 gfc_next_ascii_char ();
3237 switch (gfc_next_ascii_char ())
3240 if (match_string_p ("rameter"))
3242 /* Matched "parameter". */
3248 if (match_string_p ("inter"))
3250 /* Matched "pointer". */
3256 ch = gfc_next_ascii_char ();
3259 if (match_string_p ("vate"))
3261 /* Matched "private". */
3267 if (match_string_p ("tected"))
3269 /* Matched "protected". */
3276 if (match_string_p ("blic"))
3278 /* Matched "public". */
3286 if (match_string_p ("save"))
3291 if (match_string_p ("target"))
3296 gfc_next_ascii_char ();
3297 ch = gfc_next_ascii_char ();
3300 if (match_string_p ("lue"))
3302 /* Matched "value". */
3308 if (match_string_p ("latile"))
3310 /* Matched "volatile". */
3318 /* No double colon and no recognizable decl_type, so assume that
3319 we've been looking at something else the whole time. */
3326 /* Check to make sure any parens are paired up correctly. */
3327 if (gfc_match_parens () == MATCH_ERROR)
3334 seen_at[d] = gfc_current_locus;
3336 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3338 gfc_array_spec *as = NULL;
3340 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3341 d == DECL_CODIMENSION);
3343 if (current_as == NULL)
3345 else if (m == MATCH_YES)
3347 merge_array_spec (as, current_as, false);
3353 if (d == DECL_CODIMENSION)
3354 gfc_error ("Missing codimension specification at %C");
3356 gfc_error ("Missing dimension specification at %C");
3360 if (m == MATCH_ERROR)
3365 /* Since we've seen a double colon, we have to be looking at an
3366 attr-spec. This means that we can now issue errors. */
3367 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3372 case DECL_ALLOCATABLE:
3373 attr = "ALLOCATABLE";
3375 case DECL_ASYNCHRONOUS:
3376 attr = "ASYNCHRONOUS";
3378 case DECL_CODIMENSION:
3379 attr = "CODIMENSION";
3381 case DECL_CONTIGUOUS:
3382 attr = "CONTIGUOUS";
3384 case DECL_DIMENSION:
3391 attr = "INTENT (IN)";
3394 attr = "INTENT (OUT)";
3397 attr = "INTENT (IN OUT)";
3399 case DECL_INTRINSIC:
3405 case DECL_PARAMETER:
3411 case DECL_PROTECTED:
3426 case DECL_IS_BIND_C:
3436 attr = NULL; /* This shouldn't happen. */
3439 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3444 /* Now that we've dealt with duplicate attributes, add the attributes
3445 to the current attribute. */
3446 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3451 if (gfc_current_state () == COMP_DERIVED
3452 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3453 && d != DECL_POINTER && d != DECL_PRIVATE
3454 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3456 if (d == DECL_ALLOCATABLE)
3458 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3459 "attribute at %C in a TYPE definition")
3468 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3475 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3476 && gfc_current_state () != COMP_MODULE)
3478 if (d == DECL_PRIVATE)
3482 if (gfc_current_state () == COMP_DERIVED
3483 && gfc_state_stack->previous
3484 && gfc_state_stack->previous->state == COMP_MODULE)
3486 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3487 "at %L in a TYPE definition", attr,
3497 gfc_error ("%s attribute at %L is not allowed outside of the "
3498 "specification part of a module", attr, &seen_at[d]);
3506 case DECL_ALLOCATABLE:
3507 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
3510 case DECL_ASYNCHRONOUS:
3511 if (gfc_notify_std (GFC_STD_F2003,
3512 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3516 t = gfc_add_asynchronous (¤t_attr, NULL, &seen_at[d]);
3519 case DECL_CODIMENSION:
3520 t = gfc_add_codimension (¤t_attr, NULL, &seen_at[d]);
3523 case DECL_CONTIGUOUS:
3524 if (gfc_notify_std (GFC_STD_F2008,
3525 "Fortran 2008: CONTIGUOUS attribute at %C")
3529 t = gfc_add_contiguous (¤t_attr, NULL, &seen_at[d]);
3532 case DECL_DIMENSION:
3533 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
3537 t = gfc_add_external (¤t_attr, &seen_at[d]);
3541 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
3545 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
3549 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
3552 case DECL_INTRINSIC:
3553 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
3557 t = gfc_add_optional (¤t_attr, &seen_at[d]);
3560 case DECL_PARAMETER:
3561 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
3565 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
3568 case DECL_PROTECTED:
3569 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3571 gfc_error ("PROTECTED at %C only allowed in specification "
3572 "part of a module");
3577 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3582 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
3586 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
3591 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
3596 t = gfc_add_save (¤t_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3600 t = gfc_add_target (¤t_attr, &seen_at[d]);
3603 case DECL_IS_BIND_C:
3604 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
3608 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3613 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
3617 if (gfc_notify_std (GFC_STD_F2003,
3618 "Fortran 2003: VOLATILE attribute at %C")
3622 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
3626 gfc_internal_error ("match_attr_spec(): Bad attribute");
3636 /* Module variables implicitly have the SAVE attribute. */
3637 if (gfc_current_state () == COMP_MODULE && !current_attr.save)
3638 current_attr.save = SAVE_IMPLICIT;
3644 gfc_current_locus = start;
3645 gfc_free_array_spec (current_as);
3651 /* Set the binding label, dest_label, either with the binding label
3652 stored in the given gfc_typespec, ts, or if none was provided, it
3653 will be the symbol name in all lower case, as required by the draft
3654 (J3/04-007, section 15.4.1). If a binding label was given and
3655 there is more than one argument (num_idents), it is an error. */
3658 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3660 if (num_idents > 1 && has_name_equals)
3662 gfc_error ("Multiple identifiers provided with "
3663 "single NAME= specifier at %C");
3667 if (curr_binding_label[0] != '\0')
3669 /* Binding label given; store in temp holder til have sym. */
3670 strcpy (dest_label, curr_binding_label);
3674 /* No binding label given, and the NAME= specifier did not exist,
3675 which means there was no NAME="". */
3676 if (sym_name != NULL && has_name_equals == 0)
3677 strcpy (dest_label, sym_name);
3684 /* Set the status of the given common block as being BIND(C) or not,
3685 depending on the given parameter, is_bind_c. */
3688 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3690 com_block->is_bind_c = is_bind_c;
3695 /* Verify that the given gfc_typespec is for a C interoperable type. */
3698 verify_c_interop (gfc_typespec *ts)
3700 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3701 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3702 ? SUCCESS : FAILURE;
3703 else if (ts->is_c_interop != 1)
3710 /* Verify that the variables of a given common block, which has been
3711 defined with the attribute specifier bind(c), to be of a C
3712 interoperable type. Errors will be reported here, if
3716 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3718 gfc_symbol *curr_sym = NULL;
3719 gfc_try retval = SUCCESS;
3721 curr_sym = com_block->head;
3723 /* Make sure we have at least one symbol. */
3724 if (curr_sym == NULL)
3727 /* Here we know we have a symbol, so we'll execute this loop
3731 /* The second to last param, 1, says this is in a common block. */
3732 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3733 curr_sym = curr_sym->common_next;
3734 } while (curr_sym != NULL);
3740 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3741 an appropriate error message is reported. */
3744 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3745 int is_in_common, gfc_common_head *com_block)
3747 bool bind_c_function = false;
3748 gfc_try retval = SUCCESS;
3750 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3751 bind_c_function = true;
3753 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3755 tmp_sym = tmp_sym->result;
3756 /* Make sure it wasn't an implicitly typed result. */
3757 if (tmp_sym->attr.implicit_type)
3759 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3760 "%L may not be C interoperable", tmp_sym->name,
3761 &tmp_sym->declared_at);
3762 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3763 /* Mark it as C interoperable to prevent duplicate warnings. */
3764 tmp_sym->ts.is_c_interop = 1;
3765 tmp_sym->attr.is_c_interop = 1;
3769 /* Here, we know we have the bind(c) attribute, so if we have
3770 enough type info, then verify that it's a C interop kind.
3771 The info could be in the symbol already, or possibly still in
3772 the given ts (current_ts), so look in both. */
3773 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3775 if (verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3777 /* See if we're dealing with a sym in a common block or not. */
3778 if (is_in_common == 1)
3780 gfc_warning ("Variable '%s' in common block '%s' at %L "
3781 "may not be a C interoperable "
3782 "kind though common block '%s' is BIND(C)",
3783 tmp_sym->name, com_block->name,
3784 &(tmp_sym->declared_at), com_block->name);
3788 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3789 gfc_error ("Type declaration '%s' at %L is not C "
3790 "interoperable but it is BIND(C)",
3791 tmp_sym->name, &(tmp_sym->declared_at));
3793 gfc_warning ("Variable '%s' at %L "
3794 "may not be a C interoperable "
3795 "kind but it is bind(c)",
3796 tmp_sym->name, &(tmp_sym->declared_at));
3800 /* Variables declared w/in a common block can't be bind(c)
3801 since there's no way for C to see these variables, so there's
3802 semantically no reason for the attribute. */
3803 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3805 gfc_error ("Variable '%s' in common block '%s' at "
3806 "%L cannot be declared with BIND(C) "
3807 "since it is not a global",
3808 tmp_sym->name, com_block->name,
3809 &(tmp_sym->declared_at));
3813 /* Scalar variables that are bind(c) can not have the pointer
3814 or allocatable attributes. */
3815 if (tmp_sym->attr.is_bind_c == 1)
3817 if (tmp_sym->attr.pointer == 1)
3819 gfc_error ("Variable '%s' at %L cannot have both the "
3820 "POINTER and BIND(C) attributes",
3821 tmp_sym->name, &(tmp_sym->declared_at));
3825 if (tmp_sym->attr.allocatable == 1)
3827 gfc_error ("Variable '%s' at %L cannot have both the "
3828 "ALLOCATABLE and BIND(C) attributes",
3829 tmp_sym->name, &(tmp_sym->declared_at));
3835 /* If it is a BIND(C) function, make sure the return value is a
3836 scalar value. The previous tests in this function made sure
3837 the type is interoperable. */
3838 if (bind_c_function && tmp_sym->as != NULL)
3839 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3840 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3842 /* BIND(C) functions can not return a character string. */
3843 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3844 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
3845 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
3846 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
3847 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3848 "be a character string", tmp_sym->name,
3849 &(tmp_sym->declared_at));
3852 /* See if the symbol has been marked as private. If it has, make sure
3853 there is no binding label and warn the user if there is one. */
3854 if (tmp_sym->attr.access == ACCESS_PRIVATE
3855 && tmp_sym->binding_label[0] != '\0')
3856 /* Use gfc_warning_now because we won't say that the symbol fails
3857 just because of this. */
3858 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3859 "given the binding label '%s'", tmp_sym->name,
3860 &(tmp_sym->declared_at), tmp_sym->binding_label);
3866 /* Set the appropriate fields for a symbol that's been declared as
3867 BIND(C) (the is_bind_c flag and the binding label), and verify that
3868 the type is C interoperable. Errors are reported by the functions
3869 used to set/test these fields. */
3872 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3874 gfc_try retval = SUCCESS;
3876 /* TODO: Do we need to make sure the vars aren't marked private? */
3878 /* Set the is_bind_c bit in symbol_attribute. */
3879 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3881 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3882 num_idents) != SUCCESS)
3889 /* Set the fields marking the given common block as BIND(C), including
3890 a binding label, and report any errors encountered. */
3893 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3895 gfc_try retval = SUCCESS;
3897 /* destLabel, common name, typespec (which may have binding label). */
3898 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3902 /* Set the given common block (com_block) to being bind(c) (1). */
3903 set_com_block_bind_c (com_block, 1);
3909 /* Retrieve the list of one or more identifiers that the given bind(c)
3910 attribute applies to. */
3913 get_bind_c_idents (void)
3915 char name[GFC_MAX_SYMBOL_LEN + 1];
3917 gfc_symbol *tmp_sym = NULL;
3919 gfc_common_head *com_block = NULL;
3921 if (gfc_match_name (name) == MATCH_YES)
3923 found_id = MATCH_YES;
3924 gfc_get_ha_symbol (name, &tmp_sym);
3926 else if (match_common_name (name) == MATCH_YES)
3928 found_id = MATCH_YES;
3929 com_block = gfc_get_common (name, 0);
3933 gfc_error ("Need either entity or common block name for "
3934 "attribute specification statement at %C");
3938 /* Save the current identifier and look for more. */
3941 /* Increment the number of identifiers found for this spec stmt. */
3944 /* Make sure we have a sym or com block, and verify that it can
3945 be bind(c). Set the appropriate field(s) and look for more
3947 if (tmp_sym != NULL || com_block != NULL)
3949 if (tmp_sym != NULL)
3951 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3957 if (set_verify_bind_c_com_block(com_block, num_idents)
3962 /* Look to see if we have another identifier. */
3964 if (gfc_match_eos () == MATCH_YES)
3965 found_id = MATCH_NO;
3966 else if (gfc_match_char (',') != MATCH_YES)
3967 found_id = MATCH_NO;
3968 else if (gfc_match_name (name) == MATCH_YES)
3970 found_id = MATCH_YES;
3971 gfc_get_ha_symbol (name, &tmp_sym);
3973 else if (match_common_name (name) == MATCH_YES)
3975 found_id = MATCH_YES;
3976 com_block = gfc_get_common (name, 0);
3980 gfc_error ("Missing entity or common block name for "
3981 "attribute specification statement at %C");
3987 gfc_internal_error ("Missing symbol");
3989 } while (found_id == MATCH_YES);
3991 /* if we get here we were successful */
3996 /* Try and match a BIND(C) attribute specification statement. */
3999 gfc_match_bind_c_stmt (void)
4001 match found_match = MATCH_NO;
4006 /* This may not be necessary. */
4008 /* Clear the temporary binding label holder. */
4009 curr_binding_label[0] = '\0';
4011 /* Look for the bind(c). */
4012 found_match = gfc_match_bind_c (NULL, true);
4014 if (found_match == MATCH_YES)
4016 /* Look for the :: now, but it is not required. */
4019 /* Get the identifier(s) that needs to be updated. This may need to
4020 change to hand the flag(s) for the attr specified so all identifiers
4021 found can have all appropriate parts updated (assuming that the same
4022 spec stmt can have multiple attrs, such as both bind(c) and
4024 if (get_bind_c_idents () != SUCCESS)
4025 /* Error message should have printed already. */
4033 /* Match a data declaration statement. */
4036 gfc_match_data_decl (void)
4042 num_idents_on_line = 0;
4044 m = gfc_match_decl_type_spec (¤t_ts, 0);
4048 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4049 && gfc_current_state () != COMP_DERIVED)
4051 sym = gfc_use_derived (current_ts.u.derived);
4059 current_ts.u.derived = sym;
4062 m = match_attr_spec ();
4063 if (m == MATCH_ERROR)
4069 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4070 && current_ts.u.derived->components == NULL
4071 && !current_ts.u.derived->attr.zero_comp)
4074 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4077 gfc_find_symbol (current_ts.u.derived->name,
4078 current_ts.u.derived->ns->parent, 1, &sym);
4080 /* Any symbol that we find had better be a type definition
4081 which has its components defined. */
4082 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4083 && (current_ts.u.derived->components != NULL
4084 || current_ts.u.derived->attr.zero_comp))
4087 /* Now we have an error, which we signal, and then fix up
4088 because the knock-on is plain and simple confusing. */
4089 gfc_error_now ("Derived type at %C has not been previously defined "
4090 "and so cannot appear in a derived type definition");
4091 current_attr.pointer = 1;
4096 /* If we have an old-style character declaration, and no new-style
4097 attribute specifications, then there a comma is optional between
4098 the type specification and the variable list. */
4099 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4100 gfc_match_char (',');
4102 /* Give the types/attributes to symbols that follow. Give the element
4103 a number so that repeat character length expressions can be copied. */
4107 num_idents_on_line++;
4108 m = variable_decl (elem++);
4109 if (m == MATCH_ERROR)
4114 if (gfc_match_eos () == MATCH_YES)
4116 if (gfc_match_char (',') != MATCH_YES)
4120 if (gfc_error_flag_test () == 0)
4121 gfc_error ("Syntax error in data declaration at %C");
4124 gfc_free_data_all (gfc_current_ns);
4127 gfc_free_array_spec (current_as);
4133 /* Match a prefix associated with a function or subroutine
4134 declaration. If the typespec pointer is nonnull, then a typespec
4135 can be matched. Note that if nothing matches, MATCH_YES is
4136 returned (the null string was matched). */
4139 gfc_match_prefix (gfc_typespec *ts)
4145 gfc_clear_attr (¤t_attr);
4147 seen_impure = false;
4149 gcc_assert (!gfc_matching_prefix);
4150 gfc_matching_prefix = true;
4154 found_prefix = false;
4156 if (!seen_type && ts != NULL
4157 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4158 && gfc_match_space () == MATCH_YES)
4162 found_prefix = true;
4165 if (gfc_match ("elemental% ") == MATCH_YES)
4167 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
4170 found_prefix = true;
4173 if (gfc_match ("pure% ") == MATCH_YES)
4175 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
4178 found_prefix = true;
4181 if (gfc_match ("recursive% ") == MATCH_YES)
4183 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
4186 found_prefix = true;
4189 /* IMPURE is a somewhat special case, as it needs not set an actual
4190 attribute but rather only prevents ELEMENTAL routines from being
4191 automatically PURE. */
4192 if (gfc_match ("impure% ") == MATCH_YES)
4194 if (gfc_notify_std (GFC_STD_F2008,
4195 "Fortran 2008: IMPURE procedure at %C")
4200 found_prefix = true;
4203 while (found_prefix);
4205 /* IMPURE and PURE must not both appear, of course. */
4206 if (seen_impure && current_attr.pure)
4208 gfc_error ("PURE and IMPURE must not appear both at %C");
4212 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4213 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4215 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
4219 /* At this point, the next item is not a prefix. */
4220 gcc_assert (gfc_matching_prefix);
4221 gfc_matching_prefix = false;
4225 gcc_assert (gfc_matching_prefix);
4226 gfc_matching_prefix = false;
4231 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4234 copy_prefix (symbol_attribute *dest, locus *where)
4236 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
4239 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
4242 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
4249 /* Match a formal argument list. */
4252 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4254 gfc_formal_arglist *head, *tail, *p, *q;
4255 char name[GFC_MAX_SYMBOL_LEN + 1];
4261 if (gfc_match_char ('(') != MATCH_YES)
4268 if (gfc_match_char (')') == MATCH_YES)
4273 if (gfc_match_char ('*') == MATCH_YES)
4277 m = gfc_match_name (name);
4281 if (gfc_get_symbol (name, NULL, &sym))
4285 p = gfc_get_formal_arglist ();
4297 /* We don't add the VARIABLE flavor because the name could be a
4298 dummy procedure. We don't apply these attributes to formal
4299 arguments of statement functions. */
4300 if (sym != NULL && !st_flag
4301 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
4302 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
4308 /* The name of a program unit can be in a different namespace,
4309 so check for it explicitly. After the statement is accepted,
4310 the name is checked for especially in gfc_get_symbol(). */
4311 if (gfc_new_block != NULL && sym != NULL
4312 && strcmp (sym->name, gfc_new_block->name) == 0)
4314 gfc_error ("Name '%s' at %C is the name of the procedure",
4320 if (gfc_match_char (')') == MATCH_YES)
4323 m = gfc_match_char (',');
4326 gfc_error ("Unexpected junk in formal argument list at %C");
4332 /* Check for duplicate symbols in the formal argument list. */
4335 for (p = head; p->next; p = p->next)
4340 for (q = p->next; q; q = q->next)
4341 if (p->sym == q->sym)
4343 gfc_error ("Duplicate symbol '%s' in formal argument list "
4344 "at %C", p->sym->name);
4352 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
4362 gfc_free_formal_arglist (head);
4367 /* Match a RESULT specification following a function declaration or
4368 ENTRY statement. Also matches the end-of-statement. */
4371 match_result (gfc_symbol *function, gfc_symbol **result)
4373 char name[GFC_MAX_SYMBOL_LEN + 1];
4377 if (gfc_match (" result (") != MATCH_YES)
4380 m = gfc_match_name (name);
4384 /* Get the right paren, and that's it because there could be the
4385 bind(c) attribute after the result clause. */
4386 if (gfc_match_char(')') != MATCH_YES)
4388 /* TODO: should report the missing right paren here. */
4392 if (strcmp (function->name, name) == 0)
4394 gfc_error ("RESULT variable at %C must be different than function name");
4398 if (gfc_get_symbol (name, NULL, &r))
4401 if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4410 /* Match a function suffix, which could be a combination of a result
4411 clause and BIND(C), either one, or neither. The draft does not
4412 require them to come in a specific order. */
4415 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4417 match is_bind_c; /* Found bind(c). */
4418 match is_result; /* Found result clause. */
4419 match found_match; /* Status of whether we've found a good match. */
4420 char peek_char; /* Character we're going to peek at. */
4421 bool allow_binding_name;
4423 /* Initialize to having found nothing. */
4424 found_match = MATCH_NO;
4425 is_bind_c = MATCH_NO;
4426 is_result = MATCH_NO;
4428 /* Get the next char to narrow between result and bind(c). */
4429 gfc_gobble_whitespace ();
4430 peek_char = gfc_peek_ascii_char ();
4432 /* C binding names are not allowed for internal procedures. */
4433 if (gfc_current_state () == COMP_CONTAINS
4434 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4435 allow_binding_name = false;
4437 allow_binding_name = true;
4442 /* Look for result clause. */
4443 is_result = match_result (sym, result);
4444 if (is_result == MATCH_YES)
4446 /* Now see if there is a bind(c) after it. */
4447 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4448 /* We've found the result clause and possibly bind(c). */
4449 found_match = MATCH_YES;
4452 /* This should only be MATCH_ERROR. */
4453 found_match = is_result;
4456 /* Look for bind(c) first. */
4457 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4458 if (is_bind_c == MATCH_YES)
4460 /* Now see if a result clause followed it. */
4461 is_result = match_result (sym, result);
4462 found_match = MATCH_YES;
4466 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4467 found_match = MATCH_ERROR;
4471 gfc_error ("Unexpected junk after function declaration at %C");
4472 found_match = MATCH_ERROR;
4476 if (is_bind_c == MATCH_YES)
4478 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4479 if (gfc_current_state () == COMP_CONTAINS
4480 && sym->ns->proc_name->attr.flavor != FL_MODULE
4481 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4482 "at %L may not be specified for an internal "
4483 "procedure", &gfc_current_locus)
4487 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4496 /* Procedure pointer return value without RESULT statement:
4497 Add "hidden" result variable named "ppr@". */
4500 add_hidden_procptr_result (gfc_symbol *sym)
4504 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4507 /* First usage case: PROCEDURE and EXTERNAL statements. */
4508 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4509 && strcmp (gfc_current_block ()->name, sym->name) == 0
4510 && sym->attr.external;
4511 /* Second usage case: INTERFACE statements. */
4512 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4513 && gfc_state_stack->previous->state == COMP_FUNCTION
4514 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4520 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4524 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4525 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4526 st2->n.sym = stree->n.sym;
4528 sym->result = stree->n.sym;
4530 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4531 sym->result->attr.pointer = sym->attr.pointer;
4532 sym->result->attr.external = sym->attr.external;
4533 sym->result->attr.referenced = sym->attr.referenced;
4534 sym->result->ts = sym->ts;
4535 sym->attr.proc_pointer = 0;
4536 sym->attr.pointer = 0;
4537 sym->attr.external = 0;
4538 if (sym->result->attr.external && sym->result->attr.pointer)
4540 sym->result->attr.pointer = 0;
4541 sym->result->attr.proc_pointer = 1;
4544 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4546 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4547 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4548 && sym->result && sym->result != sym && sym->result->attr.external
4549 && sym == gfc_current_ns->proc_name
4550 && sym == sym->result->ns->proc_name
4551 && strcmp ("ppr@", sym->result->name) == 0)
4553 sym->result->attr.proc_pointer = 1;
4554 sym->attr.pointer = 0;
4562 /* Match the interface for a PROCEDURE declaration,
4563 including brackets (R1212). */
4566 match_procedure_interface (gfc_symbol **proc_if)
4570 locus old_loc, entry_loc;
4571 gfc_namespace *old_ns = gfc_current_ns;
4572 char name[GFC_MAX_SYMBOL_LEN + 1];
4574 old_loc = entry_loc = gfc_current_locus;
4575 gfc_clear_ts (¤t_ts);
4577 if (gfc_match (" (") != MATCH_YES)
4579 gfc_current_locus = entry_loc;
4583 /* Get the type spec. for the procedure interface. */
4584 old_loc = gfc_current_locus;
4585 m = gfc_match_decl_type_spec (¤t_ts, 0);
4586 gfc_gobble_whitespace ();
4587 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4590 if (m == MATCH_ERROR)
4593 /* Procedure interface is itself a procedure. */
4594 gfc_current_locus = old_loc;
4595 m = gfc_match_name (name);
4597 /* First look to see if it is already accessible in the current
4598 namespace because it is use associated or contained. */
4600 if (gfc_find_sym_tree (name, NULL, 0, &st))
4603 /* If it is still not found, then try the parent namespace, if it
4604 exists and create the symbol there if it is still not found. */
4605 if (gfc_current_ns->parent)
4606 gfc_current_ns = gfc_current_ns->parent;
4607 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4610 gfc_current_ns = old_ns;
4611 *proc_if = st->n.sym;
4613 /* Various interface checks. */
4617 /* Resolve interface if possible. That way, attr.procedure is only set
4618 if it is declared by a later procedure-declaration-stmt, which is
4619 invalid per C1212. */
4620 while ((*proc_if)->ts.interface)
4621 *proc_if = (*proc_if)->ts.interface;
4623 if ((*proc_if)->generic)
4625 gfc_error ("Interface '%s' at %C may not be generic",
4629 if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
4631 gfc_error ("Interface '%s' at %C may not be a statement function",
4635 /* Handle intrinsic procedures. */
4636 if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4637 || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4638 && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4639 || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4640 (*proc_if)->attr.intrinsic = 1;
4641 if ((*proc_if)->attr.intrinsic
4642 && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
4644 gfc_error ("Intrinsic procedure '%s' not allowed "
4645 "in PROCEDURE statement at %C", (*proc_if)->name);
4651 if (gfc_match (" )") != MATCH_YES)
4653 gfc_current_locus = entry_loc;
4661 /* Match a PROCEDURE declaration (R1211). */
4664 match_procedure_decl (void)
4667 gfc_symbol *sym, *proc_if = NULL;
4669 gfc_expr *initializer = NULL;
4671 /* Parse interface (with brackets). */
4672 m = match_procedure_interface (&proc_if);
4676 /* Parse attributes (with colons). */
4677 m = match_attr_spec();
4678 if (m == MATCH_ERROR)
4681 /* Get procedure symbols. */
4684 m = gfc_match_symbol (&sym, 0);
4687 else if (m == MATCH_ERROR)
4690 /* Add current_attr to the symbol attributes. */
4691 if (gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
4694 if (sym->attr.is_bind_c)
4696 /* Check for C1218. */
4697 if (!proc_if || !proc_if->attr.is_bind_c)
4699 gfc_error ("BIND(C) attribute at %C requires "
4700 "an interface with BIND(C)");
4703 /* Check for C1217. */
4704 if (has_name_equals && sym->attr.pointer)
4706 gfc_error ("BIND(C) procedure with NAME may not have "
4707 "POINTER attribute at %C");
4710 if (has_name_equals && sym->attr.dummy)
4712 gfc_error ("Dummy procedure at %C may not have "
4713 "BIND(C) attribute with NAME");
4716 /* Set binding label for BIND(C). */
4717 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4721 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4724 if (add_hidden_procptr_result (sym) == SUCCESS)
4727 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4730 /* Set interface. */
4731 if (proc_if != NULL)
4733 if (sym->ts.type != BT_UNKNOWN)
4735 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4736 sym->name, &gfc_current_locus,
4737 gfc_basic_typename (sym->ts.type));
4740 sym->ts.interface = proc_if;
4741 sym->attr.untyped = 1;
4742 sym->attr.if_source = IFSRC_IFBODY;
4744 else if (current_ts.type != BT_UNKNOWN)
4746 if (gfc_add_type (sym, ¤t_ts, &gfc_current_locus) == FAILURE)
4748 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4749 sym->ts.interface->ts = current_ts;
4750 sym->ts.interface->attr.flavor = FL_PROCEDURE;
4751 sym->ts.interface->attr.function = 1;
4752 sym->attr.function = 1;
4753 sym->attr.if_source = IFSRC_UNKNOWN;
4756 if (gfc_match (" =>") == MATCH_YES)
4758 if (!current_attr.pointer)
4760 gfc_error ("Initialization at %C isn't for a pointer variable");
4765 m = match_pointer_init (&initializer, 1);
4769 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4775 gfc_set_sym_referenced (sym);
4777 if (gfc_match_eos () == MATCH_YES)
4779 if (gfc_match_char (',') != MATCH_YES)
4784 gfc_error ("Syntax error in PROCEDURE statement at %C");
4788 /* Free stuff up and return. */
4789 gfc_free_expr (initializer);
4795 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4798 /* Match a procedure pointer component declaration (R445). */
4801 match_ppc_decl (void)
4804 gfc_symbol *proc_if = NULL;
4808 gfc_expr *initializer = NULL;
4809 gfc_typebound_proc* tb;
4810 char name[GFC_MAX_SYMBOL_LEN + 1];
4812 /* Parse interface (with brackets). */
4813 m = match_procedure_interface (&proc_if);
4817 /* Parse attributes. */
4818 tb = XCNEW (gfc_typebound_proc);
4819 tb->where = gfc_current_locus;
4820 m = match_binding_attributes (tb, false, true);
4821 if (m == MATCH_ERROR)
4824 gfc_clear_attr (¤t_attr);
4825 current_attr.procedure = 1;
4826 current_attr.proc_pointer = 1;
4827 current_attr.access = tb->access;
4828 current_attr.flavor = FL_PROCEDURE;
4830 /* Match the colons (required). */
4831 if (gfc_match (" ::") != MATCH_YES)
4833 gfc_error ("Expected '::' after binding-attributes at %C");
4837 /* Check for C450. */
4838 if (!tb->nopass && proc_if == NULL)
4840 gfc_error("NOPASS or explicit interface required at %C");
4844 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure pointer "
4845 "component at %C") == FAILURE)
4848 /* Match PPC names. */
4852 m = gfc_match_name (name);
4855 else if (m == MATCH_ERROR)
4858 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
4861 /* Add current_attr to the symbol attributes. */
4862 if (gfc_copy_attr (&c->attr, ¤t_attr, NULL) == FAILURE)
4865 if (gfc_add_external (&c->attr, NULL) == FAILURE)
4868 if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
4873 /* Set interface. */
4874 if (proc_if != NULL)
4876 c->ts.interface = proc_if;
4877 c->attr.untyped = 1;
4878 c->attr.if_source = IFSRC_IFBODY;
4880 else if (ts.type != BT_UNKNOWN)
4883 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4884 c->ts.interface->ts = ts;
4885 c->ts.interface->attr.flavor = FL_PROCEDURE;
4886 c->ts.interface->attr.function = 1;
4887 c->attr.function = 1;
4888 c->attr.if_source = IFSRC_UNKNOWN;
4891 if (gfc_match (" =>") == MATCH_YES)
4893 m = match_pointer_init (&initializer, 1);
4896 gfc_free_expr (initializer);
4899 c->initializer = initializer;
4902 if (gfc_match_eos () == MATCH_YES)
4904 if (gfc_match_char (',') != MATCH_YES)
4909 gfc_error ("Syntax error in procedure pointer component at %C");
4914 /* Match a PROCEDURE declaration inside an interface (R1206). */
4917 match_procedure_in_interface (void)
4921 char name[GFC_MAX_SYMBOL_LEN + 1];
4923 if (current_interface.type == INTERFACE_NAMELESS
4924 || current_interface.type == INTERFACE_ABSTRACT)
4926 gfc_error ("PROCEDURE at %C must be in a generic interface");
4932 m = gfc_match_name (name);
4935 else if (m == MATCH_ERROR)
4937 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4940 if (gfc_add_interface (sym) == FAILURE)
4943 if (gfc_match_eos () == MATCH_YES)
4945 if (gfc_match_char (',') != MATCH_YES)
4952 gfc_error ("Syntax error in PROCEDURE statement at %C");
4957 /* General matcher for PROCEDURE declarations. */
4959 static match match_procedure_in_type (void);
4962 gfc_match_procedure (void)
4966 switch (gfc_current_state ())
4971 case COMP_SUBROUTINE:
4974 m = match_procedure_decl ();
4976 case COMP_INTERFACE:
4977 m = match_procedure_in_interface ();
4980 m = match_ppc_decl ();
4982 case COMP_DERIVED_CONTAINS:
4983 m = match_procedure_in_type ();
4992 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
5000 /* Warn if a matched procedure has the same name as an intrinsic; this is
5001 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5002 parser-state-stack to find out whether we're in a module. */
5005 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5009 in_module = (gfc_state_stack->previous
5010 && gfc_state_stack->previous->state == COMP_MODULE);
5012 gfc_warn_intrinsic_shadow (sym, in_module, func);
5016 /* Match a function declaration. */
5019 gfc_match_function_decl (void)
5021 char name[GFC_MAX_SYMBOL_LEN + 1];
5022 gfc_symbol *sym, *result;
5026 match found_match; /* Status returned by match func. */
5028 if (gfc_current_state () != COMP_NONE
5029 && gfc_current_state () != COMP_INTERFACE
5030 && gfc_current_state () != COMP_CONTAINS)
5033 gfc_clear_ts (¤t_ts);
5035 old_loc = gfc_current_locus;
5037 m = gfc_match_prefix (¤t_ts);
5040 gfc_current_locus = old_loc;
5044 if (gfc_match ("function% %n", name) != MATCH_YES)
5046 gfc_current_locus = old_loc;
5049 if (get_proc_name (name, &sym, false))
5052 if (add_hidden_procptr_result (sym) == SUCCESS)
5055 gfc_new_block = sym;
5057 m = gfc_match_formal_arglist (sym, 0, 0);
5060 gfc_error ("Expected formal argument list in function "
5061 "definition at %C");
5065 else if (m == MATCH_ERROR)
5070 /* According to the draft, the bind(c) and result clause can
5071 come in either order after the formal_arg_list (i.e., either
5072 can be first, both can exist together or by themselves or neither
5073 one). Therefore, the match_result can't match the end of the
5074 string, and check for the bind(c) or result clause in either order. */
5075 found_match = gfc_match_eos ();
5077 /* Make sure that it isn't already declared as BIND(C). If it is, it
5078 must have been marked BIND(C) with a BIND(C) attribute and that is
5079 not allowed for procedures. */
5080 if (sym->attr.is_bind_c == 1)
5082 sym->attr.is_bind_c = 0;
5083 if (sym->old_symbol != NULL)
5084 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5085 "variables or common blocks",
5086 &(sym->old_symbol->declared_at));
5088 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5089 "variables or common blocks", &gfc_current_locus);
5092 if (found_match != MATCH_YES)
5094 /* If we haven't found the end-of-statement, look for a suffix. */
5095 suffix_match = gfc_match_suffix (sym, &result);
5096 if (suffix_match == MATCH_YES)
5097 /* Need to get the eos now. */
5098 found_match = gfc_match_eos ();
5100 found_match = suffix_match;
5103 if(found_match != MATCH_YES)
5107 /* Make changes to the symbol. */
5110 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
5113 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
5114 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5117 /* Delay matching the function characteristics until after the
5118 specification block by signalling kind=-1. */
5119 sym->declared_at = old_loc;
5120 if (current_ts.type != BT_UNKNOWN)
5121 current_ts.kind = -1;
5123 current_ts.kind = 0;
5127 if (current_ts.type != BT_UNKNOWN
5128 && gfc_add_type (sym, ¤t_ts, &gfc_current_locus) == FAILURE)
5134 if (current_ts.type != BT_UNKNOWN
5135 && gfc_add_type (result, ¤t_ts, &gfc_current_locus)
5138 sym->result = result;
5141 /* Warn if this procedure has the same name as an intrinsic. */
5142 warn_intrinsic_shadow (sym, true);
5148 gfc_current_locus = old_loc;
5153 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5154 pass the name of the entry, rather than the gfc_current_block name, and
5155 to return false upon finding an existing global entry. */
5158 add_global_entry (const char *name, int sub)
5161 enum gfc_symbol_type type;
5163 s = gfc_get_gsymbol(name);
5164 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5167 || (s->type != GSYM_UNKNOWN
5168 && s->type != type))
5169 gfc_global_used(s, NULL);
5173 s->where = gfc_current_locus;
5175 s->ns = gfc_current_ns;
5182 /* Match an ENTRY statement. */
5185 gfc_match_entry (void)
5190 char name[GFC_MAX_SYMBOL_LEN + 1];
5191 gfc_compile_state state;
5195 bool module_procedure;
5199 m = gfc_match_name (name);
5203 if (gfc_notify_std (GFC_STD_F2008_OBS, "Fortran 2008 obsolescent feature: "
5204 "ENTRY statement at %C") == FAILURE)
5207 state = gfc_current_state ();
5208 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5213 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5216 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5218 case COMP_BLOCK_DATA:
5219 gfc_error ("ENTRY statement at %C cannot appear within "
5222 case COMP_INTERFACE:
5223 gfc_error ("ENTRY statement at %C cannot appear within "
5227 gfc_error ("ENTRY statement at %C cannot appear within "
5228 "a DERIVED TYPE block");
5231 gfc_error ("ENTRY statement at %C cannot appear within "
5232 "an IF-THEN block");
5235 gfc_error ("ENTRY statement at %C cannot appear within "
5239 gfc_error ("ENTRY statement at %C cannot appear within "
5243 gfc_error ("ENTRY statement at %C cannot appear within "
5247 gfc_error ("ENTRY statement at %C cannot appear within "
5251 gfc_error ("ENTRY statement at %C cannot appear within "
5252 "a contained subprogram");
5255 gfc_internal_error ("gfc_match_entry(): Bad state");
5260 module_procedure = gfc_current_ns->parent != NULL
5261 && gfc_current_ns->parent->proc_name
5262 && gfc_current_ns->parent->proc_name->attr.flavor
5265 if (gfc_current_ns->parent != NULL
5266 && gfc_current_ns->parent->proc_name
5267 && !module_procedure)
5269 gfc_error("ENTRY statement at %C cannot appear in a "
5270 "contained procedure");
5274 /* Module function entries need special care in get_proc_name
5275 because previous references within the function will have
5276 created symbols attached to the current namespace. */
5277 if (get_proc_name (name, &entry,
5278 gfc_current_ns->parent != NULL
5279 && module_procedure))
5282 proc = gfc_current_block ();
5284 /* Make sure that it isn't already declared as BIND(C). If it is, it
5285 must have been marked BIND(C) with a BIND(C) attribute and that is
5286 not allowed for procedures. */
5287 if (entry->attr.is_bind_c == 1)
5289 entry->attr.is_bind_c = 0;
5290 if (entry->old_symbol != NULL)
5291 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5292 "variables or common blocks",
5293 &(entry->old_symbol->declared_at));
5295 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5296 "variables or common blocks", &gfc_current_locus);
5299 /* Check what next non-whitespace character is so we can tell if there
5300 is the required parens if we have a BIND(C). */
5301 gfc_gobble_whitespace ();
5302 peek_char = gfc_peek_ascii_char ();
5304 if (state == COMP_SUBROUTINE)
5306 /* An entry in a subroutine. */
5307 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
5310 m = gfc_match_formal_arglist (entry, 0, 1);
5314 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5315 never be an internal procedure. */
5316 is_bind_c = gfc_match_bind_c (entry, true);
5317 if (is_bind_c == MATCH_ERROR)
5319 if (is_bind_c == MATCH_YES)
5321 if (peek_char != '(')
5323 gfc_error ("Missing required parentheses before BIND(C) at %C");
5326 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
5331 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5332 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
5337 /* An entry in a function.
5338 We need to take special care because writing
5343 ENTRY f() RESULT (r)
5345 ENTRY f RESULT (r). */
5346 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
5349 old_loc = gfc_current_locus;
5350 if (gfc_match_eos () == MATCH_YES)
5352 gfc_current_locus = old_loc;
5353 /* Match the empty argument list, and add the interface to
5355 m = gfc_match_formal_arglist (entry, 0, 1);
5358 m = gfc_match_formal_arglist (entry, 0, 0);
5365 if (gfc_match_eos () == MATCH_YES)
5367 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5368 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5371 entry->result = entry;
5375 m = gfc_match_suffix (entry, &result);
5377 gfc_syntax_error (ST_ENTRY);
5383 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
5384 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
5385 || gfc_add_function (&entry->attr, result->name, NULL)
5388 entry->result = result;
5392 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5393 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5395 entry->result = entry;
5400 if (gfc_match_eos () != MATCH_YES)
5402 gfc_syntax_error (ST_ENTRY);
5406 entry->attr.recursive = proc->attr.recursive;
5407 entry->attr.elemental = proc->attr.elemental;
5408 entry->attr.pure = proc->attr.pure;
5410 el = gfc_get_entry_list ();
5412 el->next = gfc_current_ns->entries;
5413 gfc_current_ns->entries = el;
5415 el->id = el->next->id + 1;
5419 new_st.op = EXEC_ENTRY;
5420 new_st.ext.entry = el;
5426 /* Match a subroutine statement, including optional prefixes. */
5429 gfc_match_subroutine (void)
5431 char name[GFC_MAX_SYMBOL_LEN + 1];
5436 bool allow_binding_name;
5438 if (gfc_current_state () != COMP_NONE
5439 && gfc_current_state () != COMP_INTERFACE
5440 && gfc_current_state () != COMP_CONTAINS)
5443 m = gfc_match_prefix (NULL);
5447 m = gfc_match ("subroutine% %n", name);
5451 if (get_proc_name (name, &sym, false))
5454 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5455 the symbol existed before. */
5456 sym->declared_at = gfc_current_locus;
5458 if (add_hidden_procptr_result (sym) == SUCCESS)
5461 gfc_new_block = sym;
5463 /* Check what next non-whitespace character is so we can tell if there
5464 is the required parens if we have a BIND(C). */
5465 gfc_gobble_whitespace ();
5466 peek_char = gfc_peek_ascii_char ();
5468 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5471 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5474 /* Make sure that it isn't already declared as BIND(C). If it is, it
5475 must have been marked BIND(C) with a BIND(C) attribute and that is
5476 not allowed for procedures. */
5477 if (sym->attr.is_bind_c == 1)
5479 sym->attr.is_bind_c = 0;
5480 if (sym->old_symbol != NULL)
5481 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5482 "variables or common blocks",
5483 &(sym->old_symbol->declared_at));
5485 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5486 "variables or common blocks", &gfc_current_locus);
5489 /* C binding names are not allowed for internal procedures. */
5490 if (gfc_current_state () == COMP_CONTAINS
5491 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5492 allow_binding_name = false;
5494 allow_binding_name = true;
5496 /* Here, we are just checking if it has the bind(c) attribute, and if
5497 so, then we need to make sure it's all correct. If it doesn't,
5498 we still need to continue matching the rest of the subroutine line. */
5499 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5500 if (is_bind_c == MATCH_ERROR)
5502 /* There was an attempt at the bind(c), but it was wrong. An
5503 error message should have been printed w/in the gfc_match_bind_c
5504 so here we'll just return the MATCH_ERROR. */
5508 if (is_bind_c == MATCH_YES)
5510 /* The following is allowed in the Fortran 2008 draft. */
5511 if (gfc_current_state () == COMP_CONTAINS
5512 && sym->ns->proc_name->attr.flavor != FL_MODULE
5513 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5514 "at %L may not be specified for an internal "
5515 "procedure", &gfc_current_locus)
5519 if (peek_char != '(')
5521 gfc_error ("Missing required parentheses before BIND(C) at %C");
5524 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5529 if (gfc_match_eos () != MATCH_YES)
5531 gfc_syntax_error (ST_SUBROUTINE);
5535 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5538 /* Warn if it has the same name as an intrinsic. */
5539 warn_intrinsic_shadow (sym, false);
5545 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5546 given, and set the binding label in either the given symbol (if not
5547 NULL), or in the current_ts. The symbol may be NULL because we may
5548 encounter the BIND(C) before the declaration itself. Return
5549 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5550 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5551 or MATCH_YES if the specifier was correct and the binding label and
5552 bind(c) fields were set correctly for the given symbol or the
5553 current_ts. If allow_binding_name is false, no binding name may be
5557 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5559 /* binding label, if exists */
5560 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
5564 /* Initialize the flag that specifies whether we encountered a NAME=
5565 specifier or not. */
5566 has_name_equals = 0;
5568 /* Init the first char to nil so we can catch if we don't have
5569 the label (name attr) or the symbol name yet. */
5570 binding_label[0] = '\0';
5572 /* This much we have to be able to match, in this order, if
5573 there is a bind(c) label. */
5574 if (gfc_match (" bind ( c ") != MATCH_YES)
5577 /* Now see if there is a binding label, or if we've reached the
5578 end of the bind(c) attribute without one. */
5579 if (gfc_match_char (',') == MATCH_YES)
5581 if (gfc_match (" name = ") != MATCH_YES)
5583 gfc_error ("Syntax error in NAME= specifier for binding label "
5585 /* should give an error message here */
5589 has_name_equals = 1;
5591 /* Get the opening quote. */
5592 double_quote = MATCH_YES;
5593 single_quote = MATCH_YES;
5594 double_quote = gfc_match_char ('"');
5595 if (double_quote != MATCH_YES)
5596 single_quote = gfc_match_char ('\'');
5597 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5599 gfc_error ("Syntax error in NAME= specifier for binding label "
5604 /* Grab the binding label, using functions that will not lower
5605 case the names automatically. */
5606 if (gfc_match_name_C (binding_label) != MATCH_YES)
5609 /* Get the closing quotation. */
5610 if (double_quote == MATCH_YES)
5612 if (gfc_match_char ('"') != MATCH_YES)
5614 gfc_error ("Missing closing quote '\"' for binding label at %C");
5615 /* User started string with '"' so looked to match it. */
5621 if (gfc_match_char ('\'') != MATCH_YES)
5623 gfc_error ("Missing closing quote '\'' for binding label at %C");
5624 /* User started string with "'" char. */
5630 /* Get the required right paren. */
5631 if (gfc_match_char (')') != MATCH_YES)
5633 gfc_error ("Missing closing paren for binding label at %C");
5637 if (has_name_equals && !allow_binding_name)
5639 gfc_error ("No binding name is allowed in BIND(C) at %C");
5643 if (has_name_equals && sym != NULL && sym->attr.dummy)
5645 gfc_error ("For dummy procedure %s, no binding name is "
5646 "allowed in BIND(C) at %C", sym->name);
5651 /* Save the binding label to the symbol. If sym is null, we're
5652 probably matching the typespec attributes of a declaration and
5653 haven't gotten the name yet, and therefore, no symbol yet. */
5654 if (binding_label[0] != '\0')
5658 strcpy (sym->binding_label, binding_label);
5661 strcpy (curr_binding_label, binding_label);
5663 else if (allow_binding_name)
5665 /* No binding label, but if symbol isn't null, we
5666 can set the label for it here.
5667 If name="" or allow_binding_name is false, no C binding name is
5669 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5670 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
5673 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5674 && current_interface.type == INTERFACE_ABSTRACT)
5676 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5684 /* Return nonzero if we're currently compiling a contained procedure. */
5687 contained_procedure (void)
5689 gfc_state_data *s = gfc_state_stack;
5691 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5692 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5698 /* Set the kind of each enumerator. The kind is selected such that it is
5699 interoperable with the corresponding C enumeration type, making
5700 sure that -fshort-enums is honored. */
5705 enumerator_history *current_history = NULL;
5709 if (max_enum == NULL || enum_history == NULL)
5712 if (!flag_short_enums)
5718 kind = gfc_integer_kinds[i++].kind;
5720 while (kind < gfc_c_int_kind
5721 && gfc_check_integer_range (max_enum->initializer->value.integer,
5724 current_history = enum_history;
5725 while (current_history != NULL)
5727 current_history->sym->ts.kind = kind;
5728 current_history = current_history->next;
5733 /* Match any of the various end-block statements. Returns the type of
5734 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5735 and END BLOCK statements cannot be replaced by a single END statement. */
5738 gfc_match_end (gfc_statement *st)
5740 char name[GFC_MAX_SYMBOL_LEN + 1];
5741 gfc_compile_state state;
5743 const char *block_name;
5748 old_loc = gfc_current_locus;
5749 if (gfc_match ("end") != MATCH_YES)
5752 state = gfc_current_state ();
5753 block_name = gfc_current_block () == NULL
5754 ? NULL : gfc_current_block ()->name;
5758 case COMP_ASSOCIATE:
5760 if (!strncmp (block_name, "block@", strlen("block@")))
5765 case COMP_DERIVED_CONTAINS:
5766 state = gfc_state_stack->previous->state;
5767 block_name = gfc_state_stack->previous->sym == NULL
5768 ? NULL : gfc_state_stack->previous->sym->name;
5779 *st = ST_END_PROGRAM;
5780 target = " program";
5784 case COMP_SUBROUTINE:
5785 *st = ST_END_SUBROUTINE;
5786 target = " subroutine";
5787 eos_ok = !contained_procedure ();
5791 *st = ST_END_FUNCTION;
5792 target = " function";
5793 eos_ok = !contained_procedure ();
5796 case COMP_BLOCK_DATA:
5797 *st = ST_END_BLOCK_DATA;
5798 target = " block data";
5803 *st = ST_END_MODULE;
5808 case COMP_INTERFACE:
5809 *st = ST_END_INTERFACE;
5810 target = " interface";
5815 case COMP_DERIVED_CONTAINS:
5821 case COMP_ASSOCIATE:
5822 *st = ST_END_ASSOCIATE;
5823 target = " associate";
5846 *st = ST_END_CRITICAL;
5847 target = " critical";
5852 case COMP_SELECT_TYPE:
5853 *st = ST_END_SELECT;
5859 *st = ST_END_FORALL;
5874 last_initializer = NULL;
5876 gfc_free_enum_history ();
5880 gfc_error ("Unexpected END statement at %C");
5884 if (gfc_match_eos () == MATCH_YES)
5886 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
5888 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: END statement "
5889 "instead of %s statement at %L",
5890 gfc_ascii_statement (*st), &old_loc) == FAILURE)
5895 /* We would have required END [something]. */
5896 gfc_error ("%s statement expected at %L",
5897 gfc_ascii_statement (*st), &old_loc);
5904 /* Verify that we've got the sort of end-block that we're expecting. */
5905 if (gfc_match (target) != MATCH_YES)
5907 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5911 /* If we're at the end, make sure a block name wasn't required. */
5912 if (gfc_match_eos () == MATCH_YES)
5915 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5916 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
5917 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
5923 gfc_error ("Expected block name of '%s' in %s statement at %C",
5924 block_name, gfc_ascii_statement (*st));
5929 /* END INTERFACE has a special handler for its several possible endings. */
5930 if (*st == ST_END_INTERFACE)
5931 return gfc_match_end_interface ();
5933 /* We haven't hit the end of statement, so what is left must be an
5935 m = gfc_match_space ();
5937 m = gfc_match_name (name);
5940 gfc_error ("Expected terminating name at %C");
5944 if (block_name == NULL)
5947 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
5949 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5950 gfc_ascii_statement (*st));
5953 /* Procedure pointer as function result. */
5954 else if (strcmp (block_name, "ppr@") == 0
5955 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
5957 gfc_error ("Expected label '%s' for %s statement at %C",
5958 gfc_current_block ()->ns->proc_name->name,
5959 gfc_ascii_statement (*st));
5963 if (gfc_match_eos () == MATCH_YES)
5967 gfc_syntax_error (*st);
5970 gfc_current_locus = old_loc;
5976 /***************** Attribute declaration statements ****************/
5978 /* Set the attribute of a single variable. */
5983 char name[GFC_MAX_SYMBOL_LEN + 1];
5991 m = gfc_match_name (name);
5995 if (find_special (name, &sym, false))
5998 var_locus = gfc_current_locus;
6000 /* Deal with possible array specification for certain attributes. */
6001 if (current_attr.dimension
6002 || current_attr.codimension
6003 || current_attr.allocatable
6004 || current_attr.pointer
6005 || current_attr.target)
6007 m = gfc_match_array_spec (&as, !current_attr.codimension,
6008 !current_attr.dimension
6009 && !current_attr.pointer
6010 && !current_attr.target);
6011 if (m == MATCH_ERROR)
6014 if (current_attr.dimension && m == MATCH_NO)
6016 gfc_error ("Missing array specification at %L in DIMENSION "
6017 "statement", &var_locus);
6022 if (current_attr.dimension && sym->value)
6024 gfc_error ("Dimensions specified for %s at %L after its "
6025 "initialisation", sym->name, &var_locus);
6030 if (current_attr.codimension && m == MATCH_NO)
6032 gfc_error ("Missing array specification at %L in CODIMENSION "
6033 "statement", &var_locus);
6038 if ((current_attr.allocatable || current_attr.pointer)
6039 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6041 gfc_error ("Array specification must be deferred at %L", &var_locus);
6047 /* Update symbol table. DIMENSION attribute is set in
6048 gfc_set_array_spec(). For CLASS variables, this must be applied
6049 to the first component, or '_data' field. */
6050 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6052 if (gfc_copy_attr (&CLASS_DATA (sym)->attr, ¤t_attr, &var_locus)
6061 if (current_attr.dimension == 0 && current_attr.codimension == 0
6062 && gfc_copy_attr (&sym->attr, ¤t_attr, &var_locus) == FAILURE)
6069 if (sym->ts.type == BT_CLASS
6070 && gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false) == FAILURE)
6076 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
6082 if (sym->attr.cray_pointee && sym->as != NULL)
6084 /* Fix the array spec. */
6085 m = gfc_mod_pointee_as (sym->as);
6086 if (m == MATCH_ERROR)
6090 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
6096 if ((current_attr.external || current_attr.intrinsic)
6097 && sym->attr.flavor != FL_PROCEDURE
6098 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6104 add_hidden_procptr_result (sym);
6109 gfc_free_array_spec (as);
6114 /* Generic attribute declaration subroutine. Used for attributes that
6115 just have a list of names. */
6122 /* Gobble the optional double colon, by simply ignoring the result
6132 if (gfc_match_eos () == MATCH_YES)
6138 if (gfc_match_char (',') != MATCH_YES)
6140 gfc_error ("Unexpected character in variable list at %C");
6150 /* This routine matches Cray Pointer declarations of the form:
6151 pointer ( <pointer>, <pointee> )
6153 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6154 The pointer, if already declared, should be an integer. Otherwise, we
6155 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6156 be either a scalar, or an array declaration. No space is allocated for
6157 the pointee. For the statement
6158 pointer (ipt, ar(10))
6159 any subsequent uses of ar will be translated (in C-notation) as
6160 ar(i) => ((<type> *) ipt)(i)
6161 After gimplification, pointee variable will disappear in the code. */
6164 cray_pointer_decl (void)
6167 gfc_array_spec *as = NULL;
6168 gfc_symbol *cptr; /* Pointer symbol. */
6169 gfc_symbol *cpte; /* Pointee symbol. */
6175 if (gfc_match_char ('(') != MATCH_YES)
6177 gfc_error ("Expected '(' at %C");
6181 /* Match pointer. */
6182 var_locus = gfc_current_locus;
6183 gfc_clear_attr (¤t_attr);
6184 gfc_add_cray_pointer (¤t_attr, &var_locus);
6185 current_ts.type = BT_INTEGER;
6186 current_ts.kind = gfc_index_integer_kind;
6188 m = gfc_match_symbol (&cptr, 0);
6191 gfc_error ("Expected variable name at %C");
6195 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
6198 gfc_set_sym_referenced (cptr);
6200 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6202 cptr->ts.type = BT_INTEGER;
6203 cptr->ts.kind = gfc_index_integer_kind;
6205 else if (cptr->ts.type != BT_INTEGER)
6207 gfc_error ("Cray pointer at %C must be an integer");
6210 else if (cptr->ts.kind < gfc_index_integer_kind)
6211 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6212 " memory addresses require %d bytes",
6213 cptr->ts.kind, gfc_index_integer_kind);
6215 if (gfc_match_char (',') != MATCH_YES)
6217 gfc_error ("Expected \",\" at %C");
6221 /* Match Pointee. */
6222 var_locus = gfc_current_locus;
6223 gfc_clear_attr (¤t_attr);
6224 gfc_add_cray_pointee (¤t_attr, &var_locus);
6225 current_ts.type = BT_UNKNOWN;
6226 current_ts.kind = 0;
6228 m = gfc_match_symbol (&cpte, 0);
6231 gfc_error ("Expected variable name at %C");
6235 /* Check for an optional array spec. */
6236 m = gfc_match_array_spec (&as, true, false);
6237 if (m == MATCH_ERROR)
6239 gfc_free_array_spec (as);
6242 else if (m == MATCH_NO)
6244 gfc_free_array_spec (as);
6248 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
6251 gfc_set_sym_referenced (cpte);
6253 if (cpte->as == NULL)
6255 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
6256 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6258 else if (as != NULL)
6260 gfc_error ("Duplicate array spec for Cray pointee at %C");
6261 gfc_free_array_spec (as);
6267 if (cpte->as != NULL)
6269 /* Fix array spec. */
6270 m = gfc_mod_pointee_as (cpte->as);
6271 if (m == MATCH_ERROR)
6275 /* Point the Pointee at the Pointer. */
6276 cpte->cp_pointer = cptr;
6278 if (gfc_match_char (')') != MATCH_YES)
6280 gfc_error ("Expected \")\" at %C");
6283 m = gfc_match_char (',');
6285 done = true; /* Stop searching for more declarations. */
6289 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6290 || gfc_match_eos () != MATCH_YES)
6292 gfc_error ("Expected \",\" or end of statement at %C");
6300 gfc_match_external (void)
6303 gfc_clear_attr (¤t_attr);
6304 current_attr.external = 1;
6306 return attr_decl ();
6311 gfc_match_intent (void)
6315 /* This is not allowed within a BLOCK construct! */
6316 if (gfc_current_state () == COMP_BLOCK)
6318 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6322 intent = match_intent_spec ();
6323 if (intent == INTENT_UNKNOWN)
6326 gfc_clear_attr (¤t_attr);
6327 current_attr.intent = intent;
6329 return attr_decl ();
6334 gfc_match_intrinsic (void)
6337 gfc_clear_attr (¤t_attr);
6338 current_attr.intrinsic = 1;
6340 return attr_decl ();
6345 gfc_match_optional (void)
6347 /* This is not allowed within a BLOCK construct! */
6348 if (gfc_current_state () == COMP_BLOCK)
6350 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6354 gfc_clear_attr (¤t_attr);
6355 current_attr.optional = 1;
6357 return attr_decl ();
6362 gfc_match_pointer (void)
6364 gfc_gobble_whitespace ();
6365 if (gfc_peek_ascii_char () == '(')
6367 if (!gfc_option.flag_cray_pointer)
6369 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6373 return cray_pointer_decl ();
6377 gfc_clear_attr (¤t_attr);
6378 current_attr.pointer = 1;
6380 return attr_decl ();
6386 gfc_match_allocatable (void)
6388 gfc_clear_attr (¤t_attr);
6389 current_attr.allocatable = 1;
6391 return attr_decl ();
6396 gfc_match_codimension (void)
6398 gfc_clear_attr (¤t_attr);
6399 current_attr.codimension = 1;
6401 return attr_decl ();
6406 gfc_match_contiguous (void)
6408 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTIGUOUS statement at %C")
6412 gfc_clear_attr (¤t_attr);
6413 current_attr.contiguous = 1;
6415 return attr_decl ();
6420 gfc_match_dimension (void)
6422 gfc_clear_attr (¤t_attr);
6423 current_attr.dimension = 1;
6425 return attr_decl ();
6430 gfc_match_target (void)
6432 gfc_clear_attr (¤t_attr);
6433 current_attr.target = 1;
6435 return attr_decl ();
6439 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6443 access_attr_decl (gfc_statement st)
6445 char name[GFC_MAX_SYMBOL_LEN + 1];
6446 interface_type type;
6449 gfc_intrinsic_op op;
6452 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6457 m = gfc_match_generic_spec (&type, name, &op);
6460 if (m == MATCH_ERROR)
6465 case INTERFACE_NAMELESS:
6466 case INTERFACE_ABSTRACT:
6469 case INTERFACE_GENERIC:
6470 if (gfc_get_symbol (name, NULL, &sym))
6473 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
6474 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6475 sym->name, NULL) == FAILURE)
6480 case INTERFACE_INTRINSIC_OP:
6481 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6483 gfc_intrinsic_op other_op;
6485 gfc_current_ns->operator_access[op] =
6486 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6488 /* Handle the case if there is another op with the same
6489 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6490 other_op = gfc_equivalent_op (op);
6492 if (other_op != INTRINSIC_NONE)
6493 gfc_current_ns->operator_access[other_op] =
6494 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6499 gfc_error ("Access specification of the %s operator at %C has "
6500 "already been specified", gfc_op2string (op));
6506 case INTERFACE_USER_OP:
6507 uop = gfc_get_uop (name);
6509 if (uop->access == ACCESS_UNKNOWN)
6511 uop->access = (st == ST_PUBLIC)
6512 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6516 gfc_error ("Access specification of the .%s. operator at %C "
6517 "has already been specified", sym->name);
6524 if (gfc_match_char (',') == MATCH_NO)
6528 if (gfc_match_eos () != MATCH_YES)
6533 gfc_syntax_error (st);
6541 gfc_match_protected (void)
6546 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6548 gfc_error ("PROTECTED at %C only allowed in specification "
6549 "part of a module");
6554 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
6558 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6563 if (gfc_match_eos () == MATCH_YES)
6568 m = gfc_match_symbol (&sym, 0);
6572 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6585 if (gfc_match_eos () == MATCH_YES)
6587 if (gfc_match_char (',') != MATCH_YES)
6594 gfc_error ("Syntax error in PROTECTED statement at %C");
6599 /* The PRIVATE statement is a bit weird in that it can be an attribute
6600 declaration, but also works as a standalone statement inside of a
6601 type declaration or a module. */
6604 gfc_match_private (gfc_statement *st)
6607 if (gfc_match ("private") != MATCH_YES)
6610 if (gfc_current_state () != COMP_MODULE
6611 && !(gfc_current_state () == COMP_DERIVED
6612 && gfc_state_stack->previous
6613 && gfc_state_stack->previous->state == COMP_MODULE)
6614 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6615 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6616 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6618 gfc_error ("PRIVATE statement at %C is only allowed in the "
6619 "specification part of a module");
6623 if (gfc_current_state () == COMP_DERIVED)
6625 if (gfc_match_eos () == MATCH_YES)
6631 gfc_syntax_error (ST_PRIVATE);
6635 if (gfc_match_eos () == MATCH_YES)
6642 return access_attr_decl (ST_PRIVATE);
6647 gfc_match_public (gfc_statement *st)
6650 if (gfc_match ("public") != MATCH_YES)
6653 if (gfc_current_state () != COMP_MODULE)
6655 gfc_error ("PUBLIC statement at %C is only allowed in the "
6656 "specification part of a module");
6660 if (gfc_match_eos () == MATCH_YES)
6667 return access_attr_decl (ST_PUBLIC);
6671 /* Workhorse for gfc_match_parameter. */
6681 m = gfc_match_symbol (&sym, 0);
6683 gfc_error ("Expected variable name at %C in PARAMETER statement");
6688 if (gfc_match_char ('=') == MATCH_NO)
6690 gfc_error ("Expected = sign in PARAMETER statement at %C");
6694 m = gfc_match_init_expr (&init);
6696 gfc_error ("Expected expression at %C in PARAMETER statement");
6700 if (sym->ts.type == BT_UNKNOWN
6701 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6707 if (gfc_check_assign_symbol (sym, init) == FAILURE
6708 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6716 gfc_error ("Initializing already initialized variable at %C");
6721 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
6722 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6725 gfc_free_expr (init);
6730 /* Match a parameter statement, with the weird syntax that these have. */
6733 gfc_match_parameter (void)
6737 if (gfc_match_char ('(') == MATCH_NO)
6746 if (gfc_match (" )%t") == MATCH_YES)
6749 if (gfc_match_char (',') != MATCH_YES)
6751 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6761 /* Save statements have a special syntax. */
6764 gfc_match_save (void)
6766 char n[GFC_MAX_SYMBOL_LEN+1];
6771 if (gfc_match_eos () == MATCH_YES)
6773 if (gfc_current_ns->seen_save)
6775 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6776 "follows previous SAVE statement")
6781 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6785 if (gfc_current_ns->save_all)
6787 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6788 "blanket SAVE statement")
6797 m = gfc_match_symbol (&sym, 0);
6801 if (gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
6802 &gfc_current_locus) == FAILURE)
6813 m = gfc_match (" / %n /", &n);
6814 if (m == MATCH_ERROR)
6819 c = gfc_get_common (n, 0);
6822 gfc_current_ns->seen_save = 1;
6825 if (gfc_match_eos () == MATCH_YES)
6827 if (gfc_match_char (',') != MATCH_YES)
6834 gfc_error ("Syntax error in SAVE statement at %C");
6840 gfc_match_value (void)
6845 /* This is not allowed within a BLOCK construct! */
6846 if (gfc_current_state () == COMP_BLOCK)
6848 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
6852 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
6856 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6861 if (gfc_match_eos () == MATCH_YES)
6866 m = gfc_match_symbol (&sym, 0);
6870 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
6883 if (gfc_match_eos () == MATCH_YES)
6885 if (gfc_match_char (',') != MATCH_YES)
6892 gfc_error ("Syntax error in VALUE statement at %C");
6898 gfc_match_volatile (void)
6903 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
6907 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6912 if (gfc_match_eos () == MATCH_YES)
6917 /* VOLATILE is special because it can be added to host-associated
6918 symbols locally. Except for coarrays. */
6919 m = gfc_match_symbol (&sym, 1);
6923 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
6924 for variable in a BLOCK which is defined outside of the BLOCK. */
6925 if (sym->ns != gfc_current_ns && sym->attr.codimension)
6927 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
6928 "%C, which is use-/host-associated", sym->name);
6931 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6944 if (gfc_match_eos () == MATCH_YES)
6946 if (gfc_match_char (',') != MATCH_YES)
6953 gfc_error ("Syntax error in VOLATILE statement at %C");
6959 gfc_match_asynchronous (void)
6964 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS statement at %C")
6968 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6973 if (gfc_match_eos () == MATCH_YES)
6978 /* ASYNCHRONOUS is special because it can be added to host-associated
6980 m = gfc_match_symbol (&sym, 1);
6984 if (gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus)
6997 if (gfc_match_eos () == MATCH_YES)
6999 if (gfc_match_char (',') != MATCH_YES)
7006 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7011 /* Match a module procedure statement. Note that we have to modify
7012 symbols in the parent's namespace because the current one was there
7013 to receive symbols that are in an interface's formal argument list. */
7016 gfc_match_modproc (void)
7018 char name[GFC_MAX_SYMBOL_LEN + 1];
7022 gfc_namespace *module_ns;
7023 gfc_interface *old_interface_head, *interface;
7025 if (gfc_state_stack->state != COMP_INTERFACE
7026 || gfc_state_stack->previous == NULL
7027 || current_interface.type == INTERFACE_NAMELESS
7028 || current_interface.type == INTERFACE_ABSTRACT)
7030 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7035 module_ns = gfc_current_ns->parent;
7036 for (; module_ns; module_ns = module_ns->parent)
7037 if (module_ns->proc_name->attr.flavor == FL_MODULE
7038 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7039 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7040 && !module_ns->proc_name->attr.contained))
7043 if (module_ns == NULL)
7046 /* Store the current state of the interface. We will need it if we
7047 end up with a syntax error and need to recover. */
7048 old_interface_head = gfc_current_interface_head ();
7050 /* Check if the F2008 optional double colon appears. */
7051 gfc_gobble_whitespace ();
7052 old_locus = gfc_current_locus;
7053 if (gfc_match ("::") == MATCH_YES)
7055 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: double colon in "
7056 "MODULE PROCEDURE statement at %L", &old_locus)
7061 gfc_current_locus = old_locus;
7066 old_locus = gfc_current_locus;
7068 m = gfc_match_name (name);
7074 /* Check for syntax error before starting to add symbols to the
7075 current namespace. */
7076 if (gfc_match_eos () == MATCH_YES)
7079 if (!last && gfc_match_char (',') != MATCH_YES)
7082 /* Now we're sure the syntax is valid, we process this item
7084 if (gfc_get_symbol (name, module_ns, &sym))
7087 if (sym->attr.intrinsic)
7089 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7090 "PROCEDURE", &old_locus);
7094 if (sym->attr.proc != PROC_MODULE
7095 && gfc_add_procedure (&sym->attr, PROC_MODULE,
7096 sym->name, NULL) == FAILURE)
7099 if (gfc_add_interface (sym) == FAILURE)
7102 sym->attr.mod_proc = 1;
7103 sym->declared_at = old_locus;
7112 /* Restore the previous state of the interface. */
7113 interface = gfc_current_interface_head ();
7114 gfc_set_current_interface_head (old_interface_head);
7116 /* Free the new interfaces. */
7117 while (interface != old_interface_head)
7119 gfc_interface *i = interface->next;
7124 /* And issue a syntax error. */
7125 gfc_syntax_error (ST_MODULE_PROC);
7130 /* Check a derived type that is being extended. */
7132 check_extended_derived_type (char *name)
7134 gfc_symbol *extended;
7136 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7138 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7144 gfc_error ("No such symbol in TYPE definition at %C");
7148 if (extended->attr.flavor != FL_DERIVED)
7150 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7151 "derived type", name);
7155 if (extended->attr.is_bind_c)
7157 gfc_error ("'%s' cannot be extended at %C because it "
7158 "is BIND(C)", extended->name);
7162 if (extended->attr.sequence)
7164 gfc_error ("'%s' cannot be extended at %C because it "
7165 "is a SEQUENCE type", extended->name);
7173 /* Match the optional attribute specifiers for a type declaration.
7174 Return MATCH_ERROR if an error is encountered in one of the handled
7175 attributes (public, private, bind(c)), MATCH_NO if what's found is
7176 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7177 checking on attribute conflicts needs to be done. */
7180 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7182 /* See if the derived type is marked as private. */
7183 if (gfc_match (" , private") == MATCH_YES)
7185 if (gfc_current_state () != COMP_MODULE)
7187 gfc_error ("Derived type at %C can only be PRIVATE in the "
7188 "specification part of a module");
7192 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
7195 else if (gfc_match (" , public") == MATCH_YES)
7197 if (gfc_current_state () != COMP_MODULE)
7199 gfc_error ("Derived type at %C can only be PUBLIC in the "
7200 "specification part of a module");
7204 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
7207 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7209 /* If the type is defined to be bind(c) it then needs to make
7210 sure that all fields are interoperable. This will
7211 need to be a semantic check on the finished derived type.
7212 See 15.2.3 (lines 9-12) of F2003 draft. */
7213 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
7216 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7218 else if (gfc_match (" , abstract") == MATCH_YES)
7220 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
7224 if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
7227 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
7229 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
7235 /* If we get here, something matched. */
7240 /* Match the beginning of a derived type declaration. If a type name
7241 was the result of a function, then it is possible to have a symbol
7242 already to be known as a derived type yet have no components. */
7245 gfc_match_derived_decl (void)
7247 char name[GFC_MAX_SYMBOL_LEN + 1];
7248 char parent[GFC_MAX_SYMBOL_LEN + 1];
7249 symbol_attribute attr;
7251 gfc_symbol *extended;
7253 match is_type_attr_spec = MATCH_NO;
7254 bool seen_attr = false;
7256 if (gfc_current_state () == COMP_DERIVED)
7261 gfc_clear_attr (&attr);
7266 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7267 if (is_type_attr_spec == MATCH_ERROR)
7269 if (is_type_attr_spec == MATCH_YES)
7271 } while (is_type_attr_spec == MATCH_YES);
7273 /* Deal with derived type extensions. The extension attribute has
7274 been added to 'attr' but now the parent type must be found and
7277 extended = check_extended_derived_type (parent);
7279 if (parent[0] && !extended)
7282 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7284 gfc_error ("Expected :: in TYPE definition at %C");
7288 m = gfc_match (" %n%t", name);
7292 /* Make sure the name is not the name of an intrinsic type. */
7293 if (gfc_is_intrinsic_typename (name))
7295 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7300 if (gfc_get_symbol (name, NULL, &sym))
7303 if (sym->ts.type != BT_UNKNOWN)
7305 gfc_error ("Derived type name '%s' at %C already has a basic type "
7306 "of %s", sym->name, gfc_typename (&sym->ts));
7310 /* The symbol may already have the derived attribute without the
7311 components. The ways this can happen is via a function
7312 definition, an INTRINSIC statement or a subtype in another
7313 derived type that is a pointer. The first part of the AND clause
7314 is true if the symbol is not the return value of a function. */
7315 if (sym->attr.flavor != FL_DERIVED
7316 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
7319 if (sym->components != NULL || sym->attr.zero_comp)
7321 gfc_error ("Derived type definition of '%s' at %C has already been "
7322 "defined", sym->name);
7326 if (attr.access != ACCESS_UNKNOWN
7327 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
7330 /* See if the derived type was labeled as bind(c). */
7331 if (attr.is_bind_c != 0)
7332 sym->attr.is_bind_c = attr.is_bind_c;
7334 /* Construct the f2k_derived namespace if it is not yet there. */
7335 if (!sym->f2k_derived)
7336 sym->f2k_derived = gfc_get_namespace (NULL, 0);
7338 if (extended && !sym->components)
7343 /* Add the extended derived type as the first component. */
7344 gfc_add_component (sym, parent, &p);
7346 gfc_set_sym_referenced (extended);
7348 p->ts.type = BT_DERIVED;
7349 p->ts.u.derived = extended;
7350 p->initializer = gfc_default_initializer (&p->ts);
7352 /* Set extension level. */
7353 if (extended->attr.extension == 255)
7355 /* Since the extension field is 8 bit wide, we can only have
7356 up to 255 extension levels. */
7357 gfc_error ("Maximum extension level reached with type '%s' at %L",
7358 extended->name, &extended->declared_at);
7361 sym->attr.extension = extended->attr.extension + 1;
7363 /* Provide the links between the extended type and its extension. */
7364 if (!extended->f2k_derived)
7365 extended->f2k_derived = gfc_get_namespace (NULL, 0);
7366 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7370 if (!sym->hash_value)
7371 /* Set the hash for the compound name for this type. */
7372 sym->hash_value = gfc_hash_value (sym);
7374 /* Take over the ABSTRACT attribute. */
7375 sym->attr.abstract = attr.abstract;
7377 gfc_new_block = sym;
7383 /* Cray Pointees can be declared as:
7384 pointer (ipt, a (n,m,...,*)) */
7387 gfc_mod_pointee_as (gfc_array_spec *as)
7389 as->cray_pointee = true; /* This will be useful to know later. */
7390 if (as->type == AS_ASSUMED_SIZE)
7391 as->cp_was_assumed = true;
7392 else if (as->type == AS_ASSUMED_SHAPE)
7394 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7401 /* Match the enum definition statement, here we are trying to match
7402 the first line of enum definition statement.
7403 Returns MATCH_YES if match is found. */
7406 gfc_match_enum (void)
7410 m = gfc_match_eos ();
7414 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
7422 /* Returns an initializer whose value is one higher than the value of the
7423 LAST_INITIALIZER argument. If the argument is NULL, the
7424 initializers value will be set to zero. The initializer's kind
7425 will be set to gfc_c_int_kind.
7427 If -fshort-enums is given, the appropriate kind will be selected
7428 later after all enumerators have been parsed. A warning is issued
7429 here if an initializer exceeds gfc_c_int_kind. */
7432 enum_initializer (gfc_expr *last_initializer, locus where)
7435 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7437 mpz_init (result->value.integer);
7439 if (last_initializer != NULL)
7441 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7442 result->where = last_initializer->where;
7444 if (gfc_check_integer_range (result->value.integer,
7445 gfc_c_int_kind) != ARITH_OK)
7447 gfc_error ("Enumerator exceeds the C integer type at %C");
7453 /* Control comes here, if it's the very first enumerator and no
7454 initializer has been given. It will be initialized to zero. */
7455 mpz_set_si (result->value.integer, 0);
7462 /* Match a variable name with an optional initializer. When this
7463 subroutine is called, a variable is expected to be parsed next.
7464 Depending on what is happening at the moment, updates either the
7465 symbol table or the current interface. */
7468 enumerator_decl (void)
7470 char name[GFC_MAX_SYMBOL_LEN + 1];
7471 gfc_expr *initializer;
7472 gfc_array_spec *as = NULL;
7480 old_locus = gfc_current_locus;
7482 /* When we get here, we've just matched a list of attributes and
7483 maybe a type and a double colon. The next thing we expect to see
7484 is the name of the symbol. */
7485 m = gfc_match_name (name);
7489 var_locus = gfc_current_locus;
7491 /* OK, we've successfully matched the declaration. Now put the
7492 symbol in the current namespace. If we fail to create the symbol,
7494 if (build_sym (name, NULL, false, &as, &var_locus) == FAILURE)
7500 /* The double colon must be present in order to have initializers.
7501 Otherwise the statement is ambiguous with an assignment statement. */
7504 if (gfc_match_char ('=') == MATCH_YES)
7506 m = gfc_match_init_expr (&initializer);
7509 gfc_error ("Expected an initialization expression at %C");
7518 /* If we do not have an initializer, the initialization value of the
7519 previous enumerator (stored in last_initializer) is incremented
7520 by 1 and is used to initialize the current enumerator. */
7521 if (initializer == NULL)
7522 initializer = enum_initializer (last_initializer, old_locus);
7524 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7526 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7532 /* Store this current initializer, for the next enumerator variable
7533 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7534 use last_initializer below. */
7535 last_initializer = initializer;
7536 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7538 /* Maintain enumerator history. */
7539 gfc_find_symbol (name, NULL, 0, &sym);
7540 create_enum_history (sym, last_initializer);
7542 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
7545 /* Free stuff up and return. */
7546 gfc_free_expr (initializer);
7552 /* Match the enumerator definition statement. */
7555 gfc_match_enumerator_def (void)
7560 gfc_clear_ts (¤t_ts);
7562 m = gfc_match (" enumerator");
7566 m = gfc_match (" :: ");
7567 if (m == MATCH_ERROR)
7570 colon_seen = (m == MATCH_YES);
7572 if (gfc_current_state () != COMP_ENUM)
7574 gfc_error ("ENUM definition statement expected before %C");
7575 gfc_free_enum_history ();
7579 (¤t_ts)->type = BT_INTEGER;
7580 (¤t_ts)->kind = gfc_c_int_kind;
7582 gfc_clear_attr (¤t_attr);
7583 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
7592 m = enumerator_decl ();
7593 if (m == MATCH_ERROR)
7595 gfc_free_enum_history ();
7601 if (gfc_match_eos () == MATCH_YES)
7603 if (gfc_match_char (',') != MATCH_YES)
7607 if (gfc_current_state () == COMP_ENUM)
7609 gfc_free_enum_history ();
7610 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7615 gfc_free_array_spec (current_as);
7622 /* Match binding attributes. */
7625 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7627 bool found_passing = false;
7628 bool seen_ptr = false;
7629 match m = MATCH_YES;
7631 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7632 this case the defaults are in there. */
7633 ba->access = ACCESS_UNKNOWN;
7634 ba->pass_arg = NULL;
7635 ba->pass_arg_num = 0;
7637 ba->non_overridable = 0;
7641 /* If we find a comma, we believe there are binding attributes. */
7642 m = gfc_match_char (',');
7648 /* Access specifier. */
7650 m = gfc_match (" public");
7651 if (m == MATCH_ERROR)
7655 if (ba->access != ACCESS_UNKNOWN)
7657 gfc_error ("Duplicate access-specifier at %C");
7661 ba->access = ACCESS_PUBLIC;
7665 m = gfc_match (" private");
7666 if (m == MATCH_ERROR)
7670 if (ba->access != ACCESS_UNKNOWN)
7672 gfc_error ("Duplicate access-specifier at %C");
7676 ba->access = ACCESS_PRIVATE;
7680 /* If inside GENERIC, the following is not allowed. */
7685 m = gfc_match (" nopass");
7686 if (m == MATCH_ERROR)
7692 gfc_error ("Binding attributes already specify passing,"
7693 " illegal NOPASS at %C");
7697 found_passing = true;
7702 /* PASS possibly including argument. */
7703 m = gfc_match (" pass");
7704 if (m == MATCH_ERROR)
7708 char arg[GFC_MAX_SYMBOL_LEN + 1];
7712 gfc_error ("Binding attributes already specify passing,"
7713 " illegal PASS at %C");
7717 m = gfc_match (" ( %n )", arg);
7718 if (m == MATCH_ERROR)
7721 ba->pass_arg = gfc_get_string (arg);
7722 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7724 found_passing = true;
7732 m = gfc_match (" pointer");
7733 if (m == MATCH_ERROR)
7739 gfc_error ("Duplicate POINTER attribute at %C");
7749 /* NON_OVERRIDABLE flag. */
7750 m = gfc_match (" non_overridable");
7751 if (m == MATCH_ERROR)
7755 if (ba->non_overridable)
7757 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7761 ba->non_overridable = 1;
7765 /* DEFERRED flag. */
7766 m = gfc_match (" deferred");
7767 if (m == MATCH_ERROR)
7773 gfc_error ("Duplicate DEFERRED at %C");
7784 /* Nothing matching found. */
7786 gfc_error ("Expected access-specifier at %C");
7788 gfc_error ("Expected binding attribute at %C");
7791 while (gfc_match_char (',') == MATCH_YES);
7793 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
7794 if (ba->non_overridable && ba->deferred)
7796 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7803 if (ba->access == ACCESS_UNKNOWN)
7804 ba->access = gfc_typebound_default_access;
7806 if (ppc && !seen_ptr)
7808 gfc_error ("POINTER attribute is required for procedure pointer component"
7820 /* Match a PROCEDURE specific binding inside a derived type. */
7823 match_procedure_in_type (void)
7825 char name[GFC_MAX_SYMBOL_LEN + 1];
7826 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
7827 char* target = NULL, *ifc = NULL;
7828 gfc_typebound_proc tb;
7837 /* Check current state. */
7838 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
7839 block = gfc_state_stack->previous->sym;
7842 /* Try to match PROCEDURE(interface). */
7843 if (gfc_match (" (") == MATCH_YES)
7845 m = gfc_match_name (target_buf);
7846 if (m == MATCH_ERROR)
7850 gfc_error ("Interface-name expected after '(' at %C");
7854 if (gfc_match (" )") != MATCH_YES)
7856 gfc_error ("')' expected at %C");
7863 /* Construct the data structure. */
7864 memset (&tb, 0, sizeof (tb));
7865 tb.where = gfc_current_locus;
7867 /* Match binding attributes. */
7868 m = match_binding_attributes (&tb, false, false);
7869 if (m == MATCH_ERROR)
7871 seen_attrs = (m == MATCH_YES);
7873 /* Check that attribute DEFERRED is given if an interface is specified. */
7874 if (tb.deferred && !ifc)
7876 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7879 if (ifc && !tb.deferred)
7881 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7885 /* Match the colons. */
7886 m = gfc_match (" ::");
7887 if (m == MATCH_ERROR)
7889 seen_colons = (m == MATCH_YES);
7890 if (seen_attrs && !seen_colons)
7892 gfc_error ("Expected '::' after binding-attributes at %C");
7896 /* Match the binding names. */
7899 m = gfc_match_name (name);
7900 if (m == MATCH_ERROR)
7904 gfc_error ("Expected binding name at %C");
7908 if (num>1 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: PROCEDURE list"
7909 " at %C") == FAILURE)
7912 /* Try to match the '=> target', if it's there. */
7914 m = gfc_match (" =>");
7915 if (m == MATCH_ERROR)
7921 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7927 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7932 m = gfc_match_name (target_buf);
7933 if (m == MATCH_ERROR)
7937 gfc_error ("Expected binding target after '=>' at %C");
7940 target = target_buf;
7943 /* If no target was found, it has the same name as the binding. */
7947 /* Get the namespace to insert the symbols into. */
7948 ns = block->f2k_derived;
7951 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7952 if (tb.deferred && !block->attr.abstract)
7954 gfc_error ("Type '%s' containing DEFERRED binding at %C "
7955 "is not ABSTRACT", block->name);
7959 /* See if we already have a binding with this name in the symtree which
7960 would be an error. If a GENERIC already targetted this binding, it may
7961 be already there but then typebound is still NULL. */
7962 stree = gfc_find_symtree (ns->tb_sym_root, name);
7963 if (stree && stree->n.tb)
7965 gfc_error ("There is already a procedure with binding name '%s' for "
7966 "the derived type '%s' at %C", name, block->name);
7970 /* Insert it and set attributes. */
7974 stree = gfc_new_symtree (&ns->tb_sym_root, name);
7977 stree->n.tb = gfc_get_typebound_proc (&tb);
7979 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
7982 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
7984 if (gfc_match_eos () == MATCH_YES)
7986 if (gfc_match_char (',') != MATCH_YES)
7991 gfc_error ("Syntax error in PROCEDURE statement at %C");
7996 /* Match a GENERIC procedure binding inside a derived type. */
7999 gfc_match_generic (void)
8001 char name[GFC_MAX_SYMBOL_LEN + 1];
8002 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
8004 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8005 gfc_typebound_proc* tb;
8007 interface_type op_type;
8008 gfc_intrinsic_op op;
8011 /* Check current state. */
8012 if (gfc_current_state () == COMP_DERIVED)
8014 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8017 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8019 block = gfc_state_stack->previous->sym;
8020 ns = block->f2k_derived;
8021 gcc_assert (block && ns);
8023 memset (&tbattr, 0, sizeof (tbattr));
8024 tbattr.where = gfc_current_locus;
8026 /* See if we get an access-specifier. */
8027 m = match_binding_attributes (&tbattr, true, false);
8028 if (m == MATCH_ERROR)
8031 /* Now the colons, those are required. */
8032 if (gfc_match (" ::") != MATCH_YES)
8034 gfc_error ("Expected '::' at %C");
8038 /* Match the binding name; depending on type (operator / generic) format
8039 it for future error messages into bind_name. */
8041 m = gfc_match_generic_spec (&op_type, name, &op);
8042 if (m == MATCH_ERROR)
8046 gfc_error ("Expected generic name or operator descriptor at %C");
8052 case INTERFACE_GENERIC:
8053 snprintf (bind_name, sizeof (bind_name), "%s", name);
8056 case INTERFACE_USER_OP:
8057 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8060 case INTERFACE_INTRINSIC_OP:
8061 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8062 gfc_op2string (op));
8069 /* Match the required =>. */
8070 if (gfc_match (" =>") != MATCH_YES)
8072 gfc_error ("Expected '=>' at %C");
8076 /* Try to find existing GENERIC binding with this name / for this operator;
8077 if there is something, check that it is another GENERIC and then extend
8078 it rather than building a new node. Otherwise, create it and put it
8079 at the right position. */
8083 case INTERFACE_USER_OP:
8084 case INTERFACE_GENERIC:
8086 const bool is_op = (op_type == INTERFACE_USER_OP);
8089 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8101 case INTERFACE_INTRINSIC_OP:
8111 if (!tb->is_generic)
8113 gcc_assert (op_type == INTERFACE_GENERIC);
8114 gfc_error ("There's already a non-generic procedure with binding name"
8115 " '%s' for the derived type '%s' at %C",
8116 bind_name, block->name);
8120 if (tb->access != tbattr.access)
8122 gfc_error ("Binding at %C must have the same access as already"
8123 " defined binding '%s'", bind_name);
8129 tb = gfc_get_typebound_proc (NULL);
8130 tb->where = gfc_current_locus;
8131 tb->access = tbattr.access;
8133 tb->u.generic = NULL;
8137 case INTERFACE_GENERIC:
8138 case INTERFACE_USER_OP:
8140 const bool is_op = (op_type == INTERFACE_USER_OP);
8143 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8151 case INTERFACE_INTRINSIC_OP:
8160 /* Now, match all following names as specific targets. */
8163 gfc_symtree* target_st;
8164 gfc_tbp_generic* target;
8166 m = gfc_match_name (name);
8167 if (m == MATCH_ERROR)
8171 gfc_error ("Expected specific binding name at %C");
8175 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8177 /* See if this is a duplicate specification. */
8178 for (target = tb->u.generic; target; target = target->next)
8179 if (target_st == target->specific_st)
8181 gfc_error ("'%s' already defined as specific binding for the"
8182 " generic '%s' at %C", name, bind_name);
8186 target = gfc_get_tbp_generic ();
8187 target->specific_st = target_st;
8188 target->specific = NULL;
8189 target->next = tb->u.generic;
8190 tb->u.generic = target;
8192 while (gfc_match (" ,") == MATCH_YES);
8194 /* Here should be the end. */
8195 if (gfc_match_eos () != MATCH_YES)
8197 gfc_error ("Junk after GENERIC binding at %C");
8208 /* Match a FINAL declaration inside a derived type. */
8211 gfc_match_final_decl (void)
8213 char name[GFC_MAX_SYMBOL_LEN + 1];
8216 gfc_namespace* module_ns;
8220 if (gfc_current_form == FORM_FREE)
8222 char c = gfc_peek_ascii_char ();
8223 if (!gfc_is_whitespace (c) && c != ':')
8227 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8229 if (gfc_current_form == FORM_FIXED)
8232 gfc_error ("FINAL declaration at %C must be inside a derived type "
8233 "CONTAINS section");
8237 block = gfc_state_stack->previous->sym;
8240 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8241 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8243 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8244 " specification part of a MODULE");
8248 module_ns = gfc_current_ns;
8249 gcc_assert (module_ns);
8250 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8252 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8253 if (gfc_match (" ::") == MATCH_ERROR)
8256 /* Match the sequence of procedure names. */
8263 if (first && gfc_match_eos () == MATCH_YES)
8265 gfc_error ("Empty FINAL at %C");
8269 m = gfc_match_name (name);
8272 gfc_error ("Expected module procedure name at %C");
8275 else if (m != MATCH_YES)
8278 if (gfc_match_eos () == MATCH_YES)
8280 if (!last && gfc_match_char (',') != MATCH_YES)
8282 gfc_error ("Expected ',' at %C");
8286 if (gfc_get_symbol (name, module_ns, &sym))
8288 gfc_error ("Unknown procedure name \"%s\" at %C", name);
8292 /* Mark the symbol as module procedure. */
8293 if (sym->attr.proc != PROC_MODULE
8294 && gfc_add_procedure (&sym->attr, PROC_MODULE,
8295 sym->name, NULL) == FAILURE)
8298 /* Check if we already have this symbol in the list, this is an error. */
8299 for (f = block->f2k_derived->finalizers; f; f = f->next)
8300 if (f->proc_sym == sym)
8302 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8307 /* Add this symbol to the list of finalizers. */
8308 gcc_assert (block->f2k_derived);
8310 f = XCNEW (gfc_finalizer);
8312 f->proc_tree = NULL;
8313 f->where = gfc_current_locus;
8314 f->next = block->f2k_derived->finalizers;
8315 block->f2k_derived->finalizers = f;
8325 const ext_attr_t ext_attr_list[] = {
8326 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8327 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8328 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
8329 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
8330 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
8331 { NULL, EXT_ATTR_LAST, NULL }
8334 /* Match a !GCC$ ATTRIBUTES statement of the form:
8335 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8336 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8338 TODO: We should support all GCC attributes using the same syntax for
8339 the attribute list, i.e. the list in C
8340 __attributes(( attribute-list ))
8342 !GCC$ ATTRIBUTES attribute-list ::
8343 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8346 As there is absolutely no risk of confusion, we should never return
8349 gfc_match_gcc_attributes (void)
8351 symbol_attribute attr;
8352 char name[GFC_MAX_SYMBOL_LEN + 1];
8357 gfc_clear_attr (&attr);
8362 if (gfc_match_name (name) != MATCH_YES)
8365 for (id = 0; id < EXT_ATTR_LAST; id++)
8366 if (strcmp (name, ext_attr_list[id].name) == 0)
8369 if (id == EXT_ATTR_LAST)
8371 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8375 if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
8379 gfc_gobble_whitespace ();
8380 ch = gfc_next_ascii_char ();
8383 /* This is the successful exit condition for the loop. */
8384 if (gfc_next_ascii_char () == ':')
8394 if (gfc_match_eos () == MATCH_YES)
8399 m = gfc_match_name (name);
8403 if (find_special (name, &sym, true))
8406 sym->attr.ext_attr |= attr.ext_attr;
8408 if (gfc_match_eos () == MATCH_YES)
8411 if (gfc_match_char (',') != MATCH_YES)
8418 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");