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];
305 m = gfc_match_literal_constant (&expr, 1);
312 if (m == MATCH_ERROR)
315 m = gfc_match_null (result);
319 m = gfc_match_name (name);
323 if (gfc_find_symbol (name, NULL, 1, &sym))
327 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
329 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
333 else if (sym->attr.flavor == FL_DERIVED)
334 return gfc_match_structure_constructor (sym, result);
336 *result = gfc_copy_expr (sym->value);
341 /* Match a list of values in a DATA statement. The leading '/' has
342 already been seen at this point. */
345 top_val_list (gfc_data *data)
347 gfc_data_value *new, *tail;
356 m = match_data_constant (&expr);
359 if (m == MATCH_ERROR)
362 new = gfc_get_data_value ();
371 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
379 msg = gfc_extract_int (expr, &tmp);
380 gfc_free_expr (expr);
388 m = match_data_constant (&tail->expr);
391 if (m == MATCH_ERROR)
395 if (gfc_match_char ('/') == MATCH_YES)
397 if (gfc_match_char (',') == MATCH_NO)
404 gfc_syntax_error (ST_DATA);
405 gfc_free_data_all (gfc_current_ns);
410 /* Matches an old style initialization. */
413 match_old_style_init (const char *name)
420 /* Set up data structure to hold initializers. */
421 gfc_find_sym_tree (name, NULL, 0, &st);
424 newdata = gfc_get_data ();
425 newdata->var = gfc_get_data_variable ();
426 newdata->var->expr = gfc_get_variable_expr (st);
427 newdata->where = gfc_current_locus;
429 /* Match initial value list. This also eats the terminal
431 m = top_val_list (newdata);
440 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
445 /* Mark the variable as having appeared in a data statement. */
446 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
452 /* Chain in namespace list of DATA initializers. */
453 newdata->next = gfc_current_ns->data;
454 gfc_current_ns->data = newdata;
460 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
461 we are matching a DATA statement and are therefore issuing an error
462 if we encounter something unexpected, if not, we're trying to match
463 an old-style initialization expression of the form INTEGER I /2/. */
466 gfc_match_data (void)
471 gfc_set_in_match_data (true);
475 new = gfc_get_data ();
476 new->where = gfc_current_locus;
478 m = top_var_list (new);
482 m = top_val_list (new);
486 new->next = gfc_current_ns->data;
487 gfc_current_ns->data = new;
489 if (gfc_match_eos () == MATCH_YES)
492 gfc_match_char (','); /* Optional comma */
495 gfc_set_in_match_data (false);
499 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
506 gfc_set_in_match_data (false);
512 /************************ Declaration statements *********************/
514 /* Match an intent specification. Since this can only happen after an
515 INTENT word, a legal intent-spec must follow. */
518 match_intent_spec (void)
521 if (gfc_match (" ( in out )") == MATCH_YES)
523 if (gfc_match (" ( in )") == MATCH_YES)
525 if (gfc_match (" ( out )") == MATCH_YES)
528 gfc_error ("Bad INTENT specification at %C");
529 return INTENT_UNKNOWN;
533 /* Matches a character length specification, which is either a
534 specification expression or a '*'. */
537 char_len_param_value (gfc_expr **expr)
539 if (gfc_match_char ('*') == MATCH_YES)
545 return gfc_match_expr (expr);
549 /* A character length is a '*' followed by a literal integer or a
550 char_len_param_value in parenthesis. */
553 match_char_length (gfc_expr **expr)
558 m = gfc_match_char ('*');
562 m = gfc_match_small_literal_int (&length, NULL);
563 if (m == MATCH_ERROR)
568 *expr = gfc_int_expr (length);
572 if (gfc_match_char ('(') == MATCH_NO)
575 m = char_len_param_value (expr);
576 if (m == MATCH_ERROR)
581 if (gfc_match_char (')') == MATCH_NO)
583 gfc_free_expr (*expr);
591 gfc_error ("Syntax error in character length specification at %C");
596 /* Special subroutine for finding a symbol. Check if the name is found
597 in the current name space. If not, and we're compiling a function or
598 subroutine and the parent compilation unit is an interface, then check
599 to see if the name we've been given is the name of the interface
600 (located in another namespace). */
603 find_special (const char *name, gfc_symbol **result)
608 i = gfc_get_symbol (name, NULL, result);
612 if (gfc_current_state () != COMP_SUBROUTINE
613 && gfc_current_state () != COMP_FUNCTION)
616 s = gfc_state_stack->previous;
620 if (s->state != COMP_INTERFACE)
623 goto end; /* Nameless interface */
625 if (strcmp (name, s->sym->name) == 0)
636 /* Special subroutine for getting a symbol node associated with a
637 procedure name, used in SUBROUTINE and FUNCTION statements. The
638 symbol is created in the parent using with symtree node in the
639 child unit pointing to the symbol. If the current namespace has no
640 parent, then the symbol is just created in the current unit. */
643 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
649 /* Module functions have to be left in their own namespace because
650 they have potentially (almost certainly!) already been referenced.
651 In this sense, they are rather like external functions. This is
652 fixed up in resolve.c(resolve_entries), where the symbol name-
653 space is set to point to the master function, so that the fake
654 result mechanism can work. */
655 if (module_fcn_entry)
656 rc = gfc_get_symbol (name, NULL, result);
658 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
661 gfc_current_ns->refs++;
663 if (sym && !sym->new && gfc_current_state () != COMP_INTERFACE)
665 /* Trap another encompassed procedure with the same name. All
666 these conditions are necessary to avoid picking up an entry
667 whose name clashes with that of the encompassing procedure;
668 this is handled using gsymbols to register unique,globally
670 if (sym->attr.flavor != 0
671 && sym->attr.proc != 0
672 && (sym->attr.subroutine || sym->attr.function)
673 && sym->attr.if_source != IFSRC_UNKNOWN)
674 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
675 name, &sym->declared_at);
677 /* Trap declarations of attributes in encompassing scope. The
678 signature for this is that ts.kind is set. Legitimate
679 references only set ts.type. */
680 if (sym->ts.kind != 0
681 && !sym->attr.implicit_type
682 && sym->attr.proc == 0
683 && gfc_current_ns->parent != NULL
684 && sym->attr.access == 0
685 && !module_fcn_entry)
686 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
687 "and must not have attributes declared at %L",
688 name, &sym->declared_at);
691 if (gfc_current_ns->parent == NULL || *result == NULL)
694 /* Module function entries will already have a symtree in
695 the current namespace but will need one at module level. */
696 if (module_fcn_entry)
697 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
699 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
704 /* See if the procedure should be a module procedure */
706 if (((sym->ns->proc_name != NULL
707 && sym->ns->proc_name->attr.flavor == FL_MODULE
708 && sym->attr.proc != PROC_MODULE) || module_fcn_entry)
709 && gfc_add_procedure (&sym->attr, PROC_MODULE,
710 sym->name, NULL) == FAILURE)
717 /* Function called by variable_decl() that adds a name to the symbol
721 build_sym (const char *name, gfc_charlen *cl,
722 gfc_array_spec **as, locus *var_locus)
724 symbol_attribute attr;
727 if (gfc_get_symbol (name, NULL, &sym))
730 /* Start updating the symbol table. Add basic type attribute
732 if (current_ts.type != BT_UNKNOWN
733 && (sym->attr.implicit_type == 0
734 || !gfc_compare_types (&sym->ts, ¤t_ts))
735 && gfc_add_type (sym, ¤t_ts, var_locus) == FAILURE)
738 if (sym->ts.type == BT_CHARACTER)
741 /* Add dimension attribute if present. */
742 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
746 /* Add attribute to symbol. The copy is so that we can reset the
747 dimension attribute. */
751 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
758 /* Set character constant to the given length. The constant will be padded or
762 gfc_set_constant_character_len (int len, gfc_expr *expr, bool array)
767 gcc_assert (expr->expr_type == EXPR_CONSTANT);
768 gcc_assert (expr->ts.type == BT_CHARACTER && expr->ts.kind == 1);
770 slen = expr->value.character.length;
773 s = gfc_getmem (len + 1);
774 memcpy (s, expr->value.character.string, MIN (len, slen));
776 memset (&s[slen], ' ', len - slen);
778 if (gfc_option.warn_character_truncation && slen > len)
779 gfc_warning_now ("CHARACTER expression at %L is being truncated "
780 "(%d/%d)", &expr->where, slen, len);
782 /* Apply the standard by 'hand' otherwise it gets cleared for
784 if (array && slen < len && !(gfc_option.allow_std & GFC_STD_GNU))
785 gfc_error_now ("The CHARACTER elements of the array constructor "
786 "at %L must have the same length (%d/%d)",
787 &expr->where, slen, len);
790 gfc_free (expr->value.character.string);
791 expr->value.character.string = s;
792 expr->value.character.length = len;
797 /* Function to create and update the enumerator history
798 using the information passed as arguments.
799 Pointer "max_enum" is also updated, to point to
800 enum history node containing largest initializer.
802 SYM points to the symbol node of enumerator.
803 INIT points to its enumerator value. */
806 create_enum_history (gfc_symbol *sym, gfc_expr *init)
808 enumerator_history *new_enum_history;
809 gcc_assert (sym != NULL && init != NULL);
811 new_enum_history = gfc_getmem (sizeof (enumerator_history));
813 new_enum_history->sym = sym;
814 new_enum_history->initializer = init;
815 new_enum_history->next = NULL;
817 if (enum_history == NULL)
819 enum_history = new_enum_history;
820 max_enum = enum_history;
824 new_enum_history->next = enum_history;
825 enum_history = new_enum_history;
827 if (mpz_cmp (max_enum->initializer->value.integer,
828 new_enum_history->initializer->value.integer) < 0)
829 max_enum = new_enum_history;
834 /* Function to free enum kind history. */
837 gfc_free_enum_history (void)
839 enumerator_history *current = enum_history;
840 enumerator_history *next;
842 while (current != NULL)
844 next = current->next;
853 /* Function called by variable_decl() that adds an initialization
854 expression to a symbol. */
857 add_init_expr_to_sym (const char *name, gfc_expr **initp,
860 symbol_attribute attr;
865 if (find_special (name, &sym))
870 /* If this symbol is confirming an implicit parameter type,
871 then an initialization expression is not allowed. */
872 if (attr.flavor == FL_PARAMETER
873 && sym->value != NULL
876 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
885 gfc_error ("Initializer not allowed for COMMON variable '%s' at %C",
892 /* An initializer is required for PARAMETER declarations. */
893 if (attr.flavor == FL_PARAMETER)
895 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
901 /* If a variable appears in a DATA block, it cannot have an
905 gfc_error ("Variable '%s' at %C with an initializer already "
906 "appears in a DATA statement", sym->name);
910 /* Check if the assignment can happen. This has to be put off
911 until later for a derived type variable. */
912 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
913 && gfc_check_assign_symbol (sym, init) == FAILURE)
916 if (sym->ts.type == BT_CHARACTER && sym->ts.cl)
918 /* Update symbol character length according initializer. */
919 if (sym->ts.cl->length == NULL)
921 /* If there are multiple CHARACTER variables declared on
922 the same line, we don't want them to share the same
924 sym->ts.cl = gfc_get_charlen ();
925 sym->ts.cl->next = gfc_current_ns->cl_list;
926 gfc_current_ns->cl_list = sym->ts.cl;
928 if (sym->attr.flavor == FL_PARAMETER
929 && init->expr_type == EXPR_ARRAY)
930 sym->ts.cl->length = gfc_copy_expr (init->ts.cl->length);
932 /* Update initializer character length according symbol. */
933 else if (sym->ts.cl->length->expr_type == EXPR_CONSTANT)
935 int len = mpz_get_si (sym->ts.cl->length->value.integer);
938 if (init->expr_type == EXPR_CONSTANT)
939 gfc_set_constant_character_len (len, init, false);
940 else if (init->expr_type == EXPR_ARRAY)
942 gfc_free_expr (init->ts.cl->length);
943 init->ts.cl->length = gfc_copy_expr (sym->ts.cl->length);
944 for (p = init->value.constructor; p; p = p->next)
945 gfc_set_constant_character_len (len, p->expr, false);
950 /* Add initializer. Make sure we keep the ranks sane. */
951 if (sym->attr.dimension && init->rank == 0)
952 init->rank = sym->as->rank;
958 /* Maintain enumerator history. */
959 if (gfc_current_state () == COMP_ENUM)
960 create_enum_history (sym, init);
966 /* Function called by variable_decl() that adds a name to a structure
970 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
975 /* If the current symbol is of the same derived type that we're
976 constructing, it must have the pointer attribute. */
977 if (current_ts.type == BT_DERIVED
978 && current_ts.derived == gfc_current_block ()
979 && current_attr.pointer == 0)
981 gfc_error ("Component at %C must have the POINTER attribute");
985 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
987 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
989 gfc_error ("Array component of structure at %C must have explicit "
990 "or deferred shape");
995 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1000 gfc_set_component_attr (c, ¤t_attr);
1002 c->initializer = *init;
1010 /* Check array components. */
1015 gfc_error ("Allocatable component at %C must be an array");
1024 if (c->as->type != AS_DEFERRED)
1026 gfc_error ("Pointer array component of structure at %C must have a "
1031 else if (c->allocatable)
1033 if (c->as->type != AS_DEFERRED)
1035 gfc_error ("Allocatable component of structure at %C must have a "
1042 if (c->as->type != AS_EXPLICIT)
1044 gfc_error ("Array component of structure at %C must have an "
1054 /* Match a 'NULL()', and possibly take care of some side effects. */
1057 gfc_match_null (gfc_expr **result)
1063 m = gfc_match (" null ( )");
1067 /* The NULL symbol now has to be/become an intrinsic function. */
1068 if (gfc_get_symbol ("null", NULL, &sym))
1070 gfc_error ("NULL() initialization at %C is ambiguous");
1074 gfc_intrinsic_symbol (sym);
1076 if (sym->attr.proc != PROC_INTRINSIC
1077 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1078 sym->name, NULL) == FAILURE
1079 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1082 e = gfc_get_expr ();
1083 e->where = gfc_current_locus;
1084 e->expr_type = EXPR_NULL;
1085 e->ts.type = BT_UNKNOWN;
1093 /* Match a variable name with an optional initializer. When this
1094 subroutine is called, a variable is expected to be parsed next.
1095 Depending on what is happening at the moment, updates either the
1096 symbol table or the current interface. */
1099 variable_decl (int elem)
1101 char name[GFC_MAX_SYMBOL_LEN + 1];
1102 gfc_expr *initializer, *char_len;
1104 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1115 old_locus = gfc_current_locus;
1117 /* When we get here, we've just matched a list of attributes and
1118 maybe a type and a double colon. The next thing we expect to see
1119 is the name of the symbol. */
1120 m = gfc_match_name (name);
1124 var_locus = gfc_current_locus;
1126 /* Now we could see the optional array spec. or character length. */
1127 m = gfc_match_array_spec (&as);
1128 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1129 cp_as = gfc_copy_array_spec (as);
1130 else if (m == MATCH_ERROR)
1134 as = gfc_copy_array_spec (current_as);
1135 else if (gfc_current_state () == COMP_ENUM)
1137 gfc_error ("Enumerator cannot be array at %C");
1138 gfc_free_enum_history ();
1147 if (current_ts.type == BT_CHARACTER)
1149 switch (match_char_length (&char_len))
1152 cl = gfc_get_charlen ();
1153 cl->next = gfc_current_ns->cl_list;
1154 gfc_current_ns->cl_list = cl;
1156 cl->length = char_len;
1159 /* Non-constant lengths need to be copied after the first
1162 if (elem > 1 && current_ts.cl->length
1163 && current_ts.cl->length->expr_type != EXPR_CONSTANT)
1165 cl = gfc_get_charlen ();
1166 cl->next = gfc_current_ns->cl_list;
1167 gfc_current_ns->cl_list = cl;
1168 cl->length = gfc_copy_expr (current_ts.cl->length);
1180 /* If this symbol has already shown up in a Cray Pointer declaration,
1181 then we want to set the type & bail out. */
1182 if (gfc_option.flag_cray_pointer)
1184 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1185 if (sym != NULL && sym->attr.cray_pointee)
1187 sym->ts.type = current_ts.type;
1188 sym->ts.kind = current_ts.kind;
1190 sym->ts.derived = current_ts.derived;
1193 /* Check to see if we have an array specification. */
1196 if (sym->as != NULL)
1198 gfc_error ("Duplicate array spec for Cray pointee at %C");
1199 gfc_free_array_spec (cp_as);
1205 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1206 gfc_internal_error ("Couldn't set pointee array spec.");
1208 /* Fix the array spec. */
1209 m = gfc_mod_pointee_as (sym->as);
1210 if (m == MATCH_ERROR)
1218 gfc_free_array_spec (cp_as);
1223 /* OK, we've successfully matched the declaration. Now put the
1224 symbol in the current namespace, because it might be used in the
1225 optional initialization expression for this symbol, e.g. this is
1228 integer, parameter :: i = huge(i)
1230 This is only true for parameters or variables of a basic type.
1231 For components of derived types, it is not true, so we don't
1232 create a symbol for those yet. If we fail to create the symbol,
1234 if (gfc_current_state () != COMP_DERIVED
1235 && build_sym (name, cl, &as, &var_locus) == FAILURE)
1241 /* An interface body specifies all of the procedure's characteristics and these
1242 shall be consistent with those specified in the procedure definition, except
1243 that the interface may specify a procedure that is not pure if the procedure
1244 is defined to be pure(12.3.2). */
1245 if (current_ts.type == BT_DERIVED
1246 && gfc_current_ns->proc_name
1247 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1248 && current_ts.derived->ns != gfc_current_ns
1249 && !gfc_current_ns->has_import_set)
1251 gfc_error ("the type of '%s' at %C has not been declared within the "
1257 /* In functions that have a RESULT variable defined, the function
1258 name always refers to function calls. Therefore, the name is
1259 not allowed to appear in specification statements. */
1260 if (gfc_current_state () == COMP_FUNCTION
1261 && gfc_current_block () != NULL
1262 && gfc_current_block ()->result != NULL
1263 && gfc_current_block ()->result != gfc_current_block ()
1264 && strcmp (gfc_current_block ()->name, name) == 0)
1266 gfc_error ("Function name '%s' not allowed at %C", name);
1271 /* We allow old-style initializations of the form
1272 integer i /2/, j(4) /3*3, 1/
1273 (if no colon has been seen). These are different from data
1274 statements in that initializers are only allowed to apply to the
1275 variable immediately preceding, i.e.
1277 is not allowed. Therefore we have to do some work manually, that
1278 could otherwise be left to the matchers for DATA statements. */
1280 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1282 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1283 "initialization at %C") == FAILURE)
1286 return match_old_style_init (name);
1289 /* The double colon must be present in order to have initializers.
1290 Otherwise the statement is ambiguous with an assignment statement. */
1293 if (gfc_match (" =>") == MATCH_YES)
1295 if (!current_attr.pointer)
1297 gfc_error ("Initialization at %C isn't for a pointer variable");
1302 m = gfc_match_null (&initializer);
1305 gfc_error ("Pointer initialization requires a NULL() at %C");
1309 if (gfc_pure (NULL))
1311 gfc_error ("Initialization of pointer at %C is not allowed in "
1312 "a PURE procedure");
1320 else if (gfc_match_char ('=') == MATCH_YES)
1322 if (current_attr.pointer)
1324 gfc_error ("Pointer initialization at %C requires '=>', "
1330 m = gfc_match_init_expr (&initializer);
1333 gfc_error ("Expected an initialization expression at %C");
1337 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL))
1339 gfc_error ("Initialization of variable at %C is not allowed in "
1340 "a PURE procedure");
1349 if (initializer != NULL && current_attr.allocatable
1350 && gfc_current_state () == COMP_DERIVED)
1352 gfc_error ("Initialization of allocatable component at %C is not "
1358 /* Check if we are parsing an enumeration and if the current enumerator
1359 variable has an initializer or not. If it does not have an
1360 initializer, the initialization value of the previous enumerator
1361 (stored in last_initializer) is incremented by 1 and is used to
1362 initialize the current enumerator. */
1363 if (gfc_current_state () == COMP_ENUM)
1365 if (initializer == NULL)
1366 initializer = gfc_enum_initializer (last_initializer, old_locus);
1368 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
1370 gfc_error("ENUMERATOR %L not initialized with integer expression",
1373 gfc_free_enum_history ();
1377 /* Store this current initializer, for the next enumerator
1378 variable to be parsed. */
1379 last_initializer = initializer;
1382 /* Add the initializer. Note that it is fine if initializer is
1383 NULL here, because we sometimes also need to check if a
1384 declaration *must* have an initialization expression. */
1385 if (gfc_current_state () != COMP_DERIVED)
1386 t = add_init_expr_to_sym (name, &initializer, &var_locus);
1389 if (current_ts.type == BT_DERIVED
1390 && !current_attr.pointer && !initializer)
1391 initializer = gfc_default_initializer (¤t_ts);
1392 t = build_struct (name, cl, &initializer, &as);
1395 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
1398 /* Free stuff up and return. */
1399 gfc_free_expr (initializer);
1400 gfc_free_array_spec (as);
1406 /* Match an extended-f77 kind specification. */
1409 gfc_match_old_kind_spec (gfc_typespec *ts)
1414 if (gfc_match_char ('*') != MATCH_YES)
1417 m = gfc_match_small_literal_int (&ts->kind, NULL);
1421 original_kind = ts->kind;
1423 /* Massage the kind numbers for complex types. */
1424 if (ts->type == BT_COMPLEX)
1428 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1429 gfc_basic_typename (ts->type), original_kind);
1435 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1437 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1438 gfc_basic_typename (ts->type), original_kind);
1442 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
1443 gfc_basic_typename (ts->type), original_kind) == FAILURE)
1450 /* Match a kind specification. Since kinds are generally optional, we
1451 usually return MATCH_NO if something goes wrong. If a "kind="
1452 string is found, then we know we have an error. */
1455 gfc_match_kind_spec (gfc_typespec *ts)
1465 where = gfc_current_locus;
1467 if (gfc_match_char ('(') == MATCH_NO)
1470 /* Also gobbles optional text. */
1471 if (gfc_match (" kind = ") == MATCH_YES)
1474 n = gfc_match_init_expr (&e);
1476 gfc_error ("Expected initialization expression at %C");
1482 gfc_error ("Expected scalar initialization expression at %C");
1487 msg = gfc_extract_int (e, &ts->kind);
1498 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
1500 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
1501 gfc_basic_typename (ts->type));
1507 if (gfc_match_char (')') != MATCH_YES)
1509 gfc_error ("Missing right parenthesis at %C");
1517 gfc_current_locus = where;
1522 /* Match the various kind/length specifications in a CHARACTER
1523 declaration. We don't return MATCH_NO. */
1526 match_char_spec (gfc_typespec *ts)
1528 int i, kind, seen_length;
1533 kind = gfc_default_character_kind;
1537 /* Try the old-style specification first. */
1538 old_char_selector = 0;
1540 m = match_char_length (&len);
1544 old_char_selector = 1;
1549 m = gfc_match_char ('(');
1552 m = MATCH_YES; /* character without length is a single char */
1556 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ) */
1557 if (gfc_match (" kind =") == MATCH_YES)
1559 m = gfc_match_small_int (&kind);
1560 if (m == MATCH_ERROR)
1565 if (gfc_match (" , len =") == MATCH_NO)
1568 m = char_len_param_value (&len);
1571 if (m == MATCH_ERROR)
1578 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>" */
1579 if (gfc_match (" len =") == MATCH_YES)
1581 m = char_len_param_value (&len);
1584 if (m == MATCH_ERROR)
1588 if (gfc_match_char (')') == MATCH_YES)
1591 if (gfc_match (" , kind =") != MATCH_YES)
1594 gfc_match_small_int (&kind);
1596 if (gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1598 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1605 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ) */
1606 m = char_len_param_value (&len);
1609 if (m == MATCH_ERROR)
1613 m = gfc_match_char (')');
1617 if (gfc_match_char (',') != MATCH_YES)
1620 gfc_match (" kind ="); /* Gobble optional text */
1622 m = gfc_match_small_int (&kind);
1623 if (m == MATCH_ERROR)
1629 /* Require a right-paren at this point. */
1630 m = gfc_match_char (')');
1635 gfc_error ("Syntax error in CHARACTER declaration at %C");
1639 if (m == MATCH_YES && gfc_validate_kind (BT_CHARACTER, kind, true) < 0)
1641 gfc_error ("Kind %d is not a CHARACTER kind at %C", kind);
1647 gfc_free_expr (len);
1651 /* Do some final massaging of the length values. */
1652 cl = gfc_get_charlen ();
1653 cl->next = gfc_current_ns->cl_list;
1654 gfc_current_ns->cl_list = cl;
1656 if (seen_length == 0)
1657 cl->length = gfc_int_expr (1);
1660 if (len == NULL || gfc_extract_int (len, &i) != NULL || i >= 0)
1664 gfc_free_expr (len);
1665 cl->length = gfc_int_expr (0);
1676 /* Matches a type specification. If successful, sets the ts structure
1677 to the matched specification. This is necessary for FUNCTION and
1678 IMPLICIT statements.
1680 If implicit_flag is nonzero, then we don't check for the optional
1681 kind specification. Not doing so is needed for matching an IMPLICIT
1682 statement correctly. */
1685 match_type_spec (gfc_typespec *ts, int implicit_flag)
1687 char name[GFC_MAX_SYMBOL_LEN + 1];
1694 if (gfc_match (" byte") == MATCH_YES)
1696 if (gfc_notify_std(GFC_STD_GNU, "Extension: BYTE type at %C")
1700 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
1702 gfc_error ("BYTE type used at %C "
1703 "is not available on the target machine");
1707 ts->type = BT_INTEGER;
1712 if (gfc_match (" integer") == MATCH_YES)
1714 ts->type = BT_INTEGER;
1715 ts->kind = gfc_default_integer_kind;
1719 if (gfc_match (" character") == MATCH_YES)
1721 ts->type = BT_CHARACTER;
1722 if (implicit_flag == 0)
1723 return match_char_spec (ts);
1728 if (gfc_match (" real") == MATCH_YES)
1731 ts->kind = gfc_default_real_kind;
1735 if (gfc_match (" double precision") == MATCH_YES)
1738 ts->kind = gfc_default_double_kind;
1742 if (gfc_match (" complex") == MATCH_YES)
1744 ts->type = BT_COMPLEX;
1745 ts->kind = gfc_default_complex_kind;
1749 if (gfc_match (" double complex") == MATCH_YES)
1751 if (gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C does not "
1752 "conform to the Fortran 95 standard") == FAILURE)
1755 ts->type = BT_COMPLEX;
1756 ts->kind = gfc_default_double_kind;
1760 if (gfc_match (" logical") == MATCH_YES)
1762 ts->type = BT_LOGICAL;
1763 ts->kind = gfc_default_logical_kind;
1767 m = gfc_match (" type ( %n )", name);
1771 /* Search for the name but allow the components to be defined later. */
1772 if (gfc_get_ha_symbol (name, &sym))
1774 gfc_error ("Type name '%s' at %C is ambiguous", name);
1778 if (sym->attr.flavor != FL_DERIVED
1779 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
1782 ts->type = BT_DERIVED;
1789 /* For all types except double, derived and character, look for an
1790 optional kind specifier. MATCH_NO is actually OK at this point. */
1791 if (implicit_flag == 1)
1794 if (gfc_current_form == FORM_FREE)
1796 c = gfc_peek_char();
1797 if (!gfc_is_whitespace(c) && c != '*' && c != '('
1798 && c != ':' && c != ',')
1802 m = gfc_match_kind_spec (ts);
1803 if (m == MATCH_NO && ts->type != BT_CHARACTER)
1804 m = gfc_match_old_kind_spec (ts);
1807 m = MATCH_YES; /* No kind specifier found. */
1813 /* Match an IMPLICIT NONE statement. Actually, this statement is
1814 already matched in parse.c, or we would not end up here in the
1815 first place. So the only thing we need to check, is if there is
1816 trailing garbage. If not, the match is successful. */
1819 gfc_match_implicit_none (void)
1821 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
1825 /* Match the letter range(s) of an IMPLICIT statement. */
1828 match_implicit_range (void)
1830 int c, c1, c2, inner;
1833 cur_loc = gfc_current_locus;
1835 gfc_gobble_whitespace ();
1836 c = gfc_next_char ();
1839 gfc_error ("Missing character range in IMPLICIT at %C");
1846 gfc_gobble_whitespace ();
1847 c1 = gfc_next_char ();
1851 gfc_gobble_whitespace ();
1852 c = gfc_next_char ();
1857 inner = 0; /* Fall through */
1864 gfc_gobble_whitespace ();
1865 c2 = gfc_next_char ();
1869 gfc_gobble_whitespace ();
1870 c = gfc_next_char ();
1872 if ((c != ',') && (c != ')'))
1885 gfc_error ("Letters must be in alphabetic order in "
1886 "IMPLICIT statement at %C");
1890 /* See if we can add the newly matched range to the pending
1891 implicits from this IMPLICIT statement. We do not check for
1892 conflicts with whatever earlier IMPLICIT statements may have
1893 set. This is done when we've successfully finished matching
1895 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
1902 gfc_syntax_error (ST_IMPLICIT);
1904 gfc_current_locus = cur_loc;
1909 /* Match an IMPLICIT statement, storing the types for
1910 gfc_set_implicit() if the statement is accepted by the parser.
1911 There is a strange looking, but legal syntactic construction
1912 possible. It looks like:
1914 IMPLICIT INTEGER (a-b) (c-d)
1916 This is legal if "a-b" is a constant expression that happens to
1917 equal one of the legal kinds for integers. The real problem
1918 happens with an implicit specification that looks like:
1920 IMPLICIT INTEGER (a-b)
1922 In this case, a typespec matcher that is "greedy" (as most of the
1923 matchers are) gobbles the character range as a kindspec, leaving
1924 nothing left. We therefore have to go a bit more slowly in the
1925 matching process by inhibiting the kindspec checking during
1926 typespec matching and checking for a kind later. */
1929 gfc_match_implicit (void)
1936 /* We don't allow empty implicit statements. */
1937 if (gfc_match_eos () == MATCH_YES)
1939 gfc_error ("Empty IMPLICIT statement at %C");
1945 /* First cleanup. */
1946 gfc_clear_new_implicit ();
1948 /* A basic type is mandatory here. */
1949 m = match_type_spec (&ts, 1);
1950 if (m == MATCH_ERROR)
1955 cur_loc = gfc_current_locus;
1956 m = match_implicit_range ();
1960 /* We may have <TYPE> (<RANGE>). */
1961 gfc_gobble_whitespace ();
1962 c = gfc_next_char ();
1963 if ((c == '\n') || (c == ','))
1965 /* Check for CHARACTER with no length parameter. */
1966 if (ts.type == BT_CHARACTER && !ts.cl)
1968 ts.kind = gfc_default_character_kind;
1969 ts.cl = gfc_get_charlen ();
1970 ts.cl->next = gfc_current_ns->cl_list;
1971 gfc_current_ns->cl_list = ts.cl;
1972 ts.cl->length = gfc_int_expr (1);
1975 /* Record the Successful match. */
1976 if (gfc_merge_new_implicit (&ts) != SUCCESS)
1981 gfc_current_locus = cur_loc;
1984 /* Discard the (incorrectly) matched range. */
1985 gfc_clear_new_implicit ();
1987 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
1988 if (ts.type == BT_CHARACTER)
1989 m = match_char_spec (&ts);
1992 m = gfc_match_kind_spec (&ts);
1995 m = gfc_match_old_kind_spec (&ts);
1996 if (m == MATCH_ERROR)
2002 if (m == MATCH_ERROR)
2005 m = match_implicit_range ();
2006 if (m == MATCH_ERROR)
2011 gfc_gobble_whitespace ();
2012 c = gfc_next_char ();
2013 if ((c != '\n') && (c != ','))
2016 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2024 gfc_syntax_error (ST_IMPLICIT);
2031 gfc_match_import (void)
2033 char name[GFC_MAX_SYMBOL_LEN + 1];
2038 if (gfc_current_ns->proc_name == NULL ||
2039 gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2041 gfc_error ("IMPORT statement at %C only permitted in "
2042 "an INTERFACE body");
2046 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2050 if (gfc_match_eos () == MATCH_YES)
2052 /* All host variables should be imported. */
2053 gfc_current_ns->has_import_set = 1;
2057 if (gfc_match (" ::") == MATCH_YES)
2059 if (gfc_match_eos () == MATCH_YES)
2061 gfc_error ("Expecting list of named entities at %C");
2068 m = gfc_match (" %n", name);
2072 if (gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2074 gfc_error ("Type name '%s' at %C is ambiguous", name);
2080 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2081 "at %C - does not exist.", name);
2085 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
2087 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2092 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
2095 sym->ns = gfc_current_ns;
2107 if (gfc_match_eos () == MATCH_YES)
2109 if (gfc_match_char (',') != MATCH_YES)
2116 gfc_error ("Syntax error in IMPORT statement at %C");
2120 /* Matches an attribute specification including array specs. If
2121 successful, leaves the variables current_attr and current_as
2122 holding the specification. Also sets the colon_seen variable for
2123 later use by matchers associated with initializations.
2125 This subroutine is a little tricky in the sense that we don't know
2126 if we really have an attr-spec until we hit the double colon.
2127 Until that time, we can only return MATCH_NO. This forces us to
2128 check for duplicate specification at this level. */
2131 match_attr_spec (void)
2133 /* Modifiers that can exist in a type statement. */
2135 { GFC_DECL_BEGIN = 0,
2136 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
2137 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
2138 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
2139 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
2140 DECL_COLON, DECL_NONE,
2141 GFC_DECL_END /* Sentinel */
2145 /* GFC_DECL_END is the sentinel, index starts at 0. */
2146 #define NUM_DECL GFC_DECL_END
2148 static mstring decls[] = {
2149 minit (", allocatable", DECL_ALLOCATABLE),
2150 minit (", dimension", DECL_DIMENSION),
2151 minit (", external", DECL_EXTERNAL),
2152 minit (", intent ( in )", DECL_IN),
2153 minit (", intent ( out )", DECL_OUT),
2154 minit (", intent ( in out )", DECL_INOUT),
2155 minit (", intrinsic", DECL_INTRINSIC),
2156 minit (", optional", DECL_OPTIONAL),
2157 minit (", parameter", DECL_PARAMETER),
2158 minit (", pointer", DECL_POINTER),
2159 minit (", protected", DECL_PROTECTED),
2160 minit (", private", DECL_PRIVATE),
2161 minit (", public", DECL_PUBLIC),
2162 minit (", save", DECL_SAVE),
2163 minit (", target", DECL_TARGET),
2164 minit (", value", DECL_VALUE),
2165 minit (", volatile", DECL_VOLATILE),
2166 minit ("::", DECL_COLON),
2167 minit (NULL, DECL_NONE)
2170 locus start, seen_at[NUM_DECL];
2177 gfc_clear_attr (¤t_attr);
2178 start = gfc_current_locus;
2183 /* See if we get all of the keywords up to the final double colon. */
2184 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2189 d = (decl_types) gfc_match_strings (decls);
2190 if (d == DECL_NONE || d == DECL_COLON)
2193 if (gfc_current_state () == COMP_ENUM)
2195 gfc_error ("Enumerator cannot have attributes %C");
2200 seen_at[d] = gfc_current_locus;
2202 if (d == DECL_DIMENSION)
2204 m = gfc_match_array_spec (¤t_as);
2208 gfc_error ("Missing dimension specification at %C");
2212 if (m == MATCH_ERROR)
2217 /* If we are parsing an enumeration and have ensured that no other
2218 attributes are present we can now set the parameter attribute. */
2219 if (gfc_current_state () == COMP_ENUM)
2221 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, NULL);
2229 /* No double colon, so assume that we've been looking at something
2230 else the whole time. */
2237 /* Since we've seen a double colon, we have to be looking at an
2238 attr-spec. This means that we can now issue errors. */
2239 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2244 case DECL_ALLOCATABLE:
2245 attr = "ALLOCATABLE";
2247 case DECL_DIMENSION:
2254 attr = "INTENT (IN)";
2257 attr = "INTENT (OUT)";
2260 attr = "INTENT (IN OUT)";
2262 case DECL_INTRINSIC:
2268 case DECL_PARAMETER:
2274 case DECL_PROTECTED:
2296 attr = NULL; /* This shouldn't happen */
2299 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
2304 /* Now that we've dealt with duplicate attributes, add the attributes
2305 to the current attribute. */
2306 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
2311 if (gfc_current_state () == COMP_DERIVED
2312 && d != DECL_DIMENSION && d != DECL_POINTER
2313 && d != DECL_COLON && d != DECL_NONE)
2315 if (d == DECL_ALLOCATABLE)
2317 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
2318 "attribute at %C in a TYPE definition")
2327 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
2334 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
2335 && gfc_current_state () != COMP_MODULE)
2337 if (d == DECL_PRIVATE)
2342 gfc_error ("%s attribute at %L is not allowed outside of a MODULE",
2350 case DECL_ALLOCATABLE:
2351 t = gfc_add_allocatable (¤t_attr, &seen_at[d]);
2354 case DECL_DIMENSION:
2355 t = gfc_add_dimension (¤t_attr, NULL, &seen_at[d]);
2359 t = gfc_add_external (¤t_attr, &seen_at[d]);
2363 t = gfc_add_intent (¤t_attr, INTENT_IN, &seen_at[d]);
2367 t = gfc_add_intent (¤t_attr, INTENT_OUT, &seen_at[d]);
2371 t = gfc_add_intent (¤t_attr, INTENT_INOUT, &seen_at[d]);
2374 case DECL_INTRINSIC:
2375 t = gfc_add_intrinsic (¤t_attr, &seen_at[d]);
2379 t = gfc_add_optional (¤t_attr, &seen_at[d]);
2382 case DECL_PARAMETER:
2383 t = gfc_add_flavor (¤t_attr, FL_PARAMETER, NULL, &seen_at[d]);
2387 t = gfc_add_pointer (¤t_attr, &seen_at[d]);
2390 case DECL_PROTECTED:
2391 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
2393 gfc_error ("PROTECTED at %C only allowed in specification "
2394 "part of a module");
2399 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
2404 t = gfc_add_protected (¤t_attr, NULL, &seen_at[d]);
2408 t = gfc_add_access (¤t_attr, ACCESS_PRIVATE, NULL,
2413 t = gfc_add_access (¤t_attr, ACCESS_PUBLIC, NULL,
2418 t = gfc_add_save (¤t_attr, NULL, &seen_at[d]);
2422 t = gfc_add_target (¤t_attr, &seen_at[d]);
2426 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
2431 t = gfc_add_value (¤t_attr, NULL, &seen_at[d]);
2435 if (gfc_notify_std (GFC_STD_F2003,
2436 "Fortran 2003: VOLATILE attribute at %C")
2440 t = gfc_add_volatile (¤t_attr, NULL, &seen_at[d]);
2444 gfc_internal_error ("match_attr_spec(): Bad attribute");
2458 gfc_current_locus = start;
2459 gfc_free_array_spec (current_as);
2465 /* Match a data declaration statement. */
2468 gfc_match_data_decl (void)
2474 m = match_type_spec (¤t_ts, 0);
2478 if (current_ts.type == BT_DERIVED && gfc_current_state () != COMP_DERIVED)
2480 sym = gfc_use_derived (current_ts.derived);
2488 current_ts.derived = sym;
2491 m = match_attr_spec ();
2492 if (m == MATCH_ERROR)
2498 if (current_ts.type == BT_DERIVED && current_ts.derived->components == NULL)
2501 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
2504 gfc_find_symbol (current_ts.derived->name,
2505 current_ts.derived->ns->parent, 1, &sym);
2507 /* Any symbol that we find had better be a type definition
2508 which has its components defined. */
2509 if (sym != NULL && sym->attr.flavor == FL_DERIVED
2510 && current_ts.derived->components != NULL)
2513 /* Now we have an error, which we signal, and then fix up
2514 because the knock-on is plain and simple confusing. */
2515 gfc_error_now ("Derived type at %C has not been previously defined "
2516 "and so cannot appear in a derived type definition");
2517 current_attr.pointer = 1;
2522 /* If we have an old-style character declaration, and no new-style
2523 attribute specifications, then there a comma is optional between
2524 the type specification and the variable list. */
2525 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
2526 gfc_match_char (',');
2528 /* Give the types/attributes to symbols that follow. Give the element
2529 a number so that repeat character length expressions can be copied. */
2533 m = variable_decl (elem++);
2534 if (m == MATCH_ERROR)
2539 if (gfc_match_eos () == MATCH_YES)
2541 if (gfc_match_char (',') != MATCH_YES)
2545 if (gfc_error_flag_test () == 0)
2546 gfc_error ("Syntax error in data declaration at %C");
2549 gfc_free_data_all (gfc_current_ns);
2552 gfc_free_array_spec (current_as);
2558 /* Match a prefix associated with a function or subroutine
2559 declaration. If the typespec pointer is nonnull, then a typespec
2560 can be matched. Note that if nothing matches, MATCH_YES is
2561 returned (the null string was matched). */
2564 match_prefix (gfc_typespec *ts)
2568 gfc_clear_attr (¤t_attr);
2572 if (!seen_type && ts != NULL
2573 && match_type_spec (ts, 0) == MATCH_YES
2574 && gfc_match_space () == MATCH_YES)
2581 if (gfc_match ("elemental% ") == MATCH_YES)
2583 if (gfc_add_elemental (¤t_attr, NULL) == FAILURE)
2589 if (gfc_match ("pure% ") == MATCH_YES)
2591 if (gfc_add_pure (¤t_attr, NULL) == FAILURE)
2597 if (gfc_match ("recursive% ") == MATCH_YES)
2599 if (gfc_add_recursive (¤t_attr, NULL) == FAILURE)
2605 /* At this point, the next item is not a prefix. */
2610 /* Copy attributes matched by match_prefix() to attributes on a symbol. */
2613 copy_prefix (symbol_attribute *dest, locus *where)
2615 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
2618 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
2621 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
2628 /* Match a formal argument list. */
2631 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
2633 gfc_formal_arglist *head, *tail, *p, *q;
2634 char name[GFC_MAX_SYMBOL_LEN + 1];
2640 if (gfc_match_char ('(') != MATCH_YES)
2647 if (gfc_match_char (')') == MATCH_YES)
2652 if (gfc_match_char ('*') == MATCH_YES)
2656 m = gfc_match_name (name);
2660 if (gfc_get_symbol (name, NULL, &sym))
2664 p = gfc_get_formal_arglist ();
2676 /* We don't add the VARIABLE flavor because the name could be a
2677 dummy procedure. We don't apply these attributes to formal
2678 arguments of statement functions. */
2679 if (sym != NULL && !st_flag
2680 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
2681 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
2687 /* The name of a program unit can be in a different namespace,
2688 so check for it explicitly. After the statement is accepted,
2689 the name is checked for especially in gfc_get_symbol(). */
2690 if (gfc_new_block != NULL && sym != NULL
2691 && strcmp (sym->name, gfc_new_block->name) == 0)
2693 gfc_error ("Name '%s' at %C is the name of the procedure",
2699 if (gfc_match_char (')') == MATCH_YES)
2702 m = gfc_match_char (',');
2705 gfc_error ("Unexpected junk in formal argument list at %C");
2711 /* Check for duplicate symbols in the formal argument list. */
2714 for (p = head; p->next; p = p->next)
2719 for (q = p->next; q; q = q->next)
2720 if (p->sym == q->sym)
2722 gfc_error ("Duplicate symbol '%s' in formal argument list "
2723 "at %C", p->sym->name);
2731 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL) ==
2741 gfc_free_formal_arglist (head);
2746 /* Match a RESULT specification following a function declaration or
2747 ENTRY statement. Also matches the end-of-statement. */
2750 match_result (gfc_symbol * function, gfc_symbol **result)
2752 char name[GFC_MAX_SYMBOL_LEN + 1];
2756 if (gfc_match (" result (") != MATCH_YES)
2759 m = gfc_match_name (name);
2763 if (gfc_match (" )%t") != MATCH_YES)
2765 gfc_error ("Unexpected junk following RESULT variable at %C");
2769 if (strcmp (function->name, name) == 0)
2771 gfc_error ("RESULT variable at %C must be different than function name");
2775 if (gfc_get_symbol (name, NULL, &r))
2778 if (gfc_add_flavor (&r->attr, FL_VARIABLE, r->name, NULL) == FAILURE
2779 || gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
2788 /* Match a function declaration. */
2791 gfc_match_function_decl (void)
2793 char name[GFC_MAX_SYMBOL_LEN + 1];
2794 gfc_symbol *sym, *result;
2798 if (gfc_current_state () != COMP_NONE
2799 && gfc_current_state () != COMP_INTERFACE
2800 && gfc_current_state () != COMP_CONTAINS)
2803 gfc_clear_ts (¤t_ts);
2805 old_loc = gfc_current_locus;
2807 m = match_prefix (¤t_ts);
2810 gfc_current_locus = old_loc;
2814 if (gfc_match ("function% %n", name) != MATCH_YES)
2816 gfc_current_locus = old_loc;
2820 if (get_proc_name (name, &sym, false))
2822 gfc_new_block = sym;
2824 m = gfc_match_formal_arglist (sym, 0, 0);
2827 gfc_error ("Expected formal argument list in function "
2828 "definition at %C");
2832 else if (m == MATCH_ERROR)
2837 if (gfc_match_eos () != MATCH_YES)
2839 /* See if a result variable is present. */
2840 m = match_result (sym, &result);
2842 gfc_error ("Unexpected junk after function declaration at %C");
2851 /* Make changes to the symbol. */
2854 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2857 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
2858 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
2861 if (current_ts.type != BT_UNKNOWN && sym->ts.type != BT_UNKNOWN
2862 && !sym->attr.implicit_type)
2864 gfc_error ("Function '%s' at %C already has a type of %s", name,
2865 gfc_basic_typename (sym->ts.type));
2871 sym->ts = current_ts;
2876 result->ts = current_ts;
2877 sym->result = result;
2883 gfc_current_locus = old_loc;
2888 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
2889 pass the name of the entry, rather than the gfc_current_block name, and
2890 to return false upon finding an existing global entry. */
2893 add_global_entry (const char *name, int sub)
2897 s = gfc_get_gsymbol(name);
2900 || (s->type != GSYM_UNKNOWN
2901 && s->type != (sub ? GSYM_SUBROUTINE : GSYM_FUNCTION)))
2902 global_used(s, NULL);
2905 s->type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
2906 s->where = gfc_current_locus;
2914 /* Match an ENTRY statement. */
2917 gfc_match_entry (void)
2922 char name[GFC_MAX_SYMBOL_LEN + 1];
2923 gfc_compile_state state;
2927 bool module_procedure;
2929 m = gfc_match_name (name);
2933 state = gfc_current_state ();
2934 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
2939 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
2942 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
2944 case COMP_BLOCK_DATA:
2945 gfc_error ("ENTRY statement at %C cannot appear within "
2948 case COMP_INTERFACE:
2949 gfc_error ("ENTRY statement at %C cannot appear within "
2953 gfc_error ("ENTRY statement at %C cannot appear within "
2954 "a DERIVED TYPE block");
2957 gfc_error ("ENTRY statement at %C cannot appear within "
2958 "an IF-THEN block");
2961 gfc_error ("ENTRY statement at %C cannot appear within "
2965 gfc_error ("ENTRY statement at %C cannot appear within "
2969 gfc_error ("ENTRY statement at %C cannot appear within "
2973 gfc_error ("ENTRY statement at %C cannot appear within "
2977 gfc_error ("ENTRY statement at %C cannot appear within "
2978 "a contained subprogram");
2981 gfc_internal_error ("gfc_match_entry(): Bad state");
2986 module_procedure = gfc_current_ns->parent != NULL
2987 && gfc_current_ns->parent->proc_name
2988 && gfc_current_ns->parent->proc_name->attr.flavor
2991 if (gfc_current_ns->parent != NULL
2992 && gfc_current_ns->parent->proc_name
2993 && !module_procedure)
2995 gfc_error("ENTRY statement at %C cannot appear in a "
2996 "contained procedure");
3000 /* Module function entries need special care in get_proc_name
3001 because previous references within the function will have
3002 created symbols attached to the current namespace. */
3003 if (get_proc_name (name, &entry,
3004 gfc_current_ns->parent != NULL
3006 && gfc_current_ns->proc_name->attr.function))
3009 proc = gfc_current_block ();
3011 if (state == COMP_SUBROUTINE)
3013 /* An entry in a subroutine. */
3014 if (!add_global_entry (name, 1))
3017 m = gfc_match_formal_arglist (entry, 0, 1);
3021 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3022 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
3027 /* An entry in a function.
3028 We need to take special care because writing
3033 ENTRY f() RESULT (r)
3035 ENTRY f RESULT (r). */
3036 if (!add_global_entry (name, 0))
3039 old_loc = gfc_current_locus;
3040 if (gfc_match_eos () == MATCH_YES)
3042 gfc_current_locus = old_loc;
3043 /* Match the empty argument list, and add the interface to
3045 m = gfc_match_formal_arglist (entry, 0, 1);
3048 m = gfc_match_formal_arglist (entry, 0, 0);
3055 if (gfc_match_eos () == MATCH_YES)
3057 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
3058 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
3061 entry->result = entry;
3065 m = match_result (proc, &result);
3067 gfc_syntax_error (ST_ENTRY);
3071 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
3072 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
3073 || gfc_add_function (&entry->attr, result->name, NULL)
3077 entry->result = result;
3080 if (proc->attr.recursive && result == NULL)
3082 gfc_error ("RESULT attribute required in ENTRY statement at %C");
3087 if (gfc_match_eos () != MATCH_YES)
3089 gfc_syntax_error (ST_ENTRY);
3093 entry->attr.recursive = proc->attr.recursive;
3094 entry->attr.elemental = proc->attr.elemental;
3095 entry->attr.pure = proc->attr.pure;
3097 el = gfc_get_entry_list ();
3099 el->next = gfc_current_ns->entries;
3100 gfc_current_ns->entries = el;
3102 el->id = el->next->id + 1;
3106 new_st.op = EXEC_ENTRY;
3107 new_st.ext.entry = el;
3113 /* Match a subroutine statement, including optional prefixes. */
3116 gfc_match_subroutine (void)
3118 char name[GFC_MAX_SYMBOL_LEN + 1];
3122 if (gfc_current_state () != COMP_NONE
3123 && gfc_current_state () != COMP_INTERFACE
3124 && gfc_current_state () != COMP_CONTAINS)
3127 m = match_prefix (NULL);
3131 m = gfc_match ("subroutine% %n", name);
3135 if (get_proc_name (name, &sym, false))
3137 gfc_new_block = sym;
3139 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
3142 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
3145 if (gfc_match_eos () != MATCH_YES)
3147 gfc_syntax_error (ST_SUBROUTINE);
3151 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
3158 /* Return nonzero if we're currently compiling a contained procedure. */
3161 contained_procedure (void)
3165 for (s=gfc_state_stack; s; s=s->previous)
3166 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
3167 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
3173 /* Set the kind of each enumerator. The kind is selected such that it is
3174 interoperable with the corresponding C enumeration type, making
3175 sure that -fshort-enums is honored. */
3180 enumerator_history *current_history = NULL;
3184 if (max_enum == NULL || enum_history == NULL)
3187 if (!gfc_option.fshort_enums)
3193 kind = gfc_integer_kinds[i++].kind;
3195 while (kind < gfc_c_int_kind
3196 && gfc_check_integer_range (max_enum->initializer->value.integer,
3199 current_history = enum_history;
3200 while (current_history != NULL)
3202 current_history->sym->ts.kind = kind;
3203 current_history = current_history->next;
3208 /* Match any of the various end-block statements. Returns the type of
3209 END to the caller. The END INTERFACE, END IF, END DO and END
3210 SELECT statements cannot be replaced by a single END statement. */
3213 gfc_match_end (gfc_statement *st)
3215 char name[GFC_MAX_SYMBOL_LEN + 1];
3216 gfc_compile_state state;
3218 const char *block_name;
3223 old_loc = gfc_current_locus;
3224 if (gfc_match ("end") != MATCH_YES)
3227 state = gfc_current_state ();
3228 block_name = gfc_current_block () == NULL
3229 ? NULL : gfc_current_block ()->name;
3231 if (state == COMP_CONTAINS)
3233 state = gfc_state_stack->previous->state;
3234 block_name = gfc_state_stack->previous->sym == NULL
3235 ? NULL : gfc_state_stack->previous->sym->name;
3242 *st = ST_END_PROGRAM;
3243 target = " program";
3247 case COMP_SUBROUTINE:
3248 *st = ST_END_SUBROUTINE;
3249 target = " subroutine";
3250 eos_ok = !contained_procedure ();
3254 *st = ST_END_FUNCTION;
3255 target = " function";
3256 eos_ok = !contained_procedure ();
3259 case COMP_BLOCK_DATA:
3260 *st = ST_END_BLOCK_DATA;
3261 target = " block data";
3266 *st = ST_END_MODULE;
3271 case COMP_INTERFACE:
3272 *st = ST_END_INTERFACE;
3273 target = " interface";
3296 *st = ST_END_SELECT;
3302 *st = ST_END_FORALL;
3317 last_initializer = NULL;
3319 gfc_free_enum_history ();
3323 gfc_error ("Unexpected END statement at %C");
3327 if (gfc_match_eos () == MATCH_YES)
3331 /* We would have required END [something] */
3332 gfc_error ("%s statement expected at %L",
3333 gfc_ascii_statement (*st), &old_loc);
3340 /* Verify that we've got the sort of end-block that we're expecting. */
3341 if (gfc_match (target) != MATCH_YES)
3343 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
3347 /* If we're at the end, make sure a block name wasn't required. */
3348 if (gfc_match_eos () == MATCH_YES)
3351 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT)
3354 if (gfc_current_block () == NULL)
3357 gfc_error ("Expected block name of '%s' in %s statement at %C",
3358 block_name, gfc_ascii_statement (*st));
3363 /* END INTERFACE has a special handler for its several possible endings. */
3364 if (*st == ST_END_INTERFACE)
3365 return gfc_match_end_interface ();
3367 /* We haven't hit the end of statement, so what is left must be an end-name. */
3368 m = gfc_match_space ();
3370 m = gfc_match_name (name);
3373 gfc_error ("Expected terminating name at %C");
3377 if (block_name == NULL)
3380 if (strcmp (name, block_name) != 0)
3382 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
3383 gfc_ascii_statement (*st));
3387 if (gfc_match_eos () == MATCH_YES)
3391 gfc_syntax_error (*st);
3394 gfc_current_locus = old_loc;
3400 /***************** Attribute declaration statements ****************/
3402 /* Set the attribute of a single variable. */
3407 char name[GFC_MAX_SYMBOL_LEN + 1];
3415 m = gfc_match_name (name);
3419 if (find_special (name, &sym))
3422 var_locus = gfc_current_locus;
3424 /* Deal with possible array specification for certain attributes. */
3425 if (current_attr.dimension
3426 || current_attr.allocatable
3427 || current_attr.pointer
3428 || current_attr.target)
3430 m = gfc_match_array_spec (&as);
3431 if (m == MATCH_ERROR)
3434 if (current_attr.dimension && m == MATCH_NO)
3436 gfc_error ("Missing array specification at %L in DIMENSION "
3437 "statement", &var_locus);
3442 if ((current_attr.allocatable || current_attr.pointer)
3443 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
3445 gfc_error ("Array specification must be deferred at %L", &var_locus);
3451 /* Update symbol table. DIMENSION attribute is set
3452 in gfc_set_array_spec(). */
3453 if (current_attr.dimension == 0
3454 && gfc_copy_attr (&sym->attr, ¤t_attr, NULL) == FAILURE)
3460 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
3466 if (sym->attr.cray_pointee && sym->as != NULL)
3468 /* Fix the array spec. */
3469 m = gfc_mod_pointee_as (sym->as);
3470 if (m == MATCH_ERROR)
3474 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
3480 if ((current_attr.external || current_attr.intrinsic)
3481 && sym->attr.flavor != FL_PROCEDURE
3482 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
3491 gfc_free_array_spec (as);
3496 /* Generic attribute declaration subroutine. Used for attributes that
3497 just have a list of names. */
3504 /* Gobble the optional double colon, by simply ignoring the result
3514 if (gfc_match_eos () == MATCH_YES)
3520 if (gfc_match_char (',') != MATCH_YES)
3522 gfc_error ("Unexpected character in variable list at %C");
3532 /* This routine matches Cray Pointer declarations of the form:
3533 pointer ( <pointer>, <pointee> )
3535 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
3536 The pointer, if already declared, should be an integer. Otherwise, we
3537 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
3538 be either a scalar, or an array declaration. No space is allocated for
3539 the pointee. For the statement
3540 pointer (ipt, ar(10))
3541 any subsequent uses of ar will be translated (in C-notation) as
3542 ar(i) => ((<type> *) ipt)(i)
3543 After gimplification, pointee variable will disappear in the code. */
3546 cray_pointer_decl (void)
3550 gfc_symbol *cptr; /* Pointer symbol. */
3551 gfc_symbol *cpte; /* Pointee symbol. */
3557 if (gfc_match_char ('(') != MATCH_YES)
3559 gfc_error ("Expected '(' at %C");
3563 /* Match pointer. */
3564 var_locus = gfc_current_locus;
3565 gfc_clear_attr (¤t_attr);
3566 gfc_add_cray_pointer (¤t_attr, &var_locus);
3567 current_ts.type = BT_INTEGER;
3568 current_ts.kind = gfc_index_integer_kind;
3570 m = gfc_match_symbol (&cptr, 0);
3573 gfc_error ("Expected variable name at %C");
3577 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
3580 gfc_set_sym_referenced (cptr);
3582 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
3584 cptr->ts.type = BT_INTEGER;
3585 cptr->ts.kind = gfc_index_integer_kind;
3587 else if (cptr->ts.type != BT_INTEGER)
3589 gfc_error ("Cray pointer at %C must be an integer");
3592 else if (cptr->ts.kind < gfc_index_integer_kind)
3593 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
3594 " memory addresses require %d bytes",
3595 cptr->ts.kind, gfc_index_integer_kind);
3597 if (gfc_match_char (',') != MATCH_YES)
3599 gfc_error ("Expected \",\" at %C");
3603 /* Match Pointee. */
3604 var_locus = gfc_current_locus;
3605 gfc_clear_attr (¤t_attr);
3606 gfc_add_cray_pointee (¤t_attr, &var_locus);
3607 current_ts.type = BT_UNKNOWN;
3608 current_ts.kind = 0;
3610 m = gfc_match_symbol (&cpte, 0);
3613 gfc_error ("Expected variable name at %C");
3617 /* Check for an optional array spec. */
3618 m = gfc_match_array_spec (&as);
3619 if (m == MATCH_ERROR)
3621 gfc_free_array_spec (as);
3624 else if (m == MATCH_NO)
3626 gfc_free_array_spec (as);
3630 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
3633 gfc_set_sym_referenced (cpte);
3635 if (cpte->as == NULL)
3637 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
3638 gfc_internal_error ("Couldn't set Cray pointee array spec.");
3640 else if (as != NULL)
3642 gfc_error ("Duplicate array spec for Cray pointee at %C");
3643 gfc_free_array_spec (as);
3649 if (cpte->as != NULL)
3651 /* Fix array spec. */
3652 m = gfc_mod_pointee_as (cpte->as);
3653 if (m == MATCH_ERROR)
3657 /* Point the Pointee at the Pointer. */
3658 cpte->cp_pointer = cptr;
3660 if (gfc_match_char (')') != MATCH_YES)
3662 gfc_error ("Expected \")\" at %C");
3665 m = gfc_match_char (',');
3667 done = true; /* Stop searching for more declarations. */
3671 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
3672 || gfc_match_eos () != MATCH_YES)
3674 gfc_error ("Expected \",\" or end of statement at %C");
3682 gfc_match_external (void)
3685 gfc_clear_attr (¤t_attr);
3686 current_attr.external = 1;
3688 return attr_decl ();
3693 gfc_match_intent (void)
3697 intent = match_intent_spec ();
3698 if (intent == INTENT_UNKNOWN)
3701 gfc_clear_attr (¤t_attr);
3702 current_attr.intent = intent;
3704 return attr_decl ();
3709 gfc_match_intrinsic (void)
3712 gfc_clear_attr (¤t_attr);
3713 current_attr.intrinsic = 1;
3715 return attr_decl ();
3720 gfc_match_optional (void)
3723 gfc_clear_attr (¤t_attr);
3724 current_attr.optional = 1;
3726 return attr_decl ();
3731 gfc_match_pointer (void)
3733 gfc_gobble_whitespace ();
3734 if (gfc_peek_char () == '(')
3736 if (!gfc_option.flag_cray_pointer)
3738 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
3742 return cray_pointer_decl ();
3746 gfc_clear_attr (¤t_attr);
3747 current_attr.pointer = 1;
3749 return attr_decl ();
3755 gfc_match_allocatable (void)
3757 gfc_clear_attr (¤t_attr);
3758 current_attr.allocatable = 1;
3760 return attr_decl ();
3765 gfc_match_dimension (void)
3767 gfc_clear_attr (¤t_attr);
3768 current_attr.dimension = 1;
3770 return attr_decl ();
3775 gfc_match_target (void)
3777 gfc_clear_attr (¤t_attr);
3778 current_attr.target = 1;
3780 return attr_decl ();
3784 /* Match the list of entities being specified in a PUBLIC or PRIVATE
3788 access_attr_decl (gfc_statement st)
3790 char name[GFC_MAX_SYMBOL_LEN + 1];
3791 interface_type type;
3794 gfc_intrinsic_op operator;
3797 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
3802 m = gfc_match_generic_spec (&type, name, &operator);
3805 if (m == MATCH_ERROR)
3810 case INTERFACE_NAMELESS:
3813 case INTERFACE_GENERIC:
3814 if (gfc_get_symbol (name, NULL, &sym))
3817 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
3818 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
3819 sym->name, NULL) == FAILURE)
3824 case INTERFACE_INTRINSIC_OP:
3825 if (gfc_current_ns->operator_access[operator] == ACCESS_UNKNOWN)
3827 gfc_current_ns->operator_access[operator] =
3828 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3832 gfc_error ("Access specification of the %s operator at %C has "
3833 "already been specified", gfc_op2string (operator));
3839 case INTERFACE_USER_OP:
3840 uop = gfc_get_uop (name);
3842 if (uop->access == ACCESS_UNKNOWN)
3844 uop->access = (st == ST_PUBLIC)
3845 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
3849 gfc_error ("Access specification of the .%s. operator at %C "
3850 "has already been specified", sym->name);
3857 if (gfc_match_char (',') == MATCH_NO)
3861 if (gfc_match_eos () != MATCH_YES)
3866 gfc_syntax_error (st);
3874 gfc_match_protected (void)
3879 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3881 gfc_error ("PROTECTED at %C only allowed in specification "
3882 "part of a module");
3887 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
3891 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
3896 if (gfc_match_eos () == MATCH_YES)
3901 m = gfc_match_symbol (&sym, 0);
3905 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
3918 if (gfc_match_eos () == MATCH_YES)
3920 if (gfc_match_char (',') != MATCH_YES)
3927 gfc_error ("Syntax error in PROTECTED statement at %C");
3932 /* The PRIVATE statement is a bit weird in that it can be a attribute
3933 declaration, but also works as a standlone statement inside of a
3934 type declaration or a module. */
3937 gfc_match_private (gfc_statement *st)
3940 if (gfc_match ("private") != MATCH_YES)
3943 if (gfc_current_state () == COMP_DERIVED)
3945 if (gfc_match_eos () == MATCH_YES)
3951 gfc_syntax_error (ST_PRIVATE);
3955 if (gfc_match_eos () == MATCH_YES)
3962 return access_attr_decl (ST_PRIVATE);
3967 gfc_match_public (gfc_statement *st)
3970 if (gfc_match ("public") != MATCH_YES)
3973 if (gfc_match_eos () == MATCH_YES)
3980 return access_attr_decl (ST_PUBLIC);
3984 /* Workhorse for gfc_match_parameter. */
3993 m = gfc_match_symbol (&sym, 0);
3995 gfc_error ("Expected variable name at %C in PARAMETER statement");
4000 if (gfc_match_char ('=') == MATCH_NO)
4002 gfc_error ("Expected = sign in PARAMETER statement at %C");
4006 m = gfc_match_init_expr (&init);
4008 gfc_error ("Expected expression at %C in PARAMETER statement");
4012 if (sym->ts.type == BT_UNKNOWN
4013 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
4019 if (gfc_check_assign_symbol (sym, init) == FAILURE
4020 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
4026 if (sym->ts.type == BT_CHARACTER
4027 && sym->ts.cl != NULL
4028 && sym->ts.cl->length != NULL
4029 && sym->ts.cl->length->expr_type == EXPR_CONSTANT
4030 && init->expr_type == EXPR_CONSTANT
4031 && init->ts.type == BT_CHARACTER
4032 && init->ts.kind == 1)
4033 gfc_set_constant_character_len (
4034 mpz_get_si (sym->ts.cl->length->value.integer), init, false);
4040 gfc_free_expr (init);
4045 /* Match a parameter statement, with the weird syntax that these have. */
4048 gfc_match_parameter (void)
4052 if (gfc_match_char ('(') == MATCH_NO)
4061 if (gfc_match (" )%t") == MATCH_YES)
4064 if (gfc_match_char (',') != MATCH_YES)
4066 gfc_error ("Unexpected characters in PARAMETER statement at %C");
4076 /* Save statements have a special syntax. */
4079 gfc_match_save (void)
4081 char n[GFC_MAX_SYMBOL_LEN+1];
4086 if (gfc_match_eos () == MATCH_YES)
4088 if (gfc_current_ns->seen_save)
4090 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
4091 "follows previous SAVE statement")
4096 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
4100 if (gfc_current_ns->save_all)
4102 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
4103 "blanket SAVE statement")
4112 m = gfc_match_symbol (&sym, 0);
4116 if (gfc_add_save (&sym->attr, sym->name, &gfc_current_locus)
4128 m = gfc_match (" / %n /", &n);
4129 if (m == MATCH_ERROR)
4134 c = gfc_get_common (n, 0);
4137 gfc_current_ns->seen_save = 1;
4140 if (gfc_match_eos () == MATCH_YES)
4142 if (gfc_match_char (',') != MATCH_YES)
4149 gfc_error ("Syntax error in SAVE statement at %C");
4155 gfc_match_value (void)
4160 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
4164 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4169 if (gfc_match_eos () == MATCH_YES)
4174 m = gfc_match_symbol (&sym, 0);
4178 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
4191 if (gfc_match_eos () == MATCH_YES)
4193 if (gfc_match_char (',') != MATCH_YES)
4200 gfc_error ("Syntax error in VALUE statement at %C");
4205 gfc_match_volatile (void)
4210 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
4214 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
4219 if (gfc_match_eos () == MATCH_YES)
4224 m = gfc_match_symbol (&sym, 0);
4228 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
4241 if (gfc_match_eos () == MATCH_YES)
4243 if (gfc_match_char (',') != MATCH_YES)
4250 gfc_error ("Syntax error in VOLATILE statement at %C");
4256 /* Match a module procedure statement. Note that we have to modify
4257 symbols in the parent's namespace because the current one was there
4258 to receive symbols that are in an interface's formal argument list. */
4261 gfc_match_modproc (void)
4263 char name[GFC_MAX_SYMBOL_LEN + 1];
4267 if (gfc_state_stack->state != COMP_INTERFACE
4268 || gfc_state_stack->previous == NULL
4269 || current_interface.type == INTERFACE_NAMELESS)
4271 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
4278 m = gfc_match_name (name);
4284 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4287 if (sym->attr.proc != PROC_MODULE
4288 && gfc_add_procedure (&sym->attr, PROC_MODULE,
4289 sym->name, NULL) == FAILURE)
4292 if (gfc_add_interface (sym) == FAILURE)
4295 sym->attr.mod_proc = 1;
4297 if (gfc_match_eos () == MATCH_YES)
4299 if (gfc_match_char (',') != MATCH_YES)
4306 gfc_syntax_error (ST_MODULE_PROC);
4311 /* Match the beginning of a derived type declaration. If a type name
4312 was the result of a function, then it is possible to have a symbol
4313 already to be known as a derived type yet have no components. */
4316 gfc_match_derived_decl (void)
4318 char name[GFC_MAX_SYMBOL_LEN + 1];
4319 symbol_attribute attr;
4323 if (gfc_current_state () == COMP_DERIVED)
4326 gfc_clear_attr (&attr);
4329 if (gfc_match (" , private") == MATCH_YES)
4331 if (gfc_find_state (COMP_MODULE) == FAILURE)
4333 gfc_error ("Derived type at %C can only be PRIVATE within a MODULE");
4337 if (gfc_add_access (&attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
4342 if (gfc_match (" , public") == MATCH_YES)
4344 if (gfc_find_state (COMP_MODULE) == FAILURE)
4346 gfc_error ("Derived type at %C can only be PUBLIC within a MODULE");
4350 if (gfc_add_access (&attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
4355 if (gfc_match (" ::") != MATCH_YES && attr.access != ACCESS_UNKNOWN)
4357 gfc_error ("Expected :: in TYPE definition at %C");
4361 m = gfc_match (" %n%t", name);
4365 /* Make sure the name isn't the name of an intrinsic type. The
4366 'double precision' type doesn't get past the name matcher. */
4367 if (strcmp (name, "integer") == 0
4368 || strcmp (name, "real") == 0
4369 || strcmp (name, "character") == 0
4370 || strcmp (name, "logical") == 0
4371 || strcmp (name, "complex") == 0)
4373 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
4378 if (gfc_get_symbol (name, NULL, &sym))
4381 if (sym->ts.type != BT_UNKNOWN)
4383 gfc_error ("Derived type name '%s' at %C already has a basic type "
4384 "of %s", sym->name, gfc_typename (&sym->ts));
4388 /* The symbol may already have the derived attribute without the
4389 components. The ways this can happen is via a function
4390 definition, an INTRINSIC statement or a subtype in another
4391 derived type that is a pointer. The first part of the AND clause
4392 is true if a the symbol is not the return value of a function. */
4393 if (sym->attr.flavor != FL_DERIVED
4394 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
4397 if (sym->components != NULL)
4399 gfc_error ("Derived type definition of '%s' at %C has already been "
4400 "defined", sym->name);
4404 if (attr.access != ACCESS_UNKNOWN
4405 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
4408 gfc_new_block = sym;
4414 /* Cray Pointees can be declared as:
4415 pointer (ipt, a (n,m,...,*))
4416 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
4417 cheat and set a constant bound of 1 for the last dimension, if this
4418 is the case. Since there is no bounds-checking for Cray Pointees,
4419 this will be okay. */
4422 gfc_mod_pointee_as (gfc_array_spec *as)
4424 as->cray_pointee = true; /* This will be useful to know later. */
4425 if (as->type == AS_ASSUMED_SIZE)
4427 as->type = AS_EXPLICIT;
4428 as->upper[as->rank - 1] = gfc_int_expr (1);
4429 as->cp_was_assumed = true;
4431 else if (as->type == AS_ASSUMED_SHAPE)
4433 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
4440 /* Match the enum definition statement, here we are trying to match
4441 the first line of enum definition statement.
4442 Returns MATCH_YES if match is found. */
4445 gfc_match_enum (void)
4449 m = gfc_match_eos ();
4453 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM AND ENUMERATOR at %C")
4461 /* Match the enumerator definition statement. */
4464 gfc_match_enumerator_def (void)
4469 gfc_clear_ts (¤t_ts);
4471 m = gfc_match (" enumerator");
4475 if (gfc_current_state () != COMP_ENUM)
4477 gfc_error ("ENUM definition statement expected before %C");
4478 gfc_free_enum_history ();
4482 (¤t_ts)->type = BT_INTEGER;
4483 (¤t_ts)->kind = gfc_c_int_kind;
4485 m = match_attr_spec ();
4486 if (m == MATCH_ERROR)
4495 m = variable_decl (elem++);
4496 if (m == MATCH_ERROR)
4501 if (gfc_match_eos () == MATCH_YES)
4503 if (gfc_match_char (',') != MATCH_YES)
4507 if (gfc_current_state () == COMP_ENUM)
4509 gfc_free_enum_history ();
4510 gfc_error ("Syntax error in ENUMERATOR definition at %C");
4515 gfc_free_array_spec (current_as);