1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
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"
31 /* Macros to access allocate memory for gfc_data_variable,
32 gfc_data_value and gfc_data. */
33 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
34 #define gfc_get_data_value() XCNEW (gfc_data_value)
35 #define gfc_get_data() XCNEW (gfc_data)
38 static gfc_try set_binding_label (const char **, const char *, int);
41 /* This flag is set if an old-style length selector is matched
42 during a type-declaration statement. */
44 static int old_char_selector;
46 /* When variables acquire types and attributes from a declaration
47 statement, they get them from the following static variables. The
48 first part of a declaration sets these variables and the second
49 part copies these into symbol structures. */
51 static gfc_typespec current_ts;
53 static symbol_attribute current_attr;
54 static gfc_array_spec *current_as;
55 static int colon_seen;
57 /* The current binding label (if any). */
58 static const char* curr_binding_label;
59 /* Need to know how many identifiers are on the current data declaration
60 line in case we're given the BIND(C) attribute with a NAME= specifier. */
61 static int num_idents_on_line;
62 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
63 can supply a name if the curr_binding_label is nil and NAME= was not. */
64 static int has_name_equals = 0;
66 /* Initializer of the previous enumerator. */
68 static gfc_expr *last_initializer;
70 /* History of all the enumerators is maintained, so that
71 kind values of all the enumerators could be updated depending
72 upon the maximum initialized value. */
74 typedef struct enumerator_history
77 gfc_expr *initializer;
78 struct enumerator_history *next;
82 /* Header of enum history chain. */
84 static enumerator_history *enum_history = NULL;
86 /* Pointer of enum history node containing largest initializer. */
88 static enumerator_history *max_enum = NULL;
90 /* gfc_new_block points to the symbol of a newly matched block. */
92 gfc_symbol *gfc_new_block;
94 bool gfc_matching_function;
97 /********************* DATA statement subroutines *********************/
99 static bool in_match_data = false;
102 gfc_in_match_data (void)
104 return in_match_data;
108 set_in_match_data (bool set_value)
110 in_match_data = set_value;
113 /* Free a gfc_data_variable structure and everything beneath it. */
116 free_variable (gfc_data_variable *p)
118 gfc_data_variable *q;
123 gfc_free_expr (p->expr);
124 gfc_free_iterator (&p->iter, 0);
125 free_variable (p->list);
131 /* Free a gfc_data_value structure and everything beneath it. */
134 free_value (gfc_data_value *p)
141 mpz_clear (p->repeat);
142 gfc_free_expr (p->expr);
148 /* Free a list of gfc_data structures. */
151 gfc_free_data (gfc_data *p)
158 free_variable (p->var);
159 free_value (p->value);
165 /* Free all data in a namespace. */
168 gfc_free_data_all (gfc_namespace *ns)
181 static match var_element (gfc_data_variable *);
183 /* Match a list of variables terminated by an iterator and a right
187 var_list (gfc_data_variable *parent)
189 gfc_data_variable *tail, var;
192 m = var_element (&var);
193 if (m == MATCH_ERROR)
198 tail = gfc_get_data_variable ();
205 if (gfc_match_char (',') != MATCH_YES)
208 m = gfc_match_iterator (&parent->iter, 1);
211 if (m == MATCH_ERROR)
214 m = var_element (&var);
215 if (m == MATCH_ERROR)
220 tail->next = gfc_get_data_variable ();
226 if (gfc_match_char (')') != MATCH_YES)
231 gfc_syntax_error (ST_DATA);
236 /* Match a single element in a data variable list, which can be a
237 variable-iterator list. */
240 var_element (gfc_data_variable *new_var)
245 memset (new_var, 0, sizeof (gfc_data_variable));
247 if (gfc_match_char ('(') == MATCH_YES)
248 return var_list (new_var);
250 m = gfc_match_variable (&new_var->expr, 0);
254 sym = new_var->expr->symtree->n.sym;
256 /* Symbol should already have an associated type. */
257 if (gfc_check_symbol_typed (sym, gfc_current_ns,
258 false, gfc_current_locus) == FAILURE)
261 if (!sym->attr.function && gfc_current_ns->parent
262 && gfc_current_ns->parent == sym->ns)
264 gfc_error ("Host associated variable '%s' may not be in the DATA "
265 "statement at %C", sym->name);
269 if (gfc_current_state () != COMP_BLOCK_DATA
270 && sym->attr.in_common
271 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
272 "common block variable '%s' in DATA statement at %C",
273 sym->name) == FAILURE)
276 if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
283 /* Match the top-level list of data variables. */
286 top_var_list (gfc_data *d)
288 gfc_data_variable var, *tail, *new_var;
295 m = var_element (&var);
298 if (m == MATCH_ERROR)
301 new_var = gfc_get_data_variable ();
307 tail->next = new_var;
311 if (gfc_match_char ('/') == MATCH_YES)
313 if (gfc_match_char (',') != MATCH_YES)
320 gfc_syntax_error (ST_DATA);
321 gfc_free_data_all (gfc_current_ns);
327 match_data_constant (gfc_expr **result)
329 char name[GFC_MAX_SYMBOL_LEN + 1];
330 gfc_symbol *sym, *dt_sym = NULL;
335 m = gfc_match_literal_constant (&expr, 1);
342 if (m == MATCH_ERROR)
345 m = gfc_match_null (result);
349 old_loc = gfc_current_locus;
351 /* Should this be a structure component, try to match it
352 before matching a name. */
353 m = gfc_match_rvalue (result);
354 if (m == MATCH_ERROR)
357 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
359 if (gfc_simplify_expr (*result, 0) == FAILURE)
364 gfc_current_locus = old_loc;
366 m = gfc_match_name (name);
370 if (gfc_find_symbol (name, NULL, 1, &sym))
373 if (sym && sym->attr.generic)
374 dt_sym = gfc_find_dt_in_generic (sym);
377 || (sym->attr.flavor != FL_PARAMETER
378 && (!dt_sym || dt_sym->attr.flavor != FL_DERIVED)))
380 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
384 else if (dt_sym && dt_sym->attr.flavor == FL_DERIVED)
385 return gfc_match_structure_constructor (dt_sym, result);
387 /* Check to see if the value is an initialization array expression. */
388 if (sym->value->expr_type == EXPR_ARRAY)
390 gfc_current_locus = old_loc;
392 m = gfc_match_init_expr (result);
393 if (m == MATCH_ERROR)
398 if (gfc_simplify_expr (*result, 0) == FAILURE)
401 if ((*result)->expr_type == EXPR_CONSTANT)
405 gfc_error ("Invalid initializer %s in Data statement at %C", name);
411 *result = gfc_copy_expr (sym->value);
416 /* Match a list of values in a DATA statement. The leading '/' has
417 already been seen at this point. */
420 top_val_list (gfc_data *data)
422 gfc_data_value *new_val, *tail;
430 m = match_data_constant (&expr);
433 if (m == MATCH_ERROR)
436 new_val = gfc_get_data_value ();
437 mpz_init (new_val->repeat);
440 data->value = new_val;
442 tail->next = new_val;
446 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
449 mpz_set_ui (tail->repeat, 1);
453 if (expr->ts.type == BT_INTEGER)
454 mpz_set (tail->repeat, expr->value.integer);
455 gfc_free_expr (expr);
457 m = match_data_constant (&tail->expr);
460 if (m == MATCH_ERROR)
464 if (gfc_match_char ('/') == MATCH_YES)
466 if (gfc_match_char (',') == MATCH_NO)
473 gfc_syntax_error (ST_DATA);
474 gfc_free_data_all (gfc_current_ns);
479 /* Matches an old style initialization. */
482 match_old_style_init (const char *name)
489 /* Set up data structure to hold initializers. */
490 gfc_find_sym_tree (name, NULL, 0, &st);
493 newdata = gfc_get_data ();
494 newdata->var = gfc_get_data_variable ();
495 newdata->var->expr = gfc_get_variable_expr (st);
496 newdata->where = gfc_current_locus;
498 /* Match initial value list. This also eats the terminal '/'. */
499 m = top_val_list (newdata);
508 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
512 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
514 /* Mark the variable as having appeared in a data statement. */
515 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
521 /* Chain in namespace list of DATA initializers. */
522 newdata->next = gfc_current_ns->data;
523 gfc_current_ns->data = newdata;
529 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
530 we are matching a DATA statement and are therefore issuing an error
531 if we encounter something unexpected, if not, we're trying to match
532 an old-style initialization expression of the form INTEGER I /2/. */
535 gfc_match_data (void)
540 set_in_match_data (true);
544 new_data = gfc_get_data ();
545 new_data->where = gfc_current_locus;
547 m = top_var_list (new_data);
551 m = top_val_list (new_data);
555 new_data->next = gfc_current_ns->data;
556 gfc_current_ns->data = new_data;
558 if (gfc_match_eos () == MATCH_YES)
561 gfc_match_char (','); /* Optional comma */
564 set_in_match_data (false);
568 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
571 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
576 set_in_match_data (false);
577 gfc_free_data (new_data);
582 /************************ Declaration statements *********************/
585 /* Auxilliary function to merge DIMENSION and CODIMENSION array specs. */
588 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
592 if (to->rank == 0 && from->rank > 0)
594 to->rank = from->rank;
595 to->type = from->type;
596 to->cray_pointee = from->cray_pointee;
597 to->cp_was_assumed = from->cp_was_assumed;
599 for (i = 0; i < to->corank; i++)
601 to->lower[from->rank + i] = to->lower[i];
602 to->upper[from->rank + i] = to->upper[i];
604 for (i = 0; i < from->rank; i++)
608 to->lower[i] = gfc_copy_expr (from->lower[i]);
609 to->upper[i] = gfc_copy_expr (from->upper[i]);
613 to->lower[i] = from->lower[i];
614 to->upper[i] = from->upper[i];
618 else if (to->corank == 0 && from->corank > 0)
620 to->corank = from->corank;
621 to->cotype = from->cotype;
623 for (i = 0; i < from->corank; i++)
627 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
628 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
632 to->lower[to->rank + i] = from->lower[i];
633 to->upper[to->rank + i] = from->upper[i];
640 /* Match an intent specification. Since this can only happen after an
641 INTENT word, a legal intent-spec must follow. */
644 match_intent_spec (void)
647 if (gfc_match (" ( in out )") == MATCH_YES)
649 if (gfc_match (" ( in )") == MATCH_YES)
651 if (gfc_match (" ( out )") == MATCH_YES)
654 gfc_error ("Bad INTENT specification at %C");
655 return INTENT_UNKNOWN;
659 /* Matches a character length specification, which is either a
660 specification expression, '*', or ':'. */
663 char_len_param_value (gfc_expr **expr, bool *deferred)
670 if (gfc_match_char ('*') == MATCH_YES)
673 if (gfc_match_char (':') == MATCH_YES)
675 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: deferred type "
676 "parameter at %C") == FAILURE)
684 m = gfc_match_expr (expr);
687 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
690 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
692 if ((*expr)->value.function.actual
693 && (*expr)->value.function.actual->expr->symtree)
696 e = (*expr)->value.function.actual->expr;
697 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
698 && e->expr_type == EXPR_VARIABLE)
700 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
702 if (e->symtree->n.sym->ts.type == BT_CHARACTER
703 && e->symtree->n.sym->ts.u.cl
704 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
712 gfc_error ("Conflict in attributes of function argument at %C");
717 /* A character length is a '*' followed by a literal integer or a
718 char_len_param_value in parenthesis. */
721 match_char_length (gfc_expr **expr, bool *deferred)
727 m = gfc_match_char ('*');
731 m = gfc_match_small_literal_int (&length, NULL);
732 if (m == MATCH_ERROR)
737 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
738 "Old-style character length at %C") == FAILURE)
740 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
744 if (gfc_match_char ('(') == MATCH_NO)
747 m = char_len_param_value (expr, deferred);
748 if (m != MATCH_YES && gfc_matching_function)
754 if (m == MATCH_ERROR)
759 if (gfc_match_char (')') == MATCH_NO)
761 gfc_free_expr (*expr);
769 gfc_error ("Syntax error in character length specification at %C");
774 /* Special subroutine for finding a symbol. Check if the name is found
775 in the current name space. If not, and we're compiling a function or
776 subroutine and the parent compilation unit is an interface, then check
777 to see if the name we've been given is the name of the interface
778 (located in another namespace). */
781 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
787 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
790 *result = st ? st->n.sym : NULL;
794 if (gfc_current_state () != COMP_SUBROUTINE
795 && gfc_current_state () != COMP_FUNCTION)
798 s = gfc_state_stack->previous;
802 if (s->state != COMP_INTERFACE)
805 goto end; /* Nameless interface. */
807 if (strcmp (name, s->sym->name) == 0)
818 /* Special subroutine for getting a symbol node associated with a
819 procedure name, used in SUBROUTINE and FUNCTION statements. The
820 symbol is created in the parent using with symtree node in the
821 child unit pointing to the symbol. If the current namespace has no
822 parent, then the symbol is just created in the current unit. */
825 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
831 /* Module functions have to be left in their own namespace because
832 they have potentially (almost certainly!) already been referenced.
833 In this sense, they are rather like external functions. This is
834 fixed up in resolve.c(resolve_entries), where the symbol name-
835 space is set to point to the master function, so that the fake
836 result mechanism can work. */
837 if (module_fcn_entry)
839 /* Present if entry is declared to be a module procedure. */
840 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
843 rc = gfc_get_symbol (name, NULL, result);
844 else if (!gfc_get_symbol (name, NULL, &sym) && sym
845 && (*result)->ts.type == BT_UNKNOWN
846 && sym->attr.flavor == FL_UNKNOWN)
847 /* Pick up the typespec for the entry, if declared in the function
848 body. Note that this symbol is FL_UNKNOWN because it will
849 only have appeared in a type declaration. The local symtree
850 is set to point to the module symbol and a unique symtree
851 to the local version. This latter ensures a correct clearing
854 /* If the ENTRY proceeds its specification, we need to ensure
855 that this does not raise a "has no IMPLICIT type" error. */
856 if (sym->ts.type == BT_UNKNOWN)
857 sym->attr.untyped = 1;
859 (*result)->ts = sym->ts;
861 /* Put the symbol in the procedure namespace so that, should
862 the ENTRY precede its specification, the specification
864 (*result)->ns = gfc_current_ns;
866 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
868 st = gfc_get_unique_symtree (gfc_current_ns);
873 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
879 gfc_current_ns->refs++;
881 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
883 /* Trap another encompassed procedure with the same name. All
884 these conditions are necessary to avoid picking up an entry
885 whose name clashes with that of the encompassing procedure;
886 this is handled using gsymbols to register unique,globally
888 if (sym->attr.flavor != 0
889 && sym->attr.proc != 0
890 && (sym->attr.subroutine || sym->attr.function)
891 && sym->attr.if_source != IFSRC_UNKNOWN)
892 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
893 name, &sym->declared_at);
895 /* Trap a procedure with a name the same as interface in the
896 encompassing scope. */
897 if (sym->attr.generic != 0
898 && (sym->attr.subroutine || sym->attr.function)
899 && !sym->attr.mod_proc)
900 gfc_error_now ("Name '%s' at %C is already defined"
901 " as a generic interface at %L",
902 name, &sym->declared_at);
904 /* Trap declarations of attributes in encompassing scope. The
905 signature for this is that ts.kind is set. Legitimate
906 references only set ts.type. */
907 if (sym->ts.kind != 0
908 && !sym->attr.implicit_type
909 && sym->attr.proc == 0
910 && gfc_current_ns->parent != NULL
911 && sym->attr.access == 0
912 && !module_fcn_entry)
913 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
914 "and must not have attributes declared at %L",
915 name, &sym->declared_at);
918 if (gfc_current_ns->parent == NULL || *result == NULL)
921 /* Module function entries will already have a symtree in
922 the current namespace but will need one at module level. */
923 if (module_fcn_entry)
925 /* Present if entry is declared to be a module procedure. */
926 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
928 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
931 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
936 /* See if the procedure should be a module procedure. */
938 if (((sym->ns->proc_name != NULL
939 && sym->ns->proc_name->attr.flavor == FL_MODULE
940 && sym->attr.proc != PROC_MODULE)
941 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
942 && gfc_add_procedure (&sym->attr, PROC_MODULE,
943 sym->name, NULL) == FAILURE)
950 /* Verify that the given symbol representing a parameter is C
951 interoperable, by checking to see if it was marked as such after
952 its declaration. If the given symbol is not interoperable, a
953 warning is reported, thus removing the need to return the status to
954 the calling function. The standard does not require the user use
955 one of the iso_c_binding named constants to declare an
956 interoperable parameter, but we can't be sure if the param is C
957 interop or not if the user doesn't. For example, integer(4) may be
958 legal Fortran, but doesn't have meaning in C. It may interop with
959 a number of the C types, which causes a problem because the
960 compiler can't know which one. This code is almost certainly not
961 portable, and the user will get what they deserve if the C type
962 across platforms isn't always interoperable with integer(4). If
963 the user had used something like integer(c_int) or integer(c_long),
964 the compiler could have automatically handled the varying sizes
968 gfc_verify_c_interop_param (gfc_symbol *sym)
970 int is_c_interop = 0;
971 gfc_try retval = SUCCESS;
973 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
974 Don't repeat the checks here. */
975 if (sym->attr.implicit_type)
978 /* For subroutines or functions that are passed to a BIND(C) procedure,
979 they're interoperable if they're BIND(C) and their params are all
981 if (sym->attr.flavor == FL_PROCEDURE)
983 if (sym->attr.is_bind_c == 0)
985 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
986 "attribute to be C interoperable", sym->name,
987 &(sym->declared_at));
993 if (sym->attr.is_c_interop == 1)
994 /* We've already checked this procedure; don't check it again. */
997 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1002 /* See if we've stored a reference to a procedure that owns sym. */
1003 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1005 if (sym->ns->proc_name->attr.is_bind_c == 1)
1007 is_c_interop = (gfc_verify_c_interop (&(sym->ts)) == SUCCESS ? 1 : 0);
1009 if (is_c_interop != 1)
1011 /* Make personalized messages to give better feedback. */
1012 if (sym->ts.type == BT_DERIVED)
1013 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1014 "BIND(C) procedure '%s' but is not C interoperable "
1015 "because derived type '%s' is not C interoperable",
1016 sym->name, &(sym->declared_at),
1017 sym->ns->proc_name->name,
1018 sym->ts.u.derived->name);
1019 else if (sym->ts.type == BT_CLASS)
1020 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1021 "BIND(C) procedure '%s' but is not C interoperable "
1022 "because it is polymorphic",
1023 sym->name, &(sym->declared_at),
1024 sym->ns->proc_name->name);
1026 gfc_warning ("Variable '%s' at %L is a parameter to the "
1027 "BIND(C) procedure '%s' but may not be C "
1029 sym->name, &(sym->declared_at),
1030 sym->ns->proc_name->name);
1033 /* Character strings are only C interoperable if they have a
1035 if (sym->ts.type == BT_CHARACTER)
1037 gfc_charlen *cl = sym->ts.u.cl;
1038 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1039 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1041 gfc_error ("Character argument '%s' at %L "
1042 "must be length 1 because "
1043 "procedure '%s' is BIND(C)",
1044 sym->name, &sym->declared_at,
1045 sym->ns->proc_name->name);
1050 /* We have to make sure that any param to a bind(c) routine does
1051 not have the allocatable, pointer, or optional attributes,
1052 according to J3/04-007, section 5.1. */
1053 if (sym->attr.allocatable == 1)
1055 gfc_error ("Variable '%s' at %L cannot have the "
1056 "ALLOCATABLE attribute because procedure '%s'"
1057 " is BIND(C)", sym->name, &(sym->declared_at),
1058 sym->ns->proc_name->name);
1062 if (sym->attr.pointer == 1)
1064 gfc_error ("Variable '%s' at %L cannot have the "
1065 "POINTER attribute because procedure '%s'"
1066 " is BIND(C)", sym->name, &(sym->declared_at),
1067 sym->ns->proc_name->name);
1071 if (sym->attr.optional == 1 && sym->attr.value)
1073 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1074 "and the VALUE attribute because procedure '%s' "
1075 "is BIND(C)", sym->name, &(sym->declared_at),
1076 sym->ns->proc_name->name);
1079 else if (sym->attr.optional == 1
1080 && gfc_notify_std (GFC_STD_F2008_TS, "TS29113: Variable '%s' "
1081 "at %L with OPTIONAL attribute in "
1082 "procedure '%s' which is BIND(C)",
1083 sym->name, &(sym->declared_at),
1084 sym->ns->proc_name->name)
1088 /* Make sure that if it has the dimension attribute, that it is
1089 either assumed size or explicit shape. */
1090 if (sym->as != NULL)
1092 if (sym->as->type == AS_ASSUMED_SHAPE)
1094 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1095 "argument to the procedure '%s' at %L because "
1096 "the procedure is BIND(C)", sym->name,
1097 &(sym->declared_at), sym->ns->proc_name->name,
1098 &(sym->ns->proc_name->declared_at));
1102 if (sym->as->type == AS_DEFERRED)
1104 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1105 "argument to the procedure '%s' at %L because "
1106 "the procedure is BIND(C)", sym->name,
1107 &(sym->declared_at), sym->ns->proc_name->name,
1108 &(sym->ns->proc_name->declared_at));
1120 /* Function called by variable_decl() that adds a name to the symbol table. */
1123 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1124 gfc_array_spec **as, locus *var_locus)
1126 symbol_attribute attr;
1129 if (gfc_get_symbol (name, NULL, &sym))
1132 /* Start updating the symbol table. Add basic type attribute if present. */
1133 if (current_ts.type != BT_UNKNOWN
1134 && (sym->attr.implicit_type == 0
1135 || !gfc_compare_types (&sym->ts, ¤t_ts))
1136 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
1139 if (sym->ts.type == BT_CHARACTER)
1142 sym->ts.deferred = cl_deferred;
1145 /* Add dimension attribute if present. */
1146 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1150 /* Add attribute to symbol. The copy is so that we can reset the
1151 dimension attribute. */
1152 attr = current_attr;
1154 attr.codimension = 0;
1156 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1159 /* Finish any work that may need to be done for the binding label,
1160 if it's a bind(c). The bind(c) attr is found before the symbol
1161 is made, and before the symbol name (for data decls), so the
1162 current_ts is holding the binding label, or nothing if the
1163 name= attr wasn't given. Therefore, test here if we're dealing
1164 with a bind(c) and make sure the binding label is set correctly. */
1165 if (sym->attr.is_bind_c == 1)
1167 if (!sym->binding_label)
1169 /* Set the binding label and verify that if a NAME= was specified
1170 then only one identifier was in the entity-decl-list. */
1171 if (set_binding_label (&sym->binding_label, sym->name,
1172 num_idents_on_line) == FAILURE)
1177 /* See if we know we're in a common block, and if it's a bind(c)
1178 common then we need to make sure we're an interoperable type. */
1179 if (sym->attr.in_common == 1)
1181 /* Test the common block object. */
1182 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1183 && sym->ts.is_c_interop != 1)
1185 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1186 "must be declared with a C interoperable "
1187 "kind since common block '%s' is BIND(C)",
1188 sym->name, sym->common_block->name,
1189 sym->common_block->name);
1194 sym->attr.implied_index = 0;
1196 if (sym->ts.type == BT_CLASS)
1197 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1203 /* Set character constant to the given length. The constant will be padded or
1204 truncated. If we're inside an array constructor without a typespec, we
1205 additionally check that all elements have the same length; check_len -1
1206 means no checking. */
1209 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1214 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1215 gcc_assert (expr->ts.type == BT_CHARACTER);
1217 slen = expr->value.character.length;
1220 s = gfc_get_wide_string (len + 1);
1221 memcpy (s, expr->value.character.string,
1222 MIN (len, slen) * sizeof (gfc_char_t));
1224 gfc_wide_memset (&s[slen], ' ', len - slen);
1226 if (gfc_option.warn_character_truncation && slen > len)
1227 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1228 "(%d/%d)", &expr->where, slen, len);
1230 /* Apply the standard by 'hand' otherwise it gets cleared for
1232 if (check_len != -1 && slen != check_len
1233 && !(gfc_option.allow_std & GFC_STD_GNU))
1234 gfc_error_now ("The CHARACTER elements of the array constructor "
1235 "at %L must have the same length (%d/%d)",
1236 &expr->where, slen, check_len);
1239 free (expr->value.character.string);
1240 expr->value.character.string = s;
1241 expr->value.character.length = len;
1246 /* Function to create and update the enumerator history
1247 using the information passed as arguments.
1248 Pointer "max_enum" is also updated, to point to
1249 enum history node containing largest initializer.
1251 SYM points to the symbol node of enumerator.
1252 INIT points to its enumerator value. */
1255 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1257 enumerator_history *new_enum_history;
1258 gcc_assert (sym != NULL && init != NULL);
1260 new_enum_history = XCNEW (enumerator_history);
1262 new_enum_history->sym = sym;
1263 new_enum_history->initializer = init;
1264 new_enum_history->next = NULL;
1266 if (enum_history == NULL)
1268 enum_history = new_enum_history;
1269 max_enum = enum_history;
1273 new_enum_history->next = enum_history;
1274 enum_history = new_enum_history;
1276 if (mpz_cmp (max_enum->initializer->value.integer,
1277 new_enum_history->initializer->value.integer) < 0)
1278 max_enum = new_enum_history;
1283 /* Function to free enum kind history. */
1286 gfc_free_enum_history (void)
1288 enumerator_history *current = enum_history;
1289 enumerator_history *next;
1291 while (current != NULL)
1293 next = current->next;
1298 enum_history = NULL;
1302 /* Function called by variable_decl() that adds an initialization
1303 expression to a symbol. */
1306 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1308 symbol_attribute attr;
1313 if (find_special (name, &sym, false))
1318 /* If this symbol is confirming an implicit parameter type,
1319 then an initialization expression is not allowed. */
1320 if (attr.flavor == FL_PARAMETER
1321 && sym->value != NULL
1324 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1331 /* An initializer is required for PARAMETER declarations. */
1332 if (attr.flavor == FL_PARAMETER)
1334 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1340 /* If a variable appears in a DATA block, it cannot have an
1344 gfc_error ("Variable '%s' at %C with an initializer already "
1345 "appears in a DATA statement", sym->name);
1349 /* Check if the assignment can happen. This has to be put off
1350 until later for derived type variables and procedure pointers. */
1351 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1352 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1353 && !sym->attr.proc_pointer
1354 && gfc_check_assign_symbol (sym, init) == FAILURE)
1357 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1358 && init->ts.type == BT_CHARACTER)
1360 /* Update symbol character length according initializer. */
1361 if (gfc_check_assign_symbol (sym, init) == FAILURE)
1364 if (sym->ts.u.cl->length == NULL)
1367 /* If there are multiple CHARACTER variables declared on the
1368 same line, we don't want them to share the same length. */
1369 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1371 if (sym->attr.flavor == FL_PARAMETER)
1373 if (init->expr_type == EXPR_CONSTANT)
1375 clen = init->value.character.length;
1376 sym->ts.u.cl->length
1377 = gfc_get_int_expr (gfc_default_integer_kind,
1380 else if (init->expr_type == EXPR_ARRAY)
1383 c = gfc_constructor_first (init->value.constructor);
1384 clen = c->expr->value.character.length;
1385 sym->ts.u.cl->length
1386 = gfc_get_int_expr (gfc_default_integer_kind,
1389 else if (init->ts.u.cl && init->ts.u.cl->length)
1390 sym->ts.u.cl->length =
1391 gfc_copy_expr (sym->value->ts.u.cl->length);
1394 /* Update initializer character length according symbol. */
1395 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1397 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1399 if (init->expr_type == EXPR_CONSTANT)
1400 gfc_set_constant_character_len (len, init, -1);
1401 else if (init->expr_type == EXPR_ARRAY)
1405 /* Build a new charlen to prevent simplification from
1406 deleting the length before it is resolved. */
1407 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1408 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1410 for (c = gfc_constructor_first (init->value.constructor);
1411 c; c = gfc_constructor_next (c))
1412 gfc_set_constant_character_len (len, c->expr, -1);
1417 /* If sym is implied-shape, set its upper bounds from init. */
1418 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1419 && sym->as->type == AS_IMPLIED_SHAPE)
1423 if (init->rank == 0)
1425 gfc_error ("Can't initialize implied-shape array at %L"
1426 " with scalar", &sym->declared_at);
1429 gcc_assert (sym->as->rank == init->rank);
1431 /* Shape should be present, we get an initialization expression. */
1432 gcc_assert (init->shape);
1434 for (dim = 0; dim < sym->as->rank; ++dim)
1440 lower = sym->as->lower[dim];
1441 if (lower->expr_type != EXPR_CONSTANT)
1443 gfc_error ("Non-constant lower bound in implied-shape"
1444 " declaration at %L", &lower->where);
1448 /* All dimensions must be without upper bound. */
1449 gcc_assert (!sym->as->upper[dim]);
1452 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1453 mpz_add (e->value.integer,
1454 lower->value.integer, init->shape[dim]);
1455 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1456 sym->as->upper[dim] = e;
1459 sym->as->type = AS_EXPLICIT;
1462 /* Need to check if the expression we initialized this
1463 to was one of the iso_c_binding named constants. If so,
1464 and we're a parameter (constant), let it be iso_c.
1466 integer(c_int), parameter :: my_int = c_int
1467 integer(my_int) :: my_int_2
1468 If we mark my_int as iso_c (since we can see it's value
1469 is equal to one of the named constants), then my_int_2
1470 will be considered C interoperable. */
1471 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1473 sym->ts.is_iso_c |= init->ts.is_iso_c;
1474 sym->ts.is_c_interop |= init->ts.is_c_interop;
1475 /* attr bits needed for module files. */
1476 sym->attr.is_iso_c |= init->ts.is_iso_c;
1477 sym->attr.is_c_interop |= init->ts.is_c_interop;
1478 if (init->ts.is_iso_c)
1479 sym->ts.f90_type = init->ts.f90_type;
1482 /* Add initializer. Make sure we keep the ranks sane. */
1483 if (sym->attr.dimension && init->rank == 0)
1488 if (sym->attr.flavor == FL_PARAMETER
1489 && init->expr_type == EXPR_CONSTANT
1490 && spec_size (sym->as, &size) == SUCCESS
1491 && mpz_cmp_si (size, 0) > 0)
1493 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1495 for (n = 0; n < (int)mpz_get_si (size); n++)
1496 gfc_constructor_append_expr (&array->value.constructor,
1499 : gfc_copy_expr (init),
1502 array->shape = gfc_get_shape (sym->as->rank);
1503 for (n = 0; n < sym->as->rank; n++)
1504 spec_dimen_size (sym->as, n, &array->shape[n]);
1509 init->rank = sym->as->rank;
1513 if (sym->attr.save == SAVE_NONE)
1514 sym->attr.save = SAVE_IMPLICIT;
1522 /* Function called by variable_decl() that adds a name to a structure
1526 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1527 gfc_array_spec **as)
1530 gfc_try t = SUCCESS;
1532 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1533 constructing, it must have the pointer attribute. */
1534 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1535 && current_ts.u.derived == gfc_current_block ()
1536 && current_attr.pointer == 0)
1538 gfc_error ("Component at %C must have the POINTER attribute");
1542 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1544 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1546 gfc_error ("Array component of structure at %C must have explicit "
1547 "or deferred shape");
1552 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1556 if (c->ts.type == BT_CHARACTER)
1558 c->attr = current_attr;
1560 c->initializer = *init;
1567 c->attr.codimension = 1;
1569 c->attr.dimension = 1;
1573 /* Should this ever get more complicated, combine with similar section
1574 in add_init_expr_to_sym into a separate function. */
1575 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1577 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1581 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1582 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1583 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1585 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1587 if (c->initializer->expr_type == EXPR_CONSTANT)
1588 gfc_set_constant_character_len (len, c->initializer, -1);
1589 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1590 c->initializer->ts.u.cl->length->value.integer))
1592 gfc_constructor *ctor;
1593 ctor = gfc_constructor_first (c->initializer->value.constructor);
1598 bool has_ts = (c->initializer->ts.u.cl
1599 && c->initializer->ts.u.cl->length_from_typespec);
1601 /* Remember the length of the first element for checking
1602 that all elements *in the constructor* have the same
1603 length. This need not be the length of the LHS! */
1604 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1605 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1606 first_len = ctor->expr->value.character.length;
1608 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1609 if (ctor->expr->expr_type == EXPR_CONSTANT)
1611 gfc_set_constant_character_len (len, ctor->expr,
1612 has_ts ? -1 : first_len);
1613 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1619 /* Check array components. */
1620 if (!c->attr.dimension)
1623 if (c->attr.pointer)
1625 if (c->as->type != AS_DEFERRED)
1627 gfc_error ("Pointer array component of structure at %C must have a "
1632 else if (c->attr.allocatable)
1634 if (c->as->type != AS_DEFERRED)
1636 gfc_error ("Allocatable component of structure at %C must have a "
1643 if (c->as->type != AS_EXPLICIT)
1645 gfc_error ("Array component of structure at %C must have an "
1652 if (c->ts.type == BT_CLASS)
1654 bool delayed = (gfc_state_stack->sym == c->ts.u.derived)
1655 || (!c->ts.u.derived->components
1656 && !c->ts.u.derived->attr.zero_comp);
1657 return gfc_build_class_symbol (&c->ts, &c->attr, &c->as, delayed);
1664 /* Match a 'NULL()', and possibly take care of some side effects. */
1667 gfc_match_null (gfc_expr **result)
1672 m = gfc_match (" null ( )");
1676 /* The NULL symbol now has to be/become an intrinsic function. */
1677 if (gfc_get_symbol ("null", NULL, &sym))
1679 gfc_error ("NULL() initialization at %C is ambiguous");
1683 gfc_intrinsic_symbol (sym);
1685 if (sym->attr.proc != PROC_INTRINSIC
1686 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1687 sym->name, NULL) == FAILURE
1688 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1691 *result = gfc_get_null_expr (&gfc_current_locus);
1697 /* Match the initialization expr for a data pointer or procedure pointer. */
1700 match_pointer_init (gfc_expr **init, int procptr)
1704 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1706 gfc_error ("Initialization of pointer at %C is not allowed in "
1707 "a PURE procedure");
1711 /* Match NULL() initilization. */
1712 m = gfc_match_null (init);
1716 /* Match non-NULL initialization. */
1717 gfc_matching_ptr_assignment = !procptr;
1718 gfc_matching_procptr_assignment = procptr;
1719 m = gfc_match_rvalue (init);
1720 gfc_matching_ptr_assignment = 0;
1721 gfc_matching_procptr_assignment = 0;
1722 if (m == MATCH_ERROR)
1724 else if (m == MATCH_NO)
1726 gfc_error ("Error in pointer initialization at %C");
1729 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
1732 gfc_resolve_expr (*init);
1734 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: non-NULL pointer "
1735 "initialization at %C") == FAILURE)
1743 check_function_name (char *name)
1745 /* In functions that have a RESULT variable defined, the function name always
1746 refers to function calls. Therefore, the name is not allowed to appear in
1747 specification statements. When checking this, be careful about
1748 'hidden' procedure pointer results ('ppr@'). */
1750 if (gfc_current_state () == COMP_FUNCTION)
1752 gfc_symbol *block = gfc_current_block ();
1753 if (block && block->result && block->result != block
1754 && strcmp (block->result->name, "ppr@") != 0
1755 && strcmp (block->name, name) == 0)
1757 gfc_error ("Function name '%s' not allowed at %C", name);
1766 /* Match a variable name with an optional initializer. When this
1767 subroutine is called, a variable is expected to be parsed next.
1768 Depending on what is happening at the moment, updates either the
1769 symbol table or the current interface. */
1772 variable_decl (int elem)
1774 char name[GFC_MAX_SYMBOL_LEN + 1];
1775 gfc_expr *initializer, *char_len;
1777 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1789 /* When we get here, we've just matched a list of attributes and
1790 maybe a type and a double colon. The next thing we expect to see
1791 is the name of the symbol. */
1792 m = gfc_match_name (name);
1796 var_locus = gfc_current_locus;
1798 /* Now we could see the optional array spec. or character length. */
1799 m = gfc_match_array_spec (&as, true, true);
1800 if (m == MATCH_ERROR)
1804 as = gfc_copy_array_spec (current_as);
1805 else if (current_as)
1806 merge_array_spec (current_as, as, true);
1808 if (gfc_option.flag_cray_pointer)
1809 cp_as = gfc_copy_array_spec (as);
1811 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1812 determine (and check) whether it can be implied-shape. If it
1813 was parsed as assumed-size, change it because PARAMETERs can not
1817 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1820 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1825 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1826 && current_attr.flavor == FL_PARAMETER)
1827 as->type = AS_IMPLIED_SHAPE;
1829 if (as->type == AS_IMPLIED_SHAPE
1830 && gfc_notify_std (GFC_STD_F2008,
1831 "Fortran 2008: Implied-shape array at %L",
1832 &var_locus) == FAILURE)
1841 cl_deferred = false;
1843 if (current_ts.type == BT_CHARACTER)
1845 switch (match_char_length (&char_len, &cl_deferred))
1848 cl = gfc_new_charlen (gfc_current_ns, NULL);
1850 cl->length = char_len;
1853 /* Non-constant lengths need to be copied after the first
1854 element. Also copy assumed lengths. */
1857 && (current_ts.u.cl->length == NULL
1858 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1860 cl = gfc_new_charlen (gfc_current_ns, NULL);
1861 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1864 cl = current_ts.u.cl;
1866 cl_deferred = current_ts.deferred;
1875 /* If this symbol has already shown up in a Cray Pointer declaration,
1876 then we want to set the type & bail out. */
1877 if (gfc_option.flag_cray_pointer)
1879 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1880 if (sym != NULL && sym->attr.cray_pointee)
1882 sym->ts.type = current_ts.type;
1883 sym->ts.kind = current_ts.kind;
1885 sym->ts.u.derived = current_ts.u.derived;
1886 sym->ts.is_c_interop = current_ts.is_c_interop;
1887 sym->ts.is_iso_c = current_ts.is_iso_c;
1890 /* Check to see if we have an array specification. */
1893 if (sym->as != NULL)
1895 gfc_error ("Duplicate array spec for Cray pointee at %C");
1896 gfc_free_array_spec (cp_as);
1902 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1903 gfc_internal_error ("Couldn't set pointee array spec.");
1905 /* Fix the array spec. */
1906 m = gfc_mod_pointee_as (sym->as);
1907 if (m == MATCH_ERROR)
1915 gfc_free_array_spec (cp_as);
1919 /* Procedure pointer as function result. */
1920 if (gfc_current_state () == COMP_FUNCTION
1921 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1922 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1923 strcpy (name, "ppr@");
1925 if (gfc_current_state () == COMP_FUNCTION
1926 && strcmp (name, gfc_current_block ()->name) == 0
1927 && gfc_current_block ()->result
1928 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1929 strcpy (name, "ppr@");
1931 /* OK, we've successfully matched the declaration. Now put the
1932 symbol in the current namespace, because it might be used in the
1933 optional initialization expression for this symbol, e.g. this is
1936 integer, parameter :: i = huge(i)
1938 This is only true for parameters or variables of a basic type.
1939 For components of derived types, it is not true, so we don't
1940 create a symbol for those yet. If we fail to create the symbol,
1942 if (gfc_current_state () != COMP_DERIVED
1943 && build_sym (name, cl, cl_deferred, &as, &var_locus) == FAILURE)
1949 if (check_function_name (name) == FAILURE)
1955 /* We allow old-style initializations of the form
1956 integer i /2/, j(4) /3*3, 1/
1957 (if no colon has been seen). These are different from data
1958 statements in that initializers are only allowed to apply to the
1959 variable immediately preceding, i.e.
1961 is not allowed. Therefore we have to do some work manually, that
1962 could otherwise be left to the matchers for DATA statements. */
1964 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1966 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1967 "initialization at %C") == FAILURE)
1970 return match_old_style_init (name);
1973 /* The double colon must be present in order to have initializers.
1974 Otherwise the statement is ambiguous with an assignment statement. */
1977 if (gfc_match (" =>") == MATCH_YES)
1979 if (!current_attr.pointer)
1981 gfc_error ("Initialization at %C isn't for a pointer variable");
1986 m = match_pointer_init (&initializer, 0);
1990 else if (gfc_match_char ('=') == MATCH_YES)
1992 if (current_attr.pointer)
1994 gfc_error ("Pointer initialization at %C requires '=>', "
2000 m = gfc_match_init_expr (&initializer);
2003 gfc_error ("Expected an initialization expression at %C");
2007 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2008 && gfc_state_stack->state != COMP_DERIVED)
2010 gfc_error ("Initialization of variable at %C is not allowed in "
2011 "a PURE procedure");
2015 if (current_attr.flavor != FL_PARAMETER
2016 && gfc_state_stack->state != COMP_DERIVED)
2017 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2024 if (initializer != NULL && current_attr.allocatable
2025 && gfc_current_state () == COMP_DERIVED)
2027 gfc_error ("Initialization of allocatable component at %C is not "
2033 /* Add the initializer. Note that it is fine if initializer is
2034 NULL here, because we sometimes also need to check if a
2035 declaration *must* have an initialization expression. */
2036 if (gfc_current_state () != COMP_DERIVED)
2037 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2040 if (current_ts.type == BT_DERIVED
2041 && !current_attr.pointer && !initializer)
2042 initializer = gfc_default_initializer (¤t_ts);
2043 t = build_struct (name, cl, &initializer, &as);
2046 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
2049 /* Free stuff up and return. */
2050 gfc_free_expr (initializer);
2051 gfc_free_array_spec (as);
2057 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2058 This assumes that the byte size is equal to the kind number for
2059 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2062 gfc_match_old_kind_spec (gfc_typespec *ts)
2067 if (gfc_match_char ('*') != MATCH_YES)
2070 m = gfc_match_small_literal_int (&ts->kind, NULL);
2074 original_kind = ts->kind;
2076 /* Massage the kind numbers for complex types. */
2077 if (ts->type == BT_COMPLEX)
2081 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2082 gfc_basic_typename (ts->type), original_kind);
2089 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2092 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2096 if (gfc_option.flag_real4_kind == 8)
2098 if (gfc_option.flag_real4_kind == 10)
2100 if (gfc_option.flag_real4_kind == 16)
2106 if (gfc_option.flag_real8_kind == 4)
2108 if (gfc_option.flag_real8_kind == 10)
2110 if (gfc_option.flag_real8_kind == 16)
2115 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2117 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2118 gfc_basic_typename (ts->type), original_kind);
2122 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2123 gfc_basic_typename (ts->type), original_kind) == FAILURE)
2130 /* Match a kind specification. Since kinds are generally optional, we
2131 usually return MATCH_NO if something goes wrong. If a "kind="
2132 string is found, then we know we have an error. */
2135 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2147 where = loc = gfc_current_locus;
2152 if (gfc_match_char ('(') == MATCH_NO)
2155 /* Also gobbles optional text. */
2156 if (gfc_match (" kind = ") == MATCH_YES)
2159 loc = gfc_current_locus;
2162 n = gfc_match_init_expr (&e);
2166 if (gfc_matching_function)
2168 /* The function kind expression might include use associated or
2169 imported parameters and try again after the specification
2171 if (gfc_match_char (')') != MATCH_YES)
2173 gfc_error ("Missing right parenthesis at %C");
2179 gfc_undo_symbols ();
2184 /* ....or else, the match is real. */
2186 gfc_error ("Expected initialization expression at %C");
2194 gfc_error ("Expected scalar initialization expression at %C");
2199 msg = gfc_extract_int (e, &ts->kind);
2208 /* Before throwing away the expression, let's see if we had a
2209 C interoperable kind (and store the fact). */
2210 if (e->ts.is_c_interop == 1)
2212 /* Mark this as c interoperable if being declared with one
2213 of the named constants from iso_c_binding. */
2214 ts->is_c_interop = e->ts.is_iso_c;
2215 ts->f90_type = e->ts.f90_type;
2221 /* Ignore errors to this point, if we've gotten here. This means
2222 we ignore the m=MATCH_ERROR from above. */
2223 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2225 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2226 gfc_basic_typename (ts->type));
2227 gfc_current_locus = where;
2231 /* Warn if, e.g., c_int is used for a REAL variable, but not
2232 if, e.g., c_double is used for COMPLEX as the standard
2233 explicitly says that the kind type parameter for complex and real
2234 variable is the same, i.e. c_float == c_float_complex. */
2235 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2236 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2237 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2238 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2239 "is %s", gfc_basic_typename (ts->f90_type), &where,
2240 gfc_basic_typename (ts->type));
2242 gfc_gobble_whitespace ();
2243 if ((c = gfc_next_ascii_char ()) != ')'
2244 && (ts->type != BT_CHARACTER || c != ','))
2246 if (ts->type == BT_CHARACTER)
2247 gfc_error ("Missing right parenthesis or comma at %C");
2249 gfc_error ("Missing right parenthesis at %C");
2253 /* All tests passed. */
2256 if(m == MATCH_ERROR)
2257 gfc_current_locus = where;
2259 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2262 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2266 if (gfc_option.flag_real4_kind == 8)
2268 if (gfc_option.flag_real4_kind == 10)
2270 if (gfc_option.flag_real4_kind == 16)
2276 if (gfc_option.flag_real8_kind == 4)
2278 if (gfc_option.flag_real8_kind == 10)
2280 if (gfc_option.flag_real8_kind == 16)
2285 /* Return what we know from the test(s). */
2290 gfc_current_locus = where;
2296 match_char_kind (int * kind, int * is_iso_c)
2305 where = gfc_current_locus;
2307 n = gfc_match_init_expr (&e);
2309 if (n != MATCH_YES && gfc_matching_function)
2311 /* The expression might include use-associated or imported
2312 parameters and try again after the specification
2315 gfc_undo_symbols ();
2320 gfc_error ("Expected initialization expression at %C");
2326 gfc_error ("Expected scalar initialization expression at %C");
2331 msg = gfc_extract_int (e, kind);
2332 *is_iso_c = e->ts.is_iso_c;
2342 /* Ignore errors to this point, if we've gotten here. This means
2343 we ignore the m=MATCH_ERROR from above. */
2344 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2346 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2350 /* All tests passed. */
2353 if (m == MATCH_ERROR)
2354 gfc_current_locus = where;
2356 /* Return what we know from the test(s). */
2361 gfc_current_locus = where;
2366 /* Match the various kind/length specifications in a CHARACTER
2367 declaration. We don't return MATCH_NO. */
2370 gfc_match_char_spec (gfc_typespec *ts)
2372 int kind, seen_length, is_iso_c;
2384 /* Try the old-style specification first. */
2385 old_char_selector = 0;
2387 m = match_char_length (&len, &deferred);
2391 old_char_selector = 1;
2396 m = gfc_match_char ('(');
2399 m = MATCH_YES; /* Character without length is a single char. */
2403 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2404 if (gfc_match (" kind =") == MATCH_YES)
2406 m = match_char_kind (&kind, &is_iso_c);
2408 if (m == MATCH_ERROR)
2413 if (gfc_match (" , len =") == MATCH_NO)
2416 m = char_len_param_value (&len, &deferred);
2419 if (m == MATCH_ERROR)
2426 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2427 if (gfc_match (" len =") == MATCH_YES)
2429 m = char_len_param_value (&len, &deferred);
2432 if (m == MATCH_ERROR)
2436 if (gfc_match_char (')') == MATCH_YES)
2439 if (gfc_match (" , kind =") != MATCH_YES)
2442 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2448 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2449 m = char_len_param_value (&len, &deferred);
2452 if (m == MATCH_ERROR)
2456 m = gfc_match_char (')');
2460 if (gfc_match_char (',') != MATCH_YES)
2463 gfc_match (" kind ="); /* Gobble optional text. */
2465 m = match_char_kind (&kind, &is_iso_c);
2466 if (m == MATCH_ERROR)
2472 /* Require a right-paren at this point. */
2473 m = gfc_match_char (')');
2478 gfc_error ("Syntax error in CHARACTER declaration at %C");
2480 gfc_free_expr (len);
2484 /* Deal with character functions after USE and IMPORT statements. */
2485 if (gfc_matching_function)
2487 gfc_free_expr (len);
2488 gfc_undo_symbols ();
2494 gfc_free_expr (len);
2498 /* Do some final massaging of the length values. */
2499 cl = gfc_new_charlen (gfc_current_ns, NULL);
2501 if (seen_length == 0)
2502 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2507 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2508 ts->deferred = deferred;
2510 /* We have to know if it was a c interoperable kind so we can
2511 do accurate type checking of bind(c) procs, etc. */
2513 /* Mark this as c interoperable if being declared with one
2514 of the named constants from iso_c_binding. */
2515 ts->is_c_interop = is_iso_c;
2516 else if (len != NULL)
2517 /* Here, we might have parsed something such as: character(c_char)
2518 In this case, the parsing code above grabs the c_char when
2519 looking for the length (line 1690, roughly). it's the last
2520 testcase for parsing the kind params of a character variable.
2521 However, it's not actually the length. this seems like it
2523 To see if the user used a C interop kind, test the expr
2524 of the so called length, and see if it's C interoperable. */
2525 ts->is_c_interop = len->ts.is_iso_c;
2531 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2532 structure to the matched specification. This is necessary for FUNCTION and
2533 IMPLICIT statements.
2535 If implicit_flag is nonzero, then we don't check for the optional
2536 kind specification. Not doing so is needed for matching an IMPLICIT
2537 statement correctly. */
2540 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2542 char name[GFC_MAX_SYMBOL_LEN + 1];
2543 gfc_symbol *sym, *dt_sym;
2546 bool seen_deferred_kind, matched_type;
2547 const char *dt_name;
2549 /* A belt and braces check that the typespec is correctly being treated
2550 as a deferred characteristic association. */
2551 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2552 && (gfc_current_block ()->result->ts.kind == -1)
2553 && (ts->kind == -1);
2555 if (seen_deferred_kind)
2558 /* Clear the current binding label, in case one is given. */
2559 curr_binding_label = NULL;
2561 if (gfc_match (" byte") == MATCH_YES)
2563 if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2567 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2569 gfc_error ("BYTE type used at %C "
2570 "is not available on the target machine");
2574 ts->type = BT_INTEGER;
2580 m = gfc_match (" type ( %n", name);
2581 matched_type = (m == MATCH_YES);
2583 if ((matched_type && strcmp ("integer", name) == 0)
2584 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2586 ts->type = BT_INTEGER;
2587 ts->kind = gfc_default_integer_kind;
2591 if ((matched_type && strcmp ("character", name) == 0)
2592 || (!matched_type && gfc_match (" character") == MATCH_YES))
2595 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2596 "intrinsic-type-spec at %C") == FAILURE)
2599 ts->type = BT_CHARACTER;
2600 if (implicit_flag == 0)
2601 m = gfc_match_char_spec (ts);
2605 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2611 if ((matched_type && strcmp ("real", name) == 0)
2612 || (!matched_type && gfc_match (" real") == MATCH_YES))
2615 ts->kind = gfc_default_real_kind;
2620 && (strcmp ("doubleprecision", name) == 0
2621 || (strcmp ("double", name) == 0
2622 && gfc_match (" precision") == MATCH_YES)))
2623 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2626 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2627 "intrinsic-type-spec at %C") == FAILURE)
2629 if (matched_type && gfc_match_char (')') != MATCH_YES)
2633 ts->kind = gfc_default_double_kind;
2637 if ((matched_type && strcmp ("complex", name) == 0)
2638 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2640 ts->type = BT_COMPLEX;
2641 ts->kind = gfc_default_complex_kind;
2646 && (strcmp ("doublecomplex", name) == 0
2647 || (strcmp ("double", name) == 0
2648 && gfc_match (" complex") == MATCH_YES)))
2649 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2651 if (gfc_notify_std (GFC_STD_GNU, "Extension: DOUBLE COMPLEX at %C")
2656 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2657 "intrinsic-type-spec at %C") == FAILURE)
2660 if (matched_type && gfc_match_char (')') != MATCH_YES)
2663 ts->type = BT_COMPLEX;
2664 ts->kind = gfc_default_double_kind;
2668 if ((matched_type && strcmp ("logical", name) == 0)
2669 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2671 ts->type = BT_LOGICAL;
2672 ts->kind = gfc_default_logical_kind;
2677 m = gfc_match_char (')');
2680 ts->type = BT_DERIVED;
2683 /* Match CLASS declarations. */
2684 m = gfc_match (" class ( * )");
2685 if (m == MATCH_ERROR)
2687 else if (m == MATCH_YES)
2689 gfc_fatal_error ("Unlimited polymorphism at %C not yet supported");
2693 m = gfc_match (" class ( %n )", name);
2696 ts->type = BT_CLASS;
2698 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2703 /* Defer association of the derived type until the end of the
2704 specification block. However, if the derived type can be
2705 found, add it to the typespec. */
2706 if (gfc_matching_function)
2708 ts->u.derived = NULL;
2709 if (gfc_current_state () != COMP_INTERFACE
2710 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2712 sym = gfc_find_dt_in_generic (sym);
2713 ts->u.derived = sym;
2718 /* Search for the name but allow the components to be defined later. If
2719 type = -1, this typespec has been seen in a function declaration but
2720 the type could not be accessed at that point. The actual derived type is
2721 stored in a symtree with the first letter of the name captialized; the
2722 symtree with the all lower-case name contains the associated
2723 generic function. */
2724 dt_name = gfc_get_string ("%c%s",
2725 (char) TOUPPER ((unsigned char) name[0]),
2726 (const char*)&name[1]);
2731 gfc_get_ha_symbol (name, &sym);
2732 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2734 gfc_error ("Type name '%s' at %C is ambiguous", name);
2737 if (sym->generic && !dt_sym)
2738 dt_sym = gfc_find_dt_in_generic (sym);
2740 else if (ts->kind == -1)
2742 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2743 || gfc_current_ns->has_import_set;
2744 gfc_find_symbol (name, NULL, iface, &sym);
2745 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2747 gfc_error ("Type name '%s' at %C is ambiguous", name);
2750 if (sym && sym->generic && !dt_sym)
2751 dt_sym = gfc_find_dt_in_generic (sym);
2758 if ((sym->attr.flavor != FL_UNKNOWN
2759 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2760 || sym->attr.subroutine)
2762 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2763 "entity at %L, which has the same name", name,
2768 gfc_set_sym_referenced (sym);
2769 if (!sym->attr.generic
2770 && gfc_add_generic (&sym->attr, sym->name, NULL) == FAILURE)
2773 if (!sym->attr.function
2774 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2779 gfc_interface *intr, *head;
2781 /* Use upper case to save the actual derived-type symbol. */
2782 gfc_get_symbol (dt_name, NULL, &dt_sym);
2783 dt_sym->name = gfc_get_string (sym->name);
2784 head = sym->generic;
2785 intr = gfc_get_interface ();
2787 intr->where = gfc_current_locus;
2789 sym->generic = intr;
2790 sym->attr.if_source = IFSRC_DECL;
2793 gfc_set_sym_referenced (dt_sym);
2795 if (dt_sym->attr.flavor != FL_DERIVED
2796 && gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL)
2800 ts->u.derived = dt_sym;
2806 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2807 "intrinsic-type-spec at %C") == FAILURE)
2810 /* For all types except double, derived and character, look for an
2811 optional kind specifier. MATCH_NO is actually OK at this point. */
2812 if (implicit_flag == 1)
2814 if (matched_type && gfc_match_char (')') != MATCH_YES)
2820 if (gfc_current_form == FORM_FREE)
2822 c = gfc_peek_ascii_char ();
2823 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2824 && c != ':' && c != ',')
2826 if (matched_type && c == ')')
2828 gfc_next_ascii_char ();
2835 m = gfc_match_kind_spec (ts, false);
2836 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2837 m = gfc_match_old_kind_spec (ts);
2839 if (matched_type && gfc_match_char (')') != MATCH_YES)
2842 /* Defer association of the KIND expression of function results
2843 until after USE and IMPORT statements. */
2844 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2845 || gfc_matching_function)
2849 m = MATCH_YES; /* No kind specifier found. */
2855 /* Match an IMPLICIT NONE statement. Actually, this statement is
2856 already matched in parse.c, or we would not end up here in the
2857 first place. So the only thing we need to check, is if there is
2858 trailing garbage. If not, the match is successful. */
2861 gfc_match_implicit_none (void)
2863 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2867 /* Match the letter range(s) of an IMPLICIT statement. */
2870 match_implicit_range (void)
2876 cur_loc = gfc_current_locus;
2878 gfc_gobble_whitespace ();
2879 c = gfc_next_ascii_char ();
2882 gfc_error ("Missing character range in IMPLICIT at %C");
2889 gfc_gobble_whitespace ();
2890 c1 = gfc_next_ascii_char ();
2894 gfc_gobble_whitespace ();
2895 c = gfc_next_ascii_char ();
2900 inner = 0; /* Fall through. */
2907 gfc_gobble_whitespace ();
2908 c2 = gfc_next_ascii_char ();
2912 gfc_gobble_whitespace ();
2913 c = gfc_next_ascii_char ();
2915 if ((c != ',') && (c != ')'))
2928 gfc_error ("Letters must be in alphabetic order in "
2929 "IMPLICIT statement at %C");
2933 /* See if we can add the newly matched range to the pending
2934 implicits from this IMPLICIT statement. We do not check for
2935 conflicts with whatever earlier IMPLICIT statements may have
2936 set. This is done when we've successfully finished matching
2938 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2945 gfc_syntax_error (ST_IMPLICIT);
2947 gfc_current_locus = cur_loc;
2952 /* Match an IMPLICIT statement, storing the types for
2953 gfc_set_implicit() if the statement is accepted by the parser.
2954 There is a strange looking, but legal syntactic construction
2955 possible. It looks like:
2957 IMPLICIT INTEGER (a-b) (c-d)
2959 This is legal if "a-b" is a constant expression that happens to
2960 equal one of the legal kinds for integers. The real problem
2961 happens with an implicit specification that looks like:
2963 IMPLICIT INTEGER (a-b)
2965 In this case, a typespec matcher that is "greedy" (as most of the
2966 matchers are) gobbles the character range as a kindspec, leaving
2967 nothing left. We therefore have to go a bit more slowly in the
2968 matching process by inhibiting the kindspec checking during
2969 typespec matching and checking for a kind later. */
2972 gfc_match_implicit (void)
2981 /* We don't allow empty implicit statements. */
2982 if (gfc_match_eos () == MATCH_YES)
2984 gfc_error ("Empty IMPLICIT statement at %C");
2990 /* First cleanup. */
2991 gfc_clear_new_implicit ();
2993 /* A basic type is mandatory here. */
2994 m = gfc_match_decl_type_spec (&ts, 1);
2995 if (m == MATCH_ERROR)
3000 cur_loc = gfc_current_locus;
3001 m = match_implicit_range ();
3005 /* We may have <TYPE> (<RANGE>). */
3006 gfc_gobble_whitespace ();
3007 c = gfc_next_ascii_char ();
3008 if ((c == '\n') || (c == ','))
3010 /* Check for CHARACTER with no length parameter. */
3011 if (ts.type == BT_CHARACTER && !ts.u.cl)
3013 ts.kind = gfc_default_character_kind;
3014 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3015 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3019 /* Record the Successful match. */
3020 if (gfc_merge_new_implicit (&ts) != SUCCESS)
3025 gfc_current_locus = cur_loc;
3028 /* Discard the (incorrectly) matched range. */
3029 gfc_clear_new_implicit ();
3031 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3032 if (ts.type == BT_CHARACTER)
3033 m = gfc_match_char_spec (&ts);
3036 m = gfc_match_kind_spec (&ts, false);
3039 m = gfc_match_old_kind_spec (&ts);
3040 if (m == MATCH_ERROR)
3046 if (m == MATCH_ERROR)
3049 m = match_implicit_range ();
3050 if (m == MATCH_ERROR)
3055 gfc_gobble_whitespace ();
3056 c = gfc_next_ascii_char ();
3057 if ((c != '\n') && (c != ','))
3060 if (gfc_merge_new_implicit (&ts) != SUCCESS)
3068 gfc_syntax_error (ST_IMPLICIT);
3076 gfc_match_import (void)
3078 char name[GFC_MAX_SYMBOL_LEN + 1];
3083 if (gfc_current_ns->proc_name == NULL
3084 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3086 gfc_error ("IMPORT statement at %C only permitted in "
3087 "an INTERFACE body");
3091 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
3095 if (gfc_match_eos () == MATCH_YES)
3097 /* All host variables should be imported. */
3098 gfc_current_ns->has_import_set = 1;
3102 if (gfc_match (" ::") == MATCH_YES)
3104 if (gfc_match_eos () == MATCH_YES)
3106 gfc_error ("Expecting list of named entities at %C");
3114 m = gfc_match (" %n", name);
3118 if (gfc_current_ns->parent != NULL
3119 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3121 gfc_error ("Type name '%s' at %C is ambiguous", name);
3124 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3125 && gfc_find_symbol (name,
3126 gfc_current_ns->proc_name->ns->parent,
3129 gfc_error ("Type name '%s' at %C is ambiguous", name);
3135 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3136 "at %C - does not exist.", name);
3140 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3142 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3147 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3150 sym->attr.imported = 1;
3152 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3154 /* The actual derived type is stored in a symtree with the first
3155 letter of the name captialized; the symtree with the all
3156 lower-case name contains the associated generic function. */
3157 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3158 gfc_get_string ("%c%s",
3159 (char) TOUPPER ((unsigned char) name[0]),
3163 sym->attr.imported = 1;
3176 if (gfc_match_eos () == MATCH_YES)
3178 if (gfc_match_char (',') != MATCH_YES)
3185 gfc_error ("Syntax error in IMPORT statement at %C");
3190 /* A minimal implementation of gfc_match without whitespace, escape
3191 characters or variable arguments. Returns true if the next
3192 characters match the TARGET template exactly. */
3195 match_string_p (const char *target)
3199 for (p = target; *p; p++)
3200 if ((char) gfc_next_ascii_char () != *p)
3205 /* Matches an attribute specification including array specs. If
3206 successful, leaves the variables current_attr and current_as
3207 holding the specification. Also sets the colon_seen variable for
3208 later use by matchers associated with initializations.
3210 This subroutine is a little tricky in the sense that we don't know
3211 if we really have an attr-spec until we hit the double colon.
3212 Until that time, we can only return MATCH_NO. This forces us to
3213 check for duplicate specification at this level. */
3216 match_attr_spec (void)
3218 /* Modifiers that can exist in a type statement. */
3220 { GFC_DECL_BEGIN = 0,
3221 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3222 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3223 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3224 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3225 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3226 DECL_NONE, GFC_DECL_END /* Sentinel */
3230 /* GFC_DECL_END is the sentinel, index starts at 0. */
3231 #define NUM_DECL GFC_DECL_END
3233 locus start, seen_at[NUM_DECL];
3240 gfc_clear_attr (¤t_attr);
3241 start = gfc_current_locus;
3246 /* See if we get all of the keywords up to the final double colon. */
3247 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3255 gfc_gobble_whitespace ();
3257 ch = gfc_next_ascii_char ();
3260 /* This is the successful exit condition for the loop. */
3261 if (gfc_next_ascii_char () == ':')
3266 gfc_gobble_whitespace ();
3267 switch (gfc_peek_ascii_char ())
3270 gfc_next_ascii_char ();
3271 switch (gfc_next_ascii_char ())
3274 if (match_string_p ("locatable"))
3276 /* Matched "allocatable". */
3277 d = DECL_ALLOCATABLE;
3282 if (match_string_p ("ynchronous"))
3284 /* Matched "asynchronous". */
3285 d = DECL_ASYNCHRONOUS;
3292 /* Try and match the bind(c). */
3293 m = gfc_match_bind_c (NULL, true);
3296 else if (m == MATCH_ERROR)
3301 gfc_next_ascii_char ();
3302 if ('o' != gfc_next_ascii_char ())
3304 switch (gfc_next_ascii_char ())
3307 if (match_string_p ("imension"))
3309 d = DECL_CODIMENSION;
3313 if (match_string_p ("tiguous"))
3315 d = DECL_CONTIGUOUS;
3322 if (match_string_p ("dimension"))
3327 if (match_string_p ("external"))
3332 if (match_string_p ("int"))
3334 ch = gfc_next_ascii_char ();
3337 if (match_string_p ("nt"))
3339 /* Matched "intent". */
3340 /* TODO: Call match_intent_spec from here. */
3341 if (gfc_match (" ( in out )") == MATCH_YES)
3343 else if (gfc_match (" ( in )") == MATCH_YES)
3345 else if (gfc_match (" ( out )") == MATCH_YES)
3351 if (match_string_p ("insic"))
3353 /* Matched "intrinsic". */
3361 if (match_string_p ("optional"))
3366 gfc_next_ascii_char ();
3367 switch (gfc_next_ascii_char ())
3370 if (match_string_p ("rameter"))
3372 /* Matched "parameter". */
3378 if (match_string_p ("inter"))
3380 /* Matched "pointer". */
3386 ch = gfc_next_ascii_char ();
3389 if (match_string_p ("vate"))
3391 /* Matched "private". */
3397 if (match_string_p ("tected"))
3399 /* Matched "protected". */
3406 if (match_string_p ("blic"))
3408 /* Matched "public". */
3416 if (match_string_p ("save"))
3421 if (match_string_p ("target"))
3426 gfc_next_ascii_char ();
3427 ch = gfc_next_ascii_char ();
3430 if (match_string_p ("lue"))
3432 /* Matched "value". */
3438 if (match_string_p ("latile"))
3440 /* Matched "volatile". */
3448 /* No double colon and no recognizable decl_type, so assume that
3449 we've been looking at something else the whole time. */
3456 /* Check to make sure any parens are paired up correctly. */
3457 if (gfc_match_parens () == MATCH_ERROR)
3464 seen_at[d] = gfc_current_locus;
3466 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3468 gfc_array_spec *as = NULL;
3470 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3471 d == DECL_CODIMENSION);
3473 if (current_as == NULL)
3475 else if (m == MATCH_YES)
3477 merge_array_spec (as, current_as, false);
3483 if (d == DECL_CODIMENSION)
3484 gfc_error ("Missing codimension specification at %C");
3486 gfc_error ("Missing dimension specification at %C");
3490 if (m == MATCH_ERROR)
3495 /* Since we've seen a double colon, we have to be looking at an
3496 attr-spec. This means that we can now issue errors. */
3497 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3502 case DECL_ALLOCATABLE:
3503 attr = "ALLOCATABLE";
3505 case DECL_ASYNCHRONOUS:
3506 attr = "ASYNCHRONOUS";
3508 case DECL_CODIMENSION:
3509 attr = "CODIMENSION";
3511 case DECL_CONTIGUOUS:
3512 attr = "CONTIGUOUS";
3514 case DECL_DIMENSION:
3521 attr = "INTENT (IN)";
3524 attr = "INTENT (OUT)";
3527 attr = "INTENT (IN OUT)";
3529 case DECL_INTRINSIC:
3535 case DECL_PARAMETER:
3541 case DECL_PROTECTED:
3556 case DECL_IS_BIND_C:
3566 attr = NULL; /* This shouldn't happen. */
3569 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3574 /* Now that we've dealt with duplicate attributes, add the attributes
3575 to the current attribute. */
3576 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3581 if (gfc_current_state () == COMP_DERIVED
3582 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3583 && d != DECL_POINTER && d != DECL_PRIVATE
3584 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3586 if (d == DECL_ALLOCATABLE)
3588 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3589 "attribute at %C in a TYPE definition")
3598 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3605 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3606 && gfc_current_state () != COMP_MODULE)
3608 if (d == DECL_PRIVATE)
3612 if (gfc_current_state () == COMP_DERIVED
3613 && gfc_state_stack->previous
3614 && gfc_state_stack->previous->state == COMP_MODULE)
3616 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3617 "at %L in a TYPE definition", attr,
3627 gfc_error ("%s attribute at %L is not allowed outside of the "
3628 "specification part of a module", attr, &seen_at[d]);
3636 case DECL_ALLOCATABLE:
3637 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
3640 case DECL_ASYNCHRONOUS:
3641 if (gfc_notify_std (GFC_STD_F2003,
3642 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3646 t = gfc_add_asynchronous (¤t_attr, NULL, &seen_at[d]);
3649 case DECL_CODIMENSION:
3650 t = gfc_add_codimension (¤t_attr, NULL, &seen_at[d]);
3653 case DECL_CONTIGUOUS:
3654 if (gfc_notify_std (GFC_STD_F2008,
3655 "Fortran 2008: CONTIGUOUS attribute at %C")
3659 t = gfc_add_contiguous (¤t_attr, NULL, &seen_at[d]);
3662 case DECL_DIMENSION:
3663 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
3667 t = gfc_add_external (¤t_attr, &seen_at[d]);
3671 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
3675 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
3679 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
3682 case DECL_INTRINSIC:
3683 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
3687 t = gfc_add_optional (¤t_attr, &seen_at[d]);
3690 case DECL_PARAMETER:
3691 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
3695 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
3698 case DECL_PROTECTED:
3699 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3701 gfc_error ("PROTECTED at %C only allowed in specification "
3702 "part of a module");
3707 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3712 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
3716 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
3721 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
3726 t = gfc_add_save (¤t_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3730 t = gfc_add_target (¤t_attr, &seen_at[d]);
3733 case DECL_IS_BIND_C:
3734 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
3738 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3743 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
3747 if (gfc_notify_std (GFC_STD_F2003,
3748 "Fortran 2003: VOLATILE attribute at %C")
3752 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
3756 gfc_internal_error ("match_attr_spec(): Bad attribute");
3766 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3767 if (gfc_current_state () == COMP_MODULE && !current_attr.save
3768 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
3769 current_attr.save = SAVE_IMPLICIT;
3775 gfc_current_locus = start;
3776 gfc_free_array_spec (current_as);
3782 /* Set the binding label, dest_label, either with the binding label
3783 stored in the given gfc_typespec, ts, or if none was provided, it
3784 will be the symbol name in all lower case, as required by the draft
3785 (J3/04-007, section 15.4.1). If a binding label was given and
3786 there is more than one argument (num_idents), it is an error. */
3789 set_binding_label (const char **dest_label, const char *sym_name,
3792 if (num_idents > 1 && has_name_equals)
3794 gfc_error ("Multiple identifiers provided with "
3795 "single NAME= specifier at %C");
3799 if (curr_binding_label)
3800 /* Binding label given; store in temp holder til have sym. */
3801 *dest_label = curr_binding_label;
3804 /* No binding label given, and the NAME= specifier did not exist,
3805 which means there was no NAME="". */
3806 if (sym_name != NULL && has_name_equals == 0)
3807 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
3814 /* Set the status of the given common block as being BIND(C) or not,
3815 depending on the given parameter, is_bind_c. */
3818 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3820 com_block->is_bind_c = is_bind_c;
3825 /* Verify that the given gfc_typespec is for a C interoperable type. */
3828 gfc_verify_c_interop (gfc_typespec *ts)
3830 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3831 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3832 ? SUCCESS : FAILURE;
3833 else if (ts->type == BT_CLASS)
3835 else if (ts->is_c_interop != 1)
3842 /* Verify that the variables of a given common block, which has been
3843 defined with the attribute specifier bind(c), to be of a C
3844 interoperable type. Errors will be reported here, if
3848 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3850 gfc_symbol *curr_sym = NULL;
3851 gfc_try retval = SUCCESS;
3853 curr_sym = com_block->head;
3855 /* Make sure we have at least one symbol. */
3856 if (curr_sym == NULL)
3859 /* Here we know we have a symbol, so we'll execute this loop
3863 /* The second to last param, 1, says this is in a common block. */
3864 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3865 curr_sym = curr_sym->common_next;
3866 } while (curr_sym != NULL);
3872 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3873 an appropriate error message is reported. */
3876 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3877 int is_in_common, gfc_common_head *com_block)
3879 bool bind_c_function = false;
3880 gfc_try retval = SUCCESS;
3882 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3883 bind_c_function = true;
3885 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3887 tmp_sym = tmp_sym->result;
3888 /* Make sure it wasn't an implicitly typed result. */
3889 if (tmp_sym->attr.implicit_type)
3891 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3892 "%L may not be C interoperable", tmp_sym->name,
3893 &tmp_sym->declared_at);
3894 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3895 /* Mark it as C interoperable to prevent duplicate warnings. */
3896 tmp_sym->ts.is_c_interop = 1;
3897 tmp_sym->attr.is_c_interop = 1;
3901 /* Here, we know we have the bind(c) attribute, so if we have
3902 enough type info, then verify that it's a C interop kind.
3903 The info could be in the symbol already, or possibly still in
3904 the given ts (current_ts), so look in both. */
3905 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3907 if (gfc_verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3909 /* See if we're dealing with a sym in a common block or not. */
3910 if (is_in_common == 1)
3912 gfc_warning ("Variable '%s' in common block '%s' at %L "
3913 "may not be a C interoperable "
3914 "kind though common block '%s' is BIND(C)",
3915 tmp_sym->name, com_block->name,
3916 &(tmp_sym->declared_at), com_block->name);
3920 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3921 gfc_error ("Type declaration '%s' at %L is not C "
3922 "interoperable but it is BIND(C)",
3923 tmp_sym->name, &(tmp_sym->declared_at));
3925 gfc_warning ("Variable '%s' at %L "
3926 "may not be a C interoperable "
3927 "kind but it is bind(c)",
3928 tmp_sym->name, &(tmp_sym->declared_at));
3932 /* Variables declared w/in a common block can't be bind(c)
3933 since there's no way for C to see these variables, so there's
3934 semantically no reason for the attribute. */
3935 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3937 gfc_error ("Variable '%s' in common block '%s' at "
3938 "%L cannot be declared with BIND(C) "
3939 "since it is not a global",
3940 tmp_sym->name, com_block->name,
3941 &(tmp_sym->declared_at));
3945 /* Scalar variables that are bind(c) can not have the pointer
3946 or allocatable attributes. */
3947 if (tmp_sym->attr.is_bind_c == 1)
3949 if (tmp_sym->attr.pointer == 1)
3951 gfc_error ("Variable '%s' at %L cannot have both the "
3952 "POINTER and BIND(C) attributes",
3953 tmp_sym->name, &(tmp_sym->declared_at));
3957 if (tmp_sym->attr.allocatable == 1)
3959 gfc_error ("Variable '%s' at %L cannot have both the "
3960 "ALLOCATABLE and BIND(C) attributes",
3961 tmp_sym->name, &(tmp_sym->declared_at));
3967 /* If it is a BIND(C) function, make sure the return value is a
3968 scalar value. The previous tests in this function made sure
3969 the type is interoperable. */
3970 if (bind_c_function && tmp_sym->as != NULL)
3971 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3972 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3974 /* BIND(C) functions can not return a character string. */
3975 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3976 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
3977 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
3978 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
3979 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3980 "be a character string", tmp_sym->name,
3981 &(tmp_sym->declared_at));
3984 /* See if the symbol has been marked as private. If it has, make sure
3985 there is no binding label and warn the user if there is one. */
3986 if (tmp_sym->attr.access == ACCESS_PRIVATE
3987 && tmp_sym->binding_label)
3988 /* Use gfc_warning_now because we won't say that the symbol fails
3989 just because of this. */
3990 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3991 "given the binding label '%s'", tmp_sym->name,
3992 &(tmp_sym->declared_at), tmp_sym->binding_label);
3998 /* Set the appropriate fields for a symbol that's been declared as
3999 BIND(C) (the is_bind_c flag and the binding label), and verify that
4000 the type is C interoperable. Errors are reported by the functions
4001 used to set/test these fields. */
4004 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4006 gfc_try retval = SUCCESS;
4008 /* TODO: Do we need to make sure the vars aren't marked private? */
4010 /* Set the is_bind_c bit in symbol_attribute. */
4011 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4013 if (set_binding_label (&tmp_sym->binding_label, tmp_sym->name,
4014 num_idents) != SUCCESS)
4021 /* Set the fields marking the given common block as BIND(C), including
4022 a binding label, and report any errors encountered. */
4025 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4027 gfc_try retval = SUCCESS;
4029 /* destLabel, common name, typespec (which may have binding label). */
4030 if (set_binding_label (&com_block->binding_label, com_block->name,
4035 /* Set the given common block (com_block) to being bind(c) (1). */
4036 set_com_block_bind_c (com_block, 1);
4042 /* Retrieve the list of one or more identifiers that the given bind(c)
4043 attribute applies to. */
4046 get_bind_c_idents (void)
4048 char name[GFC_MAX_SYMBOL_LEN + 1];
4050 gfc_symbol *tmp_sym = NULL;
4052 gfc_common_head *com_block = NULL;
4054 if (gfc_match_name (name) == MATCH_YES)
4056 found_id = MATCH_YES;
4057 gfc_get_ha_symbol (name, &tmp_sym);
4059 else if (match_common_name (name) == MATCH_YES)
4061 found_id = MATCH_YES;
4062 com_block = gfc_get_common (name, 0);
4066 gfc_error ("Need either entity or common block name for "
4067 "attribute specification statement at %C");
4071 /* Save the current identifier and look for more. */
4074 /* Increment the number of identifiers found for this spec stmt. */
4077 /* Make sure we have a sym or com block, and verify that it can
4078 be bind(c). Set the appropriate field(s) and look for more
4080 if (tmp_sym != NULL || com_block != NULL)
4082 if (tmp_sym != NULL)
4084 if (set_verify_bind_c_sym (tmp_sym, num_idents)
4090 if (set_verify_bind_c_com_block(com_block, num_idents)
4095 /* Look to see if we have another identifier. */
4097 if (gfc_match_eos () == MATCH_YES)
4098 found_id = MATCH_NO;
4099 else if (gfc_match_char (',') != MATCH_YES)
4100 found_id = MATCH_NO;
4101 else if (gfc_match_name (name) == MATCH_YES)
4103 found_id = MATCH_YES;
4104 gfc_get_ha_symbol (name, &tmp_sym);
4106 else if (match_common_name (name) == MATCH_YES)
4108 found_id = MATCH_YES;
4109 com_block = gfc_get_common (name, 0);
4113 gfc_error ("Missing entity or common block name for "
4114 "attribute specification statement at %C");
4120 gfc_internal_error ("Missing symbol");
4122 } while (found_id == MATCH_YES);
4124 /* if we get here we were successful */
4129 /* Try and match a BIND(C) attribute specification statement. */
4132 gfc_match_bind_c_stmt (void)
4134 match found_match = MATCH_NO;
4139 /* This may not be necessary. */
4141 /* Clear the temporary binding label holder. */
4142 curr_binding_label = NULL;
4144 /* Look for the bind(c). */
4145 found_match = gfc_match_bind_c (NULL, true);
4147 if (found_match == MATCH_YES)
4149 /* Look for the :: now, but it is not required. */
4152 /* Get the identifier(s) that needs to be updated. This may need to
4153 change to hand the flag(s) for the attr specified so all identifiers
4154 found can have all appropriate parts updated (assuming that the same
4155 spec stmt can have multiple attrs, such as both bind(c) and
4157 if (get_bind_c_idents () != SUCCESS)
4158 /* Error message should have printed already. */
4166 /* Match a data declaration statement. */
4169 gfc_match_data_decl (void)
4175 num_idents_on_line = 0;
4177 m = gfc_match_decl_type_spec (¤t_ts, 0);
4181 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4182 && gfc_current_state () != COMP_DERIVED)
4184 sym = gfc_use_derived (current_ts.u.derived);
4192 current_ts.u.derived = sym;
4195 m = match_attr_spec ();
4196 if (m == MATCH_ERROR)
4202 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4203 && current_ts.u.derived->components == NULL
4204 && !current_ts.u.derived->attr.zero_comp)
4207 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4210 gfc_find_symbol (current_ts.u.derived->name,
4211 current_ts.u.derived->ns, 1, &sym);
4213 /* Any symbol that we find had better be a type definition
4214 which has its components defined. */
4215 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4216 && (current_ts.u.derived->components != NULL
4217 || current_ts.u.derived->attr.zero_comp))
4220 /* Now we have an error, which we signal, and then fix up
4221 because the knock-on is plain and simple confusing. */
4222 gfc_error_now ("Derived type at %C has not been previously defined "
4223 "and so cannot appear in a derived type definition");
4224 current_attr.pointer = 1;
4229 /* If we have an old-style character declaration, and no new-style
4230 attribute specifications, then there a comma is optional between
4231 the type specification and the variable list. */
4232 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4233 gfc_match_char (',');
4235 /* Give the types/attributes to symbols that follow. Give the element
4236 a number so that repeat character length expressions can be copied. */
4240 num_idents_on_line++;
4241 m = variable_decl (elem++);
4242 if (m == MATCH_ERROR)
4247 if (gfc_match_eos () == MATCH_YES)
4249 if (gfc_match_char (',') != MATCH_YES)
4253 if (gfc_error_flag_test () == 0)
4254 gfc_error ("Syntax error in data declaration at %C");
4257 gfc_free_data_all (gfc_current_ns);
4260 gfc_free_array_spec (current_as);
4266 /* Match a prefix associated with a function or subroutine
4267 declaration. If the typespec pointer is nonnull, then a typespec
4268 can be matched. Note that if nothing matches, MATCH_YES is
4269 returned (the null string was matched). */
4272 gfc_match_prefix (gfc_typespec *ts)
4278 gfc_clear_attr (¤t_attr);
4280 seen_impure = false;
4282 gcc_assert (!gfc_matching_prefix);
4283 gfc_matching_prefix = true;
4287 found_prefix = false;
4289 if (!seen_type && ts != NULL
4290 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4291 && gfc_match_space () == MATCH_YES)
4295 found_prefix = true;
4298 if (gfc_match ("elemental% ") == MATCH_YES)
4300 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
4303 found_prefix = true;
4306 if (gfc_match ("pure% ") == MATCH_YES)
4308 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
4311 found_prefix = true;
4314 if (gfc_match ("recursive% ") == MATCH_YES)
4316 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
4319 found_prefix = true;
4322 /* IMPURE is a somewhat special case, as it needs not set an actual
4323 attribute but rather only prevents ELEMENTAL routines from being
4324 automatically PURE. */
4325 if (gfc_match ("impure% ") == MATCH_YES)
4327 if (gfc_notify_std (GFC_STD_F2008,
4328 "Fortran 2008: IMPURE procedure at %C")
4333 found_prefix = true;
4336 while (found_prefix);
4338 /* IMPURE and PURE must not both appear, of course. */
4339 if (seen_impure && current_attr.pure)
4341 gfc_error ("PURE and IMPURE must not appear both at %C");
4345 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4346 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4348 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
4352 /* At this point, the next item is not a prefix. */
4353 gcc_assert (gfc_matching_prefix);
4354 gfc_matching_prefix = false;
4358 gcc_assert (gfc_matching_prefix);
4359 gfc_matching_prefix = false;
4364 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4367 copy_prefix (symbol_attribute *dest, locus *where)
4369 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
4372 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
4375 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
4382 /* Match a formal argument list. */
4385 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4387 gfc_formal_arglist *head, *tail, *p, *q;
4388 char name[GFC_MAX_SYMBOL_LEN + 1];
4394 if (gfc_match_char ('(') != MATCH_YES)
4401 if (gfc_match_char (')') == MATCH_YES)
4406 if (gfc_match_char ('*') == MATCH_YES)
4410 m = gfc_match_name (name);
4414 if (gfc_get_symbol (name, NULL, &sym))
4418 p = gfc_get_formal_arglist ();
4430 /* We don't add the VARIABLE flavor because the name could be a
4431 dummy procedure. We don't apply these attributes to formal
4432 arguments of statement functions. */
4433 if (sym != NULL && !st_flag
4434 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
4435 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
4441 /* The name of a program unit can be in a different namespace,
4442 so check for it explicitly. After the statement is accepted,
4443 the name is checked for especially in gfc_get_symbol(). */
4444 if (gfc_new_block != NULL && sym != NULL
4445 && strcmp (sym->name, gfc_new_block->name) == 0)
4447 gfc_error ("Name '%s' at %C is the name of the procedure",
4453 if (gfc_match_char (')') == MATCH_YES)
4456 m = gfc_match_char (',');
4459 gfc_error ("Unexpected junk in formal argument list at %C");
4465 /* Check for duplicate symbols in the formal argument list. */
4468 for (p = head; p->next; p = p->next)
4473 for (q = p->next; q; q = q->next)
4474 if (p->sym == q->sym)
4476 gfc_error ("Duplicate symbol '%s' in formal argument list "
4477 "at %C", p->sym->name);
4485 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
4495 gfc_free_formal_arglist (head);
4500 /* Match a RESULT specification following a function declaration or
4501 ENTRY statement. Also matches the end-of-statement. */
4504 match_result (gfc_symbol *function, gfc_symbol **result)
4506 char name[GFC_MAX_SYMBOL_LEN + 1];
4510 if (gfc_match (" result (") != MATCH_YES)
4513 m = gfc_match_name (name);
4517 /* Get the right paren, and that's it because there could be the
4518 bind(c) attribute after the result clause. */
4519 if (gfc_match_char(')') != MATCH_YES)
4521 /* TODO: should report the missing right paren here. */
4525 if (strcmp (function->name, name) == 0)
4527 gfc_error ("RESULT variable at %C must be different than function name");
4531 if (gfc_get_symbol (name, NULL, &r))
4534 if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4543 /* Match a function suffix, which could be a combination of a result
4544 clause and BIND(C), either one, or neither. The draft does not
4545 require them to come in a specific order. */
4548 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4550 match is_bind_c; /* Found bind(c). */
4551 match is_result; /* Found result clause. */
4552 match found_match; /* Status of whether we've found a good match. */
4553 char peek_char; /* Character we're going to peek at. */
4554 bool allow_binding_name;
4556 /* Initialize to having found nothing. */
4557 found_match = MATCH_NO;
4558 is_bind_c = MATCH_NO;
4559 is_result = MATCH_NO;
4561 /* Get the next char to narrow between result and bind(c). */
4562 gfc_gobble_whitespace ();
4563 peek_char = gfc_peek_ascii_char ();
4565 /* C binding names are not allowed for internal procedures. */
4566 if (gfc_current_state () == COMP_CONTAINS
4567 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4568 allow_binding_name = false;
4570 allow_binding_name = true;
4575 /* Look for result clause. */
4576 is_result = match_result (sym, result);
4577 if (is_result == MATCH_YES)
4579 /* Now see if there is a bind(c) after it. */
4580 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4581 /* We've found the result clause and possibly bind(c). */
4582 found_match = MATCH_YES;
4585 /* This should only be MATCH_ERROR. */
4586 found_match = is_result;
4589 /* Look for bind(c) first. */
4590 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4591 if (is_bind_c == MATCH_YES)
4593 /* Now see if a result clause followed it. */
4594 is_result = match_result (sym, result);
4595 found_match = MATCH_YES;
4599 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4600 found_match = MATCH_ERROR;
4604 gfc_error ("Unexpected junk after function declaration at %C");
4605 found_match = MATCH_ERROR;
4609 if (is_bind_c == MATCH_YES)
4611 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4612 if (gfc_current_state () == COMP_CONTAINS
4613 && sym->ns->proc_name->attr.flavor != FL_MODULE
4614 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4615 "at %L may not be specified for an internal "
4616 "procedure", &gfc_current_locus)
4620 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4629 /* Procedure pointer return value without RESULT statement:
4630 Add "hidden" result variable named "ppr@". */
4633 add_hidden_procptr_result (gfc_symbol *sym)
4637 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4640 /* First usage case: PROCEDURE and EXTERNAL statements. */
4641 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4642 && strcmp (gfc_current_block ()->name, sym->name) == 0
4643 && sym->attr.external;
4644 /* Second usage case: INTERFACE statements. */
4645 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4646 && gfc_state_stack->previous->state == COMP_FUNCTION
4647 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4653 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4657 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4658 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4659 st2->n.sym = stree->n.sym;
4661 sym->result = stree->n.sym;
4663 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4664 sym->result->attr.pointer = sym->attr.pointer;
4665 sym->result->attr.external = sym->attr.external;
4666 sym->result->attr.referenced = sym->attr.referenced;
4667 sym->result->ts = sym->ts;
4668 sym->attr.proc_pointer = 0;
4669 sym->attr.pointer = 0;
4670 sym->attr.external = 0;
4671 if (sym->result->attr.external && sym->result->attr.pointer)
4673 sym->result->attr.pointer = 0;
4674 sym->result->attr.proc_pointer = 1;
4677 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4679 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4680 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4681 && sym->result && sym->result != sym && sym->result->attr.external
4682 && sym == gfc_current_ns->proc_name
4683 && sym == sym->result->ns->proc_name
4684 && strcmp ("ppr@", sym->result->name) == 0)
4686 sym->result->attr.proc_pointer = 1;
4687 sym->attr.pointer = 0;
4695 /* Match the interface for a PROCEDURE declaration,
4696 including brackets (R1212). */
4699 match_procedure_interface (gfc_symbol **proc_if)
4703 locus old_loc, entry_loc;
4704 gfc_namespace *old_ns = gfc_current_ns;
4705 char name[GFC_MAX_SYMBOL_LEN + 1];
4707 old_loc = entry_loc = gfc_current_locus;
4708 gfc_clear_ts (¤t_ts);
4710 if (gfc_match (" (") != MATCH_YES)
4712 gfc_current_locus = entry_loc;
4716 /* Get the type spec. for the procedure interface. */
4717 old_loc = gfc_current_locus;
4718 m = gfc_match_decl_type_spec (¤t_ts, 0);
4719 gfc_gobble_whitespace ();
4720 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4723 if (m == MATCH_ERROR)
4726 /* Procedure interface is itself a procedure. */
4727 gfc_current_locus = old_loc;
4728 m = gfc_match_name (name);
4730 /* First look to see if it is already accessible in the current
4731 namespace because it is use associated or contained. */
4733 if (gfc_find_sym_tree (name, NULL, 0, &st))
4736 /* If it is still not found, then try the parent namespace, if it
4737 exists and create the symbol there if it is still not found. */
4738 if (gfc_current_ns->parent)
4739 gfc_current_ns = gfc_current_ns->parent;
4740 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4743 gfc_current_ns = old_ns;
4744 *proc_if = st->n.sym;
4746 /* Various interface checks. */
4750 /* Resolve interface if possible. That way, attr.procedure is only set
4751 if it is declared by a later procedure-declaration-stmt, which is
4752 invalid per C1212. */
4753 while ((*proc_if)->ts.interface)
4754 *proc_if = (*proc_if)->ts.interface;
4756 if ((*proc_if)->generic)
4758 gfc_error ("Interface '%s' at %C may not be generic",
4762 if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
4764 gfc_error ("Interface '%s' at %C may not be a statement function",
4768 /* Handle intrinsic procedures. */
4769 if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4770 || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4771 && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4772 || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4773 (*proc_if)->attr.intrinsic = 1;
4774 if ((*proc_if)->attr.intrinsic
4775 && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
4777 gfc_error ("Intrinsic procedure '%s' not allowed "
4778 "in PROCEDURE statement at %C", (*proc_if)->name);
4784 if (gfc_match (" )") != MATCH_YES)
4786 gfc_current_locus = entry_loc;
4794 /* Match a PROCEDURE declaration (R1211). */
4797 match_procedure_decl (void)
4800 gfc_symbol *sym, *proc_if = NULL;
4802 gfc_expr *initializer = NULL;
4804 /* Parse interface (with brackets). */
4805 m = match_procedure_interface (&proc_if);
4809 /* Parse attributes (with colons). */
4810 m = match_attr_spec();
4811 if (m == MATCH_ERROR)
4814 /* Get procedure symbols. */
4817 m = gfc_match_symbol (&sym, 0);
4820 else if (m == MATCH_ERROR)
4823 /* Add current_attr to the symbol attributes. */
4824 if (gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
4827 if (sym->attr.is_bind_c)
4829 /* Check for C1218. */
4830 if (!proc_if || !proc_if->attr.is_bind_c)
4832 gfc_error ("BIND(C) attribute at %C requires "
4833 "an interface with BIND(C)");
4836 /* Check for C1217. */
4837 if (has_name_equals && sym->attr.pointer)
4839 gfc_error ("BIND(C) procedure with NAME may not have "
4840 "POINTER attribute at %C");
4843 if (has_name_equals && sym->attr.dummy)
4845 gfc_error ("Dummy procedure at %C may not have "
4846 "BIND(C) attribute with NAME");
4849 /* Set binding label for BIND(C). */
4850 if (set_binding_label (&sym->binding_label, sym->name, num)
4855 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4858 if (add_hidden_procptr_result (sym) == SUCCESS)
4861 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4864 /* Set interface. */
4865 if (proc_if != NULL)
4867 if (sym->ts.type != BT_UNKNOWN)
4869 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4870 sym->name, &gfc_current_locus,
4871 gfc_basic_typename (sym->ts.type));
4874 sym->ts.interface = proc_if;
4875 sym->attr.untyped = 1;
4876 sym->attr.if_source = IFSRC_IFBODY;
4878 else if (current_ts.type != BT_UNKNOWN)
4880 if (gfc_add_type (sym, ¤t_ts, &gfc_current_locus) == FAILURE)
4882 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4883 sym->ts.interface->ts = current_ts;
4884 sym->ts.interface->attr.flavor = FL_PROCEDURE;
4885 sym->ts.interface->attr.function = 1;
4886 sym->attr.function = 1;
4887 sym->attr.if_source = IFSRC_UNKNOWN;
4890 if (gfc_match (" =>") == MATCH_YES)
4892 if (!current_attr.pointer)
4894 gfc_error ("Initialization at %C isn't for a pointer variable");
4899 m = match_pointer_init (&initializer, 1);
4903 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4909 gfc_set_sym_referenced (sym);
4911 if (gfc_match_eos () == MATCH_YES)
4913 if (gfc_match_char (',') != MATCH_YES)
4918 gfc_error ("Syntax error in PROCEDURE statement at %C");
4922 /* Free stuff up and return. */
4923 gfc_free_expr (initializer);
4929 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4932 /* Match a procedure pointer component declaration (R445). */
4935 match_ppc_decl (void)
4938 gfc_symbol *proc_if = NULL;
4942 gfc_expr *initializer = NULL;
4943 gfc_typebound_proc* tb;
4944 char name[GFC_MAX_SYMBOL_LEN + 1];
4946 /* Parse interface (with brackets). */
4947 m = match_procedure_interface (&proc_if);
4951 /* Parse attributes. */
4952 tb = XCNEW (gfc_typebound_proc);
4953 tb->where = gfc_current_locus;
4954 m = match_binding_attributes (tb, false, true);
4955 if (m == MATCH_ERROR)
4958 gfc_clear_attr (¤t_attr);
4959 current_attr.procedure = 1;
4960 current_attr.proc_pointer = 1;
4961 current_attr.access = tb->access;
4962 current_attr.flavor = FL_PROCEDURE;
4964 /* Match the colons (required). */
4965 if (gfc_match (" ::") != MATCH_YES)
4967 gfc_error ("Expected '::' after binding-attributes at %C");
4971 /* Check for C450. */
4972 if (!tb->nopass && proc_if == NULL)
4974 gfc_error("NOPASS or explicit interface required at %C");
4978 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure pointer "
4979 "component at %C") == FAILURE)
4982 /* Match PPC names. */
4986 m = gfc_match_name (name);
4989 else if (m == MATCH_ERROR)
4992 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
4995 /* Add current_attr to the symbol attributes. */
4996 if (gfc_copy_attr (&c->attr, ¤t_attr, NULL) == FAILURE)
4999 if (gfc_add_external (&c->attr, NULL) == FAILURE)
5002 if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
5007 /* Set interface. */
5008 if (proc_if != NULL)
5010 c->ts.interface = proc_if;
5011 c->attr.untyped = 1;
5012 c->attr.if_source = IFSRC_IFBODY;
5014 else if (ts.type != BT_UNKNOWN)
5017 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5018 c->ts.interface->ts = ts;
5019 c->ts.interface->attr.flavor = FL_PROCEDURE;
5020 c->ts.interface->attr.function = 1;
5021 c->attr.function = 1;
5022 c->attr.if_source = IFSRC_UNKNOWN;
5025 if (gfc_match (" =>") == MATCH_YES)
5027 m = match_pointer_init (&initializer, 1);
5030 gfc_free_expr (initializer);
5033 c->initializer = initializer;
5036 if (gfc_match_eos () == MATCH_YES)
5038 if (gfc_match_char (',') != MATCH_YES)
5043 gfc_error ("Syntax error in procedure pointer component at %C");
5048 /* Match a PROCEDURE declaration inside an interface (R1206). */
5051 match_procedure_in_interface (void)
5055 char name[GFC_MAX_SYMBOL_LEN + 1];
5057 if (current_interface.type == INTERFACE_NAMELESS
5058 || current_interface.type == INTERFACE_ABSTRACT)
5060 gfc_error ("PROCEDURE at %C must be in a generic interface");
5066 m = gfc_match_name (name);
5069 else if (m == MATCH_ERROR)
5071 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5074 if (gfc_add_interface (sym) == FAILURE)
5077 if (gfc_match_eos () == MATCH_YES)
5079 if (gfc_match_char (',') != MATCH_YES)
5086 gfc_error ("Syntax error in PROCEDURE statement at %C");
5091 /* General matcher for PROCEDURE declarations. */
5093 static match match_procedure_in_type (void);
5096 gfc_match_procedure (void)
5100 switch (gfc_current_state ())
5105 case COMP_SUBROUTINE:
5108 m = match_procedure_decl ();
5110 case COMP_INTERFACE:
5111 m = match_procedure_in_interface ();
5114 m = match_ppc_decl ();
5116 case COMP_DERIVED_CONTAINS:
5117 m = match_procedure_in_type ();
5126 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
5134 /* Warn if a matched procedure has the same name as an intrinsic; this is
5135 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5136 parser-state-stack to find out whether we're in a module. */
5139 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5143 in_module = (gfc_state_stack->previous
5144 && gfc_state_stack->previous->state == COMP_MODULE);
5146 gfc_warn_intrinsic_shadow (sym, in_module, func);
5150 /* Match a function declaration. */
5153 gfc_match_function_decl (void)
5155 char name[GFC_MAX_SYMBOL_LEN + 1];
5156 gfc_symbol *sym, *result;
5160 match found_match; /* Status returned by match func. */
5162 if (gfc_current_state () != COMP_NONE
5163 && gfc_current_state () != COMP_INTERFACE
5164 && gfc_current_state () != COMP_CONTAINS)
5167 gfc_clear_ts (¤t_ts);
5169 old_loc = gfc_current_locus;
5171 m = gfc_match_prefix (¤t_ts);
5174 gfc_current_locus = old_loc;
5178 if (gfc_match ("function% %n", name) != MATCH_YES)
5180 gfc_current_locus = old_loc;
5183 if (get_proc_name (name, &sym, false))
5186 if (add_hidden_procptr_result (sym) == SUCCESS)
5189 gfc_new_block = sym;
5191 m = gfc_match_formal_arglist (sym, 0, 0);
5194 gfc_error ("Expected formal argument list in function "
5195 "definition at %C");
5199 else if (m == MATCH_ERROR)
5204 /* According to the draft, the bind(c) and result clause can
5205 come in either order after the formal_arg_list (i.e., either
5206 can be first, both can exist together or by themselves or neither
5207 one). Therefore, the match_result can't match the end of the
5208 string, and check for the bind(c) or result clause in either order. */
5209 found_match = gfc_match_eos ();
5211 /* Make sure that it isn't already declared as BIND(C). If it is, it
5212 must have been marked BIND(C) with a BIND(C) attribute and that is
5213 not allowed for procedures. */
5214 if (sym->attr.is_bind_c == 1)
5216 sym->attr.is_bind_c = 0;
5217 if (sym->old_symbol != NULL)
5218 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5219 "variables or common blocks",
5220 &(sym->old_symbol->declared_at));
5222 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5223 "variables or common blocks", &gfc_current_locus);
5226 if (found_match != MATCH_YES)
5228 /* If we haven't found the end-of-statement, look for a suffix. */
5229 suffix_match = gfc_match_suffix (sym, &result);
5230 if (suffix_match == MATCH_YES)
5231 /* Need to get the eos now. */
5232 found_match = gfc_match_eos ();
5234 found_match = suffix_match;
5237 if(found_match != MATCH_YES)
5241 /* Make changes to the symbol. */
5244 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
5247 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
5248 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5251 /* Delay matching the function characteristics until after the
5252 specification block by signalling kind=-1. */
5253 sym->declared_at = old_loc;
5254 if (current_ts.type != BT_UNKNOWN)
5255 current_ts.kind = -1;
5257 current_ts.kind = 0;
5261 if (current_ts.type != BT_UNKNOWN
5262 && gfc_add_type (sym, ¤t_ts, &gfc_current_locus) == FAILURE)
5268 if (current_ts.type != BT_UNKNOWN
5269 && gfc_add_type (result, ¤t_ts, &gfc_current_locus)
5272 sym->result = result;
5275 /* Warn if this procedure has the same name as an intrinsic. */
5276 warn_intrinsic_shadow (sym, true);
5282 gfc_current_locus = old_loc;
5287 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5288 pass the name of the entry, rather than the gfc_current_block name, and
5289 to return false upon finding an existing global entry. */
5292 add_global_entry (const char *name, int sub)
5295 enum gfc_symbol_type type;
5297 s = gfc_get_gsymbol(name);
5298 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5301 || (s->type != GSYM_UNKNOWN
5302 && s->type != type))
5303 gfc_global_used(s, NULL);
5307 s->where = gfc_current_locus;
5309 s->ns = gfc_current_ns;
5316 /* Match an ENTRY statement. */
5319 gfc_match_entry (void)
5324 char name[GFC_MAX_SYMBOL_LEN + 1];
5325 gfc_compile_state state;
5329 bool module_procedure;
5333 m = gfc_match_name (name);
5337 if (gfc_notify_std (GFC_STD_F2008_OBS, "Fortran 2008 obsolescent feature: "
5338 "ENTRY statement at %C") == FAILURE)
5341 state = gfc_current_state ();
5342 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5347 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5350 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5352 case COMP_BLOCK_DATA:
5353 gfc_error ("ENTRY statement at %C cannot appear within "
5356 case COMP_INTERFACE:
5357 gfc_error ("ENTRY statement at %C cannot appear within "
5361 gfc_error ("ENTRY statement at %C cannot appear within "
5362 "a DERIVED TYPE block");
5365 gfc_error ("ENTRY statement at %C cannot appear within "
5366 "an IF-THEN block");
5369 case COMP_DO_CONCURRENT:
5370 gfc_error ("ENTRY statement at %C cannot appear within "
5374 gfc_error ("ENTRY statement at %C cannot appear within "
5378 gfc_error ("ENTRY statement at %C cannot appear within "
5382 gfc_error ("ENTRY statement at %C cannot appear within "
5386 gfc_error ("ENTRY statement at %C cannot appear within "
5387 "a contained subprogram");
5390 gfc_internal_error ("gfc_match_entry(): Bad state");
5395 module_procedure = gfc_current_ns->parent != NULL
5396 && gfc_current_ns->parent->proc_name
5397 && gfc_current_ns->parent->proc_name->attr.flavor
5400 if (gfc_current_ns->parent != NULL
5401 && gfc_current_ns->parent->proc_name
5402 && !module_procedure)
5404 gfc_error("ENTRY statement at %C cannot appear in a "
5405 "contained procedure");
5409 /* Module function entries need special care in get_proc_name
5410 because previous references within the function will have
5411 created symbols attached to the current namespace. */
5412 if (get_proc_name (name, &entry,
5413 gfc_current_ns->parent != NULL
5414 && module_procedure))
5417 proc = gfc_current_block ();
5419 /* Make sure that it isn't already declared as BIND(C). If it is, it
5420 must have been marked BIND(C) with a BIND(C) attribute and that is
5421 not allowed for procedures. */
5422 if (entry->attr.is_bind_c == 1)
5424 entry->attr.is_bind_c = 0;
5425 if (entry->old_symbol != NULL)
5426 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5427 "variables or common blocks",
5428 &(entry->old_symbol->declared_at));
5430 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5431 "variables or common blocks", &gfc_current_locus);
5434 /* Check what next non-whitespace character is so we can tell if there
5435 is the required parens if we have a BIND(C). */
5436 gfc_gobble_whitespace ();
5437 peek_char = gfc_peek_ascii_char ();
5439 if (state == COMP_SUBROUTINE)
5441 /* An entry in a subroutine. */
5442 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
5445 m = gfc_match_formal_arglist (entry, 0, 1);
5449 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5450 never be an internal procedure. */
5451 is_bind_c = gfc_match_bind_c (entry, true);
5452 if (is_bind_c == MATCH_ERROR)
5454 if (is_bind_c == MATCH_YES)
5456 if (peek_char != '(')
5458 gfc_error ("Missing required parentheses before BIND(C) at %C");
5461 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
5466 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5467 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
5472 /* An entry in a function.
5473 We need to take special care because writing
5478 ENTRY f() RESULT (r)
5480 ENTRY f RESULT (r). */
5481 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
5484 old_loc = gfc_current_locus;
5485 if (gfc_match_eos () == MATCH_YES)
5487 gfc_current_locus = old_loc;
5488 /* Match the empty argument list, and add the interface to
5490 m = gfc_match_formal_arglist (entry, 0, 1);
5493 m = gfc_match_formal_arglist (entry, 0, 0);
5500 if (gfc_match_eos () == MATCH_YES)
5502 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5503 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5506 entry->result = entry;
5510 m = gfc_match_suffix (entry, &result);
5512 gfc_syntax_error (ST_ENTRY);
5518 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
5519 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
5520 || gfc_add_function (&entry->attr, result->name, NULL)
5523 entry->result = result;
5527 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5528 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5530 entry->result = entry;
5535 if (gfc_match_eos () != MATCH_YES)
5537 gfc_syntax_error (ST_ENTRY);
5541 entry->attr.recursive = proc->attr.recursive;
5542 entry->attr.elemental = proc->attr.elemental;
5543 entry->attr.pure = proc->attr.pure;
5545 el = gfc_get_entry_list ();
5547 el->next = gfc_current_ns->entries;
5548 gfc_current_ns->entries = el;
5550 el->id = el->next->id + 1;
5554 new_st.op = EXEC_ENTRY;
5555 new_st.ext.entry = el;
5561 /* Match a subroutine statement, including optional prefixes. */
5564 gfc_match_subroutine (void)
5566 char name[GFC_MAX_SYMBOL_LEN + 1];
5571 bool allow_binding_name;
5573 if (gfc_current_state () != COMP_NONE
5574 && gfc_current_state () != COMP_INTERFACE
5575 && gfc_current_state () != COMP_CONTAINS)
5578 m = gfc_match_prefix (NULL);
5582 m = gfc_match ("subroutine% %n", name);
5586 if (get_proc_name (name, &sym, false))
5589 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5590 the symbol existed before. */
5591 sym->declared_at = gfc_current_locus;
5593 if (add_hidden_procptr_result (sym) == SUCCESS)
5596 gfc_new_block = sym;
5598 /* Check what next non-whitespace character is so we can tell if there
5599 is the required parens if we have a BIND(C). */
5600 gfc_gobble_whitespace ();
5601 peek_char = gfc_peek_ascii_char ();
5603 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5606 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5609 /* Make sure that it isn't already declared as BIND(C). If it is, it
5610 must have been marked BIND(C) with a BIND(C) attribute and that is
5611 not allowed for procedures. */
5612 if (sym->attr.is_bind_c == 1)
5614 sym->attr.is_bind_c = 0;
5615 if (sym->old_symbol != NULL)
5616 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5617 "variables or common blocks",
5618 &(sym->old_symbol->declared_at));
5620 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5621 "variables or common blocks", &gfc_current_locus);
5624 /* C binding names are not allowed for internal procedures. */
5625 if (gfc_current_state () == COMP_CONTAINS
5626 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5627 allow_binding_name = false;
5629 allow_binding_name = true;
5631 /* Here, we are just checking if it has the bind(c) attribute, and if
5632 so, then we need to make sure it's all correct. If it doesn't,
5633 we still need to continue matching the rest of the subroutine line. */
5634 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5635 if (is_bind_c == MATCH_ERROR)
5637 /* There was an attempt at the bind(c), but it was wrong. An
5638 error message should have been printed w/in the gfc_match_bind_c
5639 so here we'll just return the MATCH_ERROR. */
5643 if (is_bind_c == MATCH_YES)
5645 /* The following is allowed in the Fortran 2008 draft. */
5646 if (gfc_current_state () == COMP_CONTAINS
5647 && sym->ns->proc_name->attr.flavor != FL_MODULE
5648 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5649 "at %L may not be specified for an internal "
5650 "procedure", &gfc_current_locus)
5654 if (peek_char != '(')
5656 gfc_error ("Missing required parentheses before BIND(C) at %C");
5659 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5664 if (gfc_match_eos () != MATCH_YES)
5666 gfc_syntax_error (ST_SUBROUTINE);
5670 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5673 /* Warn if it has the same name as an intrinsic. */
5674 warn_intrinsic_shadow (sym, false);
5680 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5681 given, and set the binding label in either the given symbol (if not
5682 NULL), or in the current_ts. The symbol may be NULL because we may
5683 encounter the BIND(C) before the declaration itself. Return
5684 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5685 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5686 or MATCH_YES if the specifier was correct and the binding label and
5687 bind(c) fields were set correctly for the given symbol or the
5688 current_ts. If allow_binding_name is false, no binding name may be
5692 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5694 /* binding label, if exists */
5695 const char* binding_label = NULL;
5699 /* Initialize the flag that specifies whether we encountered a NAME=
5700 specifier or not. */
5701 has_name_equals = 0;
5703 /* This much we have to be able to match, in this order, if
5704 there is a bind(c) label. */
5705 if (gfc_match (" bind ( c ") != MATCH_YES)
5708 /* Now see if there is a binding label, or if we've reached the
5709 end of the bind(c) attribute without one. */
5710 if (gfc_match_char (',') == MATCH_YES)
5712 if (gfc_match (" name = ") != MATCH_YES)
5714 gfc_error ("Syntax error in NAME= specifier for binding label "
5716 /* should give an error message here */
5720 has_name_equals = 1;
5722 /* Get the opening quote. */
5723 double_quote = MATCH_YES;
5724 single_quote = MATCH_YES;
5725 double_quote = gfc_match_char ('"');
5726 if (double_quote != MATCH_YES)
5727 single_quote = gfc_match_char ('\'');
5728 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5730 gfc_error ("Syntax error in NAME= specifier for binding label "
5735 /* Grab the binding label, using functions that will not lower
5736 case the names automatically. */
5737 if (gfc_match_name_C (&binding_label) != MATCH_YES)
5740 /* Get the closing quotation. */
5741 if (double_quote == MATCH_YES)
5743 if (gfc_match_char ('"') != MATCH_YES)
5745 gfc_error ("Missing closing quote '\"' for binding label at %C");
5746 /* User started string with '"' so looked to match it. */
5752 if (gfc_match_char ('\'') != MATCH_YES)
5754 gfc_error ("Missing closing quote '\'' for binding label at %C");
5755 /* User started string with "'" char. */
5761 /* Get the required right paren. */
5762 if (gfc_match_char (')') != MATCH_YES)
5764 gfc_error ("Missing closing paren for binding label at %C");
5768 if (has_name_equals && !allow_binding_name)
5770 gfc_error ("No binding name is allowed in BIND(C) at %C");
5774 if (has_name_equals && sym != NULL && sym->attr.dummy)
5776 gfc_error ("For dummy procedure %s, no binding name is "
5777 "allowed in BIND(C) at %C", sym->name);
5782 /* Save the binding label to the symbol. If sym is null, we're
5783 probably matching the typespec attributes of a declaration and
5784 haven't gotten the name yet, and therefore, no symbol yet. */
5788 sym->binding_label = binding_label;
5790 curr_binding_label = binding_label;
5792 else if (allow_binding_name)
5794 /* No binding label, but if symbol isn't null, we
5795 can set the label for it here.
5796 If name="" or allow_binding_name is false, no C binding name is
5798 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5799 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
5802 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5803 && current_interface.type == INTERFACE_ABSTRACT)
5805 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5813 /* Return nonzero if we're currently compiling a contained procedure. */
5816 contained_procedure (void)
5818 gfc_state_data *s = gfc_state_stack;
5820 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5821 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5827 /* Set the kind of each enumerator. The kind is selected such that it is
5828 interoperable with the corresponding C enumeration type, making
5829 sure that -fshort-enums is honored. */
5834 enumerator_history *current_history = NULL;
5838 if (max_enum == NULL || enum_history == NULL)
5841 if (!flag_short_enums)
5847 kind = gfc_integer_kinds[i++].kind;
5849 while (kind < gfc_c_int_kind
5850 && gfc_check_integer_range (max_enum->initializer->value.integer,
5853 current_history = enum_history;
5854 while (current_history != NULL)
5856 current_history->sym->ts.kind = kind;
5857 current_history = current_history->next;
5862 /* Match any of the various end-block statements. Returns the type of
5863 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5864 and END BLOCK statements cannot be replaced by a single END statement. */
5867 gfc_match_end (gfc_statement *st)
5869 char name[GFC_MAX_SYMBOL_LEN + 1];
5870 gfc_compile_state state;
5872 const char *block_name;
5876 gfc_namespace *parent_ns, *ns, *prev_ns;
5877 gfc_namespace **nsp;
5879 old_loc = gfc_current_locus;
5880 if (gfc_match ("end") != MATCH_YES)
5883 state = gfc_current_state ();
5884 block_name = gfc_current_block () == NULL
5885 ? NULL : gfc_current_block ()->name;
5889 case COMP_ASSOCIATE:
5891 if (!strncmp (block_name, "block@", strlen("block@")))
5896 case COMP_DERIVED_CONTAINS:
5897 state = gfc_state_stack->previous->state;
5898 block_name = gfc_state_stack->previous->sym == NULL
5899 ? NULL : gfc_state_stack->previous->sym->name;
5910 *st = ST_END_PROGRAM;
5911 target = " program";
5915 case COMP_SUBROUTINE:
5916 *st = ST_END_SUBROUTINE;
5917 target = " subroutine";
5918 eos_ok = !contained_procedure ();
5922 *st = ST_END_FUNCTION;
5923 target = " function";
5924 eos_ok = !contained_procedure ();
5927 case COMP_BLOCK_DATA:
5928 *st = ST_END_BLOCK_DATA;
5929 target = " block data";
5934 *st = ST_END_MODULE;
5939 case COMP_INTERFACE:
5940 *st = ST_END_INTERFACE;
5941 target = " interface";
5946 case COMP_DERIVED_CONTAINS:
5952 case COMP_ASSOCIATE:
5953 *st = ST_END_ASSOCIATE;
5954 target = " associate";
5971 case COMP_DO_CONCURRENT:
5978 *st = ST_END_CRITICAL;
5979 target = " critical";
5984 case COMP_SELECT_TYPE:
5985 *st = ST_END_SELECT;
5991 *st = ST_END_FORALL;
6006 last_initializer = NULL;
6008 gfc_free_enum_history ();
6012 gfc_error ("Unexpected END statement at %C");
6016 if (gfc_match_eos () == MATCH_YES)
6018 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6020 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: END statement "
6021 "instead of %s statement at %L",
6022 gfc_ascii_statement (*st), &old_loc) == FAILURE)
6027 /* We would have required END [something]. */
6028 gfc_error ("%s statement expected at %L",
6029 gfc_ascii_statement (*st), &old_loc);
6036 /* Verify that we've got the sort of end-block that we're expecting. */
6037 if (gfc_match (target) != MATCH_YES)
6039 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
6043 /* If we're at the end, make sure a block name wasn't required. */
6044 if (gfc_match_eos () == MATCH_YES)
6047 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6048 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6049 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6055 gfc_error ("Expected block name of '%s' in %s statement at %C",
6056 block_name, gfc_ascii_statement (*st));
6061 /* END INTERFACE has a special handler for its several possible endings. */
6062 if (*st == ST_END_INTERFACE)
6063 return gfc_match_end_interface ();
6065 /* We haven't hit the end of statement, so what is left must be an
6067 m = gfc_match_space ();
6069 m = gfc_match_name (name);
6072 gfc_error ("Expected terminating name at %C");
6076 if (block_name == NULL)
6079 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6081 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
6082 gfc_ascii_statement (*st));
6085 /* Procedure pointer as function result. */
6086 else if (strcmp (block_name, "ppr@") == 0
6087 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6089 gfc_error ("Expected label '%s' for %s statement at %C",
6090 gfc_current_block ()->ns->proc_name->name,
6091 gfc_ascii_statement (*st));
6095 if (gfc_match_eos () == MATCH_YES)
6099 gfc_syntax_error (*st);
6102 gfc_current_locus = old_loc;
6104 /* If we are missing an END BLOCK, we created a half-ready namespace.
6105 Remove it from the parent namespace's sibling list. */
6107 if (state == COMP_BLOCK)
6109 parent_ns = gfc_current_ns->parent;
6111 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
6117 if (ns == gfc_current_ns)
6119 if (prev_ns == NULL)
6122 prev_ns->sibling = ns->sibling;
6128 gfc_free_namespace (gfc_current_ns);
6129 gfc_current_ns = parent_ns;
6137 /***************** Attribute declaration statements ****************/
6139 /* Set the attribute of a single variable. */
6144 char name[GFC_MAX_SYMBOL_LEN + 1];
6152 m = gfc_match_name (name);
6156 if (find_special (name, &sym, false))
6159 if (check_function_name (name) == FAILURE)
6165 var_locus = gfc_current_locus;
6167 /* Deal with possible array specification for certain attributes. */
6168 if (current_attr.dimension
6169 || current_attr.codimension
6170 || current_attr.allocatable
6171 || current_attr.pointer
6172 || current_attr.target)
6174 m = gfc_match_array_spec (&as, !current_attr.codimension,
6175 !current_attr.dimension
6176 && !current_attr.pointer
6177 && !current_attr.target);
6178 if (m == MATCH_ERROR)
6181 if (current_attr.dimension && m == MATCH_NO)
6183 gfc_error ("Missing array specification at %L in DIMENSION "
6184 "statement", &var_locus);
6189 if (current_attr.dimension && sym->value)
6191 gfc_error ("Dimensions specified for %s at %L after its "
6192 "initialisation", sym->name, &var_locus);
6197 if (current_attr.codimension && m == MATCH_NO)
6199 gfc_error ("Missing array specification at %L in CODIMENSION "
6200 "statement", &var_locus);
6205 if ((current_attr.allocatable || current_attr.pointer)
6206 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6208 gfc_error ("Array specification must be deferred at %L", &var_locus);
6214 /* Update symbol table. DIMENSION attribute is set in
6215 gfc_set_array_spec(). For CLASS variables, this must be applied
6216 to the first component, or '_data' field. */
6217 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6219 if (gfc_copy_attr (&CLASS_DATA (sym)->attr, ¤t_attr, &var_locus)
6228 if (current_attr.dimension == 0 && current_attr.codimension == 0
6229 && gfc_copy_attr (&sym->attr, ¤t_attr, &var_locus) == FAILURE)
6236 if (sym->ts.type == BT_CLASS
6237 && gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false) == FAILURE)
6243 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
6249 if (sym->attr.cray_pointee && sym->as != NULL)
6251 /* Fix the array spec. */
6252 m = gfc_mod_pointee_as (sym->as);
6253 if (m == MATCH_ERROR)
6257 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
6263 if ((current_attr.external || current_attr.intrinsic)
6264 && sym->attr.flavor != FL_PROCEDURE
6265 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6271 add_hidden_procptr_result (sym);
6276 gfc_free_array_spec (as);
6281 /* Generic attribute declaration subroutine. Used for attributes that
6282 just have a list of names. */
6289 /* Gobble the optional double colon, by simply ignoring the result
6299 if (gfc_match_eos () == MATCH_YES)
6305 if (gfc_match_char (',') != MATCH_YES)
6307 gfc_error ("Unexpected character in variable list at %C");
6317 /* This routine matches Cray Pointer declarations of the form:
6318 pointer ( <pointer>, <pointee> )
6320 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6321 The pointer, if already declared, should be an integer. Otherwise, we
6322 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6323 be either a scalar, or an array declaration. No space is allocated for
6324 the pointee. For the statement
6325 pointer (ipt, ar(10))
6326 any subsequent uses of ar will be translated (in C-notation) as
6327 ar(i) => ((<type> *) ipt)(i)
6328 After gimplification, pointee variable will disappear in the code. */
6331 cray_pointer_decl (void)
6334 gfc_array_spec *as = NULL;
6335 gfc_symbol *cptr; /* Pointer symbol. */
6336 gfc_symbol *cpte; /* Pointee symbol. */
6342 if (gfc_match_char ('(') != MATCH_YES)
6344 gfc_error ("Expected '(' at %C");
6348 /* Match pointer. */
6349 var_locus = gfc_current_locus;
6350 gfc_clear_attr (¤t_attr);
6351 gfc_add_cray_pointer (¤t_attr, &var_locus);
6352 current_ts.type = BT_INTEGER;
6353 current_ts.kind = gfc_index_integer_kind;
6355 m = gfc_match_symbol (&cptr, 0);
6358 gfc_error ("Expected variable name at %C");
6362 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
6365 gfc_set_sym_referenced (cptr);
6367 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6369 cptr->ts.type = BT_INTEGER;
6370 cptr->ts.kind = gfc_index_integer_kind;
6372 else if (cptr->ts.type != BT_INTEGER)
6374 gfc_error ("Cray pointer at %C must be an integer");
6377 else if (cptr->ts.kind < gfc_index_integer_kind)
6378 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6379 " memory addresses require %d bytes",
6380 cptr->ts.kind, gfc_index_integer_kind);
6382 if (gfc_match_char (',') != MATCH_YES)
6384 gfc_error ("Expected \",\" at %C");
6388 /* Match Pointee. */
6389 var_locus = gfc_current_locus;
6390 gfc_clear_attr (¤t_attr);
6391 gfc_add_cray_pointee (¤t_attr, &var_locus);
6392 current_ts.type = BT_UNKNOWN;
6393 current_ts.kind = 0;
6395 m = gfc_match_symbol (&cpte, 0);
6398 gfc_error ("Expected variable name at %C");
6402 /* Check for an optional array spec. */
6403 m = gfc_match_array_spec (&as, true, false);
6404 if (m == MATCH_ERROR)
6406 gfc_free_array_spec (as);
6409 else if (m == MATCH_NO)
6411 gfc_free_array_spec (as);
6415 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
6418 gfc_set_sym_referenced (cpte);
6420 if (cpte->as == NULL)
6422 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
6423 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6425 else if (as != NULL)
6427 gfc_error ("Duplicate array spec for Cray pointee at %C");
6428 gfc_free_array_spec (as);
6434 if (cpte->as != NULL)
6436 /* Fix array spec. */
6437 m = gfc_mod_pointee_as (cpte->as);
6438 if (m == MATCH_ERROR)
6442 /* Point the Pointee at the Pointer. */
6443 cpte->cp_pointer = cptr;
6445 if (gfc_match_char (')') != MATCH_YES)
6447 gfc_error ("Expected \")\" at %C");
6450 m = gfc_match_char (',');
6452 done = true; /* Stop searching for more declarations. */
6456 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6457 || gfc_match_eos () != MATCH_YES)
6459 gfc_error ("Expected \",\" or end of statement at %C");
6467 gfc_match_external (void)
6470 gfc_clear_attr (¤t_attr);
6471 current_attr.external = 1;
6473 return attr_decl ();
6478 gfc_match_intent (void)
6482 /* This is not allowed within a BLOCK construct! */
6483 if (gfc_current_state () == COMP_BLOCK)
6485 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6489 intent = match_intent_spec ();
6490 if (intent == INTENT_UNKNOWN)
6493 gfc_clear_attr (¤t_attr);
6494 current_attr.intent = intent;
6496 return attr_decl ();
6501 gfc_match_intrinsic (void)
6504 gfc_clear_attr (¤t_attr);
6505 current_attr.intrinsic = 1;
6507 return attr_decl ();
6512 gfc_match_optional (void)
6514 /* This is not allowed within a BLOCK construct! */
6515 if (gfc_current_state () == COMP_BLOCK)
6517 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6521 gfc_clear_attr (¤t_attr);
6522 current_attr.optional = 1;
6524 return attr_decl ();
6529 gfc_match_pointer (void)
6531 gfc_gobble_whitespace ();
6532 if (gfc_peek_ascii_char () == '(')
6534 if (!gfc_option.flag_cray_pointer)
6536 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6540 return cray_pointer_decl ();
6544 gfc_clear_attr (¤t_attr);
6545 current_attr.pointer = 1;
6547 return attr_decl ();
6553 gfc_match_allocatable (void)
6555 gfc_clear_attr (¤t_attr);
6556 current_attr.allocatable = 1;
6558 return attr_decl ();
6563 gfc_match_codimension (void)
6565 gfc_clear_attr (¤t_attr);
6566 current_attr.codimension = 1;
6568 return attr_decl ();
6573 gfc_match_contiguous (void)
6575 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTIGUOUS statement at %C")
6579 gfc_clear_attr (¤t_attr);
6580 current_attr.contiguous = 1;
6582 return attr_decl ();
6587 gfc_match_dimension (void)
6589 gfc_clear_attr (¤t_attr);
6590 current_attr.dimension = 1;
6592 return attr_decl ();
6597 gfc_match_target (void)
6599 gfc_clear_attr (¤t_attr);
6600 current_attr.target = 1;
6602 return attr_decl ();
6606 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6610 access_attr_decl (gfc_statement st)
6612 char name[GFC_MAX_SYMBOL_LEN + 1];
6613 interface_type type;
6615 gfc_symbol *sym, *dt_sym;
6616 gfc_intrinsic_op op;
6619 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6624 m = gfc_match_generic_spec (&type, name, &op);
6627 if (m == MATCH_ERROR)
6632 case INTERFACE_NAMELESS:
6633 case INTERFACE_ABSTRACT:
6636 case INTERFACE_GENERIC:
6637 if (gfc_get_symbol (name, NULL, &sym))
6640 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
6641 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6642 sym->name, NULL) == FAILURE)
6645 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
6646 && gfc_add_access (&dt_sym->attr,
6647 (st == ST_PUBLIC) ? ACCESS_PUBLIC
6649 sym->name, NULL) == FAILURE)
6654 case INTERFACE_INTRINSIC_OP:
6655 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6657 gfc_intrinsic_op other_op;
6659 gfc_current_ns->operator_access[op] =
6660 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6662 /* Handle the case if there is another op with the same
6663 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6664 other_op = gfc_equivalent_op (op);
6666 if (other_op != INTRINSIC_NONE)
6667 gfc_current_ns->operator_access[other_op] =
6668 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6673 gfc_error ("Access specification of the %s operator at %C has "
6674 "already been specified", gfc_op2string (op));
6680 case INTERFACE_USER_OP:
6681 uop = gfc_get_uop (name);
6683 if (uop->access == ACCESS_UNKNOWN)
6685 uop->access = (st == ST_PUBLIC)
6686 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6690 gfc_error ("Access specification of the .%s. operator at %C "
6691 "has already been specified", sym->name);
6698 if (gfc_match_char (',') == MATCH_NO)
6702 if (gfc_match_eos () != MATCH_YES)
6707 gfc_syntax_error (st);
6715 gfc_match_protected (void)
6720 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6722 gfc_error ("PROTECTED at %C only allowed in specification "
6723 "part of a module");
6728 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
6732 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6737 if (gfc_match_eos () == MATCH_YES)
6742 m = gfc_match_symbol (&sym, 0);
6746 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6759 if (gfc_match_eos () == MATCH_YES)
6761 if (gfc_match_char (',') != MATCH_YES)
6768 gfc_error ("Syntax error in PROTECTED statement at %C");
6773 /* The PRIVATE statement is a bit weird in that it can be an attribute
6774 declaration, but also works as a standalone statement inside of a
6775 type declaration or a module. */
6778 gfc_match_private (gfc_statement *st)
6781 if (gfc_match ("private") != MATCH_YES)
6784 if (gfc_current_state () != COMP_MODULE
6785 && !(gfc_current_state () == COMP_DERIVED
6786 && gfc_state_stack->previous
6787 && gfc_state_stack->previous->state == COMP_MODULE)
6788 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6789 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6790 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6792 gfc_error ("PRIVATE statement at %C is only allowed in the "
6793 "specification part of a module");
6797 if (gfc_current_state () == COMP_DERIVED)
6799 if (gfc_match_eos () == MATCH_YES)
6805 gfc_syntax_error (ST_PRIVATE);
6809 if (gfc_match_eos () == MATCH_YES)
6816 return access_attr_decl (ST_PRIVATE);
6821 gfc_match_public (gfc_statement *st)
6824 if (gfc_match ("public") != MATCH_YES)
6827 if (gfc_current_state () != COMP_MODULE)
6829 gfc_error ("PUBLIC statement at %C is only allowed in the "
6830 "specification part of a module");
6834 if (gfc_match_eos () == MATCH_YES)
6841 return access_attr_decl (ST_PUBLIC);
6845 /* Workhorse for gfc_match_parameter. */
6855 m = gfc_match_symbol (&sym, 0);
6857 gfc_error ("Expected variable name at %C in PARAMETER statement");
6862 if (gfc_match_char ('=') == MATCH_NO)
6864 gfc_error ("Expected = sign in PARAMETER statement at %C");
6868 m = gfc_match_init_expr (&init);
6870 gfc_error ("Expected expression at %C in PARAMETER statement");
6874 if (sym->ts.type == BT_UNKNOWN
6875 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6881 if (gfc_check_assign_symbol (sym, init) == FAILURE
6882 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6890 gfc_error ("Initializing already initialized variable at %C");
6895 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
6896 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6899 gfc_free_expr (init);
6904 /* Match a parameter statement, with the weird syntax that these have. */
6907 gfc_match_parameter (void)
6911 if (gfc_match_char ('(') == MATCH_NO)
6920 if (gfc_match (" )%t") == MATCH_YES)
6923 if (gfc_match_char (',') != MATCH_YES)
6925 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6935 /* Save statements have a special syntax. */
6938 gfc_match_save (void)
6940 char n[GFC_MAX_SYMBOL_LEN+1];
6945 if (gfc_match_eos () == MATCH_YES)
6947 if (gfc_current_ns->seen_save)
6949 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6950 "follows previous SAVE statement")
6955 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6959 if (gfc_current_ns->save_all)
6961 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6962 "blanket SAVE statement")
6971 m = gfc_match_symbol (&sym, 0);
6975 if (gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
6976 &gfc_current_locus) == FAILURE)
6987 m = gfc_match (" / %n /", &n);
6988 if (m == MATCH_ERROR)
6993 c = gfc_get_common (n, 0);
6996 gfc_current_ns->seen_save = 1;
6999 if (gfc_match_eos () == MATCH_YES)
7001 if (gfc_match_char (',') != MATCH_YES)
7008 gfc_error ("Syntax error in SAVE statement at %C");
7014 gfc_match_value (void)
7019 /* This is not allowed within a BLOCK construct! */
7020 if (gfc_current_state () == COMP_BLOCK)
7022 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7026 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
7030 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7035 if (gfc_match_eos () == MATCH_YES)
7040 m = gfc_match_symbol (&sym, 0);
7044 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
7057 if (gfc_match_eos () == MATCH_YES)
7059 if (gfc_match_char (',') != MATCH_YES)
7066 gfc_error ("Syntax error in VALUE statement at %C");
7072 gfc_match_volatile (void)
7077 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
7081 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7086 if (gfc_match_eos () == MATCH_YES)
7091 /* VOLATILE is special because it can be added to host-associated
7092 symbols locally. Except for coarrays. */
7093 m = gfc_match_symbol (&sym, 1);
7097 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7098 for variable in a BLOCK which is defined outside of the BLOCK. */
7099 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7101 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
7102 "%C, which is use-/host-associated", sym->name);
7105 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
7118 if (gfc_match_eos () == MATCH_YES)
7120 if (gfc_match_char (',') != MATCH_YES)
7127 gfc_error ("Syntax error in VOLATILE statement at %C");
7133 gfc_match_asynchronous (void)
7138 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS statement at %C")
7142 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7147 if (gfc_match_eos () == MATCH_YES)
7152 /* ASYNCHRONOUS is special because it can be added to host-associated
7154 m = gfc_match_symbol (&sym, 1);
7158 if (gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus)
7171 if (gfc_match_eos () == MATCH_YES)
7173 if (gfc_match_char (',') != MATCH_YES)
7180 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7185 /* Match a module procedure statement. Note that we have to modify
7186 symbols in the parent's namespace because the current one was there
7187 to receive symbols that are in an interface's formal argument list. */
7190 gfc_match_modproc (void)
7192 char name[GFC_MAX_SYMBOL_LEN + 1];
7196 gfc_namespace *module_ns;
7197 gfc_interface *old_interface_head, *interface;
7199 if (gfc_state_stack->state != COMP_INTERFACE
7200 || gfc_state_stack->previous == NULL
7201 || current_interface.type == INTERFACE_NAMELESS
7202 || current_interface.type == INTERFACE_ABSTRACT)
7204 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7209 module_ns = gfc_current_ns->parent;
7210 for (; module_ns; module_ns = module_ns->parent)
7211 if (module_ns->proc_name->attr.flavor == FL_MODULE
7212 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7213 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7214 && !module_ns->proc_name->attr.contained))
7217 if (module_ns == NULL)
7220 /* Store the current state of the interface. We will need it if we
7221 end up with a syntax error and need to recover. */
7222 old_interface_head = gfc_current_interface_head ();
7224 /* Check if the F2008 optional double colon appears. */
7225 gfc_gobble_whitespace ();
7226 old_locus = gfc_current_locus;
7227 if (gfc_match ("::") == MATCH_YES)
7229 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: double colon in "
7230 "MODULE PROCEDURE statement at %L", &old_locus)
7235 gfc_current_locus = old_locus;
7240 old_locus = gfc_current_locus;
7242 m = gfc_match_name (name);
7248 /* Check for syntax error before starting to add symbols to the
7249 current namespace. */
7250 if (gfc_match_eos () == MATCH_YES)
7253 if (!last && gfc_match_char (',') != MATCH_YES)
7256 /* Now we're sure the syntax is valid, we process this item
7258 if (gfc_get_symbol (name, module_ns, &sym))
7261 if (sym->attr.intrinsic)
7263 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7264 "PROCEDURE", &old_locus);
7268 if (sym->attr.proc != PROC_MODULE
7269 && gfc_add_procedure (&sym->attr, PROC_MODULE,
7270 sym->name, NULL) == FAILURE)
7273 if (gfc_add_interface (sym) == FAILURE)
7276 sym->attr.mod_proc = 1;
7277 sym->declared_at = old_locus;
7286 /* Restore the previous state of the interface. */
7287 interface = gfc_current_interface_head ();
7288 gfc_set_current_interface_head (old_interface_head);
7290 /* Free the new interfaces. */
7291 while (interface != old_interface_head)
7293 gfc_interface *i = interface->next;
7298 /* And issue a syntax error. */
7299 gfc_syntax_error (ST_MODULE_PROC);
7304 /* Check a derived type that is being extended. */
7307 check_extended_derived_type (char *name)
7309 gfc_symbol *extended;
7311 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7313 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7317 extended = gfc_find_dt_in_generic (extended);
7322 gfc_error ("Symbol '%s' at %C has not been previously defined", name);
7326 if (extended->attr.flavor != FL_DERIVED)
7328 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7329 "derived type", name);
7333 if (extended->attr.is_bind_c)
7335 gfc_error ("'%s' cannot be extended at %C because it "
7336 "is BIND(C)", extended->name);
7340 if (extended->attr.sequence)
7342 gfc_error ("'%s' cannot be extended at %C because it "
7343 "is a SEQUENCE type", extended->name);
7351 /* Match the optional attribute specifiers for a type declaration.
7352 Return MATCH_ERROR if an error is encountered in one of the handled
7353 attributes (public, private, bind(c)), MATCH_NO if what's found is
7354 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7355 checking on attribute conflicts needs to be done. */
7358 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7360 /* See if the derived type is marked as private. */
7361 if (gfc_match (" , private") == MATCH_YES)
7363 if (gfc_current_state () != COMP_MODULE)
7365 gfc_error ("Derived type at %C can only be PRIVATE in the "
7366 "specification part of a module");
7370 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
7373 else if (gfc_match (" , public") == MATCH_YES)
7375 if (gfc_current_state () != COMP_MODULE)
7377 gfc_error ("Derived type at %C can only be PUBLIC in the "
7378 "specification part of a module");
7382 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
7385 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7387 /* If the type is defined to be bind(c) it then needs to make
7388 sure that all fields are interoperable. This will
7389 need to be a semantic check on the finished derived type.
7390 See 15.2.3 (lines 9-12) of F2003 draft. */
7391 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
7394 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7396 else if (gfc_match (" , abstract") == MATCH_YES)
7398 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
7402 if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
7405 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
7407 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
7413 /* If we get here, something matched. */
7418 /* Match the beginning of a derived type declaration. If a type name
7419 was the result of a function, then it is possible to have a symbol
7420 already to be known as a derived type yet have no components. */
7423 gfc_match_derived_decl (void)
7425 char name[GFC_MAX_SYMBOL_LEN + 1];
7426 char parent[GFC_MAX_SYMBOL_LEN + 1];
7427 symbol_attribute attr;
7428 gfc_symbol *sym, *gensym;
7429 gfc_symbol *extended;
7431 match is_type_attr_spec = MATCH_NO;
7432 bool seen_attr = false;
7433 gfc_interface *intr = NULL, *head;
7435 if (gfc_current_state () == COMP_DERIVED)
7440 gfc_clear_attr (&attr);
7445 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7446 if (is_type_attr_spec == MATCH_ERROR)
7448 if (is_type_attr_spec == MATCH_YES)
7450 } while (is_type_attr_spec == MATCH_YES);
7452 /* Deal with derived type extensions. The extension attribute has
7453 been added to 'attr' but now the parent type must be found and
7456 extended = check_extended_derived_type (parent);
7458 if (parent[0] && !extended)
7461 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7463 gfc_error ("Expected :: in TYPE definition at %C");
7467 m = gfc_match (" %n%t", name);
7471 /* Make sure the name is not the name of an intrinsic type. */
7472 if (gfc_is_intrinsic_typename (name))
7474 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7479 if (gfc_get_symbol (name, NULL, &gensym))
7482 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
7484 gfc_error ("Derived type name '%s' at %C already has a basic type "
7485 "of %s", gensym->name, gfc_typename (&gensym->ts));
7489 if (!gensym->attr.generic
7490 && gfc_add_generic (&gensym->attr, gensym->name, NULL) == FAILURE)
7493 if (!gensym->attr.function
7494 && gfc_add_function (&gensym->attr, gensym->name, NULL) == FAILURE)
7497 sym = gfc_find_dt_in_generic (gensym);
7499 if (sym && (sym->components != NULL || sym->attr.zero_comp))
7501 gfc_error ("Derived type definition of '%s' at %C has already been "
7502 "defined", sym->name);
7508 /* Use upper case to save the actual derived-type symbol. */
7509 gfc_get_symbol (gfc_get_string ("%c%s",
7510 (char) TOUPPER ((unsigned char) gensym->name[0]),
7511 &gensym->name[1]), NULL, &sym);
7512 sym->name = gfc_get_string (gensym->name);
7513 head = gensym->generic;
7514 intr = gfc_get_interface ();
7516 intr->where = gfc_current_locus;
7517 intr->sym->declared_at = gfc_current_locus;
7519 gensym->generic = intr;
7520 gensym->attr.if_source = IFSRC_DECL;
7523 /* The symbol may already have the derived attribute without the
7524 components. The ways this can happen is via a function
7525 definition, an INTRINSIC statement or a subtype in another
7526 derived type that is a pointer. The first part of the AND clause
7527 is true if the symbol is not the return value of a function. */
7528 if (sym->attr.flavor != FL_DERIVED
7529 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
7532 if (attr.access != ACCESS_UNKNOWN
7533 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
7535 else if (sym->attr.access == ACCESS_UNKNOWN
7536 && gensym->attr.access != ACCESS_UNKNOWN
7537 && gfc_add_access (&sym->attr, gensym->attr.access, sym->name, NULL)
7541 if (sym->attr.access != ACCESS_UNKNOWN
7542 && gensym->attr.access == ACCESS_UNKNOWN)
7543 gensym->attr.access = sym->attr.access;
7545 /* See if the derived type was labeled as bind(c). */
7546 if (attr.is_bind_c != 0)
7547 sym->attr.is_bind_c = attr.is_bind_c;
7549 /* Construct the f2k_derived namespace if it is not yet there. */
7550 if (!sym->f2k_derived)
7551 sym->f2k_derived = gfc_get_namespace (NULL, 0);
7553 if (extended && !sym->components)
7558 /* Add the extended derived type as the first component. */
7559 gfc_add_component (sym, parent, &p);
7561 gfc_set_sym_referenced (extended);
7563 p->ts.type = BT_DERIVED;
7564 p->ts.u.derived = extended;
7565 p->initializer = gfc_default_initializer (&p->ts);
7567 /* Set extension level. */
7568 if (extended->attr.extension == 255)
7570 /* Since the extension field is 8 bit wide, we can only have
7571 up to 255 extension levels. */
7572 gfc_error ("Maximum extension level reached with type '%s' at %L",
7573 extended->name, &extended->declared_at);
7576 sym->attr.extension = extended->attr.extension + 1;
7578 /* Provide the links between the extended type and its extension. */
7579 if (!extended->f2k_derived)
7580 extended->f2k_derived = gfc_get_namespace (NULL, 0);
7581 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7585 if (!sym->hash_value)
7586 /* Set the hash for the compound name for this type. */
7587 sym->hash_value = gfc_hash_value (sym);
7589 /* Take over the ABSTRACT attribute. */
7590 sym->attr.abstract = attr.abstract;
7592 gfc_new_block = sym;
7598 /* Cray Pointees can be declared as:
7599 pointer (ipt, a (n,m,...,*)) */
7602 gfc_mod_pointee_as (gfc_array_spec *as)
7604 as->cray_pointee = true; /* This will be useful to know later. */
7605 if (as->type == AS_ASSUMED_SIZE)
7606 as->cp_was_assumed = true;
7607 else if (as->type == AS_ASSUMED_SHAPE)
7609 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7616 /* Match the enum definition statement, here we are trying to match
7617 the first line of enum definition statement.
7618 Returns MATCH_YES if match is found. */
7621 gfc_match_enum (void)
7625 m = gfc_match_eos ();
7629 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
7637 /* Returns an initializer whose value is one higher than the value of the
7638 LAST_INITIALIZER argument. If the argument is NULL, the
7639 initializers value will be set to zero. The initializer's kind
7640 will be set to gfc_c_int_kind.
7642 If -fshort-enums is given, the appropriate kind will be selected
7643 later after all enumerators have been parsed. A warning is issued
7644 here if an initializer exceeds gfc_c_int_kind. */
7647 enum_initializer (gfc_expr *last_initializer, locus where)
7650 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7652 mpz_init (result->value.integer);
7654 if (last_initializer != NULL)
7656 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7657 result->where = last_initializer->where;
7659 if (gfc_check_integer_range (result->value.integer,
7660 gfc_c_int_kind) != ARITH_OK)
7662 gfc_error ("Enumerator exceeds the C integer type at %C");
7668 /* Control comes here, if it's the very first enumerator and no
7669 initializer has been given. It will be initialized to zero. */
7670 mpz_set_si (result->value.integer, 0);
7677 /* Match a variable name with an optional initializer. When this
7678 subroutine is called, a variable is expected to be parsed next.
7679 Depending on what is happening at the moment, updates either the
7680 symbol table or the current interface. */
7683 enumerator_decl (void)
7685 char name[GFC_MAX_SYMBOL_LEN + 1];
7686 gfc_expr *initializer;
7687 gfc_array_spec *as = NULL;
7695 old_locus = gfc_current_locus;
7697 /* When we get here, we've just matched a list of attributes and
7698 maybe a type and a double colon. The next thing we expect to see
7699 is the name of the symbol. */
7700 m = gfc_match_name (name);
7704 var_locus = gfc_current_locus;
7706 /* OK, we've successfully matched the declaration. Now put the
7707 symbol in the current namespace. If we fail to create the symbol,
7709 if (build_sym (name, NULL, false, &as, &var_locus) == FAILURE)
7715 /* The double colon must be present in order to have initializers.
7716 Otherwise the statement is ambiguous with an assignment statement. */
7719 if (gfc_match_char ('=') == MATCH_YES)
7721 m = gfc_match_init_expr (&initializer);
7724 gfc_error ("Expected an initialization expression at %C");
7733 /* If we do not have an initializer, the initialization value of the
7734 previous enumerator (stored in last_initializer) is incremented
7735 by 1 and is used to initialize the current enumerator. */
7736 if (initializer == NULL)
7737 initializer = enum_initializer (last_initializer, old_locus);
7739 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7741 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7747 /* Store this current initializer, for the next enumerator variable
7748 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7749 use last_initializer below. */
7750 last_initializer = initializer;
7751 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7753 /* Maintain enumerator history. */
7754 gfc_find_symbol (name, NULL, 0, &sym);
7755 create_enum_history (sym, last_initializer);
7757 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
7760 /* Free stuff up and return. */
7761 gfc_free_expr (initializer);
7767 /* Match the enumerator definition statement. */
7770 gfc_match_enumerator_def (void)
7775 gfc_clear_ts (¤t_ts);
7777 m = gfc_match (" enumerator");
7781 m = gfc_match (" :: ");
7782 if (m == MATCH_ERROR)
7785 colon_seen = (m == MATCH_YES);
7787 if (gfc_current_state () != COMP_ENUM)
7789 gfc_error ("ENUM definition statement expected before %C");
7790 gfc_free_enum_history ();
7794 (¤t_ts)->type = BT_INTEGER;
7795 (¤t_ts)->kind = gfc_c_int_kind;
7797 gfc_clear_attr (¤t_attr);
7798 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
7807 m = enumerator_decl ();
7808 if (m == MATCH_ERROR)
7810 gfc_free_enum_history ();
7816 if (gfc_match_eos () == MATCH_YES)
7818 if (gfc_match_char (',') != MATCH_YES)
7822 if (gfc_current_state () == COMP_ENUM)
7824 gfc_free_enum_history ();
7825 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7830 gfc_free_array_spec (current_as);
7837 /* Match binding attributes. */
7840 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7842 bool found_passing = false;
7843 bool seen_ptr = false;
7844 match m = MATCH_YES;
7846 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7847 this case the defaults are in there. */
7848 ba->access = ACCESS_UNKNOWN;
7849 ba->pass_arg = NULL;
7850 ba->pass_arg_num = 0;
7852 ba->non_overridable = 0;
7856 /* If we find a comma, we believe there are binding attributes. */
7857 m = gfc_match_char (',');
7863 /* Access specifier. */
7865 m = gfc_match (" public");
7866 if (m == MATCH_ERROR)
7870 if (ba->access != ACCESS_UNKNOWN)
7872 gfc_error ("Duplicate access-specifier at %C");
7876 ba->access = ACCESS_PUBLIC;
7880 m = gfc_match (" private");
7881 if (m == MATCH_ERROR)
7885 if (ba->access != ACCESS_UNKNOWN)
7887 gfc_error ("Duplicate access-specifier at %C");
7891 ba->access = ACCESS_PRIVATE;
7895 /* If inside GENERIC, the following is not allowed. */
7900 m = gfc_match (" nopass");
7901 if (m == MATCH_ERROR)
7907 gfc_error ("Binding attributes already specify passing,"
7908 " illegal NOPASS at %C");
7912 found_passing = true;
7917 /* PASS possibly including argument. */
7918 m = gfc_match (" pass");
7919 if (m == MATCH_ERROR)
7923 char arg[GFC_MAX_SYMBOL_LEN + 1];
7927 gfc_error ("Binding attributes already specify passing,"
7928 " illegal PASS at %C");
7932 m = gfc_match (" ( %n )", arg);
7933 if (m == MATCH_ERROR)
7936 ba->pass_arg = gfc_get_string (arg);
7937 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7939 found_passing = true;
7947 m = gfc_match (" pointer");
7948 if (m == MATCH_ERROR)
7954 gfc_error ("Duplicate POINTER attribute at %C");
7964 /* NON_OVERRIDABLE flag. */
7965 m = gfc_match (" non_overridable");
7966 if (m == MATCH_ERROR)
7970 if (ba->non_overridable)
7972 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7976 ba->non_overridable = 1;
7980 /* DEFERRED flag. */
7981 m = gfc_match (" deferred");
7982 if (m == MATCH_ERROR)
7988 gfc_error ("Duplicate DEFERRED at %C");
7999 /* Nothing matching found. */
8001 gfc_error ("Expected access-specifier at %C");
8003 gfc_error ("Expected binding attribute at %C");
8006 while (gfc_match_char (',') == MATCH_YES);
8008 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8009 if (ba->non_overridable && ba->deferred)
8011 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8018 if (ba->access == ACCESS_UNKNOWN)
8019 ba->access = gfc_typebound_default_access;
8021 if (ppc && !seen_ptr)
8023 gfc_error ("POINTER attribute is required for procedure pointer component"
8035 /* Match a PROCEDURE specific binding inside a derived type. */
8038 match_procedure_in_type (void)
8040 char name[GFC_MAX_SYMBOL_LEN + 1];
8041 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
8042 char* target = NULL, *ifc = NULL;
8043 gfc_typebound_proc tb;
8052 /* Check current state. */
8053 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8054 block = gfc_state_stack->previous->sym;
8057 /* Try to match PROCEDURE(interface). */
8058 if (gfc_match (" (") == MATCH_YES)
8060 m = gfc_match_name (target_buf);
8061 if (m == MATCH_ERROR)
8065 gfc_error ("Interface-name expected after '(' at %C");
8069 if (gfc_match (" )") != MATCH_YES)
8071 gfc_error ("')' expected at %C");
8078 /* Construct the data structure. */
8079 memset (&tb, 0, sizeof (tb));
8080 tb.where = gfc_current_locus;
8082 /* Match binding attributes. */
8083 m = match_binding_attributes (&tb, false, false);
8084 if (m == MATCH_ERROR)
8086 seen_attrs = (m == MATCH_YES);
8088 /* Check that attribute DEFERRED is given if an interface is specified. */
8089 if (tb.deferred && !ifc)
8091 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8094 if (ifc && !tb.deferred)
8096 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8100 /* Match the colons. */
8101 m = gfc_match (" ::");
8102 if (m == MATCH_ERROR)
8104 seen_colons = (m == MATCH_YES);
8105 if (seen_attrs && !seen_colons)
8107 gfc_error ("Expected '::' after binding-attributes at %C");
8111 /* Match the binding names. */
8114 m = gfc_match_name (name);
8115 if (m == MATCH_ERROR)
8119 gfc_error ("Expected binding name at %C");
8123 if (num>1 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: PROCEDURE list"
8124 " at %C") == FAILURE)
8127 /* Try to match the '=> target', if it's there. */
8129 m = gfc_match (" =>");
8130 if (m == MATCH_ERROR)
8136 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
8142 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
8147 m = gfc_match_name (target_buf);
8148 if (m == MATCH_ERROR)
8152 gfc_error ("Expected binding target after '=>' at %C");
8155 target = target_buf;
8158 /* If no target was found, it has the same name as the binding. */
8162 /* Get the namespace to insert the symbols into. */
8163 ns = block->f2k_derived;
8166 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8167 if (tb.deferred && !block->attr.abstract)
8169 gfc_error ("Type '%s' containing DEFERRED binding at %C "
8170 "is not ABSTRACT", block->name);
8174 /* See if we already have a binding with this name in the symtree which
8175 would be an error. If a GENERIC already targetted this binding, it may
8176 be already there but then typebound is still NULL. */
8177 stree = gfc_find_symtree (ns->tb_sym_root, name);
8178 if (stree && stree->n.tb)
8180 gfc_error ("There is already a procedure with binding name '%s' for "
8181 "the derived type '%s' at %C", name, block->name);
8185 /* Insert it and set attributes. */
8189 stree = gfc_new_symtree (&ns->tb_sym_root, name);
8192 stree->n.tb = gfc_get_typebound_proc (&tb);
8194 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8197 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
8199 if (gfc_match_eos () == MATCH_YES)
8201 if (gfc_match_char (',') != MATCH_YES)
8206 gfc_error ("Syntax error in PROCEDURE statement at %C");
8211 /* Match a GENERIC procedure binding inside a derived type. */
8214 gfc_match_generic (void)
8216 char name[GFC_MAX_SYMBOL_LEN + 1];
8217 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
8219 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8220 gfc_typebound_proc* tb;
8222 interface_type op_type;
8223 gfc_intrinsic_op op;
8226 /* Check current state. */
8227 if (gfc_current_state () == COMP_DERIVED)
8229 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8232 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8234 block = gfc_state_stack->previous->sym;
8235 ns = block->f2k_derived;
8236 gcc_assert (block && ns);
8238 memset (&tbattr, 0, sizeof (tbattr));
8239 tbattr.where = gfc_current_locus;
8241 /* See if we get an access-specifier. */
8242 m = match_binding_attributes (&tbattr, true, false);
8243 if (m == MATCH_ERROR)
8246 /* Now the colons, those are required. */
8247 if (gfc_match (" ::") != MATCH_YES)
8249 gfc_error ("Expected '::' at %C");
8253 /* Match the binding name; depending on type (operator / generic) format
8254 it for future error messages into bind_name. */
8256 m = gfc_match_generic_spec (&op_type, name, &op);
8257 if (m == MATCH_ERROR)
8261 gfc_error ("Expected generic name or operator descriptor at %C");
8267 case INTERFACE_GENERIC:
8268 snprintf (bind_name, sizeof (bind_name), "%s", name);
8271 case INTERFACE_USER_OP:
8272 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8275 case INTERFACE_INTRINSIC_OP:
8276 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8277 gfc_op2string (op));
8284 /* Match the required =>. */
8285 if (gfc_match (" =>") != MATCH_YES)
8287 gfc_error ("Expected '=>' at %C");
8291 /* Try to find existing GENERIC binding with this name / for this operator;
8292 if there is something, check that it is another GENERIC and then extend
8293 it rather than building a new node. Otherwise, create it and put it
8294 at the right position. */
8298 case INTERFACE_USER_OP:
8299 case INTERFACE_GENERIC:
8301 const bool is_op = (op_type == INTERFACE_USER_OP);
8304 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8316 case INTERFACE_INTRINSIC_OP:
8326 if (!tb->is_generic)
8328 gcc_assert (op_type == INTERFACE_GENERIC);
8329 gfc_error ("There's already a non-generic procedure with binding name"
8330 " '%s' for the derived type '%s' at %C",
8331 bind_name, block->name);
8335 if (tb->access != tbattr.access)
8337 gfc_error ("Binding at %C must have the same access as already"
8338 " defined binding '%s'", bind_name);
8344 tb = gfc_get_typebound_proc (NULL);
8345 tb->where = gfc_current_locus;
8346 tb->access = tbattr.access;
8348 tb->u.generic = NULL;
8352 case INTERFACE_GENERIC:
8353 case INTERFACE_USER_OP:
8355 const bool is_op = (op_type == INTERFACE_USER_OP);
8358 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8366 case INTERFACE_INTRINSIC_OP:
8375 /* Now, match all following names as specific targets. */
8378 gfc_symtree* target_st;
8379 gfc_tbp_generic* target;
8381 m = gfc_match_name (name);
8382 if (m == MATCH_ERROR)
8386 gfc_error ("Expected specific binding name at %C");
8390 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8392 /* See if this is a duplicate specification. */
8393 for (target = tb->u.generic; target; target = target->next)
8394 if (target_st == target->specific_st)
8396 gfc_error ("'%s' already defined as specific binding for the"
8397 " generic '%s' at %C", name, bind_name);
8401 target = gfc_get_tbp_generic ();
8402 target->specific_st = target_st;
8403 target->specific = NULL;
8404 target->next = tb->u.generic;
8405 target->is_operator = ((op_type == INTERFACE_USER_OP)
8406 || (op_type == INTERFACE_INTRINSIC_OP));
8407 tb->u.generic = target;
8409 while (gfc_match (" ,") == MATCH_YES);
8411 /* Here should be the end. */
8412 if (gfc_match_eos () != MATCH_YES)
8414 gfc_error ("Junk after GENERIC binding at %C");
8425 /* Match a FINAL declaration inside a derived type. */
8428 gfc_match_final_decl (void)
8430 char name[GFC_MAX_SYMBOL_LEN + 1];
8433 gfc_namespace* module_ns;
8437 if (gfc_current_form == FORM_FREE)
8439 char c = gfc_peek_ascii_char ();
8440 if (!gfc_is_whitespace (c) && c != ':')
8444 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8446 if (gfc_current_form == FORM_FIXED)
8449 gfc_error ("FINAL declaration at %C must be inside a derived type "
8450 "CONTAINS section");
8454 block = gfc_state_stack->previous->sym;
8457 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8458 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8460 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8461 " specification part of a MODULE");
8465 module_ns = gfc_current_ns;
8466 gcc_assert (module_ns);
8467 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8469 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8470 if (gfc_match (" ::") == MATCH_ERROR)
8473 /* Match the sequence of procedure names. */
8480 if (first && gfc_match_eos () == MATCH_YES)
8482 gfc_error ("Empty FINAL at %C");
8486 m = gfc_match_name (name);
8489 gfc_error ("Expected module procedure name at %C");
8492 else if (m != MATCH_YES)
8495 if (gfc_match_eos () == MATCH_YES)
8497 if (!last && gfc_match_char (',') != MATCH_YES)
8499 gfc_error ("Expected ',' at %C");
8503 if (gfc_get_symbol (name, module_ns, &sym))
8505 gfc_error ("Unknown procedure name \"%s\" at %C", name);
8509 /* Mark the symbol as module procedure. */
8510 if (sym->attr.proc != PROC_MODULE
8511 && gfc_add_procedure (&sym->attr, PROC_MODULE,
8512 sym->name, NULL) == FAILURE)
8515 /* Check if we already have this symbol in the list, this is an error. */
8516 for (f = block->f2k_derived->finalizers; f; f = f->next)
8517 if (f->proc_sym == sym)
8519 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8524 /* Add this symbol to the list of finalizers. */
8525 gcc_assert (block->f2k_derived);
8527 f = XCNEW (gfc_finalizer);
8529 f->proc_tree = NULL;
8530 f->where = gfc_current_locus;
8531 f->next = block->f2k_derived->finalizers;
8532 block->f2k_derived->finalizers = f;
8542 const ext_attr_t ext_attr_list[] = {
8543 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8544 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8545 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
8546 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
8547 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
8548 { NULL, EXT_ATTR_LAST, NULL }
8551 /* Match a !GCC$ ATTRIBUTES statement of the form:
8552 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8553 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8555 TODO: We should support all GCC attributes using the same syntax for
8556 the attribute list, i.e. the list in C
8557 __attributes(( attribute-list ))
8559 !GCC$ ATTRIBUTES attribute-list ::
8560 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8563 As there is absolutely no risk of confusion, we should never return
8566 gfc_match_gcc_attributes (void)
8568 symbol_attribute attr;
8569 char name[GFC_MAX_SYMBOL_LEN + 1];
8574 gfc_clear_attr (&attr);
8579 if (gfc_match_name (name) != MATCH_YES)
8582 for (id = 0; id < EXT_ATTR_LAST; id++)
8583 if (strcmp (name, ext_attr_list[id].name) == 0)
8586 if (id == EXT_ATTR_LAST)
8588 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8592 if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
8596 gfc_gobble_whitespace ();
8597 ch = gfc_next_ascii_char ();
8600 /* This is the successful exit condition for the loop. */
8601 if (gfc_next_ascii_char () == ':')
8611 if (gfc_match_eos () == MATCH_YES)
8616 m = gfc_match_name (name);
8620 if (find_special (name, &sym, true))
8623 sym->attr.ext_attr |= attr.ext_attr;
8625 if (gfc_match_eos () == MATCH_YES)
8628 if (gfc_match_char (',') != MATCH_YES)
8635 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");