1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
28 #include "constructor.h"
31 /* Macros to access allocate memory for gfc_data_variable,
32 gfc_data_value and gfc_data. */
33 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
34 #define gfc_get_data_value() XCNEW (gfc_data_value)
35 #define gfc_get_data() XCNEW (gfc_data)
38 static gfc_try set_binding_label (const char **, const char *, int);
41 /* This flag is set if an old-style length selector is matched
42 during a type-declaration statement. */
44 static int old_char_selector;
46 /* When variables acquire types and attributes from a declaration
47 statement, they get them from the following static variables. The
48 first part of a declaration sets these variables and the second
49 part copies these into symbol structures. */
51 static gfc_typespec current_ts;
53 static symbol_attribute current_attr;
54 static gfc_array_spec *current_as;
55 static int colon_seen;
57 /* The current binding label (if any). */
58 static const char* curr_binding_label;
59 /* Need to know how many identifiers are on the current data declaration
60 line in case we're given the BIND(C) attribute with a NAME= specifier. */
61 static int num_idents_on_line;
62 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
63 can supply a name if the curr_binding_label is nil and NAME= was not. */
64 static int has_name_equals = 0;
66 /* Initializer of the previous enumerator. */
68 static gfc_expr *last_initializer;
70 /* History of all the enumerators is maintained, so that
71 kind values of all the enumerators could be updated depending
72 upon the maximum initialized value. */
74 typedef struct enumerator_history
77 gfc_expr *initializer;
78 struct enumerator_history *next;
82 /* Header of enum history chain. */
84 static enumerator_history *enum_history = NULL;
86 /* Pointer of enum history node containing largest initializer. */
88 static enumerator_history *max_enum = NULL;
90 /* gfc_new_block points to the symbol of a newly matched block. */
92 gfc_symbol *gfc_new_block;
94 bool gfc_matching_function;
97 /********************* DATA statement subroutines *********************/
99 static bool in_match_data = false;
102 gfc_in_match_data (void)
104 return in_match_data;
108 set_in_match_data (bool set_value)
110 in_match_data = set_value;
113 /* Free a gfc_data_variable structure and everything beneath it. */
116 free_variable (gfc_data_variable *p)
118 gfc_data_variable *q;
123 gfc_free_expr (p->expr);
124 gfc_free_iterator (&p->iter, 0);
125 free_variable (p->list);
131 /* Free a gfc_data_value structure and everything beneath it. */
134 free_value (gfc_data_value *p)
141 mpz_clear (p->repeat);
142 gfc_free_expr (p->expr);
148 /* Free a list of gfc_data structures. */
151 gfc_free_data (gfc_data *p)
158 free_variable (p->var);
159 free_value (p->value);
165 /* Free all data in a namespace. */
168 gfc_free_data_all (gfc_namespace *ns)
181 static match var_element (gfc_data_variable *);
183 /* Match a list of variables terminated by an iterator and a right
187 var_list (gfc_data_variable *parent)
189 gfc_data_variable *tail, var;
192 m = var_element (&var);
193 if (m == MATCH_ERROR)
198 tail = gfc_get_data_variable ();
205 if (gfc_match_char (',') != MATCH_YES)
208 m = gfc_match_iterator (&parent->iter, 1);
211 if (m == MATCH_ERROR)
214 m = var_element (&var);
215 if (m == MATCH_ERROR)
220 tail->next = gfc_get_data_variable ();
226 if (gfc_match_char (')') != MATCH_YES)
231 gfc_syntax_error (ST_DATA);
236 /* Match a single element in a data variable list, which can be a
237 variable-iterator list. */
240 var_element (gfc_data_variable *new_var)
245 memset (new_var, 0, sizeof (gfc_data_variable));
247 if (gfc_match_char ('(') == MATCH_YES)
248 return var_list (new_var);
250 m = gfc_match_variable (&new_var->expr, 0);
254 sym = new_var->expr->symtree->n.sym;
256 /* Symbol should already have an associated type. */
257 if (gfc_check_symbol_typed (sym, gfc_current_ns,
258 false, gfc_current_locus) == FAILURE)
261 if (!sym->attr.function && gfc_current_ns->parent
262 && gfc_current_ns->parent == sym->ns)
264 gfc_error ("Host associated variable '%s' may not be in the DATA "
265 "statement at %C", sym->name);
269 if (gfc_current_state () != COMP_BLOCK_DATA
270 && sym->attr.in_common
271 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
272 "common block variable '%s' in DATA statement at %C",
273 sym->name) == FAILURE)
276 if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
283 /* Match the top-level list of data variables. */
286 top_var_list (gfc_data *d)
288 gfc_data_variable var, *tail, *new_var;
295 m = var_element (&var);
298 if (m == MATCH_ERROR)
301 new_var = gfc_get_data_variable ();
307 tail->next = new_var;
311 if (gfc_match_char ('/') == MATCH_YES)
313 if (gfc_match_char (',') != MATCH_YES)
320 gfc_syntax_error (ST_DATA);
321 gfc_free_data_all (gfc_current_ns);
327 match_data_constant (gfc_expr **result)
329 char name[GFC_MAX_SYMBOL_LEN + 1];
330 gfc_symbol *sym, *dt_sym = NULL;
335 m = gfc_match_literal_constant (&expr, 1);
342 if (m == MATCH_ERROR)
345 m = gfc_match_null (result);
349 old_loc = gfc_current_locus;
351 /* Should this be a structure component, try to match it
352 before matching a name. */
353 m = gfc_match_rvalue (result);
354 if (m == MATCH_ERROR)
357 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
359 if (gfc_simplify_expr (*result, 0) == FAILURE)
364 gfc_current_locus = old_loc;
366 m = gfc_match_name (name);
370 if (gfc_find_symbol (name, NULL, 1, &sym))
373 if (sym && sym->attr.generic)
374 dt_sym = gfc_find_dt_in_generic (sym);
377 || (sym->attr.flavor != FL_PARAMETER
378 && (!dt_sym || dt_sym->attr.flavor != FL_DERIVED)))
380 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
384 else if (dt_sym && dt_sym->attr.flavor == FL_DERIVED)
385 return gfc_match_structure_constructor (dt_sym, result);
387 /* Check to see if the value is an initialization array expression. */
388 if (sym->value->expr_type == EXPR_ARRAY)
390 gfc_current_locus = old_loc;
392 m = gfc_match_init_expr (result);
393 if (m == MATCH_ERROR)
398 if (gfc_simplify_expr (*result, 0) == FAILURE)
401 if ((*result)->expr_type == EXPR_CONSTANT)
405 gfc_error ("Invalid initializer %s in Data statement at %C", name);
411 *result = gfc_copy_expr (sym->value);
416 /* Match a list of values in a DATA statement. The leading '/' has
417 already been seen at this point. */
420 top_val_list (gfc_data *data)
422 gfc_data_value *new_val, *tail;
430 m = match_data_constant (&expr);
433 if (m == MATCH_ERROR)
436 new_val = gfc_get_data_value ();
437 mpz_init (new_val->repeat);
440 data->value = new_val;
442 tail->next = new_val;
446 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
449 mpz_set_ui (tail->repeat, 1);
453 if (expr->ts.type == BT_INTEGER)
454 mpz_set (tail->repeat, expr->value.integer);
455 gfc_free_expr (expr);
457 m = match_data_constant (&tail->expr);
460 if (m == MATCH_ERROR)
464 if (gfc_match_char ('/') == MATCH_YES)
466 if (gfc_match_char (',') == MATCH_NO)
473 gfc_syntax_error (ST_DATA);
474 gfc_free_data_all (gfc_current_ns);
479 /* Matches an old style initialization. */
482 match_old_style_init (const char *name)
489 /* Set up data structure to hold initializers. */
490 gfc_find_sym_tree (name, NULL, 0, &st);
493 newdata = gfc_get_data ();
494 newdata->var = gfc_get_data_variable ();
495 newdata->var->expr = gfc_get_variable_expr (st);
496 newdata->where = gfc_current_locus;
498 /* Match initial value list. This also eats the terminal '/'. */
499 m = top_val_list (newdata);
508 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
512 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
514 /* Mark the variable as having appeared in a data statement. */
515 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
521 /* Chain in namespace list of DATA initializers. */
522 newdata->next = gfc_current_ns->data;
523 gfc_current_ns->data = newdata;
529 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
530 we are matching a DATA statement and are therefore issuing an error
531 if we encounter something unexpected, if not, we're trying to match
532 an old-style initialization expression of the form INTEGER I /2/. */
535 gfc_match_data (void)
540 set_in_match_data (true);
544 new_data = gfc_get_data ();
545 new_data->where = gfc_current_locus;
547 m = top_var_list (new_data);
551 m = top_val_list (new_data);
555 new_data->next = gfc_current_ns->data;
556 gfc_current_ns->data = new_data;
558 if (gfc_match_eos () == MATCH_YES)
561 gfc_match_char (','); /* Optional comma */
564 set_in_match_data (false);
568 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
571 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
576 set_in_match_data (false);
577 gfc_free_data (new_data);
582 /************************ Declaration statements *********************/
585 /* Auxilliary function to merge DIMENSION and CODIMENSION array specs. */
588 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
592 if (to->rank == 0 && from->rank > 0)
594 to->rank = from->rank;
595 to->type = from->type;
596 to->cray_pointee = from->cray_pointee;
597 to->cp_was_assumed = from->cp_was_assumed;
599 for (i = 0; i < to->corank; i++)
601 to->lower[from->rank + i] = to->lower[i];
602 to->upper[from->rank + i] = to->upper[i];
604 for (i = 0; i < from->rank; i++)
608 to->lower[i] = gfc_copy_expr (from->lower[i]);
609 to->upper[i] = gfc_copy_expr (from->upper[i]);
613 to->lower[i] = from->lower[i];
614 to->upper[i] = from->upper[i];
618 else if (to->corank == 0 && from->corank > 0)
620 to->corank = from->corank;
621 to->cotype = from->cotype;
623 for (i = 0; i < from->corank; i++)
627 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
628 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
632 to->lower[to->rank + i] = from->lower[i];
633 to->upper[to->rank + i] = from->upper[i];
640 /* Match an intent specification. Since this can only happen after an
641 INTENT word, a legal intent-spec must follow. */
644 match_intent_spec (void)
647 if (gfc_match (" ( in out )") == MATCH_YES)
649 if (gfc_match (" ( in )") == MATCH_YES)
651 if (gfc_match (" ( out )") == MATCH_YES)
654 gfc_error ("Bad INTENT specification at %C");
655 return INTENT_UNKNOWN;
659 /* Matches a character length specification, which is either a
660 specification expression, '*', or ':'. */
663 char_len_param_value (gfc_expr **expr, bool *deferred)
670 if (gfc_match_char ('*') == MATCH_YES)
673 if (gfc_match_char (':') == MATCH_YES)
675 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: deferred type "
676 "parameter at %C") == FAILURE)
684 m = gfc_match_expr (expr);
687 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
690 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
692 if ((*expr)->value.function.actual
693 && (*expr)->value.function.actual->expr->symtree)
696 e = (*expr)->value.function.actual->expr;
697 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
698 && e->expr_type == EXPR_VARIABLE)
700 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
702 if (e->symtree->n.sym->ts.type == BT_CHARACTER
703 && e->symtree->n.sym->ts.u.cl
704 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
712 gfc_error ("Conflict in attributes of function argument at %C");
717 /* A character length is a '*' followed by a literal integer or a
718 char_len_param_value in parenthesis. */
721 match_char_length (gfc_expr **expr, bool *deferred)
727 m = gfc_match_char ('*');
731 m = gfc_match_small_literal_int (&length, NULL);
732 if (m == MATCH_ERROR)
737 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
738 "Old-style character length at %C") == FAILURE)
740 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
744 if (gfc_match_char ('(') == MATCH_NO)
747 m = char_len_param_value (expr, deferred);
748 if (m != MATCH_YES && gfc_matching_function)
754 if (m == MATCH_ERROR)
759 if (gfc_match_char (')') == MATCH_NO)
761 gfc_free_expr (*expr);
769 gfc_error ("Syntax error in character length specification at %C");
774 /* Special subroutine for finding a symbol. Check if the name is found
775 in the current name space. If not, and we're compiling a function or
776 subroutine and the parent compilation unit is an interface, then check
777 to see if the name we've been given is the name of the interface
778 (located in another namespace). */
781 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
787 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
790 *result = st ? st->n.sym : NULL;
794 if (gfc_current_state () != COMP_SUBROUTINE
795 && gfc_current_state () != COMP_FUNCTION)
798 s = gfc_state_stack->previous;
802 if (s->state != COMP_INTERFACE)
805 goto end; /* Nameless interface. */
807 if (strcmp (name, s->sym->name) == 0)
818 /* Special subroutine for getting a symbol node associated with a
819 procedure name, used in SUBROUTINE and FUNCTION statements. The
820 symbol is created in the parent using with symtree node in the
821 child unit pointing to the symbol. If the current namespace has no
822 parent, then the symbol is just created in the current unit. */
825 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
831 /* Module functions have to be left in their own namespace because
832 they have potentially (almost certainly!) already been referenced.
833 In this sense, they are rather like external functions. This is
834 fixed up in resolve.c(resolve_entries), where the symbol name-
835 space is set to point to the master function, so that the fake
836 result mechanism can work. */
837 if (module_fcn_entry)
839 /* Present if entry is declared to be a module procedure. */
840 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
843 rc = gfc_get_symbol (name, NULL, result);
844 else if (!gfc_get_symbol (name, NULL, &sym) && sym
845 && (*result)->ts.type == BT_UNKNOWN
846 && sym->attr.flavor == FL_UNKNOWN)
847 /* Pick up the typespec for the entry, if declared in the function
848 body. Note that this symbol is FL_UNKNOWN because it will
849 only have appeared in a type declaration. The local symtree
850 is set to point to the module symbol and a unique symtree
851 to the local version. This latter ensures a correct clearing
854 /* If the ENTRY proceeds its specification, we need to ensure
855 that this does not raise a "has no IMPLICIT type" error. */
856 if (sym->ts.type == BT_UNKNOWN)
857 sym->attr.untyped = 1;
859 (*result)->ts = sym->ts;
861 /* Put the symbol in the procedure namespace so that, should
862 the ENTRY precede its specification, the specification
864 (*result)->ns = gfc_current_ns;
866 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
868 st = gfc_get_unique_symtree (gfc_current_ns);
873 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
879 gfc_current_ns->refs++;
881 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
883 /* Trap another encompassed procedure with the same name. All
884 these conditions are necessary to avoid picking up an entry
885 whose name clashes with that of the encompassing procedure;
886 this is handled using gsymbols to register unique,globally
888 if (sym->attr.flavor != 0
889 && sym->attr.proc != 0
890 && (sym->attr.subroutine || sym->attr.function)
891 && sym->attr.if_source != IFSRC_UNKNOWN)
892 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
893 name, &sym->declared_at);
895 /* Trap a procedure with a name the same as interface in the
896 encompassing scope. */
897 if (sym->attr.generic != 0
898 && (sym->attr.subroutine || sym->attr.function)
899 && !sym->attr.mod_proc)
900 gfc_error_now ("Name '%s' at %C is already defined"
901 " as a generic interface at %L",
902 name, &sym->declared_at);
904 /* Trap declarations of attributes in encompassing scope. The
905 signature for this is that ts.kind is set. Legitimate
906 references only set ts.type. */
907 if (sym->ts.kind != 0
908 && !sym->attr.implicit_type
909 && sym->attr.proc == 0
910 && gfc_current_ns->parent != NULL
911 && sym->attr.access == 0
912 && !module_fcn_entry)
913 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
914 "and must not have attributes declared at %L",
915 name, &sym->declared_at);
918 if (gfc_current_ns->parent == NULL || *result == NULL)
921 /* Module function entries will already have a symtree in
922 the current namespace but will need one at module level. */
923 if (module_fcn_entry)
925 /* Present if entry is declared to be a module procedure. */
926 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
928 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
931 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
936 /* See if the procedure should be a module procedure. */
938 if (((sym->ns->proc_name != NULL
939 && sym->ns->proc_name->attr.flavor == FL_MODULE
940 && sym->attr.proc != PROC_MODULE)
941 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
942 && gfc_add_procedure (&sym->attr, PROC_MODULE,
943 sym->name, NULL) == FAILURE)
950 /* Verify that the given symbol representing a parameter is C
951 interoperable, by checking to see if it was marked as such after
952 its declaration. If the given symbol is not interoperable, a
953 warning is reported, thus removing the need to return the status to
954 the calling function. The standard does not require the user use
955 one of the iso_c_binding named constants to declare an
956 interoperable parameter, but we can't be sure if the param is C
957 interop or not if the user doesn't. For example, integer(4) may be
958 legal Fortran, but doesn't have meaning in C. It may interop with
959 a number of the C types, which causes a problem because the
960 compiler can't know which one. This code is almost certainly not
961 portable, and the user will get what they deserve if the C type
962 across platforms isn't always interoperable with integer(4). If
963 the user had used something like integer(c_int) or integer(c_long),
964 the compiler could have automatically handled the varying sizes
968 gfc_verify_c_interop_param (gfc_symbol *sym)
970 int is_c_interop = 0;
971 gfc_try retval = SUCCESS;
973 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
974 Don't repeat the checks here. */
975 if (sym->attr.implicit_type)
978 /* For subroutines or functions that are passed to a BIND(C) procedure,
979 they're interoperable if they're BIND(C) and their params are all
981 if (sym->attr.flavor == FL_PROCEDURE)
983 if (sym->attr.is_bind_c == 0)
985 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
986 "attribute to be C interoperable", sym->name,
987 &(sym->declared_at));
993 if (sym->attr.is_c_interop == 1)
994 /* We've already checked this procedure; don't check it again. */
997 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1002 /* See if we've stored a reference to a procedure that owns sym. */
1003 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1005 if (sym->ns->proc_name->attr.is_bind_c == 1)
1007 is_c_interop = (gfc_verify_c_interop (&(sym->ts)) == SUCCESS ? 1 : 0);
1009 if (is_c_interop != 1)
1011 /* Make personalized messages to give better feedback. */
1012 if (sym->ts.type == BT_DERIVED)
1013 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1014 "BIND(C) procedure '%s' but is not C interoperable "
1015 "because derived type '%s' is not C interoperable",
1016 sym->name, &(sym->declared_at),
1017 sym->ns->proc_name->name,
1018 sym->ts.u.derived->name);
1019 else if (sym->ts.type == BT_CLASS)
1020 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1021 "BIND(C) procedure '%s' but is not C interoperable "
1022 "because it is polymorphic",
1023 sym->name, &(sym->declared_at),
1024 sym->ns->proc_name->name);
1026 gfc_warning ("Variable '%s' at %L is a parameter to the "
1027 "BIND(C) procedure '%s' but may not be C "
1029 sym->name, &(sym->declared_at),
1030 sym->ns->proc_name->name);
1033 /* Character strings are only C interoperable if they have a
1035 if (sym->ts.type == BT_CHARACTER)
1037 gfc_charlen *cl = sym->ts.u.cl;
1038 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1039 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1041 gfc_error ("Character argument '%s' at %L "
1042 "must be length 1 because "
1043 "procedure '%s' is BIND(C)",
1044 sym->name, &sym->declared_at,
1045 sym->ns->proc_name->name);
1050 /* We have to make sure that any param to a bind(c) routine does
1051 not have the allocatable, pointer, or optional attributes,
1052 according to J3/04-007, section 5.1. */
1053 if (sym->attr.allocatable == 1)
1055 gfc_error ("Variable '%s' at %L cannot have the "
1056 "ALLOCATABLE attribute because procedure '%s'"
1057 " is BIND(C)", sym->name, &(sym->declared_at),
1058 sym->ns->proc_name->name);
1062 if (sym->attr.pointer == 1)
1064 gfc_error ("Variable '%s' at %L cannot have the "
1065 "POINTER attribute because procedure '%s'"
1066 " is BIND(C)", sym->name, &(sym->declared_at),
1067 sym->ns->proc_name->name);
1071 if (sym->attr.optional == 1 && sym->attr.value)
1073 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1074 "and the VALUE attribute because procedure '%s' "
1075 "is BIND(C)", sym->name, &(sym->declared_at),
1076 sym->ns->proc_name->name);
1079 else if (sym->attr.optional == 1
1080 && gfc_notify_std (GFC_STD_F2008_TS, "TS29113: Variable '%s' "
1081 "at %L with OPTIONAL attribute in "
1082 "procedure '%s' which is BIND(C)",
1083 sym->name, &(sym->declared_at),
1084 sym->ns->proc_name->name)
1088 /* Make sure that if it has the dimension attribute, that it is
1089 either assumed size or explicit shape. */
1090 if (sym->as != NULL)
1092 if (sym->as->type == AS_ASSUMED_SHAPE)
1094 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1095 "argument to the procedure '%s' at %L because "
1096 "the procedure is BIND(C)", sym->name,
1097 &(sym->declared_at), sym->ns->proc_name->name,
1098 &(sym->ns->proc_name->declared_at));
1102 if (sym->as->type == AS_DEFERRED)
1104 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1105 "argument to the procedure '%s' at %L because "
1106 "the procedure is BIND(C)", sym->name,
1107 &(sym->declared_at), sym->ns->proc_name->name,
1108 &(sym->ns->proc_name->declared_at));
1120 /* Function called by variable_decl() that adds a name to the symbol table. */
1123 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1124 gfc_array_spec **as, locus *var_locus)
1126 symbol_attribute attr;
1129 if (gfc_get_symbol (name, NULL, &sym))
1132 /* Start updating the symbol table. Add basic type attribute if present. */
1133 if (current_ts.type != BT_UNKNOWN
1134 && (sym->attr.implicit_type == 0
1135 || !gfc_compare_types (&sym->ts, ¤t_ts))
1136 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
1139 if (sym->ts.type == BT_CHARACTER)
1142 sym->ts.deferred = cl_deferred;
1145 /* Add dimension attribute if present. */
1146 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1150 /* Add attribute to symbol. The copy is so that we can reset the
1151 dimension attribute. */
1152 attr = current_attr;
1154 attr.codimension = 0;
1156 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1159 /* Finish any work that may need to be done for the binding label,
1160 if it's a bind(c). The bind(c) attr is found before the symbol
1161 is made, and before the symbol name (for data decls), so the
1162 current_ts is holding the binding label, or nothing if the
1163 name= attr wasn't given. Therefore, test here if we're dealing
1164 with a bind(c) and make sure the binding label is set correctly. */
1165 if (sym->attr.is_bind_c == 1)
1167 if (!sym->binding_label)
1169 /* Set the binding label and verify that if a NAME= was specified
1170 then only one identifier was in the entity-decl-list. */
1171 if (set_binding_label (&sym->binding_label, sym->name,
1172 num_idents_on_line) == FAILURE)
1177 /* See if we know we're in a common block, and if it's a bind(c)
1178 common then we need to make sure we're an interoperable type. */
1179 if (sym->attr.in_common == 1)
1181 /* Test the common block object. */
1182 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1183 && sym->ts.is_c_interop != 1)
1185 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1186 "must be declared with a C interoperable "
1187 "kind since common block '%s' is BIND(C)",
1188 sym->name, sym->common_block->name,
1189 sym->common_block->name);
1194 sym->attr.implied_index = 0;
1196 if (sym->ts.type == BT_CLASS)
1197 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1203 /* Set character constant to the given length. The constant will be padded or
1204 truncated. If we're inside an array constructor without a typespec, we
1205 additionally check that all elements have the same length; check_len -1
1206 means no checking. */
1209 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1214 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1215 gcc_assert (expr->ts.type == BT_CHARACTER);
1217 slen = expr->value.character.length;
1220 s = gfc_get_wide_string (len + 1);
1221 memcpy (s, expr->value.character.string,
1222 MIN (len, slen) * sizeof (gfc_char_t));
1224 gfc_wide_memset (&s[slen], ' ', len - slen);
1226 if (gfc_option.warn_character_truncation && slen > len)
1227 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1228 "(%d/%d)", &expr->where, slen, len);
1230 /* Apply the standard by 'hand' otherwise it gets cleared for
1232 if (check_len != -1 && slen != check_len
1233 && !(gfc_option.allow_std & GFC_STD_GNU))
1234 gfc_error_now ("The CHARACTER elements of the array constructor "
1235 "at %L must have the same length (%d/%d)",
1236 &expr->where, slen, check_len);
1239 free (expr->value.character.string);
1240 expr->value.character.string = s;
1241 expr->value.character.length = len;
1246 /* Function to create and update the enumerator history
1247 using the information passed as arguments.
1248 Pointer "max_enum" is also updated, to point to
1249 enum history node containing largest initializer.
1251 SYM points to the symbol node of enumerator.
1252 INIT points to its enumerator value. */
1255 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1257 enumerator_history *new_enum_history;
1258 gcc_assert (sym != NULL && init != NULL);
1260 new_enum_history = XCNEW (enumerator_history);
1262 new_enum_history->sym = sym;
1263 new_enum_history->initializer = init;
1264 new_enum_history->next = NULL;
1266 if (enum_history == NULL)
1268 enum_history = new_enum_history;
1269 max_enum = enum_history;
1273 new_enum_history->next = enum_history;
1274 enum_history = new_enum_history;
1276 if (mpz_cmp (max_enum->initializer->value.integer,
1277 new_enum_history->initializer->value.integer) < 0)
1278 max_enum = new_enum_history;
1283 /* Function to free enum kind history. */
1286 gfc_free_enum_history (void)
1288 enumerator_history *current = enum_history;
1289 enumerator_history *next;
1291 while (current != NULL)
1293 next = current->next;
1298 enum_history = NULL;
1302 /* Function called by variable_decl() that adds an initialization
1303 expression to a symbol. */
1306 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1308 symbol_attribute attr;
1313 if (find_special (name, &sym, false))
1318 /* If this symbol is confirming an implicit parameter type,
1319 then an initialization expression is not allowed. */
1320 if (attr.flavor == FL_PARAMETER
1321 && sym->value != NULL
1324 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1331 /* An initializer is required for PARAMETER declarations. */
1332 if (attr.flavor == FL_PARAMETER)
1334 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1340 /* If a variable appears in a DATA block, it cannot have an
1344 gfc_error ("Variable '%s' at %C with an initializer already "
1345 "appears in a DATA statement", sym->name);
1349 /* Check if the assignment can happen. This has to be put off
1350 until later for derived type variables and procedure pointers. */
1351 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1352 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1353 && !sym->attr.proc_pointer
1354 && gfc_check_assign_symbol (sym, init) == FAILURE)
1357 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1358 && init->ts.type == BT_CHARACTER)
1360 /* Update symbol character length according initializer. */
1361 if (gfc_check_assign_symbol (sym, init) == FAILURE)
1364 if (sym->ts.u.cl->length == NULL)
1367 /* If there are multiple CHARACTER variables declared on the
1368 same line, we don't want them to share the same length. */
1369 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1371 if (sym->attr.flavor == FL_PARAMETER)
1373 if (init->expr_type == EXPR_CONSTANT)
1375 clen = init->value.character.length;
1376 sym->ts.u.cl->length
1377 = gfc_get_int_expr (gfc_default_integer_kind,
1380 else if (init->expr_type == EXPR_ARRAY)
1383 c = gfc_constructor_first (init->value.constructor);
1384 clen = c->expr->value.character.length;
1385 sym->ts.u.cl->length
1386 = gfc_get_int_expr (gfc_default_integer_kind,
1389 else if (init->ts.u.cl && init->ts.u.cl->length)
1390 sym->ts.u.cl->length =
1391 gfc_copy_expr (sym->value->ts.u.cl->length);
1394 /* Update initializer character length according symbol. */
1395 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1397 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1399 if (init->expr_type == EXPR_CONSTANT)
1400 gfc_set_constant_character_len (len, init, -1);
1401 else if (init->expr_type == EXPR_ARRAY)
1405 /* Build a new charlen to prevent simplification from
1406 deleting the length before it is resolved. */
1407 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1408 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1410 for (c = gfc_constructor_first (init->value.constructor);
1411 c; c = gfc_constructor_next (c))
1412 gfc_set_constant_character_len (len, c->expr, -1);
1417 /* If sym is implied-shape, set its upper bounds from init. */
1418 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1419 && sym->as->type == AS_IMPLIED_SHAPE)
1423 if (init->rank == 0)
1425 gfc_error ("Can't initialize implied-shape array at %L"
1426 " with scalar", &sym->declared_at);
1429 gcc_assert (sym->as->rank == init->rank);
1431 /* Shape should be present, we get an initialization expression. */
1432 gcc_assert (init->shape);
1434 for (dim = 0; dim < sym->as->rank; ++dim)
1440 lower = sym->as->lower[dim];
1441 if (lower->expr_type != EXPR_CONSTANT)
1443 gfc_error ("Non-constant lower bound in implied-shape"
1444 " declaration at %L", &lower->where);
1448 /* All dimensions must be without upper bound. */
1449 gcc_assert (!sym->as->upper[dim]);
1452 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1453 mpz_add (e->value.integer,
1454 lower->value.integer, init->shape[dim]);
1455 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1456 sym->as->upper[dim] = e;
1459 sym->as->type = AS_EXPLICIT;
1462 /* Need to check if the expression we initialized this
1463 to was one of the iso_c_binding named constants. If so,
1464 and we're a parameter (constant), let it be iso_c.
1466 integer(c_int), parameter :: my_int = c_int
1467 integer(my_int) :: my_int_2
1468 If we mark my_int as iso_c (since we can see it's value
1469 is equal to one of the named constants), then my_int_2
1470 will be considered C interoperable. */
1471 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1473 sym->ts.is_iso_c |= init->ts.is_iso_c;
1474 sym->ts.is_c_interop |= init->ts.is_c_interop;
1475 /* attr bits needed for module files. */
1476 sym->attr.is_iso_c |= init->ts.is_iso_c;
1477 sym->attr.is_c_interop |= init->ts.is_c_interop;
1478 if (init->ts.is_iso_c)
1479 sym->ts.f90_type = init->ts.f90_type;
1482 /* Add initializer. Make sure we keep the ranks sane. */
1483 if (sym->attr.dimension && init->rank == 0)
1488 if (sym->attr.flavor == FL_PARAMETER
1489 && init->expr_type == EXPR_CONSTANT
1490 && spec_size (sym->as, &size) == SUCCESS
1491 && mpz_cmp_si (size, 0) > 0)
1493 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1495 for (n = 0; n < (int)mpz_get_si (size); n++)
1496 gfc_constructor_append_expr (&array->value.constructor,
1499 : gfc_copy_expr (init),
1502 array->shape = gfc_get_shape (sym->as->rank);
1503 for (n = 0; n < sym->as->rank; n++)
1504 spec_dimen_size (sym->as, n, &array->shape[n]);
1509 init->rank = sym->as->rank;
1513 if (sym->attr.save == SAVE_NONE)
1514 sym->attr.save = SAVE_IMPLICIT;
1522 /* Function called by variable_decl() that adds a name to a structure
1526 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1527 gfc_array_spec **as)
1530 gfc_try t = SUCCESS;
1532 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1533 constructing, it must have the pointer attribute. */
1534 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1535 && current_ts.u.derived == gfc_current_block ()
1536 && current_attr.pointer == 0)
1538 gfc_error ("Component at %C must have the POINTER attribute");
1542 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1544 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1546 gfc_error ("Array component of structure at %C must have explicit "
1547 "or deferred shape");
1552 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1556 if (c->ts.type == BT_CHARACTER)
1558 c->attr = current_attr;
1560 c->initializer = *init;
1567 c->attr.codimension = 1;
1569 c->attr.dimension = 1;
1573 /* Should this ever get more complicated, combine with similar section
1574 in add_init_expr_to_sym into a separate function. */
1575 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1577 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1581 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1582 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1583 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1585 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1587 if (c->initializer->expr_type == EXPR_CONSTANT)
1588 gfc_set_constant_character_len (len, c->initializer, -1);
1589 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1590 c->initializer->ts.u.cl->length->value.integer))
1592 gfc_constructor *ctor;
1593 ctor = gfc_constructor_first (c->initializer->value.constructor);
1598 bool has_ts = (c->initializer->ts.u.cl
1599 && c->initializer->ts.u.cl->length_from_typespec);
1601 /* Remember the length of the first element for checking
1602 that all elements *in the constructor* have the same
1603 length. This need not be the length of the LHS! */
1604 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1605 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1606 first_len = ctor->expr->value.character.length;
1608 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1609 if (ctor->expr->expr_type == EXPR_CONSTANT)
1611 gfc_set_constant_character_len (len, ctor->expr,
1612 has_ts ? -1 : first_len);
1613 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1619 /* Check array components. */
1620 if (!c->attr.dimension)
1623 if (c->attr.pointer)
1625 if (c->as->type != AS_DEFERRED)
1627 gfc_error ("Pointer array component of structure at %C must have a "
1632 else if (c->attr.allocatable)
1634 if (c->as->type != AS_DEFERRED)
1636 gfc_error ("Allocatable component of structure at %C must have a "
1643 if (c->as->type != AS_EXPLICIT)
1645 gfc_error ("Array component of structure at %C must have an "
1652 if (c->ts.type == BT_CLASS)
1654 bool delayed = (gfc_state_stack->sym == c->ts.u.derived)
1655 || (!c->ts.u.derived->components
1656 && !c->ts.u.derived->attr.zero_comp);
1657 return gfc_build_class_symbol (&c->ts, &c->attr, &c->as, delayed);
1664 /* Match a 'NULL()', and possibly take care of some side effects. */
1667 gfc_match_null (gfc_expr **result)
1672 m = gfc_match (" null ( )");
1676 /* The NULL symbol now has to be/become an intrinsic function. */
1677 if (gfc_get_symbol ("null", NULL, &sym))
1679 gfc_error ("NULL() initialization at %C is ambiguous");
1683 gfc_intrinsic_symbol (sym);
1685 if (sym->attr.proc != PROC_INTRINSIC
1686 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1687 sym->name, NULL) == FAILURE
1688 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1691 *result = gfc_get_null_expr (&gfc_current_locus);
1697 /* Match the initialization expr for a data pointer or procedure pointer. */
1700 match_pointer_init (gfc_expr **init, int procptr)
1704 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1706 gfc_error ("Initialization of pointer at %C is not allowed in "
1707 "a PURE procedure");
1711 /* Match NULL() initilization. */
1712 m = gfc_match_null (init);
1716 /* Match non-NULL initialization. */
1717 gfc_matching_ptr_assignment = !procptr;
1718 gfc_matching_procptr_assignment = procptr;
1719 m = gfc_match_rvalue (init);
1720 gfc_matching_ptr_assignment = 0;
1721 gfc_matching_procptr_assignment = 0;
1722 if (m == MATCH_ERROR)
1724 else if (m == MATCH_NO)
1726 gfc_error ("Error in pointer initialization at %C");
1729 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
1732 gfc_resolve_expr (*init);
1734 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: non-NULL pointer "
1735 "initialization at %C") == FAILURE)
1743 check_function_name (char *name)
1745 /* In functions that have a RESULT variable defined, the function name always
1746 refers to function calls. Therefore, the name is not allowed to appear in
1747 specification statements. When checking this, be careful about
1748 'hidden' procedure pointer results ('ppr@'). */
1750 if (gfc_current_state () == COMP_FUNCTION)
1752 gfc_symbol *block = gfc_current_block ();
1753 if (block && block->result && block->result != block
1754 && strcmp (block->result->name, "ppr@") != 0
1755 && strcmp (block->name, name) == 0)
1757 gfc_error ("Function name '%s' not allowed at %C", name);
1766 /* Match a variable name with an optional initializer. When this
1767 subroutine is called, a variable is expected to be parsed next.
1768 Depending on what is happening at the moment, updates either the
1769 symbol table or the current interface. */
1772 variable_decl (int elem)
1774 char name[GFC_MAX_SYMBOL_LEN + 1];
1775 gfc_expr *initializer, *char_len;
1777 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1789 /* When we get here, we've just matched a list of attributes and
1790 maybe a type and a double colon. The next thing we expect to see
1791 is the name of the symbol. */
1792 m = gfc_match_name (name);
1796 var_locus = gfc_current_locus;
1798 /* Now we could see the optional array spec. or character length. */
1799 m = gfc_match_array_spec (&as, true, true);
1800 if (m == MATCH_ERROR)
1804 as = gfc_copy_array_spec (current_as);
1805 else if (current_as)
1806 merge_array_spec (current_as, as, true);
1808 if (gfc_option.flag_cray_pointer)
1809 cp_as = gfc_copy_array_spec (as);
1811 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1812 determine (and check) whether it can be implied-shape. If it
1813 was parsed as assumed-size, change it because PARAMETERs can not
1817 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1820 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1825 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1826 && current_attr.flavor == FL_PARAMETER)
1827 as->type = AS_IMPLIED_SHAPE;
1829 if (as->type == AS_IMPLIED_SHAPE
1830 && gfc_notify_std (GFC_STD_F2008,
1831 "Fortran 2008: Implied-shape array at %L",
1832 &var_locus) == FAILURE)
1841 cl_deferred = false;
1843 if (current_ts.type == BT_CHARACTER)
1845 switch (match_char_length (&char_len, &cl_deferred))
1848 cl = gfc_new_charlen (gfc_current_ns, NULL);
1850 cl->length = char_len;
1853 /* Non-constant lengths need to be copied after the first
1854 element. Also copy assumed lengths. */
1857 && (current_ts.u.cl->length == NULL
1858 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1860 cl = gfc_new_charlen (gfc_current_ns, NULL);
1861 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1864 cl = current_ts.u.cl;
1866 cl_deferred = current_ts.deferred;
1875 /* If this symbol has already shown up in a Cray Pointer declaration,
1876 then we want to set the type & bail out. */
1877 if (gfc_option.flag_cray_pointer)
1879 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1880 if (sym != NULL && sym->attr.cray_pointee)
1882 sym->ts.type = current_ts.type;
1883 sym->ts.kind = current_ts.kind;
1885 sym->ts.u.derived = current_ts.u.derived;
1886 sym->ts.is_c_interop = current_ts.is_c_interop;
1887 sym->ts.is_iso_c = current_ts.is_iso_c;
1890 /* Check to see if we have an array specification. */
1893 if (sym->as != NULL)
1895 gfc_error ("Duplicate array spec for Cray pointee at %C");
1896 gfc_free_array_spec (cp_as);
1902 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1903 gfc_internal_error ("Couldn't set pointee array spec.");
1905 /* Fix the array spec. */
1906 m = gfc_mod_pointee_as (sym->as);
1907 if (m == MATCH_ERROR)
1915 gfc_free_array_spec (cp_as);
1919 /* Procedure pointer as function result. */
1920 if (gfc_current_state () == COMP_FUNCTION
1921 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1922 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1923 strcpy (name, "ppr@");
1925 if (gfc_current_state () == COMP_FUNCTION
1926 && strcmp (name, gfc_current_block ()->name) == 0
1927 && gfc_current_block ()->result
1928 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1929 strcpy (name, "ppr@");
1931 /* OK, we've successfully matched the declaration. Now put the
1932 symbol in the current namespace, because it might be used in the
1933 optional initialization expression for this symbol, e.g. this is
1936 integer, parameter :: i = huge(i)
1938 This is only true for parameters or variables of a basic type.
1939 For components of derived types, it is not true, so we don't
1940 create a symbol for those yet. If we fail to create the symbol,
1942 if (gfc_current_state () != COMP_DERIVED
1943 && build_sym (name, cl, cl_deferred, &as, &var_locus) == FAILURE)
1949 if (check_function_name (name) == FAILURE)
1955 /* We allow old-style initializations of the form
1956 integer i /2/, j(4) /3*3, 1/
1957 (if no colon has been seen). These are different from data
1958 statements in that initializers are only allowed to apply to the
1959 variable immediately preceding, i.e.
1961 is not allowed. Therefore we have to do some work manually, that
1962 could otherwise be left to the matchers for DATA statements. */
1964 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1966 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1967 "initialization at %C") == FAILURE)
1970 return match_old_style_init (name);
1973 /* The double colon must be present in order to have initializers.
1974 Otherwise the statement is ambiguous with an assignment statement. */
1977 if (gfc_match (" =>") == MATCH_YES)
1979 if (!current_attr.pointer)
1981 gfc_error ("Initialization at %C isn't for a pointer variable");
1986 m = match_pointer_init (&initializer, 0);
1990 else if (gfc_match_char ('=') == MATCH_YES)
1992 if (current_attr.pointer)
1994 gfc_error ("Pointer initialization at %C requires '=>', "
2000 m = gfc_match_init_expr (&initializer);
2003 gfc_error ("Expected an initialization expression at %C");
2007 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2008 && gfc_state_stack->state != COMP_DERIVED)
2010 gfc_error ("Initialization of variable at %C is not allowed in "
2011 "a PURE procedure");
2015 if (current_attr.flavor != FL_PARAMETER
2016 && gfc_state_stack->state != COMP_DERIVED)
2017 gfc_unset_implicit_pure (gfc_current_ns->proc_name);
2024 if (initializer != NULL && current_attr.allocatable
2025 && gfc_current_state () == COMP_DERIVED)
2027 gfc_error ("Initialization of allocatable component at %C is not "
2033 /* Add the initializer. Note that it is fine if initializer is
2034 NULL here, because we sometimes also need to check if a
2035 declaration *must* have an initialization expression. */
2036 if (gfc_current_state () != COMP_DERIVED)
2037 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2040 if (current_ts.type == BT_DERIVED
2041 && !current_attr.pointer && !initializer)
2042 initializer = gfc_default_initializer (¤t_ts);
2043 t = build_struct (name, cl, &initializer, &as);
2046 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
2049 /* Free stuff up and return. */
2050 gfc_free_expr (initializer);
2051 gfc_free_array_spec (as);
2057 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2058 This assumes that the byte size is equal to the kind number for
2059 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2062 gfc_match_old_kind_spec (gfc_typespec *ts)
2067 if (gfc_match_char ('*') != MATCH_YES)
2070 m = gfc_match_small_literal_int (&ts->kind, NULL);
2074 original_kind = ts->kind;
2076 /* Massage the kind numbers for complex types. */
2077 if (ts->type == BT_COMPLEX)
2081 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2082 gfc_basic_typename (ts->type), original_kind);
2089 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2092 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2096 if (gfc_option.flag_real4_kind == 8)
2098 if (gfc_option.flag_real4_kind == 10)
2100 if (gfc_option.flag_real4_kind == 16)
2106 if (gfc_option.flag_real8_kind == 4)
2108 if (gfc_option.flag_real8_kind == 10)
2110 if (gfc_option.flag_real8_kind == 16)
2115 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2117 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2118 gfc_basic_typename (ts->type), original_kind);
2122 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2123 gfc_basic_typename (ts->type), original_kind) == FAILURE)
2130 /* Match a kind specification. Since kinds are generally optional, we
2131 usually return MATCH_NO if something goes wrong. If a "kind="
2132 string is found, then we know we have an error. */
2135 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2147 where = loc = gfc_current_locus;
2152 if (gfc_match_char ('(') == MATCH_NO)
2155 /* Also gobbles optional text. */
2156 if (gfc_match (" kind = ") == MATCH_YES)
2159 loc = gfc_current_locus;
2162 n = gfc_match_init_expr (&e);
2166 if (gfc_matching_function)
2168 /* The function kind expression might include use associated or
2169 imported parameters and try again after the specification
2171 if (gfc_match_char (')') != MATCH_YES)
2173 gfc_error ("Missing right parenthesis at %C");
2179 gfc_undo_symbols ();
2184 /* ....or else, the match is real. */
2186 gfc_error ("Expected initialization expression at %C");
2194 gfc_error ("Expected scalar initialization expression at %C");
2199 msg = gfc_extract_int (e, &ts->kind);
2208 /* Before throwing away the expression, let's see if we had a
2209 C interoperable kind (and store the fact). */
2210 if (e->ts.is_c_interop == 1)
2212 /* Mark this as c interoperable if being declared with one
2213 of the named constants from iso_c_binding. */
2214 ts->is_c_interop = e->ts.is_iso_c;
2215 ts->f90_type = e->ts.f90_type;
2221 /* Ignore errors to this point, if we've gotten here. This means
2222 we ignore the m=MATCH_ERROR from above. */
2223 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2225 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2226 gfc_basic_typename (ts->type));
2227 gfc_current_locus = where;
2231 /* Warn if, e.g., c_int is used for a REAL variable, but not
2232 if, e.g., c_double is used for COMPLEX as the standard
2233 explicitly says that the kind type parameter for complex and real
2234 variable is the same, i.e. c_float == c_float_complex. */
2235 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2236 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2237 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2238 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2239 "is %s", gfc_basic_typename (ts->f90_type), &where,
2240 gfc_basic_typename (ts->type));
2242 gfc_gobble_whitespace ();
2243 if ((c = gfc_next_ascii_char ()) != ')'
2244 && (ts->type != BT_CHARACTER || c != ','))
2246 if (ts->type == BT_CHARACTER)
2247 gfc_error ("Missing right parenthesis or comma at %C");
2249 gfc_error ("Missing right parenthesis at %C");
2253 /* All tests passed. */
2256 if(m == MATCH_ERROR)
2257 gfc_current_locus = where;
2259 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2262 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2266 if (gfc_option.flag_real4_kind == 8)
2268 if (gfc_option.flag_real4_kind == 10)
2270 if (gfc_option.flag_real4_kind == 16)
2276 if (gfc_option.flag_real8_kind == 4)
2278 if (gfc_option.flag_real8_kind == 10)
2280 if (gfc_option.flag_real8_kind == 16)
2285 /* Return what we know from the test(s). */
2290 gfc_current_locus = where;
2296 match_char_kind (int * kind, int * is_iso_c)
2305 where = gfc_current_locus;
2307 n = gfc_match_init_expr (&e);
2309 if (n != MATCH_YES && gfc_matching_function)
2311 /* The expression might include use-associated or imported
2312 parameters and try again after the specification
2315 gfc_undo_symbols ();
2320 gfc_error ("Expected initialization expression at %C");
2326 gfc_error ("Expected scalar initialization expression at %C");
2331 msg = gfc_extract_int (e, kind);
2332 *is_iso_c = e->ts.is_iso_c;
2342 /* Ignore errors to this point, if we've gotten here. This means
2343 we ignore the m=MATCH_ERROR from above. */
2344 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2346 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2350 /* All tests passed. */
2353 if (m == MATCH_ERROR)
2354 gfc_current_locus = where;
2356 /* Return what we know from the test(s). */
2361 gfc_current_locus = where;
2366 /* Match the various kind/length specifications in a CHARACTER
2367 declaration. We don't return MATCH_NO. */
2370 gfc_match_char_spec (gfc_typespec *ts)
2372 int kind, seen_length, is_iso_c;
2384 /* Try the old-style specification first. */
2385 old_char_selector = 0;
2387 m = match_char_length (&len, &deferred);
2391 old_char_selector = 1;
2396 m = gfc_match_char ('(');
2399 m = MATCH_YES; /* Character without length is a single char. */
2403 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2404 if (gfc_match (" kind =") == MATCH_YES)
2406 m = match_char_kind (&kind, &is_iso_c);
2408 if (m == MATCH_ERROR)
2413 if (gfc_match (" , len =") == MATCH_NO)
2416 m = char_len_param_value (&len, &deferred);
2419 if (m == MATCH_ERROR)
2426 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2427 if (gfc_match (" len =") == MATCH_YES)
2429 m = char_len_param_value (&len, &deferred);
2432 if (m == MATCH_ERROR)
2436 if (gfc_match_char (')') == MATCH_YES)
2439 if (gfc_match (" , kind =") != MATCH_YES)
2442 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2448 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2449 m = char_len_param_value (&len, &deferred);
2452 if (m == MATCH_ERROR)
2456 m = gfc_match_char (')');
2460 if (gfc_match_char (',') != MATCH_YES)
2463 gfc_match (" kind ="); /* Gobble optional text. */
2465 m = match_char_kind (&kind, &is_iso_c);
2466 if (m == MATCH_ERROR)
2472 /* Require a right-paren at this point. */
2473 m = gfc_match_char (')');
2478 gfc_error ("Syntax error in CHARACTER declaration at %C");
2480 gfc_free_expr (len);
2484 /* Deal with character functions after USE and IMPORT statements. */
2485 if (gfc_matching_function)
2487 gfc_free_expr (len);
2488 gfc_undo_symbols ();
2494 gfc_free_expr (len);
2498 /* Do some final massaging of the length values. */
2499 cl = gfc_new_charlen (gfc_current_ns, NULL);
2501 if (seen_length == 0)
2502 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2507 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2508 ts->deferred = deferred;
2510 /* We have to know if it was a c interoperable kind so we can
2511 do accurate type checking of bind(c) procs, etc. */
2513 /* Mark this as c interoperable if being declared with one
2514 of the named constants from iso_c_binding. */
2515 ts->is_c_interop = is_iso_c;
2516 else if (len != NULL)
2517 /* Here, we might have parsed something such as: character(c_char)
2518 In this case, the parsing code above grabs the c_char when
2519 looking for the length (line 1690, roughly). it's the last
2520 testcase for parsing the kind params of a character variable.
2521 However, it's not actually the length. this seems like it
2523 To see if the user used a C interop kind, test the expr
2524 of the so called length, and see if it's C interoperable. */
2525 ts->is_c_interop = len->ts.is_iso_c;
2531 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2532 structure to the matched specification. This is necessary for FUNCTION and
2533 IMPLICIT statements.
2535 If implicit_flag is nonzero, then we don't check for the optional
2536 kind specification. Not doing so is needed for matching an IMPLICIT
2537 statement correctly. */
2540 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2542 char name[GFC_MAX_SYMBOL_LEN + 1];
2543 gfc_symbol *sym, *dt_sym;
2546 bool seen_deferred_kind, matched_type;
2547 const char *dt_name;
2549 /* A belt and braces check that the typespec is correctly being treated
2550 as a deferred characteristic association. */
2551 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2552 && (gfc_current_block ()->result->ts.kind == -1)
2553 && (ts->kind == -1);
2555 if (seen_deferred_kind)
2558 /* Clear the current binding label, in case one is given. */
2559 curr_binding_label = NULL;
2561 if (gfc_match (" byte") == MATCH_YES)
2563 if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2567 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2569 gfc_error ("BYTE type used at %C "
2570 "is not available on the target machine");
2574 ts->type = BT_INTEGER;
2580 m = gfc_match (" type ( %n", name);
2581 matched_type = (m == MATCH_YES);
2583 if ((matched_type && strcmp ("integer", name) == 0)
2584 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2586 ts->type = BT_INTEGER;
2587 ts->kind = gfc_default_integer_kind;
2591 if ((matched_type && strcmp ("character", name) == 0)
2592 || (!matched_type && gfc_match (" character") == MATCH_YES))
2595 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2596 "intrinsic-type-spec at %C") == FAILURE)
2599 ts->type = BT_CHARACTER;
2600 if (implicit_flag == 0)
2601 m = gfc_match_char_spec (ts);
2605 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2611 if ((matched_type && strcmp ("real", name) == 0)
2612 || (!matched_type && gfc_match (" real") == MATCH_YES))
2615 ts->kind = gfc_default_real_kind;
2620 && (strcmp ("doubleprecision", name) == 0
2621 || (strcmp ("double", name) == 0
2622 && gfc_match (" precision") == MATCH_YES)))
2623 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2626 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2627 "intrinsic-type-spec at %C") == FAILURE)
2629 if (matched_type && gfc_match_char (')') != MATCH_YES)
2633 ts->kind = gfc_default_double_kind;
2637 if ((matched_type && strcmp ("complex", name) == 0)
2638 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2640 ts->type = BT_COMPLEX;
2641 ts->kind = gfc_default_complex_kind;
2646 && (strcmp ("doublecomplex", name) == 0
2647 || (strcmp ("double", name) == 0
2648 && gfc_match (" complex") == MATCH_YES)))
2649 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2651 if (gfc_notify_std (GFC_STD_GNU, "Extension: DOUBLE COMPLEX at %C")
2656 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2657 "intrinsic-type-spec at %C") == FAILURE)
2660 if (matched_type && gfc_match_char (')') != MATCH_YES)
2663 ts->type = BT_COMPLEX;
2664 ts->kind = gfc_default_double_kind;
2668 if ((matched_type && strcmp ("logical", name) == 0)
2669 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2671 ts->type = BT_LOGICAL;
2672 ts->kind = gfc_default_logical_kind;
2677 m = gfc_match_char (')');
2680 ts->type = BT_DERIVED;
2683 /* Match CLASS declarations. */
2684 m = gfc_match (" class ( * )");
2685 if (m == MATCH_ERROR)
2687 else if (m == MATCH_YES)
2689 gfc_fatal_error ("Unlimited polymorphism at %C not yet supported");
2693 m = gfc_match (" class ( %n )", name);
2696 ts->type = BT_CLASS;
2698 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2703 /* Defer association of the derived type until the end of the
2704 specification block. However, if the derived type can be
2705 found, add it to the typespec. */
2706 if (gfc_matching_function)
2708 ts->u.derived = NULL;
2709 if (gfc_current_state () != COMP_INTERFACE
2710 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2712 sym = gfc_find_dt_in_generic (sym);
2713 ts->u.derived = sym;
2718 /* Search for the name but allow the components to be defined later. If
2719 type = -1, this typespec has been seen in a function declaration but
2720 the type could not be accessed at that point. The actual derived type is
2721 stored in a symtree with the first letter of the name captialized; the
2722 symtree with the all lower-case name contains the associated
2723 generic function. */
2724 dt_name = gfc_get_string ("%c%s",
2725 (char) TOUPPER ((unsigned char) name[0]),
2726 (const char*)&name[1]);
2731 gfc_get_ha_symbol (name, &sym);
2732 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2734 gfc_error ("Type name '%s' at %C is ambiguous", name);
2737 if (sym->generic && !dt_sym)
2738 dt_sym = gfc_find_dt_in_generic (sym);
2740 else if (ts->kind == -1)
2742 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2743 || gfc_current_ns->has_import_set;
2744 gfc_find_symbol (name, NULL, iface, &sym);
2745 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2747 gfc_error ("Type name '%s' at %C is ambiguous", name);
2750 if (sym && sym->generic && !dt_sym)
2751 dt_sym = gfc_find_dt_in_generic (sym);
2758 if ((sym->attr.flavor != FL_UNKNOWN
2759 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2760 || sym->attr.subroutine)
2762 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2763 "entity at %L, which has the same name", name,
2768 gfc_set_sym_referenced (sym);
2769 if (!sym->attr.generic
2770 && gfc_add_generic (&sym->attr, sym->name, NULL) == FAILURE)
2773 if (!sym->attr.function
2774 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2779 gfc_interface *intr, *head;
2781 /* Use upper case to save the actual derived-type symbol. */
2782 gfc_get_symbol (dt_name, NULL, &dt_sym);
2783 dt_sym->name = gfc_get_string (sym->name);
2784 head = sym->generic;
2785 intr = gfc_get_interface ();
2787 intr->where = gfc_current_locus;
2789 sym->generic = intr;
2790 sym->attr.if_source = IFSRC_DECL;
2793 gfc_set_sym_referenced (dt_sym);
2795 if (dt_sym->attr.flavor != FL_DERIVED
2796 && gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL)
2800 ts->u.derived = dt_sym;
2806 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2807 "intrinsic-type-spec at %C") == FAILURE)
2810 /* For all types except double, derived and character, look for an
2811 optional kind specifier. MATCH_NO is actually OK at this point. */
2812 if (implicit_flag == 1)
2814 if (matched_type && gfc_match_char (')') != MATCH_YES)
2820 if (gfc_current_form == FORM_FREE)
2822 c = gfc_peek_ascii_char ();
2823 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2824 && c != ':' && c != ',')
2826 if (matched_type && c == ')')
2828 gfc_next_ascii_char ();
2835 m = gfc_match_kind_spec (ts, false);
2836 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2837 m = gfc_match_old_kind_spec (ts);
2839 if (matched_type && gfc_match_char (')') != MATCH_YES)
2842 /* Defer association of the KIND expression of function results
2843 until after USE and IMPORT statements. */
2844 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2845 || gfc_matching_function)
2849 m = MATCH_YES; /* No kind specifier found. */
2855 /* Match an IMPLICIT NONE statement. Actually, this statement is
2856 already matched in parse.c, or we would not end up here in the
2857 first place. So the only thing we need to check, is if there is
2858 trailing garbage. If not, the match is successful. */
2861 gfc_match_implicit_none (void)
2863 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2867 /* Match the letter range(s) of an IMPLICIT statement. */
2870 match_implicit_range (void)
2876 cur_loc = gfc_current_locus;
2878 gfc_gobble_whitespace ();
2879 c = gfc_next_ascii_char ();
2882 gfc_error ("Missing character range in IMPLICIT at %C");
2889 gfc_gobble_whitespace ();
2890 c1 = gfc_next_ascii_char ();
2894 gfc_gobble_whitespace ();
2895 c = gfc_next_ascii_char ();
2900 inner = 0; /* Fall through. */
2907 gfc_gobble_whitespace ();
2908 c2 = gfc_next_ascii_char ();
2912 gfc_gobble_whitespace ();
2913 c = gfc_next_ascii_char ();
2915 if ((c != ',') && (c != ')'))
2928 gfc_error ("Letters must be in alphabetic order in "
2929 "IMPLICIT statement at %C");
2933 /* See if we can add the newly matched range to the pending
2934 implicits from this IMPLICIT statement. We do not check for
2935 conflicts with whatever earlier IMPLICIT statements may have
2936 set. This is done when we've successfully finished matching
2938 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2945 gfc_syntax_error (ST_IMPLICIT);
2947 gfc_current_locus = cur_loc;
2952 /* Match an IMPLICIT statement, storing the types for
2953 gfc_set_implicit() if the statement is accepted by the parser.
2954 There is a strange looking, but legal syntactic construction
2955 possible. It looks like:
2957 IMPLICIT INTEGER (a-b) (c-d)
2959 This is legal if "a-b" is a constant expression that happens to
2960 equal one of the legal kinds for integers. The real problem
2961 happens with an implicit specification that looks like:
2963 IMPLICIT INTEGER (a-b)
2965 In this case, a typespec matcher that is "greedy" (as most of the
2966 matchers are) gobbles the character range as a kindspec, leaving
2967 nothing left. We therefore have to go a bit more slowly in the
2968 matching process by inhibiting the kindspec checking during
2969 typespec matching and checking for a kind later. */
2972 gfc_match_implicit (void)
2981 /* We don't allow empty implicit statements. */
2982 if (gfc_match_eos () == MATCH_YES)
2984 gfc_error ("Empty IMPLICIT statement at %C");
2990 /* First cleanup. */
2991 gfc_clear_new_implicit ();
2993 /* A basic type is mandatory here. */
2994 m = gfc_match_decl_type_spec (&ts, 1);
2995 if (m == MATCH_ERROR)
3000 cur_loc = gfc_current_locus;
3001 m = match_implicit_range ();
3005 /* We may have <TYPE> (<RANGE>). */
3006 gfc_gobble_whitespace ();
3007 c = gfc_next_ascii_char ();
3008 if ((c == '\n') || (c == ','))
3010 /* Check for CHARACTER with no length parameter. */
3011 if (ts.type == BT_CHARACTER && !ts.u.cl)
3013 ts.kind = gfc_default_character_kind;
3014 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3015 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3019 /* Record the Successful match. */
3020 if (gfc_merge_new_implicit (&ts) != SUCCESS)
3025 gfc_current_locus = cur_loc;
3028 /* Discard the (incorrectly) matched range. */
3029 gfc_clear_new_implicit ();
3031 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3032 if (ts.type == BT_CHARACTER)
3033 m = gfc_match_char_spec (&ts);
3036 m = gfc_match_kind_spec (&ts, false);
3039 m = gfc_match_old_kind_spec (&ts);
3040 if (m == MATCH_ERROR)
3046 if (m == MATCH_ERROR)
3049 m = match_implicit_range ();
3050 if (m == MATCH_ERROR)
3055 gfc_gobble_whitespace ();
3056 c = gfc_next_ascii_char ();
3057 if ((c != '\n') && (c != ','))
3060 if (gfc_merge_new_implicit (&ts) != SUCCESS)
3068 gfc_syntax_error (ST_IMPLICIT);
3076 gfc_match_import (void)
3078 char name[GFC_MAX_SYMBOL_LEN + 1];
3083 if (gfc_current_ns->proc_name == NULL
3084 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3086 gfc_error ("IMPORT statement at %C only permitted in "
3087 "an INTERFACE body");
3091 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
3095 if (gfc_match_eos () == MATCH_YES)
3097 /* All host variables should be imported. */
3098 gfc_current_ns->has_import_set = 1;
3102 if (gfc_match (" ::") == MATCH_YES)
3104 if (gfc_match_eos () == MATCH_YES)
3106 gfc_error ("Expecting list of named entities at %C");
3114 m = gfc_match (" %n", name);
3118 if (gfc_current_ns->parent != NULL
3119 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3121 gfc_error ("Type name '%s' at %C is ambiguous", name);
3124 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3125 && gfc_find_symbol (name,
3126 gfc_current_ns->proc_name->ns->parent,
3129 gfc_error ("Type name '%s' at %C is ambiguous", name);
3135 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3136 "at %C - does not exist.", name);
3140 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3142 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3147 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3150 sym->attr.imported = 1;
3152 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3154 /* The actual derived type is stored in a symtree with the first
3155 letter of the name captialized; the symtree with the all
3156 lower-case name contains the associated generic function. */
3157 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3158 gfc_get_string ("%c%s",
3159 (char) TOUPPER ((unsigned char) name[0]),
3163 sym->attr.imported = 1;
3176 if (gfc_match_eos () == MATCH_YES)
3178 if (gfc_match_char (',') != MATCH_YES)
3185 gfc_error ("Syntax error in IMPORT statement at %C");
3190 /* A minimal implementation of gfc_match without whitespace, escape
3191 characters or variable arguments. Returns true if the next
3192 characters match the TARGET template exactly. */
3195 match_string_p (const char *target)
3199 for (p = target; *p; p++)
3200 if ((char) gfc_next_ascii_char () != *p)
3205 /* Matches an attribute specification including array specs. If
3206 successful, leaves the variables current_attr and current_as
3207 holding the specification. Also sets the colon_seen variable for
3208 later use by matchers associated with initializations.
3210 This subroutine is a little tricky in the sense that we don't know
3211 if we really have an attr-spec until we hit the double colon.
3212 Until that time, we can only return MATCH_NO. This forces us to
3213 check for duplicate specification at this level. */
3216 match_attr_spec (void)
3218 /* Modifiers that can exist in a type statement. */
3220 { GFC_DECL_BEGIN = 0,
3221 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3222 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3223 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3224 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3225 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3226 DECL_NONE, GFC_DECL_END /* Sentinel */
3230 /* GFC_DECL_END is the sentinel, index starts at 0. */
3231 #define NUM_DECL GFC_DECL_END
3233 locus start, seen_at[NUM_DECL];
3240 gfc_clear_attr (¤t_attr);
3241 start = gfc_current_locus;
3246 /* See if we get all of the keywords up to the final double colon. */
3247 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3255 gfc_gobble_whitespace ();
3257 ch = gfc_next_ascii_char ();
3260 /* This is the successful exit condition for the loop. */
3261 if (gfc_next_ascii_char () == ':')
3266 gfc_gobble_whitespace ();
3267 switch (gfc_peek_ascii_char ())
3270 gfc_next_ascii_char ();
3271 switch (gfc_next_ascii_char ())
3274 if (match_string_p ("locatable"))
3276 /* Matched "allocatable". */
3277 d = DECL_ALLOCATABLE;
3282 if (match_string_p ("ynchronous"))
3284 /* Matched "asynchronous". */
3285 d = DECL_ASYNCHRONOUS;
3292 /* Try and match the bind(c). */
3293 m = gfc_match_bind_c (NULL, true);
3296 else if (m == MATCH_ERROR)
3301 gfc_next_ascii_char ();
3302 if ('o' != gfc_next_ascii_char ())
3304 switch (gfc_next_ascii_char ())
3307 if (match_string_p ("imension"))
3309 d = DECL_CODIMENSION;
3313 if (match_string_p ("tiguous"))
3315 d = DECL_CONTIGUOUS;
3322 if (match_string_p ("dimension"))
3327 if (match_string_p ("external"))
3332 if (match_string_p ("int"))
3334 ch = gfc_next_ascii_char ();
3337 if (match_string_p ("nt"))
3339 /* Matched "intent". */
3340 /* TODO: Call match_intent_spec from here. */
3341 if (gfc_match (" ( in out )") == MATCH_YES)
3343 else if (gfc_match (" ( in )") == MATCH_YES)
3345 else if (gfc_match (" ( out )") == MATCH_YES)
3351 if (match_string_p ("insic"))
3353 /* Matched "intrinsic". */
3361 if (match_string_p ("optional"))
3366 gfc_next_ascii_char ();
3367 switch (gfc_next_ascii_char ())
3370 if (match_string_p ("rameter"))
3372 /* Matched "parameter". */
3378 if (match_string_p ("inter"))
3380 /* Matched "pointer". */
3386 ch = gfc_next_ascii_char ();
3389 if (match_string_p ("vate"))
3391 /* Matched "private". */
3397 if (match_string_p ("tected"))
3399 /* Matched "protected". */
3406 if (match_string_p ("blic"))
3408 /* Matched "public". */
3416 if (match_string_p ("save"))
3421 if (match_string_p ("target"))
3426 gfc_next_ascii_char ();
3427 ch = gfc_next_ascii_char ();
3430 if (match_string_p ("lue"))
3432 /* Matched "value". */
3438 if (match_string_p ("latile"))
3440 /* Matched "volatile". */
3448 /* No double colon and no recognizable decl_type, so assume that
3449 we've been looking at something else the whole time. */
3456 /* Check to make sure any parens are paired up correctly. */
3457 if (gfc_match_parens () == MATCH_ERROR)
3464 seen_at[d] = gfc_current_locus;
3466 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3468 gfc_array_spec *as = NULL;
3470 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3471 d == DECL_CODIMENSION);
3473 if (current_as == NULL)
3475 else if (m == MATCH_YES)
3477 merge_array_spec (as, current_as, false);
3483 if (d == DECL_CODIMENSION)
3484 gfc_error ("Missing codimension specification at %C");
3486 gfc_error ("Missing dimension specification at %C");
3490 if (m == MATCH_ERROR)
3495 /* Since we've seen a double colon, we have to be looking at an
3496 attr-spec. This means that we can now issue errors. */
3497 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3502 case DECL_ALLOCATABLE:
3503 attr = "ALLOCATABLE";
3505 case DECL_ASYNCHRONOUS:
3506 attr = "ASYNCHRONOUS";
3508 case DECL_CODIMENSION:
3509 attr = "CODIMENSION";
3511 case DECL_CONTIGUOUS:
3512 attr = "CONTIGUOUS";