1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
30 /* Macros to access allocate memory for gfc_data_variable,
31 gfc_data_value and gfc_data. */
32 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
33 #define gfc_get_data_value() XCNEW (gfc_data_value)
34 #define gfc_get_data() XCNEW (gfc_data)
37 /* This flag is set if an old-style length selector is matched
38 during a type-declaration statement. */
40 static int old_char_selector;
42 /* When variables acquire types and attributes from a declaration
43 statement, they get them from the following static variables. The
44 first part of a declaration sets these variables and the second
45 part copies these into symbol structures. */
47 static gfc_typespec current_ts;
49 static symbol_attribute current_attr;
50 static gfc_array_spec *current_as;
51 static int colon_seen;
53 /* The current binding label (if any). */
54 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
55 /* Need to know how many identifiers are on the current data declaration
56 line in case we're given the BIND(C) attribute with a NAME= specifier. */
57 static int num_idents_on_line;
58 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
59 can supply a name if the curr_binding_label is nil and NAME= was not. */
60 static int has_name_equals = 0;
62 /* Initializer of the previous enumerator. */
64 static gfc_expr *last_initializer;
66 /* History of all the enumerators is maintained, so that
67 kind values of all the enumerators could be updated depending
68 upon the maximum initialized value. */
70 typedef struct enumerator_history
73 gfc_expr *initializer;
74 struct enumerator_history *next;
78 /* Header of enum history chain. */
80 static enumerator_history *enum_history = NULL;
82 /* Pointer of enum history node containing largest initializer. */
84 static enumerator_history *max_enum = NULL;
86 /* gfc_new_block points to the symbol of a newly matched block. */
88 gfc_symbol *gfc_new_block;
90 bool gfc_matching_function;
93 /********************* DATA statement subroutines *********************/
95 static bool in_match_data = false;
98 gfc_in_match_data (void)
100 return in_match_data;
104 set_in_match_data (bool set_value)
106 in_match_data = set_value;
109 /* Free a gfc_data_variable structure and everything beneath it. */
112 free_variable (gfc_data_variable *p)
114 gfc_data_variable *q;
119 gfc_free_expr (p->expr);
120 gfc_free_iterator (&p->iter, 0);
121 free_variable (p->list);
127 /* Free a gfc_data_value structure and everything beneath it. */
130 free_value (gfc_data_value *p)
137 gfc_free_expr (p->expr);
143 /* Free a list of gfc_data structures. */
146 gfc_free_data (gfc_data *p)
153 free_variable (p->var);
154 free_value (p->value);
160 /* Free all data in a namespace. */
163 gfc_free_data_all (gfc_namespace *ns)
176 static match var_element (gfc_data_variable *);
178 /* Match a list of variables terminated by an iterator and a right
182 var_list (gfc_data_variable *parent)
184 gfc_data_variable *tail, var;
187 m = var_element (&var);
188 if (m == MATCH_ERROR)
193 tail = gfc_get_data_variable ();
200 if (gfc_match_char (',') != MATCH_YES)
203 m = gfc_match_iterator (&parent->iter, 1);
206 if (m == MATCH_ERROR)
209 m = var_element (&var);
210 if (m == MATCH_ERROR)
215 tail->next = gfc_get_data_variable ();
221 if (gfc_match_char (')') != MATCH_YES)
226 gfc_syntax_error (ST_DATA);
231 /* Match a single element in a data variable list, which can be a
232 variable-iterator list. */
235 var_element (gfc_data_variable *new_var)
240 memset (new_var, 0, sizeof (gfc_data_variable));
242 if (gfc_match_char ('(') == MATCH_YES)
243 return var_list (new_var);
245 m = gfc_match_variable (&new_var->expr, 0);
249 sym = new_var->expr->symtree->n.sym;
251 /* Symbol should already have an associated type. */
252 if (gfc_check_symbol_typed (sym, gfc_current_ns,
253 false, gfc_current_locus) == FAILURE)
256 if (!sym->attr.function && gfc_current_ns->parent
257 && gfc_current_ns->parent == sym->ns)
259 gfc_error ("Host associated variable '%s' may not be in the DATA "
260 "statement at %C", sym->name);
264 if (gfc_current_state () != COMP_BLOCK_DATA
265 && sym->attr.in_common
266 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
267 "common block variable '%s' in DATA statement at %C",
268 sym->name) == FAILURE)
271 if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
278 /* Match the top-level list of data variables. */
281 top_var_list (gfc_data *d)
283 gfc_data_variable var, *tail, *new_var;
290 m = var_element (&var);
293 if (m == MATCH_ERROR)
296 new_var = gfc_get_data_variable ();
302 tail->next = new_var;
306 if (gfc_match_char ('/') == MATCH_YES)
308 if (gfc_match_char (',') != MATCH_YES)
315 gfc_syntax_error (ST_DATA);
316 gfc_free_data_all (gfc_current_ns);
322 match_data_constant (gfc_expr **result)
324 char name[GFC_MAX_SYMBOL_LEN + 1];
330 m = gfc_match_literal_constant (&expr, 1);
337 if (m == MATCH_ERROR)
340 m = gfc_match_null (result);
344 old_loc = gfc_current_locus;
346 /* Should this be a structure component, try to match it
347 before matching a name. */
348 m = gfc_match_rvalue (result);
349 if (m == MATCH_ERROR)
352 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
354 if (gfc_simplify_expr (*result, 0) == FAILURE)
359 gfc_current_locus = old_loc;
361 m = gfc_match_name (name);
365 if (gfc_find_symbol (name, NULL, 1, &sym))
369 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
371 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
375 else if (sym->attr.flavor == FL_DERIVED)
376 return gfc_match_structure_constructor (sym, result, false);
378 /* Check to see if the value is an initialization array expression. */
379 if (sym->value->expr_type == EXPR_ARRAY)
381 gfc_current_locus = old_loc;
383 m = gfc_match_init_expr (result);
384 if (m == MATCH_ERROR)
389 if (gfc_simplify_expr (*result, 0) == FAILURE)
392 if ((*result)->expr_type == EXPR_CONSTANT)
396 gfc_error ("Invalid initializer %s in Data statement at %C", name);
402 *result = gfc_copy_expr (sym->value);
407 /* Match a list of values in a DATA statement. The leading '/' has
408 already been seen at this point. */
411 top_val_list (gfc_data *data)
413 gfc_data_value *new_val, *tail;
421 m = match_data_constant (&expr);
424 if (m == MATCH_ERROR)
427 new_val = gfc_get_data_value ();
428 mpz_init (new_val->repeat);
431 data->value = new_val;
433 tail->next = new_val;
437 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
440 mpz_set_ui (tail->repeat, 1);
444 if (expr->ts.type == BT_INTEGER)
445 mpz_set (tail->repeat, expr->value.integer);
446 gfc_free_expr (expr);
448 m = match_data_constant (&tail->expr);
451 if (m == MATCH_ERROR)
455 if (gfc_match_char ('/') == MATCH_YES)
457 if (gfc_match_char (',') == MATCH_NO)
464 gfc_syntax_error (ST_DATA);
465 gfc_free_data_all (gfc_current_ns);
470 /* Matches an old style initialization. */
473 match_old_style_init (const char *name)
480 /* Set up data structure to hold initializers. */
481 gfc_find_sym_tree (name, NULL, 0, &st);
484 newdata = gfc_get_data ();
485 newdata->var = gfc_get_data_variable ();
486 newdata->var->expr = gfc_get_variable_expr (st);
487 newdata->where = gfc_current_locus;
489 /* Match initial value list. This also eats the terminal '/'. */
490 m = top_val_list (newdata);
499 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
504 /* Mark the variable as having appeared in a data statement. */
505 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
511 /* Chain in namespace list of DATA initializers. */
512 newdata->next = gfc_current_ns->data;
513 gfc_current_ns->data = newdata;
519 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
520 we are matching a DATA statement and are therefore issuing an error
521 if we encounter something unexpected, if not, we're trying to match
522 an old-style initialization expression of the form INTEGER I /2/. */
525 gfc_match_data (void)
530 set_in_match_data (true);
534 new_data = gfc_get_data ();
535 new_data->where = gfc_current_locus;
537 m = top_var_list (new_data);
541 m = top_val_list (new_data);
545 new_data->next = gfc_current_ns->data;
546 gfc_current_ns->data = new_data;
548 if (gfc_match_eos () == MATCH_YES)
551 gfc_match_char (','); /* Optional comma */
554 set_in_match_data (false);
558 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
565 set_in_match_data (false);
566 gfc_free_data (new_data);
571 /************************ Declaration statements *********************/
573 /* Match an intent specification. Since this can only happen after an
574 INTENT word, a legal intent-spec must follow. */
577 match_intent_spec (void)
580 if (gfc_match (" ( in out )") == MATCH_YES)
582 if (gfc_match (" ( in )") == MATCH_YES)
584 if (gfc_match (" ( out )") == MATCH_YES)
587 gfc_error ("Bad INTENT specification at %C");
588 return INTENT_UNKNOWN;
592 /* Matches a character length specification, which is either a
593 specification expression or a '*'. */
596 char_len_param_value (gfc_expr **expr)
600 if (gfc_match_char ('*') == MATCH_YES)
606 m = gfc_match_expr (expr);
609 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
612 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
614 if ((*expr)->value.function.actual
615 && (*expr)->value.function.actual->expr->symtree)
618 e = (*expr)->value.function.actual->expr;
619 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
620 && e->expr_type == EXPR_VARIABLE)
622 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
624 if (e->symtree->n.sym->ts.type == BT_CHARACTER
625 && e->symtree->n.sym->ts.u.cl
626 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
634 gfc_error ("Conflict in attributes of function argument at %C");
639 /* A character length is a '*' followed by a literal integer or a
640 char_len_param_value in parenthesis. */
643 match_char_length (gfc_expr **expr)
648 m = gfc_match_char ('*');
652 m = gfc_match_small_literal_int (&length, NULL);
653 if (m == MATCH_ERROR)
658 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
659 "Old-style character length at %C") == FAILURE)
661 *expr = gfc_int_expr (length);
665 if (gfc_match_char ('(') == MATCH_NO)
668 m = char_len_param_value (expr);
669 if (m != MATCH_YES && gfc_matching_function)
675 if (m == MATCH_ERROR)
680 if (gfc_match_char (')') == MATCH_NO)
682 gfc_free_expr (*expr);
690 gfc_error ("Syntax error in character length specification at %C");
695 /* Special subroutine for finding a symbol. Check if the name is found
696 in the current name space. If not, and we're compiling a function or
697 subroutine and the parent compilation unit is an interface, then check
698 to see if the name we've been given is the name of the interface
699 (located in another namespace). */
702 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
708 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
711 *result = st ? st->n.sym : NULL;
715 if (gfc_current_state () != COMP_SUBROUTINE
716 && gfc_current_state () != COMP_FUNCTION)
719 s = gfc_state_stack->previous;
723 if (s->state != COMP_INTERFACE)
726 goto end; /* Nameless interface. */
728 if (strcmp (name, s->sym->name) == 0)
739 /* Special subroutine for getting a symbol node associated with a
740 procedure name, used in SUBROUTINE and FUNCTION statements. The
741 symbol is created in the parent using with symtree node in the
742 child unit pointing to the symbol. If the current namespace has no
743 parent, then the symbol is just created in the current unit. */
746 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
752 /* Module functions have to be left in their own namespace because
753 they have potentially (almost certainly!) already been referenced.
754 In this sense, they are rather like external functions. This is
755 fixed up in resolve.c(resolve_entries), where the symbol name-
756 space is set to point to the master function, so that the fake
757 result mechanism can work. */
758 if (module_fcn_entry)
760 /* Present if entry is declared to be a module procedure. */
761 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
764 rc = gfc_get_symbol (name, NULL, result);
765 else if (!gfc_get_symbol (name, NULL, &sym) && sym
766 && (*result)->ts.type == BT_UNKNOWN
767 && sym->attr.flavor == FL_UNKNOWN)
768 /* Pick up the typespec for the entry, if declared in the function
769 body. Note that this symbol is FL_UNKNOWN because it will
770 only have appeared in a type declaration. The local symtree
771 is set to point to the module symbol and a unique symtree
772 to the local version. This latter ensures a correct clearing
775 /* If the ENTRY proceeds its specification, we need to ensure
776 that this does not raise a "has no IMPLICIT type" error. */
777 if (sym->ts.type == BT_UNKNOWN)
778 sym->attr.untyped = 1;
780 (*result)->ts = sym->ts;
782 /* Put the symbol in the procedure namespace so that, should
783 the ENTRY precede its specification, the specification
785 (*result)->ns = gfc_current_ns;
787 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
789 st = gfc_get_unique_symtree (gfc_current_ns);
794 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
800 gfc_current_ns->refs++;
802 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
804 /* Trap another encompassed procedure with the same name. All
805 these conditions are necessary to avoid picking up an entry
806 whose name clashes with that of the encompassing procedure;
807 this is handled using gsymbols to register unique,globally
809 if (sym->attr.flavor != 0
810 && sym->attr.proc != 0
811 && (sym->attr.subroutine || sym->attr.function)
812 && sym->attr.if_source != IFSRC_UNKNOWN)
813 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
814 name, &sym->declared_at);
816 /* Trap a procedure with a name the same as interface in the
817 encompassing scope. */
818 if (sym->attr.generic != 0
819 && (sym->attr.subroutine || sym->attr.function)
820 && !sym->attr.mod_proc)
821 gfc_error_now ("Name '%s' at %C is already defined"
822 " as a generic interface at %L",
823 name, &sym->declared_at);
825 /* Trap declarations of attributes in encompassing scope. The
826 signature for this is that ts.kind is set. Legitimate
827 references only set ts.type. */
828 if (sym->ts.kind != 0
829 && !sym->attr.implicit_type
830 && sym->attr.proc == 0
831 && gfc_current_ns->parent != NULL
832 && sym->attr.access == 0
833 && !module_fcn_entry)
834 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
835 "and must not have attributes declared at %L",
836 name, &sym->declared_at);
839 if (gfc_current_ns->parent == NULL || *result == NULL)
842 /* Module function entries will already have a symtree in
843 the current namespace but will need one at module level. */
844 if (module_fcn_entry)
846 /* Present if entry is declared to be a module procedure. */
847 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
849 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
852 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
857 /* See if the procedure should be a module procedure. */
859 if (((sym->ns->proc_name != NULL
860 && sym->ns->proc_name->attr.flavor == FL_MODULE
861 && sym->attr.proc != PROC_MODULE)
862 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
863 && gfc_add_procedure (&sym->attr, PROC_MODULE,
864 sym->name, NULL) == FAILURE)
871 /* Verify that the given symbol representing a parameter is C
872 interoperable, by checking to see if it was marked as such after
873 its declaration. If the given symbol is not interoperable, a
874 warning is reported, thus removing the need to return the status to
875 the calling function. The standard does not require the user use
876 one of the iso_c_binding named constants to declare an
877 interoperable parameter, but we can't be sure if the param is C
878 interop or not if the user doesn't. For example, integer(4) may be
879 legal Fortran, but doesn't have meaning in C. It may interop with
880 a number of the C types, which causes a problem because the
881 compiler can't know which one. This code is almost certainly not
882 portable, and the user will get what they deserve if the C type
883 across platforms isn't always interoperable with integer(4). If
884 the user had used something like integer(c_int) or integer(c_long),
885 the compiler could have automatically handled the varying sizes
889 verify_c_interop_param (gfc_symbol *sym)
891 int is_c_interop = 0;
892 gfc_try retval = SUCCESS;
894 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
895 Don't repeat the checks here. */
896 if (sym->attr.implicit_type)
899 /* For subroutines or functions that are passed to a BIND(C) procedure,
900 they're interoperable if they're BIND(C) and their params are all
902 if (sym->attr.flavor == FL_PROCEDURE)
904 if (sym->attr.is_bind_c == 0)
906 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
907 "attribute to be C interoperable", sym->name,
908 &(sym->declared_at));
914 if (sym->attr.is_c_interop == 1)
915 /* We've already checked this procedure; don't check it again. */
918 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
923 /* See if we've stored a reference to a procedure that owns sym. */
924 if (sym->ns != NULL && sym->ns->proc_name != NULL)
926 if (sym->ns->proc_name->attr.is_bind_c == 1)
929 (verify_c_interop (&(sym->ts))
932 if (is_c_interop != 1)
934 /* Make personalized messages to give better feedback. */
935 if (sym->ts.type == BT_DERIVED)
936 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
937 " procedure '%s' but is not C interoperable "
938 "because derived type '%s' is not C interoperable",
939 sym->name, &(sym->declared_at),
940 sym->ns->proc_name->name,
941 sym->ts.u.derived->name);
943 gfc_warning ("Variable '%s' at %L is a parameter to the "
944 "BIND(C) procedure '%s' but may not be C "
946 sym->name, &(sym->declared_at),
947 sym->ns->proc_name->name);
950 /* Character strings are only C interoperable if they have a
952 if (sym->ts.type == BT_CHARACTER)
954 gfc_charlen *cl = sym->ts.u.cl;
955 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
956 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
958 gfc_error ("Character argument '%s' at %L "
959 "must be length 1 because "
960 "procedure '%s' is BIND(C)",
961 sym->name, &sym->declared_at,
962 sym->ns->proc_name->name);
967 /* We have to make sure that any param to a bind(c) routine does
968 not have the allocatable, pointer, or optional attributes,
969 according to J3/04-007, section 5.1. */
970 if (sym->attr.allocatable == 1)
972 gfc_error ("Variable '%s' at %L cannot have the "
973 "ALLOCATABLE attribute because procedure '%s'"
974 " is BIND(C)", sym->name, &(sym->declared_at),
975 sym->ns->proc_name->name);
979 if (sym->attr.pointer == 1)
981 gfc_error ("Variable '%s' at %L cannot have the "
982 "POINTER attribute because procedure '%s'"
983 " is BIND(C)", sym->name, &(sym->declared_at),
984 sym->ns->proc_name->name);
988 if (sym->attr.optional == 1)
990 gfc_error ("Variable '%s' at %L cannot have the "
991 "OPTIONAL attribute because procedure '%s'"
992 " is BIND(C)", sym->name, &(sym->declared_at),
993 sym->ns->proc_name->name);
997 /* Make sure that if it has the dimension attribute, that it is
998 either assumed size or explicit shape. */
1001 if (sym->as->type == AS_ASSUMED_SHAPE)
1003 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1004 "argument to the procedure '%s' at %L because "
1005 "the procedure is BIND(C)", sym->name,
1006 &(sym->declared_at), sym->ns->proc_name->name,
1007 &(sym->ns->proc_name->declared_at));
1011 if (sym->as->type == AS_DEFERRED)
1013 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1014 "argument to the procedure '%s' at %L because "
1015 "the procedure is BIND(C)", sym->name,
1016 &(sym->declared_at), sym->ns->proc_name->name,
1017 &(sym->ns->proc_name->declared_at));
1028 /* Build a polymorphic CLASS entity, using the symbol that comes from build_sym.
1029 A CLASS entity is represented by an encapsulating type, which contains the
1030 declared type as '$data' component, plus an integer component '$vindex'
1031 which determines the dynamic type, and another integer '$size', which
1032 contains the size of the dynamic type structure. */
1035 encapsulate_class_symbol (gfc_typespec *ts, symbol_attribute *attr,
1036 gfc_array_spec **as)
1038 char name[GFC_MAX_SYMBOL_LEN + 5];
1042 /* Determine the name of the encapsulating type. */
1043 if ((*as) && (*as)->rank && attr->allocatable)
1044 sprintf (name, ".class.%s.%d.a", ts->u.derived->name, (*as)->rank);
1045 else if ((*as) && (*as)->rank)
1046 sprintf (name, ".class.%s.%d", ts->u.derived->name, (*as)->rank);
1047 else if (attr->allocatable)
1048 sprintf (name, ".class.%s.a", ts->u.derived->name);
1050 sprintf (name, ".class.%s", ts->u.derived->name);
1052 gfc_find_symbol (name, ts->u.derived->ns, 0, &fclass);
1056 /* If not there, create a new symbol. */
1057 fclass = gfc_new_symbol (name, ts->u.derived->ns);
1058 st = gfc_new_symtree (&ts->u.derived->ns->sym_root, name);
1060 gfc_set_sym_referenced (fclass);
1062 fclass->ts.type = BT_UNKNOWN;
1063 fclass->vindex = ts->u.derived->vindex;
1064 fclass->attr.abstract = ts->u.derived->attr.abstract;
1065 if (ts->u.derived->f2k_derived)
1066 fclass->f2k_derived = gfc_get_namespace (NULL, 0);
1067 if (gfc_add_flavor (&fclass->attr, FL_DERIVED,
1068 NULL, &gfc_current_locus) == FAILURE)
1071 /* Add component '$data'. */
1072 if (gfc_add_component (fclass, "$data", &c) == FAILURE)
1075 c->ts.type = BT_DERIVED;
1076 c->attr.access = ACCESS_PRIVATE;
1077 c->ts.u.derived = ts->u.derived;
1078 c->attr.pointer = attr->pointer || attr->dummy;
1079 c->attr.allocatable = attr->allocatable;
1080 c->attr.dimension = attr->dimension;
1081 c->attr.abstract = ts->u.derived->attr.abstract;
1083 c->initializer = gfc_get_expr ();
1084 c->initializer->expr_type = EXPR_NULL;
1086 /* Add component '$vindex'. */
1087 if (gfc_add_component (fclass, "$vindex", &c) == FAILURE)
1089 c->ts.type = BT_INTEGER;
1091 c->attr.access = ACCESS_PRIVATE;
1092 c->initializer = gfc_int_expr (0);
1094 /* Add component '$size'. */
1095 if (gfc_add_component (fclass, "$size", &c) == FAILURE)
1097 c->ts.type = BT_INTEGER;
1099 c->attr.access = ACCESS_PRIVATE;
1100 c->initializer = gfc_int_expr (0);
1103 fclass->attr.extension = 1;
1104 fclass->attr.is_class = 1;
1105 ts->u.derived = fclass;
1106 attr->allocatable = attr->pointer = attr->dimension = 0;
1107 (*as) = NULL; /* XXX */
1111 /* Function called by variable_decl() that adds a name to the symbol table. */
1114 build_sym (const char *name, gfc_charlen *cl,
1115 gfc_array_spec **as, locus *var_locus)
1117 symbol_attribute attr;
1120 if (gfc_get_symbol (name, NULL, &sym))
1123 /* Start updating the symbol table. Add basic type attribute if present. */
1124 if (current_ts.type != BT_UNKNOWN
1125 && (sym->attr.implicit_type == 0
1126 || !gfc_compare_types (&sym->ts, ¤t_ts))
1127 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
1130 if (sym->ts.type == BT_CHARACTER)
1133 /* Add dimension attribute if present. */
1134 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1138 /* Add attribute to symbol. The copy is so that we can reset the
1139 dimension attribute. */
1140 attr = current_attr;
1143 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1146 /* Finish any work that may need to be done for the binding label,
1147 if it's a bind(c). The bind(c) attr is found before the symbol
1148 is made, and before the symbol name (for data decls), so the
1149 current_ts is holding the binding label, or nothing if the
1150 name= attr wasn't given. Therefore, test here if we're dealing
1151 with a bind(c) and make sure the binding label is set correctly. */
1152 if (sym->attr.is_bind_c == 1)
1154 if (sym->binding_label[0] == '\0')
1156 /* Set the binding label and verify that if a NAME= was specified
1157 then only one identifier was in the entity-decl-list. */
1158 if (set_binding_label (sym->binding_label, sym->name,
1159 num_idents_on_line) == FAILURE)
1164 /* See if we know we're in a common block, and if it's a bind(c)
1165 common then we need to make sure we're an interoperable type. */
1166 if (sym->attr.in_common == 1)
1168 /* Test the common block object. */
1169 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1170 && sym->ts.is_c_interop != 1)
1172 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1173 "must be declared with a C interoperable "
1174 "kind since common block '%s' is BIND(C)",
1175 sym->name, sym->common_block->name,
1176 sym->common_block->name);
1181 sym->attr.implied_index = 0;
1183 if (sym->ts.type == BT_CLASS)
1184 encapsulate_class_symbol (&sym->ts, &sym->attr, &sym->as);
1190 /* Set character constant to the given length. The constant will be padded or
1191 truncated. If we're inside an array constructor without a typespec, we
1192 additionally check that all elements have the same length; check_len -1
1193 means no checking. */
1196 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1201 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1202 gcc_assert (expr->ts.type == BT_CHARACTER);
1204 slen = expr->value.character.length;
1207 s = gfc_get_wide_string (len + 1);
1208 memcpy (s, expr->value.character.string,
1209 MIN (len, slen) * sizeof (gfc_char_t));
1211 gfc_wide_memset (&s[slen], ' ', len - slen);
1213 if (gfc_option.warn_character_truncation && slen > len)
1214 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1215 "(%d/%d)", &expr->where, slen, len);
1217 /* Apply the standard by 'hand' otherwise it gets cleared for
1219 if (check_len != -1 && slen != check_len
1220 && !(gfc_option.allow_std & GFC_STD_GNU))
1221 gfc_error_now ("The CHARACTER elements of the array constructor "
1222 "at %L must have the same length (%d/%d)",
1223 &expr->where, slen, check_len);
1226 gfc_free (expr->value.character.string);
1227 expr->value.character.string = s;
1228 expr->value.character.length = len;
1233 /* Function to create and update the enumerator history
1234 using the information passed as arguments.
1235 Pointer "max_enum" is also updated, to point to
1236 enum history node containing largest initializer.
1238 SYM points to the symbol node of enumerator.
1239 INIT points to its enumerator value. */
1242 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1244 enumerator_history *new_enum_history;
1245 gcc_assert (sym != NULL && init != NULL);
1247 new_enum_history = XCNEW (enumerator_history);
1249 new_enum_history->sym = sym;
1250 new_enum_history->initializer = init;
1251 new_enum_history->next = NULL;
1253 if (enum_history == NULL)
1255 enum_history = new_enum_history;
1256 max_enum = enum_history;
1260 new_enum_history->next = enum_history;
1261 enum_history = new_enum_history;
1263 if (mpz_cmp (max_enum->initializer->value.integer,
1264 new_enum_history->initializer->value.integer) < 0)
1265 max_enum = new_enum_history;
1270 /* Function to free enum kind history. */
1273 gfc_free_enum_history (void)
1275 enumerator_history *current = enum_history;
1276 enumerator_history *next;
1278 while (current != NULL)
1280 next = current->next;
1285 enum_history = NULL;
1289 /* Function called by variable_decl() that adds an initialization
1290 expression to a symbol. */
1293 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1295 symbol_attribute attr;
1300 if (find_special (name, &sym, false))
1305 /* If this symbol is confirming an implicit parameter type,
1306 then an initialization expression is not allowed. */
1307 if (attr.flavor == FL_PARAMETER
1308 && sym->value != NULL
1311 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1318 /* An initializer is required for PARAMETER declarations. */
1319 if (attr.flavor == FL_PARAMETER)
1321 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1327 /* If a variable appears in a DATA block, it cannot have an
1331 gfc_error ("Variable '%s' at %C with an initializer already "
1332 "appears in a DATA statement", sym->name);
1336 /* Check if the assignment can happen. This has to be put off
1337 until later for a derived type variable. */
1338 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1339 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1340 && gfc_check_assign_symbol (sym, init) == FAILURE)
1343 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1344 && init->ts.type == BT_CHARACTER)
1346 /* Update symbol character length according initializer. */
1347 if (gfc_check_assign_symbol (sym, init) == FAILURE)
1350 if (sym->ts.u.cl->length == NULL)
1353 /* If there are multiple CHARACTER variables declared on the
1354 same line, we don't want them to share the same length. */
1355 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1357 if (sym->attr.flavor == FL_PARAMETER)
1359 if (init->expr_type == EXPR_CONSTANT)
1361 clen = init->value.character.length;
1362 sym->ts.u.cl->length = gfc_int_expr (clen);
1364 else if (init->expr_type == EXPR_ARRAY)
1366 gfc_expr *p = init->value.constructor->expr;
1367 clen = p->value.character.length;
1368 sym->ts.u.cl->length = gfc_int_expr (clen);
1370 else if (init->ts.u.cl && init->ts.u.cl->length)
1371 sym->ts.u.cl->length =
1372 gfc_copy_expr (sym->value->ts.u.cl->length);
1375 /* Update initializer character length according symbol. */
1376 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1378 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1379 gfc_constructor * p;
1381 if (init->expr_type == EXPR_CONSTANT)
1382 gfc_set_constant_character_len (len, init, -1);
1383 else if (init->expr_type == EXPR_ARRAY)
1385 /* Build a new charlen to prevent simplification from
1386 deleting the length before it is resolved. */
1387 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1388 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1390 for (p = init->value.constructor; p; p = p->next)
1391 gfc_set_constant_character_len (len, p->expr, -1);
1396 /* Need to check if the expression we initialized this
1397 to was one of the iso_c_binding named constants. If so,
1398 and we're a parameter (constant), let it be iso_c.
1400 integer(c_int), parameter :: my_int = c_int
1401 integer(my_int) :: my_int_2
1402 If we mark my_int as iso_c (since we can see it's value
1403 is equal to one of the named constants), then my_int_2
1404 will be considered C interoperable. */
1405 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1407 sym->ts.is_iso_c |= init->ts.is_iso_c;
1408 sym->ts.is_c_interop |= init->ts.is_c_interop;
1409 /* attr bits needed for module files. */
1410 sym->attr.is_iso_c |= init->ts.is_iso_c;
1411 sym->attr.is_c_interop |= init->ts.is_c_interop;
1412 if (init->ts.is_iso_c)
1413 sym->ts.f90_type = init->ts.f90_type;
1416 /* Add initializer. Make sure we keep the ranks sane. */
1417 if (sym->attr.dimension && init->rank == 0)
1423 if (sym->attr.flavor == FL_PARAMETER
1424 && init->expr_type == EXPR_CONSTANT
1425 && spec_size (sym->as, &size) == SUCCESS
1426 && mpz_cmp_si (size, 0) > 0)
1428 array = gfc_start_constructor (init->ts.type, init->ts.kind,
1431 array->value.constructor = c = NULL;
1432 for (n = 0; n < (int)mpz_get_si (size); n++)
1434 if (array->value.constructor == NULL)
1436 array->value.constructor = c = gfc_get_constructor ();
1441 c->next = gfc_get_constructor ();
1443 c->expr = gfc_copy_expr (init);
1447 array->shape = gfc_get_shape (sym->as->rank);
1448 for (n = 0; n < sym->as->rank; n++)
1449 spec_dimen_size (sym->as, n, &array->shape[n]);
1454 init->rank = sym->as->rank;
1458 if (sym->attr.save == SAVE_NONE)
1459 sym->attr.save = SAVE_IMPLICIT;
1467 /* Function called by variable_decl() that adds a name to a structure
1471 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1472 gfc_array_spec **as)
1476 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1477 constructing, it must have the pointer attribute. */
1478 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1479 && current_ts.u.derived == gfc_current_block ()
1480 && current_attr.pointer == 0)
1482 gfc_error ("Component at %C must have the POINTER attribute");
1486 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1488 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1490 gfc_error ("Array component of structure at %C must have explicit "
1491 "or deferred shape");
1496 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1500 if (c->ts.type == BT_CHARACTER)
1502 c->attr = current_attr;
1504 c->initializer = *init;
1509 c->attr.dimension = 1;
1512 /* Should this ever get more complicated, combine with similar section
1513 in add_init_expr_to_sym into a separate function. */
1514 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer && c->ts.u.cl
1515 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1519 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1520 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1521 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1523 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1525 if (c->initializer->expr_type == EXPR_CONSTANT)
1526 gfc_set_constant_character_len (len, c->initializer, -1);
1527 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1528 c->initializer->ts.u.cl->length->value.integer))
1531 gfc_constructor *ctor = c->initializer->value.constructor;
1533 has_ts = (c->initializer->ts.u.cl
1534 && c->initializer->ts.u.cl->length_from_typespec);
1540 /* Remember the length of the first element for checking
1541 that all elements *in the constructor* have the same
1542 length. This need not be the length of the LHS! */
1543 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1544 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1545 first_len = ctor->expr->value.character.length;
1547 for (; ctor; ctor = ctor->next)
1549 if (ctor->expr->expr_type == EXPR_CONSTANT)
1550 gfc_set_constant_character_len (len, ctor->expr,
1551 has_ts ? -1 : first_len);
1557 if (c->ts.type == BT_CLASS)
1558 encapsulate_class_symbol (&c->ts, &c->attr, &c->as);
1560 /* Check array components. */
1561 if (!c->attr.dimension)
1564 if (c->attr.pointer)
1566 if (c->as->type != AS_DEFERRED)
1568 gfc_error ("Pointer array component of structure at %C must have a "
1573 else if (c->attr.allocatable)
1575 if (c->as->type != AS_DEFERRED)
1577 gfc_error ("Allocatable component of structure at %C must have a "
1584 if (c->as->type != AS_EXPLICIT)
1586 gfc_error ("Array component of structure at %C must have an "
1596 /* Match a 'NULL()', and possibly take care of some side effects. */
1599 gfc_match_null (gfc_expr **result)
1605 m = gfc_match (" null ( )");
1609 /* The NULL symbol now has to be/become an intrinsic function. */
1610 if (gfc_get_symbol ("null", NULL, &sym))
1612 gfc_error ("NULL() initialization at %C is ambiguous");
1616 gfc_intrinsic_symbol (sym);
1618 if (sym->attr.proc != PROC_INTRINSIC
1619 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1620 sym->name, NULL) == FAILURE
1621 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1624 e = gfc_get_expr ();
1625 e->where = gfc_current_locus;
1626 e->expr_type = EXPR_NULL;
1627 e->ts.type = BT_UNKNOWN;
1635 /* Match a variable name with an optional initializer. When this
1636 subroutine is called, a variable is expected to be parsed next.
1637 Depending on what is happening at the moment, updates either the
1638 symbol table or the current interface. */
1641 variable_decl (int elem)
1643 char name[GFC_MAX_SYMBOL_LEN + 1];
1644 gfc_expr *initializer, *char_len;
1646 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1657 old_locus = gfc_current_locus;
1659 /* When we get here, we've just matched a list of attributes and
1660 maybe a type and a double colon. The next thing we expect to see
1661 is the name of the symbol. */
1662 m = gfc_match_name (name);
1666 var_locus = gfc_current_locus;
1668 /* Now we could see the optional array spec. or character length. */
1669 m = gfc_match_array_spec (&as);
1670 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1671 cp_as = gfc_copy_array_spec (as);
1672 else if (m == MATCH_ERROR)
1676 as = gfc_copy_array_spec (current_as);
1681 if (current_ts.type == BT_CHARACTER)
1683 switch (match_char_length (&char_len))
1686 cl = gfc_new_charlen (gfc_current_ns, NULL);
1688 cl->length = char_len;
1691 /* Non-constant lengths need to be copied after the first
1692 element. Also copy assumed lengths. */
1695 && (current_ts.u.cl->length == NULL
1696 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1698 cl = gfc_new_charlen (gfc_current_ns, NULL);
1699 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1702 cl = current_ts.u.cl;
1711 /* If this symbol has already shown up in a Cray Pointer declaration,
1712 then we want to set the type & bail out. */
1713 if (gfc_option.flag_cray_pointer)
1715 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1716 if (sym != NULL && sym->attr.cray_pointee)
1718 sym->ts.type = current_ts.type;
1719 sym->ts.kind = current_ts.kind;
1721 sym->ts.u.derived = current_ts.u.derived;
1722 sym->ts.is_c_interop = current_ts.is_c_interop;
1723 sym->ts.is_iso_c = current_ts.is_iso_c;
1726 /* Check to see if we have an array specification. */
1729 if (sym->as != NULL)
1731 gfc_error ("Duplicate array spec for Cray pointee at %C");
1732 gfc_free_array_spec (cp_as);
1738 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1739 gfc_internal_error ("Couldn't set pointee array spec.");
1741 /* Fix the array spec. */
1742 m = gfc_mod_pointee_as (sym->as);
1743 if (m == MATCH_ERROR)
1751 gfc_free_array_spec (cp_as);
1755 /* Procedure pointer as function result. */
1756 if (gfc_current_state () == COMP_FUNCTION
1757 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1758 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1759 strcpy (name, "ppr@");
1761 if (gfc_current_state () == COMP_FUNCTION
1762 && strcmp (name, gfc_current_block ()->name) == 0
1763 && gfc_current_block ()->result
1764 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1765 strcpy (name, "ppr@");
1767 /* OK, we've successfully matched the declaration. Now put the
1768 symbol in the current namespace, because it might be used in the
1769 optional initialization expression for this symbol, e.g. this is
1772 integer, parameter :: i = huge(i)
1774 This is only true for parameters or variables of a basic type.
1775 For components of derived types, it is not true, so we don't
1776 create a symbol for those yet. If we fail to create the symbol,
1778 if (gfc_current_state () != COMP_DERIVED
1779 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1785 /* An interface body specifies all of the procedure's
1786 characteristics and these shall be consistent with those
1787 specified in the procedure definition, except that the interface
1788 may specify a procedure that is not pure if the procedure is
1789 defined to be pure(12.3.2). */
1790 if (current_ts.type == BT_DERIVED
1791 && gfc_current_ns->proc_name
1792 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1793 && current_ts.u.derived->ns != gfc_current_ns)
1796 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.u.derived->name);
1797 if (!(current_ts.u.derived->attr.imported
1799 && st->n.sym == current_ts.u.derived)
1800 && !gfc_current_ns->has_import_set)
1802 gfc_error ("the type of '%s' at %C has not been declared within the "
1809 /* In functions that have a RESULT variable defined, the function
1810 name always refers to function calls. Therefore, the name is
1811 not allowed to appear in specification statements. */
1812 if (gfc_current_state () == COMP_FUNCTION
1813 && gfc_current_block () != NULL
1814 && gfc_current_block ()->result != NULL
1815 && gfc_current_block ()->result != gfc_current_block ()
1816 && strcmp (gfc_current_block ()->name, name) == 0)
1818 gfc_error ("Function name '%s' not allowed at %C", name);
1823 /* We allow old-style initializations of the form
1824 integer i /2/, j(4) /3*3, 1/
1825 (if no colon has been seen). These are different from data
1826 statements in that initializers are only allowed to apply to the
1827 variable immediately preceding, i.e.
1829 is not allowed. Therefore we have to do some work manually, that
1830 could otherwise be left to the matchers for DATA statements. */
1832 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1834 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1835 "initialization at %C") == FAILURE)
1838 return match_old_style_init (name);
1841 /* The double colon must be present in order to have initializers.
1842 Otherwise the statement is ambiguous with an assignment statement. */
1845 if (gfc_match (" =>") == MATCH_YES)
1847 if (!current_attr.pointer)
1849 gfc_error ("Initialization at %C isn't for a pointer variable");
1854 m = gfc_match_null (&initializer);
1857 gfc_error ("Pointer initialization requires a NULL() at %C");
1861 if (gfc_pure (NULL))
1863 gfc_error ("Initialization of pointer at %C is not allowed in "
1864 "a PURE procedure");
1872 else if (gfc_match_char ('=') == MATCH_YES)
1874 if (current_attr.pointer)
1876 gfc_error ("Pointer initialization at %C requires '=>', "
1882 m = gfc_match_init_expr (&initializer);
1885 gfc_error ("Expected an initialization expression at %C");
1889 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1891 gfc_error ("Initialization of variable at %C is not allowed in "
1892 "a PURE procedure");
1901 if (initializer != NULL && current_attr.allocatable
1902 && gfc_current_state () == COMP_DERIVED)
1904 gfc_error ("Initialization of allocatable component at %C is not "
1910 /* Add the initializer. Note that it is fine if initializer is
1911 NULL here, because we sometimes also need to check if a
1912 declaration *must* have an initialization expression. */
1913 if (gfc_current_state () != COMP_DERIVED)
1914 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1917 if (current_ts.type == BT_DERIVED
1918 && !current_attr.pointer && !initializer)
1919 initializer = gfc_default_initializer (¤t_ts);
1920 t = build_struct (name, cl, &initializer, &as);
1923 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1926 /* Free stuff up and return. */
1927 gfc_free_expr (initializer);
1928 gfc_free_array_spec (as);
1934 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1935 This assumes that the byte size is equal to the kind number for
1936 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1939 gfc_match_old_kind_spec (gfc_typespec *ts)
1944 if (gfc_match_char ('*') != MATCH_YES)
1947 m = gfc_match_small_literal_int (&ts->kind, NULL);
1951 original_kind = ts->kind;
1953 /* Massage the kind numbers for complex types. */
1954 if (ts->type == BT_COMPLEX)
1958 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1959 gfc_basic_typename (ts->type), original_kind);
1965 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1967 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1968 gfc_basic_typename (ts->type), original_kind);
1972 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1973 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1980 /* Match a kind specification. Since kinds are generally optional, we
1981 usually return MATCH_NO if something goes wrong. If a "kind="
1982 string is found, then we know we have an error. */
1985 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
1997 where = loc = gfc_current_locus;
2002 if (gfc_match_char ('(') == MATCH_NO)
2005 /* Also gobbles optional text. */
2006 if (gfc_match (" kind = ") == MATCH_YES)
2009 loc = gfc_current_locus;
2012 n = gfc_match_init_expr (&e);
2016 if (gfc_matching_function)
2018 /* The function kind expression might include use associated or
2019 imported parameters and try again after the specification
2021 if (gfc_match_char (')') != MATCH_YES)
2023 gfc_error ("Missing right parenthesis at %C");
2029 gfc_undo_symbols ();
2034 /* ....or else, the match is real. */
2036 gfc_error ("Expected initialization expression at %C");
2044 gfc_error ("Expected scalar initialization expression at %C");
2049 msg = gfc_extract_int (e, &ts->kind);
2058 /* Before throwing away the expression, let's see if we had a
2059 C interoperable kind (and store the fact). */
2060 if (e->ts.is_c_interop == 1)
2062 /* Mark this as c interoperable if being declared with one
2063 of the named constants from iso_c_binding. */
2064 ts->is_c_interop = e->ts.is_iso_c;
2065 ts->f90_type = e->ts.f90_type;
2071 /* Ignore errors to this point, if we've gotten here. This means
2072 we ignore the m=MATCH_ERROR from above. */
2073 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2075 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2076 gfc_basic_typename (ts->type));
2077 gfc_current_locus = where;
2081 /* Warn if, e.g., c_int is used for a REAL variable, but not
2082 if, e.g., c_double is used for COMPLEX as the standard
2083 explicitly says that the kind type parameter for complex and real
2084 variable is the same, i.e. c_float == c_float_complex. */
2085 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2086 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2087 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2088 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2089 "is %s", gfc_basic_typename (ts->f90_type), &where,
2090 gfc_basic_typename (ts->type));
2092 gfc_gobble_whitespace ();
2093 if ((c = gfc_next_ascii_char ()) != ')'
2094 && (ts->type != BT_CHARACTER || c != ','))
2096 if (ts->type == BT_CHARACTER)
2097 gfc_error ("Missing right parenthesis or comma at %C");
2099 gfc_error ("Missing right parenthesis at %C");
2103 /* All tests passed. */
2106 if(m == MATCH_ERROR)
2107 gfc_current_locus = where;
2109 /* Return what we know from the test(s). */
2114 gfc_current_locus = where;
2120 match_char_kind (int * kind, int * is_iso_c)
2129 where = gfc_current_locus;
2131 n = gfc_match_init_expr (&e);
2133 if (n != MATCH_YES && gfc_matching_function)
2135 /* The expression might include use-associated or imported
2136 parameters and try again after the specification
2139 gfc_undo_symbols ();
2144 gfc_error ("Expected initialization expression at %C");
2150 gfc_error ("Expected scalar initialization expression at %C");
2155 msg = gfc_extract_int (e, kind);
2156 *is_iso_c = e->ts.is_iso_c;
2166 /* Ignore errors to this point, if we've gotten here. This means
2167 we ignore the m=MATCH_ERROR from above. */
2168 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2170 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2174 /* All tests passed. */
2177 if (m == MATCH_ERROR)
2178 gfc_current_locus = where;
2180 /* Return what we know from the test(s). */
2185 gfc_current_locus = where;
2190 /* Match the various kind/length specifications in a CHARACTER
2191 declaration. We don't return MATCH_NO. */
2194 gfc_match_char_spec (gfc_typespec *ts)
2196 int kind, seen_length, is_iso_c;
2206 /* Try the old-style specification first. */
2207 old_char_selector = 0;
2209 m = match_char_length (&len);
2213 old_char_selector = 1;
2218 m = gfc_match_char ('(');
2221 m = MATCH_YES; /* Character without length is a single char. */
2225 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2226 if (gfc_match (" kind =") == MATCH_YES)
2228 m = match_char_kind (&kind, &is_iso_c);
2230 if (m == MATCH_ERROR)
2235 if (gfc_match (" , len =") == MATCH_NO)
2238 m = char_len_param_value (&len);
2241 if (m == MATCH_ERROR)
2248 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2249 if (gfc_match (" len =") == MATCH_YES)
2251 m = char_len_param_value (&len);
2254 if (m == MATCH_ERROR)
2258 if (gfc_match_char (')') == MATCH_YES)
2261 if (gfc_match (" , kind =") != MATCH_YES)
2264 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2270 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2271 m = char_len_param_value (&len);
2274 if (m == MATCH_ERROR)
2278 m = gfc_match_char (')');
2282 if (gfc_match_char (',') != MATCH_YES)
2285 gfc_match (" kind ="); /* Gobble optional text. */
2287 m = match_char_kind (&kind, &is_iso_c);
2288 if (m == MATCH_ERROR)
2294 /* Require a right-paren at this point. */
2295 m = gfc_match_char (')');
2300 gfc_error ("Syntax error in CHARACTER declaration at %C");
2302 gfc_free_expr (len);
2306 /* Deal with character functions after USE and IMPORT statements. */
2307 if (gfc_matching_function)
2309 gfc_free_expr (len);
2310 gfc_undo_symbols ();
2316 gfc_free_expr (len);
2320 /* Do some final massaging of the length values. */
2321 cl = gfc_new_charlen (gfc_current_ns, NULL);
2323 if (seen_length == 0)
2324 cl->length = gfc_int_expr (1);
2329 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2331 /* We have to know if it was a c interoperable kind so we can
2332 do accurate type checking of bind(c) procs, etc. */
2334 /* Mark this as c interoperable if being declared with one
2335 of the named constants from iso_c_binding. */
2336 ts->is_c_interop = is_iso_c;
2337 else if (len != NULL)
2338 /* Here, we might have parsed something such as: character(c_char)
2339 In this case, the parsing code above grabs the c_char when
2340 looking for the length (line 1690, roughly). it's the last
2341 testcase for parsing the kind params of a character variable.
2342 However, it's not actually the length. this seems like it
2344 To see if the user used a C interop kind, test the expr
2345 of the so called length, and see if it's C interoperable. */
2346 ts->is_c_interop = len->ts.is_iso_c;
2352 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2353 structure to the matched specification. This is necessary for FUNCTION and
2354 IMPLICIT statements.
2356 If implicit_flag is nonzero, then we don't check for the optional
2357 kind specification. Not doing so is needed for matching an IMPLICIT
2358 statement correctly. */
2361 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2363 char name[GFC_MAX_SYMBOL_LEN + 1];
2367 bool seen_deferred_kind;
2369 /* A belt and braces check that the typespec is correctly being treated
2370 as a deferred characteristic association. */
2371 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2372 && (gfc_current_block ()->result->ts.kind == -1)
2373 && (ts->kind == -1);
2375 if (seen_deferred_kind)
2378 /* Clear the current binding label, in case one is given. */
2379 curr_binding_label[0] = '\0';
2381 if (gfc_match (" byte") == MATCH_YES)
2383 if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2387 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2389 gfc_error ("BYTE type used at %C "
2390 "is not available on the target machine");
2394 ts->type = BT_INTEGER;
2399 if (gfc_match (" integer") == MATCH_YES)
2401 ts->type = BT_INTEGER;
2402 ts->kind = gfc_default_integer_kind;
2406 if (gfc_match (" character") == MATCH_YES)
2408 ts->type = BT_CHARACTER;
2409 if (implicit_flag == 0)
2410 return gfc_match_char_spec (ts);
2415 if (gfc_match (" real") == MATCH_YES)
2418 ts->kind = gfc_default_real_kind;
2422 if (gfc_match (" double precision") == MATCH_YES)
2425 ts->kind = gfc_default_double_kind;
2429 if (gfc_match (" complex") == MATCH_YES)
2431 ts->type = BT_COMPLEX;
2432 ts->kind = gfc_default_complex_kind;
2436 if (gfc_match (" double complex") == MATCH_YES)
2438 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
2439 "conform to the Fortran 95 standard") == FAILURE)
2442 ts->type = BT_COMPLEX;
2443 ts->kind = gfc_default_double_kind;
2447 if (gfc_match (" logical") == MATCH_YES)
2449 ts->type = BT_LOGICAL;
2450 ts->kind = gfc_default_logical_kind;
2454 m = gfc_match (" type ( %n )", name);
2456 ts->type = BT_DERIVED;
2459 m = gfc_match (" class ( %n )", name);
2462 ts->type = BT_CLASS;
2464 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2469 /* Defer association of the derived type until the end of the
2470 specification block. However, if the derived type can be
2471 found, add it to the typespec. */
2472 if (gfc_matching_function)
2474 ts->u.derived = NULL;
2475 if (gfc_current_state () != COMP_INTERFACE
2476 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2477 ts->u.derived = sym;
2481 /* Search for the name but allow the components to be defined later. If
2482 type = -1, this typespec has been seen in a function declaration but
2483 the type could not be accessed at that point. */
2485 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2487 gfc_error ("Type name '%s' at %C is ambiguous", name);
2490 else if (ts->kind == -1)
2492 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2493 || gfc_current_ns->has_import_set;
2494 if (gfc_find_symbol (name, NULL, iface, &sym))
2496 gfc_error ("Type name '%s' at %C is ambiguous", name);
2505 if (sym->attr.flavor != FL_DERIVED
2506 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2509 gfc_set_sym_referenced (sym);
2510 ts->u.derived = sym;
2515 /* For all types except double, derived and character, look for an
2516 optional kind specifier. MATCH_NO is actually OK at this point. */
2517 if (implicit_flag == 1)
2520 if (gfc_current_form == FORM_FREE)
2522 c = gfc_peek_ascii_char ();
2523 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2524 && c != ':' && c != ',')
2528 m = gfc_match_kind_spec (ts, false);
2529 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2530 m = gfc_match_old_kind_spec (ts);
2532 /* Defer association of the KIND expression of function results
2533 until after USE and IMPORT statements. */
2534 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2535 || gfc_matching_function)
2539 m = MATCH_YES; /* No kind specifier found. */
2545 /* Match an IMPLICIT NONE statement. Actually, this statement is
2546 already matched in parse.c, or we would not end up here in the
2547 first place. So the only thing we need to check, is if there is
2548 trailing garbage. If not, the match is successful. */
2551 gfc_match_implicit_none (void)
2553 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2557 /* Match the letter range(s) of an IMPLICIT statement. */
2560 match_implicit_range (void)
2566 cur_loc = gfc_current_locus;
2568 gfc_gobble_whitespace ();
2569 c = gfc_next_ascii_char ();
2572 gfc_error ("Missing character range in IMPLICIT at %C");
2579 gfc_gobble_whitespace ();
2580 c1 = gfc_next_ascii_char ();
2584 gfc_gobble_whitespace ();
2585 c = gfc_next_ascii_char ();
2590 inner = 0; /* Fall through. */
2597 gfc_gobble_whitespace ();
2598 c2 = gfc_next_ascii_char ();
2602 gfc_gobble_whitespace ();
2603 c = gfc_next_ascii_char ();
2605 if ((c != ',') && (c != ')'))
2618 gfc_error ("Letters must be in alphabetic order in "
2619 "IMPLICIT statement at %C");
2623 /* See if we can add the newly matched range to the pending
2624 implicits from this IMPLICIT statement. We do not check for
2625 conflicts with whatever earlier IMPLICIT statements may have
2626 set. This is done when we've successfully finished matching
2628 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2635 gfc_syntax_error (ST_IMPLICIT);
2637 gfc_current_locus = cur_loc;
2642 /* Match an IMPLICIT statement, storing the types for
2643 gfc_set_implicit() if the statement is accepted by the parser.
2644 There is a strange looking, but legal syntactic construction
2645 possible. It looks like:
2647 IMPLICIT INTEGER (a-b) (c-d)
2649 This is legal if "a-b" is a constant expression that happens to
2650 equal one of the legal kinds for integers. The real problem
2651 happens with an implicit specification that looks like:
2653 IMPLICIT INTEGER (a-b)
2655 In this case, a typespec matcher that is "greedy" (as most of the
2656 matchers are) gobbles the character range as a kindspec, leaving
2657 nothing left. We therefore have to go a bit more slowly in the
2658 matching process by inhibiting the kindspec checking during
2659 typespec matching and checking for a kind later. */
2662 gfc_match_implicit (void)
2671 /* We don't allow empty implicit statements. */
2672 if (gfc_match_eos () == MATCH_YES)
2674 gfc_error ("Empty IMPLICIT statement at %C");
2680 /* First cleanup. */
2681 gfc_clear_new_implicit ();
2683 /* A basic type is mandatory here. */
2684 m = gfc_match_decl_type_spec (&ts, 1);
2685 if (m == MATCH_ERROR)
2690 cur_loc = gfc_current_locus;
2691 m = match_implicit_range ();
2695 /* We may have <TYPE> (<RANGE>). */
2696 gfc_gobble_whitespace ();
2697 c = gfc_next_ascii_char ();
2698 if ((c == '\n') || (c == ','))
2700 /* Check for CHARACTER with no length parameter. */
2701 if (ts.type == BT_CHARACTER && !ts.u.cl)
2703 ts.kind = gfc_default_character_kind;
2704 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2705 ts.u.cl->length = gfc_int_expr (1);
2708 /* Record the Successful match. */
2709 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2714 gfc_current_locus = cur_loc;
2717 /* Discard the (incorrectly) matched range. */
2718 gfc_clear_new_implicit ();
2720 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2721 if (ts.type == BT_CHARACTER)
2722 m = gfc_match_char_spec (&ts);
2725 m = gfc_match_kind_spec (&ts, false);
2728 m = gfc_match_old_kind_spec (&ts);
2729 if (m == MATCH_ERROR)
2735 if (m == MATCH_ERROR)
2738 m = match_implicit_range ();
2739 if (m == MATCH_ERROR)
2744 gfc_gobble_whitespace ();
2745 c = gfc_next_ascii_char ();
2746 if ((c != '\n') && (c != ','))
2749 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2757 gfc_syntax_error (ST_IMPLICIT);
2765 gfc_match_import (void)
2767 char name[GFC_MAX_SYMBOL_LEN + 1];
2772 if (gfc_current_ns->proc_name == NULL
2773 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2775 gfc_error ("IMPORT statement at %C only permitted in "
2776 "an INTERFACE body");
2780 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2784 if (gfc_match_eos () == MATCH_YES)
2786 /* All host variables should be imported. */
2787 gfc_current_ns->has_import_set = 1;
2791 if (gfc_match (" ::") == MATCH_YES)
2793 if (gfc_match_eos () == MATCH_YES)
2795 gfc_error ("Expecting list of named entities at %C");
2802 m = gfc_match (" %n", name);
2806 if (gfc_current_ns->parent != NULL
2807 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2809 gfc_error ("Type name '%s' at %C is ambiguous", name);
2812 else if (gfc_current_ns->proc_name->ns->parent != NULL
2813 && gfc_find_symbol (name,
2814 gfc_current_ns->proc_name->ns->parent,
2817 gfc_error ("Type name '%s' at %C is ambiguous", name);
2823 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2824 "at %C - does not exist.", name);
2828 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2830 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2835 st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
2838 sym->attr.imported = 1;
2850 if (gfc_match_eos () == MATCH_YES)
2852 if (gfc_match_char (',') != MATCH_YES)
2859 gfc_error ("Syntax error in IMPORT statement at %C");
2864 /* A minimal implementation of gfc_match without whitespace, escape
2865 characters or variable arguments. Returns true if the next
2866 characters match the TARGET template exactly. */
2869 match_string_p (const char *target)
2873 for (p = target; *p; p++)
2874 if ((char) gfc_next_ascii_char () != *p)
2879 /* Matches an attribute specification including array specs. If
2880 successful, leaves the variables current_attr and current_as
2881 holding the specification. Also sets the colon_seen variable for
2882 later use by matchers associated with initializations.
2884 This subroutine is a little tricky in the sense that we don't know
2885 if we really have an attr-spec until we hit the double colon.
2886 Until that time, we can only return MATCH_NO. This forces us to
2887 check for duplicate specification at this level. */
2890 match_attr_spec (void)
2892 /* Modifiers that can exist in a type statement. */
2894 { GFC_DECL_BEGIN = 0,
2895 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2896 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2897 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2898 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2899 DECL_IS_BIND_C, DECL_NONE,
2900 GFC_DECL_END /* Sentinel */
2904 /* GFC_DECL_END is the sentinel, index starts at 0. */
2905 #define NUM_DECL GFC_DECL_END
2907 locus start, seen_at[NUM_DECL];
2914 gfc_clear_attr (¤t_attr);
2915 start = gfc_current_locus;
2920 /* See if we get all of the keywords up to the final double colon. */
2921 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2929 gfc_gobble_whitespace ();
2931 ch = gfc_next_ascii_char ();
2934 /* This is the successful exit condition for the loop. */
2935 if (gfc_next_ascii_char () == ':')
2940 gfc_gobble_whitespace ();
2941 switch (gfc_peek_ascii_char ())
2944 if (match_string_p ("allocatable"))
2945 d = DECL_ALLOCATABLE;
2949 /* Try and match the bind(c). */
2950 m = gfc_match_bind_c (NULL, true);
2953 else if (m == MATCH_ERROR)
2958 if (match_string_p ("dimension"))
2963 if (match_string_p ("external"))
2968 if (match_string_p ("int"))
2970 ch = gfc_next_ascii_char ();
2973 if (match_string_p ("nt"))
2975 /* Matched "intent". */
2976 /* TODO: Call match_intent_spec from here. */
2977 if (gfc_match (" ( in out )") == MATCH_YES)
2979 else if (gfc_match (" ( in )") == MATCH_YES)
2981 else if (gfc_match (" ( out )") == MATCH_YES)
2987 if (match_string_p ("insic"))
2989 /* Matched "intrinsic". */
2997 if (match_string_p ("optional"))
3002 gfc_next_ascii_char ();
3003 switch (gfc_next_ascii_char ())
3006 if (match_string_p ("rameter"))
3008 /* Matched "parameter". */
3014 if (match_string_p ("inter"))
3016 /* Matched "pointer". */
3022 ch = gfc_next_ascii_char ();
3025 if (match_string_p ("vate"))
3027 /* Matched "private". */
3033 if (match_string_p ("tected"))
3035 /* Matched "protected". */
3042 if (match_string_p ("blic"))
3044 /* Matched "public". */
3052 if (match_string_p ("save"))
3057 if (match_string_p ("target"))
3062 gfc_next_ascii_char ();
3063 ch = gfc_next_ascii_char ();
3066 if (match_string_p ("lue"))
3068 /* Matched "value". */
3074 if (match_string_p ("latile"))
3076 /* Matched "volatile". */
3084 /* No double colon and no recognizable decl_type, so assume that
3085 we've been looking at something else the whole time. */
3092 /* Check to make sure any parens are paired up correctly. */
3093 if (gfc_match_parens () == MATCH_ERROR)
3100 seen_at[d] = gfc_current_locus;
3102 if (d == DECL_DIMENSION)
3104 m = gfc_match_array_spec (¤t_as);
3108 gfc_error ("Missing dimension specification at %C");
3112 if (m == MATCH_ERROR)
3117 /* Since we've seen a double colon, we have to be looking at an
3118 attr-spec. This means that we can now issue errors. */
3119 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3124 case DECL_ALLOCATABLE:
3125 attr = "ALLOCATABLE";
3127 case DECL_DIMENSION:
3134 attr = "INTENT (IN)";
3137 attr = "INTENT (OUT)";
3140 attr = "INTENT (IN OUT)";
3142 case DECL_INTRINSIC:
3148 case DECL_PARAMETER:
3154 case DECL_PROTECTED:
3169 case DECL_IS_BIND_C:
3179 attr = NULL; /* This shouldn't happen. */
3182 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3187 /* Now that we've dealt with duplicate attributes, add the attributes
3188 to the current attribute. */
3189 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3194 if (gfc_current_state () == COMP_DERIVED
3195 && d != DECL_DIMENSION && d != DECL_POINTER
3196 && d != DECL_PRIVATE && d != DECL_PUBLIC
3199 if (d == DECL_ALLOCATABLE)
3201 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3202 "attribute at %C in a TYPE definition")
3211 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3218 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3219 && gfc_current_state () != COMP_MODULE)
3221 if (d == DECL_PRIVATE)
3225 if (gfc_current_state () == COMP_DERIVED
3226 && gfc_state_stack->previous
3227 && gfc_state_stack->previous->state == COMP_MODULE)
3229 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3230 "at %L in a TYPE definition", attr,
3240 gfc_error ("%s attribute at %L is not allowed outside of the "
3241 "specification part of a module", attr, &seen_at[d]);
3249 case DECL_ALLOCATABLE:
3250 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
3253 case DECL_DIMENSION:
3254 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
3258 t = gfc_add_external (¤t_attr, &seen_at[d]);
3262 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
3266 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
3270 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
3273 case DECL_INTRINSIC:
3274 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
3278 t = gfc_add_optional (¤t_attr, &seen_at[d]);
3281 case DECL_PARAMETER:
3282 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
3286 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
3289 case DECL_PROTECTED:
3290 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3292 gfc_error ("PROTECTED at %C only allowed in specification "
3293 "part of a module");
3298 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3303 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
3307 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
3312 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
3317 t = gfc_add_save (¤t_attr, NULL, &seen_at[d]);
3321 t = gfc_add_target (¤t_attr, &seen_at[d]);
3324 case DECL_IS_BIND_C:
3325 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
3329 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3334 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
3338 if (gfc_notify_std (GFC_STD_F2003,
3339 "Fortran 2003: VOLATILE attribute at %C")
3343 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
3347 gfc_internal_error ("match_attr_spec(): Bad attribute");
3361 gfc_current_locus = start;
3362 gfc_free_array_spec (current_as);
3368 /* Set the binding label, dest_label, either with the binding label
3369 stored in the given gfc_typespec, ts, or if none was provided, it
3370 will be the symbol name in all lower case, as required by the draft
3371 (J3/04-007, section 15.4.1). If a binding label was given and
3372 there is more than one argument (num_idents), it is an error. */
3375 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3377 if (num_idents > 1 && has_name_equals)
3379 gfc_error ("Multiple identifiers provided with "
3380 "single NAME= specifier at %C");
3384 if (curr_binding_label[0] != '\0')
3386 /* Binding label given; store in temp holder til have sym. */
3387 strcpy (dest_label, curr_binding_label);
3391 /* No binding label given, and the NAME= specifier did not exist,
3392 which means there was no NAME="". */
3393 if (sym_name != NULL && has_name_equals == 0)
3394 strcpy (dest_label, sym_name);
3401 /* Set the status of the given common block as being BIND(C) or not,
3402 depending on the given parameter, is_bind_c. */
3405 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3407 com_block->is_bind_c = is_bind_c;
3412 /* Verify that the given gfc_typespec is for a C interoperable type. */
3415 verify_c_interop (gfc_typespec *ts)
3417 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3418 return (ts->u.derived->ts.is_c_interop ? SUCCESS : FAILURE);
3419 else if (ts->is_c_interop != 1)
3426 /* Verify that the variables of a given common block, which has been
3427 defined with the attribute specifier bind(c), to be of a C
3428 interoperable type. Errors will be reported here, if
3432 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3434 gfc_symbol *curr_sym = NULL;
3435 gfc_try retval = SUCCESS;
3437 curr_sym = com_block->head;
3439 /* Make sure we have at least one symbol. */
3440 if (curr_sym == NULL)
3443 /* Here we know we have a symbol, so we'll execute this loop
3447 /* The second to last param, 1, says this is in a common block. */
3448 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3449 curr_sym = curr_sym->common_next;
3450 } while (curr_sym != NULL);
3456 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3457 an appropriate error message is reported. */
3460 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3461 int is_in_common, gfc_common_head *com_block)
3463 bool bind_c_function = false;
3464 gfc_try retval = SUCCESS;
3466 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3467 bind_c_function = true;
3469 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3471 tmp_sym = tmp_sym->result;
3472 /* Make sure it wasn't an implicitly typed result. */
3473 if (tmp_sym->attr.implicit_type)
3475 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3476 "%L may not be C interoperable", tmp_sym->name,
3477 &tmp_sym->declared_at);
3478 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3479 /* Mark it as C interoperable to prevent duplicate warnings. */
3480 tmp_sym->ts.is_c_interop = 1;
3481 tmp_sym->attr.is_c_interop = 1;
3485 /* Here, we know we have the bind(c) attribute, so if we have
3486 enough type info, then verify that it's a C interop kind.
3487 The info could be in the symbol already, or possibly still in
3488 the given ts (current_ts), so look in both. */
3489 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3491 if (verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3493 /* See if we're dealing with a sym in a common block or not. */
3494 if (is_in_common == 1)
3496 gfc_warning ("Variable '%s' in common block '%s' at %L "
3497 "may not be a C interoperable "
3498 "kind though common block '%s' is BIND(C)",
3499 tmp_sym->name, com_block->name,
3500 &(tmp_sym->declared_at), com_block->name);
3504 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3505 gfc_error ("Type declaration '%s' at %L is not C "
3506 "interoperable but it is BIND(C)",
3507 tmp_sym->name, &(tmp_sym->declared_at));
3509 gfc_warning ("Variable '%s' at %L "
3510 "may not be a C interoperable "
3511 "kind but it is bind(c)",
3512 tmp_sym->name, &(tmp_sym->declared_at));
3516 /* Variables declared w/in a common block can't be bind(c)
3517 since there's no way for C to see these variables, so there's
3518 semantically no reason for the attribute. */
3519 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3521 gfc_error ("Variable '%s' in common block '%s' at "
3522 "%L cannot be declared with BIND(C) "
3523 "since it is not a global",
3524 tmp_sym->name, com_block->name,
3525 &(tmp_sym->declared_at));
3529 /* Scalar variables that are bind(c) can not have the pointer
3530 or allocatable attributes. */
3531 if (tmp_sym->attr.is_bind_c == 1)
3533 if (tmp_sym->attr.pointer == 1)
3535 gfc_error ("Variable '%s' at %L cannot have both the "
3536 "POINTER and BIND(C) attributes",
3537 tmp_sym->name, &(tmp_sym->declared_at));
3541 if (tmp_sym->attr.allocatable == 1)
3543 gfc_error ("Variable '%s' at %L cannot have both the "
3544 "ALLOCATABLE and BIND(C) attributes",
3545 tmp_sym->name, &(tmp_sym->declared_at));
3551 /* If it is a BIND(C) function, make sure the return value is a
3552 scalar value. The previous tests in this function made sure
3553 the type is interoperable. */
3554 if (bind_c_function && tmp_sym->as != NULL)
3555 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3556 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3558 /* BIND(C) functions can not return a character string. */
3559 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3560 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
3561 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
3562 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
3563 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3564 "be a character string", tmp_sym->name,
3565 &(tmp_sym->declared_at));
3568 /* See if the symbol has been marked as private. If it has, make sure
3569 there is no binding label and warn the user if there is one. */
3570 if (tmp_sym->attr.access == ACCESS_PRIVATE
3571 && tmp_sym->binding_label[0] != '\0')
3572 /* Use gfc_warning_now because we won't say that the symbol fails
3573 just because of this. */
3574 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3575 "given the binding label '%s'", tmp_sym->name,
3576 &(tmp_sym->declared_at), tmp_sym->binding_label);
3582 /* Set the appropriate fields for a symbol that's been declared as
3583 BIND(C) (the is_bind_c flag and the binding label), and verify that
3584 the type is C interoperable. Errors are reported by the functions
3585 used to set/test these fields. */
3588 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3590 gfc_try retval = SUCCESS;
3592 /* TODO: Do we need to make sure the vars aren't marked private? */
3594 /* Set the is_bind_c bit in symbol_attribute. */
3595 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3597 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3598 num_idents) != SUCCESS)
3605 /* Set the fields marking the given common block as BIND(C), including
3606 a binding label, and report any errors encountered. */
3609 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3611 gfc_try retval = SUCCESS;
3613 /* destLabel, common name, typespec (which may have binding label). */
3614 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3618 /* Set the given common block (com_block) to being bind(c) (1). */
3619 set_com_block_bind_c (com_block, 1);
3625 /* Retrieve the list of one or more identifiers that the given bind(c)
3626 attribute applies to. */
3629 get_bind_c_idents (void)
3631 char name[GFC_MAX_SYMBOL_LEN + 1];
3633 gfc_symbol *tmp_sym = NULL;
3635 gfc_common_head *com_block = NULL;
3637 if (gfc_match_name (name) == MATCH_YES)
3639 found_id = MATCH_YES;
3640 gfc_get_ha_symbol (name, &tmp_sym);
3642 else if (match_common_name (name) == MATCH_YES)
3644 found_id = MATCH_YES;
3645 com_block = gfc_get_common (name, 0);
3649 gfc_error ("Need either entity or common block name for "
3650 "attribute specification statement at %C");
3654 /* Save the current identifier and look for more. */
3657 /* Increment the number of identifiers found for this spec stmt. */
3660 /* Make sure we have a sym or com block, and verify that it can
3661 be bind(c). Set the appropriate field(s) and look for more
3663 if (tmp_sym != NULL || com_block != NULL)
3665 if (tmp_sym != NULL)
3667 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3673 if (set_verify_bind_c_com_block(com_block, num_idents)
3678 /* Look to see if we have another identifier. */
3680 if (gfc_match_eos () == MATCH_YES)
3681 found_id = MATCH_NO;
3682 else if (gfc_match_char (',') != MATCH_YES)
3683 found_id = MATCH_NO;
3684 else if (gfc_match_name (name) == MATCH_YES)
3686 found_id = MATCH_YES;
3687 gfc_get_ha_symbol (name, &tmp_sym);
3689 else if (match_common_name (name) == MATCH_YES)
3691 found_id = MATCH_YES;
3692 com_block = gfc_get_common (name, 0);
3696 gfc_error ("Missing entity or common block name for "
3697 "attribute specification statement at %C");
3703 gfc_internal_error ("Missing symbol");
3705 } while (found_id == MATCH_YES);
3707 /* if we get here we were successful */
3712 /* Try and match a BIND(C) attribute specification statement. */
3715 gfc_match_bind_c_stmt (void)
3717 match found_match = MATCH_NO;
3722 /* This may not be necessary. */
3724 /* Clear the temporary binding label holder. */
3725 curr_binding_label[0] = '\0';
3727 /* Look for the bind(c). */
3728 found_match = gfc_match_bind_c (NULL, true);
3730 if (found_match == MATCH_YES)
3732 /* Look for the :: now, but it is not required. */
3735 /* Get the identifier(s) that needs to be updated. This may need to
3736 change to hand the flag(s) for the attr specified so all identifiers
3737 found can have all appropriate parts updated (assuming that the same
3738 spec stmt can have multiple attrs, such as both bind(c) and
3740 if (get_bind_c_idents () != SUCCESS)
3741 /* Error message should have printed already. */
3749 /* Match a data declaration statement. */
3752 gfc_match_data_decl (void)
3758 num_idents_on_line = 0;
3760 m = gfc_match_decl_type_spec (¤t_ts, 0);
3764 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
3766 sym = gfc_use_derived (current_ts.u.derived);
3774 current_ts.u.derived = sym;
3777 m = match_attr_spec ();
3778 if (m == MATCH_ERROR)
3784 if (current_ts.type == BT_DERIVED && current_ts.u.derived->components == NULL
3785 && !current_ts.u.derived->attr.zero_comp)
3788 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3791 gfc_find_symbol (current_ts.u.derived->name,
3792 current_ts.u.derived->ns->parent, 1, &sym);
3794 /* Any symbol that we find had better be a type definition
3795 which has its components defined. */
3796 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3797 && (current_ts.u.derived->components != NULL
3798 || current_ts.u.derived->attr.zero_comp))
3801 /* Now we have an error, which we signal, and then fix up
3802 because the knock-on is plain and simple confusing. */
3803 gfc_error_now ("Derived type at %C has not been previously defined "
3804 "and so cannot appear in a derived type definition");
3805 current_attr.pointer = 1;
3810 /* If we have an old-style character declaration, and no new-style
3811 attribute specifications, then there a comma is optional between
3812 the type specification and the variable list. */
3813 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
3814 gfc_match_char (',');
3816 /* Give the types/attributes to symbols that follow. Give the element
3817 a number so that repeat character length expressions can be copied. */
3821 num_idents_on_line++;
3822 m = variable_decl (elem++);
3823 if (m == MATCH_ERROR)
3828 if (gfc_match_eos () == MATCH_YES)
3830 if (gfc_match_char (',') != MATCH_YES)
3834 if (gfc_error_flag_test () == 0)
3835 gfc_error ("Syntax error in data declaration at %C");
3838 gfc_free_data_all (gfc_current_ns);
3841 gfc_free_array_spec (current_as);
3847 /* Match a prefix associated with a function or subroutine
3848 declaration. If the typespec pointer is nonnull, then a typespec
3849 can be matched. Note that if nothing matches, MATCH_YES is
3850 returned (the null string was matched). */
3853 gfc_match_prefix (gfc_typespec *ts)
3857 gfc_clear_attr (¤t_attr);
3860 gcc_assert (!gfc_matching_prefix);
3861 gfc_matching_prefix = true;
3864 if (!seen_type && ts != NULL
3865 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
3866 && gfc_match_space () == MATCH_YES)
3873 if (gfc_match ("elemental% ") == MATCH_YES)
3875 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
3881 if (gfc_match ("pure% ") == MATCH_YES)
3883 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
3889 if (gfc_match ("recursive% ") == MATCH_YES)
3891 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
3897 /* At this point, the next item is not a prefix. */
3898 gcc_assert (gfc_matching_prefix);
3899 gfc_matching_prefix = false;
3903 gcc_assert (gfc_matching_prefix);
3904 gfc_matching_prefix = false;
3909 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
3912 copy_prefix (symbol_attribute *dest, locus *where)
3914 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
3917 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
3920 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
3927 /* Match a formal argument list. */
3930 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
3932 gfc_formal_arglist *head, *tail, *p, *q;
3933 char name[GFC_MAX_SYMBOL_LEN + 1];
3939 if (gfc_match_char ('(') != MATCH_YES)
3946 if (gfc_match_char (')') == MATCH_YES)
3951 if (gfc_match_char ('*') == MATCH_YES)
3955 m = gfc_match_name (name);
3959 if (gfc_get_symbol (name, NULL, &sym))
3963 p = gfc_get_formal_arglist ();
3975 /* We don't add the VARIABLE flavor because the name could be a
3976 dummy procedure. We don't apply these attributes to formal
3977 arguments of statement functions. */
3978 if (sym != NULL && !st_flag
3979 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
3980 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
3986 /* The name of a program unit can be in a different namespace,
3987 so check for it explicitly. After the statement is accepted,
3988 the name is checked for especially in gfc_get_symbol(). */
3989 if (gfc_new_block != NULL && sym != NULL
3990 && strcmp (sym->name, gfc_new_block->name) == 0)
3992 gfc_error ("Name '%s' at %C is the name of the procedure",
3998 if (gfc_match_char (')') == MATCH_YES)
4001 m = gfc_match_char (',');
4004 gfc_error ("Unexpected junk in formal argument list at %C");
4010 /* Check for duplicate symbols in the formal argument list. */
4013 for (p = head; p->next; p = p->next)
4018 for (q = p->next; q; q = q->next)
4019 if (p->sym == q->sym)
4021 gfc_error ("Duplicate symbol '%s' in formal argument list "
4022 "at %C", p->sym->name);
4030 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
4040 gfc_free_formal_arglist (head);
4045 /* Match a RESULT specification following a function declaration or
4046 ENTRY statement. Also matches the end-of-statement. */
4049 match_result (gfc_symbol *function, gfc_symbol **result)
4051 char name[GFC_MAX_SYMBOL_LEN + 1];
4055 if (gfc_match (" result (") != MATCH_YES)
4058 m = gfc_match_name (name);
4062 /* Get the right paren, and that's it because there could be the
4063 bind(c) attribute after the result clause. */
4064 if (gfc_match_char(')') != MATCH_YES)
4066 /* TODO: should report the missing right paren here. */
4070 if (strcmp (function->name, name) == 0)
4072 gfc_error ("RESULT variable at %C must be different than function name");
4076 if (gfc_get_symbol (name, NULL, &r))
4079 if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4088 /* Match a function suffix, which could be a combination of a result
4089 clause and BIND(C), either one, or neither. The draft does not
4090 require them to come in a specific order. */
4093 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4095 match is_bind_c; /* Found bind(c). */
4096 match is_result; /* Found result clause. */
4097 match found_match; /* Status of whether we've found a good match. */
4098 char peek_char; /* Character we're going to peek at. */
4099 bool allow_binding_name;
4101 /* Initialize to having found nothing. */
4102 found_match = MATCH_NO;
4103 is_bind_c = MATCH_NO;
4104 is_result = MATCH_NO;
4106 /* Get the next char to narrow between result and bind(c). */
4107 gfc_gobble_whitespace ();
4108 peek_char = gfc_peek_ascii_char ();
4110 /* C binding names are not allowed for internal procedures. */
4111 if (gfc_current_state () == COMP_CONTAINS
4112 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4113 allow_binding_name = false;
4115 allow_binding_name = true;
4120 /* Look for result clause. */
4121 is_result = match_result (sym, result);
4122 if (is_result == MATCH_YES)
4124 /* Now see if there is a bind(c) after it. */
4125 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4126 /* We've found the result clause and possibly bind(c). */
4127 found_match = MATCH_YES;
4130 /* This should only be MATCH_ERROR. */
4131 found_match = is_result;
4134 /* Look for bind(c) first. */
4135 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4136 if (is_bind_c == MATCH_YES)
4138 /* Now see if a result clause followed it. */
4139 is_result = match_result (sym, result);
4140 found_match = MATCH_YES;
4144 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4145 found_match = MATCH_ERROR;
4149 gfc_error ("Unexpected junk after function declaration at %C");
4150 found_match = MATCH_ERROR;
4154 if (is_bind_c == MATCH_YES)
4156 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4157 if (gfc_current_state () == COMP_CONTAINS
4158 && sym->ns->proc_name->attr.flavor != FL_MODULE
4159 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4160 "at %L may not be specified for an internal "
4161 "procedure", &gfc_current_locus)
4165 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4174 /* Procedure pointer return value without RESULT statement:
4175 Add "hidden" result variable named "ppr@". */
4178 add_hidden_procptr_result (gfc_symbol *sym)
4182 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4185 /* First usage case: PROCEDURE and EXTERNAL statements. */
4186 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4187 && strcmp (gfc_current_block ()->name, sym->name) == 0
4188 && sym->attr.external;
4189 /* Second usage case: INTERFACE statements. */
4190 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4191 && gfc_state_stack->previous->state == COMP_FUNCTION
4192 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4198 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4202 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4203 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4204 st2->n.sym = stree->n.sym;
4206 sym->result = stree->n.sym;
4208 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4209 sym->result->attr.pointer = sym->attr.pointer;
4210 sym->result->attr.external = sym->attr.external;
4211 sym->result->attr.referenced = sym->attr.referenced;
4212 sym->result->ts = sym->ts;
4213 sym->attr.proc_pointer = 0;
4214 sym->attr.pointer = 0;
4215 sym->attr.external = 0;
4216 if (sym->result->attr.external && sym->result->attr.pointer)
4218 sym->result->attr.pointer = 0;
4219 sym->result->attr.proc_pointer = 1;
4222 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4224 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4225 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4226 && sym->result && sym->result != sym && sym->result->attr.external
4227 && sym == gfc_current_ns->proc_name
4228 && sym == sym->result->ns->proc_name
4229 && strcmp ("ppr@", sym->result->name) == 0)
4231 sym->result->attr.proc_pointer = 1;
4232 sym->attr.pointer = 0;
4240 /* Match the interface for a PROCEDURE declaration,
4241 including brackets (R1212). */
4244 match_procedure_interface (gfc_symbol **proc_if)
4248 locus old_loc, entry_loc;
4249 gfc_namespace *old_ns = gfc_current_ns;
4250 char name[GFC_MAX_SYMBOL_LEN + 1];
4252 old_loc = entry_loc = gfc_current_locus;
4253 gfc_clear_ts (¤t_ts);
4255 if (gfc_match (" (") != MATCH_YES)
4257 gfc_current_locus = entry_loc;
4261 /* Get the type spec. for the procedure interface. */
4262 old_loc = gfc_current_locus;
4263 m = gfc_match_decl_type_spec (¤t_ts, 0);
4264 gfc_gobble_whitespace ();
4265 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4268 if (m == MATCH_ERROR)
4271 /* Procedure interface is itself a procedure. */
4272 gfc_current_locus = old_loc;
4273 m = gfc_match_name (name);
4275 /* First look to see if it is already accessible in the current
4276 namespace because it is use associated or contained. */
4278 if (gfc_find_sym_tree (name, NULL, 0, &st))
4281 /* If it is still not found, then try the parent namespace, if it
4282 exists and create the symbol there if it is still not found. */
4283 if (gfc_current_ns->parent)
4284 gfc_current_ns = gfc_current_ns->parent;
4285 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4288 gfc_current_ns = old_ns;
4289 *proc_if = st->n.sym;
4291 /* Various interface checks. */
4295 /* Resolve interface if possible. That way, attr.procedure is only set
4296 if it is declared by a later procedure-declaration-stmt, which is
4297 invalid per C1212. */
4298 while ((*proc_if)->ts.interface)
4299 *proc_if = (*proc_if)->ts.interface;
4301 if ((*proc_if)->generic)
4303 gfc_error ("Interface '%s' at %C may not be generic",
4307 if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
4309 gfc_error ("Interface '%s' at %C may not be a statement function",
4313 /* Handle intrinsic procedures. */
4314 if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4315 || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4316 && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4317 || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4318 (*proc_if)->attr.intrinsic = 1;
4319 if ((*proc_if)->attr.intrinsic
4320 && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
4322 gfc_error ("Intrinsic procedure '%s' not allowed "
4323 "in PROCEDURE statement at %C", (*proc_if)->name);
4329 if (gfc_match (" )") != MATCH_YES)
4331 gfc_current_locus = entry_loc;
4339 /* Match a PROCEDURE declaration (R1211). */
4342 match_procedure_decl (void)
4345 gfc_symbol *sym, *proc_if = NULL;
4347 gfc_expr *initializer = NULL;
4349 /* Parse interface (with brackets). */
4350 m = match_procedure_interface (&proc_if);
4354 /* Parse attributes (with colons). */
4355 m = match_attr_spec();
4356 if (m == MATCH_ERROR)
4359 /* Get procedure symbols. */
4362 m = gfc_match_symbol (&sym, 0);
4365 else if (m == MATCH_ERROR)
4368 /* Add current_attr to the symbol attributes. */
4369 if (gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
4372 if (sym->attr.is_bind_c)
4374 /* Check for C1218. */
4375 if (!proc_if || !proc_if->attr.is_bind_c)
4377 gfc_error ("BIND(C) attribute at %C requires "
4378 "an interface with BIND(C)");
4381 /* Check for C1217. */
4382 if (has_name_equals && sym->attr.pointer)
4384 gfc_error ("BIND(C) procedure with NAME may not have "
4385 "POINTER attribute at %C");
4388 if (has_name_equals && sym->attr.dummy)
4390 gfc_error ("Dummy procedure at %C may not have "
4391 "BIND(C) attribute with NAME");
4394 /* Set binding label for BIND(C). */
4395 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4399 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4402 if (add_hidden_procptr_result (sym) == SUCCESS)
4405 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4408 /* Set interface. */
4409 if (proc_if != NULL)
4411 if (sym->ts.type != BT_UNKNOWN)
4413 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4414 sym->name, &gfc_current_locus,
4415 gfc_basic_typename (sym->ts.type));
4418 sym->ts.interface = proc_if;
4419 sym->attr.untyped = 1;
4420 sym->attr.if_source = IFSRC_IFBODY;
4422 else if (current_ts.type != BT_UNKNOWN)
4424 if (gfc_add_type (sym, ¤t_ts, &gfc_current_locus) == FAILURE)
4426 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4427 sym->ts.interface->ts = current_ts;
4428 sym->ts.interface->attr.function = 1;
4429 sym->attr.function = sym->ts.interface->attr.function;
4430 sym->attr.if_source = IFSRC_UNKNOWN;
4433 if (gfc_match (" =>") == MATCH_YES)
4435 if (!current_attr.pointer)
4437 gfc_error ("Initialization at %C isn't for a pointer variable");
4442 m = gfc_match_null (&initializer);
4445 gfc_error ("Pointer initialization requires a NULL() at %C");
4449 if (gfc_pure (NULL))
4451 gfc_error ("Initialization of pointer at %C is not allowed in "
4452 "a PURE procedure");
4459 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4465 gfc_set_sym_referenced (sym);
4467 if (gfc_match_eos () == MATCH_YES)
4469 if (gfc_match_char (',') != MATCH_YES)
4474 gfc_error ("Syntax error in PROCEDURE statement at %C");
4478 /* Free stuff up and return. */
4479 gfc_free_expr (initializer);
4485 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4488 /* Match a procedure pointer component declaration (R445). */
4491 match_ppc_decl (void)
4494 gfc_symbol *proc_if = NULL;
4498 gfc_expr *initializer = NULL;
4499 gfc_typebound_proc* tb;
4500 char name[GFC_MAX_SYMBOL_LEN + 1];
4502 /* Parse interface (with brackets). */
4503 m = match_procedure_interface (&proc_if);
4507 /* Parse attributes. */
4508 tb = XCNEW (gfc_typebound_proc);
4509 tb->where = gfc_current_locus;
4510 m = match_binding_attributes (tb, false, true);
4511 if (m == MATCH_ERROR)
4514 gfc_clear_attr (¤t_attr);
4515 current_attr.procedure = 1;
4516 current_attr.proc_pointer = 1;
4517 current_attr.access = tb->access;
4518 current_attr.flavor = FL_PROCEDURE;
4520 /* Match the colons (required). */
4521 if (gfc_match (" ::") != MATCH_YES)
4523 gfc_error ("Expected '::' after binding-attributes at %C");
4527 /* Check for C450. */
4528 if (!tb->nopass && proc_if == NULL)
4530 gfc_error("NOPASS or explicit interface required at %C");
4534 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure pointer "
4535 "component at %C") == FAILURE)
4538 /* Match PPC names. */
4542 m = gfc_match_name (name);
4545 else if (m == MATCH_ERROR)
4548 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
4551 /* Add current_attr to the symbol attributes. */
4552 if (gfc_copy_attr (&c->attr, ¤t_attr, NULL) == FAILURE)
4555 if (gfc_add_external (&c->attr, NULL) == FAILURE)
4558 if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
4563 /* Set interface. */
4564 if (proc_if != NULL)
4566 c->ts.interface = proc_if;
4567 c->attr.untyped = 1;
4568 c->attr.if_source = IFSRC_IFBODY;
4570 else if (ts.type != BT_UNKNOWN)
4573 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4574 c->ts.interface->ts = ts;
4575 c->ts.interface->attr.function = 1;
4576 c->attr.function = c->ts.interface->attr.function;
4577 c->attr.if_source = IFSRC_UNKNOWN;
4580 if (gfc_match (" =>") == MATCH_YES)
4582 m = gfc_match_null (&initializer);
4585 gfc_error ("Pointer initialization requires a NULL() at %C");
4588 if (gfc_pure (NULL))
4590 gfc_error ("Initialization of pointer at %C is not allowed in "
4591 "a PURE procedure");
4596 gfc_free_expr (initializer);
4599 c->initializer = initializer;
4602 if (gfc_match_eos () == MATCH_YES)
4604 if (gfc_match_char (',') != MATCH_YES)
4609 gfc_error ("Syntax error in procedure pointer component at %C");
4614 /* Match a PROCEDURE declaration inside an interface (R1206). */
4617 match_procedure_in_interface (void)
4621 char name[GFC_MAX_SYMBOL_LEN + 1];
4623 if (current_interface.type == INTERFACE_NAMELESS
4624 || current_interface.type == INTERFACE_ABSTRACT)
4626 gfc_error ("PROCEDURE at %C must be in a generic interface");
4632 m = gfc_match_name (name);
4635 else if (m == MATCH_ERROR)
4637 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4640 if (gfc_add_interface (sym) == FAILURE)
4643 if (gfc_match_eos () == MATCH_YES)
4645 if (gfc_match_char (',') != MATCH_YES)
4652 gfc_error ("Syntax error in PROCEDURE statement at %C");
4657 /* General matcher for PROCEDURE declarations. */
4659 static match match_procedure_in_type (void);
4662 gfc_match_procedure (void)
4666 switch (gfc_current_state ())
4671 case COMP_SUBROUTINE:
4673 m = match_procedure_decl ();
4675 case COMP_INTERFACE:
4676 m = match_procedure_in_interface ();
4679 m = match_ppc_decl ();
4681 case COMP_DERIVED_CONTAINS:
4682 m = match_procedure_in_type ();
4691 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4699 /* Warn if a matched procedure has the same name as an intrinsic; this is
4700 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4701 parser-state-stack to find out whether we're in a module. */
4704 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
4708 in_module = (gfc_state_stack->previous
4709 && gfc_state_stack->previous->state == COMP_MODULE);
4711 gfc_warn_intrinsic_shadow (sym, in_module, func);
4715 /* Match a function declaration. */
4718 gfc_match_function_decl (void)
4720 char name[GFC_MAX_SYMBOL_LEN + 1];
4721 gfc_symbol *sym, *result;
4725 match found_match; /* Status returned by match func. */
4727 if (gfc_current_state () != COMP_NONE
4728 && gfc_current_state () != COMP_INTERFACE
4729 && gfc_current_state () != COMP_CONTAINS)
4732 gfc_clear_ts (¤t_ts);
4734 old_loc = gfc_current_locus;
4736 m = gfc_match_prefix (¤t_ts);
4739 gfc_current_locus = old_loc;
4743 if (gfc_match ("function% %n", name) != MATCH_YES)
4745 gfc_current_locus = old_loc;
4748 if (get_proc_name (name, &sym, false))
4751 if (add_hidden_procptr_result (sym) == SUCCESS)
4754 gfc_new_block = sym;
4756 m = gfc_match_formal_arglist (sym, 0, 0);
4759 gfc_error ("Expected formal argument list in function "
4760 "definition at %C");
4764 else if (m == MATCH_ERROR)
4769 /* According to the draft, the bind(c) and result clause can
4770 come in either order after the formal_arg_list (i.e., either
4771 can be first, both can exist together or by themselves or neither
4772 one). Therefore, the match_result can't match the end of the
4773 string, and check for the bind(c) or result clause in either order. */
4774 found_match = gfc_match_eos ();
4776 /* Make sure that it isn't already declared as BIND(C). If it is, it
4777 must have been marked BIND(C) with a BIND(C) attribute and that is
4778 not allowed for procedures. */
4779 if (sym->attr.is_bind_c == 1)
4781 sym->attr.is_bind_c = 0;
4782 if (sym->old_symbol != NULL)
4783 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4784 "variables or common blocks",
4785 &(sym->old_symbol->declared_at));
4787 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4788 "variables or common blocks", &gfc_current_locus);
4791 if (found_match != MATCH_YES)
4793 /* If we haven't found the end-of-statement, look for a suffix. */
4794 suffix_match = gfc_match_suffix (sym, &result);
4795 if (suffix_match == MATCH_YES)
4796 /* Need to get the eos now. */
4797 found_match = gfc_match_eos ();
4799 found_match = suffix_match;
4802 if(found_match != MATCH_YES)
4806 /* Make changes to the symbol. */
4809 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
4812 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
4813 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
4816 /* Delay matching the function characteristics until after the
4817 specification block by signalling kind=-1. */
4818 sym->declared_at = old_loc;
4819 if (current_ts.type != BT_UNKNOWN)
4820 current_ts.kind = -1;
4822 current_ts.kind = 0;
4826 if (current_ts.type != BT_UNKNOWN
4827 && gfc_add_type (sym, ¤t_ts, &gfc_current_locus) == FAILURE)
4833 if (current_ts.type != BT_UNKNOWN
4834 && gfc_add_type (result, ¤t_ts, &gfc_current_locus)
4837 sym->result = result;
4840 /* Warn if this procedure has the same name as an intrinsic. */
4841 warn_intrinsic_shadow (sym, true);
4847 gfc_current_locus = old_loc;
4852 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4853 pass the name of the entry, rather than the gfc_current_block name, and
4854 to return false upon finding an existing global entry. */
4857 add_global_entry (const char *name, int sub)
4860 enum gfc_symbol_type type;
4862 s = gfc_get_gsymbol(name);
4863 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
4866 || (s->type != GSYM_UNKNOWN
4867 && s->type != type))
4868 gfc_global_used(s, NULL);
4872 s->where = gfc_current_locus;
4874 s->ns = gfc_current_ns;
4881 /* Match an ENTRY statement. */
4884 gfc_match_entry (void)
4889 char name[GFC_MAX_SYMBOL_LEN + 1];
4890 gfc_compile_state state;
4894 bool module_procedure;
4898 m = gfc_match_name (name);
4902 state = gfc_current_state ();
4903 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
4908 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4911 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4913 case COMP_BLOCK_DATA:
4914 gfc_error ("ENTRY statement at %C cannot appear within "
4917 case COMP_INTERFACE:
4918 gfc_error ("ENTRY statement at %C cannot appear within "
4922 gfc_error ("ENTRY statement at %C cannot appear within "
4923 "a DERIVED TYPE block");
4926 gfc_error ("ENTRY statement at %C cannot appear within "
4927 "an IF-THEN block");
4930 gfc_error ("ENTRY statement at %C cannot appear within "
4934 gfc_error ("ENTRY statement at %C cannot appear within "
4938 gfc_error ("ENTRY statement at %C cannot appear within "
4942 gfc_error ("ENTRY statement at %C cannot appear within "
4946 gfc_error ("ENTRY statement at %C cannot appear within "
4947 "a contained subprogram");
4950 gfc_internal_error ("gfc_match_entry(): Bad state");
4955 module_procedure = gfc_current_ns->parent != NULL
4956 && gfc_current_ns->parent->proc_name
4957 && gfc_current_ns->parent->proc_name->attr.flavor
4960 if (gfc_current_ns->parent != NULL
4961 && gfc_current_ns->parent->proc_name
4962 && !module_procedure)
4964 gfc_error("ENTRY statement at %C cannot appear in a "
4965 "contained procedure");
4969 /* Module function entries need special care in get_proc_name
4970 because previous references within the function will have
4971 created symbols attached to the current namespace. */
4972 if (get_proc_name (name, &entry,
4973 gfc_current_ns->parent != NULL
4974 && module_procedure))
4977 proc = gfc_current_block ();
4979 /* Make sure that it isn't already declared as BIND(C). If it is, it
4980 must have been marked BIND(C) with a BIND(C) attribute and that is
4981 not allowed for procedures. */
4982 if (entry->attr.is_bind_c == 1)
4984 entry->attr.is_bind_c = 0;
4985 if (entry->old_symbol != NULL)
4986 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4987 "variables or common blocks",
4988 &(entry->old_symbol->declared_at));
4990 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4991 "variables or common blocks", &gfc_current_locus);
4994 /* Check what next non-whitespace character is so we can tell if there
4995 is the required parens if we have a BIND(C). */
4996 gfc_gobble_whitespace ();
4997 peek_char = gfc_peek_ascii_char ();
4999 if (state == COMP_SUBROUTINE)
5001 /* An entry in a subroutine. */
5002 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
5005 m = gfc_match_formal_arglist (entry, 0, 1);
5009 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5010 never be an internal procedure. */
5011 is_bind_c = gfc_match_bind_c (entry, true);
5012 if (is_bind_c == MATCH_ERROR)
5014 if (is_bind_c == MATCH_YES)
5016 if (peek_char != '(')
5018 gfc_error ("Missing required parentheses before BIND(C) at %C");
5021 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
5026 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5027 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
5032 /* An entry in a function.
5033 We need to take special care because writing
5038 ENTRY f() RESULT (r)
5040 ENTRY f RESULT (r). */
5041 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
5044 old_loc = gfc_current_locus;
5045 if (gfc_match_eos () == MATCH_YES)
5047 gfc_current_locus = old_loc;
5048 /* Match the empty argument list, and add the interface to
5050 m = gfc_match_formal_arglist (entry, 0, 1);
5053 m = gfc_match_formal_arglist (entry, 0, 0);
5060 if (gfc_match_eos () == MATCH_YES)
5062 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5063 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5066 entry->result = entry;
5070 m = gfc_match_suffix (entry, &result);
5072 gfc_syntax_error (ST_ENTRY);
5078 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
5079 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
5080 || gfc_add_function (&entry->attr, result->name, NULL)
5083 entry->result = result;
5087 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5088 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5090 entry->result = entry;
5095 if (gfc_match_eos () != MATCH_YES)
5097 gfc_syntax_error (ST_ENTRY);
5101 entry->attr.recursive = proc->attr.recursive;
5102 entry->attr.elemental = proc->attr.elemental;
5103 entry->attr.pure = proc->attr.pure;
5105 el = gfc_get_entry_list ();
5107 el->next = gfc_current_ns->entries;
5108 gfc_current_ns->entries = el;
5110 el->id = el->next->id + 1;
5114 new_st.op = EXEC_ENTRY;
5115 new_st.ext.entry = el;
5121 /* Match a subroutine statement, including optional prefixes. */
5124 gfc_match_subroutine (void)
5126 char name[GFC_MAX_SYMBOL_LEN + 1];
5131 bool allow_binding_name;
5133 if (gfc_current_state () != COMP_NONE
5134 && gfc_current_state () != COMP_INTERFACE
5135 && gfc_current_state () != COMP_CONTAINS)
5138 m = gfc_match_prefix (NULL);
5142 m = gfc_match ("subroutine% %n", name);
5146 if (get_proc_name (name, &sym, false))
5149 if (add_hidden_procptr_result (sym) == SUCCESS)
5152 gfc_new_block = sym;
5154 /* Check what next non-whitespace character is so we can tell if there
5155 is the required parens if we have a BIND(C). */
5156 gfc_gobble_whitespace ();
5157 peek_char = gfc_peek_ascii_char ();
5159 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5162 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5165 /* Make sure that it isn't already declared as BIND(C). If it is, it
5166 must have been marked BIND(C) with a BIND(C) attribute and that is
5167 not allowed for procedures. */
5168 if (sym->attr.is_bind_c == 1)
5170 sym->attr.is_bind_c = 0;
5171 if (sym->old_symbol != NULL)
5172 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5173 "variables or common blocks",
5174 &(sym->old_symbol->declared_at));
5176 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5177 "variables or common blocks", &gfc_current_locus);
5180 /* C binding names are not allowed for internal procedures. */
5181 if (gfc_current_state () == COMP_CONTAINS
5182 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5183 allow_binding_name = false;
5185 allow_binding_name = true;
5187 /* Here, we are just checking if it has the bind(c) attribute, and if
5188 so, then we need to make sure it's all correct. If it doesn't,
5189 we still need to continue matching the rest of the subroutine line. */
5190 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5191 if (is_bind_c == MATCH_ERROR)
5193 /* There was an attempt at the bind(c), but it was wrong. An
5194 error message should have been printed w/in the gfc_match_bind_c
5195 so here we'll just return the MATCH_ERROR. */
5199 if (is_bind_c == MATCH_YES)
5201 /* The following is allowed in the Fortran 2008 draft. */
5202 if (gfc_current_state () == COMP_CONTAINS
5203 && sym->ns->proc_name->attr.flavor != FL_MODULE
5204 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5205 "at %L may not be specified for an internal "
5206 "procedure", &gfc_current_locus)
5210 if (peek_char != '(')
5212 gfc_error ("Missing required parentheses before BIND(C) at %C");
5215 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5220 if (gfc_match_eos () != MATCH_YES)
5222 gfc_syntax_error (ST_SUBROUTINE);
5226 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5229 /* Warn if it has the same name as an intrinsic. */
5230 warn_intrinsic_shadow (sym, false);
5236 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5237 given, and set the binding label in either the given symbol (if not
5238 NULL), or in the current_ts. The symbol may be NULL because we may
5239 encounter the BIND(C) before the declaration itself. Return
5240 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5241 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5242 or MATCH_YES if the specifier was correct and the binding label and
5243 bind(c) fields were set correctly for the given symbol or the
5244 current_ts. If allow_binding_name is false, no binding name may be
5248 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5250 /* binding label, if exists */
5251 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
5255 /* Initialize the flag that specifies whether we encountered a NAME=
5256 specifier or not. */
5257 has_name_equals = 0;
5259 /* Init the first char to nil so we can catch if we don't have
5260 the label (name attr) or the symbol name yet. */
5261 binding_label[0] = '\0';
5263 /* This much we have to be able to match, in this order, if
5264 there is a bind(c) label. */
5265 if (gfc_match (" bind ( c ") != MATCH_YES)
5268 /* Now see if there is a binding label, or if we've reached the
5269 end of the bind(c) attribute without one. */
5270 if (gfc_match_char (',') == MATCH_YES)
5272 if (gfc_match (" name = ") != MATCH_YES)
5274 gfc_error ("Syntax error in NAME= specifier for binding label "
5276 /* should give an error message here */
5280 has_name_equals = 1;
5282 /* Get the opening quote. */
5283 double_quote = MATCH_YES;
5284 single_quote = MATCH_YES;
5285 double_quote = gfc_match_char ('"');
5286 if (double_quote != MATCH_YES)
5287 single_quote = gfc_match_char ('\'');
5288 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5290 gfc_error ("Syntax error in NAME= specifier for binding label "
5295 /* Grab the binding label, using functions that will not lower
5296 case the names automatically. */
5297 if (gfc_match_name_C (binding_label) != MATCH_YES)
5300 /* Get the closing quotation. */
5301 if (double_quote == MATCH_YES)
5303 if (gfc_match_char ('"') != MATCH_YES)
5305 gfc_error ("Missing closing quote '\"' for binding label at %C");
5306 /* User started string with '"' so looked to match it. */
5312 if (gfc_match_char ('\'') != MATCH_YES)
5314 gfc_error ("Missing closing quote '\'' for binding label at %C");
5315 /* User started string with "'" char. */
5321 /* Get the required right paren. */
5322 if (gfc_match_char (')') != MATCH_YES)
5324 gfc_error ("Missing closing paren for binding label at %C");
5328 if (has_name_equals && !allow_binding_name)
5330 gfc_error ("No binding name is allowed in BIND(C) at %C");
5334 if (has_name_equals && sym != NULL && sym->attr.dummy)
5336 gfc_error ("For dummy procedure %s, no binding name is "
5337 "allowed in BIND(C) at %C", sym->name);
5342 /* Save the binding label to the symbol. If sym is null, we're
5343 probably matching the typespec attributes of a declaration and
5344 haven't gotten the name yet, and therefore, no symbol yet. */
5345 if (binding_label[0] != '\0')
5349 strcpy (sym->binding_label, binding_label);
5352 strcpy (curr_binding_label, binding_label);
5354 else if (allow_binding_name)
5356 /* No binding label, but if symbol isn't null, we
5357 can set the label for it here.
5358 If name="" or allow_binding_name is false, no C binding name is
5360 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5361 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
5364 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5365 && current_interface.type == INTERFACE_ABSTRACT)
5367 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5375 /* Return nonzero if we're currently compiling a contained procedure. */
5378 contained_procedure (void)
5380 gfc_state_data *s = gfc_state_stack;
5382 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5383 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5389 /* Set the kind of each enumerator. The kind is selected such that it is
5390 interoperable with the corresponding C enumeration type, making
5391 sure that -fshort-enums is honored. */
5396 enumerator_history *current_history = NULL;
5400 if (max_enum == NULL || enum_history == NULL)
5403 if (!flag_short_enums)
5409 kind = gfc_integer_kinds[i++].kind;
5411 while (kind < gfc_c_int_kind
5412 && gfc_check_integer_range (max_enum->initializer->value.integer,
5415 current_history = enum_history;
5416 while (current_history != NULL)
5418 current_history->sym->ts.kind = kind;
5419 current_history = current_history->next;
5424 /* Match any of the various end-block statements. Returns the type of
5425 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5426 and END BLOCK statements cannot be replaced by a single END statement. */
5429 gfc_match_end (gfc_statement *st)
5431 char name[GFC_MAX_SYMBOL_LEN + 1];
5432 gfc_compile_state state;
5434 const char *block_name;
5439 old_loc = gfc_current_locus;
5440 if (gfc_match ("end") != MATCH_YES)
5443 state = gfc_current_state ();
5444 block_name = gfc_current_block () == NULL
5445 ? NULL : gfc_current_block ()->name;
5447 if (state == COMP_BLOCK && !strcmp (block_name, "block@"))
5450 if (state == COMP_CONTAINS || state == COMP_DERIVED_CONTAINS)
5452 state = gfc_state_stack->previous->state;
5453 block_name = gfc_state_stack->previous->sym == NULL
5454 ? NULL : gfc_state_stack->previous->sym->name;
5461 *st = ST_END_PROGRAM;
5462 target = " program";
5466 case COMP_SUBROUTINE:
5467 *st = ST_END_SUBROUTINE;
5468 target = " subroutine";
5469 eos_ok = !contained_procedure ();
5473 *st = ST_END_FUNCTION;
5474 target = " function";
5475 eos_ok = !contained_procedure ();
5478 case COMP_BLOCK_DATA:
5479 *st = ST_END_BLOCK_DATA;
5480 target = " block data";
5485 *st = ST_END_MODULE;
5490 case COMP_INTERFACE:
5491 *st = ST_END_INTERFACE;
5492 target = " interface";
5497 case COMP_DERIVED_CONTAINS:
5522 case COMP_SELECT_TYPE:
5523 *st = ST_END_SELECT;
5529 *st = ST_END_FORALL;
5544 last_initializer = NULL;
5546 gfc_free_enum_history ();
5550 gfc_error ("Unexpected END statement at %C");
5554 if (gfc_match_eos () == MATCH_YES)
5558 /* We would have required END [something]. */
5559 gfc_error ("%s statement expected at %L",
5560 gfc_ascii_statement (*st), &old_loc);
5567 /* Verify that we've got the sort of end-block that we're expecting. */
5568 if (gfc_match (target) != MATCH_YES)
5570 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5574 /* If we're at the end, make sure a block name wasn't required. */
5575 if (gfc_match_eos () == MATCH_YES)
5578 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5579 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK)
5585 gfc_error ("Expected block name of '%s' in %s statement at %C",
5586 block_name, gfc_ascii_statement (*st));
5591 /* END INTERFACE has a special handler for its several possible endings. */
5592 if (*st == ST_END_INTERFACE)
5593 return gfc_match_end_interface ();
5595 /* We haven't hit the end of statement, so what is left must be an
5597 m = gfc_match_space ();
5599 m = gfc_match_name (name);
5602 gfc_error ("Expected terminating name at %C");
5606 if (block_name == NULL)
5609 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
5611 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5612 gfc_ascii_statement (*st));
5615 /* Procedure pointer as function result. */
5616 else if (strcmp (block_name, "ppr@") == 0
5617 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
5619 gfc_error ("Expected label '%s' for %s statement at %C",
5620 gfc_current_block ()->ns->proc_name->name,
5621 gfc_ascii_statement (*st));
5625 if (gfc_match_eos () == MATCH_YES)
5629 gfc_syntax_error (*st);
5632 gfc_current_locus = old_loc;
5638 /***************** Attribute declaration statements ****************/
5640 /* Set the attribute of a single variable. */
5645 char name[GFC_MAX_SYMBOL_LEN + 1];
5653 m = gfc_match_name (name);
5657 if (find_special (name, &sym, false))
5660 var_locus = gfc_current_locus;
5662 /* Deal with possible array specification for certain attributes. */
5663 if (current_attr.dimension
5664 || current_attr.allocatable
5665 || current_attr.pointer
5666 || current_attr.target)
5668 m = gfc_match_array_spec (&as);
5669 if (m == MATCH_ERROR)
5672 if (current_attr.dimension && m == MATCH_NO)
5674 gfc_error ("Missing array specification at %L in DIMENSION "
5675 "statement", &var_locus);
5680 if (current_attr.dimension && sym->value)
5682 gfc_error ("Dimensions specified for %s at %L after its "
5683 "initialisation", sym->name, &var_locus);
5688 if ((current_attr.allocatable || current_attr.pointer)
5689 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5691 gfc_error ("Array specification must be deferred at %L", &var_locus);
5697 /* Update symbol table. DIMENSION attribute is set
5698 in gfc_set_array_spec(). */
5699 if (current_attr.dimension == 0
5700 && gfc_copy_attr (&sym->attr, ¤t_attr, &var_locus) == FAILURE)
5706 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
5712 if (sym->attr.cray_pointee && sym->as != NULL)
5714 /* Fix the array spec. */
5715 m = gfc_mod_pointee_as (sym->as);
5716 if (m == MATCH_ERROR)
5720 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
5726 if ((current_attr.external || current_attr.intrinsic)
5727 && sym->attr.flavor != FL_PROCEDURE
5728 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
5734 add_hidden_procptr_result (sym);
5739 gfc_free_array_spec (as);
5744 /* Generic attribute declaration subroutine. Used for attributes that
5745 just have a list of names. */
5752 /* Gobble the optional double colon, by simply ignoring the result
5762 if (gfc_match_eos () == MATCH_YES)
5768 if (gfc_match_char (',') != MATCH_YES)
5770 gfc_error ("Unexpected character in variable list at %C");
5780 /* This routine matches Cray Pointer declarations of the form:
5781 pointer ( <pointer>, <pointee> )
5783 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5784 The pointer, if already declared, should be an integer. Otherwise, we
5785 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5786 be either a scalar, or an array declaration. No space is allocated for
5787 the pointee. For the statement
5788 pointer (ipt, ar(10))
5789 any subsequent uses of ar will be translated (in C-notation) as
5790 ar(i) => ((<type> *) ipt)(i)
5791 After gimplification, pointee variable will disappear in the code. */
5794 cray_pointer_decl (void)
5798 gfc_symbol *cptr; /* Pointer symbol. */
5799 gfc_symbol *cpte; /* Pointee symbol. */
5805 if (gfc_match_char ('(') != MATCH_YES)
5807 gfc_error ("Expected '(' at %C");
5811 /* Match pointer. */
5812 var_locus = gfc_current_locus;
5813 gfc_clear_attr (¤t_attr);
5814 gfc_add_cray_pointer (¤t_attr, &var_locus);
5815 current_ts.type = BT_INTEGER;
5816 current_ts.kind = gfc_index_integer_kind;
5818 m = gfc_match_symbol (&cptr, 0);
5821 gfc_error ("Expected variable name at %C");
5825 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
5828 gfc_set_sym_referenced (cptr);
5830 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
5832 cptr->ts.type = BT_INTEGER;
5833 cptr->ts.kind = gfc_index_integer_kind;
5835 else if (cptr->ts.type != BT_INTEGER)
5837 gfc_error ("Cray pointer at %C must be an integer");
5840 else if (cptr->ts.kind < gfc_index_integer_kind)
5841 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5842 " memory addresses require %d bytes",
5843 cptr->ts.kind, gfc_index_integer_kind);
5845 if (gfc_match_char (',') != MATCH_YES)
5847 gfc_error ("Expected \",\" at %C");
5851 /* Match Pointee. */
5852 var_locus = gfc_current_locus;
5853 gfc_clear_attr (¤t_attr);
5854 gfc_add_cray_pointee (¤t_attr, &var_locus);
5855 current_ts.type = BT_UNKNOWN;
5856 current_ts.kind = 0;
5858 m = gfc_match_symbol (&cpte, 0);
5861 gfc_error ("Expected variable name at %C");
5865 /* Check for an optional array spec. */
5866 m = gfc_match_array_spec (&as);
5867 if (m == MATCH_ERROR)
5869 gfc_free_array_spec (as);
5872 else if (m == MATCH_NO)
5874 gfc_free_array_spec (as);
5878 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
5881 gfc_set_sym_referenced (cpte);
5883 if (cpte->as == NULL)
5885 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
5886 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5888 else if (as != NULL)
5890 gfc_error ("Duplicate array spec for Cray pointee at %C");
5891 gfc_free_array_spec (as);
5897 if (cpte->as != NULL)
5899 /* Fix array spec. */
5900 m = gfc_mod_pointee_as (cpte->as);
5901 if (m == MATCH_ERROR)
5905 /* Point the Pointee at the Pointer. */
5906 cpte->cp_pointer = cptr;
5908 if (gfc_match_char (')') != MATCH_YES)
5910 gfc_error ("Expected \")\" at %C");
5913 m = gfc_match_char (',');
5915 done = true; /* Stop searching for more declarations. */
5919 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
5920 || gfc_match_eos () != MATCH_YES)
5922 gfc_error ("Expected \",\" or end of statement at %C");
5930 gfc_match_external (void)
5933 gfc_clear_attr (¤t_attr);
5934 current_attr.external = 1;
5936 return attr_decl ();
5941 gfc_match_intent (void)
5945 /* This is not allowed within a BLOCK construct! */
5946 if (gfc_current_state () == COMP_BLOCK)
5948 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
5952 intent = match_intent_spec ();
5953 if (intent == INTENT_UNKNOWN)
5956 gfc_clear_attr (¤t_attr);
5957 current_attr.intent = intent;
5959 return attr_decl ();
5964 gfc_match_intrinsic (void)
5967 gfc_clear_attr (¤t_attr);
5968 current_attr.intrinsic = 1;
5970 return attr_decl ();
5975 gfc_match_optional (void)
5977 /* This is not allowed within a BLOCK construct! */
5978 if (gfc_current_state () == COMP_BLOCK)
5980 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
5984 gfc_clear_attr (¤t_attr);
5985 current_attr.optional = 1;
5987 return attr_decl ();
5992 gfc_match_pointer (void)
5994 gfc_gobble_whitespace ();
5995 if (gfc_peek_ascii_char () == '(')
5997 if (!gfc_option.flag_cray_pointer)
5999 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6003 return cray_pointer_decl ();
6007 gfc_clear_attr (¤t_attr);
6008 current_attr.pointer = 1;
6010 return attr_decl ();
6016 gfc_match_allocatable (void)
6018 gfc_clear_attr (¤t_attr);
6019 current_attr.allocatable = 1;
6021 return attr_decl ();
6026 gfc_match_dimension (void)
6028 gfc_clear_attr (¤t_attr);
6029 current_attr.dimension = 1;
6031 return attr_decl ();
6036 gfc_match_target (void)
6038 gfc_clear_attr (¤t_attr);
6039 current_attr.target = 1;
6041 return attr_decl ();
6045 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6049 access_attr_decl (gfc_statement st)
6051 char name[GFC_MAX_SYMBOL_LEN + 1];
6052 interface_type type;
6055 gfc_intrinsic_op op;
6058 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6063 m = gfc_match_generic_spec (&type, name, &op);
6066 if (m == MATCH_ERROR)
6071 case INTERFACE_NAMELESS:
6072 case INTERFACE_ABSTRACT:
6075 case INTERFACE_GENERIC:
6076 if (gfc_get_symbol (name, NULL, &sym))
6079 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
6080 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6081 sym->name, NULL) == FAILURE)
6086 case INTERFACE_INTRINSIC_OP:
6087 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6089 gfc_current_ns->operator_access[op] =
6090 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6094 gfc_error ("Access specification of the %s operator at %C has "
6095 "already been specified", gfc_op2string (op));
6101 case INTERFACE_USER_OP:
6102 uop = gfc_get_uop (name);
6104 if (uop->access == ACCESS_UNKNOWN)
6106 uop->access = (st == ST_PUBLIC)
6107 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6111 gfc_error ("Access specification of the .%s. operator at %C "
6112 "has already been specified", sym->name);
6119 if (gfc_match_char (',') == MATCH_NO)
6123 if (gfc_match_eos () != MATCH_YES)
6128 gfc_syntax_error (st);
6136 gfc_match_protected (void)
6141 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6143 gfc_error ("PROTECTED at %C only allowed in specification "
6144 "part of a module");
6149 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
6153 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6158 if (gfc_match_eos () == MATCH_YES)
6163 m = gfc_match_symbol (&sym, 0);
6167 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6180 if (gfc_match_eos () == MATCH_YES)
6182 if (gfc_match_char (',') != MATCH_YES)
6189 gfc_error ("Syntax error in PROTECTED statement at %C");
6194 /* The PRIVATE statement is a bit weird in that it can be an attribute
6195 declaration, but also works as a standalone statement inside of a
6196 type declaration or a module. */
6199 gfc_match_private (gfc_statement *st)
6202 if (gfc_match ("private") != MATCH_YES)
6205 if (gfc_current_state () != COMP_MODULE
6206 && !(gfc_current_state () == COMP_DERIVED
6207 && gfc_state_stack->previous
6208 && gfc_state_stack->previous->state == COMP_MODULE)
6209 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6210 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6211 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6213 gfc_error ("PRIVATE statement at %C is only allowed in the "
6214 "specification part of a module");
6218 if (gfc_current_state () == COMP_DERIVED)
6220 if (gfc_match_eos () == MATCH_YES)
6226 gfc_syntax_error (ST_PRIVATE);
6230 if (gfc_match_eos () == MATCH_YES)
6237 return access_attr_decl (ST_PRIVATE);
6242 gfc_match_public (gfc_statement *st)
6245 if (gfc_match ("public") != MATCH_YES)
6248 if (gfc_current_state () != COMP_MODULE)
6250 gfc_error ("PUBLIC statement at %C is only allowed in the "
6251 "specification part of a module");
6255 if (gfc_match_eos () == MATCH_YES)
6262 return access_attr_decl (ST_PUBLIC);
6266 /* Workhorse for gfc_match_parameter. */
6276 m = gfc_match_symbol (&sym, 0);
6278 gfc_error ("Expected variable name at %C in PARAMETER statement");
6283 if (gfc_match_char ('=') == MATCH_NO)
6285 gfc_error ("Expected = sign in PARAMETER statement at %C");
6289 m = gfc_match_init_expr (&init);
6291 gfc_error ("Expected expression at %C in PARAMETER statement");
6295 if (sym->ts.type == BT_UNKNOWN
6296 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6302 if (gfc_check_assign_symbol (sym, init) == FAILURE
6303 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6311 gfc_error ("Initializing already initialized variable at %C");
6316 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
6317 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6320 gfc_free_expr (init);
6325 /* Match a parameter statement, with the weird syntax that these have. */
6328 gfc_match_parameter (void)
6332 if (gfc_match_char ('(') == MATCH_NO)
6341 if (gfc_match (" )%t") == MATCH_YES)
6344 if (gfc_match_char (',') != MATCH_YES)
6346 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6356 /* Save statements have a special syntax. */
6359 gfc_match_save (void)
6361 char n[GFC_MAX_SYMBOL_LEN+1];
6366 if (gfc_match_eos () == MATCH_YES)
6368 if (gfc_current_ns->seen_save)
6370 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6371 "follows previous SAVE statement")
6376 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6380 if (gfc_current_ns->save_all)
6382 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6383 "blanket SAVE statement")
6392 m = gfc_match_symbol (&sym, 0);
6396 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
6408 m = gfc_match (" / %n /", &n);
6409 if (m == MATCH_ERROR)
6414 c = gfc_get_common (n, 0);
6417 gfc_current_ns->seen_save = 1;
6420 if (gfc_match_eos () == MATCH_YES)
6422 if (gfc_match_char (',') != MATCH_YES)
6429 gfc_error ("Syntax error in SAVE statement at %C");
6435 gfc_match_value (void)
6440 /* This is not allowed within a BLOCK construct! */
6441 if (gfc_current_state () == COMP_BLOCK)
6443 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
6447 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
6451 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6456 if (gfc_match_eos () == MATCH_YES)
6461 m = gfc_match_symbol (&sym, 0);
6465 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
6478 if (gfc_match_eos () == MATCH_YES)
6480 if (gfc_match_char (',') != MATCH_YES)
6487 gfc_error ("Syntax error in VALUE statement at %C");
6493 gfc_match_volatile (void)
6498 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
6502 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6507 if (gfc_match_eos () == MATCH_YES)
6512 /* VOLATILE is special because it can be added to host-associated
6514 m = gfc_match_symbol (&sym, 1);
6518 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6531 if (gfc_match_eos () == MATCH_YES)
6533 if (gfc_match_char (',') != MATCH_YES)
6540 gfc_error ("Syntax error in VOLATILE statement at %C");
6545 /* Match a module procedure statement. Note that we have to modify
6546 symbols in the parent's namespace because the current one was there
6547 to receive symbols that are in an interface's formal argument list. */
6550 gfc_match_modproc (void)
6552 char name[GFC_MAX_SYMBOL_LEN + 1];
6555 gfc_namespace *module_ns;
6556 gfc_interface *old_interface_head, *interface;
6558 if (gfc_state_stack->state != COMP_INTERFACE
6559 || gfc_state_stack->previous == NULL
6560 || current_interface.type == INTERFACE_NAMELESS
6561 || current_interface.type == INTERFACE_ABSTRACT)
6563 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6568 module_ns = gfc_current_ns->parent;
6569 for (; module_ns; module_ns = module_ns->parent)
6570 if (module_ns->proc_name->attr.flavor == FL_MODULE
6571 || module_ns->proc_name->attr.flavor == FL_PROGRAM
6572 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
6573 && !module_ns->proc_name->attr.contained))
6576 if (module_ns == NULL)
6579 /* Store the current state of the interface. We will need it if we
6580 end up with a syntax error and need to recover. */
6581 old_interface_head = gfc_current_interface_head ();
6585 locus old_locus = gfc_current_locus;
6588 m = gfc_match_name (name);
6594 /* Check for syntax error before starting to add symbols to the
6595 current namespace. */
6596 if (gfc_match_eos () == MATCH_YES)
6598 if (!last && gfc_match_char (',') != MATCH_YES)
6601 /* Now we're sure the syntax is valid, we process this item
6603 if (gfc_get_symbol (name, module_ns, &sym))
6606 if (sym->attr.intrinsic)
6608 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
6609 "PROCEDURE", &old_locus);
6613 if (sym->attr.proc != PROC_MODULE
6614 && gfc_add_procedure (&sym->attr, PROC_MODULE,
6615 sym->name, NULL) == FAILURE)
6618 if (gfc_add_interface (sym) == FAILURE)
6621 sym->attr.mod_proc = 1;
6622 sym->declared_at = old_locus;
6631 /* Restore the previous state of the interface. */
6632 interface = gfc_current_interface_head ();
6633 gfc_set_current_interface_head (old_interface_head);
6635 /* Free the new interfaces. */
6636 while (interface != old_interface_head)
6638 gfc_interface *i = interface->next;
6639 gfc_free (interface);
6643 /* And issue a syntax error. */
6644 gfc_syntax_error (ST_MODULE_PROC);
6649 /* Check a derived type that is being extended. */
6651 check_extended_derived_type (char *name)
6653 gfc_symbol *extended;
6655 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
6657 gfc_error ("Ambiguous symbol in TYPE definition at %C");
6663 gfc_error ("No such symbol in TYPE definition at %C");
6667 if (extended->attr.flavor != FL_DERIVED)
6669 gfc_error ("'%s' in EXTENDS expression at %C is not a "
6670 "derived type", name);
6674 if (extended->attr.is_bind_c)
6676 gfc_error ("'%s' cannot be extended at %C because it "
6677 "is BIND(C)", extended->name);
6681 if (extended->attr.sequence)
6683 gfc_error ("'%s' cannot be extended at %C because it "
6684 "is a SEQUENCE type", extended->name);
6692 /* Match the optional attribute specifiers for a type declaration.
6693 Return MATCH_ERROR if an error is encountered in one of the handled
6694 attributes (public, private, bind(c)), MATCH_NO if what's found is
6695 not a handled attribute, and MATCH_YES otherwise. TODO: More error
6696 checking on attribute conflicts needs to be done. */
6699 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
6701 /* See if the derived type is marked as private. */
6702 if (gfc_match (" , private") == MATCH_YES)
6704 if (gfc_current_state () != COMP_MODULE)
6706 gfc_error ("Derived type at %C can only be PRIVATE in the "
6707 "specification part of a module");
6711 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
6714 else if (gfc_match (" , public") == MATCH_YES)
6716 if (gfc_current_state () != COMP_MODULE)
6718 gfc_error ("Derived type at %C can only be PUBLIC in the "
6719 "specification part of a module");
6723 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
6726 else if (gfc_match (" , bind ( c )") == MATCH_YES)
6728 /* If the type is defined to be bind(c) it then needs to make
6729 sure that all fields are interoperable. This will
6730 need to be a semantic check on the finished derived type.
6731 See 15.2.3 (lines 9-12) of F2003 draft. */
6732 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
6735 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
6737 else if (gfc_match (" , abstract") == MATCH_YES)
6739 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
6743 if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
6746 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
6748 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
6754 /* If we get here, something matched. */
6759 /* Assign a hash value for a derived type. The algorithm is that of
6760 SDBM. The hashed string is '[module_name #] derived_name'. */
6762 hash_value (gfc_symbol *sym)
6764 unsigned int hash = 0;
6768 /* Hash of the module or procedure name. */
6769 if (sym->module != NULL)
6771 else if (sym->ns && sym->ns->proc_name
6772 && sym->ns->proc_name->attr.flavor == FL_MODULE)
6773 c = sym->ns->proc_name->name;
6780 for (i = 0; i < len; i++, c++)
6781 hash = (hash << 6) + (hash << 16) - hash + (*c);
6783 /* Disambiguate between 'a' in 'aa' and 'aa' in 'a'. */
6784 hash = (hash << 6) + (hash << 16) - hash + '#';
6787 /* Hash of the derived type name. */
6788 len = strlen (sym->name);
6790 for (i = 0; i < len; i++, c++)
6791 hash = (hash << 6) + (hash << 16) - hash + (*c);
6793 /* Return the hash but take the modulus for the sake of module read,
6794 even though this slightly increases the chance of collision. */
6795 return (hash % 100000000);
6799 /* Match the beginning of a derived type declaration. If a type name
6800 was the result of a function, then it is possible to have a symbol
6801 already to be known as a derived type yet have no components. */
6804 gfc_match_derived_decl (void)
6806 char name[GFC_MAX_SYMBOL_LEN + 1];
6807 char parent[GFC_MAX_SYMBOL_LEN + 1];
6808 symbol_attribute attr;
6810 gfc_symbol *extended;
6812 match is_type_attr_spec = MATCH_NO;
6813 bool seen_attr = false;
6815 if (gfc_current_state () == COMP_DERIVED)
6820 gfc_clear_attr (&attr);
6825 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
6826 if (is_type_attr_spec == MATCH_ERROR)
6828 if (is_type_attr_spec == MATCH_YES)
6830 } while (is_type_attr_spec == MATCH_YES);
6832 /* Deal with derived type extensions. The extension attribute has
6833 been added to 'attr' but now the parent type must be found and
6836 extended = check_extended_derived_type (parent);
6838 if (parent[0] && !extended)
6841 if (gfc_match (" ::") != MATCH_YES && seen_attr)
6843 gfc_error ("Expected :: in TYPE definition at %C");
6847 m = gfc_match (" %n%t", name);
6851 /* Make sure the name is not the name of an intrinsic type. */
6852 if (gfc_is_intrinsic_typename (name))
6854 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
6859 if (gfc_get_symbol (name, NULL, &sym))
6862 if (sym->ts.type != BT_UNKNOWN)
6864 gfc_error ("Derived type name '%s' at %C already has a basic type "
6865 "of %s", sym->name, gfc_typename (&sym->ts));
6869 /* The symbol may already have the derived attribute without the
6870 components. The ways this can happen is via a function
6871 definition, an INTRINSIC statement or a subtype in another
6872 derived type that is a pointer. The first part of the AND clause
6873 is true if the symbol is not the return value of a function. */
6874 if (sym->attr.flavor != FL_DERIVED
6875 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
6878 if (sym->components != NULL || sym->attr.zero_comp)
6880 gfc_error ("Derived type definition of '%s' at %C has already been "
6881 "defined", sym->name);
6885 if (attr.access != ACCESS_UNKNOWN
6886 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
6889 /* See if the derived type was labeled as bind(c). */
6890 if (attr.is_bind_c != 0)
6891 sym->attr.is_bind_c = attr.is_bind_c;
6893 /* Construct the f2k_derived namespace if it is not yet there. */
6894 if (!sym->f2k_derived)
6895 sym->f2k_derived = gfc_get_namespace (NULL, 0);
6897 if (extended && !sym->components)
6902 /* Add the extended derived type as the first component. */
6903 gfc_add_component (sym, parent, &p);
6904 sym->attr.extension = attr.extension;
6906 gfc_set_sym_referenced (extended);
6908 p->ts.type = BT_DERIVED;
6909 p->ts.u.derived = extended;
6910 p->initializer = gfc_default_initializer (&p->ts);
6912 /* Provide the links between the extended type and its extension. */
6913 if (!extended->f2k_derived)
6914 extended->f2k_derived = gfc_get_namespace (NULL, 0);
6915 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
6920 /* Set the vindex for this type. */
6921 sym->vindex = hash_value (sym);
6923 /* Take over the ABSTRACT attribute. */
6924 sym->attr.abstract = attr.abstract;
6926 gfc_new_block = sym;
6932 /* Cray Pointees can be declared as:
6933 pointer (ipt, a (n,m,...,*))
6934 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6935 cheat and set a constant bound of 1 for the last dimension, if this
6936 is the case. Since there is no bounds-checking for Cray Pointees,
6937 this will be okay. */
6940 gfc_mod_pointee_as (gfc_array_spec *as)
6942 as->cray_pointee = true; /* This will be useful to know later. */
6943 if (as->type == AS_ASSUMED_SIZE)
6945 as->type = AS_EXPLICIT;
6946 as->upper[as->rank - 1] = gfc_int_expr (1);
6947 as->cp_was_assumed = true;
6949 else if (as->type == AS_ASSUMED_SHAPE)
6951 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6958 /* Match the enum definition statement, here we are trying to match
6959 the first line of enum definition statement.
6960 Returns MATCH_YES if match is found. */
6963 gfc_match_enum (void)
6967 m = gfc_match_eos ();
6971 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
6979 /* Returns an initializer whose value is one higher than the value of the
6980 LAST_INITIALIZER argument. If the argument is NULL, the
6981 initializers value will be set to zero. The initializer's kind
6982 will be set to gfc_c_int_kind.
6984 If -fshort-enums is given, the appropriate kind will be selected
6985 later after all enumerators have been parsed. A warning is issued
6986 here if an initializer exceeds gfc_c_int_kind. */
6989 enum_initializer (gfc_expr *last_initializer, locus where)
6993 result = gfc_get_expr ();
6994 result->expr_type = EXPR_CONSTANT;
6995 result->ts.type = BT_INTEGER;
6996 result->ts.kind = gfc_c_int_kind;
6997 result->where = where;
6999 mpz_init (result->value.integer);
7001 if (last_initializer != NULL)
7003 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7004 result->where = last_initializer->where;
7006 if (gfc_check_integer_range (result->value.integer,
7007 gfc_c_int_kind) != ARITH_OK)
7009 gfc_error ("Enumerator exceeds the C integer type at %C");
7015 /* Control comes here, if it's the very first enumerator and no
7016 initializer has been given. It will be initialized to zero. */
7017 mpz_set_si (result->value.integer, 0);
7024 /* Match a variable name with an optional initializer. When this
7025 subroutine is called, a variable is expected to be parsed next.
7026 Depending on what is happening at the moment, updates either the
7027 symbol table or the current interface. */
7030 enumerator_decl (void)
7032 char name[GFC_MAX_SYMBOL_LEN + 1];
7033 gfc_expr *initializer;
7034 gfc_array_spec *as = NULL;
7042 old_locus = gfc_current_locus;
7044 /* When we get here, we've just matched a list of attributes and
7045 maybe a type and a double colon. The next thing we expect to see
7046 is the name of the symbol. */
7047 m = gfc_match_name (name);
7051 var_locus = gfc_current_locus;
7053 /* OK, we've successfully matched the declaration. Now put the
7054 symbol in the current namespace. If we fail to create the symbol,
7056 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
7062 /* The double colon must be present in order to have initializers.
7063 Otherwise the statement is ambiguous with an assignment statement. */
7066 if (gfc_match_char ('=') == MATCH_YES)
7068 m = gfc_match_init_expr (&initializer);
7071 gfc_error ("Expected an initialization expression at %C");
7080 /* If we do not have an initializer, the initialization value of the
7081 previous enumerator (stored in last_initializer) is incremented
7082 by 1 and is used to initialize the current enumerator. */
7083 if (initializer == NULL)
7084 initializer = enum_initializer (last_initializer, old_locus);
7086 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7088 gfc_error("ENUMERATOR %L not initialized with integer expression",
7091 gfc_free_enum_history ();
7095 /* Store this current initializer, for the next enumerator variable
7096 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7097 use last_initializer below. */
7098 last_initializer = initializer;
7099 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7101 /* Maintain enumerator history. */
7102 gfc_find_symbol (name, NULL, 0, &sym);
7103 create_enum_history (sym, last_initializer);
7105 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
7108 /* Free stuff up and return. */
7109 gfc_free_expr (initializer);
7115 /* Match the enumerator definition statement. */
7118 gfc_match_enumerator_def (void)
7123 gfc_clear_ts (¤t_ts);
7125 m = gfc_match (" enumerator");
7129 m = gfc_match (" :: ");
7130 if (m == MATCH_ERROR)
7133 colon_seen = (m == MATCH_YES);
7135 if (gfc_current_state () != COMP_ENUM)
7137 gfc_error ("ENUM definition statement expected before %C");
7138 gfc_free_enum_history ();
7142 (¤t_ts)->type = BT_INTEGER;
7143 (¤t_ts)->kind = gfc_c_int_kind;
7145 gfc_clear_attr (¤t_attr);
7146 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
7155 m = enumerator_decl ();
7156 if (m == MATCH_ERROR)
7161 if (gfc_match_eos () == MATCH_YES)
7163 if (gfc_match_char (',') != MATCH_YES)
7167 if (gfc_current_state () == COMP_ENUM)
7169 gfc_free_enum_history ();
7170 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7175 gfc_free_array_spec (current_as);
7182 /* Match binding attributes. */
7185 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7187 bool found_passing = false;
7188 bool seen_ptr = false;
7189 match m = MATCH_YES;
7191 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7192 this case the defaults are in there. */
7193 ba->access = ACCESS_UNKNOWN;
7194 ba->pass_arg = NULL;
7195 ba->pass_arg_num = 0;
7197 ba->non_overridable = 0;
7201 /* If we find a comma, we believe there are binding attributes. */
7202 m = gfc_match_char (',');
7208 /* Access specifier. */
7210 m = gfc_match (" public");
7211 if (m == MATCH_ERROR)
7215 if (ba->access != ACCESS_UNKNOWN)
7217 gfc_error ("Duplicate access-specifier at %C");
7221 ba->access = ACCESS_PUBLIC;
7225 m = gfc_match (" private");
7226 if (m == MATCH_ERROR)
7230 if (ba->access != ACCESS_UNKNOWN)
7232 gfc_error ("Duplicate access-specifier at %C");
7236 ba->access = ACCESS_PRIVATE;
7240 /* If inside GENERIC, the following is not allowed. */
7245 m = gfc_match (" nopass");
7246 if (m == MATCH_ERROR)
7252 gfc_error ("Binding attributes already specify passing,"
7253 " illegal NOPASS at %C");
7257 found_passing = true;
7262 /* PASS possibly including argument. */
7263 m = gfc_match (" pass");
7264 if (m == MATCH_ERROR)
7268 char arg[GFC_MAX_SYMBOL_LEN + 1];
7272 gfc_error ("Binding attributes already specify passing,"
7273 " illegal PASS at %C");
7277 m = gfc_match (" ( %n )", arg);
7278 if (m == MATCH_ERROR)
7281 ba->pass_arg = gfc_get_string (arg);
7282 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7284 found_passing = true;
7292 m = gfc_match (" pointer");
7293 if (m == MATCH_ERROR)
7299 gfc_error ("Duplicate POINTER attribute at %C");
7309 /* NON_OVERRIDABLE flag. */
7310 m = gfc_match (" non_overridable");
7311 if (m == MATCH_ERROR)
7315 if (ba->non_overridable)
7317 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7321 ba->non_overridable = 1;
7325 /* DEFERRED flag. */
7326 m = gfc_match (" deferred");
7327 if (m == MATCH_ERROR)
7333 gfc_error ("Duplicate DEFERRED at %C");
7344 /* Nothing matching found. */
7346 gfc_error ("Expected access-specifier at %C");
7348 gfc_error ("Expected binding attribute at %C");
7351 while (gfc_match_char (',') == MATCH_YES);
7353 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
7354 if (ba->non_overridable && ba->deferred)
7356 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7363 if (ba->access == ACCESS_UNKNOWN)
7364 ba->access = gfc_typebound_default_access;
7366 if (ppc && !seen_ptr)
7368 gfc_error ("POINTER attribute is required for procedure pointer component"
7380 /* Match a PROCEDURE specific binding inside a derived type. */
7383 match_procedure_in_type (void)
7385 char name[GFC_MAX_SYMBOL_LEN + 1];
7386 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
7387 char* target = NULL;
7388 gfc_typebound_proc* tb;
7396 /* Check current state. */
7397 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
7398 block = gfc_state_stack->previous->sym;
7401 /* Try to match PROCEDURE(interface). */
7402 if (gfc_match (" (") == MATCH_YES)
7404 m = gfc_match_name (target_buf);
7405 if (m == MATCH_ERROR)
7409 gfc_error ("Interface-name expected after '(' at %C");
7413 if (gfc_match (" )") != MATCH_YES)
7415 gfc_error ("')' expected at %C");
7419 target = target_buf;
7422 /* Construct the data structure. */
7423 tb = gfc_get_typebound_proc ();
7424 tb->where = gfc_current_locus;
7427 /* Match binding attributes. */
7428 m = match_binding_attributes (tb, false, false);
7429 if (m == MATCH_ERROR)
7431 seen_attrs = (m == MATCH_YES);
7433 /* Check that attribute DEFERRED is given iff an interface is specified, which
7434 means target != NULL. */
7435 if (tb->deferred && !target)
7437 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7440 if (target && !tb->deferred)
7442 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7446 /* Match the colons. */
7447 m = gfc_match (" ::");
7448 if (m == MATCH_ERROR)
7450 seen_colons = (m == MATCH_YES);
7451 if (seen_attrs && !seen_colons)
7453 gfc_error ("Expected '::' after binding-attributes at %C");
7457 /* Match the binding name. */
7458 m = gfc_match_name (name);
7459 if (m == MATCH_ERROR)
7463 gfc_error ("Expected binding name at %C");
7467 /* Try to match the '=> target', if it's there. */
7468 m = gfc_match (" =>");
7469 if (m == MATCH_ERROR)
7475 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7481 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7486 m = gfc_match_name (target_buf);
7487 if (m == MATCH_ERROR)
7491 gfc_error ("Expected binding target after '=>' at %C");
7494 target = target_buf;
7497 /* Now we should have the end. */
7498 m = gfc_match_eos ();
7499 if (m == MATCH_ERROR)
7503 gfc_error ("Junk after PROCEDURE declaration at %C");
7507 /* If no target was found, it has the same name as the binding. */
7511 /* Get the namespace to insert the symbols into. */
7512 ns = block->f2k_derived;
7515 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7516 if (tb->deferred && !block->attr.abstract)
7518 gfc_error ("Type '%s' containing DEFERRED binding at %C is not ABSTRACT",
7523 /* See if we already have a binding with this name in the symtree which would
7524 be an error. If a GENERIC already targetted this binding, it may be
7525 already there but then typebound is still NULL. */
7526 stree = gfc_find_symtree (ns->tb_sym_root, name);
7527 if (stree && stree->n.tb)
7529 gfc_error ("There's already a procedure with binding name '%s' for the"
7530 " derived type '%s' at %C", name, block->name);
7534 /* Insert it and set attributes. */
7538 stree = gfc_new_symtree (&ns->tb_sym_root, name);
7543 if (gfc_get_sym_tree (target, gfc_current_ns, &tb->u.specific, false))
7545 gfc_set_sym_referenced (tb->u.specific->n.sym);
7551 /* Match a GENERIC procedure binding inside a derived type. */
7554 gfc_match_generic (void)
7556 char name[GFC_MAX_SYMBOL_LEN + 1];
7557 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
7559 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
7560 gfc_typebound_proc* tb;
7562 interface_type op_type;
7563 gfc_intrinsic_op op;
7566 /* Check current state. */
7567 if (gfc_current_state () == COMP_DERIVED)
7569 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
7572 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
7574 block = gfc_state_stack->previous->sym;
7575 ns = block->f2k_derived;
7576 gcc_assert (block && ns);
7578 /* See if we get an access-specifier. */
7579 m = match_binding_attributes (&tbattr, true, false);
7580 if (m == MATCH_ERROR)
7583 /* Now the colons, those are required. */
7584 if (gfc_match (" ::") != MATCH_YES)
7586 gfc_error ("Expected '::' at %C");
7590 /* Match the binding name; depending on type (operator / generic) format
7591 it for future error messages into bind_name. */
7593 m = gfc_match_generic_spec (&op_type, name, &op);
7594 if (m == MATCH_ERROR)
7598 gfc_error ("Expected generic name or operator descriptor at %C");
7604 case INTERFACE_GENERIC:
7605 snprintf (bind_name, sizeof (bind_name), "%s", name);
7608 case INTERFACE_USER_OP:
7609 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
7612 case INTERFACE_INTRINSIC_OP:
7613 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
7614 gfc_op2string (op));
7621 /* Match the required =>. */
7622 if (gfc_match (" =>") != MATCH_YES)
7624 gfc_error ("Expected '=>' at %C");
7628 /* Try to find existing GENERIC binding with this name / for this operator;
7629 if there is something, check that it is another GENERIC and then extend
7630 it rather than building a new node. Otherwise, create it and put it
7631 at the right position. */
7635 case INTERFACE_USER_OP:
7636 case INTERFACE_GENERIC:
7638 const bool is_op = (op_type == INTERFACE_USER_OP);
7641 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
7653 case INTERFACE_INTRINSIC_OP:
7663 if (!tb->is_generic)
7665 gcc_assert (op_type == INTERFACE_GENERIC);
7666 gfc_error ("There's already a non-generic procedure with binding name"
7667 " '%s' for the derived type '%s' at %C",
7668 bind_name, block->name);
7672 if (tb->access != tbattr.access)
7674 gfc_error ("Binding at %C must have the same access as already"
7675 " defined binding '%s'", bind_name);
7681 tb = gfc_get_typebound_proc ();
7682 tb->where = gfc_current_locus;
7683 tb->access = tbattr.access;
7685 tb->u.generic = NULL;
7689 case INTERFACE_GENERIC:
7690 case INTERFACE_USER_OP:
7692 const bool is_op = (op_type == INTERFACE_USER_OP);
7695 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
7703 case INTERFACE_INTRINSIC_OP:
7712 /* Now, match all following names as specific targets. */
7715 gfc_symtree* target_st;
7716 gfc_tbp_generic* target;
7718 m = gfc_match_name (name);
7719 if (m == MATCH_ERROR)
7723 gfc_error ("Expected specific binding name at %C");
7727 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
7729 /* See if this is a duplicate specification. */
7730 for (target = tb->u.generic; target; target = target->next)
7731 if (target_st == target->specific_st)
7733 gfc_error ("'%s' already defined as specific binding for the"
7734 " generic '%s' at %C", name, bind_name);
7738 target = gfc_get_tbp_generic ();
7739 target->specific_st = target_st;
7740 target->specific = NULL;
7741 target->next = tb->u.generic;
7742 tb->u.generic = target;
7744 while (gfc_match (" ,") == MATCH_YES);
7746 /* Here should be the end. */
7747 if (gfc_match_eos () != MATCH_YES)
7749 gfc_error ("Junk after GENERIC binding at %C");
7760 /* Match a FINAL declaration inside a derived type. */
7763 gfc_match_final_decl (void)
7765 char name[GFC_MAX_SYMBOL_LEN + 1];
7768 gfc_namespace* module_ns;
7772 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
7774 gfc_error ("FINAL declaration at %C must be inside a derived type "
7775 "CONTAINS section");
7779 block = gfc_state_stack->previous->sym;
7782 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
7783 || gfc_state_stack->previous->previous->state != COMP_MODULE)
7785 gfc_error ("Derived type declaration with FINAL at %C must be in the"
7786 " specification part of a MODULE");
7790 module_ns = gfc_current_ns;
7791 gcc_assert (module_ns);
7792 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
7794 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
7795 if (gfc_match (" ::") == MATCH_ERROR)
7798 /* Match the sequence of procedure names. */
7805 if (first && gfc_match_eos () == MATCH_YES)
7807 gfc_error ("Empty FINAL at %C");
7811 m = gfc_match_name (name);
7814 gfc_error ("Expected module procedure name at %C");
7817 else if (m != MATCH_YES)
7820 if (gfc_match_eos () == MATCH_YES)
7822 if (!last && gfc_match_char (',') != MATCH_YES)
7824 gfc_error ("Expected ',' at %C");
7828 if (gfc_get_symbol (name, module_ns, &sym))
7830 gfc_error ("Unknown procedure name \"%s\" at %C", name);
7834 /* Mark the symbol as module procedure. */
7835 if (sym->attr.proc != PROC_MODULE
7836 && gfc_add_procedure (&sym->attr, PROC_MODULE,
7837 sym->name, NULL) == FAILURE)
7840 /* Check if we already have this symbol in the list, this is an error. */
7841 for (f = block->f2k_derived->finalizers; f; f = f->next)
7842 if (f->proc_sym == sym)
7844 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
7849 /* Add this symbol to the list of finalizers. */
7850 gcc_assert (block->f2k_derived);
7852 f = XCNEW (gfc_finalizer);
7854 f->proc_tree = NULL;
7855 f->where = gfc_current_locus;
7856 f->next = block->f2k_derived->finalizers;
7857 block->f2k_derived->finalizers = f;
7867 const ext_attr_t ext_attr_list[] = {
7868 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
7869 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
7870 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
7871 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
7872 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
7873 { NULL, EXT_ATTR_LAST, NULL }
7876 /* Match a !GCC$ ATTRIBUTES statement of the form:
7877 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
7878 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
7880 TODO: We should support all GCC attributes using the same syntax for
7881 the attribute list, i.e. the list in C
7882 __attributes(( attribute-list ))
7884 !GCC$ ATTRIBUTES attribute-list ::
7885 Cf. c-parser.c's c_parser_attributes; the data can then directly be
7888 As there is absolutely no risk of confusion, we should never return
7891 gfc_match_gcc_attributes (void)
7893 symbol_attribute attr;
7894 char name[GFC_MAX_SYMBOL_LEN + 1];
7899 gfc_clear_attr (&attr);
7904 if (gfc_match_name (name) != MATCH_YES)
7907 for (id = 0; id < EXT_ATTR_LAST; id++)
7908 if (strcmp (name, ext_attr_list[id].name) == 0)
7911 if (id == EXT_ATTR_LAST)
7913 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
7917 if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
7921 gfc_gobble_whitespace ();
7922 ch = gfc_next_ascii_char ();
7925 /* This is the successful exit condition for the loop. */
7926 if (gfc_next_ascii_char () == ':')
7936 if (gfc_match_eos () == MATCH_YES)
7941 m = gfc_match_name (name);
7945 if (find_special (name, &sym, true))
7948 sym->attr.ext_attr |= attr.ext_attr;
7950 if (gfc_match_eos () == MATCH_YES)
7953 if (gfc_match_char (',') != MATCH_YES)
7960 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");