1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
28 #include "constructor.h"
30 /* Macros to access allocate memory for gfc_data_variable,
31 gfc_data_value and gfc_data. */
32 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
33 #define gfc_get_data_value() XCNEW (gfc_data_value)
34 #define gfc_get_data() XCNEW (gfc_data)
37 /* This flag is set if an old-style length selector is matched
38 during a type-declaration statement. */
40 static int old_char_selector;
42 /* When variables acquire types and attributes from a declaration
43 statement, they get them from the following static variables. The
44 first part of a declaration sets these variables and the second
45 part copies these into symbol structures. */
47 static gfc_typespec current_ts;
49 static symbol_attribute current_attr;
50 static gfc_array_spec *current_as;
51 static int colon_seen;
53 /* The current binding label (if any). */
54 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
55 /* Need to know how many identifiers are on the current data declaration
56 line in case we're given the BIND(C) attribute with a NAME= specifier. */
57 static int num_idents_on_line;
58 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
59 can supply a name if the curr_binding_label is nil and NAME= was not. */
60 static int has_name_equals = 0;
62 /* Initializer of the previous enumerator. */
64 static gfc_expr *last_initializer;
66 /* History of all the enumerators is maintained, so that
67 kind values of all the enumerators could be updated depending
68 upon the maximum initialized value. */
70 typedef struct enumerator_history
73 gfc_expr *initializer;
74 struct enumerator_history *next;
78 /* Header of enum history chain. */
80 static enumerator_history *enum_history = NULL;
82 /* Pointer of enum history node containing largest initializer. */
84 static enumerator_history *max_enum = NULL;
86 /* gfc_new_block points to the symbol of a newly matched block. */
88 gfc_symbol *gfc_new_block;
90 bool gfc_matching_function;
93 /********************* DATA statement subroutines *********************/
95 static bool in_match_data = false;
98 gfc_in_match_data (void)
100 return in_match_data;
104 set_in_match_data (bool set_value)
106 in_match_data = set_value;
109 /* Free a gfc_data_variable structure and everything beneath it. */
112 free_variable (gfc_data_variable *p)
114 gfc_data_variable *q;
119 gfc_free_expr (p->expr);
120 gfc_free_iterator (&p->iter, 0);
121 free_variable (p->list);
127 /* Free a gfc_data_value structure and everything beneath it. */
130 free_value (gfc_data_value *p)
137 mpz_clear (p->repeat);
138 gfc_free_expr (p->expr);
144 /* Free a list of gfc_data structures. */
147 gfc_free_data (gfc_data *p)
154 free_variable (p->var);
155 free_value (p->value);
161 /* Free all data in a namespace. */
164 gfc_free_data_all (gfc_namespace *ns)
177 static match var_element (gfc_data_variable *);
179 /* Match a list of variables terminated by an iterator and a right
183 var_list (gfc_data_variable *parent)
185 gfc_data_variable *tail, var;
188 m = var_element (&var);
189 if (m == MATCH_ERROR)
194 tail = gfc_get_data_variable ();
201 if (gfc_match_char (',') != MATCH_YES)
204 m = gfc_match_iterator (&parent->iter, 1);
207 if (m == MATCH_ERROR)
210 m = var_element (&var);
211 if (m == MATCH_ERROR)
216 tail->next = gfc_get_data_variable ();
222 if (gfc_match_char (')') != MATCH_YES)
227 gfc_syntax_error (ST_DATA);
232 /* Match a single element in a data variable list, which can be a
233 variable-iterator list. */
236 var_element (gfc_data_variable *new_var)
241 memset (new_var, 0, sizeof (gfc_data_variable));
243 if (gfc_match_char ('(') == MATCH_YES)
244 return var_list (new_var);
246 m = gfc_match_variable (&new_var->expr, 0);
250 sym = new_var->expr->symtree->n.sym;
252 /* Symbol should already have an associated type. */
253 if (gfc_check_symbol_typed (sym, gfc_current_ns,
254 false, gfc_current_locus) == FAILURE)
257 if (!sym->attr.function && gfc_current_ns->parent
258 && gfc_current_ns->parent == sym->ns)
260 gfc_error ("Host associated variable '%s' may not be in the DATA "
261 "statement at %C", sym->name);
265 if (gfc_current_state () != COMP_BLOCK_DATA
266 && sym->attr.in_common
267 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
268 "common block variable '%s' in DATA statement at %C",
269 sym->name) == FAILURE)
272 if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
279 /* Match the top-level list of data variables. */
282 top_var_list (gfc_data *d)
284 gfc_data_variable var, *tail, *new_var;
291 m = var_element (&var);
294 if (m == MATCH_ERROR)
297 new_var = gfc_get_data_variable ();
303 tail->next = new_var;
307 if (gfc_match_char ('/') == MATCH_YES)
309 if (gfc_match_char (',') != MATCH_YES)
316 gfc_syntax_error (ST_DATA);
317 gfc_free_data_all (gfc_current_ns);
323 match_data_constant (gfc_expr **result)
325 char name[GFC_MAX_SYMBOL_LEN + 1];
331 m = gfc_match_literal_constant (&expr, 1);
338 if (m == MATCH_ERROR)
341 m = gfc_match_null (result);
345 old_loc = gfc_current_locus;
347 /* Should this be a structure component, try to match it
348 before matching a name. */
349 m = gfc_match_rvalue (result);
350 if (m == MATCH_ERROR)
353 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
355 if (gfc_simplify_expr (*result, 0) == FAILURE)
360 gfc_current_locus = old_loc;
362 m = gfc_match_name (name);
366 if (gfc_find_symbol (name, NULL, 1, &sym))
370 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
372 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
376 else if (sym->attr.flavor == FL_DERIVED)
377 return gfc_match_structure_constructor (sym, result, false);
379 /* Check to see if the value is an initialization array expression. */
380 if (sym->value->expr_type == EXPR_ARRAY)
382 gfc_current_locus = old_loc;
384 m = gfc_match_init_expr (result);
385 if (m == MATCH_ERROR)
390 if (gfc_simplify_expr (*result, 0) == FAILURE)
393 if ((*result)->expr_type == EXPR_CONSTANT)
397 gfc_error ("Invalid initializer %s in Data statement at %C", name);
403 *result = gfc_copy_expr (sym->value);
408 /* Match a list of values in a DATA statement. The leading '/' has
409 already been seen at this point. */
412 top_val_list (gfc_data *data)
414 gfc_data_value *new_val, *tail;
422 m = match_data_constant (&expr);
425 if (m == MATCH_ERROR)
428 new_val = gfc_get_data_value ();
429 mpz_init (new_val->repeat);
432 data->value = new_val;
434 tail->next = new_val;
438 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
441 mpz_set_ui (tail->repeat, 1);
445 if (expr->ts.type == BT_INTEGER)
446 mpz_set (tail->repeat, expr->value.integer);
447 gfc_free_expr (expr);
449 m = match_data_constant (&tail->expr);
452 if (m == MATCH_ERROR)
456 if (gfc_match_char ('/') == MATCH_YES)
458 if (gfc_match_char (',') == MATCH_NO)
465 gfc_syntax_error (ST_DATA);
466 gfc_free_data_all (gfc_current_ns);
471 /* Matches an old style initialization. */
474 match_old_style_init (const char *name)
481 /* Set up data structure to hold initializers. */
482 gfc_find_sym_tree (name, NULL, 0, &st);
485 newdata = gfc_get_data ();
486 newdata->var = gfc_get_data_variable ();
487 newdata->var->expr = gfc_get_variable_expr (st);
488 newdata->where = gfc_current_locus;
490 /* Match initial value list. This also eats the terminal '/'. */
491 m = top_val_list (newdata);
500 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
505 /* Mark the variable as having appeared in a data statement. */
506 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
512 /* Chain in namespace list of DATA initializers. */
513 newdata->next = gfc_current_ns->data;
514 gfc_current_ns->data = newdata;
520 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
521 we are matching a DATA statement and are therefore issuing an error
522 if we encounter something unexpected, if not, we're trying to match
523 an old-style initialization expression of the form INTEGER I /2/. */
526 gfc_match_data (void)
531 set_in_match_data (true);
535 new_data = gfc_get_data ();
536 new_data->where = gfc_current_locus;
538 m = top_var_list (new_data);
542 m = top_val_list (new_data);
546 new_data->next = gfc_current_ns->data;
547 gfc_current_ns->data = new_data;
549 if (gfc_match_eos () == MATCH_YES)
552 gfc_match_char (','); /* Optional comma */
555 set_in_match_data (false);
559 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
566 set_in_match_data (false);
567 gfc_free_data (new_data);
572 /************************ Declaration statements *********************/
575 /* Auxilliary function to merge DIMENSION and CODIMENSION array specs. */
578 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
582 if (to->rank == 0 && from->rank > 0)
584 to->rank = from->rank;
585 to->type = from->type;
586 to->cray_pointee = from->cray_pointee;
587 to->cp_was_assumed = from->cp_was_assumed;
589 for (i = 0; i < to->corank; i++)
591 to->lower[from->rank + i] = to->lower[i];
592 to->upper[from->rank + i] = to->upper[i];
594 for (i = 0; i < from->rank; i++)
598 to->lower[i] = gfc_copy_expr (from->lower[i]);
599 to->upper[i] = gfc_copy_expr (from->upper[i]);
603 to->lower[i] = from->lower[i];
604 to->upper[i] = from->upper[i];
608 else if (to->corank == 0 && from->corank > 0)
610 to->corank = from->corank;
611 to->cotype = from->cotype;
613 for (i = 0; i < from->corank; i++)
617 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
618 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
622 to->lower[to->rank + i] = from->lower[i];
623 to->upper[to->rank + i] = from->upper[i];
630 /* Match an intent specification. Since this can only happen after an
631 INTENT word, a legal intent-spec must follow. */
634 match_intent_spec (void)
637 if (gfc_match (" ( in out )") == MATCH_YES)
639 if (gfc_match (" ( in )") == MATCH_YES)
641 if (gfc_match (" ( out )") == MATCH_YES)
644 gfc_error ("Bad INTENT specification at %C");
645 return INTENT_UNKNOWN;
649 /* Matches a character length specification, which is either a
650 specification expression or a '*'. */
653 char_len_param_value (gfc_expr **expr)
657 if (gfc_match_char ('*') == MATCH_YES)
663 m = gfc_match_expr (expr);
666 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
669 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
671 if ((*expr)->value.function.actual
672 && (*expr)->value.function.actual->expr->symtree)
675 e = (*expr)->value.function.actual->expr;
676 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
677 && e->expr_type == EXPR_VARIABLE)
679 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
681 if (e->symtree->n.sym->ts.type == BT_CHARACTER
682 && e->symtree->n.sym->ts.u.cl
683 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
691 gfc_error ("Conflict in attributes of function argument at %C");
696 /* A character length is a '*' followed by a literal integer or a
697 char_len_param_value in parenthesis. */
700 match_char_length (gfc_expr **expr)
705 m = gfc_match_char ('*');
709 m = gfc_match_small_literal_int (&length, NULL);
710 if (m == MATCH_ERROR)
715 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
716 "Old-style character length at %C") == FAILURE)
718 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
722 if (gfc_match_char ('(') == MATCH_NO)
725 m = char_len_param_value (expr);
726 if (m != MATCH_YES && gfc_matching_function)
732 if (m == MATCH_ERROR)
737 if (gfc_match_char (')') == MATCH_NO)
739 gfc_free_expr (*expr);
747 gfc_error ("Syntax error in character length specification at %C");
752 /* Special subroutine for finding a symbol. Check if the name is found
753 in the current name space. If not, and we're compiling a function or
754 subroutine and the parent compilation unit is an interface, then check
755 to see if the name we've been given is the name of the interface
756 (located in another namespace). */
759 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
765 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
768 *result = st ? st->n.sym : NULL;
772 if (gfc_current_state () != COMP_SUBROUTINE
773 && gfc_current_state () != COMP_FUNCTION)
776 s = gfc_state_stack->previous;
780 if (s->state != COMP_INTERFACE)
783 goto end; /* Nameless interface. */
785 if (strcmp (name, s->sym->name) == 0)
796 /* Special subroutine for getting a symbol node associated with a
797 procedure name, used in SUBROUTINE and FUNCTION statements. The
798 symbol is created in the parent using with symtree node in the
799 child unit pointing to the symbol. If the current namespace has no
800 parent, then the symbol is just created in the current unit. */
803 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
809 /* Module functions have to be left in their own namespace because
810 they have potentially (almost certainly!) already been referenced.
811 In this sense, they are rather like external functions. This is
812 fixed up in resolve.c(resolve_entries), where the symbol name-
813 space is set to point to the master function, so that the fake
814 result mechanism can work. */
815 if (module_fcn_entry)
817 /* Present if entry is declared to be a module procedure. */
818 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
821 rc = gfc_get_symbol (name, NULL, result);
822 else if (!gfc_get_symbol (name, NULL, &sym) && sym
823 && (*result)->ts.type == BT_UNKNOWN
824 && sym->attr.flavor == FL_UNKNOWN)
825 /* Pick up the typespec for the entry, if declared in the function
826 body. Note that this symbol is FL_UNKNOWN because it will
827 only have appeared in a type declaration. The local symtree
828 is set to point to the module symbol and a unique symtree
829 to the local version. This latter ensures a correct clearing
832 /* If the ENTRY proceeds its specification, we need to ensure
833 that this does not raise a "has no IMPLICIT type" error. */
834 if (sym->ts.type == BT_UNKNOWN)
835 sym->attr.untyped = 1;
837 (*result)->ts = sym->ts;
839 /* Put the symbol in the procedure namespace so that, should
840 the ENTRY precede its specification, the specification
842 (*result)->ns = gfc_current_ns;
844 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
846 st = gfc_get_unique_symtree (gfc_current_ns);
851 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
857 gfc_current_ns->refs++;
859 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
861 /* Trap another encompassed procedure with the same name. All
862 these conditions are necessary to avoid picking up an entry
863 whose name clashes with that of the encompassing procedure;
864 this is handled using gsymbols to register unique,globally
866 if (sym->attr.flavor != 0
867 && sym->attr.proc != 0
868 && (sym->attr.subroutine || sym->attr.function)
869 && sym->attr.if_source != IFSRC_UNKNOWN)
870 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
871 name, &sym->declared_at);
873 /* Trap a procedure with a name the same as interface in the
874 encompassing scope. */
875 if (sym->attr.generic != 0
876 && (sym->attr.subroutine || sym->attr.function)
877 && !sym->attr.mod_proc)
878 gfc_error_now ("Name '%s' at %C is already defined"
879 " as a generic interface at %L",
880 name, &sym->declared_at);
882 /* Trap declarations of attributes in encompassing scope. The
883 signature for this is that ts.kind is set. Legitimate
884 references only set ts.type. */
885 if (sym->ts.kind != 0
886 && !sym->attr.implicit_type
887 && sym->attr.proc == 0
888 && gfc_current_ns->parent != NULL
889 && sym->attr.access == 0
890 && !module_fcn_entry)
891 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
892 "and must not have attributes declared at %L",
893 name, &sym->declared_at);
896 if (gfc_current_ns->parent == NULL || *result == NULL)
899 /* Module function entries will already have a symtree in
900 the current namespace but will need one at module level. */
901 if (module_fcn_entry)
903 /* Present if entry is declared to be a module procedure. */
904 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
906 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
909 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
914 /* See if the procedure should be a module procedure. */
916 if (((sym->ns->proc_name != NULL
917 && sym->ns->proc_name->attr.flavor == FL_MODULE
918 && sym->attr.proc != PROC_MODULE)
919 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
920 && gfc_add_procedure (&sym->attr, PROC_MODULE,
921 sym->name, NULL) == FAILURE)
928 /* Verify that the given symbol representing a parameter is C
929 interoperable, by checking to see if it was marked as such after
930 its declaration. If the given symbol is not interoperable, a
931 warning is reported, thus removing the need to return the status to
932 the calling function. The standard does not require the user use
933 one of the iso_c_binding named constants to declare an
934 interoperable parameter, but we can't be sure if the param is C
935 interop or not if the user doesn't. For example, integer(4) may be
936 legal Fortran, but doesn't have meaning in C. It may interop with
937 a number of the C types, which causes a problem because the
938 compiler can't know which one. This code is almost certainly not
939 portable, and the user will get what they deserve if the C type
940 across platforms isn't always interoperable with integer(4). If
941 the user had used something like integer(c_int) or integer(c_long),
942 the compiler could have automatically handled the varying sizes
946 verify_c_interop_param (gfc_symbol *sym)
948 int is_c_interop = 0;
949 gfc_try retval = SUCCESS;
951 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
952 Don't repeat the checks here. */
953 if (sym->attr.implicit_type)
956 /* For subroutines or functions that are passed to a BIND(C) procedure,
957 they're interoperable if they're BIND(C) and their params are all
959 if (sym->attr.flavor == FL_PROCEDURE)
961 if (sym->attr.is_bind_c == 0)
963 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
964 "attribute to be C interoperable", sym->name,
965 &(sym->declared_at));
971 if (sym->attr.is_c_interop == 1)
972 /* We've already checked this procedure; don't check it again. */
975 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
980 /* See if we've stored a reference to a procedure that owns sym. */
981 if (sym->ns != NULL && sym->ns->proc_name != NULL)
983 if (sym->ns->proc_name->attr.is_bind_c == 1)
986 (verify_c_interop (&(sym->ts))
989 if (is_c_interop != 1)
991 /* Make personalized messages to give better feedback. */
992 if (sym->ts.type == BT_DERIVED)
993 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
994 "procedure '%s' but is not C interoperable "
995 "because derived type '%s' is not C interoperable",
996 sym->name, &(sym->declared_at),
997 sym->ns->proc_name->name,
998 sym->ts.u.derived->name);
1000 gfc_warning ("Variable '%s' at %L is a parameter to the "
1001 "BIND(C) procedure '%s' but may not be C "
1003 sym->name, &(sym->declared_at),
1004 sym->ns->proc_name->name);
1007 /* Character strings are only C interoperable if they have a
1009 if (sym->ts.type == BT_CHARACTER)
1011 gfc_charlen *cl = sym->ts.u.cl;
1012 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1013 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1015 gfc_error ("Character argument '%s' at %L "
1016 "must be length 1 because "
1017 "procedure '%s' is BIND(C)",
1018 sym->name, &sym->declared_at,
1019 sym->ns->proc_name->name);
1024 /* We have to make sure that any param to a bind(c) routine does
1025 not have the allocatable, pointer, or optional attributes,
1026 according to J3/04-007, section 5.1. */
1027 if (sym->attr.allocatable == 1)
1029 gfc_error ("Variable '%s' at %L cannot have the "
1030 "ALLOCATABLE attribute because procedure '%s'"
1031 " is BIND(C)", sym->name, &(sym->declared_at),
1032 sym->ns->proc_name->name);
1036 if (sym->attr.pointer == 1)
1038 gfc_error ("Variable '%s' at %L cannot have the "
1039 "POINTER attribute because procedure '%s'"
1040 " is BIND(C)", sym->name, &(sym->declared_at),
1041 sym->ns->proc_name->name);
1045 if (sym->attr.optional == 1)
1047 gfc_error ("Variable '%s' at %L cannot have the "
1048 "OPTIONAL attribute because procedure '%s'"
1049 " is BIND(C)", sym->name, &(sym->declared_at),
1050 sym->ns->proc_name->name);
1054 /* Make sure that if it has the dimension attribute, that it is
1055 either assumed size or explicit shape. */
1056 if (sym->as != NULL)
1058 if (sym->as->type == AS_ASSUMED_SHAPE)
1060 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1061 "argument to the procedure '%s' at %L because "
1062 "the procedure is BIND(C)", sym->name,
1063 &(sym->declared_at), sym->ns->proc_name->name,
1064 &(sym->ns->proc_name->declared_at));
1068 if (sym->as->type == AS_DEFERRED)
1070 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1071 "argument to the procedure '%s' at %L because "
1072 "the procedure is BIND(C)", sym->name,
1073 &(sym->declared_at), sym->ns->proc_name->name,
1074 &(sym->ns->proc_name->declared_at));
1086 /* Function called by variable_decl() that adds a name to the symbol table. */
1089 build_sym (const char *name, gfc_charlen *cl,
1090 gfc_array_spec **as, locus *var_locus)
1092 symbol_attribute attr;
1095 if (gfc_get_symbol (name, NULL, &sym))
1098 /* Start updating the symbol table. Add basic type attribute if present. */
1099 if (current_ts.type != BT_UNKNOWN
1100 && (sym->attr.implicit_type == 0
1101 || !gfc_compare_types (&sym->ts, ¤t_ts))
1102 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
1105 if (sym->ts.type == BT_CHARACTER)
1108 /* Add dimension attribute if present. */
1109 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1113 /* Add attribute to symbol. The copy is so that we can reset the
1114 dimension attribute. */
1115 attr = current_attr;
1117 attr.codimension = 0;
1119 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1122 /* Finish any work that may need to be done for the binding label,
1123 if it's a bind(c). The bind(c) attr is found before the symbol
1124 is made, and before the symbol name (for data decls), so the
1125 current_ts is holding the binding label, or nothing if the
1126 name= attr wasn't given. Therefore, test here if we're dealing
1127 with a bind(c) and make sure the binding label is set correctly. */
1128 if (sym->attr.is_bind_c == 1)
1130 if (sym->binding_label[0] == '\0')
1132 /* Set the binding label and verify that if a NAME= was specified
1133 then only one identifier was in the entity-decl-list. */
1134 if (set_binding_label (sym->binding_label, sym->name,
1135 num_idents_on_line) == FAILURE)
1140 /* See if we know we're in a common block, and if it's a bind(c)
1141 common then we need to make sure we're an interoperable type. */
1142 if (sym->attr.in_common == 1)
1144 /* Test the common block object. */
1145 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1146 && sym->ts.is_c_interop != 1)
1148 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1149 "must be declared with a C interoperable "
1150 "kind since common block '%s' is BIND(C)",
1151 sym->name, sym->common_block->name,
1152 sym->common_block->name);
1157 sym->attr.implied_index = 0;
1159 if (sym->ts.type == BT_CLASS
1160 && (sym->attr.class_ok = sym->attr.dummy || sym->attr.pointer
1161 || sym->attr.allocatable))
1162 gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1168 /* Set character constant to the given length. The constant will be padded or
1169 truncated. If we're inside an array constructor without a typespec, we
1170 additionally check that all elements have the same length; check_len -1
1171 means no checking. */
1174 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1179 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1180 gcc_assert (expr->ts.type == BT_CHARACTER);
1182 slen = expr->value.character.length;
1185 s = gfc_get_wide_string (len + 1);
1186 memcpy (s, expr->value.character.string,
1187 MIN (len, slen) * sizeof (gfc_char_t));
1189 gfc_wide_memset (&s[slen], ' ', len - slen);
1191 if (gfc_option.warn_character_truncation && slen > len)
1192 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1193 "(%d/%d)", &expr->where, slen, len);
1195 /* Apply the standard by 'hand' otherwise it gets cleared for
1197 if (check_len != -1 && slen != check_len
1198 && !(gfc_option.allow_std & GFC_STD_GNU))
1199 gfc_error_now ("The CHARACTER elements of the array constructor "
1200 "at %L must have the same length (%d/%d)",
1201 &expr->where, slen, check_len);
1204 gfc_free (expr->value.character.string);
1205 expr->value.character.string = s;
1206 expr->value.character.length = len;
1211 /* Function to create and update the enumerator history
1212 using the information passed as arguments.
1213 Pointer "max_enum" is also updated, to point to
1214 enum history node containing largest initializer.
1216 SYM points to the symbol node of enumerator.
1217 INIT points to its enumerator value. */
1220 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1222 enumerator_history *new_enum_history;
1223 gcc_assert (sym != NULL && init != NULL);
1225 new_enum_history = XCNEW (enumerator_history);
1227 new_enum_history->sym = sym;
1228 new_enum_history->initializer = init;
1229 new_enum_history->next = NULL;
1231 if (enum_history == NULL)
1233 enum_history = new_enum_history;
1234 max_enum = enum_history;
1238 new_enum_history->next = enum_history;
1239 enum_history = new_enum_history;
1241 if (mpz_cmp (max_enum->initializer->value.integer,
1242 new_enum_history->initializer->value.integer) < 0)
1243 max_enum = new_enum_history;
1248 /* Function to free enum kind history. */
1251 gfc_free_enum_history (void)
1253 enumerator_history *current = enum_history;
1254 enumerator_history *next;
1256 while (current != NULL)
1258 next = current->next;
1263 enum_history = NULL;
1267 /* Function called by variable_decl() that adds an initialization
1268 expression to a symbol. */
1271 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1273 symbol_attribute attr;
1278 if (find_special (name, &sym, false))
1283 /* If this symbol is confirming an implicit parameter type,
1284 then an initialization expression is not allowed. */
1285 if (attr.flavor == FL_PARAMETER
1286 && sym->value != NULL
1289 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1296 /* An initializer is required for PARAMETER declarations. */
1297 if (attr.flavor == FL_PARAMETER)
1299 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1305 /* If a variable appears in a DATA block, it cannot have an
1309 gfc_error ("Variable '%s' at %C with an initializer already "
1310 "appears in a DATA statement", sym->name);
1314 /* Check if the assignment can happen. This has to be put off
1315 until later for a derived type variable. */
1316 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1317 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1318 && gfc_check_assign_symbol (sym, init) == FAILURE)
1321 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1322 && init->ts.type == BT_CHARACTER)
1324 /* Update symbol character length according initializer. */
1325 if (gfc_check_assign_symbol (sym, init) == FAILURE)
1328 if (sym->ts.u.cl->length == NULL)
1331 /* If there are multiple CHARACTER variables declared on the
1332 same line, we don't want them to share the same length. */
1333 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1335 if (sym->attr.flavor == FL_PARAMETER)
1337 if (init->expr_type == EXPR_CONSTANT)
1339 clen = init->value.character.length;
1340 sym->ts.u.cl->length
1341 = gfc_get_int_expr (gfc_default_integer_kind,
1344 else if (init->expr_type == EXPR_ARRAY)
1347 c = gfc_constructor_first (init->value.constructor);
1348 clen = c->expr->value.character.length;
1349 sym->ts.u.cl->length
1350 = gfc_get_int_expr (gfc_default_integer_kind,
1353 else if (init->ts.u.cl && init->ts.u.cl->length)
1354 sym->ts.u.cl->length =
1355 gfc_copy_expr (sym->value->ts.u.cl->length);
1358 /* Update initializer character length according symbol. */
1359 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1361 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1363 if (init->expr_type == EXPR_CONSTANT)
1364 gfc_set_constant_character_len (len, init, -1);
1365 else if (init->expr_type == EXPR_ARRAY)
1369 /* Build a new charlen to prevent simplification from
1370 deleting the length before it is resolved. */
1371 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1372 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1374 for (c = gfc_constructor_first (init->value.constructor);
1375 c; c = gfc_constructor_next (c))
1376 gfc_set_constant_character_len (len, c->expr, -1);
1381 /* If sym is implied-shape, set its upper bounds from init. */
1382 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1383 && sym->as->type == AS_IMPLIED_SHAPE)
1387 if (init->rank == 0)
1389 gfc_error ("Can't initialize implied-shape array at %L"
1390 " with scalar", &sym->declared_at);
1393 gcc_assert (sym->as->rank == init->rank);
1395 /* Shape should be present, we get an initialization expression. */
1396 gcc_assert (init->shape);
1398 for (dim = 0; dim < sym->as->rank; ++dim)
1404 lower = sym->as->lower[dim];
1405 if (lower->expr_type != EXPR_CONSTANT)
1407 gfc_error ("Non-constant lower bound in implied-shape"
1408 " declaration at %L", &lower->where);
1412 /* All dimensions must be without upper bound. */
1413 gcc_assert (!sym->as->upper[dim]);
1416 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1417 mpz_add (e->value.integer,
1418 lower->value.integer, init->shape[dim]);
1419 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1420 sym->as->upper[dim] = e;
1423 sym->as->type = AS_EXPLICIT;
1426 /* Need to check if the expression we initialized this
1427 to was one of the iso_c_binding named constants. If so,
1428 and we're a parameter (constant), let it be iso_c.
1430 integer(c_int), parameter :: my_int = c_int
1431 integer(my_int) :: my_int_2
1432 If we mark my_int as iso_c (since we can see it's value
1433 is equal to one of the named constants), then my_int_2
1434 will be considered C interoperable. */
1435 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1437 sym->ts.is_iso_c |= init->ts.is_iso_c;
1438 sym->ts.is_c_interop |= init->ts.is_c_interop;
1439 /* attr bits needed for module files. */
1440 sym->attr.is_iso_c |= init->ts.is_iso_c;
1441 sym->attr.is_c_interop |= init->ts.is_c_interop;
1442 if (init->ts.is_iso_c)
1443 sym->ts.f90_type = init->ts.f90_type;
1446 /* Add initializer. Make sure we keep the ranks sane. */
1447 if (sym->attr.dimension && init->rank == 0)
1452 if (sym->attr.flavor == FL_PARAMETER
1453 && init->expr_type == EXPR_CONSTANT
1454 && spec_size (sym->as, &size) == SUCCESS
1455 && mpz_cmp_si (size, 0) > 0)
1457 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1459 for (n = 0; n < (int)mpz_get_si (size); n++)
1460 gfc_constructor_append_expr (&array->value.constructor,
1463 : gfc_copy_expr (init),
1466 array->shape = gfc_get_shape (sym->as->rank);
1467 for (n = 0; n < sym->as->rank; n++)
1468 spec_dimen_size (sym->as, n, &array->shape[n]);
1473 init->rank = sym->as->rank;
1477 if (sym->attr.save == SAVE_NONE)
1478 sym->attr.save = SAVE_IMPLICIT;
1486 /* Function called by variable_decl() that adds a name to a structure
1490 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1491 gfc_array_spec **as)
1494 gfc_try t = SUCCESS;
1496 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1497 constructing, it must have the pointer attribute. */
1498 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1499 && current_ts.u.derived == gfc_current_block ()
1500 && current_attr.pointer == 0)
1502 gfc_error ("Component at %C must have the POINTER attribute");
1506 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1508 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1510 gfc_error ("Array component of structure at %C must have explicit "
1511 "or deferred shape");
1516 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1520 if (c->ts.type == BT_CHARACTER)
1522 c->attr = current_attr;
1524 c->initializer = *init;
1531 c->attr.codimension = 1;
1533 c->attr.dimension = 1;
1537 /* Should this ever get more complicated, combine with similar section
1538 in add_init_expr_to_sym into a separate function. */
1539 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer && c->ts.u.cl
1540 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1544 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1545 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1546 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1548 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1550 if (c->initializer->expr_type == EXPR_CONSTANT)
1551 gfc_set_constant_character_len (len, c->initializer, -1);
1552 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1553 c->initializer->ts.u.cl->length->value.integer))
1555 gfc_constructor *ctor;
1556 ctor = gfc_constructor_first (c->initializer->value.constructor);
1561 bool has_ts = (c->initializer->ts.u.cl
1562 && c->initializer->ts.u.cl->length_from_typespec);
1564 /* Remember the length of the first element for checking
1565 that all elements *in the constructor* have the same
1566 length. This need not be the length of the LHS! */
1567 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1568 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1569 first_len = ctor->expr->value.character.length;
1571 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1572 if (ctor->expr->expr_type == EXPR_CONSTANT)
1574 gfc_set_constant_character_len (len, ctor->expr,
1575 has_ts ? -1 : first_len);
1576 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1582 /* Check array components. */
1583 if (!c->attr.dimension)
1586 if (c->attr.pointer)
1588 if (c->as->type != AS_DEFERRED)
1590 gfc_error ("Pointer array component of structure at %C must have a "
1595 else if (c->attr.allocatable)
1597 if (c->as->type != AS_DEFERRED)
1599 gfc_error ("Allocatable component of structure at %C must have a "
1606 if (c->as->type != AS_EXPLICIT)
1608 gfc_error ("Array component of structure at %C must have an "
1615 if (c->ts.type == BT_CLASS)
1616 gfc_build_class_symbol (&c->ts, &c->attr, &c->as, true);
1622 /* Match a 'NULL()', and possibly take care of some side effects. */
1625 gfc_match_null (gfc_expr **result)
1630 m = gfc_match (" null ( )");
1634 /* The NULL symbol now has to be/become an intrinsic function. */
1635 if (gfc_get_symbol ("null", NULL, &sym))
1637 gfc_error ("NULL() initialization at %C is ambiguous");
1641 gfc_intrinsic_symbol (sym);
1643 if (sym->attr.proc != PROC_INTRINSIC
1644 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1645 sym->name, NULL) == FAILURE
1646 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1649 *result = gfc_get_null_expr (&gfc_current_locus);
1655 /* Match a variable name with an optional initializer. When this
1656 subroutine is called, a variable is expected to be parsed next.
1657 Depending on what is happening at the moment, updates either the
1658 symbol table or the current interface. */
1661 variable_decl (int elem)
1663 char name[GFC_MAX_SYMBOL_LEN + 1];
1664 gfc_expr *initializer, *char_len;
1666 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1677 /* When we get here, we've just matched a list of attributes and
1678 maybe a type and a double colon. The next thing we expect to see
1679 is the name of the symbol. */
1680 m = gfc_match_name (name);
1684 var_locus = gfc_current_locus;
1686 /* Now we could see the optional array spec. or character length. */
1687 m = gfc_match_array_spec (&as, true, true);
1688 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1689 cp_as = gfc_copy_array_spec (as);
1690 else if (m == MATCH_ERROR)
1694 as = gfc_copy_array_spec (current_as);
1695 else if (current_as)
1696 merge_array_spec (current_as, as, true);
1698 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1699 determine (and check) whether it can be implied-shape. If it
1700 was parsed as assumed-size, change it because PARAMETERs can not
1704 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1707 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1712 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1713 && current_attr.flavor == FL_PARAMETER)
1714 as->type = AS_IMPLIED_SHAPE;
1716 if (as->type == AS_IMPLIED_SHAPE
1717 && gfc_notify_std (GFC_STD_F2008,
1718 "Fortran 2008: Implied-shape array at %L",
1719 &var_locus) == FAILURE)
1729 if (current_ts.type == BT_CHARACTER)
1731 switch (match_char_length (&char_len))
1734 cl = gfc_new_charlen (gfc_current_ns, NULL);
1736 cl->length = char_len;
1739 /* Non-constant lengths need to be copied after the first
1740 element. Also copy assumed lengths. */
1743 && (current_ts.u.cl->length == NULL
1744 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1746 cl = gfc_new_charlen (gfc_current_ns, NULL);
1747 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1750 cl = current_ts.u.cl;
1759 /* If this symbol has already shown up in a Cray Pointer declaration,
1760 then we want to set the type & bail out. */
1761 if (gfc_option.flag_cray_pointer)
1763 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1764 if (sym != NULL && sym->attr.cray_pointee)
1766 sym->ts.type = current_ts.type;
1767 sym->ts.kind = current_ts.kind;
1769 sym->ts.u.derived = current_ts.u.derived;
1770 sym->ts.is_c_interop = current_ts.is_c_interop;
1771 sym->ts.is_iso_c = current_ts.is_iso_c;
1774 /* Check to see if we have an array specification. */
1777 if (sym->as != NULL)
1779 gfc_error ("Duplicate array spec for Cray pointee at %C");
1780 gfc_free_array_spec (cp_as);
1786 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1787 gfc_internal_error ("Couldn't set pointee array spec.");
1789 /* Fix the array spec. */
1790 m = gfc_mod_pointee_as (sym->as);
1791 if (m == MATCH_ERROR)
1799 gfc_free_array_spec (cp_as);
1803 /* Procedure pointer as function result. */
1804 if (gfc_current_state () == COMP_FUNCTION
1805 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1806 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1807 strcpy (name, "ppr@");
1809 if (gfc_current_state () == COMP_FUNCTION
1810 && strcmp (name, gfc_current_block ()->name) == 0
1811 && gfc_current_block ()->result
1812 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1813 strcpy (name, "ppr@");
1815 /* OK, we've successfully matched the declaration. Now put the
1816 symbol in the current namespace, because it might be used in the
1817 optional initialization expression for this symbol, e.g. this is
1820 integer, parameter :: i = huge(i)
1822 This is only true for parameters or variables of a basic type.
1823 For components of derived types, it is not true, so we don't
1824 create a symbol for those yet. If we fail to create the symbol,
1826 if (gfc_current_state () != COMP_DERIVED
1827 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1833 /* An interface body specifies all of the procedure's
1834 characteristics and these shall be consistent with those
1835 specified in the procedure definition, except that the interface
1836 may specify a procedure that is not pure if the procedure is
1837 defined to be pure(12.3.2). */
1838 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1839 && gfc_current_ns->proc_name
1840 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1841 && current_ts.u.derived->ns != gfc_current_ns)
1844 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.u.derived->name);
1845 if (!(current_ts.u.derived->attr.imported
1847 && st->n.sym == current_ts.u.derived)
1848 && !gfc_current_ns->has_import_set)
1850 gfc_error ("the type of '%s' at %C has not been declared within the "
1857 /* In functions that have a RESULT variable defined, the function
1858 name always refers to function calls. Therefore, the name is
1859 not allowed to appear in specification statements. */
1860 if (gfc_current_state () == COMP_FUNCTION
1861 && gfc_current_block () != NULL
1862 && gfc_current_block ()->result != NULL
1863 && gfc_current_block ()->result != gfc_current_block ()
1864 && strcmp (gfc_current_block ()->name, name) == 0)
1866 gfc_error ("Function name '%s' not allowed at %C", name);
1871 /* We allow old-style initializations of the form
1872 integer i /2/, j(4) /3*3, 1/
1873 (if no colon has been seen). These are different from data
1874 statements in that initializers are only allowed to apply to the
1875 variable immediately preceding, i.e.
1877 is not allowed. Therefore we have to do some work manually, that
1878 could otherwise be left to the matchers for DATA statements. */
1880 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1882 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1883 "initialization at %C") == FAILURE)
1886 return match_old_style_init (name);
1889 /* The double colon must be present in order to have initializers.
1890 Otherwise the statement is ambiguous with an assignment statement. */
1893 if (gfc_match (" =>") == MATCH_YES)
1895 if (!current_attr.pointer)
1897 gfc_error ("Initialization at %C isn't for a pointer variable");
1902 m = gfc_match_null (&initializer);
1905 gfc_error ("Pointer initialization requires a NULL() at %C");
1909 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1911 gfc_error ("Initialization of pointer at %C is not allowed in "
1912 "a PURE procedure");
1920 else if (gfc_match_char ('=') == MATCH_YES)
1922 if (current_attr.pointer)
1924 gfc_error ("Pointer initialization at %C requires '=>', "
1930 m = gfc_match_init_expr (&initializer);
1933 gfc_error ("Expected an initialization expression at %C");
1937 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
1938 && gfc_state_stack->state != COMP_DERIVED)
1940 gfc_error ("Initialization of variable at %C is not allowed in "
1941 "a PURE procedure");
1950 if (initializer != NULL && current_attr.allocatable
1951 && gfc_current_state () == COMP_DERIVED)
1953 gfc_error ("Initialization of allocatable component at %C is not "
1959 /* Add the initializer. Note that it is fine if initializer is
1960 NULL here, because we sometimes also need to check if a
1961 declaration *must* have an initialization expression. */
1962 if (gfc_current_state () != COMP_DERIVED)
1963 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1966 if (current_ts.type == BT_DERIVED
1967 && !current_attr.pointer && !initializer)
1968 initializer = gfc_default_initializer (¤t_ts);
1969 t = build_struct (name, cl, &initializer, &as);
1972 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1975 /* Free stuff up and return. */
1976 gfc_free_expr (initializer);
1977 gfc_free_array_spec (as);
1983 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1984 This assumes that the byte size is equal to the kind number for
1985 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1988 gfc_match_old_kind_spec (gfc_typespec *ts)
1993 if (gfc_match_char ('*') != MATCH_YES)
1996 m = gfc_match_small_literal_int (&ts->kind, NULL);
2000 original_kind = ts->kind;
2002 /* Massage the kind numbers for complex types. */
2003 if (ts->type == BT_COMPLEX)
2007 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2008 gfc_basic_typename (ts->type), original_kind);
2014 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2016 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2017 gfc_basic_typename (ts->type), original_kind);
2021 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2022 gfc_basic_typename (ts->type), original_kind) == FAILURE)
2029 /* Match a kind specification. Since kinds are generally optional, we
2030 usually return MATCH_NO if something goes wrong. If a "kind="
2031 string is found, then we know we have an error. */
2034 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2046 where = loc = gfc_current_locus;
2051 if (gfc_match_char ('(') == MATCH_NO)
2054 /* Also gobbles optional text. */
2055 if (gfc_match (" kind = ") == MATCH_YES)
2058 loc = gfc_current_locus;
2061 n = gfc_match_init_expr (&e);
2065 if (gfc_matching_function)
2067 /* The function kind expression might include use associated or
2068 imported parameters and try again after the specification
2070 if (gfc_match_char (')') != MATCH_YES)
2072 gfc_error ("Missing right parenthesis at %C");
2078 gfc_undo_symbols ();
2083 /* ....or else, the match is real. */
2085 gfc_error ("Expected initialization expression at %C");
2093 gfc_error ("Expected scalar initialization expression at %C");
2098 msg = gfc_extract_int (e, &ts->kind);
2107 /* Before throwing away the expression, let's see if we had a
2108 C interoperable kind (and store the fact). */
2109 if (e->ts.is_c_interop == 1)
2111 /* Mark this as c interoperable if being declared with one
2112 of the named constants from iso_c_binding. */
2113 ts->is_c_interop = e->ts.is_iso_c;
2114 ts->f90_type = e->ts.f90_type;
2120 /* Ignore errors to this point, if we've gotten here. This means
2121 we ignore the m=MATCH_ERROR from above. */
2122 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2124 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2125 gfc_basic_typename (ts->type));
2126 gfc_current_locus = where;
2130 /* Warn if, e.g., c_int is used for a REAL variable, but not
2131 if, e.g., c_double is used for COMPLEX as the standard
2132 explicitly says that the kind type parameter for complex and real
2133 variable is the same, i.e. c_float == c_float_complex. */
2134 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2135 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2136 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2137 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2138 "is %s", gfc_basic_typename (ts->f90_type), &where,
2139 gfc_basic_typename (ts->type));
2141 gfc_gobble_whitespace ();
2142 if ((c = gfc_next_ascii_char ()) != ')'
2143 && (ts->type != BT_CHARACTER || c != ','))
2145 if (ts->type == BT_CHARACTER)
2146 gfc_error ("Missing right parenthesis or comma at %C");
2148 gfc_error ("Missing right parenthesis at %C");
2152 /* All tests passed. */
2155 if(m == MATCH_ERROR)
2156 gfc_current_locus = where;
2158 /* Return what we know from the test(s). */
2163 gfc_current_locus = where;
2169 match_char_kind (int * kind, int * is_iso_c)
2178 where = gfc_current_locus;
2180 n = gfc_match_init_expr (&e);
2182 if (n != MATCH_YES && gfc_matching_function)
2184 /* The expression might include use-associated or imported
2185 parameters and try again after the specification
2188 gfc_undo_symbols ();
2193 gfc_error ("Expected initialization expression at %C");
2199 gfc_error ("Expected scalar initialization expression at %C");
2204 msg = gfc_extract_int (e, kind);
2205 *is_iso_c = e->ts.is_iso_c;
2215 /* Ignore errors to this point, if we've gotten here. This means
2216 we ignore the m=MATCH_ERROR from above. */
2217 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2219 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2223 /* All tests passed. */
2226 if (m == MATCH_ERROR)
2227 gfc_current_locus = where;
2229 /* Return what we know from the test(s). */
2234 gfc_current_locus = where;
2239 /* Match the various kind/length specifications in a CHARACTER
2240 declaration. We don't return MATCH_NO. */
2243 gfc_match_char_spec (gfc_typespec *ts)
2245 int kind, seen_length, is_iso_c;
2255 /* Try the old-style specification first. */
2256 old_char_selector = 0;
2258 m = match_char_length (&len);
2262 old_char_selector = 1;
2267 m = gfc_match_char ('(');
2270 m = MATCH_YES; /* Character without length is a single char. */
2274 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2275 if (gfc_match (" kind =") == MATCH_YES)
2277 m = match_char_kind (&kind, &is_iso_c);
2279 if (m == MATCH_ERROR)
2284 if (gfc_match (" , len =") == MATCH_NO)
2287 m = char_len_param_value (&len);
2290 if (m == MATCH_ERROR)
2297 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2298 if (gfc_match (" len =") == MATCH_YES)
2300 m = char_len_param_value (&len);
2303 if (m == MATCH_ERROR)
2307 if (gfc_match_char (')') == MATCH_YES)
2310 if (gfc_match (" , kind =") != MATCH_YES)
2313 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2319 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2320 m = char_len_param_value (&len);
2323 if (m == MATCH_ERROR)
2327 m = gfc_match_char (')');
2331 if (gfc_match_char (',') != MATCH_YES)
2334 gfc_match (" kind ="); /* Gobble optional text. */
2336 m = match_char_kind (&kind, &is_iso_c);
2337 if (m == MATCH_ERROR)
2343 /* Require a right-paren at this point. */
2344 m = gfc_match_char (')');
2349 gfc_error ("Syntax error in CHARACTER declaration at %C");
2351 gfc_free_expr (len);
2355 /* Deal with character functions after USE and IMPORT statements. */
2356 if (gfc_matching_function)
2358 gfc_free_expr (len);
2359 gfc_undo_symbols ();
2365 gfc_free_expr (len);
2369 /* Do some final massaging of the length values. */
2370 cl = gfc_new_charlen (gfc_current_ns, NULL);
2372 if (seen_length == 0)
2373 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2378 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2380 /* We have to know if it was a c interoperable kind so we can
2381 do accurate type checking of bind(c) procs, etc. */
2383 /* Mark this as c interoperable if being declared with one
2384 of the named constants from iso_c_binding. */
2385 ts->is_c_interop = is_iso_c;
2386 else if (len != NULL)
2387 /* Here, we might have parsed something such as: character(c_char)
2388 In this case, the parsing code above grabs the c_char when
2389 looking for the length (line 1690, roughly). it's the last
2390 testcase for parsing the kind params of a character variable.
2391 However, it's not actually the length. this seems like it
2393 To see if the user used a C interop kind, test the expr
2394 of the so called length, and see if it's C interoperable. */
2395 ts->is_c_interop = len->ts.is_iso_c;
2401 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2402 structure to the matched specification. This is necessary for FUNCTION and
2403 IMPLICIT statements.
2405 If implicit_flag is nonzero, then we don't check for the optional
2406 kind specification. Not doing so is needed for matching an IMPLICIT
2407 statement correctly. */
2410 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2412 char name[GFC_MAX_SYMBOL_LEN + 1];
2416 bool seen_deferred_kind, matched_type;
2418 /* A belt and braces check that the typespec is correctly being treated
2419 as a deferred characteristic association. */
2420 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2421 && (gfc_current_block ()->result->ts.kind == -1)
2422 && (ts->kind == -1);
2424 if (seen_deferred_kind)
2427 /* Clear the current binding label, in case one is given. */
2428 curr_binding_label[0] = '\0';
2430 if (gfc_match (" byte") == MATCH_YES)
2432 if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2436 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2438 gfc_error ("BYTE type used at %C "
2439 "is not available on the target machine");
2443 ts->type = BT_INTEGER;
2449 m = gfc_match (" type ( %n", name);
2450 matched_type = (m == MATCH_YES);
2452 if ((matched_type && strcmp ("integer", name) == 0)
2453 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2455 ts->type = BT_INTEGER;
2456 ts->kind = gfc_default_integer_kind;
2460 if ((matched_type && strcmp ("character", name) == 0)
2461 || (!matched_type && gfc_match (" character") == MATCH_YES))
2464 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2465 "intrinsic-type-spec at %C") == FAILURE)
2468 ts->type = BT_CHARACTER;
2469 if (implicit_flag == 0)
2470 m = gfc_match_char_spec (ts);
2474 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2480 if ((matched_type && strcmp ("real", name) == 0)
2481 || (!matched_type && gfc_match (" real") == MATCH_YES))
2484 ts->kind = gfc_default_real_kind;
2489 && (strcmp ("doubleprecision", name) == 0
2490 || (strcmp ("double", name) == 0
2491 && gfc_match (" precision") == MATCH_YES)))
2492 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2495 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2496 "intrinsic-type-spec at %C") == FAILURE)
2498 if (matched_type && gfc_match_char (')') != MATCH_YES)
2502 ts->kind = gfc_default_double_kind;
2506 if ((matched_type && strcmp ("complex", name) == 0)
2507 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2509 ts->type = BT_COMPLEX;
2510 ts->kind = gfc_default_complex_kind;
2515 && (strcmp ("doublecomplex", name) == 0
2516 || (strcmp ("double", name) == 0
2517 && gfc_match (" complex") == MATCH_YES)))
2518 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2520 if (gfc_notify_std (GFC_STD_GNU, "Extension: DOUBLE COMPLEX at %C")
2525 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2526 "intrinsic-type-spec at %C") == FAILURE)
2529 if (matched_type && gfc_match_char (')') != MATCH_YES)
2532 ts->type = BT_COMPLEX;
2533 ts->kind = gfc_default_double_kind;
2537 if ((matched_type && strcmp ("logical", name) == 0)
2538 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2540 ts->type = BT_LOGICAL;
2541 ts->kind = gfc_default_logical_kind;
2546 m = gfc_match_char (')');
2549 ts->type = BT_DERIVED;
2552 m = gfc_match (" class ( %n )", name);
2555 ts->type = BT_CLASS;
2557 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2562 /* Defer association of the derived type until the end of the
2563 specification block. However, if the derived type can be
2564 found, add it to the typespec. */
2565 if (gfc_matching_function)
2567 ts->u.derived = NULL;
2568 if (gfc_current_state () != COMP_INTERFACE
2569 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2570 ts->u.derived = sym;
2574 /* Search for the name but allow the components to be defined later. If
2575 type = -1, this typespec has been seen in a function declaration but
2576 the type could not be accessed at that point. */
2578 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2580 gfc_error ("Type name '%s' at %C is ambiguous", name);
2583 else if (ts->kind == -1)
2585 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2586 || gfc_current_ns->has_import_set;
2587 if (gfc_find_symbol (name, NULL, iface, &sym))
2589 gfc_error ("Type name '%s' at %C is ambiguous", name);
2598 if (sym->attr.flavor != FL_DERIVED
2599 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2602 gfc_set_sym_referenced (sym);
2603 ts->u.derived = sym;
2609 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2610 "intrinsic-type-spec at %C") == FAILURE)
2613 /* For all types except double, derived and character, look for an
2614 optional kind specifier. MATCH_NO is actually OK at this point. */
2615 if (implicit_flag == 1)
2617 if (matched_type && gfc_match_char (')') != MATCH_YES)
2623 if (gfc_current_form == FORM_FREE)
2625 c = gfc_peek_ascii_char ();
2626 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2627 && c != ':' && c != ',')
2629 if (matched_type && c == ')')
2631 gfc_next_ascii_char ();
2638 m = gfc_match_kind_spec (ts, false);
2639 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2640 m = gfc_match_old_kind_spec (ts);
2642 if (matched_type && gfc_match_char (')') != MATCH_YES)
2645 /* Defer association of the KIND expression of function results
2646 until after USE and IMPORT statements. */
2647 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2648 || gfc_matching_function)
2652 m = MATCH_YES; /* No kind specifier found. */
2658 /* Match an IMPLICIT NONE statement. Actually, this statement is
2659 already matched in parse.c, or we would not end up here in the
2660 first place. So the only thing we need to check, is if there is
2661 trailing garbage. If not, the match is successful. */
2664 gfc_match_implicit_none (void)
2666 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2670 /* Match the letter range(s) of an IMPLICIT statement. */
2673 match_implicit_range (void)
2679 cur_loc = gfc_current_locus;
2681 gfc_gobble_whitespace ();
2682 c = gfc_next_ascii_char ();
2685 gfc_error ("Missing character range in IMPLICIT at %C");
2692 gfc_gobble_whitespace ();
2693 c1 = gfc_next_ascii_char ();
2697 gfc_gobble_whitespace ();
2698 c = gfc_next_ascii_char ();
2703 inner = 0; /* Fall through. */
2710 gfc_gobble_whitespace ();
2711 c2 = gfc_next_ascii_char ();
2715 gfc_gobble_whitespace ();
2716 c = gfc_next_ascii_char ();
2718 if ((c != ',') && (c != ')'))
2731 gfc_error ("Letters must be in alphabetic order in "
2732 "IMPLICIT statement at %C");
2736 /* See if we can add the newly matched range to the pending
2737 implicits from this IMPLICIT statement. We do not check for
2738 conflicts with whatever earlier IMPLICIT statements may have
2739 set. This is done when we've successfully finished matching
2741 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2748 gfc_syntax_error (ST_IMPLICIT);
2750 gfc_current_locus = cur_loc;
2755 /* Match an IMPLICIT statement, storing the types for
2756 gfc_set_implicit() if the statement is accepted by the parser.
2757 There is a strange looking, but legal syntactic construction
2758 possible. It looks like:
2760 IMPLICIT INTEGER (a-b) (c-d)
2762 This is legal if "a-b" is a constant expression that happens to
2763 equal one of the legal kinds for integers. The real problem
2764 happens with an implicit specification that looks like:
2766 IMPLICIT INTEGER (a-b)
2768 In this case, a typespec matcher that is "greedy" (as most of the
2769 matchers are) gobbles the character range as a kindspec, leaving
2770 nothing left. We therefore have to go a bit more slowly in the
2771 matching process by inhibiting the kindspec checking during
2772 typespec matching and checking for a kind later. */
2775 gfc_match_implicit (void)
2784 /* We don't allow empty implicit statements. */
2785 if (gfc_match_eos () == MATCH_YES)
2787 gfc_error ("Empty IMPLICIT statement at %C");
2793 /* First cleanup. */
2794 gfc_clear_new_implicit ();
2796 /* A basic type is mandatory here. */
2797 m = gfc_match_decl_type_spec (&ts, 1);
2798 if (m == MATCH_ERROR)
2803 cur_loc = gfc_current_locus;
2804 m = match_implicit_range ();
2808 /* We may have <TYPE> (<RANGE>). */
2809 gfc_gobble_whitespace ();
2810 c = gfc_next_ascii_char ();
2811 if ((c == '\n') || (c == ','))
2813 /* Check for CHARACTER with no length parameter. */
2814 if (ts.type == BT_CHARACTER && !ts.u.cl)
2816 ts.kind = gfc_default_character_kind;
2817 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2818 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
2822 /* Record the Successful match. */
2823 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2828 gfc_current_locus = cur_loc;
2831 /* Discard the (incorrectly) matched range. */
2832 gfc_clear_new_implicit ();
2834 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2835 if (ts.type == BT_CHARACTER)
2836 m = gfc_match_char_spec (&ts);
2839 m = gfc_match_kind_spec (&ts, false);
2842 m = gfc_match_old_kind_spec (&ts);
2843 if (m == MATCH_ERROR)
2849 if (m == MATCH_ERROR)
2852 m = match_implicit_range ();
2853 if (m == MATCH_ERROR)
2858 gfc_gobble_whitespace ();
2859 c = gfc_next_ascii_char ();
2860 if ((c != '\n') && (c != ','))
2863 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2871 gfc_syntax_error (ST_IMPLICIT);
2879 gfc_match_import (void)
2881 char name[GFC_MAX_SYMBOL_LEN + 1];
2886 if (gfc_current_ns->proc_name == NULL
2887 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2889 gfc_error ("IMPORT statement at %C only permitted in "
2890 "an INTERFACE body");
2894 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2898 if (gfc_match_eos () == MATCH_YES)
2900 /* All host variables should be imported. */
2901 gfc_current_ns->has_import_set = 1;
2905 if (gfc_match (" ::") == MATCH_YES)
2907 if (gfc_match_eos () == MATCH_YES)
2909 gfc_error ("Expecting list of named entities at %C");
2916 m = gfc_match (" %n", name);
2920 if (gfc_current_ns->parent != NULL
2921 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2923 gfc_error ("Type name '%s' at %C is ambiguous", name);
2926 else if (gfc_current_ns->proc_name->ns->parent != NULL
2927 && gfc_find_symbol (name,
2928 gfc_current_ns->proc_name->ns->parent,
2931 gfc_error ("Type name '%s' at %C is ambiguous", name);
2937 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2938 "at %C - does not exist.", name);
2942 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2944 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2949 st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
2952 sym->attr.imported = 1;
2964 if (gfc_match_eos () == MATCH_YES)
2966 if (gfc_match_char (',') != MATCH_YES)
2973 gfc_error ("Syntax error in IMPORT statement at %C");
2978 /* A minimal implementation of gfc_match without whitespace, escape
2979 characters or variable arguments. Returns true if the next
2980 characters match the TARGET template exactly. */
2983 match_string_p (const char *target)
2987 for (p = target; *p; p++)
2988 if ((char) gfc_next_ascii_char () != *p)
2993 /* Matches an attribute specification including array specs. If
2994 successful, leaves the variables current_attr and current_as
2995 holding the specification. Also sets the colon_seen variable for
2996 later use by matchers associated with initializations.
2998 This subroutine is a little tricky in the sense that we don't know
2999 if we really have an attr-spec until we hit the double colon.
3000 Until that time, we can only return MATCH_NO. This forces us to
3001 check for duplicate specification at this level. */
3004 match_attr_spec (void)
3006 /* Modifiers that can exist in a type statement. */
3008 { GFC_DECL_BEGIN = 0,
3009 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3010 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3011 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3012 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3013 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3014 DECL_NONE, GFC_DECL_END /* Sentinel */
3018 /* GFC_DECL_END is the sentinel, index starts at 0. */
3019 #define NUM_DECL GFC_DECL_END
3021 locus start, seen_at[NUM_DECL];
3028 gfc_clear_attr (¤t_attr);
3029 start = gfc_current_locus;
3034 /* See if we get all of the keywords up to the final double colon. */
3035 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3043 gfc_gobble_whitespace ();
3045 ch = gfc_next_ascii_char ();
3048 /* This is the successful exit condition for the loop. */
3049 if (gfc_next_ascii_char () == ':')
3054 gfc_gobble_whitespace ();
3055 switch (gfc_peek_ascii_char ())
3058 gfc_next_ascii_char ();
3059 switch (gfc_next_ascii_char ())
3062 if (match_string_p ("locatable"))
3064 /* Matched "allocatable". */
3065 d = DECL_ALLOCATABLE;
3070 if (match_string_p ("ynchronous"))
3072 /* Matched "asynchronous". */
3073 d = DECL_ASYNCHRONOUS;
3080 /* Try and match the bind(c). */
3081 m = gfc_match_bind_c (NULL, true);
3084 else if (m == MATCH_ERROR)
3089 gfc_next_ascii_char ();
3090 if ('o' != gfc_next_ascii_char ())
3092 switch (gfc_next_ascii_char ())
3095 if (match_string_p ("imension"))
3097 d = DECL_CODIMENSION;
3101 if (match_string_p ("tiguous"))
3103 d = DECL_CONTIGUOUS;
3110 if (match_string_p ("dimension"))
3115 if (match_string_p ("external"))
3120 if (match_string_p ("int"))
3122 ch = gfc_next_ascii_char ();
3125 if (match_string_p ("nt"))
3127 /* Matched "intent". */
3128 /* TODO: Call match_intent_spec from here. */
3129 if (gfc_match (" ( in out )") == MATCH_YES)
3131 else if (gfc_match (" ( in )") == MATCH_YES)
3133 else if (gfc_match (" ( out )") == MATCH_YES)
3139 if (match_string_p ("insic"))
3141 /* Matched "intrinsic". */
3149 if (match_string_p ("optional"))
3154 gfc_next_ascii_char ();
3155 switch (gfc_next_ascii_char ())
3158 if (match_string_p ("rameter"))
3160 /* Matched "parameter". */
3166 if (match_string_p ("inter"))
3168 /* Matched "pointer". */
3174 ch = gfc_next_ascii_char ();
3177 if (match_string_p ("vate"))
3179 /* Matched "private". */
3185 if (match_string_p ("tected"))
3187 /* Matched "protected". */
3194 if (match_string_p ("blic"))
3196 /* Matched "public". */
3204 if (match_string_p ("save"))
3209 if (match_string_p ("target"))
3214 gfc_next_ascii_char ();
3215 ch = gfc_next_ascii_char ();
3218 if (match_string_p ("lue"))
3220 /* Matched "value". */
3226 if (match_string_p ("latile"))
3228 /* Matched "volatile". */
3236 /* No double colon and no recognizable decl_type, so assume that
3237 we've been looking at something else the whole time. */
3244 /* Check to make sure any parens are paired up correctly. */
3245 if (gfc_match_parens () == MATCH_ERROR)
3252 seen_at[d] = gfc_current_locus;
3254 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3256 gfc_array_spec *as = NULL;
3258 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3259 d == DECL_CODIMENSION);
3261 if (current_as == NULL)
3263 else if (m == MATCH_YES)
3265 merge_array_spec (as, current_as, false);
3271 if (d == DECL_CODIMENSION)
3272 gfc_error ("Missing codimension specification at %C");
3274 gfc_error ("Missing dimension specification at %C");
3278 if (m == MATCH_ERROR)
3283 /* Since we've seen a double colon, we have to be looking at an
3284 attr-spec. This means that we can now issue errors. */
3285 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3290 case DECL_ALLOCATABLE:
3291 attr = "ALLOCATABLE";
3293 case DECL_ASYNCHRONOUS:
3294 attr = "ASYNCHRONOUS";
3296 case DECL_CODIMENSION:
3297 attr = "CODIMENSION";
3299 case DECL_CONTIGUOUS:
3300 attr = "CONTIGUOUS";
3302 case DECL_DIMENSION:
3309 attr = "INTENT (IN)";
3312 attr = "INTENT (OUT)";
3315 attr = "INTENT (IN OUT)";
3317 case DECL_INTRINSIC:
3323 case DECL_PARAMETER:
3329 case DECL_PROTECTED:
3344 case DECL_IS_BIND_C:
3354 attr = NULL; /* This shouldn't happen. */
3357 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3362 /* Now that we've dealt with duplicate attributes, add the attributes
3363 to the current attribute. */
3364 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3369 if (gfc_current_state () == COMP_DERIVED
3370 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3371 && d != DECL_POINTER && d != DECL_PRIVATE
3372 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3374 if (d == DECL_ALLOCATABLE)
3376 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3377 "attribute at %C in a TYPE definition")
3386 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3393 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3394 && gfc_current_state () != COMP_MODULE)
3396 if (d == DECL_PRIVATE)
3400 if (gfc_current_state () == COMP_DERIVED
3401 && gfc_state_stack->previous
3402 && gfc_state_stack->previous->state == COMP_MODULE)
3404 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3405 "at %L in a TYPE definition", attr,
3415 gfc_error ("%s attribute at %L is not allowed outside of the "
3416 "specification part of a module", attr, &seen_at[d]);
3424 case DECL_ALLOCATABLE:
3425 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
3428 case DECL_ASYNCHRONOUS:
3429 if (gfc_notify_std (GFC_STD_F2003,
3430 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3434 t = gfc_add_asynchronous (¤t_attr, NULL, &seen_at[d]);
3437 case DECL_CODIMENSION:
3438 t = gfc_add_codimension (¤t_attr, NULL, &seen_at[d]);
3441 case DECL_CONTIGUOUS:
3442 if (gfc_notify_std (GFC_STD_F2008,
3443 "Fortran 2008: CONTIGUOUS attribute at %C")
3447 t = gfc_add_contiguous (¤t_attr, NULL, &seen_at[d]);
3450 case DECL_DIMENSION:
3451 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
3455 t = gfc_add_external (¤t_attr, &seen_at[d]);
3459 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
3463 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
3467 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
3470 case DECL_INTRINSIC:
3471 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
3475 t = gfc_add_optional (¤t_attr, &seen_at[d]);
3478 case DECL_PARAMETER:
3479 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
3483 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
3486 case DECL_PROTECTED:
3487 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3489 gfc_error ("PROTECTED at %C only allowed in specification "
3490 "part of a module");
3495 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3500 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
3504 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
3509 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
3514 t = gfc_add_save (¤t_attr, NULL, &seen_at[d]);
3518 t = gfc_add_target (¤t_attr, &seen_at[d]);
3521 case DECL_IS_BIND_C:
3522 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
3526 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3531 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
3535 if (gfc_notify_std (GFC_STD_F2003,
3536 "Fortran 2003: VOLATILE attribute at %C")
3540 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
3544 gfc_internal_error ("match_attr_spec(): Bad attribute");
3558 gfc_current_locus = start;
3559 gfc_free_array_spec (current_as);
3565 /* Set the binding label, dest_label, either with the binding label
3566 stored in the given gfc_typespec, ts, or if none was provided, it
3567 will be the symbol name in all lower case, as required by the draft
3568 (J3/04-007, section 15.4.1). If a binding label was given and
3569 there is more than one argument (num_idents), it is an error. */
3572 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3574 if (num_idents > 1 && has_name_equals)
3576 gfc_error ("Multiple identifiers provided with "
3577 "single NAME= specifier at %C");
3581 if (curr_binding_label[0] != '\0')
3583 /* Binding label given; store in temp holder til have sym. */
3584 strcpy (dest_label, curr_binding_label);
3588 /* No binding label given, and the NAME= specifier did not exist,
3589 which means there was no NAME="". */
3590 if (sym_name != NULL && has_name_equals == 0)
3591 strcpy (dest_label, sym_name);
3598 /* Set the status of the given common block as being BIND(C) or not,
3599 depending on the given parameter, is_bind_c. */
3602 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3604 com_block->is_bind_c = is_bind_c;
3609 /* Verify that the given gfc_typespec is for a C interoperable type. */
3612 verify_c_interop (gfc_typespec *ts)
3614 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3615 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3616 ? SUCCESS : FAILURE;
3617 else if (ts->is_c_interop != 1)
3624 /* Verify that the variables of a given common block, which has been
3625 defined with the attribute specifier bind(c), to be of a C
3626 interoperable type. Errors will be reported here, if
3630 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3632 gfc_symbol *curr_sym = NULL;
3633 gfc_try retval = SUCCESS;
3635 curr_sym = com_block->head;
3637 /* Make sure we have at least one symbol. */
3638 if (curr_sym == NULL)
3641 /* Here we know we have a symbol, so we'll execute this loop
3645 /* The second to last param, 1, says this is in a common block. */
3646 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3647 curr_sym = curr_sym->common_next;
3648 } while (curr_sym != NULL);
3654 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3655 an appropriate error message is reported. */
3658 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3659 int is_in_common, gfc_common_head *com_block)
3661 bool bind_c_function = false;
3662 gfc_try retval = SUCCESS;
3664 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3665 bind_c_function = true;
3667 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3669 tmp_sym = tmp_sym->result;
3670 /* Make sure it wasn't an implicitly typed result. */
3671 if (tmp_sym->attr.implicit_type)
3673 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3674 "%L may not be C interoperable", tmp_sym->name,
3675 &tmp_sym->declared_at);
3676 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3677 /* Mark it as C interoperable to prevent duplicate warnings. */
3678 tmp_sym->ts.is_c_interop = 1;
3679 tmp_sym->attr.is_c_interop = 1;
3683 /* Here, we know we have the bind(c) attribute, so if we have
3684 enough type info, then verify that it's a C interop kind.
3685 The info could be in the symbol already, or possibly still in
3686 the given ts (current_ts), so look in both. */
3687 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3689 if (verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3691 /* See if we're dealing with a sym in a common block or not. */
3692 if (is_in_common == 1)
3694 gfc_warning ("Variable '%s' in common block '%s' at %L "
3695 "may not be a C interoperable "
3696 "kind though common block '%s' is BIND(C)",
3697 tmp_sym->name, com_block->name,
3698 &(tmp_sym->declared_at), com_block->name);
3702 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3703 gfc_error ("Type declaration '%s' at %L is not C "
3704 "interoperable but it is BIND(C)",
3705 tmp_sym->name, &(tmp_sym->declared_at));
3707 gfc_warning ("Variable '%s' at %L "
3708 "may not be a C interoperable "
3709 "kind but it is bind(c)",
3710 tmp_sym->name, &(tmp_sym->declared_at));
3714 /* Variables declared w/in a common block can't be bind(c)
3715 since there's no way for C to see these variables, so there's
3716 semantically no reason for the attribute. */
3717 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3719 gfc_error ("Variable '%s' in common block '%s' at "
3720 "%L cannot be declared with BIND(C) "
3721 "since it is not a global",
3722 tmp_sym->name, com_block->name,
3723 &(tmp_sym->declared_at));
3727 /* Scalar variables that are bind(c) can not have the pointer
3728 or allocatable attributes. */
3729 if (tmp_sym->attr.is_bind_c == 1)
3731 if (tmp_sym->attr.pointer == 1)
3733 gfc_error ("Variable '%s' at %L cannot have both the "
3734 "POINTER and BIND(C) attributes",
3735 tmp_sym->name, &(tmp_sym->declared_at));
3739 if (tmp_sym->attr.allocatable == 1)
3741 gfc_error ("Variable '%s' at %L cannot have both the "
3742 "ALLOCATABLE and BIND(C) attributes",
3743 tmp_sym->name, &(tmp_sym->declared_at));
3749 /* If it is a BIND(C) function, make sure the return value is a
3750 scalar value. The previous tests in this function made sure
3751 the type is interoperable. */
3752 if (bind_c_function && tmp_sym->as != NULL)
3753 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3754 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3756 /* BIND(C) functions can not return a character string. */
3757 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3758 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
3759 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
3760 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
3761 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3762 "be a character string", tmp_sym->name,
3763 &(tmp_sym->declared_at));
3766 /* See if the symbol has been marked as private. If it has, make sure
3767 there is no binding label and warn the user if there is one. */
3768 if (tmp_sym->attr.access == ACCESS_PRIVATE
3769 && tmp_sym->binding_label[0] != '\0')
3770 /* Use gfc_warning_now because we won't say that the symbol fails
3771 just because of this. */
3772 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3773 "given the binding label '%s'", tmp_sym->name,
3774 &(tmp_sym->declared_at), tmp_sym->binding_label);
3780 /* Set the appropriate fields for a symbol that's been declared as
3781 BIND(C) (the is_bind_c flag and the binding label), and verify that
3782 the type is C interoperable. Errors are reported by the functions
3783 used to set/test these fields. */
3786 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3788 gfc_try retval = SUCCESS;
3790 /* TODO: Do we need to make sure the vars aren't marked private? */
3792 /* Set the is_bind_c bit in symbol_attribute. */
3793 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3795 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3796 num_idents) != SUCCESS)
3803 /* Set the fields marking the given common block as BIND(C), including
3804 a binding label, and report any errors encountered. */
3807 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3809 gfc_try retval = SUCCESS;
3811 /* destLabel, common name, typespec (which may have binding label). */
3812 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3816 /* Set the given common block (com_block) to being bind(c) (1). */
3817 set_com_block_bind_c (com_block, 1);
3823 /* Retrieve the list of one or more identifiers that the given bind(c)
3824 attribute applies to. */
3827 get_bind_c_idents (void)
3829 char name[GFC_MAX_SYMBOL_LEN + 1];
3831 gfc_symbol *tmp_sym = NULL;
3833 gfc_common_head *com_block = NULL;
3835 if (gfc_match_name (name) == MATCH_YES)
3837 found_id = MATCH_YES;
3838 gfc_get_ha_symbol (name, &tmp_sym);
3840 else if (match_common_name (name) == MATCH_YES)
3842 found_id = MATCH_YES;
3843 com_block = gfc_get_common (name, 0);
3847 gfc_error ("Need either entity or common block name for "
3848 "attribute specification statement at %C");
3852 /* Save the current identifier and look for more. */
3855 /* Increment the number of identifiers found for this spec stmt. */
3858 /* Make sure we have a sym or com block, and verify that it can
3859 be bind(c). Set the appropriate field(s) and look for more
3861 if (tmp_sym != NULL || com_block != NULL)
3863 if (tmp_sym != NULL)
3865 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3871 if (set_verify_bind_c_com_block(com_block, num_idents)
3876 /* Look to see if we have another identifier. */
3878 if (gfc_match_eos () == MATCH_YES)
3879 found_id = MATCH_NO;
3880 else if (gfc_match_char (',') != MATCH_YES)
3881 found_id = MATCH_NO;
3882 else if (gfc_match_name (name) == MATCH_YES)
3884 found_id = MATCH_YES;
3885 gfc_get_ha_symbol (name, &tmp_sym);
3887 else if (match_common_name (name) == MATCH_YES)
3889 found_id = MATCH_YES;
3890 com_block = gfc_get_common (name, 0);
3894 gfc_error ("Missing entity or common block name for "
3895 "attribute specification statement at %C");
3901 gfc_internal_error ("Missing symbol");
3903 } while (found_id == MATCH_YES);
3905 /* if we get here we were successful */
3910 /* Try and match a BIND(C) attribute specification statement. */
3913 gfc_match_bind_c_stmt (void)
3915 match found_match = MATCH_NO;
3920 /* This may not be necessary. */
3922 /* Clear the temporary binding label holder. */
3923 curr_binding_label[0] = '\0';
3925 /* Look for the bind(c). */
3926 found_match = gfc_match_bind_c (NULL, true);
3928 if (found_match == MATCH_YES)
3930 /* Look for the :: now, but it is not required. */
3933 /* Get the identifier(s) that needs to be updated. This may need to
3934 change to hand the flag(s) for the attr specified so all identifiers
3935 found can have all appropriate parts updated (assuming that the same
3936 spec stmt can have multiple attrs, such as both bind(c) and
3938 if (get_bind_c_idents () != SUCCESS)
3939 /* Error message should have printed already. */
3947 /* Match a data declaration statement. */
3950 gfc_match_data_decl (void)
3956 num_idents_on_line = 0;
3958 m = gfc_match_decl_type_spec (¤t_ts, 0);
3962 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
3963 && gfc_current_state () != COMP_DERIVED)
3965 sym = gfc_use_derived (current_ts.u.derived);
3973 current_ts.u.derived = sym;
3976 m = match_attr_spec ();
3977 if (m == MATCH_ERROR)
3983 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
3984 && current_ts.u.derived->components == NULL
3985 && !current_ts.u.derived->attr.zero_comp)
3988 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
3991 gfc_find_symbol (current_ts.u.derived->name,
3992 current_ts.u.derived->ns->parent, 1, &sym);
3994 /* Any symbol that we find had better be a type definition
3995 which has its components defined. */
3996 if (sym != NULL && sym->attr.flavor == FL_DERIVED
3997 && (current_ts.u.derived->components != NULL
3998 || current_ts.u.derived->attr.zero_comp))
4001 /* Now we have an error, which we signal, and then fix up
4002 because the knock-on is plain and simple confusing. */
4003 gfc_error_now ("Derived type at %C has not been previously defined "
4004 "and so cannot appear in a derived type definition");
4005 current_attr.pointer = 1;
4010 /* If we have an old-style character declaration, and no new-style
4011 attribute specifications, then there a comma is optional between
4012 the type specification and the variable list. */
4013 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4014 gfc_match_char (',');
4016 /* Give the types/attributes to symbols that follow. Give the element
4017 a number so that repeat character length expressions can be copied. */
4021 num_idents_on_line++;
4022 m = variable_decl (elem++);
4023 if (m == MATCH_ERROR)
4028 if (gfc_match_eos () == MATCH_YES)
4030 if (gfc_match_char (',') != MATCH_YES)
4034 if (gfc_error_flag_test () == 0)
4035 gfc_error ("Syntax error in data declaration at %C");
4038 gfc_free_data_all (gfc_current_ns);
4041 gfc_free_array_spec (current_as);
4047 /* Match a prefix associated with a function or subroutine
4048 declaration. If the typespec pointer is nonnull, then a typespec
4049 can be matched. Note that if nothing matches, MATCH_YES is
4050 returned (the null string was matched). */
4053 gfc_match_prefix (gfc_typespec *ts)
4059 gfc_clear_attr (¤t_attr);
4061 seen_impure = false;
4063 gcc_assert (!gfc_matching_prefix);
4064 gfc_matching_prefix = true;
4068 found_prefix = false;
4070 if (!seen_type && ts != NULL
4071 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4072 && gfc_match_space () == MATCH_YES)
4076 found_prefix = true;
4079 if (gfc_match ("elemental% ") == MATCH_YES)
4081 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
4084 found_prefix = true;
4087 if (gfc_match ("pure% ") == MATCH_YES)
4089 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
4092 found_prefix = true;
4095 if (gfc_match ("recursive% ") == MATCH_YES)
4097 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
4100 found_prefix = true;
4103 /* IMPURE is a somewhat special case, as it needs not set an actual
4104 attribute but rather only prevents ELEMENTAL routines from being
4105 automatically PURE. */
4106 if (gfc_match ("impure% ") == MATCH_YES)
4108 if (gfc_notify_std (GFC_STD_F2008,
4109 "Fortran 2008: IMPURE procedure at %C")
4114 found_prefix = true;
4117 while (found_prefix);
4119 /* IMPURE and PURE must not both appear, of course. */
4120 if (seen_impure && current_attr.pure)
4122 gfc_error ("PURE and IMPURE must not appear both at %C");
4126 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4127 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4129 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
4133 /* At this point, the next item is not a prefix. */
4134 gcc_assert (gfc_matching_prefix);
4135 gfc_matching_prefix = false;
4139 gcc_assert (gfc_matching_prefix);
4140 gfc_matching_prefix = false;
4145 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4148 copy_prefix (symbol_attribute *dest, locus *where)
4150 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
4153 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
4156 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
4163 /* Match a formal argument list. */
4166 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4168 gfc_formal_arglist *head, *tail, *p, *q;
4169 char name[GFC_MAX_SYMBOL_LEN + 1];
4175 if (gfc_match_char ('(') != MATCH_YES)
4182 if (gfc_match_char (')') == MATCH_YES)
4187 if (gfc_match_char ('*') == MATCH_YES)
4191 m = gfc_match_name (name);
4195 if (gfc_get_symbol (name, NULL, &sym))
4199 p = gfc_get_formal_arglist ();
4211 /* We don't add the VARIABLE flavor because the name could be a
4212 dummy procedure. We don't apply these attributes to formal
4213 arguments of statement functions. */
4214 if (sym != NULL && !st_flag
4215 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
4216 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
4222 /* The name of a program unit can be in a different namespace,
4223 so check for it explicitly. After the statement is accepted,
4224 the name is checked for especially in gfc_get_symbol(). */
4225 if (gfc_new_block != NULL && sym != NULL
4226 && strcmp (sym->name, gfc_new_block->name) == 0)
4228 gfc_error ("Name '%s' at %C is the name of the procedure",
4234 if (gfc_match_char (')') == MATCH_YES)
4237 m = gfc_match_char (',');
4240 gfc_error ("Unexpected junk in formal argument list at %C");
4246 /* Check for duplicate symbols in the formal argument list. */
4249 for (p = head; p->next; p = p->next)
4254 for (q = p->next; q; q = q->next)
4255 if (p->sym == q->sym)
4257 gfc_error ("Duplicate symbol '%s' in formal argument list "
4258 "at %C", p->sym->name);
4266 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
4276 gfc_free_formal_arglist (head);
4281 /* Match a RESULT specification following a function declaration or
4282 ENTRY statement. Also matches the end-of-statement. */
4285 match_result (gfc_symbol *function, gfc_symbol **result)
4287 char name[GFC_MAX_SYMBOL_LEN + 1];
4291 if (gfc_match (" result (") != MATCH_YES)
4294 m = gfc_match_name (name);
4298 /* Get the right paren, and that's it because there could be the
4299 bind(c) attribute after the result clause. */
4300 if (gfc_match_char(')') != MATCH_YES)
4302 /* TODO: should report the missing right paren here. */
4306 if (strcmp (function->name, name) == 0)
4308 gfc_error ("RESULT variable at %C must be different than function name");
4312 if (gfc_get_symbol (name, NULL, &r))
4315 if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4324 /* Match a function suffix, which could be a combination of a result
4325 clause and BIND(C), either one, or neither. The draft does not
4326 require them to come in a specific order. */
4329 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4331 match is_bind_c; /* Found bind(c). */
4332 match is_result; /* Found result clause. */
4333 match found_match; /* Status of whether we've found a good match. */
4334 char peek_char; /* Character we're going to peek at. */
4335 bool allow_binding_name;
4337 /* Initialize to having found nothing. */
4338 found_match = MATCH_NO;
4339 is_bind_c = MATCH_NO;
4340 is_result = MATCH_NO;
4342 /* Get the next char to narrow between result and bind(c). */
4343 gfc_gobble_whitespace ();
4344 peek_char = gfc_peek_ascii_char ();
4346 /* C binding names are not allowed for internal procedures. */
4347 if (gfc_current_state () == COMP_CONTAINS
4348 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4349 allow_binding_name = false;
4351 allow_binding_name = true;
4356 /* Look for result clause. */
4357 is_result = match_result (sym, result);
4358 if (is_result == MATCH_YES)
4360 /* Now see if there is a bind(c) after it. */
4361 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4362 /* We've found the result clause and possibly bind(c). */
4363 found_match = MATCH_YES;
4366 /* This should only be MATCH_ERROR. */
4367 found_match = is_result;
4370 /* Look for bind(c) first. */
4371 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4372 if (is_bind_c == MATCH_YES)
4374 /* Now see if a result clause followed it. */
4375 is_result = match_result (sym, result);
4376 found_match = MATCH_YES;
4380 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4381 found_match = MATCH_ERROR;
4385 gfc_error ("Unexpected junk after function declaration at %C");
4386 found_match = MATCH_ERROR;
4390 if (is_bind_c == MATCH_YES)
4392 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4393 if (gfc_current_state () == COMP_CONTAINS
4394 && sym->ns->proc_name->attr.flavor != FL_MODULE
4395 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4396 "at %L may not be specified for an internal "
4397 "procedure", &gfc_current_locus)
4401 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4410 /* Procedure pointer return value without RESULT statement:
4411 Add "hidden" result variable named "ppr@". */
4414 add_hidden_procptr_result (gfc_symbol *sym)
4418 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4421 /* First usage case: PROCEDURE and EXTERNAL statements. */
4422 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4423 && strcmp (gfc_current_block ()->name, sym->name) == 0
4424 && sym->attr.external;
4425 /* Second usage case: INTERFACE statements. */
4426 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4427 && gfc_state_stack->previous->state == COMP_FUNCTION
4428 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4434 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4438 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4439 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4440 st2->n.sym = stree->n.sym;
4442 sym->result = stree->n.sym;
4444 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4445 sym->result->attr.pointer = sym->attr.pointer;
4446 sym->result->attr.external = sym->attr.external;
4447 sym->result->attr.referenced = sym->attr.referenced;
4448 sym->result->ts = sym->ts;
4449 sym->attr.proc_pointer = 0;
4450 sym->attr.pointer = 0;
4451 sym->attr.external = 0;
4452 if (sym->result->attr.external && sym->result->attr.pointer)
4454 sym->result->attr.pointer = 0;
4455 sym->result->attr.proc_pointer = 1;
4458 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4460 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4461 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4462 && sym->result && sym->result != sym && sym->result->attr.external
4463 && sym == gfc_current_ns->proc_name
4464 && sym == sym->result->ns->proc_name
4465 && strcmp ("ppr@", sym->result->name) == 0)
4467 sym->result->attr.proc_pointer = 1;
4468 sym->attr.pointer = 0;
4476 /* Match the interface for a PROCEDURE declaration,
4477 including brackets (R1212). */
4480 match_procedure_interface (gfc_symbol **proc_if)
4484 locus old_loc, entry_loc;
4485 gfc_namespace *old_ns = gfc_current_ns;
4486 char name[GFC_MAX_SYMBOL_LEN + 1];
4488 old_loc = entry_loc = gfc_current_locus;
4489 gfc_clear_ts (¤t_ts);
4491 if (gfc_match (" (") != MATCH_YES)
4493 gfc_current_locus = entry_loc;
4497 /* Get the type spec. for the procedure interface. */
4498 old_loc = gfc_current_locus;
4499 m = gfc_match_decl_type_spec (¤t_ts, 0);
4500 gfc_gobble_whitespace ();
4501 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4504 if (m == MATCH_ERROR)
4507 /* Procedure interface is itself a procedure. */
4508 gfc_current_locus = old_loc;
4509 m = gfc_match_name (name);
4511 /* First look to see if it is already accessible in the current
4512 namespace because it is use associated or contained. */
4514 if (gfc_find_sym_tree (name, NULL, 0, &st))
4517 /* If it is still not found, then try the parent namespace, if it
4518 exists and create the symbol there if it is still not found. */
4519 if (gfc_current_ns->parent)
4520 gfc_current_ns = gfc_current_ns->parent;
4521 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4524 gfc_current_ns = old_ns;
4525 *proc_if = st->n.sym;
4527 /* Various interface checks. */
4531 /* Resolve interface if possible. That way, attr.procedure is only set
4532 if it is declared by a later procedure-declaration-stmt, which is
4533 invalid per C1212. */
4534 while ((*proc_if)->ts.interface)
4535 *proc_if = (*proc_if)->ts.interface;
4537 if ((*proc_if)->generic)
4539 gfc_error ("Interface '%s' at %C may not be generic",
4543 if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
4545 gfc_error ("Interface '%s' at %C may not be a statement function",
4549 /* Handle intrinsic procedures. */
4550 if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4551 || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4552 && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4553 || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4554 (*proc_if)->attr.intrinsic = 1;
4555 if ((*proc_if)->attr.intrinsic
4556 && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
4558 gfc_error ("Intrinsic procedure '%s' not allowed "
4559 "in PROCEDURE statement at %C", (*proc_if)->name);
4565 if (gfc_match (" )") != MATCH_YES)
4567 gfc_current_locus = entry_loc;
4575 /* Match a PROCEDURE declaration (R1211). */
4578 match_procedure_decl (void)
4581 gfc_symbol *sym, *proc_if = NULL;
4583 gfc_expr *initializer = NULL;
4585 /* Parse interface (with brackets). */
4586 m = match_procedure_interface (&proc_if);
4590 /* Parse attributes (with colons). */
4591 m = match_attr_spec();
4592 if (m == MATCH_ERROR)
4595 /* Get procedure symbols. */
4598 m = gfc_match_symbol (&sym, 0);
4601 else if (m == MATCH_ERROR)
4604 /* Add current_attr to the symbol attributes. */
4605 if (gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
4608 if (sym->attr.is_bind_c)
4610 /* Check for C1218. */
4611 if (!proc_if || !proc_if->attr.is_bind_c)
4613 gfc_error ("BIND(C) attribute at %C requires "
4614 "an interface with BIND(C)");
4617 /* Check for C1217. */
4618 if (has_name_equals && sym->attr.pointer)
4620 gfc_error ("BIND(C) procedure with NAME may not have "
4621 "POINTER attribute at %C");
4624 if (has_name_equals && sym->attr.dummy)
4626 gfc_error ("Dummy procedure at %C may not have "
4627 "BIND(C) attribute with NAME");
4630 /* Set binding label for BIND(C). */
4631 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4635 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4638 if (add_hidden_procptr_result (sym) == SUCCESS)
4641 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4644 /* Set interface. */
4645 if (proc_if != NULL)
4647 if (sym->ts.type != BT_UNKNOWN)
4649 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4650 sym->name, &gfc_current_locus,
4651 gfc_basic_typename (sym->ts.type));
4654 sym->ts.interface = proc_if;
4655 sym->attr.untyped = 1;
4656 sym->attr.if_source = IFSRC_IFBODY;
4658 else if (current_ts.type != BT_UNKNOWN)
4660 if (gfc_add_type (sym, ¤t_ts, &gfc_current_locus) == FAILURE)
4662 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4663 sym->ts.interface->ts = current_ts;
4664 sym->ts.interface->attr.function = 1;
4665 sym->attr.function = sym->ts.interface->attr.function;
4666 sym->attr.if_source = IFSRC_UNKNOWN;
4669 if (gfc_match (" =>") == MATCH_YES)
4671 if (!current_attr.pointer)
4673 gfc_error ("Initialization at %C isn't for a pointer variable");
4678 m = gfc_match_null (&initializer);
4681 gfc_error ("Pointer initialization requires a NULL() at %C");
4685 if (gfc_pure (NULL))
4687 gfc_error ("Initialization of pointer at %C is not allowed in "
4688 "a PURE procedure");
4695 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4701 gfc_set_sym_referenced (sym);
4703 if (gfc_match_eos () == MATCH_YES)
4705 if (gfc_match_char (',') != MATCH_YES)
4710 gfc_error ("Syntax error in PROCEDURE statement at %C");
4714 /* Free stuff up and return. */
4715 gfc_free_expr (initializer);
4721 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4724 /* Match a procedure pointer component declaration (R445). */
4727 match_ppc_decl (void)
4730 gfc_symbol *proc_if = NULL;
4734 gfc_expr *initializer = NULL;
4735 gfc_typebound_proc* tb;
4736 char name[GFC_MAX_SYMBOL_LEN + 1];
4738 /* Parse interface (with brackets). */
4739 m = match_procedure_interface (&proc_if);
4743 /* Parse attributes. */
4744 tb = XCNEW (gfc_typebound_proc);
4745 tb->where = gfc_current_locus;
4746 m = match_binding_attributes (tb, false, true);
4747 if (m == MATCH_ERROR)
4750 gfc_clear_attr (¤t_attr);
4751 current_attr.procedure = 1;
4752 current_attr.proc_pointer = 1;
4753 current_attr.access = tb->access;
4754 current_attr.flavor = FL_PROCEDURE;
4756 /* Match the colons (required). */
4757 if (gfc_match (" ::") != MATCH_YES)
4759 gfc_error ("Expected '::' after binding-attributes at %C");
4763 /* Check for C450. */
4764 if (!tb->nopass && proc_if == NULL)
4766 gfc_error("NOPASS or explicit interface required at %C");
4770 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure pointer "
4771 "component at %C") == FAILURE)
4774 /* Match PPC names. */
4778 m = gfc_match_name (name);
4781 else if (m == MATCH_ERROR)
4784 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
4787 /* Add current_attr to the symbol attributes. */
4788 if (gfc_copy_attr (&c->attr, ¤t_attr, NULL) == FAILURE)
4791 if (gfc_add_external (&c->attr, NULL) == FAILURE)
4794 if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
4799 /* Set interface. */
4800 if (proc_if != NULL)
4802 c->ts.interface = proc_if;
4803 c->attr.untyped = 1;
4804 c->attr.if_source = IFSRC_IFBODY;
4806 else if (ts.type != BT_UNKNOWN)
4809 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4810 c->ts.interface->ts = ts;
4811 c->ts.interface->attr.function = 1;
4812 c->attr.function = c->ts.interface->attr.function;
4813 c->attr.if_source = IFSRC_UNKNOWN;
4816 if (gfc_match (" =>") == MATCH_YES)
4818 m = gfc_match_null (&initializer);
4821 gfc_error ("Pointer initialization requires a NULL() at %C");
4824 if (gfc_pure (NULL))
4826 gfc_error ("Initialization of pointer at %C is not allowed in "
4827 "a PURE procedure");
4832 gfc_free_expr (initializer);
4835 c->initializer = initializer;
4838 if (gfc_match_eos () == MATCH_YES)
4840 if (gfc_match_char (',') != MATCH_YES)
4845 gfc_error ("Syntax error in procedure pointer component at %C");
4850 /* Match a PROCEDURE declaration inside an interface (R1206). */
4853 match_procedure_in_interface (void)
4857 char name[GFC_MAX_SYMBOL_LEN + 1];
4859 if (current_interface.type == INTERFACE_NAMELESS
4860 || current_interface.type == INTERFACE_ABSTRACT)
4862 gfc_error ("PROCEDURE at %C must be in a generic interface");
4868 m = gfc_match_name (name);
4871 else if (m == MATCH_ERROR)
4873 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4876 if (gfc_add_interface (sym) == FAILURE)
4879 if (gfc_match_eos () == MATCH_YES)
4881 if (gfc_match_char (',') != MATCH_YES)
4888 gfc_error ("Syntax error in PROCEDURE statement at %C");
4893 /* General matcher for PROCEDURE declarations. */
4895 static match match_procedure_in_type (void);
4898 gfc_match_procedure (void)
4902 switch (gfc_current_state ())
4907 case COMP_SUBROUTINE:
4909 m = match_procedure_decl ();
4911 case COMP_INTERFACE:
4912 m = match_procedure_in_interface ();
4915 m = match_ppc_decl ();
4917 case COMP_DERIVED_CONTAINS:
4918 m = match_procedure_in_type ();
4927 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4935 /* Warn if a matched procedure has the same name as an intrinsic; this is
4936 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4937 parser-state-stack to find out whether we're in a module. */
4940 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
4944 in_module = (gfc_state_stack->previous
4945 && gfc_state_stack->previous->state == COMP_MODULE);
4947 gfc_warn_intrinsic_shadow (sym, in_module, func);
4951 /* Match a function declaration. */
4954 gfc_match_function_decl (void)
4956 char name[GFC_MAX_SYMBOL_LEN + 1];
4957 gfc_symbol *sym, *result;
4961 match found_match; /* Status returned by match func. */
4963 if (gfc_current_state () != COMP_NONE
4964 && gfc_current_state () != COMP_INTERFACE
4965 && gfc_current_state () != COMP_CONTAINS)
4968 gfc_clear_ts (¤t_ts);
4970 old_loc = gfc_current_locus;
4972 m = gfc_match_prefix (¤t_ts);
4975 gfc_current_locus = old_loc;
4979 if (gfc_match ("function% %n", name) != MATCH_YES)
4981 gfc_current_locus = old_loc;
4984 if (get_proc_name (name, &sym, false))
4987 if (add_hidden_procptr_result (sym) == SUCCESS)
4990 gfc_new_block = sym;
4992 m = gfc_match_formal_arglist (sym, 0, 0);
4995 gfc_error ("Expected formal argument list in function "
4996 "definition at %C");
5000 else if (m == MATCH_ERROR)
5005 /* According to the draft, the bind(c) and result clause can
5006 come in either order after the formal_arg_list (i.e., either
5007 can be first, both can exist together or by themselves or neither
5008 one). Therefore, the match_result can't match the end of the
5009 string, and check for the bind(c) or result clause in either order. */
5010 found_match = gfc_match_eos ();
5012 /* Make sure that it isn't already declared as BIND(C). If it is, it
5013 must have been marked BIND(C) with a BIND(C) attribute and that is
5014 not allowed for procedures. */
5015 if (sym->attr.is_bind_c == 1)
5017 sym->attr.is_bind_c = 0;
5018 if (sym->old_symbol != NULL)
5019 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5020 "variables or common blocks",
5021 &(sym->old_symbol->declared_at));
5023 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5024 "variables or common blocks", &gfc_current_locus);
5027 if (found_match != MATCH_YES)
5029 /* If we haven't found the end-of-statement, look for a suffix. */
5030 suffix_match = gfc_match_suffix (sym, &result);
5031 if (suffix_match == MATCH_YES)
5032 /* Need to get the eos now. */
5033 found_match = gfc_match_eos ();
5035 found_match = suffix_match;
5038 if(found_match != MATCH_YES)
5042 /* Make changes to the symbol. */
5045 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
5048 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
5049 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5052 /* Delay matching the function characteristics until after the
5053 specification block by signalling kind=-1. */
5054 sym->declared_at = old_loc;
5055 if (current_ts.type != BT_UNKNOWN)
5056 current_ts.kind = -1;
5058 current_ts.kind = 0;
5062 if (current_ts.type != BT_UNKNOWN
5063 && gfc_add_type (sym, ¤t_ts, &gfc_current_locus) == FAILURE)
5069 if (current_ts.type != BT_UNKNOWN
5070 && gfc_add_type (result, ¤t_ts, &gfc_current_locus)
5073 sym->result = result;
5076 /* Warn if this procedure has the same name as an intrinsic. */
5077 warn_intrinsic_shadow (sym, true);
5083 gfc_current_locus = old_loc;
5088 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5089 pass the name of the entry, rather than the gfc_current_block name, and
5090 to return false upon finding an existing global entry. */
5093 add_global_entry (const char *name, int sub)
5096 enum gfc_symbol_type type;
5098 s = gfc_get_gsymbol(name);
5099 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5102 || (s->type != GSYM_UNKNOWN
5103 && s->type != type))
5104 gfc_global_used(s, NULL);
5108 s->where = gfc_current_locus;
5110 s->ns = gfc_current_ns;
5117 /* Match an ENTRY statement. */
5120 gfc_match_entry (void)
5125 char name[GFC_MAX_SYMBOL_LEN + 1];
5126 gfc_compile_state state;
5130 bool module_procedure;
5134 m = gfc_match_name (name);
5138 if (gfc_notify_std (GFC_STD_F2008_OBS, "Fortran 2008 obsolescent feature: "
5139 "ENTRY statement at %C") == FAILURE)
5142 state = gfc_current_state ();
5143 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5148 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5151 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5153 case COMP_BLOCK_DATA:
5154 gfc_error ("ENTRY statement at %C cannot appear within "
5157 case COMP_INTERFACE:
5158 gfc_error ("ENTRY statement at %C cannot appear within "
5162 gfc_error ("ENTRY statement at %C cannot appear within "
5163 "a DERIVED TYPE block");
5166 gfc_error ("ENTRY statement at %C cannot appear within "
5167 "an IF-THEN block");
5170 gfc_error ("ENTRY statement at %C cannot appear within "
5174 gfc_error ("ENTRY statement at %C cannot appear within "
5178 gfc_error ("ENTRY statement at %C cannot appear within "
5182 gfc_error ("ENTRY statement at %C cannot appear within "
5186 gfc_error ("ENTRY statement at %C cannot appear within "
5187 "a contained subprogram");
5190 gfc_internal_error ("gfc_match_entry(): Bad state");
5195 module_procedure = gfc_current_ns->parent != NULL
5196 && gfc_current_ns->parent->proc_name
5197 && gfc_current_ns->parent->proc_name->attr.flavor
5200 if (gfc_current_ns->parent != NULL
5201 && gfc_current_ns->parent->proc_name
5202 && !module_procedure)
5204 gfc_error("ENTRY statement at %C cannot appear in a "
5205 "contained procedure");
5209 /* Module function entries need special care in get_proc_name
5210 because previous references within the function will have
5211 created symbols attached to the current namespace. */
5212 if (get_proc_name (name, &entry,
5213 gfc_current_ns->parent != NULL
5214 && module_procedure))
5217 proc = gfc_current_block ();
5219 /* Make sure that it isn't already declared as BIND(C). If it is, it
5220 must have been marked BIND(C) with a BIND(C) attribute and that is
5221 not allowed for procedures. */
5222 if (entry->attr.is_bind_c == 1)
5224 entry->attr.is_bind_c = 0;
5225 if (entry->old_symbol != NULL)
5226 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5227 "variables or common blocks",
5228 &(entry->old_symbol->declared_at));
5230 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5231 "variables or common blocks", &gfc_current_locus);
5234 /* Check what next non-whitespace character is so we can tell if there
5235 is the required parens if we have a BIND(C). */
5236 gfc_gobble_whitespace ();
5237 peek_char = gfc_peek_ascii_char ();
5239 if (state == COMP_SUBROUTINE)
5241 /* An entry in a subroutine. */
5242 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
5245 m = gfc_match_formal_arglist (entry, 0, 1);
5249 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5250 never be an internal procedure. */
5251 is_bind_c = gfc_match_bind_c (entry, true);
5252 if (is_bind_c == MATCH_ERROR)
5254 if (is_bind_c == MATCH_YES)
5256 if (peek_char != '(')
5258 gfc_error ("Missing required parentheses before BIND(C) at %C");
5261 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
5266 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5267 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
5272 /* An entry in a function.
5273 We need to take special care because writing
5278 ENTRY f() RESULT (r)
5280 ENTRY f RESULT (r). */
5281 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
5284 old_loc = gfc_current_locus;
5285 if (gfc_match_eos () == MATCH_YES)
5287 gfc_current_locus = old_loc;
5288 /* Match the empty argument list, and add the interface to
5290 m = gfc_match_formal_arglist (entry, 0, 1);
5293 m = gfc_match_formal_arglist (entry, 0, 0);
5300 if (gfc_match_eos () == MATCH_YES)
5302 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5303 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5306 entry->result = entry;
5310 m = gfc_match_suffix (entry, &result);
5312 gfc_syntax_error (ST_ENTRY);
5318 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
5319 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
5320 || gfc_add_function (&entry->attr, result->name, NULL)
5323 entry->result = result;
5327 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5328 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5330 entry->result = entry;
5335 if (gfc_match_eos () != MATCH_YES)
5337 gfc_syntax_error (ST_ENTRY);
5341 entry->attr.recursive = proc->attr.recursive;
5342 entry->attr.elemental = proc->attr.elemental;
5343 entry->attr.pure = proc->attr.pure;
5345 el = gfc_get_entry_list ();
5347 el->next = gfc_current_ns->entries;
5348 gfc_current_ns->entries = el;
5350 el->id = el->next->id + 1;
5354 new_st.op = EXEC_ENTRY;
5355 new_st.ext.entry = el;
5361 /* Match a subroutine statement, including optional prefixes. */
5364 gfc_match_subroutine (void)
5366 char name[GFC_MAX_SYMBOL_LEN + 1];
5371 bool allow_binding_name;
5373 if (gfc_current_state () != COMP_NONE
5374 && gfc_current_state () != COMP_INTERFACE
5375 && gfc_current_state () != COMP_CONTAINS)
5378 m = gfc_match_prefix (NULL);
5382 m = gfc_match ("subroutine% %n", name);
5386 if (get_proc_name (name, &sym, false))
5389 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5390 the symbol existed before. */
5391 sym->declared_at = gfc_current_locus;
5393 if (add_hidden_procptr_result (sym) == SUCCESS)
5396 gfc_new_block = sym;
5398 /* Check what next non-whitespace character is so we can tell if there
5399 is the required parens if we have a BIND(C). */
5400 gfc_gobble_whitespace ();
5401 peek_char = gfc_peek_ascii_char ();
5403 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5406 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5409 /* Make sure that it isn't already declared as BIND(C). If it is, it
5410 must have been marked BIND(C) with a BIND(C) attribute and that is
5411 not allowed for procedures. */
5412 if (sym->attr.is_bind_c == 1)
5414 sym->attr.is_bind_c = 0;
5415 if (sym->old_symbol != NULL)
5416 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5417 "variables or common blocks",
5418 &(sym->old_symbol->declared_at));
5420 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5421 "variables or common blocks", &gfc_current_locus);
5424 /* C binding names are not allowed for internal procedures. */
5425 if (gfc_current_state () == COMP_CONTAINS
5426 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5427 allow_binding_name = false;
5429 allow_binding_name = true;
5431 /* Here, we are just checking if it has the bind(c) attribute, and if
5432 so, then we need to make sure it's all correct. If it doesn't,
5433 we still need to continue matching the rest of the subroutine line. */
5434 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5435 if (is_bind_c == MATCH_ERROR)
5437 /* There was an attempt at the bind(c), but it was wrong. An
5438 error message should have been printed w/in the gfc_match_bind_c
5439 so here we'll just return the MATCH_ERROR. */
5443 if (is_bind_c == MATCH_YES)
5445 /* The following is allowed in the Fortran 2008 draft. */
5446 if (gfc_current_state () == COMP_CONTAINS
5447 && sym->ns->proc_name->attr.flavor != FL_MODULE
5448 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5449 "at %L may not be specified for an internal "
5450 "procedure", &gfc_current_locus)
5454 if (peek_char != '(')
5456 gfc_error ("Missing required parentheses before BIND(C) at %C");
5459 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5464 if (gfc_match_eos () != MATCH_YES)
5466 gfc_syntax_error (ST_SUBROUTINE);
5470 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5473 /* Warn if it has the same name as an intrinsic. */
5474 warn_intrinsic_shadow (sym, false);
5480 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5481 given, and set the binding label in either the given symbol (if not
5482 NULL), or in the current_ts. The symbol may be NULL because we may
5483 encounter the BIND(C) before the declaration itself. Return
5484 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5485 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5486 or MATCH_YES if the specifier was correct and the binding label and
5487 bind(c) fields were set correctly for the given symbol or the
5488 current_ts. If allow_binding_name is false, no binding name may be
5492 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5494 /* binding label, if exists */
5495 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
5499 /* Initialize the flag that specifies whether we encountered a NAME=
5500 specifier or not. */
5501 has_name_equals = 0;
5503 /* Init the first char to nil so we can catch if we don't have
5504 the label (name attr) or the symbol name yet. */
5505 binding_label[0] = '\0';
5507 /* This much we have to be able to match, in this order, if
5508 there is a bind(c) label. */
5509 if (gfc_match (" bind ( c ") != MATCH_YES)
5512 /* Now see if there is a binding label, or if we've reached the
5513 end of the bind(c) attribute without one. */
5514 if (gfc_match_char (',') == MATCH_YES)
5516 if (gfc_match (" name = ") != MATCH_YES)
5518 gfc_error ("Syntax error in NAME= specifier for binding label "
5520 /* should give an error message here */
5524 has_name_equals = 1;
5526 /* Get the opening quote. */
5527 double_quote = MATCH_YES;
5528 single_quote = MATCH_YES;
5529 double_quote = gfc_match_char ('"');
5530 if (double_quote != MATCH_YES)
5531 single_quote = gfc_match_char ('\'');
5532 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5534 gfc_error ("Syntax error in NAME= specifier for binding label "
5539 /* Grab the binding label, using functions that will not lower
5540 case the names automatically. */
5541 if (gfc_match_name_C (binding_label) != MATCH_YES)
5544 /* Get the closing quotation. */
5545 if (double_quote == MATCH_YES)
5547 if (gfc_match_char ('"') != MATCH_YES)
5549 gfc_error ("Missing closing quote '\"' for binding label at %C");
5550 /* User started string with '"' so looked to match it. */
5556 if (gfc_match_char ('\'') != MATCH_YES)
5558 gfc_error ("Missing closing quote '\'' for binding label at %C");
5559 /* User started string with "'" char. */
5565 /* Get the required right paren. */
5566 if (gfc_match_char (')') != MATCH_YES)
5568 gfc_error ("Missing closing paren for binding label at %C");
5572 if (has_name_equals && !allow_binding_name)
5574 gfc_error ("No binding name is allowed in BIND(C) at %C");
5578 if (has_name_equals && sym != NULL && sym->attr.dummy)
5580 gfc_error ("For dummy procedure %s, no binding name is "
5581 "allowed in BIND(C) at %C", sym->name);
5586 /* Save the binding label to the symbol. If sym is null, we're
5587 probably matching the typespec attributes of a declaration and
5588 haven't gotten the name yet, and therefore, no symbol yet. */
5589 if (binding_label[0] != '\0')
5593 strcpy (sym->binding_label, binding_label);
5596 strcpy (curr_binding_label, binding_label);
5598 else if (allow_binding_name)
5600 /* No binding label, but if symbol isn't null, we
5601 can set the label for it here.
5602 If name="" or allow_binding_name is false, no C binding name is
5604 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5605 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
5608 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5609 && current_interface.type == INTERFACE_ABSTRACT)
5611 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5619 /* Return nonzero if we're currently compiling a contained procedure. */
5622 contained_procedure (void)
5624 gfc_state_data *s = gfc_state_stack;
5626 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5627 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5633 /* Set the kind of each enumerator. The kind is selected such that it is
5634 interoperable with the corresponding C enumeration type, making
5635 sure that -fshort-enums is honored. */
5640 enumerator_history *current_history = NULL;
5644 if (max_enum == NULL || enum_history == NULL)
5647 if (!flag_short_enums)
5653 kind = gfc_integer_kinds[i++].kind;
5655 while (kind < gfc_c_int_kind
5656 && gfc_check_integer_range (max_enum->initializer->value.integer,
5659 current_history = enum_history;
5660 while (current_history != NULL)
5662 current_history->sym->ts.kind = kind;
5663 current_history = current_history->next;
5668 /* Match any of the various end-block statements. Returns the type of
5669 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5670 and END BLOCK statements cannot be replaced by a single END statement. */
5673 gfc_match_end (gfc_statement *st)
5675 char name[GFC_MAX_SYMBOL_LEN + 1];
5676 gfc_compile_state state;
5678 const char *block_name;
5683 old_loc = gfc_current_locus;
5684 if (gfc_match ("end") != MATCH_YES)
5687 state = gfc_current_state ();
5688 block_name = gfc_current_block () == NULL
5689 ? NULL : gfc_current_block ()->name;
5693 case COMP_ASSOCIATE:
5695 if (!strcmp (block_name, "block@"))
5700 case COMP_DERIVED_CONTAINS:
5701 state = gfc_state_stack->previous->state;
5702 block_name = gfc_state_stack->previous->sym == NULL
5703 ? NULL : gfc_state_stack->previous->sym->name;
5714 *st = ST_END_PROGRAM;
5715 target = " program";
5719 case COMP_SUBROUTINE:
5720 *st = ST_END_SUBROUTINE;
5721 target = " subroutine";
5722 eos_ok = !contained_procedure ();
5726 *st = ST_END_FUNCTION;
5727 target = " function";
5728 eos_ok = !contained_procedure ();
5731 case COMP_BLOCK_DATA:
5732 *st = ST_END_BLOCK_DATA;
5733 target = " block data";
5738 *st = ST_END_MODULE;
5743 case COMP_INTERFACE:
5744 *st = ST_END_INTERFACE;
5745 target = " interface";
5750 case COMP_DERIVED_CONTAINS:
5756 case COMP_ASSOCIATE:
5757 *st = ST_END_ASSOCIATE;
5758 target = " associate";
5781 *st = ST_END_CRITICAL;
5782 target = " critical";
5787 case COMP_SELECT_TYPE:
5788 *st = ST_END_SELECT;
5794 *st = ST_END_FORALL;
5809 last_initializer = NULL;
5811 gfc_free_enum_history ();
5815 gfc_error ("Unexpected END statement at %C");
5819 if (gfc_match_eos () == MATCH_YES)
5821 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
5823 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: END statement "
5824 "instead of %s statement at %L",
5825 gfc_ascii_statement (*st), &old_loc) == FAILURE)
5830 /* We would have required END [something]. */
5831 gfc_error ("%s statement expected at %L",
5832 gfc_ascii_statement (*st), &old_loc);
5839 /* Verify that we've got the sort of end-block that we're expecting. */
5840 if (gfc_match (target) != MATCH_YES)
5842 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5846 /* If we're at the end, make sure a block name wasn't required. */
5847 if (gfc_match_eos () == MATCH_YES)
5850 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5851 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
5852 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
5858 gfc_error ("Expected block name of '%s' in %s statement at %C",
5859 block_name, gfc_ascii_statement (*st));
5864 /* END INTERFACE has a special handler for its several possible endings. */
5865 if (*st == ST_END_INTERFACE)
5866 return gfc_match_end_interface ();
5868 /* We haven't hit the end of statement, so what is left must be an
5870 m = gfc_match_space ();
5872 m = gfc_match_name (name);
5875 gfc_error ("Expected terminating name at %C");
5879 if (block_name == NULL)
5882 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
5884 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5885 gfc_ascii_statement (*st));
5888 /* Procedure pointer as function result. */
5889 else if (strcmp (block_name, "ppr@") == 0
5890 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
5892 gfc_error ("Expected label '%s' for %s statement at %C",
5893 gfc_current_block ()->ns->proc_name->name,
5894 gfc_ascii_statement (*st));
5898 if (gfc_match_eos () == MATCH_YES)
5902 gfc_syntax_error (*st);
5905 gfc_current_locus = old_loc;
5911 /***************** Attribute declaration statements ****************/
5913 /* Set the attribute of a single variable. */
5918 char name[GFC_MAX_SYMBOL_LEN + 1];
5926 m = gfc_match_name (name);
5930 if (find_special (name, &sym, false))
5933 var_locus = gfc_current_locus;
5935 /* Deal with possible array specification for certain attributes. */
5936 if (current_attr.dimension
5937 || current_attr.codimension
5938 || current_attr.allocatable
5939 || current_attr.pointer
5940 || current_attr.target)
5942 m = gfc_match_array_spec (&as, !current_attr.codimension,
5943 !current_attr.dimension
5944 && !current_attr.pointer
5945 && !current_attr.target);
5946 if (m == MATCH_ERROR)
5949 if (current_attr.dimension && m == MATCH_NO)
5951 gfc_error ("Missing array specification at %L in DIMENSION "
5952 "statement", &var_locus);
5957 if (current_attr.dimension && sym->value)
5959 gfc_error ("Dimensions specified for %s at %L after its "
5960 "initialisation", sym->name, &var_locus);
5965 if (current_attr.codimension && m == MATCH_NO)
5967 gfc_error ("Missing array specification at %L in CODIMENSION "
5968 "statement", &var_locus);
5973 if ((current_attr.allocatable || current_attr.pointer)
5974 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
5976 gfc_error ("Array specification must be deferred at %L", &var_locus);
5982 /* Update symbol table. DIMENSION attribute is set in
5983 gfc_set_array_spec(). For CLASS variables, this must be applied
5984 to the first component, or '$data' field. */
5985 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
5987 if (gfc_copy_attr (&CLASS_DATA (sym)->attr, ¤t_attr,&var_locus)
5996 if (current_attr.dimension == 0 && current_attr.codimension == 0
5997 && gfc_copy_attr (&sym->attr, ¤t_attr, &var_locus) == FAILURE)
6004 if (sym->ts.type == BT_CLASS && !sym->attr.class_ok
6005 && (sym->attr.class_ok = sym->attr.class_ok || current_attr.allocatable
6006 || current_attr.pointer))
6007 gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
6009 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
6015 if (sym->attr.cray_pointee && sym->as != NULL)
6017 /* Fix the array spec. */
6018 m = gfc_mod_pointee_as (sym->as);
6019 if (m == MATCH_ERROR)
6023 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
6029 if ((current_attr.external || current_attr.intrinsic)
6030 && sym->attr.flavor != FL_PROCEDURE
6031 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6037 add_hidden_procptr_result (sym);
6042 gfc_free_array_spec (as);
6047 /* Generic attribute declaration subroutine. Used for attributes that
6048 just have a list of names. */
6055 /* Gobble the optional double colon, by simply ignoring the result
6065 if (gfc_match_eos () == MATCH_YES)
6071 if (gfc_match_char (',') != MATCH_YES)
6073 gfc_error ("Unexpected character in variable list at %C");
6083 /* This routine matches Cray Pointer declarations of the form:
6084 pointer ( <pointer>, <pointee> )
6086 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6087 The pointer, if already declared, should be an integer. Otherwise, we
6088 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6089 be either a scalar, or an array declaration. No space is allocated for
6090 the pointee. For the statement
6091 pointer (ipt, ar(10))
6092 any subsequent uses of ar will be translated (in C-notation) as
6093 ar(i) => ((<type> *) ipt)(i)
6094 After gimplification, pointee variable will disappear in the code. */
6097 cray_pointer_decl (void)
6100 gfc_array_spec *as = NULL;
6101 gfc_symbol *cptr; /* Pointer symbol. */
6102 gfc_symbol *cpte; /* Pointee symbol. */
6108 if (gfc_match_char ('(') != MATCH_YES)
6110 gfc_error ("Expected '(' at %C");
6114 /* Match pointer. */
6115 var_locus = gfc_current_locus;
6116 gfc_clear_attr (¤t_attr);
6117 gfc_add_cray_pointer (¤t_attr, &var_locus);
6118 current_ts.type = BT_INTEGER;
6119 current_ts.kind = gfc_index_integer_kind;
6121 m = gfc_match_symbol (&cptr, 0);
6124 gfc_error ("Expected variable name at %C");
6128 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
6131 gfc_set_sym_referenced (cptr);
6133 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6135 cptr->ts.type = BT_INTEGER;
6136 cptr->ts.kind = gfc_index_integer_kind;
6138 else if (cptr->ts.type != BT_INTEGER)
6140 gfc_error ("Cray pointer at %C must be an integer");
6143 else if (cptr->ts.kind < gfc_index_integer_kind)
6144 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6145 " memory addresses require %d bytes",
6146 cptr->ts.kind, gfc_index_integer_kind);
6148 if (gfc_match_char (',') != MATCH_YES)
6150 gfc_error ("Expected \",\" at %C");
6154 /* Match Pointee. */
6155 var_locus = gfc_current_locus;
6156 gfc_clear_attr (¤t_attr);
6157 gfc_add_cray_pointee (¤t_attr, &var_locus);
6158 current_ts.type = BT_UNKNOWN;
6159 current_ts.kind = 0;
6161 m = gfc_match_symbol (&cpte, 0);
6164 gfc_error ("Expected variable name at %C");
6168 /* Check for an optional array spec. */
6169 m = gfc_match_array_spec (&as, true, false);
6170 if (m == MATCH_ERROR)
6172 gfc_free_array_spec (as);
6175 else if (m == MATCH_NO)
6177 gfc_free_array_spec (as);
6181 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
6184 gfc_set_sym_referenced (cpte);
6186 if (cpte->as == NULL)
6188 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
6189 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6191 else if (as != NULL)
6193 gfc_error ("Duplicate array spec for Cray pointee at %C");
6194 gfc_free_array_spec (as);
6200 if (cpte->as != NULL)
6202 /* Fix array spec. */
6203 m = gfc_mod_pointee_as (cpte->as);
6204 if (m == MATCH_ERROR)
6208 /* Point the Pointee at the Pointer. */
6209 cpte->cp_pointer = cptr;
6211 if (gfc_match_char (')') != MATCH_YES)
6213 gfc_error ("Expected \")\" at %C");
6216 m = gfc_match_char (',');
6218 done = true; /* Stop searching for more declarations. */
6222 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6223 || gfc_match_eos () != MATCH_YES)
6225 gfc_error ("Expected \",\" or end of statement at %C");
6233 gfc_match_external (void)
6236 gfc_clear_attr (¤t_attr);
6237 current_attr.external = 1;
6239 return attr_decl ();
6244 gfc_match_intent (void)
6248 /* This is not allowed within a BLOCK construct! */
6249 if (gfc_current_state () == COMP_BLOCK)
6251 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6255 intent = match_intent_spec ();
6256 if (intent == INTENT_UNKNOWN)
6259 gfc_clear_attr (¤t_attr);
6260 current_attr.intent = intent;
6262 return attr_decl ();
6267 gfc_match_intrinsic (void)
6270 gfc_clear_attr (¤t_attr);
6271 current_attr.intrinsic = 1;
6273 return attr_decl ();
6278 gfc_match_optional (void)
6280 /* This is not allowed within a BLOCK construct! */
6281 if (gfc_current_state () == COMP_BLOCK)
6283 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6287 gfc_clear_attr (¤t_attr);
6288 current_attr.optional = 1;
6290 return attr_decl ();
6295 gfc_match_pointer (void)
6297 gfc_gobble_whitespace ();
6298 if (gfc_peek_ascii_char () == '(')
6300 if (!gfc_option.flag_cray_pointer)
6302 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6306 return cray_pointer_decl ();
6310 gfc_clear_attr (¤t_attr);
6311 current_attr.pointer = 1;
6313 return attr_decl ();
6319 gfc_match_allocatable (void)
6321 gfc_clear_attr (¤t_attr);
6322 current_attr.allocatable = 1;
6324 return attr_decl ();
6329 gfc_match_codimension (void)
6331 gfc_clear_attr (¤t_attr);
6332 current_attr.codimension = 1;
6334 return attr_decl ();
6339 gfc_match_contiguous (void)
6341 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTIGUOUS statement at %C")
6345 gfc_clear_attr (¤t_attr);
6346 current_attr.contiguous = 1;
6348 return attr_decl ();
6353 gfc_match_dimension (void)
6355 gfc_clear_attr (¤t_attr);
6356 current_attr.dimension = 1;
6358 return attr_decl ();
6363 gfc_match_target (void)
6365 gfc_clear_attr (¤t_attr);
6366 current_attr.target = 1;
6368 return attr_decl ();
6372 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6376 access_attr_decl (gfc_statement st)
6378 char name[GFC_MAX_SYMBOL_LEN + 1];
6379 interface_type type;
6382 gfc_intrinsic_op op;
6385 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6390 m = gfc_match_generic_spec (&type, name, &op);
6393 if (m == MATCH_ERROR)
6398 case INTERFACE_NAMELESS:
6399 case INTERFACE_ABSTRACT:
6402 case INTERFACE_GENERIC:
6403 if (gfc_get_symbol (name, NULL, &sym))
6406 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
6407 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6408 sym->name, NULL) == FAILURE)
6413 case INTERFACE_INTRINSIC_OP:
6414 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6416 gfc_current_ns->operator_access[op] =
6417 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6421 gfc_error ("Access specification of the %s operator at %C has "
6422 "already been specified", gfc_op2string (op));
6428 case INTERFACE_USER_OP:
6429 uop = gfc_get_uop (name);
6431 if (uop->access == ACCESS_UNKNOWN)
6433 uop->access = (st == ST_PUBLIC)
6434 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6438 gfc_error ("Access specification of the .%s. operator at %C "
6439 "has already been specified", sym->name);
6446 if (gfc_match_char (',') == MATCH_NO)
6450 if (gfc_match_eos () != MATCH_YES)
6455 gfc_syntax_error (st);
6463 gfc_match_protected (void)
6468 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6470 gfc_error ("PROTECTED at %C only allowed in specification "
6471 "part of a module");
6476 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
6480 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6485 if (gfc_match_eos () == MATCH_YES)
6490 m = gfc_match_symbol (&sym, 0);
6494 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6507 if (gfc_match_eos () == MATCH_YES)
6509 if (gfc_match_char (',') != MATCH_YES)
6516 gfc_error ("Syntax error in PROTECTED statement at %C");
6521 /* The PRIVATE statement is a bit weird in that it can be an attribute
6522 declaration, but also works as a standalone statement inside of a
6523 type declaration or a module. */
6526 gfc_match_private (gfc_statement *st)
6529 if (gfc_match ("private") != MATCH_YES)
6532 if (gfc_current_state () != COMP_MODULE
6533 && !(gfc_current_state () == COMP_DERIVED
6534 && gfc_state_stack->previous
6535 && gfc_state_stack->previous->state == COMP_MODULE)
6536 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6537 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6538 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6540 gfc_error ("PRIVATE statement at %C is only allowed in the "
6541 "specification part of a module");
6545 if (gfc_current_state () == COMP_DERIVED)
6547 if (gfc_match_eos () == MATCH_YES)
6553 gfc_syntax_error (ST_PRIVATE);
6557 if (gfc_match_eos () == MATCH_YES)
6564 return access_attr_decl (ST_PRIVATE);
6569 gfc_match_public (gfc_statement *st)
6572 if (gfc_match ("public") != MATCH_YES)
6575 if (gfc_current_state () != COMP_MODULE)
6577 gfc_error ("PUBLIC statement at %C is only allowed in the "
6578 "specification part of a module");
6582 if (gfc_match_eos () == MATCH_YES)
6589 return access_attr_decl (ST_PUBLIC);
6593 /* Workhorse for gfc_match_parameter. */
6603 m = gfc_match_symbol (&sym, 0);
6605 gfc_error ("Expected variable name at %C in PARAMETER statement");
6610 if (gfc_match_char ('=') == MATCH_NO)
6612 gfc_error ("Expected = sign in PARAMETER statement at %C");
6616 m = gfc_match_init_expr (&init);
6618 gfc_error ("Expected expression at %C in PARAMETER statement");
6622 if (sym->ts.type == BT_UNKNOWN
6623 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6629 if (gfc_check_assign_symbol (sym, init) == FAILURE
6630 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6638 gfc_error ("Initializing already initialized variable at %C");
6643 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
6644 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6647 gfc_free_expr (init);
6652 /* Match a parameter statement, with the weird syntax that these have. */
6655 gfc_match_parameter (void)
6659 if (gfc_match_char ('(') == MATCH_NO)
6668 if (gfc_match (" )%t") == MATCH_YES)
6671 if (gfc_match_char (',') != MATCH_YES)
6673 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6683 /* Save statements have a special syntax. */
6686 gfc_match_save (void)
6688 char n[GFC_MAX_SYMBOL_LEN+1];
6693 if (gfc_match_eos () == MATCH_YES)
6695 if (gfc_current_ns->seen_save)
6697 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6698 "follows previous SAVE statement")
6703 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6707 if (gfc_current_ns->save_all)
6709 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6710 "blanket SAVE statement")
6719 m = gfc_match_symbol (&sym, 0);
6723 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
6735 m = gfc_match (" / %n /", &n);
6736 if (m == MATCH_ERROR)
6741 c = gfc_get_common (n, 0);
6744 gfc_current_ns->seen_save = 1;
6747 if (gfc_match_eos () == MATCH_YES)
6749 if (gfc_match_char (',') != MATCH_YES)
6756 gfc_error ("Syntax error in SAVE statement at %C");
6762 gfc_match_value (void)
6767 /* This is not allowed within a BLOCK construct! */
6768 if (gfc_current_state () == COMP_BLOCK)
6770 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
6774 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
6778 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6783 if (gfc_match_eos () == MATCH_YES)
6788 m = gfc_match_symbol (&sym, 0);
6792 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
6805 if (gfc_match_eos () == MATCH_YES)
6807 if (gfc_match_char (',') != MATCH_YES)
6814 gfc_error ("Syntax error in VALUE statement at %C");
6820 gfc_match_volatile (void)
6825 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
6829 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6834 if (gfc_match_eos () == MATCH_YES)
6839 /* VOLATILE is special because it can be added to host-associated
6840 symbols locally. Except for coarrays. */
6841 m = gfc_match_symbol (&sym, 1);
6845 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
6846 for variable in a BLOCK which is defined outside of the BLOCK. */
6847 if (sym->ns != gfc_current_ns && sym->attr.codimension)
6849 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
6850 "%C, which is use-/host-associated", sym->name);
6853 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6866 if (gfc_match_eos () == MATCH_YES)
6868 if (gfc_match_char (',') != MATCH_YES)
6875 gfc_error ("Syntax error in VOLATILE statement at %C");
6881 gfc_match_asynchronous (void)
6886 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS statement at %C")
6890 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6895 if (gfc_match_eos () == MATCH_YES)
6900 /* ASYNCHRONOUS is special because it can be added to host-associated
6902 m = gfc_match_symbol (&sym, 1);
6906 if (gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus)
6919 if (gfc_match_eos () == MATCH_YES)
6921 if (gfc_match_char (',') != MATCH_YES)
6928 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
6933 /* Match a module procedure statement. Note that we have to modify
6934 symbols in the parent's namespace because the current one was there
6935 to receive symbols that are in an interface's formal argument list. */
6938 gfc_match_modproc (void)
6940 char name[GFC_MAX_SYMBOL_LEN + 1];
6943 gfc_namespace *module_ns;
6944 gfc_interface *old_interface_head, *interface;
6946 if (gfc_state_stack->state != COMP_INTERFACE
6947 || gfc_state_stack->previous == NULL
6948 || current_interface.type == INTERFACE_NAMELESS
6949 || current_interface.type == INTERFACE_ABSTRACT)
6951 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6956 module_ns = gfc_current_ns->parent;
6957 for (; module_ns; module_ns = module_ns->parent)
6958 if (module_ns->proc_name->attr.flavor == FL_MODULE
6959 || module_ns->proc_name->attr.flavor == FL_PROGRAM
6960 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
6961 && !module_ns->proc_name->attr.contained))
6964 if (module_ns == NULL)
6967 /* Store the current state of the interface. We will need it if we
6968 end up with a syntax error and need to recover. */
6969 old_interface_head = gfc_current_interface_head ();
6973 locus old_locus = gfc_current_locus;
6976 m = gfc_match_name (name);
6982 /* Check for syntax error before starting to add symbols to the
6983 current namespace. */
6984 if (gfc_match_eos () == MATCH_YES)
6986 if (!last && gfc_match_char (',') != MATCH_YES)
6989 /* Now we're sure the syntax is valid, we process this item
6991 if (gfc_get_symbol (name, module_ns, &sym))
6994 if (sym->attr.intrinsic)
6996 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
6997 "PROCEDURE", &old_locus);
7001 if (sym->attr.proc != PROC_MODULE
7002 && gfc_add_procedure (&sym->attr, PROC_MODULE,
7003 sym->name, NULL) == FAILURE)
7006 if (gfc_add_interface (sym) == FAILURE)
7009 sym->attr.mod_proc = 1;
7010 sym->declared_at = old_locus;
7019 /* Restore the previous state of the interface. */
7020 interface = gfc_current_interface_head ();
7021 gfc_set_current_interface_head (old_interface_head);
7023 /* Free the new interfaces. */
7024 while (interface != old_interface_head)
7026 gfc_interface *i = interface->next;
7027 gfc_free (interface);
7031 /* And issue a syntax error. */
7032 gfc_syntax_error (ST_MODULE_PROC);
7037 /* Check a derived type that is being extended. */
7039 check_extended_derived_type (char *name)
7041 gfc_symbol *extended;
7043 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7045 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7051 gfc_error ("No such symbol in TYPE definition at %C");
7055 if (extended->attr.flavor != FL_DERIVED)
7057 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7058 "derived type", name);
7062 if (extended->attr.is_bind_c)
7064 gfc_error ("'%s' cannot be extended at %C because it "
7065 "is BIND(C)", extended->name);
7069 if (extended->attr.sequence)
7071 gfc_error ("'%s' cannot be extended at %C because it "
7072 "is a SEQUENCE type", extended->name);
7080 /* Match the optional attribute specifiers for a type declaration.
7081 Return MATCH_ERROR if an error is encountered in one of the handled
7082 attributes (public, private, bind(c)), MATCH_NO if what's found is
7083 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7084 checking on attribute conflicts needs to be done. */
7087 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7089 /* See if the derived type is marked as private. */
7090 if (gfc_match (" , private") == MATCH_YES)
7092 if (gfc_current_state () != COMP_MODULE)
7094 gfc_error ("Derived type at %C can only be PRIVATE in the "
7095 "specification part of a module");
7099 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
7102 else if (gfc_match (" , public") == MATCH_YES)
7104 if (gfc_current_state () != COMP_MODULE)
7106 gfc_error ("Derived type at %C can only be PUBLIC in the "
7107 "specification part of a module");
7111 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
7114 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7116 /* If the type is defined to be bind(c) it then needs to make
7117 sure that all fields are interoperable. This will
7118 need to be a semantic check on the finished derived type.
7119 See 15.2.3 (lines 9-12) of F2003 draft. */
7120 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
7123 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7125 else if (gfc_match (" , abstract") == MATCH_YES)
7127 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
7131 if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
7134 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
7136 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
7142 /* If we get here, something matched. */
7147 /* Assign a hash value for a derived type. The algorithm is that of
7148 SDBM. The hashed string is '[module_name #] derived_name'. */
7150 hash_value (gfc_symbol *sym)
7152 unsigned int hash = 0;
7156 /* Hash of the module or procedure name. */
7157 if (sym->module != NULL)
7159 else if (sym->ns && sym->ns->proc_name
7160 && sym->ns->proc_name->attr.flavor == FL_MODULE)
7161 c = sym->ns->proc_name->name;
7168 for (i = 0; i < len; i++, c++)
7169 hash = (hash << 6) + (hash << 16) - hash + (*c);
7171 /* Disambiguate between 'a' in 'aa' and 'aa' in 'a'. */
7172 hash = (hash << 6) + (hash << 16) - hash + '#';
7175 /* Hash of the derived type name. */
7176 len = strlen (sym->name);
7178 for (i = 0; i < len; i++, c++)
7179 hash = (hash << 6) + (hash << 16) - hash + (*c);
7181 /* Return the hash but take the modulus for the sake of module read,
7182 even though this slightly increases the chance of collision. */
7183 return (hash % 100000000);
7187 /* Match the beginning of a derived type declaration. If a type name
7188 was the result of a function, then it is possible to have a symbol
7189 already to be known as a derived type yet have no components. */
7192 gfc_match_derived_decl (void)
7194 char name[GFC_MAX_SYMBOL_LEN + 1];
7195 char parent[GFC_MAX_SYMBOL_LEN + 1];
7196 symbol_attribute attr;
7198 gfc_symbol *extended;
7200 match is_type_attr_spec = MATCH_NO;
7201 bool seen_attr = false;
7203 if (gfc_current_state () == COMP_DERIVED)
7208 gfc_clear_attr (&attr);
7213 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7214 if (is_type_attr_spec == MATCH_ERROR)
7216 if (is_type_attr_spec == MATCH_YES)
7218 } while (is_type_attr_spec == MATCH_YES);
7220 /* Deal with derived type extensions. The extension attribute has
7221 been added to 'attr' but now the parent type must be found and
7224 extended = check_extended_derived_type (parent);
7226 if (parent[0] && !extended)
7229 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7231 gfc_error ("Expected :: in TYPE definition at %C");
7235 m = gfc_match (" %n%t", name);
7239 /* Make sure the name is not the name of an intrinsic type. */
7240 if (gfc_is_intrinsic_typename (name))
7242 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7247 if (gfc_get_symbol (name, NULL, &sym))
7250 if (sym->ts.type != BT_UNKNOWN)
7252 gfc_error ("Derived type name '%s' at %C already has a basic type "
7253 "of %s", sym->name, gfc_typename (&sym->ts));
7257 /* The symbol may already have the derived attribute without the
7258 components. The ways this can happen is via a function
7259 definition, an INTRINSIC statement or a subtype in another
7260 derived type that is a pointer. The first part of the AND clause
7261 is true if the symbol is not the return value of a function. */
7262 if (sym->attr.flavor != FL_DERIVED
7263 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
7266 if (sym->components != NULL || sym->attr.zero_comp)
7268 gfc_error ("Derived type definition of '%s' at %C has already been "
7269 "defined", sym->name);
7273 if (attr.access != ACCESS_UNKNOWN
7274 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
7277 /* See if the derived type was labeled as bind(c). */
7278 if (attr.is_bind_c != 0)
7279 sym->attr.is_bind_c = attr.is_bind_c;
7281 /* Construct the f2k_derived namespace if it is not yet there. */
7282 if (!sym->f2k_derived)
7283 sym->f2k_derived = gfc_get_namespace (NULL, 0);
7285 if (extended && !sym->components)
7290 /* Add the extended derived type as the first component. */
7291 gfc_add_component (sym, parent, &p);
7293 gfc_set_sym_referenced (extended);
7295 p->ts.type = BT_DERIVED;
7296 p->ts.u.derived = extended;
7297 p->initializer = gfc_default_initializer (&p->ts);
7299 /* Set extension level. */
7300 if (extended->attr.extension == 255)
7302 /* Since the extension field is 8 bit wide, we can only have
7303 up to 255 extension levels. */
7304 gfc_error ("Maximum extension level reached with type '%s' at %L",
7305 extended->name, &extended->declared_at);
7308 sym->attr.extension = extended->attr.extension + 1;
7310 /* Provide the links between the extended type and its extension. */
7311 if (!extended->f2k_derived)
7312 extended->f2k_derived = gfc_get_namespace (NULL, 0);
7313 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7317 if (!sym->hash_value)
7318 /* Set the hash for the compound name for this type. */
7319 sym->hash_value = hash_value (sym);
7321 /* Take over the ABSTRACT attribute. */
7322 sym->attr.abstract = attr.abstract;
7324 gfc_new_block = sym;
7330 /* Cray Pointees can be declared as:
7331 pointer (ipt, a (n,m,...,*)) */
7334 gfc_mod_pointee_as (gfc_array_spec *as)
7336 as->cray_pointee = true; /* This will be useful to know later. */
7337 if (as->type == AS_ASSUMED_SIZE)
7338 as->cp_was_assumed = true;
7339 else if (as->type == AS_ASSUMED_SHAPE)
7341 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7348 /* Match the enum definition statement, here we are trying to match
7349 the first line of enum definition statement.
7350 Returns MATCH_YES if match is found. */
7353 gfc_match_enum (void)
7357 m = gfc_match_eos ();
7361 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
7369 /* Returns an initializer whose value is one higher than the value of the
7370 LAST_INITIALIZER argument. If the argument is NULL, the
7371 initializers value will be set to zero. The initializer's kind
7372 will be set to gfc_c_int_kind.
7374 If -fshort-enums is given, the appropriate kind will be selected
7375 later after all enumerators have been parsed. A warning is issued
7376 here if an initializer exceeds gfc_c_int_kind. */
7379 enum_initializer (gfc_expr *last_initializer, locus where)
7382 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7384 mpz_init (result->value.integer);
7386 if (last_initializer != NULL)
7388 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7389 result->where = last_initializer->where;
7391 if (gfc_check_integer_range (result->value.integer,
7392 gfc_c_int_kind) != ARITH_OK)
7394 gfc_error ("Enumerator exceeds the C integer type at %C");
7400 /* Control comes here, if it's the very first enumerator and no
7401 initializer has been given. It will be initialized to zero. */
7402 mpz_set_si (result->value.integer, 0);
7409 /* Match a variable name with an optional initializer. When this
7410 subroutine is called, a variable is expected to be parsed next.
7411 Depending on what is happening at the moment, updates either the
7412 symbol table or the current interface. */
7415 enumerator_decl (void)
7417 char name[GFC_MAX_SYMBOL_LEN + 1];
7418 gfc_expr *initializer;
7419 gfc_array_spec *as = NULL;
7427 old_locus = gfc_current_locus;
7429 /* When we get here, we've just matched a list of attributes and
7430 maybe a type and a double colon. The next thing we expect to see
7431 is the name of the symbol. */
7432 m = gfc_match_name (name);
7436 var_locus = gfc_current_locus;
7438 /* OK, we've successfully matched the declaration. Now put the
7439 symbol in the current namespace. If we fail to create the symbol,
7441 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
7447 /* The double colon must be present in order to have initializers.
7448 Otherwise the statement is ambiguous with an assignment statement. */
7451 if (gfc_match_char ('=') == MATCH_YES)
7453 m = gfc_match_init_expr (&initializer);
7456 gfc_error ("Expected an initialization expression at %C");
7465 /* If we do not have an initializer, the initialization value of the
7466 previous enumerator (stored in last_initializer) is incremented
7467 by 1 and is used to initialize the current enumerator. */
7468 if (initializer == NULL)
7469 initializer = enum_initializer (last_initializer, old_locus);
7471 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7473 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7479 /* Store this current initializer, for the next enumerator variable
7480 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7481 use last_initializer below. */
7482 last_initializer = initializer;
7483 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7485 /* Maintain enumerator history. */
7486 gfc_find_symbol (name, NULL, 0, &sym);
7487 create_enum_history (sym, last_initializer);
7489 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
7492 /* Free stuff up and return. */
7493 gfc_free_expr (initializer);
7499 /* Match the enumerator definition statement. */
7502 gfc_match_enumerator_def (void)
7507 gfc_clear_ts (¤t_ts);
7509 m = gfc_match (" enumerator");
7513 m = gfc_match (" :: ");
7514 if (m == MATCH_ERROR)
7517 colon_seen = (m == MATCH_YES);
7519 if (gfc_current_state () != COMP_ENUM)
7521 gfc_error ("ENUM definition statement expected before %C");
7522 gfc_free_enum_history ();
7526 (¤t_ts)->type = BT_INTEGER;
7527 (¤t_ts)->kind = gfc_c_int_kind;
7529 gfc_clear_attr (¤t_attr);
7530 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
7539 m = enumerator_decl ();
7540 if (m == MATCH_ERROR)
7542 gfc_free_enum_history ();
7548 if (gfc_match_eos () == MATCH_YES)
7550 if (gfc_match_char (',') != MATCH_YES)
7554 if (gfc_current_state () == COMP_ENUM)
7556 gfc_free_enum_history ();
7557 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7562 gfc_free_array_spec (current_as);
7569 /* Match binding attributes. */
7572 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7574 bool found_passing = false;
7575 bool seen_ptr = false;
7576 match m = MATCH_YES;
7578 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7579 this case the defaults are in there. */
7580 ba->access = ACCESS_UNKNOWN;
7581 ba->pass_arg = NULL;
7582 ba->pass_arg_num = 0;
7584 ba->non_overridable = 0;
7588 /* If we find a comma, we believe there are binding attributes. */
7589 m = gfc_match_char (',');
7595 /* Access specifier. */
7597 m = gfc_match (" public");
7598 if (m == MATCH_ERROR)
7602 if (ba->access != ACCESS_UNKNOWN)
7604 gfc_error ("Duplicate access-specifier at %C");
7608 ba->access = ACCESS_PUBLIC;
7612 m = gfc_match (" private");
7613 if (m == MATCH_ERROR)
7617 if (ba->access != ACCESS_UNKNOWN)
7619 gfc_error ("Duplicate access-specifier at %C");
7623 ba->access = ACCESS_PRIVATE;
7627 /* If inside GENERIC, the following is not allowed. */
7632 m = gfc_match (" nopass");
7633 if (m == MATCH_ERROR)
7639 gfc_error ("Binding attributes already specify passing,"
7640 " illegal NOPASS at %C");
7644 found_passing = true;
7649 /* PASS possibly including argument. */
7650 m = gfc_match (" pass");
7651 if (m == MATCH_ERROR)
7655 char arg[GFC_MAX_SYMBOL_LEN + 1];
7659 gfc_error ("Binding attributes already specify passing,"
7660 " illegal PASS at %C");
7664 m = gfc_match (" ( %n )", arg);
7665 if (m == MATCH_ERROR)
7668 ba->pass_arg = gfc_get_string (arg);
7669 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7671 found_passing = true;
7679 m = gfc_match (" pointer");
7680 if (m == MATCH_ERROR)
7686 gfc_error ("Duplicate POINTER attribute at %C");
7696 /* NON_OVERRIDABLE flag. */
7697 m = gfc_match (" non_overridable");
7698 if (m == MATCH_ERROR)
7702 if (ba->non_overridable)
7704 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7708 ba->non_overridable = 1;
7712 /* DEFERRED flag. */
7713 m = gfc_match (" deferred");
7714 if (m == MATCH_ERROR)
7720 gfc_error ("Duplicate DEFERRED at %C");
7731 /* Nothing matching found. */
7733 gfc_error ("Expected access-specifier at %C");
7735 gfc_error ("Expected binding attribute at %C");
7738 while (gfc_match_char (',') == MATCH_YES);
7740 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
7741 if (ba->non_overridable && ba->deferred)
7743 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7750 if (ba->access == ACCESS_UNKNOWN)
7751 ba->access = gfc_typebound_default_access;
7753 if (ppc && !seen_ptr)
7755 gfc_error ("POINTER attribute is required for procedure pointer component"
7767 /* Match a PROCEDURE specific binding inside a derived type. */
7770 match_procedure_in_type (void)
7772 char name[GFC_MAX_SYMBOL_LEN + 1];
7773 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
7774 char* target = NULL, *ifc = NULL;
7775 gfc_typebound_proc tb;
7784 /* Check current state. */
7785 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
7786 block = gfc_state_stack->previous->sym;
7789 /* Try to match PROCEDURE(interface). */
7790 if (gfc_match (" (") == MATCH_YES)
7792 m = gfc_match_name (target_buf);
7793 if (m == MATCH_ERROR)
7797 gfc_error ("Interface-name expected after '(' at %C");
7801 if (gfc_match (" )") != MATCH_YES)
7803 gfc_error ("')' expected at %C");
7810 /* Construct the data structure. */
7811 memset (&tb, 0, sizeof (tb));
7812 tb.where = gfc_current_locus;
7814 /* Match binding attributes. */
7815 m = match_binding_attributes (&tb, false, false);
7816 if (m == MATCH_ERROR)
7818 seen_attrs = (m == MATCH_YES);
7820 /* Check that attribute DEFERRED is given if an interface is specified. */
7821 if (tb.deferred && !ifc)
7823 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7826 if (ifc && !tb.deferred)
7828 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7832 /* Match the colons. */
7833 m = gfc_match (" ::");
7834 if (m == MATCH_ERROR)
7836 seen_colons = (m == MATCH_YES);
7837 if (seen_attrs && !seen_colons)
7839 gfc_error ("Expected '::' after binding-attributes at %C");
7843 /* Match the binding names. */
7846 m = gfc_match_name (name);
7847 if (m == MATCH_ERROR)
7851 gfc_error ("Expected binding name at %C");
7855 if (num>1 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: PROCEDURE list"
7856 " at %C") == FAILURE)
7859 /* Try to match the '=> target', if it's there. */
7861 m = gfc_match (" =>");
7862 if (m == MATCH_ERROR)
7868 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7874 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7879 m = gfc_match_name (target_buf);
7880 if (m == MATCH_ERROR)
7884 gfc_error ("Expected binding target after '=>' at %C");
7887 target = target_buf;
7890 /* If no target was found, it has the same name as the binding. */
7894 /* Get the namespace to insert the symbols into. */
7895 ns = block->f2k_derived;
7898 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7899 if (tb.deferred && !block->attr.abstract)
7901 gfc_error ("Type '%s' containing DEFERRED binding at %C "
7902 "is not ABSTRACT", block->name);
7906 /* See if we already have a binding with this name in the symtree which
7907 would be an error. If a GENERIC already targetted this binding, it may
7908 be already there but then typebound is still NULL. */
7909 stree = gfc_find_symtree (ns->tb_sym_root, name);
7910 if (stree && stree->n.tb)
7912 gfc_error ("There is already a procedure with binding name '%s' for "
7913 "the derived type '%s' at %C", name, block->name);
7917 /* Insert it and set attributes. */
7921 stree = gfc_new_symtree (&ns->tb_sym_root, name);
7924 stree->n.tb = gfc_get_typebound_proc (&tb);
7926 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
7929 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
7931 if (gfc_match_eos () == MATCH_YES)
7933 if (gfc_match_char (',') != MATCH_YES)
7938 gfc_error ("Syntax error in PROCEDURE statement at %C");
7943 /* Match a GENERIC procedure binding inside a derived type. */
7946 gfc_match_generic (void)
7948 char name[GFC_MAX_SYMBOL_LEN + 1];
7949 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
7951 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
7952 gfc_typebound_proc* tb;
7954 interface_type op_type;
7955 gfc_intrinsic_op op;
7958 /* Check current state. */
7959 if (gfc_current_state () == COMP_DERIVED)
7961 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
7964 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
7966 block = gfc_state_stack->previous->sym;
7967 ns = block->f2k_derived;
7968 gcc_assert (block && ns);
7970 memset (&tbattr, 0, sizeof (tbattr));
7971 tbattr.where = gfc_current_locus;
7973 /* See if we get an access-specifier. */
7974 m = match_binding_attributes (&tbattr, true, false);
7975 if (m == MATCH_ERROR)
7978 /* Now the colons, those are required. */
7979 if (gfc_match (" ::") != MATCH_YES)
7981 gfc_error ("Expected '::' at %C");
7985 /* Match the binding name; depending on type (operator / generic) format
7986 it for future error messages into bind_name. */
7988 m = gfc_match_generic_spec (&op_type, name, &op);
7989 if (m == MATCH_ERROR)
7993 gfc_error ("Expected generic name or operator descriptor at %C");
7999 case INTERFACE_GENERIC:
8000 snprintf (bind_name, sizeof (bind_name), "%s", name);
8003 case INTERFACE_USER_OP:
8004 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8007 case INTERFACE_INTRINSIC_OP:
8008 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8009 gfc_op2string (op));
8016 /* Match the required =>. */
8017 if (gfc_match (" =>") != MATCH_YES)
8019 gfc_error ("Expected '=>' at %C");
8023 /* Try to find existing GENERIC binding with this name / for this operator;
8024 if there is something, check that it is another GENERIC and then extend
8025 it rather than building a new node. Otherwise, create it and put it
8026 at the right position. */
8030 case INTERFACE_USER_OP:
8031 case INTERFACE_GENERIC:
8033 const bool is_op = (op_type == INTERFACE_USER_OP);
8036 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8048 case INTERFACE_INTRINSIC_OP:
8058 if (!tb->is_generic)
8060 gcc_assert (op_type == INTERFACE_GENERIC);
8061 gfc_error ("There's already a non-generic procedure with binding name"
8062 " '%s' for the derived type '%s' at %C",
8063 bind_name, block->name);
8067 if (tb->access != tbattr.access)
8069 gfc_error ("Binding at %C must have the same access as already"
8070 " defined binding '%s'", bind_name);
8076 tb = gfc_get_typebound_proc (NULL);
8077 tb->where = gfc_current_locus;
8078 tb->access = tbattr.access;
8080 tb->u.generic = NULL;
8084 case INTERFACE_GENERIC:
8085 case INTERFACE_USER_OP:
8087 const bool is_op = (op_type == INTERFACE_USER_OP);
8090 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8098 case INTERFACE_INTRINSIC_OP:
8107 /* Now, match all following names as specific targets. */
8110 gfc_symtree* target_st;
8111 gfc_tbp_generic* target;
8113 m = gfc_match_name (name);
8114 if (m == MATCH_ERROR)
8118 gfc_error ("Expected specific binding name at %C");
8122 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8124 /* See if this is a duplicate specification. */
8125 for (target = tb->u.generic; target; target = target->next)
8126 if (target_st == target->specific_st)
8128 gfc_error ("'%s' already defined as specific binding for the"
8129 " generic '%s' at %C", name, bind_name);
8133 target = gfc_get_tbp_generic ();
8134 target->specific_st = target_st;
8135 target->specific = NULL;
8136 target->next = tb->u.generic;
8137 tb->u.generic = target;
8139 while (gfc_match (" ,") == MATCH_YES);
8141 /* Here should be the end. */
8142 if (gfc_match_eos () != MATCH_YES)
8144 gfc_error ("Junk after GENERIC binding at %C");
8155 /* Match a FINAL declaration inside a derived type. */
8158 gfc_match_final_decl (void)
8160 char name[GFC_MAX_SYMBOL_LEN + 1];
8163 gfc_namespace* module_ns;
8167 if (gfc_current_form == FORM_FREE)
8169 char c = gfc_peek_ascii_char ();
8170 if (!gfc_is_whitespace (c) && c != ':')
8174 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8176 if (gfc_current_form == FORM_FIXED)
8179 gfc_error ("FINAL declaration at %C must be inside a derived type "
8180 "CONTAINS section");
8184 block = gfc_state_stack->previous->sym;
8187 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8188 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8190 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8191 " specification part of a MODULE");
8195 module_ns = gfc_current_ns;
8196 gcc_assert (module_ns);
8197 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8199 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8200 if (gfc_match (" ::") == MATCH_ERROR)
8203 /* Match the sequence of procedure names. */
8210 if (first && gfc_match_eos () == MATCH_YES)
8212 gfc_error ("Empty FINAL at %C");
8216 m = gfc_match_name (name);
8219 gfc_error ("Expected module procedure name at %C");
8222 else if (m != MATCH_YES)
8225 if (gfc_match_eos () == MATCH_YES)
8227 if (!last && gfc_match_char (',') != MATCH_YES)
8229 gfc_error ("Expected ',' at %C");
8233 if (gfc_get_symbol (name, module_ns, &sym))
8235 gfc_error ("Unknown procedure name \"%s\" at %C", name);
8239 /* Mark the symbol as module procedure. */
8240 if (sym->attr.proc != PROC_MODULE
8241 && gfc_add_procedure (&sym->attr, PROC_MODULE,
8242 sym->name, NULL) == FAILURE)
8245 /* Check if we already have this symbol in the list, this is an error. */
8246 for (f = block->f2k_derived->finalizers; f; f = f->next)
8247 if (f->proc_sym == sym)
8249 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8254 /* Add this symbol to the list of finalizers. */
8255 gcc_assert (block->f2k_derived);
8257 f = XCNEW (gfc_finalizer);
8259 f->proc_tree = NULL;
8260 f->where = gfc_current_locus;
8261 f->next = block->f2k_derived->finalizers;
8262 block->f2k_derived->finalizers = f;
8272 const ext_attr_t ext_attr_list[] = {
8273 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8274 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8275 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
8276 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
8277 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
8278 { NULL, EXT_ATTR_LAST, NULL }
8281 /* Match a !GCC$ ATTRIBUTES statement of the form:
8282 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8283 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8285 TODO: We should support all GCC attributes using the same syntax for
8286 the attribute list, i.e. the list in C
8287 __attributes(( attribute-list ))
8289 !GCC$ ATTRIBUTES attribute-list ::
8290 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8293 As there is absolutely no risk of confusion, we should never return
8296 gfc_match_gcc_attributes (void)
8298 symbol_attribute attr;
8299 char name[GFC_MAX_SYMBOL_LEN + 1];
8304 gfc_clear_attr (&attr);
8309 if (gfc_match_name (name) != MATCH_YES)
8312 for (id = 0; id < EXT_ATTR_LAST; id++)
8313 if (strcmp (name, ext_attr_list[id].name) == 0)
8316 if (id == EXT_ATTR_LAST)
8318 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8322 if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
8326 gfc_gobble_whitespace ();
8327 ch = gfc_next_ascii_char ();
8330 /* This is the successful exit condition for the loop. */
8331 if (gfc_next_ascii_char () == ':')
8341 if (gfc_match_eos () == MATCH_YES)
8346 m = gfc_match_name (name);
8350 if (find_special (name, &sym, true))
8353 sym->attr.ext_attr |= attr.ext_attr;
8355 if (gfc_match_eos () == MATCH_YES)
8358 if (gfc_match_char (',') != MATCH_YES)
8365 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");