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 (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 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");
513 if (gfc_implicit_pure (NULL))
514 gfc_current_ns->proc_name->attr.implicit_pure = 0;
516 /* Mark the variable as having appeared in a data statement. */
517 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
523 /* Chain in namespace list of DATA initializers. */
524 newdata->next = gfc_current_ns->data;
525 gfc_current_ns->data = newdata;
531 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
532 we are matching a DATA statement and are therefore issuing an error
533 if we encounter something unexpected, if not, we're trying to match
534 an old-style initialization expression of the form INTEGER I /2/. */
537 gfc_match_data (void)
542 set_in_match_data (true);
546 new_data = gfc_get_data ();
547 new_data->where = gfc_current_locus;
549 m = top_var_list (new_data);
553 m = top_val_list (new_data);
557 new_data->next = gfc_current_ns->data;
558 gfc_current_ns->data = new_data;
560 if (gfc_match_eos () == MATCH_YES)
563 gfc_match_char (','); /* Optional comma */
566 set_in_match_data (false);
570 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
574 if (gfc_implicit_pure (NULL))
575 gfc_current_ns->proc_name->attr.implicit_pure = 0;
580 set_in_match_data (false);
581 gfc_free_data (new_data);
586 /************************ Declaration statements *********************/
589 /* Auxilliary function to merge DIMENSION and CODIMENSION array specs. */
592 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
596 if (to->rank == 0 && from->rank > 0)
598 to->rank = from->rank;
599 to->type = from->type;
600 to->cray_pointee = from->cray_pointee;
601 to->cp_was_assumed = from->cp_was_assumed;
603 for (i = 0; i < to->corank; i++)
605 to->lower[from->rank + i] = to->lower[i];
606 to->upper[from->rank + i] = to->upper[i];
608 for (i = 0; i < from->rank; i++)
612 to->lower[i] = gfc_copy_expr (from->lower[i]);
613 to->upper[i] = gfc_copy_expr (from->upper[i]);
617 to->lower[i] = from->lower[i];
618 to->upper[i] = from->upper[i];
622 else if (to->corank == 0 && from->corank > 0)
624 to->corank = from->corank;
625 to->cotype = from->cotype;
627 for (i = 0; i < from->corank; i++)
631 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
632 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
636 to->lower[to->rank + i] = from->lower[i];
637 to->upper[to->rank + i] = from->upper[i];
644 /* Match an intent specification. Since this can only happen after an
645 INTENT word, a legal intent-spec must follow. */
648 match_intent_spec (void)
651 if (gfc_match (" ( in out )") == MATCH_YES)
653 if (gfc_match (" ( in )") == MATCH_YES)
655 if (gfc_match (" ( out )") == MATCH_YES)
658 gfc_error ("Bad INTENT specification at %C");
659 return INTENT_UNKNOWN;
663 /* Matches a character length specification, which is either a
664 specification expression, '*', or ':'. */
667 char_len_param_value (gfc_expr **expr, bool *deferred)
674 if (gfc_match_char ('*') == MATCH_YES)
677 if (gfc_match_char (':') == MATCH_YES)
679 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: deferred type "
680 "parameter at %C") == FAILURE)
688 m = gfc_match_expr (expr);
691 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
694 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
696 if ((*expr)->value.function.actual
697 && (*expr)->value.function.actual->expr->symtree)
700 e = (*expr)->value.function.actual->expr;
701 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
702 && e->expr_type == EXPR_VARIABLE)
704 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
706 if (e->symtree->n.sym->ts.type == BT_CHARACTER
707 && e->symtree->n.sym->ts.u.cl
708 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
716 gfc_error ("Conflict in attributes of function argument at %C");
721 /* A character length is a '*' followed by a literal integer or a
722 char_len_param_value in parenthesis. */
725 match_char_length (gfc_expr **expr, bool *deferred)
731 m = gfc_match_char ('*');
735 m = gfc_match_small_literal_int (&length, NULL);
736 if (m == MATCH_ERROR)
741 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
742 "Old-style character length at %C") == FAILURE)
744 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
748 if (gfc_match_char ('(') == MATCH_NO)
751 m = char_len_param_value (expr, deferred);
752 if (m != MATCH_YES && gfc_matching_function)
758 if (m == MATCH_ERROR)
763 if (gfc_match_char (')') == MATCH_NO)
765 gfc_free_expr (*expr);
773 gfc_error ("Syntax error in character length specification at %C");
778 /* Special subroutine for finding a symbol. Check if the name is found
779 in the current name space. If not, and we're compiling a function or
780 subroutine and the parent compilation unit is an interface, then check
781 to see if the name we've been given is the name of the interface
782 (located in another namespace). */
785 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
791 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
794 *result = st ? st->n.sym : NULL;
798 if (gfc_current_state () != COMP_SUBROUTINE
799 && gfc_current_state () != COMP_FUNCTION)
802 s = gfc_state_stack->previous;
806 if (s->state != COMP_INTERFACE)
809 goto end; /* Nameless interface. */
811 if (strcmp (name, s->sym->name) == 0)
822 /* Special subroutine for getting a symbol node associated with a
823 procedure name, used in SUBROUTINE and FUNCTION statements. The
824 symbol is created in the parent using with symtree node in the
825 child unit pointing to the symbol. If the current namespace has no
826 parent, then the symbol is just created in the current unit. */
829 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
835 /* Module functions have to be left in their own namespace because
836 they have potentially (almost certainly!) already been referenced.
837 In this sense, they are rather like external functions. This is
838 fixed up in resolve.c(resolve_entries), where the symbol name-
839 space is set to point to the master function, so that the fake
840 result mechanism can work. */
841 if (module_fcn_entry)
843 /* Present if entry is declared to be a module procedure. */
844 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
847 rc = gfc_get_symbol (name, NULL, result);
848 else if (!gfc_get_symbol (name, NULL, &sym) && sym
849 && (*result)->ts.type == BT_UNKNOWN
850 && sym->attr.flavor == FL_UNKNOWN)
851 /* Pick up the typespec for the entry, if declared in the function
852 body. Note that this symbol is FL_UNKNOWN because it will
853 only have appeared in a type declaration. The local symtree
854 is set to point to the module symbol and a unique symtree
855 to the local version. This latter ensures a correct clearing
858 /* If the ENTRY proceeds its specification, we need to ensure
859 that this does not raise a "has no IMPLICIT type" error. */
860 if (sym->ts.type == BT_UNKNOWN)
861 sym->attr.untyped = 1;
863 (*result)->ts = sym->ts;
865 /* Put the symbol in the procedure namespace so that, should
866 the ENTRY precede its specification, the specification
868 (*result)->ns = gfc_current_ns;
870 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
872 st = gfc_get_unique_symtree (gfc_current_ns);
877 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
883 gfc_current_ns->refs++;
885 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
887 /* Trap another encompassed procedure with the same name. All
888 these conditions are necessary to avoid picking up an entry
889 whose name clashes with that of the encompassing procedure;
890 this is handled using gsymbols to register unique,globally
892 if (sym->attr.flavor != 0
893 && sym->attr.proc != 0
894 && (sym->attr.subroutine || sym->attr.function)
895 && sym->attr.if_source != IFSRC_UNKNOWN)
896 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
897 name, &sym->declared_at);
899 /* Trap a procedure with a name the same as interface in the
900 encompassing scope. */
901 if (sym->attr.generic != 0
902 && (sym->attr.subroutine || sym->attr.function)
903 && !sym->attr.mod_proc)
904 gfc_error_now ("Name '%s' at %C is already defined"
905 " as a generic interface at %L",
906 name, &sym->declared_at);
908 /* Trap declarations of attributes in encompassing scope. The
909 signature for this is that ts.kind is set. Legitimate
910 references only set ts.type. */
911 if (sym->ts.kind != 0
912 && !sym->attr.implicit_type
913 && sym->attr.proc == 0
914 && gfc_current_ns->parent != NULL
915 && sym->attr.access == 0
916 && !module_fcn_entry)
917 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
918 "and must not have attributes declared at %L",
919 name, &sym->declared_at);
922 if (gfc_current_ns->parent == NULL || *result == NULL)
925 /* Module function entries will already have a symtree in
926 the current namespace but will need one at module level. */
927 if (module_fcn_entry)
929 /* Present if entry is declared to be a module procedure. */
930 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
932 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
935 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
940 /* See if the procedure should be a module procedure. */
942 if (((sym->ns->proc_name != NULL
943 && sym->ns->proc_name->attr.flavor == FL_MODULE
944 && sym->attr.proc != PROC_MODULE)
945 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
946 && gfc_add_procedure (&sym->attr, PROC_MODULE,
947 sym->name, NULL) == FAILURE)
954 /* Verify that the given symbol representing a parameter is C
955 interoperable, by checking to see if it was marked as such after
956 its declaration. If the given symbol is not interoperable, a
957 warning is reported, thus removing the need to return the status to
958 the calling function. The standard does not require the user use
959 one of the iso_c_binding named constants to declare an
960 interoperable parameter, but we can't be sure if the param is C
961 interop or not if the user doesn't. For example, integer(4) may be
962 legal Fortran, but doesn't have meaning in C. It may interop with
963 a number of the C types, which causes a problem because the
964 compiler can't know which one. This code is almost certainly not
965 portable, and the user will get what they deserve if the C type
966 across platforms isn't always interoperable with integer(4). If
967 the user had used something like integer(c_int) or integer(c_long),
968 the compiler could have automatically handled the varying sizes
972 gfc_verify_c_interop_param (gfc_symbol *sym)
974 int is_c_interop = 0;
975 gfc_try retval = SUCCESS;
977 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
978 Don't repeat the checks here. */
979 if (sym->attr.implicit_type)
982 /* For subroutines or functions that are passed to a BIND(C) procedure,
983 they're interoperable if they're BIND(C) and their params are all
985 if (sym->attr.flavor == FL_PROCEDURE)
987 if (sym->attr.is_bind_c == 0)
989 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
990 "attribute to be C interoperable", sym->name,
991 &(sym->declared_at));
997 if (sym->attr.is_c_interop == 1)
998 /* We've already checked this procedure; don't check it again. */
1001 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1006 /* See if we've stored a reference to a procedure that owns sym. */
1007 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1009 if (sym->ns->proc_name->attr.is_bind_c == 1)
1011 is_c_interop = (gfc_verify_c_interop (&(sym->ts)) == SUCCESS ? 1 : 0);
1013 if (is_c_interop != 1)
1015 /* Make personalized messages to give better feedback. */
1016 if (sym->ts.type == BT_DERIVED)
1017 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1018 "BIND(C) procedure '%s' but is not C interoperable "
1019 "because derived type '%s' is not C interoperable",
1020 sym->name, &(sym->declared_at),
1021 sym->ns->proc_name->name,
1022 sym->ts.u.derived->name);
1023 else if (sym->ts.type == BT_CLASS)
1024 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1025 "BIND(C) procedure '%s' but is not C interoperable "
1026 "because it is polymorphic",
1027 sym->name, &(sym->declared_at),
1028 sym->ns->proc_name->name);
1030 gfc_warning ("Variable '%s' at %L is a parameter to the "
1031 "BIND(C) procedure '%s' but may not be C "
1033 sym->name, &(sym->declared_at),
1034 sym->ns->proc_name->name);
1037 /* Character strings are only C interoperable if they have a
1039 if (sym->ts.type == BT_CHARACTER)
1041 gfc_charlen *cl = sym->ts.u.cl;
1042 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1043 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1045 gfc_error ("Character argument '%s' at %L "
1046 "must be length 1 because "
1047 "procedure '%s' is BIND(C)",
1048 sym->name, &sym->declared_at,
1049 sym->ns->proc_name->name);
1054 /* We have to make sure that any param to a bind(c) routine does
1055 not have the allocatable, pointer, or optional attributes,
1056 according to J3/04-007, section 5.1. */
1057 if (sym->attr.allocatable == 1)
1059 gfc_error ("Variable '%s' at %L cannot have the "
1060 "ALLOCATABLE attribute because procedure '%s'"
1061 " is BIND(C)", sym->name, &(sym->declared_at),
1062 sym->ns->proc_name->name);
1066 if (sym->attr.pointer == 1)
1068 gfc_error ("Variable '%s' at %L cannot have the "
1069 "POINTER attribute because procedure '%s'"
1070 " is BIND(C)", sym->name, &(sym->declared_at),
1071 sym->ns->proc_name->name);
1075 if (sym->attr.optional == 1 && sym->attr.value)
1077 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1078 "and the VALUE attribute because procedure '%s' "
1079 "is BIND(C)", sym->name, &(sym->declared_at),
1080 sym->ns->proc_name->name);
1083 else if (sym->attr.optional == 1
1084 && gfc_notify_std (GFC_STD_F2008_TS, "TS29113: Variable '%s' "
1085 "at %L with OPTIONAL attribute in "
1086 "procedure '%s' which is BIND(C)",
1087 sym->name, &(sym->declared_at),
1088 sym->ns->proc_name->name)
1092 /* Make sure that if it has the dimension attribute, that it is
1093 either assumed size or explicit shape. */
1094 if (sym->as != NULL)
1096 if (sym->as->type == AS_ASSUMED_SHAPE)
1098 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1099 "argument to the procedure '%s' at %L because "
1100 "the procedure is BIND(C)", sym->name,
1101 &(sym->declared_at), sym->ns->proc_name->name,
1102 &(sym->ns->proc_name->declared_at));
1106 if (sym->as->type == AS_DEFERRED)
1108 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1109 "argument to the procedure '%s' at %L because "
1110 "the procedure is BIND(C)", sym->name,
1111 &(sym->declared_at), sym->ns->proc_name->name,
1112 &(sym->ns->proc_name->declared_at));
1124 /* Function called by variable_decl() that adds a name to the symbol table. */
1127 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1128 gfc_array_spec **as, locus *var_locus)
1130 symbol_attribute attr;
1133 if (gfc_get_symbol (name, NULL, &sym))
1136 /* Start updating the symbol table. Add basic type attribute if present. */
1137 if (current_ts.type != BT_UNKNOWN
1138 && (sym->attr.implicit_type == 0
1139 || !gfc_compare_types (&sym->ts, ¤t_ts))
1140 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
1143 if (sym->ts.type == BT_CHARACTER)
1146 sym->ts.deferred = cl_deferred;
1149 /* Add dimension attribute if present. */
1150 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1154 /* Add attribute to symbol. The copy is so that we can reset the
1155 dimension attribute. */
1156 attr = current_attr;
1158 attr.codimension = 0;
1160 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1163 /* Finish any work that may need to be done for the binding label,
1164 if it's a bind(c). The bind(c) attr is found before the symbol
1165 is made, and before the symbol name (for data decls), so the
1166 current_ts is holding the binding label, or nothing if the
1167 name= attr wasn't given. Therefore, test here if we're dealing
1168 with a bind(c) and make sure the binding label is set correctly. */
1169 if (sym->attr.is_bind_c == 1)
1171 if (!sym->binding_label)
1173 /* Set the binding label and verify that if a NAME= was specified
1174 then only one identifier was in the entity-decl-list. */
1175 if (set_binding_label (&sym->binding_label, sym->name,
1176 num_idents_on_line) == FAILURE)
1181 /* See if we know we're in a common block, and if it's a bind(c)
1182 common then we need to make sure we're an interoperable type. */
1183 if (sym->attr.in_common == 1)
1185 /* Test the common block object. */
1186 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1187 && sym->ts.is_c_interop != 1)
1189 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1190 "must be declared with a C interoperable "
1191 "kind since common block '%s' is BIND(C)",
1192 sym->name, sym->common_block->name,
1193 sym->common_block->name);
1198 sym->attr.implied_index = 0;
1200 if (sym->ts.type == BT_CLASS)
1201 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1207 /* Set character constant to the given length. The constant will be padded or
1208 truncated. If we're inside an array constructor without a typespec, we
1209 additionally check that all elements have the same length; check_len -1
1210 means no checking. */
1213 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1218 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1219 gcc_assert (expr->ts.type == BT_CHARACTER);
1221 slen = expr->value.character.length;
1224 s = gfc_get_wide_string (len + 1);
1225 memcpy (s, expr->value.character.string,
1226 MIN (len, slen) * sizeof (gfc_char_t));
1228 gfc_wide_memset (&s[slen], ' ', len - slen);
1230 if (gfc_option.warn_character_truncation && slen > len)
1231 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1232 "(%d/%d)", &expr->where, slen, len);
1234 /* Apply the standard by 'hand' otherwise it gets cleared for
1236 if (check_len != -1 && slen != check_len
1237 && !(gfc_option.allow_std & GFC_STD_GNU))
1238 gfc_error_now ("The CHARACTER elements of the array constructor "
1239 "at %L must have the same length (%d/%d)",
1240 &expr->where, slen, check_len);
1243 free (expr->value.character.string);
1244 expr->value.character.string = s;
1245 expr->value.character.length = len;
1250 /* Function to create and update the enumerator history
1251 using the information passed as arguments.
1252 Pointer "max_enum" is also updated, to point to
1253 enum history node containing largest initializer.
1255 SYM points to the symbol node of enumerator.
1256 INIT points to its enumerator value. */
1259 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1261 enumerator_history *new_enum_history;
1262 gcc_assert (sym != NULL && init != NULL);
1264 new_enum_history = XCNEW (enumerator_history);
1266 new_enum_history->sym = sym;
1267 new_enum_history->initializer = init;
1268 new_enum_history->next = NULL;
1270 if (enum_history == NULL)
1272 enum_history = new_enum_history;
1273 max_enum = enum_history;
1277 new_enum_history->next = enum_history;
1278 enum_history = new_enum_history;
1280 if (mpz_cmp (max_enum->initializer->value.integer,
1281 new_enum_history->initializer->value.integer) < 0)
1282 max_enum = new_enum_history;
1287 /* Function to free enum kind history. */
1290 gfc_free_enum_history (void)
1292 enumerator_history *current = enum_history;
1293 enumerator_history *next;
1295 while (current != NULL)
1297 next = current->next;
1302 enum_history = NULL;
1306 /* Function called by variable_decl() that adds an initialization
1307 expression to a symbol. */
1310 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1312 symbol_attribute attr;
1317 if (find_special (name, &sym, false))
1322 /* If this symbol is confirming an implicit parameter type,
1323 then an initialization expression is not allowed. */
1324 if (attr.flavor == FL_PARAMETER
1325 && sym->value != NULL
1328 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1335 /* An initializer is required for PARAMETER declarations. */
1336 if (attr.flavor == FL_PARAMETER)
1338 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1344 /* If a variable appears in a DATA block, it cannot have an
1348 gfc_error ("Variable '%s' at %C with an initializer already "
1349 "appears in a DATA statement", sym->name);
1353 /* Check if the assignment can happen. This has to be put off
1354 until later for derived type variables and procedure pointers. */
1355 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1356 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1357 && !sym->attr.proc_pointer
1358 && gfc_check_assign_symbol (sym, init) == FAILURE)
1361 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1362 && init->ts.type == BT_CHARACTER)
1364 /* Update symbol character length according initializer. */
1365 if (gfc_check_assign_symbol (sym, init) == FAILURE)
1368 if (sym->ts.u.cl->length == NULL)
1371 /* If there are multiple CHARACTER variables declared on the
1372 same line, we don't want them to share the same length. */
1373 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1375 if (sym->attr.flavor == FL_PARAMETER)
1377 if (init->expr_type == EXPR_CONSTANT)
1379 clen = init->value.character.length;
1380 sym->ts.u.cl->length
1381 = gfc_get_int_expr (gfc_default_integer_kind,
1384 else if (init->expr_type == EXPR_ARRAY)
1387 c = gfc_constructor_first (init->value.constructor);
1388 clen = c->expr->value.character.length;
1389 sym->ts.u.cl->length
1390 = gfc_get_int_expr (gfc_default_integer_kind,
1393 else if (init->ts.u.cl && init->ts.u.cl->length)
1394 sym->ts.u.cl->length =
1395 gfc_copy_expr (sym->value->ts.u.cl->length);
1398 /* Update initializer character length according symbol. */
1399 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1401 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1403 if (init->expr_type == EXPR_CONSTANT)
1404 gfc_set_constant_character_len (len, init, -1);
1405 else if (init->expr_type == EXPR_ARRAY)
1409 /* Build a new charlen to prevent simplification from
1410 deleting the length before it is resolved. */
1411 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1412 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1414 for (c = gfc_constructor_first (init->value.constructor);
1415 c; c = gfc_constructor_next (c))
1416 gfc_set_constant_character_len (len, c->expr, -1);
1421 /* If sym is implied-shape, set its upper bounds from init. */
1422 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1423 && sym->as->type == AS_IMPLIED_SHAPE)
1427 if (init->rank == 0)
1429 gfc_error ("Can't initialize implied-shape array at %L"
1430 " with scalar", &sym->declared_at);
1433 gcc_assert (sym->as->rank == init->rank);
1435 /* Shape should be present, we get an initialization expression. */
1436 gcc_assert (init->shape);
1438 for (dim = 0; dim < sym->as->rank; ++dim)
1444 lower = sym->as->lower[dim];
1445 if (lower->expr_type != EXPR_CONSTANT)
1447 gfc_error ("Non-constant lower bound in implied-shape"
1448 " declaration at %L", &lower->where);
1452 /* All dimensions must be without upper bound. */
1453 gcc_assert (!sym->as->upper[dim]);
1456 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1457 mpz_add (e->value.integer,
1458 lower->value.integer, init->shape[dim]);
1459 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1460 sym->as->upper[dim] = e;
1463 sym->as->type = AS_EXPLICIT;
1466 /* Need to check if the expression we initialized this
1467 to was one of the iso_c_binding named constants. If so,
1468 and we're a parameter (constant), let it be iso_c.
1470 integer(c_int), parameter :: my_int = c_int
1471 integer(my_int) :: my_int_2
1472 If we mark my_int as iso_c (since we can see it's value
1473 is equal to one of the named constants), then my_int_2
1474 will be considered C interoperable. */
1475 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1477 sym->ts.is_iso_c |= init->ts.is_iso_c;
1478 sym->ts.is_c_interop |= init->ts.is_c_interop;
1479 /* attr bits needed for module files. */
1480 sym->attr.is_iso_c |= init->ts.is_iso_c;
1481 sym->attr.is_c_interop |= init->ts.is_c_interop;
1482 if (init->ts.is_iso_c)
1483 sym->ts.f90_type = init->ts.f90_type;
1486 /* Add initializer. Make sure we keep the ranks sane. */
1487 if (sym->attr.dimension && init->rank == 0)
1492 if (sym->attr.flavor == FL_PARAMETER
1493 && init->expr_type == EXPR_CONSTANT
1494 && spec_size (sym->as, &size) == SUCCESS
1495 && mpz_cmp_si (size, 0) > 0)
1497 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1499 for (n = 0; n < (int)mpz_get_si (size); n++)
1500 gfc_constructor_append_expr (&array->value.constructor,
1503 : gfc_copy_expr (init),
1506 array->shape = gfc_get_shape (sym->as->rank);
1507 for (n = 0; n < sym->as->rank; n++)
1508 spec_dimen_size (sym->as, n, &array->shape[n]);
1513 init->rank = sym->as->rank;
1517 if (sym->attr.save == SAVE_NONE)
1518 sym->attr.save = SAVE_IMPLICIT;
1526 /* Function called by variable_decl() that adds a name to a structure
1530 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1531 gfc_array_spec **as)
1534 gfc_try t = SUCCESS;
1536 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1537 constructing, it must have the pointer attribute. */
1538 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1539 && current_ts.u.derived == gfc_current_block ()
1540 && current_attr.pointer == 0)
1542 gfc_error ("Component at %C must have the POINTER attribute");
1546 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1548 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1550 gfc_error ("Array component of structure at %C must have explicit "
1551 "or deferred shape");
1556 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1560 if (c->ts.type == BT_CHARACTER)
1562 c->attr = current_attr;
1564 c->initializer = *init;
1571 c->attr.codimension = 1;
1573 c->attr.dimension = 1;
1577 /* Should this ever get more complicated, combine with similar section
1578 in add_init_expr_to_sym into a separate function. */
1579 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1581 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1585 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1586 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1587 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1589 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1591 if (c->initializer->expr_type == EXPR_CONSTANT)
1592 gfc_set_constant_character_len (len, c->initializer, -1);
1593 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1594 c->initializer->ts.u.cl->length->value.integer))
1596 gfc_constructor *ctor;
1597 ctor = gfc_constructor_first (c->initializer->value.constructor);
1602 bool has_ts = (c->initializer->ts.u.cl
1603 && c->initializer->ts.u.cl->length_from_typespec);
1605 /* Remember the length of the first element for checking
1606 that all elements *in the constructor* have the same
1607 length. This need not be the length of the LHS! */
1608 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1609 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1610 first_len = ctor->expr->value.character.length;
1612 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1613 if (ctor->expr->expr_type == EXPR_CONSTANT)
1615 gfc_set_constant_character_len (len, ctor->expr,
1616 has_ts ? -1 : first_len);
1617 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1623 /* Check array components. */
1624 if (!c->attr.dimension)
1627 if (c->attr.pointer)
1629 if (c->as->type != AS_DEFERRED)
1631 gfc_error ("Pointer array component of structure at %C must have a "
1636 else if (c->attr.allocatable)
1638 if (c->as->type != AS_DEFERRED)
1640 gfc_error ("Allocatable component of structure at %C must have a "
1647 if (c->as->type != AS_EXPLICIT)
1649 gfc_error ("Array component of structure at %C must have an "
1656 if (c->ts.type == BT_CLASS)
1658 bool delayed = (gfc_state_stack->sym == c->ts.u.derived)
1659 || (!c->ts.u.derived->components
1660 && !c->ts.u.derived->attr.zero_comp);
1661 return gfc_build_class_symbol (&c->ts, &c->attr, &c->as, delayed);
1668 /* Match a 'NULL()', and possibly take care of some side effects. */
1671 gfc_match_null (gfc_expr **result)
1676 m = gfc_match (" null ( )");
1680 /* The NULL symbol now has to be/become an intrinsic function. */
1681 if (gfc_get_symbol ("null", NULL, &sym))
1683 gfc_error ("NULL() initialization at %C is ambiguous");
1687 gfc_intrinsic_symbol (sym);
1689 if (sym->attr.proc != PROC_INTRINSIC
1690 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1691 sym->name, NULL) == FAILURE
1692 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1695 *result = gfc_get_null_expr (&gfc_current_locus);
1701 /* Match the initialization expr for a data pointer or procedure pointer. */
1704 match_pointer_init (gfc_expr **init, int procptr)
1708 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1710 gfc_error ("Initialization of pointer at %C is not allowed in "
1711 "a PURE procedure");
1715 /* Match NULL() initilization. */
1716 m = gfc_match_null (init);
1720 /* Match non-NULL initialization. */
1721 gfc_matching_ptr_assignment = !procptr;
1722 gfc_matching_procptr_assignment = procptr;
1723 m = gfc_match_rvalue (init);
1724 gfc_matching_ptr_assignment = 0;
1725 gfc_matching_procptr_assignment = 0;
1726 if (m == MATCH_ERROR)
1728 else if (m == MATCH_NO)
1730 gfc_error ("Error in pointer initialization at %C");
1735 gfc_resolve_expr (*init);
1737 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: non-NULL pointer "
1738 "initialization at %C") == FAILURE)
1746 check_function_name (char *name)
1748 /* In functions that have a RESULT variable defined, the function name always
1749 refers to function calls. Therefore, the name is not allowed to appear in
1750 specification statements. When checking this, be careful about
1751 'hidden' procedure pointer results ('ppr@'). */
1753 if (gfc_current_state () == COMP_FUNCTION)
1755 gfc_symbol *block = gfc_current_block ();
1756 if (block && block->result && block->result != block
1757 && strcmp (block->result->name, "ppr@") != 0
1758 && strcmp (block->name, name) == 0)
1760 gfc_error ("Function name '%s' not allowed at %C", name);
1769 /* Match a variable name with an optional initializer. When this
1770 subroutine is called, a variable is expected to be parsed next.
1771 Depending on what is happening at the moment, updates either the
1772 symbol table or the current interface. */
1775 variable_decl (int elem)
1777 char name[GFC_MAX_SYMBOL_LEN + 1];
1778 gfc_expr *initializer, *char_len;
1780 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1792 /* When we get here, we've just matched a list of attributes and
1793 maybe a type and a double colon. The next thing we expect to see
1794 is the name of the symbol. */
1795 m = gfc_match_name (name);
1799 var_locus = gfc_current_locus;
1801 /* Now we could see the optional array spec. or character length. */
1802 m = gfc_match_array_spec (&as, true, true);
1803 if (m == MATCH_ERROR)
1807 as = gfc_copy_array_spec (current_as);
1808 else if (current_as)
1809 merge_array_spec (current_as, as, true);
1811 if (gfc_option.flag_cray_pointer)
1812 cp_as = gfc_copy_array_spec (as);
1814 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1815 determine (and check) whether it can be implied-shape. If it
1816 was parsed as assumed-size, change it because PARAMETERs can not
1820 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1823 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1828 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1829 && current_attr.flavor == FL_PARAMETER)
1830 as->type = AS_IMPLIED_SHAPE;
1832 if (as->type == AS_IMPLIED_SHAPE
1833 && gfc_notify_std (GFC_STD_F2008,
1834 "Fortran 2008: Implied-shape array at %L",
1835 &var_locus) == FAILURE)
1844 cl_deferred = false;
1846 if (current_ts.type == BT_CHARACTER)
1848 switch (match_char_length (&char_len, &cl_deferred))
1851 cl = gfc_new_charlen (gfc_current_ns, NULL);
1853 cl->length = char_len;
1856 /* Non-constant lengths need to be copied after the first
1857 element. Also copy assumed lengths. */
1860 && (current_ts.u.cl->length == NULL
1861 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1863 cl = gfc_new_charlen (gfc_current_ns, NULL);
1864 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1867 cl = current_ts.u.cl;
1869 cl_deferred = current_ts.deferred;
1878 /* If this symbol has already shown up in a Cray Pointer declaration,
1879 then we want to set the type & bail out. */
1880 if (gfc_option.flag_cray_pointer)
1882 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1883 if (sym != NULL && sym->attr.cray_pointee)
1885 sym->ts.type = current_ts.type;
1886 sym->ts.kind = current_ts.kind;
1888 sym->ts.u.derived = current_ts.u.derived;
1889 sym->ts.is_c_interop = current_ts.is_c_interop;
1890 sym->ts.is_iso_c = current_ts.is_iso_c;
1893 /* Check to see if we have an array specification. */
1896 if (sym->as != NULL)
1898 gfc_error ("Duplicate array spec for Cray pointee at %C");
1899 gfc_free_array_spec (cp_as);
1905 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1906 gfc_internal_error ("Couldn't set pointee array spec.");
1908 /* Fix the array spec. */
1909 m = gfc_mod_pointee_as (sym->as);
1910 if (m == MATCH_ERROR)
1918 gfc_free_array_spec (cp_as);
1922 /* Procedure pointer as function result. */
1923 if (gfc_current_state () == COMP_FUNCTION
1924 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1925 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1926 strcpy (name, "ppr@");
1928 if (gfc_current_state () == COMP_FUNCTION
1929 && strcmp (name, gfc_current_block ()->name) == 0
1930 && gfc_current_block ()->result
1931 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1932 strcpy (name, "ppr@");
1934 /* OK, we've successfully matched the declaration. Now put the
1935 symbol in the current namespace, because it might be used in the
1936 optional initialization expression for this symbol, e.g. this is
1939 integer, parameter :: i = huge(i)
1941 This is only true for parameters or variables of a basic type.
1942 For components of derived types, it is not true, so we don't
1943 create a symbol for those yet. If we fail to create the symbol,
1945 if (gfc_current_state () != COMP_DERIVED
1946 && build_sym (name, cl, cl_deferred, &as, &var_locus) == FAILURE)
1952 /* An interface body specifies all of the procedure's
1953 characteristics and these shall be consistent with those
1954 specified in the procedure definition, except that the interface
1955 may specify a procedure that is not pure if the procedure is
1956 defined to be pure(12.3.2). */
1957 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1958 && gfc_current_ns->proc_name
1959 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1960 && current_ts.u.derived->ns != gfc_current_ns)
1963 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.u.derived->name);
1964 if (!(current_ts.u.derived->attr.imported
1966 && gfc_find_dt_in_generic (st->n.sym) == current_ts.u.derived)
1967 && !gfc_current_ns->has_import_set)
1969 gfc_error ("The type of '%s' at %C has not been declared within the "
1976 if (check_function_name (name) == FAILURE)
1982 /* We allow old-style initializations of the form
1983 integer i /2/, j(4) /3*3, 1/
1984 (if no colon has been seen). These are different from data
1985 statements in that initializers are only allowed to apply to the
1986 variable immediately preceding, i.e.
1988 is not allowed. Therefore we have to do some work manually, that
1989 could otherwise be left to the matchers for DATA statements. */
1991 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1993 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1994 "initialization at %C") == FAILURE)
1997 return match_old_style_init (name);
2000 /* The double colon must be present in order to have initializers.
2001 Otherwise the statement is ambiguous with an assignment statement. */
2004 if (gfc_match (" =>") == MATCH_YES)
2006 if (!current_attr.pointer)
2008 gfc_error ("Initialization at %C isn't for a pointer variable");
2013 m = match_pointer_init (&initializer, 0);
2017 else if (gfc_match_char ('=') == MATCH_YES)
2019 if (current_attr.pointer)
2021 gfc_error ("Pointer initialization at %C requires '=>', "
2027 m = gfc_match_init_expr (&initializer);
2030 gfc_error ("Expected an initialization expression at %C");
2034 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2035 && gfc_state_stack->state != COMP_DERIVED)
2037 gfc_error ("Initialization of variable at %C is not allowed in "
2038 "a PURE procedure");
2047 if (initializer != NULL && current_attr.allocatable
2048 && gfc_current_state () == COMP_DERIVED)
2050 gfc_error ("Initialization of allocatable component at %C is not "
2056 /* Add the initializer. Note that it is fine if initializer is
2057 NULL here, because we sometimes also need to check if a
2058 declaration *must* have an initialization expression. */
2059 if (gfc_current_state () != COMP_DERIVED)
2060 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2063 if (current_ts.type == BT_DERIVED
2064 && !current_attr.pointer && !initializer)
2065 initializer = gfc_default_initializer (¤t_ts);
2066 t = build_struct (name, cl, &initializer, &as);
2069 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
2072 /* Free stuff up and return. */
2073 gfc_free_expr (initializer);
2074 gfc_free_array_spec (as);
2080 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2081 This assumes that the byte size is equal to the kind number for
2082 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2085 gfc_match_old_kind_spec (gfc_typespec *ts)
2090 if (gfc_match_char ('*') != MATCH_YES)
2093 m = gfc_match_small_literal_int (&ts->kind, NULL);
2097 original_kind = ts->kind;
2099 /* Massage the kind numbers for complex types. */
2100 if (ts->type == BT_COMPLEX)
2104 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2105 gfc_basic_typename (ts->type), original_kind);
2112 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2115 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2119 if (gfc_option.flag_real4_kind == 8)
2121 if (gfc_option.flag_real4_kind == 10)
2123 if (gfc_option.flag_real4_kind == 16)
2129 if (gfc_option.flag_real8_kind == 4)
2131 if (gfc_option.flag_real8_kind == 10)
2133 if (gfc_option.flag_real8_kind == 16)
2138 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2140 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2141 gfc_basic_typename (ts->type), original_kind);
2145 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2146 gfc_basic_typename (ts->type), original_kind) == FAILURE)
2153 /* Match a kind specification. Since kinds are generally optional, we
2154 usually return MATCH_NO if something goes wrong. If a "kind="
2155 string is found, then we know we have an error. */
2158 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2170 where = loc = gfc_current_locus;
2175 if (gfc_match_char ('(') == MATCH_NO)
2178 /* Also gobbles optional text. */
2179 if (gfc_match (" kind = ") == MATCH_YES)
2182 loc = gfc_current_locus;
2185 n = gfc_match_init_expr (&e);
2189 if (gfc_matching_function)
2191 /* The function kind expression might include use associated or
2192 imported parameters and try again after the specification
2194 if (gfc_match_char (')') != MATCH_YES)
2196 gfc_error ("Missing right parenthesis at %C");
2202 gfc_undo_symbols ();
2207 /* ....or else, the match is real. */
2209 gfc_error ("Expected initialization expression at %C");
2217 gfc_error ("Expected scalar initialization expression at %C");
2222 msg = gfc_extract_int (e, &ts->kind);
2231 /* Before throwing away the expression, let's see if we had a
2232 C interoperable kind (and store the fact). */
2233 if (e->ts.is_c_interop == 1)
2235 /* Mark this as c interoperable if being declared with one
2236 of the named constants from iso_c_binding. */
2237 ts->is_c_interop = e->ts.is_iso_c;
2238 ts->f90_type = e->ts.f90_type;
2244 /* Ignore errors to this point, if we've gotten here. This means
2245 we ignore the m=MATCH_ERROR from above. */
2246 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2248 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2249 gfc_basic_typename (ts->type));
2250 gfc_current_locus = where;
2254 /* Warn if, e.g., c_int is used for a REAL variable, but not
2255 if, e.g., c_double is used for COMPLEX as the standard
2256 explicitly says that the kind type parameter for complex and real
2257 variable is the same, i.e. c_float == c_float_complex. */
2258 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2259 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2260 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2261 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2262 "is %s", gfc_basic_typename (ts->f90_type), &where,
2263 gfc_basic_typename (ts->type));
2265 gfc_gobble_whitespace ();
2266 if ((c = gfc_next_ascii_char ()) != ')'
2267 && (ts->type != BT_CHARACTER || c != ','))
2269 if (ts->type == BT_CHARACTER)
2270 gfc_error ("Missing right parenthesis or comma at %C");
2272 gfc_error ("Missing right parenthesis at %C");
2276 /* All tests passed. */
2279 if(m == MATCH_ERROR)
2280 gfc_current_locus = where;
2282 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2285 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2289 if (gfc_option.flag_real4_kind == 8)
2291 if (gfc_option.flag_real4_kind == 10)
2293 if (gfc_option.flag_real4_kind == 16)
2299 if (gfc_option.flag_real8_kind == 4)
2301 if (gfc_option.flag_real8_kind == 10)
2303 if (gfc_option.flag_real8_kind == 16)
2308 /* Return what we know from the test(s). */
2313 gfc_current_locus = where;
2319 match_char_kind (int * kind, int * is_iso_c)
2328 where = gfc_current_locus;
2330 n = gfc_match_init_expr (&e);
2332 if (n != MATCH_YES && gfc_matching_function)
2334 /* The expression might include use-associated or imported
2335 parameters and try again after the specification
2338 gfc_undo_symbols ();
2343 gfc_error ("Expected initialization expression at %C");
2349 gfc_error ("Expected scalar initialization expression at %C");
2354 msg = gfc_extract_int (e, kind);
2355 *is_iso_c = e->ts.is_iso_c;
2365 /* Ignore errors to this point, if we've gotten here. This means
2366 we ignore the m=MATCH_ERROR from above. */
2367 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2369 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2373 /* All tests passed. */
2376 if (m == MATCH_ERROR)
2377 gfc_current_locus = where;
2379 /* Return what we know from the test(s). */
2384 gfc_current_locus = where;
2389 /* Match the various kind/length specifications in a CHARACTER
2390 declaration. We don't return MATCH_NO. */
2393 gfc_match_char_spec (gfc_typespec *ts)
2395 int kind, seen_length, is_iso_c;
2407 /* Try the old-style specification first. */
2408 old_char_selector = 0;
2410 m = match_char_length (&len, &deferred);
2414 old_char_selector = 1;
2419 m = gfc_match_char ('(');
2422 m = MATCH_YES; /* Character without length is a single char. */
2426 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2427 if (gfc_match (" kind =") == MATCH_YES)
2429 m = match_char_kind (&kind, &is_iso_c);
2431 if (m == MATCH_ERROR)
2436 if (gfc_match (" , len =") == MATCH_NO)
2439 m = char_len_param_value (&len, &deferred);
2442 if (m == MATCH_ERROR)
2449 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2450 if (gfc_match (" len =") == MATCH_YES)
2452 m = char_len_param_value (&len, &deferred);
2455 if (m == MATCH_ERROR)
2459 if (gfc_match_char (')') == MATCH_YES)
2462 if (gfc_match (" , kind =") != MATCH_YES)
2465 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2471 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2472 m = char_len_param_value (&len, &deferred);
2475 if (m == MATCH_ERROR)
2479 m = gfc_match_char (')');
2483 if (gfc_match_char (',') != MATCH_YES)
2486 gfc_match (" kind ="); /* Gobble optional text. */
2488 m = match_char_kind (&kind, &is_iso_c);
2489 if (m == MATCH_ERROR)
2495 /* Require a right-paren at this point. */
2496 m = gfc_match_char (')');
2501 gfc_error ("Syntax error in CHARACTER declaration at %C");
2503 gfc_free_expr (len);
2507 /* Deal with character functions after USE and IMPORT statements. */
2508 if (gfc_matching_function)
2510 gfc_free_expr (len);
2511 gfc_undo_symbols ();
2517 gfc_free_expr (len);
2521 /* Do some final massaging of the length values. */
2522 cl = gfc_new_charlen (gfc_current_ns, NULL);
2524 if (seen_length == 0)
2525 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2530 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2531 ts->deferred = deferred;
2533 /* We have to know if it was a c interoperable kind so we can
2534 do accurate type checking of bind(c) procs, etc. */
2536 /* Mark this as c interoperable if being declared with one
2537 of the named constants from iso_c_binding. */
2538 ts->is_c_interop = is_iso_c;
2539 else if (len != NULL)
2540 /* Here, we might have parsed something such as: character(c_char)
2541 In this case, the parsing code above grabs the c_char when
2542 looking for the length (line 1690, roughly). it's the last
2543 testcase for parsing the kind params of a character variable.
2544 However, it's not actually the length. this seems like it
2546 To see if the user used a C interop kind, test the expr
2547 of the so called length, and see if it's C interoperable. */
2548 ts->is_c_interop = len->ts.is_iso_c;
2554 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2555 structure to the matched specification. This is necessary for FUNCTION and
2556 IMPLICIT statements.
2558 If implicit_flag is nonzero, then we don't check for the optional
2559 kind specification. Not doing so is needed for matching an IMPLICIT
2560 statement correctly. */
2563 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2565 char name[GFC_MAX_SYMBOL_LEN + 1];
2566 gfc_symbol *sym, *dt_sym;
2569 bool seen_deferred_kind, matched_type;
2570 const char *dt_name;
2572 /* A belt and braces check that the typespec is correctly being treated
2573 as a deferred characteristic association. */
2574 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2575 && (gfc_current_block ()->result->ts.kind == -1)
2576 && (ts->kind == -1);
2578 if (seen_deferred_kind)
2581 /* Clear the current binding label, in case one is given. */
2582 curr_binding_label = NULL;
2584 if (gfc_match (" byte") == MATCH_YES)
2586 if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2590 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2592 gfc_error ("BYTE type used at %C "
2593 "is not available on the target machine");
2597 ts->type = BT_INTEGER;
2603 m = gfc_match (" type ( %n", name);
2604 matched_type = (m == MATCH_YES);
2606 if ((matched_type && strcmp ("integer", name) == 0)
2607 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2609 ts->type = BT_INTEGER;
2610 ts->kind = gfc_default_integer_kind;
2614 if ((matched_type && strcmp ("character", name) == 0)
2615 || (!matched_type && gfc_match (" character") == MATCH_YES))
2618 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2619 "intrinsic-type-spec at %C") == FAILURE)
2622 ts->type = BT_CHARACTER;
2623 if (implicit_flag == 0)
2624 m = gfc_match_char_spec (ts);
2628 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2634 if ((matched_type && strcmp ("real", name) == 0)
2635 || (!matched_type && gfc_match (" real") == MATCH_YES))
2638 ts->kind = gfc_default_real_kind;
2643 && (strcmp ("doubleprecision", name) == 0
2644 || (strcmp ("double", name) == 0
2645 && gfc_match (" precision") == MATCH_YES)))
2646 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2649 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2650 "intrinsic-type-spec at %C") == FAILURE)
2652 if (matched_type && gfc_match_char (')') != MATCH_YES)
2656 ts->kind = gfc_default_double_kind;
2660 if ((matched_type && strcmp ("complex", name) == 0)
2661 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2663 ts->type = BT_COMPLEX;
2664 ts->kind = gfc_default_complex_kind;
2669 && (strcmp ("doublecomplex", name) == 0
2670 || (strcmp ("double", name) == 0
2671 && gfc_match (" complex") == MATCH_YES)))
2672 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2674 if (gfc_notify_std (GFC_STD_GNU, "Extension: DOUBLE COMPLEX at %C")
2679 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2680 "intrinsic-type-spec at %C") == FAILURE)
2683 if (matched_type && gfc_match_char (')') != MATCH_YES)
2686 ts->type = BT_COMPLEX;
2687 ts->kind = gfc_default_double_kind;
2691 if ((matched_type && strcmp ("logical", name) == 0)
2692 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2694 ts->type = BT_LOGICAL;
2695 ts->kind = gfc_default_logical_kind;
2700 m = gfc_match_char (')');
2703 ts->type = BT_DERIVED;
2706 /* Match CLASS declarations. */
2707 m = gfc_match (" class ( * )");
2708 if (m == MATCH_ERROR)
2710 else if (m == MATCH_YES)
2712 gfc_fatal_error ("Unlimited polymorphism at %C not yet supported");
2716 m = gfc_match (" class ( %n )", name);
2719 ts->type = BT_CLASS;
2721 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2726 /* Defer association of the derived type until the end of the
2727 specification block. However, if the derived type can be
2728 found, add it to the typespec. */
2729 if (gfc_matching_function)
2731 ts->u.derived = NULL;
2732 if (gfc_current_state () != COMP_INTERFACE
2733 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2735 sym = gfc_find_dt_in_generic (sym);
2736 ts->u.derived = sym;
2741 /* Search for the name but allow the components to be defined later. If
2742 type = -1, this typespec has been seen in a function declaration but
2743 the type could not be accessed at that point. The actual derived type is
2744 stored in a symtree with the first letter of the name captialized; the
2745 symtree with the all lower-case name contains the associated
2746 generic function. */
2747 dt_name = gfc_get_string ("%c%s",
2748 (char) TOUPPER ((unsigned char) name[0]),
2749 (const char*)&name[1]);
2754 gfc_get_ha_symbol (name, &sym);
2755 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2757 gfc_error ("Type name '%s' at %C is ambiguous", name);
2760 if (sym->generic && !dt_sym)
2761 dt_sym = gfc_find_dt_in_generic (sym);
2763 else if (ts->kind == -1)
2765 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2766 || gfc_current_ns->has_import_set;
2767 gfc_find_symbol (name, NULL, iface, &sym);
2768 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2770 gfc_error ("Type name '%s' at %C is ambiguous", name);
2773 if (sym && sym->generic && !dt_sym)
2774 dt_sym = gfc_find_dt_in_generic (sym);
2781 if ((sym->attr.flavor != FL_UNKNOWN
2782 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2783 || sym->attr.subroutine)
2785 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2786 "entity at %L, which has the same name", name,
2791 gfc_set_sym_referenced (sym);
2792 if (!sym->attr.generic
2793 && gfc_add_generic (&sym->attr, sym->name, NULL) == FAILURE)
2796 if (!sym->attr.function
2797 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2802 gfc_interface *intr, *head;
2804 /* Use upper case to save the actual derived-type symbol. */
2805 gfc_get_symbol (dt_name, NULL, &dt_sym);
2806 dt_sym->name = gfc_get_string (sym->name);
2807 head = sym->generic;
2808 intr = gfc_get_interface ();
2810 intr->where = gfc_current_locus;
2812 sym->generic = intr;
2813 sym->attr.if_source = IFSRC_DECL;
2816 gfc_set_sym_referenced (dt_sym);
2818 if (dt_sym->attr.flavor != FL_DERIVED
2819 && gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL)
2823 ts->u.derived = dt_sym;
2829 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2830 "intrinsic-type-spec at %C") == FAILURE)
2833 /* For all types except double, derived and character, look for an
2834 optional kind specifier. MATCH_NO is actually OK at this point. */
2835 if (implicit_flag == 1)
2837 if (matched_type && gfc_match_char (')') != MATCH_YES)
2843 if (gfc_current_form == FORM_FREE)
2845 c = gfc_peek_ascii_char ();
2846 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2847 && c != ':' && c != ',')
2849 if (matched_type && c == ')')
2851 gfc_next_ascii_char ();
2858 m = gfc_match_kind_spec (ts, false);
2859 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2860 m = gfc_match_old_kind_spec (ts);
2862 if (matched_type && gfc_match_char (')') != MATCH_YES)
2865 /* Defer association of the KIND expression of function results
2866 until after USE and IMPORT statements. */
2867 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2868 || gfc_matching_function)
2872 m = MATCH_YES; /* No kind specifier found. */
2878 /* Match an IMPLICIT NONE statement. Actually, this statement is
2879 already matched in parse.c, or we would not end up here in the
2880 first place. So the only thing we need to check, is if there is
2881 trailing garbage. If not, the match is successful. */
2884 gfc_match_implicit_none (void)
2886 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2890 /* Match the letter range(s) of an IMPLICIT statement. */
2893 match_implicit_range (void)
2899 cur_loc = gfc_current_locus;
2901 gfc_gobble_whitespace ();
2902 c = gfc_next_ascii_char ();
2905 gfc_error ("Missing character range in IMPLICIT at %C");
2912 gfc_gobble_whitespace ();
2913 c1 = gfc_next_ascii_char ();
2917 gfc_gobble_whitespace ();
2918 c = gfc_next_ascii_char ();
2923 inner = 0; /* Fall through. */
2930 gfc_gobble_whitespace ();
2931 c2 = gfc_next_ascii_char ();
2935 gfc_gobble_whitespace ();
2936 c = gfc_next_ascii_char ();
2938 if ((c != ',') && (c != ')'))
2951 gfc_error ("Letters must be in alphabetic order in "
2952 "IMPLICIT statement at %C");
2956 /* See if we can add the newly matched range to the pending
2957 implicits from this IMPLICIT statement. We do not check for
2958 conflicts with whatever earlier IMPLICIT statements may have
2959 set. This is done when we've successfully finished matching
2961 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2968 gfc_syntax_error (ST_IMPLICIT);
2970 gfc_current_locus = cur_loc;
2975 /* Match an IMPLICIT statement, storing the types for
2976 gfc_set_implicit() if the statement is accepted by the parser.
2977 There is a strange looking, but legal syntactic construction
2978 possible. It looks like:
2980 IMPLICIT INTEGER (a-b) (c-d)
2982 This is legal if "a-b" is a constant expression that happens to
2983 equal one of the legal kinds for integers. The real problem
2984 happens with an implicit specification that looks like:
2986 IMPLICIT INTEGER (a-b)
2988 In this case, a typespec matcher that is "greedy" (as most of the
2989 matchers are) gobbles the character range as a kindspec, leaving
2990 nothing left. We therefore have to go a bit more slowly in the
2991 matching process by inhibiting the kindspec checking during
2992 typespec matching and checking for a kind later. */
2995 gfc_match_implicit (void)
3004 /* We don't allow empty implicit statements. */
3005 if (gfc_match_eos () == MATCH_YES)
3007 gfc_error ("Empty IMPLICIT statement at %C");
3013 /* First cleanup. */
3014 gfc_clear_new_implicit ();
3016 /* A basic type is mandatory here. */
3017 m = gfc_match_decl_type_spec (&ts, 1);
3018 if (m == MATCH_ERROR)
3023 cur_loc = gfc_current_locus;
3024 m = match_implicit_range ();
3028 /* We may have <TYPE> (<RANGE>). */
3029 gfc_gobble_whitespace ();
3030 c = gfc_next_ascii_char ();
3031 if ((c == '\n') || (c == ','))
3033 /* Check for CHARACTER with no length parameter. */
3034 if (ts.type == BT_CHARACTER && !ts.u.cl)
3036 ts.kind = gfc_default_character_kind;
3037 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3038 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3042 /* Record the Successful match. */
3043 if (gfc_merge_new_implicit (&ts) != SUCCESS)
3048 gfc_current_locus = cur_loc;
3051 /* Discard the (incorrectly) matched range. */
3052 gfc_clear_new_implicit ();
3054 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3055 if (ts.type == BT_CHARACTER)
3056 m = gfc_match_char_spec (&ts);
3059 m = gfc_match_kind_spec (&ts, false);
3062 m = gfc_match_old_kind_spec (&ts);
3063 if (m == MATCH_ERROR)
3069 if (m == MATCH_ERROR)
3072 m = match_implicit_range ();
3073 if (m == MATCH_ERROR)
3078 gfc_gobble_whitespace ();
3079 c = gfc_next_ascii_char ();
3080 if ((c != '\n') && (c != ','))
3083 if (gfc_merge_new_implicit (&ts) != SUCCESS)
3091 gfc_syntax_error (ST_IMPLICIT);
3099 gfc_match_import (void)
3101 char name[GFC_MAX_SYMBOL_LEN + 1];
3106 if (gfc_current_ns->proc_name == NULL
3107 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3109 gfc_error ("IMPORT statement at %C only permitted in "
3110 "an INTERFACE body");
3114 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
3118 if (gfc_match_eos () == MATCH_YES)
3120 /* All host variables should be imported. */
3121 gfc_current_ns->has_import_set = 1;
3125 if (gfc_match (" ::") == MATCH_YES)
3127 if (gfc_match_eos () == MATCH_YES)
3129 gfc_error ("Expecting list of named entities at %C");
3137 m = gfc_match (" %n", name);
3141 if (gfc_current_ns->parent != NULL
3142 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3144 gfc_error ("Type name '%s' at %C is ambiguous", name);
3147 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3148 && gfc_find_symbol (name,
3149 gfc_current_ns->proc_name->ns->parent,
3152 gfc_error ("Type name '%s' at %C is ambiguous", name);
3158 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3159 "at %C - does not exist.", name);
3163 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
3165 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3170 st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
3173 sym->attr.imported = 1;
3175 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3177 /* The actual derived type is stored in a symtree with the first
3178 letter of the name captialized; the symtree with the all
3179 lower-case name contains the associated generic function. */
3180 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3181 gfc_get_string ("%c%s",
3182 (char) TOUPPER ((unsigned char) sym->name[0]),
3186 sym->attr.imported = 1;
3199 if (gfc_match_eos () == MATCH_YES)
3201 if (gfc_match_char (',') != MATCH_YES)
3208 gfc_error ("Syntax error in IMPORT statement at %C");
3213 /* A minimal implementation of gfc_match without whitespace, escape
3214 characters or variable arguments. Returns true if the next
3215 characters match the TARGET template exactly. */
3218 match_string_p (const char *target)
3222 for (p = target; *p; p++)
3223 if ((char) gfc_next_ascii_char () != *p)
3228 /* Matches an attribute specification including array specs. If
3229 successful, leaves the variables current_attr and current_as
3230 holding the specification. Also sets the colon_seen variable for
3231 later use by matchers associated with initializations.
3233 This subroutine is a little tricky in the sense that we don't know
3234 if we really have an attr-spec until we hit the double colon.
3235 Until that time, we can only return MATCH_NO. This forces us to
3236 check for duplicate specification at this level. */
3239 match_attr_spec (void)
3241 /* Modifiers that can exist in a type statement. */
3243 { GFC_DECL_BEGIN = 0,
3244 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3245 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3246 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3247 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3248 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3249 DECL_NONE, GFC_DECL_END /* Sentinel */
3253 /* GFC_DECL_END is the sentinel, index starts at 0. */
3254 #define NUM_DECL GFC_DECL_END
3256 locus start, seen_at[NUM_DECL];
3263 gfc_clear_attr (¤t_attr);
3264 start = gfc_current_locus;
3269 /* See if we get all of the keywords up to the final double colon. */
3270 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3278 gfc_gobble_whitespace ();
3280 ch = gfc_next_ascii_char ();
3283 /* This is the successful exit condition for the loop. */
3284 if (gfc_next_ascii_char () == ':')
3289 gfc_gobble_whitespace ();
3290 switch (gfc_peek_ascii_char ())
3293 gfc_next_ascii_char ();
3294 switch (gfc_next_ascii_char ())
3297 if (match_string_p ("locatable"))
3299 /* Matched "allocatable". */
3300 d = DECL_ALLOCATABLE;
3305 if (match_string_p ("ynchronous"))
3307 /* Matched "asynchronous". */
3308 d = DECL_ASYNCHRONOUS;
3315 /* Try and match the bind(c). */
3316 m = gfc_match_bind_c (NULL, true);
3319 else if (m == MATCH_ERROR)
3324 gfc_next_ascii_char ();
3325 if ('o' != gfc_next_ascii_char ())
3327 switch (gfc_next_ascii_char ())
3330 if (match_string_p ("imension"))
3332 d = DECL_CODIMENSION;
3336 if (match_string_p ("tiguous"))
3338 d = DECL_CONTIGUOUS;
3345 if (match_string_p ("dimension"))
3350 if (match_string_p ("external"))
3355 if (match_string_p ("int"))
3357 ch = gfc_next_ascii_char ();
3360 if (match_string_p ("nt"))
3362 /* Matched "intent". */
3363 /* TODO: Call match_intent_spec from here. */
3364 if (gfc_match (" ( in out )") == MATCH_YES)
3366 else if (gfc_match (" ( in )") == MATCH_YES)
3368 else if (gfc_match (" ( out )") == MATCH_YES)
3374 if (match_string_p ("insic"))
3376 /* Matched "intrinsic". */
3384 if (match_string_p ("optional"))
3389 gfc_next_ascii_char ();
3390 switch (gfc_next_ascii_char ())
3393 if (match_string_p ("rameter"))
3395 /* Matched "parameter". */
3401 if (match_string_p ("inter"))
3403 /* Matched "pointer". */
3409 ch = gfc_next_ascii_char ();
3412 if (match_string_p ("vate"))
3414 /* Matched "private". */
3420 if (match_string_p ("tected"))
3422 /* Matched "protected". */
3429 if (match_string_p ("blic"))
3431 /* Matched "public". */
3439 if (match_string_p ("save"))
3444 if (match_string_p ("target"))
3449 gfc_next_ascii_char ();
3450 ch = gfc_next_ascii_char ();
3453 if (match_string_p ("lue"))
3455 /* Matched "value". */
3461 if (match_string_p ("latile"))
3463 /* Matched "volatile". */
3471 /* No double colon and no recognizable decl_type, so assume that
3472 we've been looking at something else the whole time. */
3479 /* Check to make sure any parens are paired up correctly. */
3480 if (gfc_match_parens () == MATCH_ERROR)
3487 seen_at[d] = gfc_current_locus;
3489 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3491 gfc_array_spec *as = NULL;
3493 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3494 d == DECL_CODIMENSION);
3496 if (current_as == NULL)
3498 else if (m == MATCH_YES)
3500 merge_array_spec (as, current_as, false);
3506 if (d == DECL_CODIMENSION)
3507 gfc_error ("Missing codimension specification at %C");
3509 gfc_error ("Missing dimension specification at %C");
3513 if (m == MATCH_ERROR)
3518 /* Since we've seen a double colon, we have to be looking at an
3519 attr-spec. This means that we can now issue errors. */
3520 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3525 case DECL_ALLOCATABLE:
3526 attr = "ALLOCATABLE";
3528 case DECL_ASYNCHRONOUS:
3529 attr = "ASYNCHRONOUS";
3531 case DECL_CODIMENSION:
3532 attr = "CODIMENSION";
3534 case DECL_CONTIGUOUS:
3535 attr = "CONTIGUOUS";
3537 case DECL_DIMENSION:
3544 attr = "INTENT (IN)";
3547 attr = "INTENT (OUT)";
3550 attr = "INTENT (IN OUT)";
3552 case DECL_INTRINSIC:
3558 case DECL_PARAMETER:
3564 case DECL_PROTECTED:
3579 case DECL_IS_BIND_C:
3589 attr = NULL; /* This shouldn't happen. */
3592 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3597 /* Now that we've dealt with duplicate attributes, add the attributes
3598 to the current attribute. */
3599 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3604 if (gfc_current_state () == COMP_DERIVED
3605 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3606 && d != DECL_POINTER && d != DECL_PRIVATE
3607 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3609 if (d == DECL_ALLOCATABLE)
3611 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3612 "attribute at %C in a TYPE definition")
3621 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3628 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3629 && gfc_current_state () != COMP_MODULE)
3631 if (d == DECL_PRIVATE)
3635 if (gfc_current_state () == COMP_DERIVED
3636 && gfc_state_stack->previous
3637 && gfc_state_stack->previous->state == COMP_MODULE)
3639 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3640 "at %L in a TYPE definition", attr,
3650 gfc_error ("%s attribute at %L is not allowed outside of the "
3651 "specification part of a module", attr, &seen_at[d]);
3659 case DECL_ALLOCATABLE:
3660 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
3663 case DECL_ASYNCHRONOUS:
3664 if (gfc_notify_std (GFC_STD_F2003,
3665 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3669 t = gfc_add_asynchronous (¤t_attr, NULL, &seen_at[d]);
3672 case DECL_CODIMENSION:
3673 t = gfc_add_codimension (¤t_attr, NULL, &seen_at[d]);
3676 case DECL_CONTIGUOUS:
3677 if (gfc_notify_std (GFC_STD_F2008,
3678 "Fortran 2008: CONTIGUOUS attribute at %C")
3682 t = gfc_add_contiguous (¤t_attr, NULL, &seen_at[d]);
3685 case DECL_DIMENSION:
3686 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
3690 t = gfc_add_external (¤t_attr, &seen_at[d]);
3694 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
3698 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
3702 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
3705 case DECL_INTRINSIC:
3706 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
3710 t = gfc_add_optional (¤t_attr, &seen_at[d]);
3713 case DECL_PARAMETER:
3714 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
3718 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
3721 case DECL_PROTECTED:
3722 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3724 gfc_error ("PROTECTED at %C only allowed in specification "
3725 "part of a module");
3730 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3735 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
3739 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
3744 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
3749 t = gfc_add_save (¤t_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3753 t = gfc_add_target (¤t_attr, &seen_at[d]);
3756 case DECL_IS_BIND_C:
3757 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
3761 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3766 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
3770 if (gfc_notify_std (GFC_STD_F2003,
3771 "Fortran 2003: VOLATILE attribute at %C")
3775 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
3779 gfc_internal_error ("match_attr_spec(): Bad attribute");
3789 /* Module variables implicitly have the SAVE attribute. */
3790 if (gfc_current_state () == COMP_MODULE && !current_attr.save)
3791 current_attr.save = SAVE_IMPLICIT;
3797 gfc_current_locus = start;
3798 gfc_free_array_spec (current_as);
3804 /* Set the binding label, dest_label, either with the binding label
3805 stored in the given gfc_typespec, ts, or if none was provided, it
3806 will be the symbol name in all lower case, as required by the draft
3807 (J3/04-007, section 15.4.1). If a binding label was given and
3808 there is more than one argument (num_idents), it is an error. */
3811 set_binding_label (char **dest_label, const char *sym_name, int num_idents)
3813 if (num_idents > 1 && has_name_equals)
3815 gfc_error ("Multiple identifiers provided with "
3816 "single NAME= specifier at %C");
3820 if (curr_binding_label)
3821 /* Binding label given; store in temp holder til have sym. */
3822 *dest_label = curr_binding_label;
3825 /* No binding label given, and the NAME= specifier did not exist,
3826 which means there was no NAME="". */
3827 if (sym_name != NULL && has_name_equals == 0)
3828 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
3835 /* Set the status of the given common block as being BIND(C) or not,
3836 depending on the given parameter, is_bind_c. */
3839 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3841 com_block->is_bind_c = is_bind_c;
3846 /* Verify that the given gfc_typespec is for a C interoperable type. */
3849 gfc_verify_c_interop (gfc_typespec *ts)
3851 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3852 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3853 ? SUCCESS : FAILURE;
3854 else if (ts->type == BT_CLASS)
3856 else if (ts->is_c_interop != 1)
3863 /* Verify that the variables of a given common block, which has been
3864 defined with the attribute specifier bind(c), to be of a C
3865 interoperable type. Errors will be reported here, if
3869 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3871 gfc_symbol *curr_sym = NULL;
3872 gfc_try retval = SUCCESS;
3874 curr_sym = com_block->head;
3876 /* Make sure we have at least one symbol. */
3877 if (curr_sym == NULL)
3880 /* Here we know we have a symbol, so we'll execute this loop
3884 /* The second to last param, 1, says this is in a common block. */
3885 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3886 curr_sym = curr_sym->common_next;
3887 } while (curr_sym != NULL);
3893 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3894 an appropriate error message is reported. */
3897 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3898 int is_in_common, gfc_common_head *com_block)
3900 bool bind_c_function = false;
3901 gfc_try retval = SUCCESS;
3903 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3904 bind_c_function = true;
3906 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3908 tmp_sym = tmp_sym->result;
3909 /* Make sure it wasn't an implicitly typed result. */
3910 if (tmp_sym->attr.implicit_type)
3912 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3913 "%L may not be C interoperable", tmp_sym->name,
3914 &tmp_sym->declared_at);
3915 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3916 /* Mark it as C interoperable to prevent duplicate warnings. */
3917 tmp_sym->ts.is_c_interop = 1;
3918 tmp_sym->attr.is_c_interop = 1;
3922 /* Here, we know we have the bind(c) attribute, so if we have
3923 enough type info, then verify that it's a C interop kind.
3924 The info could be in the symbol already, or possibly still in
3925 the given ts (current_ts), so look in both. */
3926 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3928 if (gfc_verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3930 /* See if we're dealing with a sym in a common block or not. */
3931 if (is_in_common == 1)
3933 gfc_warning ("Variable '%s' in common block '%s' at %L "
3934 "may not be a C interoperable "
3935 "kind though common block '%s' is BIND(C)",
3936 tmp_sym->name, com_block->name,
3937 &(tmp_sym->declared_at), com_block->name);
3941 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3942 gfc_error ("Type declaration '%s' at %L is not C "
3943 "interoperable but it is BIND(C)",
3944 tmp_sym->name, &(tmp_sym->declared_at));
3946 gfc_warning ("Variable '%s' at %L "
3947 "may not be a C interoperable "
3948 "kind but it is bind(c)",
3949 tmp_sym->name, &(tmp_sym->declared_at));
3953 /* Variables declared w/in a common block can't be bind(c)
3954 since there's no way for C to see these variables, so there's
3955 semantically no reason for the attribute. */
3956 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3958 gfc_error ("Variable '%s' in common block '%s' at "
3959 "%L cannot be declared with BIND(C) "
3960 "since it is not a global",
3961 tmp_sym->name, com_block->name,
3962 &(tmp_sym->declared_at));
3966 /* Scalar variables that are bind(c) can not have the pointer
3967 or allocatable attributes. */
3968 if (tmp_sym->attr.is_bind_c == 1)
3970 if (tmp_sym->attr.pointer == 1)
3972 gfc_error ("Variable '%s' at %L cannot have both the "
3973 "POINTER and BIND(C) attributes",
3974 tmp_sym->name, &(tmp_sym->declared_at));
3978 if (tmp_sym->attr.allocatable == 1)
3980 gfc_error ("Variable '%s' at %L cannot have both the "
3981 "ALLOCATABLE and BIND(C) attributes",
3982 tmp_sym->name, &(tmp_sym->declared_at));
3988 /* If it is a BIND(C) function, make sure the return value is a
3989 scalar value. The previous tests in this function made sure
3990 the type is interoperable. */
3991 if (bind_c_function && tmp_sym->as != NULL)
3992 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3993 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3995 /* BIND(C) functions can not return a character string. */
3996 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3997 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
3998 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
3999 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4000 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4001 "be a character string", tmp_sym->name,
4002 &(tmp_sym->declared_at));
4005 /* See if the symbol has been marked as private. If it has, make sure
4006 there is no binding label and warn the user if there is one. */
4007 if (tmp_sym->attr.access == ACCESS_PRIVATE
4008 && tmp_sym->binding_label)
4009 /* Use gfc_warning_now because we won't say that the symbol fails
4010 just because of this. */
4011 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
4012 "given the binding label '%s'", tmp_sym->name,
4013 &(tmp_sym->declared_at), tmp_sym->binding_label);
4019 /* Set the appropriate fields for a symbol that's been declared as
4020 BIND(C) (the is_bind_c flag and the binding label), and verify that
4021 the type is C interoperable. Errors are reported by the functions
4022 used to set/test these fields. */
4025 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4027 gfc_try retval = SUCCESS;
4029 /* TODO: Do we need to make sure the vars aren't marked private? */
4031 /* Set the is_bind_c bit in symbol_attribute. */
4032 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4034 if (set_binding_label (&tmp_sym->binding_label, tmp_sym->name,
4035 num_idents) != SUCCESS)
4042 /* Set the fields marking the given common block as BIND(C), including
4043 a binding label, and report any errors encountered. */
4046 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4048 gfc_try retval = SUCCESS;
4050 /* destLabel, common name, typespec (which may have binding label). */
4051 if (set_binding_label (&com_block->binding_label, com_block->name,
4056 /* Set the given common block (com_block) to being bind(c) (1). */
4057 set_com_block_bind_c (com_block, 1);
4063 /* Retrieve the list of one or more identifiers that the given bind(c)
4064 attribute applies to. */
4067 get_bind_c_idents (void)
4069 char name[GFC_MAX_SYMBOL_LEN + 1];
4071 gfc_symbol *tmp_sym = NULL;
4073 gfc_common_head *com_block = NULL;
4075 if (gfc_match_name (name) == MATCH_YES)
4077 found_id = MATCH_YES;
4078 gfc_get_ha_symbol (name, &tmp_sym);
4080 else if (match_common_name (name) == MATCH_YES)
4082 found_id = MATCH_YES;
4083 com_block = gfc_get_common (name, 0);
4087 gfc_error ("Need either entity or common block name for "
4088 "attribute specification statement at %C");
4092 /* Save the current identifier and look for more. */
4095 /* Increment the number of identifiers found for this spec stmt. */
4098 /* Make sure we have a sym or com block, and verify that it can
4099 be bind(c). Set the appropriate field(s) and look for more