1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007
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 2, 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 COPYING. If not, write to the Free
20 Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA
29 /* This flag is set if an old-style length selector is matched
30 during a type-declaration statement. */
32 static int old_char_selector;
34 /* When variables acquire types and attributes from a declaration
35 statement, they get them from the following static variables. The
36 first part of a declaration sets these variables and the second
37 part copies these into symbol structures. */
39 static gfc_typespec current_ts;
41 static symbol_attribute current_attr;
42 static gfc_array_spec *current_as;
43 static int colon_seen;
45 /* Initializer of the previous enumerator. */
47 static gfc_expr *last_initializer;
49 /* History of all the enumerators is maintained, so that
50 kind values of all the enumerators could be updated depending
51 upon the maximum initialized value. */
53 typedef struct enumerator_history
56 gfc_expr *initializer;
57 struct enumerator_history *next;
61 /* Header of enum history chain. */
63 static enumerator_history *enum_history = NULL;
65 /* Pointer of enum history node containing largest initializer. */
67 static enumerator_history *max_enum = NULL;
69 /* gfc_new_block points to the symbol of a newly matched block. */
71 gfc_symbol *gfc_new_block;
74 /********************* DATA statement subroutines *********************/
76 static bool in_match_data = false;
79 gfc_in_match_data (void)
85 gfc_set_in_match_data (bool set_value)
87 in_match_data = set_value;
90 /* Free a gfc_data_variable structure and everything beneath it. */
93 free_variable (gfc_data_variable *p)
100 gfc_free_expr (p->expr);
101 gfc_free_iterator (&p->iter, 0);
102 free_variable (p->list);
108 /* Free a gfc_data_value structure and everything beneath it. */
111 free_value (gfc_data_value *p)
118 gfc_free_expr (p->expr);
124 /* Free a list of gfc_data structures. */
127 gfc_free_data (gfc_data *p)
134 free_variable (p->var);
135 free_value (p->value);
141 /* Free all data in a namespace. */
144 gfc_free_data_all (gfc_namespace *ns)
157 static match var_element (gfc_data_variable *);
159 /* Match a list of variables terminated by an iterator and a right
163 var_list (gfc_data_variable *parent)
165 gfc_data_variable *tail, var;
168 m = var_element (&var);
169 if (m == MATCH_ERROR)
174 tail = gfc_get_data_variable ();
181 if (gfc_match_char (',') != MATCH_YES)
184 m = gfc_match_iterator (&parent->iter, 1);
187 if (m == MATCH_ERROR)
190 m = var_element (&var);
191 if (m == MATCH_ERROR)
196 tail->next = gfc_get_data_variable ();
202 if (gfc_match_char (')') != MATCH_YES)
207 gfc_syntax_error (ST_DATA);
212 /* Match a single element in a data variable list, which can be a
213 variable-iterator list. */
216 var_element (gfc_data_variable *new)
221 memset (new, 0, sizeof (gfc_data_variable));
223 if (gfc_match_char ('(') == MATCH_YES)
224 return var_list (new);
226 m = gfc_match_variable (&new->expr, 0);
230 sym = new->expr->symtree->n.sym;
232 if (!sym->attr.function && gfc_current_ns->parent
233 && gfc_current_ns->parent == sym->ns)
235 gfc_error ("Host associated variable '%s' may not be in the DATA "
236 "statement at %C", sym->name);
240 if (gfc_current_state () != COMP_BLOCK_DATA
241 && sym->attr.in_common
242 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
243 "common block variable '%s' in DATA statement at %C",
244 sym->name) == FAILURE)
247 if (gfc_add_data (&sym->attr, sym->name, &new->expr->where) == FAILURE)
254 /* Match the top-level list of data variables. */
257 top_var_list (gfc_data *d)
259 gfc_data_variable var, *tail, *new;
266 m = var_element (&var);
269 if (m == MATCH_ERROR)
272 new = gfc_get_data_variable ();
282 if (gfc_match_char ('/') == MATCH_YES)
284 if (gfc_match_char (',') != MATCH_YES)
291 gfc_syntax_error (ST_DATA);
292 gfc_free_data_all (gfc_current_ns);
298 match_data_constant (gfc_expr **result)
300 char name[GFC_MAX_SYMBOL_LEN + 1];
306 m = gfc_match_literal_constant (&expr, 1);
313 if (m == MATCH_ERROR)
316 m = gfc_match_null (result);
320 old_loc = gfc_current_locus;
322 /* Should this be a structure component, try to match it
323 before matching a name. */
324 m = gfc_match_rvalue (result);
325 if (m == MATCH_ERROR)
328 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
330 if (gfc_simplify_expr (*result, 0) == FAILURE)
335 gfc_current_locus = old_loc;
337 m = gfc_match_name (name);
341 if (gfc_find_symbol (name, NULL, 1, &sym))
345 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
347 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
351 else if (sym->attr.flavor == FL_DERIVED)
352 return gfc_match_structure_constructor (sym, result);
354 *result = gfc_copy_expr (sym->value);
359 /* Match a list of values in a DATA statement. The leading '/' has
360 already been seen at this point. */
363 top_val_list (gfc_data *data)
365 gfc_data_value *new, *tail;
374 m = match_data_constant (&expr);
377 if (m == MATCH_ERROR)
380 new = gfc_get_data_value ();
389 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
397 msg = gfc_extract_int (expr, &tmp);
398 gfc_free_expr (expr);
406 m = match_data_constant (&tail->expr);
409 if (m == MATCH_ERROR)
413 if (gfc_match_char ('/') == MATCH_YES)
415 if (gfc_match_char (',') == MATCH_NO)
422 gfc_syntax_error (ST_DATA);
423 gfc_free_data_all (gfc_current_ns);
428 /* Matches an old style initialization. */
431 match_old_style_init (const char *name)
438 /* Set up data structure to hold initializers. */
439 gfc_find_sym_tree (name, NULL, 0, &st);
442 newdata = gfc_get_data ();
443 newdata->var = gfc_get_data_variable ();
444 newdata->var->expr = gfc_get_variable_expr (st);
445 newdata->where = gfc_current_locus;
447 /* Match initial value list. This also eats the terminal '/'. */
448 m = top_val_list (newdata);
457 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
462 /* Mark the variable as having appeared in a data statement. */
463 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
469 /* Chain in namespace list of DATA initializers. */
470 newdata->next = gfc_current_ns->data;
471 gfc_current_ns->data = newdata;
477 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
478 we are matching a DATA statement and are therefore issuing an error
479 if we encounter something unexpected, if not, we're trying to match
480 an old-style initialization expression of the form INTEGER I /2/. */
483 gfc_match_data (void)
488 gfc_set_in_match_data (true);
492 new = gfc_get_data ();
493 new->where = gfc_current_locus;
495 m = top_var_list (new);
499 m = top_val_list (new);
503 new->next = gfc_current_ns->data;
504 gfc_current_ns->data = new;
506 if (gfc_match_eos () == MATCH_YES)
509 gfc_match_char (','); /* Optional comma */
512 gfc_set_in_match_data (false);
516 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
523 gfc_set_in_match_data (false);
529 /************************ Declaration statements *********************/
531 /* Match an intent specification. Since this can only happen after an
532 INTENT word, a legal intent-spec must follow. */
535 match_intent_spec (void)
538 if (gfc_match (" ( in out )") == MATCH_YES)
540 if (gfc_match (" ( in )") == MATCH_YES)
542 if (gfc_match (" ( out )") == MATCH_YES)
545 gfc_error ("Bad INTENT specification at %C");
546 return INTENT_UNKNOWN;
550 /* Matches a character length specification, which is either a
551 specification expression or a '*'. */
554 char_len_param_value (gfc_expr **expr)
556 if (gfc_match_char ('*') == MATCH_YES)
562 return gfc_match_expr (expr);
566 /* A character length is a '*' followed by a literal integer or a
567 char_len_param_value in parenthesis. */
570 match_char_length (gfc_expr **expr)
575 m = gfc_match_char ('*');
579 m = gfc_match_small_literal_int (&length, NULL);
580 if (m == MATCH_ERROR)
585 *expr = gfc_int_expr (length);
589 if (gfc_match_char ('(') == MATCH_NO)
592 m = char_len_param_value (expr);
593 if (m == MATCH_ERROR)
598 if (gfc_match_char (')') == MATCH_NO)
600 gfc_free_expr (*expr);
608 gfc_error ("Syntax error in character length specification at %C");
613 /* Special subroutine for finding a symbol. Check if the name is found
614 in the current name space. If not, and we're compiling a function or
615 subroutine and the parent compilation unit is an interface, then check
616 to see if the name we've been given is the name of the interface
617 (located in another namespace). */
620 find_special (const char *name, gfc_symbol **result)
625 i = gfc_get_symbol (name, NULL, result);
629 if (gfc_current_state () != COMP_SUBROUTINE
630 && gfc_current_state () != COMP_FUNCTION)
633 s = gfc_state_stack->previous;
637 if (s->state != COMP_INTERFACE)
640 goto end; /* Nameless interface. */
642 if (strcmp (name, s->sym->name) == 0)
653 /* Special subroutine for getting a symbol node associated with a
654 procedure name, used in SUBROUTINE and FUNCTION statements. The
655 symbol is created in the parent using with symtree node in the
656 child unit pointing to the symbol. If the current namespace has no
657 parent, then the symbol is just created in the current unit. */
660 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
666 /* Module functions have to be left in their own namespace because
667 they have potentially (almost certainly!) already been referenced.
668 In this sense, they are rather like external functions. This is
669 fixed up in resolve.c(resolve_entries), where the symbol name-
670 space is set to point to the master function, so that the fake
671 result mechanism can work. */
672 if (module_fcn_entry)
674 /* Present if entry is declared to be a module procedure. */
675 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
677 rc = gfc_get_symbol (name, NULL, result);
680 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
683 gfc_current_ns->refs++;
685 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
687 /* Trap another encompassed procedure with the same name. All
688 these conditions are necessary to avoid picking up an entry
689 whose name clashes with that of the encompassing procedure;
690 this is handled using gsymbols to register unique,globally
692 if (sym->attr.flavor != 0
693 && sym->attr.proc != 0
694 && (sym->attr.subroutine || sym->attr.function)
695 && sym->attr.if_source != IFSRC_UNKNOWN)
696 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
697 name, &sym->declared_at);
699 /* Trap declarations of attributes in encompassing scope. The
700 signature for this is that ts.kind is set. Legitimate
701 references only set ts.type. */
702 if (sym->ts.kind != 0
703 && !sym->attr.implicit_type
704 && sym->attr.proc == 0
705 && gfc_current_ns->parent != NULL
706 && sym->attr.access == 0
707 && !module_fcn_entry)
708 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
709 "and must not have attributes declared at %L",
710 name, &sym->declared_at);
713 if (gfc_current_ns->parent == NULL || *result == NULL)
716 /* Module function entries will already have a symtree in
717 the current namespace but will need one at module level. */
718 if (module_fcn_entry)
720 /* Present if entry is declared to be a module procedure. */
721 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
723 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
726 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
731 /* See if the procedure should be a module procedure. */
733 if (((sym->ns->proc_name != NULL
734 && sym->ns->proc_name->attr.flavor == FL_MODULE
735 && sym->attr.proc != PROC_MODULE)
736 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
737 && gfc_add_procedure (&sym->attr, PROC_MODULE,
738 sym->name, NULL) == FAILURE)
745 /* Function called by variable_decl() that adds a name to the symbol
749 build_sym (const char *name, gfc_charlen *cl,
750 gfc_array_spec **as, locus *var_locus)
752 symbol_attribute attr;
755 if (gfc_get_symbol (name, NULL, &sym))
758 /* Start updating the symbol table. Add basic type attribute if present. */
759 if (current_ts.type != BT_UNKNOWN
760 && (sym->attr.implicit_type == 0
761 || !gfc_compare_types (&sym->ts, ¤t_ts))
762 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
765 if (sym->ts.type == BT_CHARACTER)
768 /* Add dimension attribute if present. */
769 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
773 /* Add attribute to symbol. The copy is so that we can reset the
774 dimension attribute. */
778 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
781 sym->attr.implied_index = 0;
787 /* Set character constant to the given length. The constant will be padded or
791 gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
796 gcc_assert (expr->expr_type == EXPR_CONSTANT);
797 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
799 slen = expr->value.character.length;
802 s = gfc_getmem (len + 1);
803 memcpy (s, expr->value.character.string, MIN (len, slen));
805 memset (&s[slen], ' ', len - slen);
807 if (gfc_option.warn_character_truncation && slen > len)
808 gfc_warning_now ("CHARACTER expression at %L is being truncated "
809 "(%d/%d)", &expr->where, slen, len);
811 /* Apply the standard by 'hand' otherwise it gets cleared for
813 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
814 gfc_error_now ("The CHARACTER elements of the array constructor "
815 "at %L must have the same length (%d/%d)",
816 &expr->where, slen, len);
819 gfc_free (expr->value.character.string);
820 expr->value.character.string = s;
821 expr->value.character.length = len;
826 /* Function to create and update the enumerator history
827 using the information passed as arguments.
828 Pointer "max_enum" is also updated, to point to
829 enum history node containing largest initializer.
831 SYM points to the symbol node of enumerator.
832 INIT points to its enumerator value. */
835 create_enum_history (gfc_symbol *sym, gfc_expr *init)
837 enumerator_history *new_enum_history;
838 gcc_assert (sym != NULL && init != NULL);
840 new_enum_history = gfc_getmem (sizeof (enumerator_history));
842 new_enum_history->sym = sym;
843 new_enum_history->initializer = init;
844 new_enum_history->next = NULL;
846 if (enum_history == NULL)
848 enum_history = new_enum_history;
849 max_enum = enum_history;
853 new_enum_history->next = enum_history;
854 enum_history = new_enum_history;
856 if (mpz_cmp (max_enum->initializer->value.integer,
857 new_enum_history->initializer->value.integer) < 0)
858 max_enum = new_enum_history;
863 /* Function to free enum kind history. */
866 gfc_free_enum_history (void)
868 enumerator_history *current = enum_history;
869 enumerator_history *next;
871 while (current != NULL)
873 next = current->next;
882 /* Function called by variable_decl() that adds an initialization
883 expression to a symbol. */
886 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
888 symbol_attribute attr;
893 if (find_special (name, &sym))
898 /* If this symbol is confirming an implicit parameter type,
899 then an initialization expression is not allowed. */
900 if (attr.flavor == FL_PARAMETER
901 && sym->value != NULL
904 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
913 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
920 /* An initializer is required for PARAMETER declarations. */
921 if (attr.flavor == FL_PARAMETER)
923 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
929 /* If a variable appears in a DATA block, it cannot have an
933 gfc_error ("Variable '%s' at %C with an initializer already "
934 "appears in a DATA statement", sym->name);
938 /* Check if the assignment can happen. This has to be put off
939 until later for a derived type variable. */
940 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
941 && gfc_check_assign_symbol (sym, init) == FAILURE)
944 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
946 /* Update symbol character length according initializer. */
947 if (sym->ts.cl->length == NULL)
949 /* If there are multiple CHARACTER variables declared on the
950 same line, we don't want them to share the same length. */
951 sym->ts.cl = gfc_get_charlen ();
952 sym->ts.cl->next = gfc_current_ns->cl_list;
953 gfc_current_ns->cl_list = sym->ts.cl;
955 if (sym->attr.flavor == FL_PARAMETER
956 && init->expr_type == EXPR_ARRAY)
957 sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
959 /* Update initializer character length according symbol. */
960 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
962 int len = mpz_get_si (sym->ts.cl->length->value.integer);
965 if (init->expr_type == EXPR_CONSTANT)
966 gfc_set_constant_character_len (len, init, false);
967 else if (init->expr_type == EXPR_ARRAY)
969 /* Build a new charlen to prevent simplification from
970 deleting the length before it is resolved. */
971 init->ts.cl = gfc_get_charlen ();
972 init->ts.cl->next = gfc_current_ns->cl_list;
973 gfc_current_ns->cl_list = sym->ts.cl;
974 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
976 for (p = init->value.constructor; p; p = p->next)
977 gfc_set_constant_character_len (len, p->expr, false);
982 /* Add initializer. Make sure we keep the ranks sane. */
983 if (sym->attr.dimension && init->rank == 0)
989 if (sym->attr.flavor == FL_PARAMETER
990 && init->expr_type == EXPR_CONSTANT
991 && spec_size (sym->as, &size) == SUCCESS
992 && mpz_cmp_si (size, 0) > 0)
994 array = gfc_start_constructor (init->ts.type, init->ts.kind,
997 array->value.constructor = c = NULL;
998 for (n = 0; n < (int)mpz_get_si (size); n++)
1000 if (array->value.constructor == NULL)
1002 array->value.constructor = c = gfc_get_constructor ();
1007 c->next = gfc_get_constructor ();
1009 c->expr = gfc_copy_expr (init);
1013 array->shape = gfc_get_shape (sym->as->rank);
1014 for (n = 0; n < sym->as->rank; n++)
1015 spec_dimen_size (sym->as, n, &array->shape[n]);
1020 init->rank = sym->as->rank;
1031 /* Function called by variable_decl() that adds a name to a structure
1035 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1036 gfc_array_spec **as)
1040 /* If the current symbol is of the same derived type that we're
1041 constructing, it must have the pointer attribute. */
1042 if (current_ts.type == BT_DERIVED
1043 && current_ts.derived == gfc_current_block ()
1044 && current_attr.pointer == 0)
1046 gfc_error ("Component at %C must have the POINTER attribute");
1050 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1052 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1054 gfc_error ("Array component of structure at %C must have explicit "
1055 "or deferred shape");
1060 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1065 gfc_set_component_attr (c, ¤t_attr);
1067 c->initializer = *init;
1075 /* Check array components. */
1080 gfc_error ("Allocatable component at %C must be an array");
1089 if (c->as->type != AS_DEFERRED)
1091 gfc_error ("Pointer array component of structure at %C must have a "
1096 else if (c->allocatable)
1098 if (c->as->type != AS_DEFERRED)
1100 gfc_error ("Allocatable component of structure at %C must have a "
1107 if (c->as->type != AS_EXPLICIT)
1109 gfc_error ("Array component of structure at %C must have an "
1119 /* Match a 'NULL()', and possibly take care of some side effects. */
1122 gfc_match_null (gfc_expr **result)
1128 m = gfc_match (" null ( )");
1132 /* The NULL symbol now has to be/become an intrinsic function. */
1133 if (gfc_get_symbol ("null", NULL, &sym))
1135 gfc_error ("NULL() initialization at %C is ambiguous");
1139 gfc_intrinsic_symbol (sym);
1141 if (sym->attr.proc != PROC_INTRINSIC
1142 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1143 sym->name, NULL) == FAILURE
1144 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1147 e = gfc_get_expr ();
1148 e->where = gfc_current_locus;
1149 e->expr_type = EXPR_NULL;
1150 e->ts.type = BT_UNKNOWN;
1158 /* Match a variable name with an optional initializer. When this
1159 subroutine is called, a variable is expected to be parsed next.
1160 Depending on what is happening at the moment, updates either the
1161 symbol table or the current interface. */
1164 variable_decl (int elem)
1166 char name[GFC_MAX_SYMBOL_LEN + 1];
1167 gfc_expr *initializer, *char_len;
1169 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1180 old_locus = gfc_current_locus;
1182 /* When we get here, we've just matched a list of attributes and
1183 maybe a type and a double colon. The next thing we expect to see
1184 is the name of the symbol. */
1185 m = gfc_match_name (name);
1189 var_locus = gfc_current_locus;
1191 /* Now we could see the optional array spec. or character length. */
1192 m = gfc_match_array_spec (&as);
1193 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1194 cp_as = gfc_copy_array_spec (as);
1195 else if (m == MATCH_ERROR)
1199 as = gfc_copy_array_spec (current_as);
1204 if (current_ts.type == BT_CHARACTER)
1206 switch (match_char_length (&char_len))
1209 cl = gfc_get_charlen ();
1210 cl->next = gfc_current_ns->cl_list;
1211 gfc_current_ns->cl_list = cl;
1213 cl->length = char_len;
1216 /* Non-constant lengths need to be copied after the first
1219 if (elem > 1 && current_ts.cl->length
1220 && current_ts.cl->length->expr_type != EXPR_CONSTANT)
1222 cl = gfc_get_charlen ();
1223 cl->next = gfc_current_ns->cl_list;
1224 gfc_current_ns->cl_list = cl;
1225 cl->length = gfc_copy_expr (current_ts.cl->length);
1237 /* If this symbol has already shown up in a Cray Pointer declaration,
1238 then we want to set the type & bail out. */
1239 if (gfc_option.flag_cray_pointer)
1241 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1242 if (sym != NULL && sym->attr.cray_pointee)
1244 sym->ts.type = current_ts.type;
1245 sym->ts.kind = current_ts.kind;
1247 sym->ts.derived = current_ts.derived;
1250 /* Check to see if we have an array specification. */
1253 if (sym->as != NULL)
1255 gfc_error ("Duplicate array spec for Cray pointee at %C");
1256 gfc_free_array_spec (cp_as);
1262 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1263 gfc_internal_error ("Couldn't set pointee array spec.");
1265 /* Fix the array spec. */
1266 m = gfc_mod_pointee_as (sym->as);
1267 if (m == MATCH_ERROR)
1275 gfc_free_array_spec (cp_as);
1280 /* OK, we've successfully matched the declaration. Now put the
1281 symbol in the current namespace, because it might be used in the
1282 optional initialization expression for this symbol, e.g. this is
1285 integer, parameter :: i = huge(i)
1287 This is only true for parameters or variables of a basic type.
1288 For components of derived types, it is not true, so we don't
1289 create a symbol for those yet. If we fail to create the symbol,
1291 if (gfc_current_state () != COMP_DERIVED
1292 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1298 /* An interface body specifies all of the procedure's
1299 characteristics and these shall be consistent with those
1300 specified in the procedure definition, except that the interface
1301 may specify a procedure that is not pure if the procedure is
1302 defined to be pure(12.3.2). */
1303 if (current_ts.type == BT_DERIVED
1304 && gfc_current_ns->proc_name
1305 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1306 && current_ts.derived->ns != gfc_current_ns
1307 && !gfc_current_ns->has_import_set)
1309 gfc_error ("the type of '%s' at %C has not been declared within the "
1315 /* In functions that have a RESULT variable defined, the function
1316 name always refers to function calls. Therefore, the name is
1317 not allowed to appear in specification statements. */
1318 if (gfc_current_state () == COMP_FUNCTION
1319 && gfc_current_block () != NULL
1320 && gfc_current_block ()->result != NULL
1321 && gfc_current_block ()->result != gfc_current_block ()
1322 && strcmp (gfc_current_block ()->name, name) == 0)
1324 gfc_error ("Function name '%s' not allowed at %C", name);
1329 /* We allow old-style initializations of the form
1330 integer i /2/, j(4) /3*3, 1/
1331 (if no colon has been seen). These are different from data
1332 statements in that initializers are only allowed to apply to the
1333 variable immediately preceding, i.e.
1335 is not allowed. Therefore we have to do some work manually, that
1336 could otherwise be left to the matchers for DATA statements. */
1338 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1340 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1341 "initialization at %C") == FAILURE)
1344 return match_old_style_init (name);
1347 /* The double colon must be present in order to have initializers.
1348 Otherwise the statement is ambiguous with an assignment statement. */
1351 if (gfc_match (" =>") == MATCH_YES)
1353 if (!current_attr.pointer)
1355 gfc_error ("Initialization at %C isn't for a pointer variable");
1360 m = gfc_match_null (&initializer);
1363 gfc_error ("Pointer initialization requires a NULL() at %C");
1367 if (gfc_pure (NULL))
1369 gfc_error ("Initialization of pointer at %C is not allowed in "
1370 "a PURE procedure");
1378 else if (gfc_match_char ('=') == MATCH_YES)
1380 if (current_attr.pointer)
1382 gfc_error ("Pointer initialization at %C requires '=>', "
1388 m = gfc_match_init_expr (&initializer);
1391 gfc_error ("Expected an initialization expression at %C");
1395 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1397 gfc_error ("Initialization of variable at %C is not allowed in "
1398 "a PURE procedure");
1407 if (initializer != NULL && current_attr.allocatable
1408 && gfc_current_state () == COMP_DERIVED)
1410 gfc_error ("Initialization of allocatable component at %C is not "
1416 /* Add the initializer. Note that it is fine if initializer is
1417 NULL here, because we sometimes also need to check if a
1418 declaration *must* have an initialization expression. */
1419 if (gfc_current_state () != COMP_DERIVED)
1420 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1423 if (current_ts.type == BT_DERIVED
1424 && !current_attr.pointer && !initializer)
1425 initializer = gfc_default_initializer (¤t_ts);
1426 t = build_struct (name, cl, &initializer, &as);
1429 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1432 /* Free stuff up and return. */
1433 gfc_free_expr (initializer);
1434 gfc_free_array_spec (as);
1440 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1441 This assumes that the byte size is equal to the kind number for
1442 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1445 gfc_match_old_kind_spec (gfc_typespec *ts)
1450 if (gfc_match_char ('*') != MATCH_YES)
1453 m = gfc_match_small_literal_int (&ts->kind, NULL);
1457 original_kind = ts->kind;
1459 /* Massage the kind numbers for complex types. */
1460 if (ts->type == BT_COMPLEX)
1464 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1465 gfc_basic_typename (ts->type), original_kind);
1471 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1473 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1474 gfc_basic_typename (ts->type), original_kind);
1478 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1479 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1486 /* Match a kind specification. Since kinds are generally optional, we
1487 usually return MATCH_NO if something goes wrong. If a "kind="
1488 string is found, then we know we have an error. */
1491 gfc_match_kind_spec (gfc_typespec *ts)
1501 where = gfc_current_locus;
1503 if (gfc_match_char ('(') == MATCH_NO)
1506 /* Also gobbles optional text. */
1507 if (gfc_match (" kind = ") == MATCH_YES)
1510 n = gfc_match_init_expr (&e);
1512 gfc_error ("Expected initialization expression at %C");
1518 gfc_error ("Expected scalar initialization expression at %C");
1523 msg = gfc_extract_int (e, &ts->kind);
1534 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1536 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1537 gfc_basic_typename (ts->type));
1543 if (gfc_match_char (')') != MATCH_YES)
1545 gfc_error ("Missing right parenthesis at %C");
1553 gfc_current_locus = where;
1558 /* Match the various kind/length specifications in a CHARACTER
1559 declaration. We don't return MATCH_NO. */
1562 match_char_spec (gfc_typespec *ts)
1564 int kind, seen_length;
1569 kind = gfc_default_character_kind;
1573 /* Try the old-style specification first. */
1574 old_char_selector = 0;
1576 m = match_char_length (&len);
1580 old_char_selector = 1;
1585 m = gfc_match_char ('(');
1588 m = MATCH_YES; /* character without length is a single char */
1592 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ) */
1593 if (gfc_match (" kind =") == MATCH_YES)
1595 m = gfc_match_small_int (&kind);
1596 if (m == MATCH_ERROR)
1601 if (gfc_match (" , len =") == MATCH_NO)
1604 m = char_len_param_value (&len);
1607 if (m == MATCH_ERROR)
1614 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
1615 if (gfc_match (" len =") == MATCH_YES)
1617 m = char_len_param_value (&len);
1620 if (m == MATCH_ERROR)
1624 if (gfc_match_char (')') == MATCH_YES)
1627 if (gfc_match (" , kind =") != MATCH_YES)
1630 gfc_match_small_int (&kind);
1632 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1634 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1641 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
1642 m = char_len_param_value (&len);
1645 if (m == MATCH_ERROR)
1649 m = gfc_match_char (')');
1653 if (gfc_match_char (',') != MATCH_YES)
1656 gfc_match (" kind ="); /* Gobble optional text */
1658 m = gfc_match_small_int (&kind);
1659 if (m == MATCH_ERROR)
1665 /* Require a right-paren at this point. */
1666 m = gfc_match_char (')');
1671 gfc_error ("Syntax error in CHARACTER declaration at %C");
1673 gfc_free_expr (len);
1677 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1679 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1683 if (seen_length == 1 && len != NULL
1684 && len->ts.type != BT_INTEGER && len->ts.type != BT_UNKNOWN)
1686 gfc_error ("Expression at %C must be of INTEGER type");
1692 gfc_free_expr (len);
1696 /* Do some final massaging of the length values. */
1697 cl = gfc_get_charlen ();
1698 cl->next = gfc_current_ns->cl_list;
1699 gfc_current_ns->cl_list = cl;
1701 if (seen_length == 0)
1702 cl->length = gfc_int_expr (1);
1713 /* Matches a type specification. If successful, sets the ts structure
1714 to the matched specification. This is necessary for FUNCTION and
1715 IMPLICIT statements.
1717 If implicit_flag is nonzero, then we don't check for the optional
1718 kind specification. Not doing so is needed for matching an IMPLICIT
1719 statement correctly. */
1722 match_type_spec (gfc_typespec *ts, int implicit_flag)
1724 char name[GFC_MAX_SYMBOL_LEN + 1];
1731 if (gfc_match (" byte") == MATCH_YES)
1733 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
1737 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
1739 gfc_error ("BYTE type used at %C "
1740 "is not available on the target machine");
1744 ts->type = BT_INTEGER;
1749 if (gfc_match (" integer") == MATCH_YES)
1751 ts->type = BT_INTEGER;
1752 ts->kind = gfc_default_integer_kind;
1756 if (gfc_match (" character") == MATCH_YES)
1758 ts->type = BT_CHARACTER;
1759 if (implicit_flag == 0)
1760 return match_char_spec (ts);
1765 if (gfc_match (" real") == MATCH_YES)
1768 ts->kind = gfc_default_real_kind;
1772 if (gfc_match (" double precision") == MATCH_YES)
1775 ts->kind = gfc_default_double_kind;
1779 if (gfc_match (" complex") == MATCH_YES)
1781 ts->type = BT_COMPLEX;
1782 ts->kind = gfc_default_complex_kind;
1786 if (gfc_match (" double complex") == MATCH_YES)
1788 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
1789 "conform to the Fortran 95 standard") == FAILURE)
1792 ts->type = BT_COMPLEX;
1793 ts->kind = gfc_default_double_kind;
1797 if (gfc_match (" logical") == MATCH_YES)
1799 ts->type = BT_LOGICAL;
1800 ts->kind = gfc_default_logical_kind;
1804 m = gfc_match (" type ( %n )", name);
1808 /* Search for the name but allow the components to be defined later. */
1809 if (gfc_get_ha_symbol (name, &sym))
1811 gfc_error ("Type name '%s' at %C is ambiguous", name);
1815 if (sym->attr.flavor != FL_DERIVED
1816 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
1819 ts->type = BT_DERIVED;
1826 /* For all types except double, derived and character, look for an
1827 optional kind specifier. MATCH_NO is actually OK at this point. */
1828 if (implicit_flag == 1)
1831 if (gfc_current_form == FORM_FREE)
1833 c = gfc_peek_char();
1834 if (!gfc_is_whitespace(c) && c != '*' && c != '('
1835 && c != ':' && c != ',')
1839 m = gfc_match_kind_spec (ts);
1840 if (m == MATCH_NO && ts->type != BT_CHARACTER)
1841 m = gfc_match_old_kind_spec (ts);
1844 m = MATCH_YES; /* No kind specifier found. */
1850 /* Match an IMPLICIT NONE statement. Actually, this statement is
1851 already matched in parse.c, or we would not end up here in the
1852 first place. So the only thing we need to check, is if there is
1853 trailing garbage. If not, the match is successful. */
1856 gfc_match_implicit_none (void)
1858 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
1862 /* Match the letter range(s) of an IMPLICIT statement. */
1865 match_implicit_range (void)
1867 int c, c1, c2, inner;
1870 cur_loc = gfc_current_locus;
1872 gfc_gobble_whitespace ();
1873 c = gfc_next_char ();
1876 gfc_error ("Missing character range in IMPLICIT at %C");
1883 gfc_gobble_whitespace ();
1884 c1 = gfc_next_char ();
1888 gfc_gobble_whitespace ();
1889 c = gfc_next_char ();
1894 inner = 0; /* Fall through. */
1901 gfc_gobble_whitespace ();
1902 c2 = gfc_next_char ();
1906 gfc_gobble_whitespace ();
1907 c = gfc_next_char ();
1909 if ((c != ',') && (c != ')'))
1922 gfc_error ("Letters must be in alphabetic order in "
1923 "IMPLICIT statement at %C");
1927 /* See if we can add the newly matched range to the pending
1928 implicits from this IMPLICIT statement. We do not check for
1929 conflicts with whatever earlier IMPLICIT statements may have
1930 set. This is done when we've successfully finished matching
1932 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
1939 gfc_syntax_error (ST_IMPLICIT);
1941 gfc_current_locus = cur_loc;
1946 /* Match an IMPLICIT statement, storing the types for
1947 gfc_set_implicit() if the statement is accepted by the parser.
1948 There is a strange looking, but legal syntactic construction
1949 possible. It looks like:
1951 IMPLICIT INTEGER (a-b) (c-d)
1953 This is legal if "a-b" is a constant expression that happens to
1954 equal one of the legal kinds for integers. The real problem
1955 happens with an implicit specification that looks like:
1957 IMPLICIT INTEGER (a-b)
1959 In this case, a typespec matcher that is "greedy" (as most of the
1960 matchers are) gobbles the character range as a kindspec, leaving
1961 nothing left. We therefore have to go a bit more slowly in the
1962 matching process by inhibiting the kindspec checking during
1963 typespec matching and checking for a kind later. */
1966 gfc_match_implicit (void)
1973 /* We don't allow empty implicit statements. */
1974 if (gfc_match_eos () == MATCH_YES)
1976 gfc_error ("Empty IMPLICIT statement at %C");
1982 /* First cleanup. */
1983 gfc_clear_new_implicit ();
1985 /* A basic type is mandatory here. */
1986 m = match_type_spec (&ts, 1);
1987 if (m == MATCH_ERROR)
1992 cur_loc = gfc_current_locus;
1993 m = match_implicit_range ();
1997 /* We may have <TYPE> (<RANGE>). */
1998 gfc_gobble_whitespace ();
1999 c = gfc_next_char ();
2000 if ((c == '\n') || (c == ','))
2002 /* Check for CHARACTER with no length parameter. */
2003 if (ts.type == BT_CHARACTER && !ts.cl)
2005 ts.kind = gfc_default_character_kind;
2006 ts.cl = gfc_get_charlen ();
2007 ts.cl->next = gfc_current_ns->cl_list;
2008 gfc_current_ns->cl_list = ts.cl;
2009 ts.cl->length = gfc_int_expr (1);
2012 /* Record the Successful match. */
2013 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2018 gfc_current_locus = cur_loc;
2021 /* Discard the (incorrectly) matched range. */
2022 gfc_clear_new_implicit ();
2024 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2025 if (ts.type == BT_CHARACTER)
2026 m = match_char_spec (&ts);
2029 m = gfc_match_kind_spec (&ts);
2032 m = gfc_match_old_kind_spec (&ts);
2033 if (m == MATCH_ERROR)
2039 if (m == MATCH_ERROR)
2042 m = match_implicit_range ();
2043 if (m == MATCH_ERROR)
2048 gfc_gobble_whitespace ();
2049 c = gfc_next_char ();
2050 if ((c != '\n') && (c != ','))
2053 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2061 gfc_syntax_error (ST_IMPLICIT);
2069 gfc_match_import (void)
2071 char name[GFC_MAX_SYMBOL_LEN + 1];
2076 if (gfc_current_ns->proc_name == NULL
2077 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2079 gfc_error ("IMPORT statement at %C only permitted in "
2080 "an INTERFACE body");
2084 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2088 if (gfc_match_eos () == MATCH_YES)
2090 /* All host variables should be imported. */
2091 gfc_current_ns->has_import_set = 1;
2095 if (gfc_match (" ::") == MATCH_YES)
2097 if (gfc_match_eos () == MATCH_YES)
2099 gfc_error ("Expecting list of named entities at %C");
2106 m = gfc_match (" %n", name);
2110 if (gfc_current_ns->parent != NULL
2111 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2113 gfc_error ("Type name '%s' at %C is ambiguous", name);
2116 else if (gfc_current_ns->proc_name->ns->parent != NULL
2117 && gfc_find_symbol (name,
2118 gfc_current_ns->proc_name->ns->parent,
2121 gfc_error ("Type name '%s' at %C is ambiguous", name);
2127 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2128 "at %C - does not exist.", name);
2132 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2134 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2139 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2142 sym->ns = gfc_current_ns;
2154 if (gfc_match_eos () == MATCH_YES)
2156 if (gfc_match_char (',') != MATCH_YES)
2163 gfc_error ("Syntax error in IMPORT statement at %C");
2168 /* Matches an attribute specification including array specs. If
2169 successful, leaves the variables current_attr and current_as
2170 holding the specification. Also sets the colon_seen variable for
2171 later use by matchers associated with initializations.
2173 This subroutine is a little tricky in the sense that we don't know
2174 if we really have an attr-spec until we hit the double colon.
2175 Until that time, we can only return MATCH_NO. This forces us to
2176 check for duplicate specification at this level. */
2179 match_attr_spec (void)
2181 /* Modifiers that can exist in a type statement. */
2183 { GFC_DECL_BEGIN = 0,
2184 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2185 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2186 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2187 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2188 DECL_COLON, DECL_NONE,
2189 GFC_DECL_END /* Sentinel */
2193 /* GFC_DECL_END is the sentinel, index starts at 0. */
2194 #define NUM_DECL GFC_DECL_END
2196 static mstring decls[] = {
2197 minit (", allocatable", DECL_ALLOCATABLE),
2198 minit (", dimension", DECL_DIMENSION),
2199 minit (", external", DECL_EXTERNAL),
2200 minit (", intent ( in )", DECL_IN),
2201 minit (", intent ( out )", DECL_OUT),
2202 minit (", intent ( in out )", DECL_INOUT),
2203 minit (", intrinsic", DECL_INTRINSIC),
2204 minit (", optional", DECL_OPTIONAL),
2205 minit (", parameter", DECL_PARAMETER),
2206 minit (", pointer", DECL_POINTER),
2207 minit (", protected", DECL_PROTECTED),
2208 minit (", private", DECL_PRIVATE),
2209 minit (", public", DECL_PUBLIC),
2210 minit (", save", DECL_SAVE),
2211 minit (", target", DECL_TARGET),
2212 minit (", value", DECL_VALUE),
2213 minit (", volatile", DECL_VOLATILE),
2214 minit ("::", DECL_COLON),
2215 minit (NULL, DECL_NONE)
2218 locus start, seen_at[NUM_DECL];
2225 gfc_clear_attr (¤t_attr);
2226 start = gfc_current_locus;
2231 /* See if we get all of the keywords up to the final double colon. */
2232 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2237 d = (decl_types) gfc_match_strings (decls);
2238 if (d == DECL_NONE || d == DECL_COLON)
2242 seen_at[d] = gfc_current_locus;
2244 if (d == DECL_DIMENSION)
2246 m = gfc_match_array_spec (¤t_as);
2250 gfc_error ("Missing dimension specification at %C");
2254 if (m == MATCH_ERROR)
2259 /* No double colon, so assume that we've been looking at something
2260 else the whole time. */
2267 /* Since we've seen a double colon, we have to be looking at an
2268 attr-spec. This means that we can now issue errors. */
2269 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2274 case DECL_ALLOCATABLE:
2275 attr = "ALLOCATABLE";
2277 case DECL_DIMENSION:
2284 attr = "INTENT (IN)";
2287 attr = "INTENT (OUT)";
2290 attr = "INTENT (IN OUT)";
2292 case DECL_INTRINSIC:
2298 case DECL_PARAMETER:
2304 case DECL_PROTECTED:
2326 attr = NULL; /* This shouldn't happen. */
2329 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2334 /* Now that we've dealt with duplicate attributes, add the attributes
2335 to the current attribute. */
2336 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2341 if (gfc_current_state () == COMP_DERIVED
2342 && d != DECL_DIMENSION && d != DECL_POINTER
2343 && d != DECL_COLON && d != DECL_PRIVATE
2344 && d != DECL_PUBLIC && d != DECL_NONE)
2346 if (d == DECL_ALLOCATABLE)
2348 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
2349 "attribute at %C in a TYPE definition")
2358 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2365 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2366 && gfc_current_state () != COMP_MODULE)
2368 if (d == DECL_PRIVATE)
2372 if (gfc_current_state () == COMP_DERIVED
2373 && gfc_state_stack->previous
2374 && gfc_state_stack->previous->state == COMP_MODULE)
2376 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
2377 "at %L in a TYPE definition", attr,
2387 gfc_error ("%s attribute at %L is not allowed outside of the "
2388 "specification part of a module", attr, &seen_at[d]);
2396 case DECL_ALLOCATABLE:
2397 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
2400 case DECL_DIMENSION:
2401 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
2405 t = gfc_add_external (¤t_attr, &seen_at[d]);
2409 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
2413 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
2417 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
2420 case DECL_INTRINSIC:
2421 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
2425 t = gfc_add_optional (¤t_attr, &seen_at[d]);
2428 case DECL_PARAMETER:
2429 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
2433 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
2436 case DECL_PROTECTED:
2437 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
2439 gfc_error ("PROTECTED at %C only allowed in specification "
2440 "part of a module");
2445 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
2450 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
2454 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
2459 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
2464 t = gfc_add_save (¤t_attr, NULL, &seen_at[d]);
2468 t = gfc_add_target (¤t_attr, &seen_at[d]);
2472 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
2477 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
2481 if (gfc_notify_std (GFC_STD_F2003,
2482 "Fortran 2003: VOLATILE attribute at %C")
2486 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
2490 gfc_internal_error ("match_attr_spec(): Bad attribute");
2504 gfc_current_locus = start;
2505 gfc_free_array_spec (current_as);
2511 /* Match a data declaration statement. */
2514 gfc_match_data_decl (void)
2520 m = match_type_spec (¤t_ts, 0);
2524 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
2526 sym = gfc_use_derived (current_ts.derived);
2534 current_ts.derived = sym;
2537 m = match_attr_spec ();
2538 if (m == MATCH_ERROR)
2544 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
2547 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
2550 gfc_find_symbol (current_ts.derived->name,
2551 current_ts.derived->ns->parent, 1, &sym);
2553 /* Any symbol that we find had better be a type definition
2554 which has its components defined. */
2555 if (sym != NULL && sym->attr.flavor == FL_DERIVED
2556 && current_ts.derived->components != NULL)
2559 /* Now we have an error, which we signal, and then fix up
2560 because the knock-on is plain and simple confusing. */
2561 gfc_error_now ("Derived type at %C has not been previously defined "
2562 "and so cannot appear in a derived type definition");
2563 current_attr.pointer = 1;
2568 /* If we have an old-style character declaration, and no new-style
2569 attribute specifications, then there a comma is optional between
2570 the type specification and the variable list. */
2571 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
2572 gfc_match_char (',');
2574 /* Give the types/attributes to symbols that follow. Give the element
2575 a number so that repeat character length expressions can be copied. */
2579 m = variable_decl (elem++);
2580 if (m == MATCH_ERROR)
2585 if (gfc_match_eos () == MATCH_YES)
2587 if (gfc_match_char (',') != MATCH_YES)
2591 if (gfc_error_flag_test () == 0)
2592 gfc_error ("Syntax error in data declaration at %C");
2595 gfc_free_data_all (gfc_current_ns);
2598 gfc_free_array_spec (current_as);
2604 /* Match a prefix associated with a function or subroutine
2605 declaration. If the typespec pointer is nonnull, then a typespec
2606 can be matched. Note that if nothing matches, MATCH_YES is
2607 returned (the null string was matched). */
2610 match_prefix (gfc_typespec *ts)
2614 gfc_clear_attr (¤t_attr);
2618 if (!seen_type && ts != NULL
2619 && match_type_spec (ts, 0) == MATCH_YES
2620 && gfc_match_space () == MATCH_YES)
2627 if (gfc_match ("elemental% ") == MATCH_YES)
2629 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
2635 if (gfc_match ("pure% ") == MATCH_YES)
2637 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
2643 if (gfc_match ("recursive% ") == MATCH_YES)
2645 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
2651 /* At this point, the next item is not a prefix. */
2656 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
2659 copy_prefix (symbol_attribute *dest, locus *where)
2661 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
2664 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
2667 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
2674 /* Match a formal argument list. */
2677 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
2679 gfc_formal_arglist *head, *tail, *p, *q;
2680 char name[GFC_MAX_SYMBOL_LEN + 1];
2686 if (gfc_match_char ('(') != MATCH_YES)
2693 if (gfc_match_char (')') == MATCH_YES)
2698 if (gfc_match_char ('*') == MATCH_YES)
2702 m = gfc_match_name (name);
2706 if (gfc_get_symbol (name, NULL, &sym))
2710 p = gfc_get_formal_arglist ();
2722 /* We don't add the VARIABLE flavor because the name could be a
2723 dummy procedure. We don't apply these attributes to formal
2724 arguments of statement functions. */
2725 if (sym != NULL && !st_flag
2726 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
2727 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
2733 /* The name of a program unit can be in a different namespace,
2734 so check for it explicitly. After the statement is accepted,
2735 the name is checked for especially in gfc_get_symbol(). */
2736 if (gfc_new_block != NULL && sym != NULL
2737 && strcmp (sym->name, gfc_new_block->name) == 0)
2739 gfc_error ("Name '%s' at %C is the name of the procedure",
2745 if (gfc_match_char (')') == MATCH_YES)
2748 m = gfc_match_char (',');
2751 gfc_error ("Unexpected junk in formal argument list at %C");
2757 /* Check for duplicate symbols in the formal argument list. */
2760 for (p = head; p->next; p = p->next)
2765 for (q = p->next; q; q = q->next)
2766 if (p->sym == q->sym)
2768 gfc_error ("Duplicate symbol '%s' in formal argument list "
2769 "at %C", p->sym->name);
2777 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
2787 gfc_free_formal_arglist (head);
2792 /* Match a RESULT specification following a function declaration or
2793 ENTRY statement. Also matches the end-of-statement. */
2796 match_result (gfc_symbol *function, gfc_symbol **result)
2798 char name[GFC_MAX_SYMBOL_LEN + 1];
2802 if (gfc_match (" result (") != MATCH_YES)
2805 m = gfc_match_name (name);
2809 if (gfc_match (" )%t") != MATCH_YES)
2811 gfc_error ("Unexpected junk following RESULT variable at %C");
2815 if (strcmp (function->name, name) == 0)
2817 gfc_error ("RESULT variable at %C must be different than function name");
2821 if (gfc_get_symbol (name, NULL, &r))
2824 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
2825 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
2834 /* Match a function declaration. */
2837 gfc_match_function_decl (void)
2839 char name[GFC_MAX_SYMBOL_LEN + 1];
2840 gfc_symbol *sym, *result;
2844 if (gfc_current_state () != COMP_NONE
2845 && gfc_current_state () != COMP_INTERFACE
2846 && gfc_current_state () != COMP_CONTAINS)
2849 gfc_clear_ts (¤t_ts);
2851 old_loc = gfc_current_locus;
2853 m = match_prefix (¤t_ts);
2856 gfc_current_locus = old_loc;
2860 if (gfc_match ("function% %n", name) != MATCH_YES)
2862 gfc_current_locus = old_loc;
2865 if (get_proc_name (name, &sym, false))
2867 gfc_new_block = sym;
2869 m = gfc_match_formal_arglist (sym, 0, 0);
2872 gfc_error ("Expected formal argument list in function "
2873 "definition at %C");
2877 else if (m == MATCH_ERROR)
2882 if (gfc_match_eos () != MATCH_YES)
2884 /* See if a result variable is present. */
2885 m = match_result (sym, &result);
2887 gfc_error ("Unexpected junk after function declaration at %C");
2896 /* Make changes to the symbol. */
2899 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2902 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
2903 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2906 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
2907 && !sym->attr.implicit_type)
2909 gfc_error ("Function '%s' at %C already has a type of %s", name,
2910 gfc_basic_typename (sym->ts.type));
2916 sym->ts = current_ts;
2921 result->ts = current_ts;
2922 sym->result = result;
2928 gfc_current_locus = old_loc;
2933 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
2934 pass the name of the entry, rather than the gfc_current_block name, and
2935 to return false upon finding an existing global entry. */
2938 add_global_entry (const char *name, int sub)
2942 s = gfc_get_gsymbol(name);
2945 || (s->type != GSYM_UNKNOWN
2946 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
2947 global_used(s, NULL);
2950 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2951 s->where = gfc_current_locus;
2959 /* Match an ENTRY statement. */
2962 gfc_match_entry (void)
2967 char name[GFC_MAX_SYMBOL_LEN + 1];
2968 gfc_compile_state state;
2972 bool module_procedure;
2974 m = gfc_match_name (name);
2978 state = gfc_current_state ();
2979 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
2984 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
2987 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
2989 case COMP_BLOCK_DATA:
2990 gfc_error ("ENTRY statement at %C cannot appear within "
2993 case COMP_INTERFACE:
2994 gfc_error ("ENTRY statement at %C cannot appear within "
2998 gfc_error ("ENTRY statement at %C cannot appear within "
2999 "a DERIVED TYPE block");
3002 gfc_error ("ENTRY statement at %C cannot appear within "
3003 "an IF-THEN block");
3006 gfc_error ("ENTRY statement at %C cannot appear within "
3010 gfc_error ("ENTRY statement at %C cannot appear within "
3014 gfc_error ("ENTRY statement at %C cannot appear within "
3018 gfc_error ("ENTRY statement at %C cannot appear within "
3022 gfc_error ("ENTRY statement at %C cannot appear within "
3023 "a contained subprogram");
3026 gfc_internal_error ("gfc_match_entry(): Bad state");
3031 module_procedure = gfc_current_ns->parent != NULL
3032 && gfc_current_ns->parent->proc_name
3033 && gfc_current_ns->parent->proc_name->attr.flavor
3036 if (gfc_current_ns->parent != NULL
3037 && gfc_current_ns->parent->proc_name
3038 && !module_procedure)
3040 gfc_error("ENTRY statement at %C cannot appear in a "
3041 "contained procedure");
3045 /* Module function entries need special care in get_proc_name
3046 because previous references within the function will have
3047 created symbols attached to the current namespace. */
3048 if (get_proc_name (name, &entry,
3049 gfc_current_ns->parent != NULL
3051 && gfc_current_ns->proc_name->attr.function))
3054 proc = gfc_current_block ();
3056 if (state == COMP_SUBROUTINE)
3058 /* An entry in a subroutine. */
3059 if (!add_global_entry (name, 1))
3062 m = gfc_match_formal_arglist (entry, 0, 1);
3066 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3067 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
3072 /* An entry in a function.
3073 We need to take special care because writing
3078 ENTRY f() RESULT (r)
3080 ENTRY f RESULT (r). */
3081 if (!add_global_entry (name, 0))
3084 old_loc = gfc_current_locus;
3085 if (gfc_match_eos () == MATCH_YES)
3087 gfc_current_locus = old_loc;
3088 /* Match the empty argument list, and add the interface to
3090 m = gfc_match_formal_arglist (entry, 0, 1);
3093 m = gfc_match_formal_arglist (entry, 0, 0);
3100 if (gfc_match_eos () == MATCH_YES)
3102 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3103 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
3106 entry->result = entry;
3110 m = match_result (proc, &result);
3112 gfc_syntax_error (ST_ENTRY);
3116 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
3117 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
3118 || gfc_add_function (&entry->attr, result->name, NULL)
3122 entry->result = result;
3126 if (gfc_match_eos () != MATCH_YES)
3128 gfc_syntax_error (ST_ENTRY);
3132 entry->attr.recursive = proc->attr.recursive;
3133 entry->attr.elemental = proc->attr.elemental;
3134 entry->attr.pure = proc->attr.pure;
3136 el = gfc_get_entry_list ();
3138 el->next = gfc_current_ns->entries;
3139 gfc_current_ns->entries = el;
3141 el->id = el->next->id + 1;
3145 new_st.op = EXEC_ENTRY;
3146 new_st.ext.entry = el;
3152 /* Match a subroutine statement, including optional prefixes. */
3155 gfc_match_subroutine (void)
3157 char name[GFC_MAX_SYMBOL_LEN + 1];
3161 if (gfc_current_state () != COMP_NONE
3162 && gfc_current_state () != COMP_INTERFACE
3163 && gfc_current_state () != COMP_CONTAINS)
3166 m = match_prefix (NULL);
3170 m = gfc_match ("subroutine% %n", name);
3174 if (get_proc_name (name, &sym, false))
3176 gfc_new_block = sym;
3178 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
3181 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
3184 if (gfc_match_eos () != MATCH_YES)
3186 gfc_syntax_error (ST_SUBROUTINE);
3190 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
3197 /* Return nonzero if we're currently compiling a contained procedure. */
3200 contained_procedure (void)
3204 for (s=gfc_state_stack; s; s=s->previous)
3205 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
3206 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
3212 /* Set the kind of each enumerator. The kind is selected such that it is
3213 interoperable with the corresponding C enumeration type, making
3214 sure that -fshort-enums is honored. */
3219 enumerator_history *current_history = NULL;
3223 if (max_enum == NULL || enum_history == NULL)
3226 if (!gfc_option.fshort_enums)
3232 kind = gfc_integer_kinds[i++].kind;
3234 while (kind < gfc_c_int_kind
3235 && gfc_check_integer_range (max_enum->initializer->value.integer,
3238 current_history = enum_history;
3239 while (current_history != NULL)
3241 current_history->sym->ts.kind = kind;
3242 current_history = current_history->next;
3247 /* Match any of the various end-block statements. Returns the type of
3248 END to the caller. The END INTERFACE, END IF, END DO and END
3249 SELECT statements cannot be replaced by a single END statement. */
3252 gfc_match_end (gfc_statement *st)
3254 char name[GFC_MAX_SYMBOL_LEN + 1];
3255 gfc_compile_state state;
3257 const char *block_name;
3262 old_loc = gfc_current_locus;
3263 if (gfc_match ("end") != MATCH_YES)
3266 state = gfc_current_state ();
3267 block_name = gfc_current_block () == NULL
3268 ? NULL : gfc_current_block ()->name;
3270 if (state == COMP_CONTAINS)
3272 state = gfc_state_stack->previous->state;
3273 block_name = gfc_state_stack->previous->sym == NULL
3274 ? NULL : gfc_state_stack->previous->sym->name;
3281 *st = ST_END_PROGRAM;
3282 target = " program";
3286 case COMP_SUBROUTINE:
3287 *st = ST_END_SUBROUTINE;
3288 target = " subroutine";
3289 eos_ok = !contained_procedure ();
3293 *st = ST_END_FUNCTION;
3294 target = " function";
3295 eos_ok = !contained_procedure ();
3298 case COMP_BLOCK_DATA:
3299 *st = ST_END_BLOCK_DATA;
3300 target = " block data";
3305 *st = ST_END_MODULE;
3310 case COMP_INTERFACE:
3311 *st = ST_END_INTERFACE;
3312 target = " interface";
3335 *st = ST_END_SELECT;
3341 *st = ST_END_FORALL;
3356 last_initializer = NULL;
3358 gfc_free_enum_history ();
3362 gfc_error ("Unexpected END statement at %C");
3366 if (gfc_match_eos () == MATCH_YES)
3370 /* We would have required END [something]. */
3371 gfc_error ("%s statement expected at %L",
3372 gfc_ascii_statement (*st), &old_loc);
3379 /* Verify that we've got the sort of end-block that we're expecting. */
3380 if (gfc_match (target) != MATCH_YES)
3382 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
3386 /* If we're at the end, make sure a block name wasn't required. */
3387 if (gfc_match_eos () == MATCH_YES)
3390 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
3391 && *st != ST_END_FORALL && *st != ST_END_WHERE)
3394 if (gfc_current_block () == NULL)
3397 gfc_error ("Expected block name of '%s' in %s statement at %C",
3398 block_name, gfc_ascii_statement (*st));
3403 /* END INTERFACE has a special handler for its several possible endings. */
3404 if (*st == ST_END_INTERFACE)
3405 return gfc_match_end_interface ();
3407 /* We haven't hit the end of statement, so what is left must be an
3409 m = gfc_match_space ();
3411 m = gfc_match_name (name);
3414 gfc_error ("Expected terminating name at %C");
3418 if (block_name == NULL)
3421 if (strcmp (name, block_name) != 0)
3423 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
3424 gfc_ascii_statement (*st));
3428 if (gfc_match_eos () == MATCH_YES)
3432 gfc_syntax_error (*st);
3435 gfc_current_locus = old_loc;
3441 /***************** Attribute declaration statements ****************/
3443 /* Set the attribute of a single variable. */
3448 char name[GFC_MAX_SYMBOL_LEN + 1];
3456 m = gfc_match_name (name);
3460 if (find_special (name, &sym))
3463 var_locus = gfc_current_locus;
3465 /* Deal with possible array specification for certain attributes. */
3466 if (current_attr.dimension
3467 || current_attr.allocatable
3468 || current_attr.pointer
3469 || current_attr.target)
3471 m = gfc_match_array_spec (&as);
3472 if (m == MATCH_ERROR)
3475 if (current_attr.dimension && m == MATCH_NO)
3477 gfc_error ("Missing array specification at %L in DIMENSION "
3478 "statement", &var_locus);
3483 if ((current_attr.allocatable || current_attr.pointer)
3484 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
3486 gfc_error ("Array specification must be deferred at %L", &var_locus);
3492 /* Update symbol table. DIMENSION attribute is set
3493 in gfc_set_array_spec(). */
3494 if (current_attr.dimension == 0
3495 && gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
3501 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
3507 if (sym->attr.cray_pointee && sym->as != NULL)
3509 /* Fix the array spec. */
3510 m = gfc_mod_pointee_as (sym->as);
3511 if (m == MATCH_ERROR)
3515 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
3521 if ((current_attr.external || current_attr.intrinsic)
3522 && sym->attr.flavor != FL_PROCEDURE
3523 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
3532 gfc_free_array_spec (as);
3537 /* Generic attribute declaration subroutine. Used for attributes that
3538 just have a list of names. */
3545 /* Gobble the optional double colon, by simply ignoring the result
3555 if (gfc_match_eos () == MATCH_YES)
3561 if (gfc_match_char (',') != MATCH_YES)
3563 gfc_error ("Unexpected character in variable list at %C");
3573 /* This routine matches Cray Pointer declarations of the form:
3574 pointer ( <pointer>, <pointee> )
3576 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
3577 The pointer, if already declared, should be an integer. Otherwise, we
3578 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
3579 be either a scalar, or an array declaration. No space is allocated for
3580 the pointee. For the statement
3581 pointer (ipt, ar(10))
3582 any subsequent uses of ar will be translated (in C-notation) as
3583 ar(i) => ((<type> *) ipt)(i)
3584 After gimplification, pointee variable will disappear in the code. */
3587 cray_pointer_decl (void)
3591 gfc_symbol *cptr; /* Pointer symbol. */
3592 gfc_symbol *cpte; /* Pointee symbol. */
3598 if (gfc_match_char ('(') != MATCH_YES)
3600 gfc_error ("Expected '(' at %C");
3604 /* Match pointer. */
3605 var_locus = gfc_current_locus;
3606 gfc_clear_attr (¤t_attr);
3607 gfc_add_cray_pointer (¤t_attr, &var_locus);
3608 current_ts.type = BT_INTEGER;
3609 current_ts.kind = gfc_index_integer_kind;
3611 m = gfc_match_symbol (&cptr, 0);
3614 gfc_error ("Expected variable name at %C");
3618 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
3621 gfc_set_sym_referenced (cptr);
3623 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
3625 cptr->ts.type = BT_INTEGER;
3626 cptr->ts.kind = gfc_index_integer_kind;
3628 else if (cptr->ts.type != BT_INTEGER)
3630 gfc_error ("Cray pointer at %C must be an integer");
3633 else if (cptr->ts.kind < gfc_index_integer_kind)
3634 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
3635 " memory addresses require %d bytes",
3636 cptr->ts.kind, gfc_index_integer_kind);
3638 if (gfc_match_char (',') != MATCH_YES)
3640 gfc_error ("Expected \",\" at %C");
3644 /* Match Pointee. */
3645 var_locus = gfc_current_locus;
3646 gfc_clear_attr (¤t_attr);
3647 gfc_add_cray_pointee (¤t_attr, &var_locus);
3648 current_ts.type = BT_UNKNOWN;
3649 current_ts.kind = 0;
3651 m = gfc_match_symbol (&cpte, 0);
3654 gfc_error ("Expected variable name at %C");
3658 /* Check for an optional array spec. */
3659 m = gfc_match_array_spec (&as);
3660 if (m == MATCH_ERROR)
3662 gfc_free_array_spec (as);
3665 else if (m == MATCH_NO)
3667 gfc_free_array_spec (as);
3671 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
3674 gfc_set_sym_referenced (cpte);
3676 if (cpte->as == NULL)
3678 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
3679 gfc_internal_error ("Couldn't set Cray pointee array spec.");
3681 else if (as != NULL)
3683 gfc_error ("Duplicate array spec for Cray pointee at %C");
3684 gfc_free_array_spec (as);
3690 if (cpte->as != NULL)
3692 /* Fix array spec. */
3693 m = gfc_mod_pointee_as (cpte->as);
3694 if (m == MATCH_ERROR)
3698 /* Point the Pointee at the Pointer. */
3699 cpte->cp_pointer = cptr;
3701 if (gfc_match_char (')') != MATCH_YES)
3703 gfc_error ("Expected \")\" at %C");
3706 m = gfc_match_char (',');
3708 done = true; /* Stop searching for more declarations. */
3712 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
3713 || gfc_match_eos () != MATCH_YES)
3715 gfc_error ("Expected \",\" or end of statement at %C");
3723 gfc_match_external (void)
3726 gfc_clear_attr (¤t_attr);
3727 current_attr.external = 1;
3729 return attr_decl ();
3734 gfc_match_intent (void)
3738 intent = match_intent_spec ();
3739 if (intent == INTENT_UNKNOWN)
3742 gfc_clear_attr (¤t_attr);
3743 current_attr.intent = intent;
3745 return attr_decl ();
3750 gfc_match_intrinsic (void)
3753 gfc_clear_attr (¤t_attr);
3754 current_attr.intrinsic = 1;
3756 return attr_decl ();
3761 gfc_match_optional (void)
3764 gfc_clear_attr (¤t_attr);
3765 current_attr.optional = 1;
3767 return attr_decl ();
3772 gfc_match_pointer (void)
3774 gfc_gobble_whitespace ();
3775 if (gfc_peek_char () == '(')
3777 if (!gfc_option.flag_cray_pointer)
3779 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
3783 return cray_pointer_decl ();
3787 gfc_clear_attr (¤t_attr);
3788 current_attr.pointer = 1;
3790 return attr_decl ();
3796 gfc_match_allocatable (void)
3798 gfc_clear_attr (¤t_attr);
3799 current_attr.allocatable = 1;
3801 return attr_decl ();
3806 gfc_match_dimension (void)
3808 gfc_clear_attr (¤t_attr);
3809 current_attr.dimension = 1;
3811 return attr_decl ();
3816 gfc_match_target (void)
3818 gfc_clear_attr (¤t_attr);
3819 current_attr.target = 1;
3821 return attr_decl ();
3825 /* Match the list of entities being specified in a PUBLIC or PRIVATE
3829 access_attr_decl (gfc_statement st)
3831 char name[GFC_MAX_SYMBOL_LEN + 1];
3832 interface_type type;
3835 gfc_intrinsic_op operator;
3838 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
3843 m = gfc_match_generic_spec (&type, name, &operator);
3846 if (m == MATCH_ERROR)
3851 case INTERFACE_NAMELESS:
3854 case INTERFACE_GENERIC:
3855 if (gfc_get_symbol (name, NULL, &sym))
3858 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
3859 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
3860 sym->name, NULL) == FAILURE)
3865 case INTERFACE_INTRINSIC_OP:
3866 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
3868 gfc_current_ns->operator_access[operator] =
3869 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3873 gfc_error ("Access specification of the %s operator at %C has "
3874 "already been specified", gfc_op2string (operator));
3880 case INTERFACE_USER_OP:
3881 uop = gfc_get_uop (name);
3883 if (uop->access == ACCESS_UNKNOWN)
3885 uop->access = (st == ST_PUBLIC)
3886 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3890 gfc_error ("Access specification of the .%s. operator at %C "
3891 "has already been specified", sym->name);
3898 if (gfc_match_char (',') == MATCH_NO)
3902 if (gfc_match_eos () != MATCH_YES)
3907 gfc_syntax_error (st);
3915 gfc_match_protected (void)
3920 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3922 gfc_error ("PROTECTED at %C only allowed in specification "
3923 "part of a module");
3928 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
3932 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
3937 if (gfc_match_eos () == MATCH_YES)
3942 m = gfc_match_symbol (&sym, 0);
3946 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
3959 if (gfc_match_eos () == MATCH_YES)
3961 if (gfc_match_char (',') != MATCH_YES)
3968 gfc_error ("Syntax error in PROTECTED statement at %C");
3973 /* The PRIVATE statement is a bit weird in that it can be a attribute
3974 declaration, but also works as a standlone statement inside of a
3975 type declaration or a module. */
3978 gfc_match_private (gfc_statement *st)
3981 if (gfc_match ("private") != MATCH_YES)
3984 if (gfc_current_state () != COMP_MODULE
3985 && (gfc_current_state () != COMP_DERIVED
3986 || !gfc_state_stack->previous
3987 || gfc_state_stack->previous->state != COMP_MODULE))
3989 gfc_error ("PRIVATE statement at %C is only allowed in the "
3990 "specification part of a module");
3994 if (gfc_current_state () == COMP_DERIVED)
3996 if (gfc_match_eos () == MATCH_YES)
4002 gfc_syntax_error (ST_PRIVATE);
4006 if (gfc_match_eos () == MATCH_YES)
4013 return access_attr_decl (ST_PRIVATE);
4018 gfc_match_public (gfc_statement *st)
4021 if (gfc_match ("public") != MATCH_YES)
4024 if (gfc_current_state () != COMP_MODULE)
4026 gfc_error ("PUBLIC statement at %C is only allowed in the "
4027 "specification part of a module");
4031 if (gfc_match_eos () == MATCH_YES)
4038 return access_attr_decl (ST_PUBLIC);
4042 /* Workhorse for gfc_match_parameter. */
4051 m = gfc_match_symbol (&sym, 0);
4053 gfc_error ("Expected variable name at %C in PARAMETER statement");
4058 if (gfc_match_char ('=') == MATCH_NO)
4060 gfc_error ("Expected = sign in PARAMETER statement at %C");
4064 m = gfc_match_init_expr (&init);
4066 gfc_error ("Expected expression at %C in PARAMETER statement");
4070 if (sym->ts.type == BT_UNKNOWN
4071 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
4077 if (gfc_check_assign_symbol (sym, init) == FAILURE
4078 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
4084 if (sym->ts.type == BT_CHARACTER
4085 && sym->ts.cl != NULL
4086 && sym->ts.cl->length != NULL
4087 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
4088 && init->expr_type == EXPR_CONSTANT
4089 && init->ts.type == BT_CHARACTER
4090 && init->ts.kind == 1)
4091 gfc_set_constant_character_len (
4092 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
4098 gfc_free_expr (init);
4103 /* Match a parameter statement, with the weird syntax that these have. */
4106 gfc_match_parameter (void)
4110 if (gfc_match_char ('(') == MATCH_NO)
4119 if (gfc_match (" )%t") == MATCH_YES)
4122 if (gfc_match_char (',') != MATCH_YES)
4124 gfc_error ("Unexpected characters in PARAMETER statement at %C");
4134 /* Save statements have a special syntax. */
4137 gfc_match_save (void)
4139 char n[GFC_MAX_SYMBOL_LEN+1];
4144 if (gfc_match_eos () == MATCH_YES)
4146 if (gfc_current_ns->seen_save)
4148 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
4149 "follows previous SAVE statement")
4154 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
4158 if (gfc_current_ns->save_all)
4160 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
4161 "blanket SAVE statement")
4170 m = gfc_match_symbol (&sym, 0);
4174 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
4186 m = gfc_match (" / %n /", &n);
4187 if (m == MATCH_ERROR)
4192 c = gfc_get_common (n, 0);
4195 gfc_current_ns->seen_save = 1;
4198 if (gfc_match_eos () == MATCH_YES)
4200 if (gfc_match_char (',') != MATCH_YES)
4207 gfc_error ("Syntax error in SAVE statement at %C");
4213 gfc_match_value (void)
4218 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
4222 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4227 if (gfc_match_eos () == MATCH_YES)
4232 m = gfc_match_symbol (&sym, 0);
4236 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
4249 if (gfc_match_eos () == MATCH_YES)
4251 if (gfc_match_char (',') != MATCH_YES)
4258 gfc_error ("Syntax error in VALUE statement at %C");
4264 gfc_match_volatile (void)
4269 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
4273 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4278 if (gfc_match_eos () == MATCH_YES)
4283 /* VOLATILE is special because it can be added to host-associated
4285 m = gfc_match_symbol (&sym, 1);
4289 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
4302 if (gfc_match_eos () == MATCH_YES)
4304 if (gfc_match_char (',') != MATCH_YES)
4311 gfc_error ("Syntax error in VOLATILE statement at %C");
4316 /* Match a module procedure statement. Note that we have to modify
4317 symbols in the parent's namespace because the current one was there
4318 to receive symbols that are in an interface's formal argument list. */
4321 gfc_match_modproc (void)
4323 char name[GFC_MAX_SYMBOL_LEN + 1];
4326 gfc_namespace *module_ns;
4328 if (gfc_state_stack->state != COMP_INTERFACE
4329 || gfc_state_stack->previous == NULL
4330 || current_interface.type == INTERFACE_NAMELESS)
4332 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
4337 module_ns = gfc_current_ns->parent;
4338 for (; module_ns; module_ns = module_ns->parent)
4339 if (module_ns->proc_name->attr.flavor == FL_MODULE)
4342 if (module_ns == NULL)
4347 m = gfc_match_name (name);
4353 if (gfc_get_symbol (name, module_ns, &sym))
4356 if (sym->attr.proc != PROC_MODULE
4357 && gfc_add_procedure (&sym->attr, PROC_MODULE,
4358 sym->name, NULL) == FAILURE)
4361 if (gfc_add_interface (sym) == FAILURE)
4364 sym->attr.mod_proc = 1;
4366 if (gfc_match_eos () == MATCH_YES)
4368 if (gfc_match_char (',') != MATCH_YES)
4375 gfc_syntax_error (ST_MODULE_PROC);
4380 /* Match the beginning of a derived type declaration. If a type name
4381 was the result of a function, then it is possible to have a symbol
4382 already to be known as a derived type yet have no components. */
4385 gfc_match_derived_decl (void)
4387 char name[GFC_MAX_SYMBOL_LEN + 1];
4388 symbol_attribute attr;
4392 if (gfc_current_state () == COMP_DERIVED)
4395 gfc_clear_attr (&attr);
4398 if (gfc_match (" , private") == MATCH_YES)
4400 if (gfc_current_state () != COMP_MODULE)
4402 gfc_error ("Derived type at %C can only be PRIVATE in the "
4403 "specification part of a module");
4407 if (gfc_add_access (&attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
4412 if (gfc_match (" , public") == MATCH_YES)
4414 if (gfc_current_state () != COMP_MODULE)
4416 gfc_error ("Derived type at %C can only be PUBLIC in the "
4417 "specification part of a module");
4421 if (gfc_add_access (&attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
4426 if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
4428 gfc_error ("Expected :: in TYPE definition at %C");
4432 m = gfc_match (" %n%t", name);
4436 /* Make sure the name isn't the name of an intrinsic type. The
4437 'double {precision,complex}' types don't get past the name
4438 matcher, unless they're written as a single word or in fixed
4440 if (strcmp (name, "integer") == 0
4441 || strcmp (name, "real") == 0
4442 || strcmp (name, "character") == 0
4443 || strcmp (name, "logical") == 0
4444 || strcmp (name, "complex") == 0
4445 || strcmp (name, "doubleprecision") == 0
4446 || strcmp (name, "doublecomplex") == 0)
4448 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
4453 if (gfc_get_symbol (name, NULL, &sym))
4456 if (sym->ts.type != BT_UNKNOWN)
4458 gfc_error ("Derived type name '%s' at %C already has a basic type "
4459 "of %s", sym->name, gfc_typename (&sym->ts));
4463 /* The symbol may already have the derived attribute without the
4464 components. The ways this can happen is via a function
4465 definition, an INTRINSIC statement or a subtype in another
4466 derived type that is a pointer. The first part of the AND clause
4467 is true if a the symbol is not the return value of a function. */
4468 if (sym->attr.flavor != FL_DERIVED
4469 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
4472 if (sym->components != NULL)
4474 gfc_error ("Derived type definition of '%s' at %C has already been "
4475 "defined", sym->name);
4479 if (attr.access != ACCESS_UNKNOWN
4480 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
4483 gfc_new_block = sym;
4489 /* Cray Pointees can be declared as:
4490 pointer (ipt, a (n,m,...,*))
4491 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
4492 cheat and set a constant bound of 1 for the last dimension, if this
4493 is the case. Since there is no bounds-checking for Cray Pointees,
4494 this will be okay. */
4497 gfc_mod_pointee_as (gfc_array_spec *as)
4499 as->cray_pointee = true; /* This will be useful to know later. */
4500 if (as->type == AS_ASSUMED_SIZE)
4502 as->type = AS_EXPLICIT;
4503 as->upper[as->rank - 1] = gfc_int_expr (1);
4504 as->cp_was_assumed = true;
4506 else if (as->type == AS_ASSUMED_SHAPE)
4508 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
4515 /* Match the enum definition statement, here we are trying to match
4516 the first line of enum definition statement.
4517 Returns MATCH_YES if match is found. */
4520 gfc_match_enum (void)
4524 m = gfc_match_eos ();
4528 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
4536 /* Match a variable name with an optional initializer. When this
4537 subroutine is called, a variable is expected to be parsed next.
4538 Depending on what is happening at the moment, updates either the
4539 symbol table or the current interface. */
4542 enumerator_decl (void)
4544 char name[GFC_MAX_SYMBOL_LEN + 1];
4545 gfc_expr *initializer;
4546 gfc_array_spec *as = NULL;
4554 old_locus = gfc_current_locus;
4556 /* When we get here, we've just matched a list of attributes and
4557 maybe a type and a double colon. The next thing we expect to see
4558 is the name of the symbol. */
4559 m = gfc_match_name (name);
4563 var_locus = gfc_current_locus;
4565 /* OK, we've successfully matched the declaration. Now put the
4566 symbol in the current namespace. If we fail to create the symbol,
4568 if (build_sym (name, NULL, &as, &var_locus) == FAILURE)
4574 /* The double colon must be present in order to have initializers.
4575 Otherwise the statement is ambiguous with an assignment statement. */
4578 if (gfc_match_char ('=') == MATCH_YES)
4580 m = gfc_match_init_expr (&initializer);
4583 gfc_error ("Expected an initialization expression at %C");
4592 /* If we do not have an initializer, the initialization value of the
4593 previous enumerator (stored in last_initializer) is incremented
4594 by 1 and is used to initialize the current enumerator. */
4595 if (initializer == NULL)
4596 initializer = gfc_enum_initializer (last_initializer, old_locus);
4598 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
4600 gfc_error("ENUMERATOR %L not initialized with integer expression",
4603 gfc_free_enum_history ();
4607 /* Store this current initializer, for the next enumerator variable
4608 to be parsed. add_init_expr_to_sym() zeros initializer, so we
4609 use last_initializer below. */
4610 last_initializer = initializer;
4611 t = add_init_expr_to_sym (name, &initializer, &var_locus);
4613 /* Maintain enumerator history. */
4614 gfc_find_symbol (name, NULL, 0, &sym);
4615 create_enum_history (sym, last_initializer);
4617 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
4620 /* Free stuff up and return. */
4621 gfc_free_expr (initializer);
4627 /* Match the enumerator definition statement. */
4630 gfc_match_enumerator_def (void)
4635 gfc_clear_ts (¤t_ts);
4637 m = gfc_match (" enumerator");
4641 m = gfc_match (" :: ");
4642 if (m == MATCH_ERROR)
4645 colon_seen = (m == MATCH_YES);
4647 if (gfc_current_state () != COMP_ENUM)
4649 gfc_error ("ENUM definition statement expected before %C");
4650 gfc_free_enum_history ();
4654 (¤t_ts)->type = BT_INTEGER;
4655 (¤t_ts)->kind = gfc_c_int_kind;
4657 gfc_clear_attr (¤t_attr);
4658 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
4667 m = enumerator_decl ();
4668 if (m == MATCH_ERROR)
4673 if (gfc_match_eos () == MATCH_YES)
4675 if (gfc_match_char (',') != MATCH_YES)
4679 if (gfc_current_state () == COMP_ENUM)
4681 gfc_free_enum_history ();
4682 gfc_error ("Syntax error in ENUMERATOR definition at %C");
4687 gfc_free_array_spec (current_as);