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");
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 if (check_function_name (name) == FAILURE)
1958 /* We allow old-style initializations of the form
1959 integer i /2/, j(4) /3*3, 1/
1960 (if no colon has been seen). These are different from data
1961 statements in that initializers are only allowed to apply to the
1962 variable immediately preceding, i.e.
1964 is not allowed. Therefore we have to do some work manually, that
1965 could otherwise be left to the matchers for DATA statements. */
1967 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1969 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1970 "initialization at %C") == FAILURE)
1973 return match_old_style_init (name);
1976 /* The double colon must be present in order to have initializers.
1977 Otherwise the statement is ambiguous with an assignment statement. */
1980 if (gfc_match (" =>") == MATCH_YES)
1982 if (!current_attr.pointer)
1984 gfc_error ("Initialization at %C isn't for a pointer variable");
1989 m = match_pointer_init (&initializer, 0);
1993 else if (gfc_match_char ('=') == MATCH_YES)
1995 if (current_attr.pointer)
1997 gfc_error ("Pointer initialization at %C requires '=>', "
2003 m = gfc_match_init_expr (&initializer);
2006 gfc_error ("Expected an initialization expression at %C");
2010 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2011 && gfc_state_stack->state != COMP_DERIVED)
2013 gfc_error ("Initialization of variable at %C is not allowed in "
2014 "a PURE procedure");
2023 if (initializer != NULL && current_attr.allocatable
2024 && gfc_current_state () == COMP_DERIVED)
2026 gfc_error ("Initialization of allocatable component at %C is not "
2032 /* Add the initializer. Note that it is fine if initializer is
2033 NULL here, because we sometimes also need to check if a
2034 declaration *must* have an initialization expression. */
2035 if (gfc_current_state () != COMP_DERIVED)
2036 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2039 if (current_ts.type == BT_DERIVED
2040 && !current_attr.pointer && !initializer)
2041 initializer = gfc_default_initializer (¤t_ts);
2042 t = build_struct (name, cl, &initializer, &as);
2045 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
2048 /* Free stuff up and return. */
2049 gfc_free_expr (initializer);
2050 gfc_free_array_spec (as);
2056 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2057 This assumes that the byte size is equal to the kind number for
2058 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2061 gfc_match_old_kind_spec (gfc_typespec *ts)
2066 if (gfc_match_char ('*') != MATCH_YES)
2069 m = gfc_match_small_literal_int (&ts->kind, NULL);
2073 original_kind = ts->kind;
2075 /* Massage the kind numbers for complex types. */
2076 if (ts->type == BT_COMPLEX)
2080 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2081 gfc_basic_typename (ts->type), original_kind);
2088 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2091 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2095 if (gfc_option.flag_real4_kind == 8)
2097 if (gfc_option.flag_real4_kind == 10)
2099 if (gfc_option.flag_real4_kind == 16)
2105 if (gfc_option.flag_real8_kind == 4)
2107 if (gfc_option.flag_real8_kind == 10)
2109 if (gfc_option.flag_real8_kind == 16)
2114 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2116 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2117 gfc_basic_typename (ts->type), original_kind);
2121 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2122 gfc_basic_typename (ts->type), original_kind) == FAILURE)
2129 /* Match a kind specification. Since kinds are generally optional, we
2130 usually return MATCH_NO if something goes wrong. If a "kind="
2131 string is found, then we know we have an error. */
2134 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2146 where = loc = gfc_current_locus;
2151 if (gfc_match_char ('(') == MATCH_NO)
2154 /* Also gobbles optional text. */
2155 if (gfc_match (" kind = ") == MATCH_YES)
2158 loc = gfc_current_locus;
2161 n = gfc_match_init_expr (&e);
2165 if (gfc_matching_function)
2167 /* The function kind expression might include use associated or
2168 imported parameters and try again after the specification
2170 if (gfc_match_char (')') != MATCH_YES)
2172 gfc_error ("Missing right parenthesis at %C");
2178 gfc_undo_symbols ();
2183 /* ....or else, the match is real. */
2185 gfc_error ("Expected initialization expression at %C");
2193 gfc_error ("Expected scalar initialization expression at %C");
2198 msg = gfc_extract_int (e, &ts->kind);
2207 /* Before throwing away the expression, let's see if we had a
2208 C interoperable kind (and store the fact). */
2209 if (e->ts.is_c_interop == 1)
2211 /* Mark this as c interoperable if being declared with one
2212 of the named constants from iso_c_binding. */
2213 ts->is_c_interop = e->ts.is_iso_c;
2214 ts->f90_type = e->ts.f90_type;
2220 /* Ignore errors to this point, if we've gotten here. This means
2221 we ignore the m=MATCH_ERROR from above. */
2222 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2224 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2225 gfc_basic_typename (ts->type));
2226 gfc_current_locus = where;
2230 /* Warn if, e.g., c_int is used for a REAL variable, but not
2231 if, e.g., c_double is used for COMPLEX as the standard
2232 explicitly says that the kind type parameter for complex and real
2233 variable is the same, i.e. c_float == c_float_complex. */
2234 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2235 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2236 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2237 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2238 "is %s", gfc_basic_typename (ts->f90_type), &where,
2239 gfc_basic_typename (ts->type));
2241 gfc_gobble_whitespace ();
2242 if ((c = gfc_next_ascii_char ()) != ')'
2243 && (ts->type != BT_CHARACTER || c != ','))
2245 if (ts->type == BT_CHARACTER)
2246 gfc_error ("Missing right parenthesis or comma at %C");
2248 gfc_error ("Missing right parenthesis at %C");
2252 /* All tests passed. */
2255 if(m == MATCH_ERROR)
2256 gfc_current_locus = where;
2258 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2261 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2265 if (gfc_option.flag_real4_kind == 8)
2267 if (gfc_option.flag_real4_kind == 10)
2269 if (gfc_option.flag_real4_kind == 16)
2275 if (gfc_option.flag_real8_kind == 4)
2277 if (gfc_option.flag_real8_kind == 10)
2279 if (gfc_option.flag_real8_kind == 16)
2284 /* Return what we know from the test(s). */
2289 gfc_current_locus = where;
2295 match_char_kind (int * kind, int * is_iso_c)
2304 where = gfc_current_locus;
2306 n = gfc_match_init_expr (&e);
2308 if (n != MATCH_YES && gfc_matching_function)
2310 /* The expression might include use-associated or imported
2311 parameters and try again after the specification
2314 gfc_undo_symbols ();
2319 gfc_error ("Expected initialization expression at %C");
2325 gfc_error ("Expected scalar initialization expression at %C");
2330 msg = gfc_extract_int (e, kind);
2331 *is_iso_c = e->ts.is_iso_c;
2341 /* Ignore errors to this point, if we've gotten here. This means
2342 we ignore the m=MATCH_ERROR from above. */
2343 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2345 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2349 /* All tests passed. */
2352 if (m == MATCH_ERROR)
2353 gfc_current_locus = where;
2355 /* Return what we know from the test(s). */
2360 gfc_current_locus = where;
2365 /* Match the various kind/length specifications in a CHARACTER
2366 declaration. We don't return MATCH_NO. */
2369 gfc_match_char_spec (gfc_typespec *ts)
2371 int kind, seen_length, is_iso_c;
2383 /* Try the old-style specification first. */
2384 old_char_selector = 0;
2386 m = match_char_length (&len, &deferred);
2390 old_char_selector = 1;
2395 m = gfc_match_char ('(');
2398 m = MATCH_YES; /* Character without length is a single char. */
2402 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2403 if (gfc_match (" kind =") == MATCH_YES)
2405 m = match_char_kind (&kind, &is_iso_c);
2407 if (m == MATCH_ERROR)
2412 if (gfc_match (" , len =") == MATCH_NO)
2415 m = char_len_param_value (&len, &deferred);
2418 if (m == MATCH_ERROR)
2425 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2426 if (gfc_match (" len =") == MATCH_YES)
2428 m = char_len_param_value (&len, &deferred);
2431 if (m == MATCH_ERROR)
2435 if (gfc_match_char (')') == MATCH_YES)
2438 if (gfc_match (" , kind =") != MATCH_YES)
2441 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2447 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2448 m = char_len_param_value (&len, &deferred);
2451 if (m == MATCH_ERROR)
2455 m = gfc_match_char (')');
2459 if (gfc_match_char (',') != MATCH_YES)
2462 gfc_match (" kind ="); /* Gobble optional text. */
2464 m = match_char_kind (&kind, &is_iso_c);
2465 if (m == MATCH_ERROR)
2471 /* Require a right-paren at this point. */
2472 m = gfc_match_char (')');
2477 gfc_error ("Syntax error in CHARACTER declaration at %C");
2479 gfc_free_expr (len);
2483 /* Deal with character functions after USE and IMPORT statements. */
2484 if (gfc_matching_function)
2486 gfc_free_expr (len);
2487 gfc_undo_symbols ();
2493 gfc_free_expr (len);
2497 /* Do some final massaging of the length values. */
2498 cl = gfc_new_charlen (gfc_current_ns, NULL);
2500 if (seen_length == 0)
2501 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2506 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2507 ts->deferred = deferred;
2509 /* We have to know if it was a c interoperable kind so we can
2510 do accurate type checking of bind(c) procs, etc. */
2512 /* Mark this as c interoperable if being declared with one
2513 of the named constants from iso_c_binding. */
2514 ts->is_c_interop = is_iso_c;
2515 else if (len != NULL)
2516 /* Here, we might have parsed something such as: character(c_char)
2517 In this case, the parsing code above grabs the c_char when
2518 looking for the length (line 1690, roughly). it's the last
2519 testcase for parsing the kind params of a character variable.
2520 However, it's not actually the length. this seems like it
2522 To see if the user used a C interop kind, test the expr
2523 of the so called length, and see if it's C interoperable. */
2524 ts->is_c_interop = len->ts.is_iso_c;
2530 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2531 structure to the matched specification. This is necessary for FUNCTION and
2532 IMPLICIT statements.
2534 If implicit_flag is nonzero, then we don't check for the optional
2535 kind specification. Not doing so is needed for matching an IMPLICIT
2536 statement correctly. */
2539 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2541 char name[GFC_MAX_SYMBOL_LEN + 1];
2542 gfc_symbol *sym, *dt_sym;
2545 bool seen_deferred_kind, matched_type;
2546 const char *dt_name;
2548 /* A belt and braces check that the typespec is correctly being treated
2549 as a deferred characteristic association. */
2550 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2551 && (gfc_current_block ()->result->ts.kind == -1)
2552 && (ts->kind == -1);
2554 if (seen_deferred_kind)
2557 /* Clear the current binding label, in case one is given. */
2558 curr_binding_label = NULL;
2560 if (gfc_match (" byte") == MATCH_YES)
2562 if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2566 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2568 gfc_error ("BYTE type used at %C "
2569 "is not available on the target machine");
2573 ts->type = BT_INTEGER;
2579 m = gfc_match (" type ( %n", name);
2580 matched_type = (m == MATCH_YES);
2582 if ((matched_type && strcmp ("integer", name) == 0)
2583 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2585 ts->type = BT_INTEGER;
2586 ts->kind = gfc_default_integer_kind;
2590 if ((matched_type && strcmp ("character", name) == 0)
2591 || (!matched_type && gfc_match (" character") == MATCH_YES))
2594 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2595 "intrinsic-type-spec at %C") == FAILURE)
2598 ts->type = BT_CHARACTER;
2599 if (implicit_flag == 0)
2600 m = gfc_match_char_spec (ts);
2604 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2610 if ((matched_type && strcmp ("real", name) == 0)
2611 || (!matched_type && gfc_match (" real") == MATCH_YES))
2614 ts->kind = gfc_default_real_kind;
2619 && (strcmp ("doubleprecision", name) == 0
2620 || (strcmp ("double", name) == 0
2621 && gfc_match (" precision") == MATCH_YES)))
2622 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2625 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2626 "intrinsic-type-spec at %C") == FAILURE)
2628 if (matched_type && gfc_match_char (')') != MATCH_YES)
2632 ts->kind = gfc_default_double_kind;
2636 if ((matched_type && strcmp ("complex", name) == 0)
2637 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2639 ts->type = BT_COMPLEX;
2640 ts->kind = gfc_default_complex_kind;
2645 && (strcmp ("doublecomplex", name) == 0
2646 || (strcmp ("double", name) == 0
2647 && gfc_match (" complex") == MATCH_YES)))
2648 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2650 if (gfc_notify_std (GFC_STD_GNU, "Extension: DOUBLE COMPLEX at %C")
2655 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2656 "intrinsic-type-spec at %C") == FAILURE)
2659 if (matched_type && gfc_match_char (')') != MATCH_YES)
2662 ts->type = BT_COMPLEX;
2663 ts->kind = gfc_default_double_kind;
2667 if ((matched_type && strcmp ("logical", name) == 0)
2668 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2670 ts->type = BT_LOGICAL;
2671 ts->kind = gfc_default_logical_kind;
2676 m = gfc_match_char (')');
2679 ts->type = BT_DERIVED;
2682 /* Match CLASS declarations. */
2683 m = gfc_match (" class ( * )");
2684 if (m == MATCH_ERROR)
2686 else if (m == MATCH_YES)
2688 gfc_fatal_error ("Unlimited polymorphism at %C not yet supported");
2692 m = gfc_match (" class ( %n )", name);
2695 ts->type = BT_CLASS;
2697 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2702 /* Defer association of the derived type until the end of the
2703 specification block. However, if the derived type can be
2704 found, add it to the typespec. */
2705 if (gfc_matching_function)
2707 ts->u.derived = NULL;
2708 if (gfc_current_state () != COMP_INTERFACE
2709 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2711 sym = gfc_find_dt_in_generic (sym);
2712 ts->u.derived = sym;
2717 /* Search for the name but allow the components to be defined later. If
2718 type = -1, this typespec has been seen in a function declaration but
2719 the type could not be accessed at that point. The actual derived type is
2720 stored in a symtree with the first letter of the name captialized; the
2721 symtree with the all lower-case name contains the associated
2722 generic function. */
2723 dt_name = gfc_get_string ("%c%s",
2724 (char) TOUPPER ((unsigned char) name[0]),
2725 (const char*)&name[1]);
2730 gfc_get_ha_symbol (name, &sym);
2731 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2733 gfc_error ("Type name '%s' at %C is ambiguous", name);
2736 if (sym->generic && !dt_sym)
2737 dt_sym = gfc_find_dt_in_generic (sym);
2739 else if (ts->kind == -1)
2741 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2742 || gfc_current_ns->has_import_set;
2743 gfc_find_symbol (name, NULL, iface, &sym);
2744 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2746 gfc_error ("Type name '%s' at %C is ambiguous", name);
2749 if (sym && sym->generic && !dt_sym)
2750 dt_sym = gfc_find_dt_in_generic (sym);
2757 if ((sym->attr.flavor != FL_UNKNOWN
2758 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2759 || sym->attr.subroutine)
2761 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2762 "entity at %L, which has the same name", name,
2767 gfc_set_sym_referenced (sym);
2768 if (!sym->attr.generic
2769 && gfc_add_generic (&sym->attr, sym->name, NULL) == FAILURE)
2772 if (!sym->attr.function
2773 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2778 gfc_interface *intr, *head;
2780 /* Use upper case to save the actual derived-type symbol. */
2781 gfc_get_symbol (dt_name, NULL, &dt_sym);
2782 dt_sym->name = gfc_get_string (sym->name);
2783 head = sym->generic;
2784 intr = gfc_get_interface ();
2786 intr->where = gfc_current_locus;
2788 sym->generic = intr;
2789 sym->attr.if_source = IFSRC_DECL;
2792 gfc_set_sym_referenced (dt_sym);
2794 if (dt_sym->attr.flavor != FL_DERIVED
2795 && gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL)
2799 ts->u.derived = dt_sym;
2805 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2806 "intrinsic-type-spec at %C") == FAILURE)
2809 /* For all types except double, derived and character, look for an
2810 optional kind specifier. MATCH_NO is actually OK at this point. */
2811 if (implicit_flag == 1)
2813 if (matched_type && gfc_match_char (')') != MATCH_YES)
2819 if (gfc_current_form == FORM_FREE)
2821 c = gfc_peek_ascii_char ();
2822 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2823 && c != ':' && c != ',')
2825 if (matched_type && c == ')')
2827 gfc_next_ascii_char ();
2834 m = gfc_match_kind_spec (ts, false);
2835 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2836 m = gfc_match_old_kind_spec (ts);
2838 if (matched_type && gfc_match_char (')') != MATCH_YES)
2841 /* Defer association of the KIND expression of function results
2842 until after USE and IMPORT statements. */
2843 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2844 || gfc_matching_function)
2848 m = MATCH_YES; /* No kind specifier found. */
2854 /* Match an IMPLICIT NONE statement. Actually, this statement is
2855 already matched in parse.c, or we would not end up here in the
2856 first place. So the only thing we need to check, is if there is
2857 trailing garbage. If not, the match is successful. */
2860 gfc_match_implicit_none (void)
2862 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2866 /* Match the letter range(s) of an IMPLICIT statement. */
2869 match_implicit_range (void)
2875 cur_loc = gfc_current_locus;
2877 gfc_gobble_whitespace ();
2878 c = gfc_next_ascii_char ();
2881 gfc_error ("Missing character range in IMPLICIT at %C");
2888 gfc_gobble_whitespace ();
2889 c1 = gfc_next_ascii_char ();
2893 gfc_gobble_whitespace ();
2894 c = gfc_next_ascii_char ();
2899 inner = 0; /* Fall through. */
2906 gfc_gobble_whitespace ();
2907 c2 = gfc_next_ascii_char ();
2911 gfc_gobble_whitespace ();
2912 c = gfc_next_ascii_char ();
2914 if ((c != ',') && (c != ')'))
2927 gfc_error ("Letters must be in alphabetic order in "
2928 "IMPLICIT statement at %C");
2932 /* See if we can add the newly matched range to the pending
2933 implicits from this IMPLICIT statement. We do not check for
2934 conflicts with whatever earlier IMPLICIT statements may have
2935 set. This is done when we've successfully finished matching
2937 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2944 gfc_syntax_error (ST_IMPLICIT);
2946 gfc_current_locus = cur_loc;
2951 /* Match an IMPLICIT statement, storing the types for
2952 gfc_set_implicit() if the statement is accepted by the parser.
2953 There is a strange looking, but legal syntactic construction
2954 possible. It looks like:
2956 IMPLICIT INTEGER (a-b) (c-d)
2958 This is legal if "a-b" is a constant expression that happens to
2959 equal one of the legal kinds for integers. The real problem
2960 happens with an implicit specification that looks like:
2962 IMPLICIT INTEGER (a-b)
2964 In this case, a typespec matcher that is "greedy" (as most of the
2965 matchers are) gobbles the character range as a kindspec, leaving
2966 nothing left. We therefore have to go a bit more slowly in the
2967 matching process by inhibiting the kindspec checking during
2968 typespec matching and checking for a kind later. */
2971 gfc_match_implicit (void)
2980 /* We don't allow empty implicit statements. */
2981 if (gfc_match_eos () == MATCH_YES)
2983 gfc_error ("Empty IMPLICIT statement at %C");
2989 /* First cleanup. */
2990 gfc_clear_new_implicit ();
2992 /* A basic type is mandatory here. */
2993 m = gfc_match_decl_type_spec (&ts, 1);
2994 if (m == MATCH_ERROR)
2999 cur_loc = gfc_current_locus;
3000 m = match_implicit_range ();
3004 /* We may have <TYPE> (<RANGE>). */
3005 gfc_gobble_whitespace ();
3006 c = gfc_next_ascii_char ();
3007 if ((c == '\n') || (c == ','))
3009 /* Check for CHARACTER with no length parameter. */
3010 if (ts.type == BT_CHARACTER && !ts.u.cl)
3012 ts.kind = gfc_default_character_kind;
3013 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3014 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3018 /* Record the Successful match. */
3019 if (gfc_merge_new_implicit (&ts) != SUCCESS)
3024 gfc_current_locus = cur_loc;
3027 /* Discard the (incorrectly) matched range. */
3028 gfc_clear_new_implicit ();
3030 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3031 if (ts.type == BT_CHARACTER)
3032 m = gfc_match_char_spec (&ts);
3035 m = gfc_match_kind_spec (&ts, false);
3038 m = gfc_match_old_kind_spec (&ts);
3039 if (m == MATCH_ERROR)
3045 if (m == MATCH_ERROR)
3048 m = match_implicit_range ();
3049 if (m == MATCH_ERROR)
3054 gfc_gobble_whitespace ();
3055 c = gfc_next_ascii_char ();
3056 if ((c != '\n') && (c != ','))
3059 if (gfc_merge_new_implicit (&ts) != SUCCESS)
3067 gfc_syntax_error (ST_IMPLICIT);
3075 gfc_match_import (void)
3077 char name[GFC_MAX_SYMBOL_LEN + 1];
3082 if (gfc_current_ns->proc_name == NULL
3083 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3085 gfc_error ("IMPORT statement at %C only permitted in "
3086 "an INTERFACE body");
3090 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
3094 if (gfc_match_eos () == MATCH_YES)
3096 /* All host variables should be imported. */
3097 gfc_current_ns->has_import_set = 1;
3101 if (gfc_match (" ::") == MATCH_YES)
3103 if (gfc_match_eos () == MATCH_YES)
3105 gfc_error ("Expecting list of named entities at %C");
3113 m = gfc_match (" %n", name);
3117 if (gfc_current_ns->parent != NULL
3118 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3120 gfc_error ("Type name '%s' at %C is ambiguous", name);
3123 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3124 && gfc_find_symbol (name,
3125 gfc_current_ns->proc_name->ns->parent,
3128 gfc_error ("Type name '%s' at %C is ambiguous", name);
3134 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3135 "at %C - does not exist.", name);
3139 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3141 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3146 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3149 sym->attr.imported = 1;
3151 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3153 /* The actual derived type is stored in a symtree with the first
3154 letter of the name captialized; the symtree with the all
3155 lower-case name contains the associated generic function. */
3156 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3157 gfc_get_string ("%c%s",
3158 (char) TOUPPER ((unsigned char) name[0]),
3162 sym->attr.imported = 1;
3175 if (gfc_match_eos () == MATCH_YES)
3177 if (gfc_match_char (',') != MATCH_YES)
3184 gfc_error ("Syntax error in IMPORT statement at %C");
3189 /* A minimal implementation of gfc_match without whitespace, escape
3190 characters or variable arguments. Returns true if the next
3191 characters match the TARGET template exactly. */
3194 match_string_p (const char *target)
3198 for (p = target; *p; p++)
3199 if ((char) gfc_next_ascii_char () != *p)
3204 /* Matches an attribute specification including array specs. If
3205 successful, leaves the variables current_attr and current_as
3206 holding the specification. Also sets the colon_seen variable for
3207 later use by matchers associated with initializations.
3209 This subroutine is a little tricky in the sense that we don't know
3210 if we really have an attr-spec until we hit the double colon.
3211 Until that time, we can only return MATCH_NO. This forces us to
3212 check for duplicate specification at this level. */
3215 match_attr_spec (void)
3217 /* Modifiers that can exist in a type statement. */
3219 { GFC_DECL_BEGIN = 0,
3220 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3221 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3222 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3223 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3224 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3225 DECL_NONE, GFC_DECL_END /* Sentinel */
3229 /* GFC_DECL_END is the sentinel, index starts at 0. */
3230 #define NUM_DECL GFC_DECL_END
3232 locus start, seen_at[NUM_DECL];
3239 gfc_clear_attr (¤t_attr);
3240 start = gfc_current_locus;
3245 /* See if we get all of the keywords up to the final double colon. */
3246 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3254 gfc_gobble_whitespace ();
3256 ch = gfc_next_ascii_char ();
3259 /* This is the successful exit condition for the loop. */
3260 if (gfc_next_ascii_char () == ':')
3265 gfc_gobble_whitespace ();
3266 switch (gfc_peek_ascii_char ())
3269 gfc_next_ascii_char ();
3270 switch (gfc_next_ascii_char ())
3273 if (match_string_p ("locatable"))
3275 /* Matched "allocatable". */
3276 d = DECL_ALLOCATABLE;
3281 if (match_string_p ("ynchronous"))
3283 /* Matched "asynchronous". */
3284 d = DECL_ASYNCHRONOUS;
3291 /* Try and match the bind(c). */
3292 m = gfc_match_bind_c (NULL, true);
3295 else if (m == MATCH_ERROR)
3300 gfc_next_ascii_char ();
3301 if ('o' != gfc_next_ascii_char ())
3303 switch (gfc_next_ascii_char ())
3306 if (match_string_p ("imension"))
3308 d = DECL_CODIMENSION;
3312 if (match_string_p ("tiguous"))
3314 d = DECL_CONTIGUOUS;
3321 if (match_string_p ("dimension"))
3326 if (match_string_p ("external"))
3331 if (match_string_p ("int"))
3333 ch = gfc_next_ascii_char ();
3336 if (match_string_p ("nt"))
3338 /* Matched "intent". */
3339 /* TODO: Call match_intent_spec from here. */
3340 if (gfc_match (" ( in out )") == MATCH_YES)
3342 else if (gfc_match (" ( in )") == MATCH_YES)
3344 else if (gfc_match (" ( out )") == MATCH_YES)
3350 if (match_string_p ("insic"))
3352 /* Matched "intrinsic". */
3360 if (match_string_p ("optional"))
3365 gfc_next_ascii_char ();
3366 switch (gfc_next_ascii_char ())
3369 if (match_string_p ("rameter"))
3371 /* Matched "parameter". */
3377 if (match_string_p ("inter"))
3379 /* Matched "pointer". */
3385 ch = gfc_next_ascii_char ();
3388 if (match_string_p ("vate"))
3390 /* Matched "private". */
3396 if (match_string_p ("tected"))
3398 /* Matched "protected". */
3405 if (match_string_p ("blic"))
3407 /* Matched "public". */
3415 if (match_string_p ("save"))
3420 if (match_string_p ("target"))
3425 gfc_next_ascii_char ();
3426 ch = gfc_next_ascii_char ();
3429 if (match_string_p ("lue"))
3431 /* Matched "value". */
3437 if (match_string_p ("latile"))
3439 /* Matched "volatile". */
3447 /* No double colon and no recognizable decl_type, so assume that
3448 we've been looking at something else the whole time. */
3455 /* Check to make sure any parens are paired up correctly. */
3456 if (gfc_match_parens () == MATCH_ERROR)
3463 seen_at[d] = gfc_current_locus;
3465 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3467 gfc_array_spec *as = NULL;
3469 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3470 d == DECL_CODIMENSION);
3472 if (current_as == NULL)
3474 else if (m == MATCH_YES)
3476 merge_array_spec (as, current_as, false);
3482 if (d == DECL_CODIMENSION)
3483 gfc_error ("Missing codimension specification at %C");
3485 gfc_error ("Missing dimension specification at %C");
3489 if (m == MATCH_ERROR)
3494 /* Since we've seen a double colon, we have to be looking at an
3495 attr-spec. This means that we can now issue errors. */
3496 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3501 case DECL_ALLOCATABLE:
3502 attr = "ALLOCATABLE";
3504 case DECL_ASYNCHRONOUS:
3505 attr = "ASYNCHRONOUS";
3507 case DECL_CODIMENSION:
3508 attr = "CODIMENSION";
3510 case DECL_CONTIGUOUS:
3511 attr = "CONTIGUOUS";
3513 case DECL_DIMENSION:
3520 attr = "INTENT (IN)";
3523 attr = "INTENT (OUT)";
3526 attr = "INTENT (IN OUT)";
3528 case DECL_INTRINSIC:
3534 case DECL_PARAMETER:
3540 case DECL_PROTECTED:
3555 case DECL_IS_BIND_C:
3565 attr = NULL; /* This shouldn't happen. */
3568 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3573 /* Now that we've dealt with duplicate attributes, add the attributes
3574 to the current attribute. */
3575 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3580 if (gfc_current_state () == COMP_DERIVED
3581 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3582 && d != DECL_POINTER && d != DECL_PRIVATE
3583 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3585 if (d == DECL_ALLOCATABLE)
3587 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3588 "attribute at %C in a TYPE definition")
3597 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3604 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3605 && gfc_current_state () != COMP_MODULE)
3607 if (d == DECL_PRIVATE)
3611 if (gfc_current_state () == COMP_DERIVED
3612 && gfc_state_stack->previous
3613 && gfc_state_stack->previous->state == COMP_MODULE)
3615 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3616 "at %L in a TYPE definition", attr,
3626 gfc_error ("%s attribute at %L is not allowed outside of the "
3627 "specification part of a module", attr, &seen_at[d]);
3635 case DECL_ALLOCATABLE:
3636 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
3639 case DECL_ASYNCHRONOUS:
3640 if (gfc_notify_std (GFC_STD_F2003,
3641 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3645 t = gfc_add_asynchronous (¤t_attr, NULL, &seen_at[d]);
3648 case DECL_CODIMENSION:
3649 t = gfc_add_codimension (¤t_attr, NULL, &seen_at[d]);
3652 case DECL_CONTIGUOUS:
3653 if (gfc_notify_std (GFC_STD_F2008,
3654 "Fortran 2008: CONTIGUOUS attribute at %C")
3658 t = gfc_add_contiguous (¤t_attr, NULL, &seen_at[d]);
3661 case DECL_DIMENSION:
3662 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
3666 t = gfc_add_external (¤t_attr, &seen_at[d]);
3670 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
3674 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
3678 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
3681 case DECL_INTRINSIC:
3682 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
3686 t = gfc_add_optional (¤t_attr, &seen_at[d]);
3689 case DECL_PARAMETER:
3690 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
3694 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
3697 case DECL_PROTECTED:
3698 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3700 gfc_error ("PROTECTED at %C only allowed in specification "
3701 "part of a module");
3706 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3711 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
3715 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
3720 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
3725 t = gfc_add_save (¤t_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3729 t = gfc_add_target (¤t_attr, &seen_at[d]);
3732 case DECL_IS_BIND_C:
3733 t = gfc_add_is_bind_c(¤t_attr, NULL, &seen_at[d], 0);
3737 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3742 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
3746 if (gfc_notify_std (GFC_STD_F2003,
3747 "Fortran 2003: VOLATILE attribute at %C")
3751 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
3755 gfc_internal_error ("match_attr_spec(): Bad attribute");
3765 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3766 if (gfc_current_state () == COMP_MODULE && !current_attr.save
3767 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
3768 current_attr.save = SAVE_IMPLICIT;
3774 gfc_current_locus = start;
3775 gfc_free_array_spec (current_as);
3781 /* Set the binding label, dest_label, either with the binding label
3782 stored in the given gfc_typespec, ts, or if none was provided, it
3783 will be the symbol name in all lower case, as required by the draft
3784 (J3/04-007, section 15.4.1). If a binding label was given and
3785 there is more than one argument (num_idents), it is an error. */
3788 set_binding_label (const char **dest_label, const char *sym_name,
3791 if (num_idents > 1 && has_name_equals)
3793 gfc_error ("Multiple identifiers provided with "
3794 "single NAME= specifier at %C");
3798 if (curr_binding_label)
3799 /* Binding label given; store in temp holder til have sym. */
3800 *dest_label = curr_binding_label;
3803 /* No binding label given, and the NAME= specifier did not exist,
3804 which means there was no NAME="". */
3805 if (sym_name != NULL && has_name_equals == 0)
3806 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
3813 /* Set the status of the given common block as being BIND(C) or not,
3814 depending on the given parameter, is_bind_c. */
3817 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3819 com_block->is_bind_c = is_bind_c;
3824 /* Verify that the given gfc_typespec is for a C interoperable type. */
3827 gfc_verify_c_interop (gfc_typespec *ts)
3829 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3830 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3831 ? SUCCESS : FAILURE;
3832 else if (ts->type == BT_CLASS)
3834 else if (ts->is_c_interop != 1)
3841 /* Verify that the variables of a given common block, which has been
3842 defined with the attribute specifier bind(c), to be of a C
3843 interoperable type. Errors will be reported here, if
3847 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3849 gfc_symbol *curr_sym = NULL;
3850 gfc_try retval = SUCCESS;
3852 curr_sym = com_block->head;
3854 /* Make sure we have at least one symbol. */
3855 if (curr_sym == NULL)
3858 /* Here we know we have a symbol, so we'll execute this loop
3862 /* The second to last param, 1, says this is in a common block. */
3863 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3864 curr_sym = curr_sym->common_next;
3865 } while (curr_sym != NULL);
3871 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3872 an appropriate error message is reported. */
3875 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3876 int is_in_common, gfc_common_head *com_block)
3878 bool bind_c_function = false;
3879 gfc_try retval = SUCCESS;
3881 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3882 bind_c_function = true;
3884 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3886 tmp_sym = tmp_sym->result;
3887 /* Make sure it wasn't an implicitly typed result. */
3888 if (tmp_sym->attr.implicit_type)
3890 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3891 "%L may not be C interoperable", tmp_sym->name,
3892 &tmp_sym->declared_at);
3893 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3894 /* Mark it as C interoperable to prevent duplicate warnings. */
3895 tmp_sym->ts.is_c_interop = 1;
3896 tmp_sym->attr.is_c_interop = 1;
3900 /* Here, we know we have the bind(c) attribute, so if we have
3901 enough type info, then verify that it's a C interop kind.
3902 The info could be in the symbol already, or possibly still in
3903 the given ts (current_ts), so look in both. */
3904 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3906 if (gfc_verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3908 /* See if we're dealing with a sym in a common block or not. */
3909 if (is_in_common == 1)
3911 gfc_warning ("Variable '%s' in common block '%s' at %L "
3912 "may not be a C interoperable "
3913 "kind though common block '%s' is BIND(C)",
3914 tmp_sym->name, com_block->name,
3915 &(tmp_sym->declared_at), com_block->name);
3919 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3920 gfc_error ("Type declaration '%s' at %L is not C "
3921 "interoperable but it is BIND(C)",
3922 tmp_sym->name, &(tmp_sym->declared_at));
3924 gfc_warning ("Variable '%s' at %L "
3925 "may not be a C interoperable "
3926 "kind but it is bind(c)",
3927 tmp_sym->name, &(tmp_sym->declared_at));
3931 /* Variables declared w/in a common block can't be bind(c)
3932 since there's no way for C to see these variables, so there's
3933 semantically no reason for the attribute. */
3934 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3936 gfc_error ("Variable '%s' in common block '%s' at "
3937 "%L cannot be declared with BIND(C) "
3938 "since it is not a global",
3939 tmp_sym->name, com_block->name,
3940 &(tmp_sym->declared_at));
3944 /* Scalar variables that are bind(c) can not have the pointer
3945 or allocatable attributes. */
3946 if (tmp_sym->attr.is_bind_c == 1)
3948 if (tmp_sym->attr.pointer == 1)
3950 gfc_error ("Variable '%s' at %L cannot have both the "
3951 "POINTER and BIND(C) attributes",
3952 tmp_sym->name, &(tmp_sym->declared_at));
3956 if (tmp_sym->attr.allocatable == 1)
3958 gfc_error ("Variable '%s' at %L cannot have both the "
3959 "ALLOCATABLE and BIND(C) attributes",
3960 tmp_sym->name, &(tmp_sym->declared_at));
3966 /* If it is a BIND(C) function, make sure the return value is a
3967 scalar value. The previous tests in this function made sure
3968 the type is interoperable. */
3969 if (bind_c_function && tmp_sym->as != NULL)
3970 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3971 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3973 /* BIND(C) functions can not return a character string. */
3974 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3975 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
3976 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
3977 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
3978 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3979 "be a character string", tmp_sym->name,
3980 &(tmp_sym->declared_at));
3983 /* See if the symbol has been marked as private. If it has, make sure
3984 there is no binding label and warn the user if there is one. */
3985 if (tmp_sym->attr.access == ACCESS_PRIVATE
3986 && tmp_sym->binding_label)
3987 /* Use gfc_warning_now because we won't say that the symbol fails
3988 just because of this. */
3989 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3990 "given the binding label '%s'", tmp_sym->name,
3991 &(tmp_sym->declared_at), tmp_sym->binding_label);
3997 /* Set the appropriate fields for a symbol that's been declared as
3998 BIND(C) (the is_bind_c flag and the binding label), and verify that
3999 the type is C interoperable. Errors are reported by the functions
4000 used to set/test these fields. */
4003 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4005 gfc_try retval = SUCCESS;
4007 /* TODO: Do we need to make sure the vars aren't marked private? */
4009 /* Set the is_bind_c bit in symbol_attribute. */
4010 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4012 if (set_binding_label (&tmp_sym->binding_label, tmp_sym->name,
4013 num_idents) != SUCCESS)
4020 /* Set the fields marking the given common block as BIND(C), including
4021 a binding label, and report any errors encountered. */
4024 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4026 gfc_try retval = SUCCESS;
4028 /* destLabel, common name, typespec (which may have binding label). */
4029 if (set_binding_label (&com_block->binding_label, com_block->name,
4034 /* Set the given common block (com_block) to being bind(c) (1). */
4035 set_com_block_bind_c (com_block, 1);
4041 /* Retrieve the list of one or more identifiers that the given bind(c)
4042 attribute applies to. */
4045 get_bind_c_idents (void)
4047 char name[GFC_MAX_SYMBOL_LEN + 1];
4049 gfc_symbol *tmp_sym = NULL;
4051 gfc_common_head *com_block = NULL;
4053 if (gfc_match_name (name) == MATCH_YES)
4055 found_id = MATCH_YES;
4056 gfc_get_ha_symbol (name, &tmp_sym);
4058 else if (match_common_name (name) == MATCH_YES)
4060 found_id = MATCH_YES;
4061 com_block = gfc_get_common (name, 0);
4065 gfc_error ("Need either entity or common block name for "
4066 "attribute specification statement at %C");
4070 /* Save the current identifier and look for more. */
4073 /* Increment the number of identifiers found for this spec stmt. */
4076 /* Make sure we have a sym or com block, and verify that it can
4077 be bind(c). Set the appropriate field(s) and look for more
4079 if (tmp_sym != NULL || com_block != NULL)
4081 if (tmp_sym != NULL)
4083 if (set_verify_bind_c_sym (tmp_sym, num_idents)
4089 if (set_verify_bind_c_com_block(com_block, num_idents)
4094 /* Look to see if we have another identifier. */
4096 if (gfc_match_eos () == MATCH_YES)
4097 found_id = MATCH_NO;
4098 else if (gfc_match_char (',') != MATCH_YES)
4099 found_id = MATCH_NO;