* ChangeLog: Fix whitespace.
[official-gcc.git] / gcc / fortran / decl.c
blob2d405fe983802fb829bb5b014d01d109beb0de4f
1 /* Declaration statement matcher
2 Copyright (C) 2002-2014 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #include "config.h"
22 #include "system.h"
23 #include "coretypes.h"
24 #include "gfortran.h"
25 #include "match.h"
26 #include "parse.h"
27 #include "flags.h"
28 #include "constructor.h"
29 #include "tree.h"
30 #include "stringpool.h"
32 /* Macros to access allocate memory for gfc_data_variable,
33 gfc_data_value and gfc_data. */
34 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
35 #define gfc_get_data_value() XCNEW (gfc_data_value)
36 #define gfc_get_data() XCNEW (gfc_data)
39 static bool set_binding_label (const char **, const char *, int);
42 /* This flag is set if an old-style length selector is matched
43 during a type-declaration statement. */
45 static int old_char_selector;
47 /* When variables acquire types and attributes from a declaration
48 statement, they get them from the following static variables. The
49 first part of a declaration sets these variables and the second
50 part copies these into symbol structures. */
52 static gfc_typespec current_ts;
54 static symbol_attribute current_attr;
55 static gfc_array_spec *current_as;
56 static int colon_seen;
58 /* The current binding label (if any). */
59 static const char* curr_binding_label;
60 /* Need to know how many identifiers are on the current data declaration
61 line in case we're given the BIND(C) attribute with a NAME= specifier. */
62 static int num_idents_on_line;
63 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
64 can supply a name if the curr_binding_label is nil and NAME= was not. */
65 static int has_name_equals = 0;
67 /* Initializer of the previous enumerator. */
69 static gfc_expr *last_initializer;
71 /* History of all the enumerators is maintained, so that
72 kind values of all the enumerators could be updated depending
73 upon the maximum initialized value. */
75 typedef struct enumerator_history
77 gfc_symbol *sym;
78 gfc_expr *initializer;
79 struct enumerator_history *next;
81 enumerator_history;
83 /* Header of enum history chain. */
85 static enumerator_history *enum_history = NULL;
87 /* Pointer of enum history node containing largest initializer. */
89 static enumerator_history *max_enum = NULL;
91 /* gfc_new_block points to the symbol of a newly matched block. */
93 gfc_symbol *gfc_new_block;
95 bool gfc_matching_function;
98 /********************* DATA statement subroutines *********************/
100 static bool in_match_data = false;
102 bool
103 gfc_in_match_data (void)
105 return in_match_data;
108 static void
109 set_in_match_data (bool set_value)
111 in_match_data = set_value;
114 /* Free a gfc_data_variable structure and everything beneath it. */
116 static void
117 free_variable (gfc_data_variable *p)
119 gfc_data_variable *q;
121 for (; p; p = q)
123 q = p->next;
124 gfc_free_expr (p->expr);
125 gfc_free_iterator (&p->iter, 0);
126 free_variable (p->list);
127 free (p);
132 /* Free a gfc_data_value structure and everything beneath it. */
134 static void
135 free_value (gfc_data_value *p)
137 gfc_data_value *q;
139 for (; p; p = q)
141 q = p->next;
142 mpz_clear (p->repeat);
143 gfc_free_expr (p->expr);
144 free (p);
149 /* Free a list of gfc_data structures. */
151 void
152 gfc_free_data (gfc_data *p)
154 gfc_data *q;
156 for (; p; p = q)
158 q = p->next;
159 free_variable (p->var);
160 free_value (p->value);
161 free (p);
166 /* Free all data in a namespace. */
168 static void
169 gfc_free_data_all (gfc_namespace *ns)
171 gfc_data *d;
173 for (;ns->data;)
175 d = ns->data->next;
176 free (ns->data);
177 ns->data = d;
182 static match var_element (gfc_data_variable *);
184 /* Match a list of variables terminated by an iterator and a right
185 parenthesis. */
187 static match
188 var_list (gfc_data_variable *parent)
190 gfc_data_variable *tail, var;
191 match m;
193 m = var_element (&var);
194 if (m == MATCH_ERROR)
195 return MATCH_ERROR;
196 if (m == MATCH_NO)
197 goto syntax;
199 tail = gfc_get_data_variable ();
200 *tail = var;
202 parent->list = tail;
204 for (;;)
206 if (gfc_match_char (',') != MATCH_YES)
207 goto syntax;
209 m = gfc_match_iterator (&parent->iter, 1);
210 if (m == MATCH_YES)
211 break;
212 if (m == MATCH_ERROR)
213 return MATCH_ERROR;
215 m = var_element (&var);
216 if (m == MATCH_ERROR)
217 return MATCH_ERROR;
218 if (m == MATCH_NO)
219 goto syntax;
221 tail->next = gfc_get_data_variable ();
222 tail = tail->next;
224 *tail = var;
227 if (gfc_match_char (')') != MATCH_YES)
228 goto syntax;
229 return MATCH_YES;
231 syntax:
232 gfc_syntax_error (ST_DATA);
233 return MATCH_ERROR;
237 /* Match a single element in a data variable list, which can be a
238 variable-iterator list. */
240 static match
241 var_element (gfc_data_variable *new_var)
243 match m;
244 gfc_symbol *sym;
246 memset (new_var, 0, sizeof (gfc_data_variable));
248 if (gfc_match_char ('(') == MATCH_YES)
249 return var_list (new_var);
251 m = gfc_match_variable (&new_var->expr, 0);
252 if (m != MATCH_YES)
253 return m;
255 sym = new_var->expr->symtree->n.sym;
257 /* Symbol should already have an associated type. */
258 if (!gfc_check_symbol_typed (sym, gfc_current_ns, false, gfc_current_locus))
259 return MATCH_ERROR;
261 if (!sym->attr.function && gfc_current_ns->parent
262 && gfc_current_ns->parent == sym->ns)
264 gfc_error ("Host associated variable '%s' may not be in the DATA "
265 "statement at %C", sym->name);
266 return MATCH_ERROR;
269 if (gfc_current_state () != COMP_BLOCK_DATA
270 && sym->attr.in_common
271 && !gfc_notify_std (GFC_STD_GNU, "initialization of "
272 "common block variable '%s' in DATA statement at %C",
273 sym->name))
274 return MATCH_ERROR;
276 if (!gfc_add_data (&sym->attr, sym->name, &new_var->expr->where))
277 return MATCH_ERROR;
279 return MATCH_YES;
283 /* Match the top-level list of data variables. */
285 static match
286 top_var_list (gfc_data *d)
288 gfc_data_variable var, *tail, *new_var;
289 match m;
291 tail = NULL;
293 for (;;)
295 m = var_element (&var);
296 if (m == MATCH_NO)
297 goto syntax;
298 if (m == MATCH_ERROR)
299 return MATCH_ERROR;
301 new_var = gfc_get_data_variable ();
302 *new_var = var;
304 if (tail == NULL)
305 d->var = new_var;
306 else
307 tail->next = new_var;
309 tail = new_var;
311 if (gfc_match_char ('/') == MATCH_YES)
312 break;
313 if (gfc_match_char (',') != MATCH_YES)
314 goto syntax;
317 return MATCH_YES;
319 syntax:
320 gfc_syntax_error (ST_DATA);
321 gfc_free_data_all (gfc_current_ns);
322 return MATCH_ERROR;
326 static match
327 match_data_constant (gfc_expr **result)
329 char name[GFC_MAX_SYMBOL_LEN + 1];
330 gfc_symbol *sym, *dt_sym = NULL;
331 gfc_expr *expr;
332 match m;
333 locus old_loc;
335 m = gfc_match_literal_constant (&expr, 1);
336 if (m == MATCH_YES)
338 *result = expr;
339 return MATCH_YES;
342 if (m == MATCH_ERROR)
343 return MATCH_ERROR;
345 m = gfc_match_null (result);
346 if (m != MATCH_NO)
347 return m;
349 old_loc = gfc_current_locus;
351 /* Should this be a structure component, try to match it
352 before matching a name. */
353 m = gfc_match_rvalue (result);
354 if (m == MATCH_ERROR)
355 return m;
357 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
359 if (!gfc_simplify_expr (*result, 0))
360 m = MATCH_ERROR;
361 return m;
363 else if (m == MATCH_YES)
364 gfc_free_expr (*result);
366 gfc_current_locus = old_loc;
368 m = gfc_match_name (name);
369 if (m != MATCH_YES)
370 return m;
372 if (gfc_find_symbol (name, NULL, 1, &sym))
373 return MATCH_ERROR;
375 if (sym && sym->attr.generic)
376 dt_sym = gfc_find_dt_in_generic (sym);
378 if (sym == NULL
379 || (sym->attr.flavor != FL_PARAMETER
380 && (!dt_sym || dt_sym->attr.flavor != FL_DERIVED)))
382 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
383 name);
384 return MATCH_ERROR;
386 else if (dt_sym && dt_sym->attr.flavor == FL_DERIVED)
387 return gfc_match_structure_constructor (dt_sym, result);
389 /* Check to see if the value is an initialization array expression. */
390 if (sym->value->expr_type == EXPR_ARRAY)
392 gfc_current_locus = old_loc;
394 m = gfc_match_init_expr (result);
395 if (m == MATCH_ERROR)
396 return m;
398 if (m == MATCH_YES)
400 if (!gfc_simplify_expr (*result, 0))
401 m = MATCH_ERROR;
403 if ((*result)->expr_type == EXPR_CONSTANT)
404 return m;
405 else
407 gfc_error ("Invalid initializer %s in Data statement at %C", name);
408 return MATCH_ERROR;
413 *result = gfc_copy_expr (sym->value);
414 return MATCH_YES;
418 /* Match a list of values in a DATA statement. The leading '/' has
419 already been seen at this point. */
421 static match
422 top_val_list (gfc_data *data)
424 gfc_data_value *new_val, *tail;
425 gfc_expr *expr;
426 match m;
428 tail = NULL;
430 for (;;)
432 m = match_data_constant (&expr);
433 if (m == MATCH_NO)
434 goto syntax;
435 if (m == MATCH_ERROR)
436 return MATCH_ERROR;
438 new_val = gfc_get_data_value ();
439 mpz_init (new_val->repeat);
441 if (tail == NULL)
442 data->value = new_val;
443 else
444 tail->next = new_val;
446 tail = new_val;
448 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
450 tail->expr = expr;
451 mpz_set_ui (tail->repeat, 1);
453 else
455 mpz_set (tail->repeat, expr->value.integer);
456 gfc_free_expr (expr);
458 m = match_data_constant (&tail->expr);
459 if (m == MATCH_NO)
460 goto syntax;
461 if (m == MATCH_ERROR)
462 return MATCH_ERROR;
465 if (gfc_match_char ('/') == MATCH_YES)
466 break;
467 if (gfc_match_char (',') == MATCH_NO)
468 goto syntax;
471 return MATCH_YES;
473 syntax:
474 gfc_syntax_error (ST_DATA);
475 gfc_free_data_all (gfc_current_ns);
476 return MATCH_ERROR;
480 /* Matches an old style initialization. */
482 static match
483 match_old_style_init (const char *name)
485 match m;
486 gfc_symtree *st;
487 gfc_symbol *sym;
488 gfc_data *newdata;
490 /* Set up data structure to hold initializers. */
491 gfc_find_sym_tree (name, NULL, 0, &st);
492 sym = st->n.sym;
494 newdata = gfc_get_data ();
495 newdata->var = gfc_get_data_variable ();
496 newdata->var->expr = gfc_get_variable_expr (st);
497 newdata->where = gfc_current_locus;
499 /* Match initial value list. This also eats the terminal '/'. */
500 m = top_val_list (newdata);
501 if (m != MATCH_YES)
503 free (newdata);
504 return m;
507 if (gfc_pure (NULL))
509 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
510 free (newdata);
511 return MATCH_ERROR;
514 if (gfc_implicit_pure (NULL))
515 gfc_current_ns->proc_name->attr.implicit_pure = 0;
517 /* Mark the variable as having appeared in a data statement. */
518 if (!gfc_add_data (&sym->attr, sym->name, &sym->declared_at))
520 free (newdata);
521 return MATCH_ERROR;
524 /* Chain in namespace list of DATA initializers. */
525 newdata->next = gfc_current_ns->data;
526 gfc_current_ns->data = newdata;
528 return m;
532 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
533 we are matching a DATA statement and are therefore issuing an error
534 if we encounter something unexpected, if not, we're trying to match
535 an old-style initialization expression of the form INTEGER I /2/. */
537 match
538 gfc_match_data (void)
540 gfc_data *new_data;
541 match m;
543 set_in_match_data (true);
545 for (;;)
547 new_data = gfc_get_data ();
548 new_data->where = gfc_current_locus;
550 m = top_var_list (new_data);
551 if (m != MATCH_YES)
552 goto cleanup;
554 m = top_val_list (new_data);
555 if (m != MATCH_YES)
556 goto cleanup;
558 new_data->next = gfc_current_ns->data;
559 gfc_current_ns->data = new_data;
561 if (gfc_match_eos () == MATCH_YES)
562 break;
564 gfc_match_char (','); /* Optional comma */
567 set_in_match_data (false);
569 if (gfc_pure (NULL))
571 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
572 return MATCH_ERROR;
575 if (gfc_implicit_pure (NULL))
576 gfc_current_ns->proc_name->attr.implicit_pure = 0;
578 return MATCH_YES;
580 cleanup:
581 set_in_match_data (false);
582 gfc_free_data (new_data);
583 return MATCH_ERROR;
587 /************************ Declaration statements *********************/
590 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
592 static bool
593 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
595 int i;
597 if ((from->type == AS_ASSUMED_RANK && to->corank)
598 || (to->type == AS_ASSUMED_RANK && from->corank))
600 gfc_error ("The assumed-rank array at %C shall not have a codimension");
601 return false;
604 if (to->rank == 0 && from->rank > 0)
606 to->rank = from->rank;
607 to->type = from->type;
608 to->cray_pointee = from->cray_pointee;
609 to->cp_was_assumed = from->cp_was_assumed;
611 for (i = 0; i < to->corank; i++)
613 to->lower[from->rank + i] = to->lower[i];
614 to->upper[from->rank + i] = to->upper[i];
616 for (i = 0; i < from->rank; i++)
618 if (copy)
620 to->lower[i] = gfc_copy_expr (from->lower[i]);
621 to->upper[i] = gfc_copy_expr (from->upper[i]);
623 else
625 to->lower[i] = from->lower[i];
626 to->upper[i] = from->upper[i];
630 else if (to->corank == 0 && from->corank > 0)
632 to->corank = from->corank;
633 to->cotype = from->cotype;
635 for (i = 0; i < from->corank; i++)
637 if (copy)
639 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
640 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
642 else
644 to->lower[to->rank + i] = from->lower[i];
645 to->upper[to->rank + i] = from->upper[i];
650 return true;
654 /* Match an intent specification. Since this can only happen after an
655 INTENT word, a legal intent-spec must follow. */
657 static sym_intent
658 match_intent_spec (void)
661 if (gfc_match (" ( in out )") == MATCH_YES)
662 return INTENT_INOUT;
663 if (gfc_match (" ( in )") == MATCH_YES)
664 return INTENT_IN;
665 if (gfc_match (" ( out )") == MATCH_YES)
666 return INTENT_OUT;
668 gfc_error ("Bad INTENT specification at %C");
669 return INTENT_UNKNOWN;
673 /* Matches a character length specification, which is either a
674 specification expression, '*', or ':'. */
676 static match
677 char_len_param_value (gfc_expr **expr, bool *deferred)
679 match m;
681 *expr = NULL;
682 *deferred = false;
684 if (gfc_match_char ('*') == MATCH_YES)
685 return MATCH_YES;
687 if (gfc_match_char (':') == MATCH_YES)
689 if (!gfc_notify_std (GFC_STD_F2003, "deferred type "
690 "parameter at %C"))
691 return MATCH_ERROR;
693 *deferred = true;
695 return MATCH_YES;
698 m = gfc_match_expr (expr);
700 if (m == MATCH_YES
701 && !gfc_expr_check_typed (*expr, gfc_current_ns, false))
702 return MATCH_ERROR;
704 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
706 if ((*expr)->value.function.actual
707 && (*expr)->value.function.actual->expr->symtree)
709 gfc_expr *e;
710 e = (*expr)->value.function.actual->expr;
711 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
712 && e->expr_type == EXPR_VARIABLE)
714 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
715 goto syntax;
716 if (e->symtree->n.sym->ts.type == BT_CHARACTER
717 && e->symtree->n.sym->ts.u.cl
718 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
719 goto syntax;
723 return m;
725 syntax:
726 gfc_error ("Conflict in attributes of function argument at %C");
727 return MATCH_ERROR;
731 /* A character length is a '*' followed by a literal integer or a
732 char_len_param_value in parenthesis. */
734 static match
735 match_char_length (gfc_expr **expr, bool *deferred, bool obsolescent_check)
737 int length;
738 match m;
740 *deferred = false;
741 m = gfc_match_char ('*');
742 if (m != MATCH_YES)
743 return m;
745 m = gfc_match_small_literal_int (&length, NULL);
746 if (m == MATCH_ERROR)
747 return m;
749 if (m == MATCH_YES)
751 if (obsolescent_check
752 && !gfc_notify_std (GFC_STD_F95_OBS, "Old-style character length at %C"))
753 return MATCH_ERROR;
754 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
755 return m;
758 if (gfc_match_char ('(') == MATCH_NO)
759 goto syntax;
761 m = char_len_param_value (expr, deferred);
762 if (m != MATCH_YES && gfc_matching_function)
764 gfc_undo_symbols ();
765 m = MATCH_YES;
768 if (m == MATCH_ERROR)
769 return m;
770 if (m == MATCH_NO)
771 goto syntax;
773 if (gfc_match_char (')') == MATCH_NO)
775 gfc_free_expr (*expr);
776 *expr = NULL;
777 goto syntax;
780 return MATCH_YES;
782 syntax:
783 gfc_error ("Syntax error in character length specification at %C");
784 return MATCH_ERROR;
788 /* Special subroutine for finding a symbol. Check if the name is found
789 in the current name space. If not, and we're compiling a function or
790 subroutine and the parent compilation unit is an interface, then check
791 to see if the name we've been given is the name of the interface
792 (located in another namespace). */
794 static int
795 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
797 gfc_state_data *s;
798 gfc_symtree *st;
799 int i;
801 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
802 if (i == 0)
804 *result = st ? st->n.sym : NULL;
805 goto end;
808 if (gfc_current_state () != COMP_SUBROUTINE
809 && gfc_current_state () != COMP_FUNCTION)
810 goto end;
812 s = gfc_state_stack->previous;
813 if (s == NULL)
814 goto end;
816 if (s->state != COMP_INTERFACE)
817 goto end;
818 if (s->sym == NULL)
819 goto end; /* Nameless interface. */
821 if (strcmp (name, s->sym->name) == 0)
823 *result = s->sym;
824 return 0;
827 end:
828 return i;
832 /* Special subroutine for getting a symbol node associated with a
833 procedure name, used in SUBROUTINE and FUNCTION statements. The
834 symbol is created in the parent using with symtree node in the
835 child unit pointing to the symbol. If the current namespace has no
836 parent, then the symbol is just created in the current unit. */
838 static int
839 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
841 gfc_symtree *st;
842 gfc_symbol *sym;
843 int rc = 0;
845 /* Module functions have to be left in their own namespace because
846 they have potentially (almost certainly!) already been referenced.
847 In this sense, they are rather like external functions. This is
848 fixed up in resolve.c(resolve_entries), where the symbol name-
849 space is set to point to the master function, so that the fake
850 result mechanism can work. */
851 if (module_fcn_entry)
853 /* Present if entry is declared to be a module procedure. */
854 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
856 if (*result == NULL)
857 rc = gfc_get_symbol (name, NULL, result);
858 else if (!gfc_get_symbol (name, NULL, &sym) && sym
859 && (*result)->ts.type == BT_UNKNOWN
860 && sym->attr.flavor == FL_UNKNOWN)
861 /* Pick up the typespec for the entry, if declared in the function
862 body. Note that this symbol is FL_UNKNOWN because it will
863 only have appeared in a type declaration. The local symtree
864 is set to point to the module symbol and a unique symtree
865 to the local version. This latter ensures a correct clearing
866 of the symbols. */
868 /* If the ENTRY proceeds its specification, we need to ensure
869 that this does not raise a "has no IMPLICIT type" error. */
870 if (sym->ts.type == BT_UNKNOWN)
871 sym->attr.untyped = 1;
873 (*result)->ts = sym->ts;
875 /* Put the symbol in the procedure namespace so that, should
876 the ENTRY precede its specification, the specification
877 can be applied. */
878 (*result)->ns = gfc_current_ns;
880 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
881 st->n.sym = *result;
882 st = gfc_get_unique_symtree (gfc_current_ns);
883 st->n.sym = sym;
886 else
887 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
889 if (rc)
890 return rc;
892 sym = *result;
894 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
896 /* Trap another encompassed procedure with the same name. All
897 these conditions are necessary to avoid picking up an entry
898 whose name clashes with that of the encompassing procedure;
899 this is handled using gsymbols to register unique,globally
900 accessible names. */
901 if (sym->attr.flavor != 0
902 && sym->attr.proc != 0
903 && (sym->attr.subroutine || sym->attr.function)
904 && sym->attr.if_source != IFSRC_UNKNOWN)
905 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
906 name, &sym->declared_at);
908 /* Trap a procedure with a name the same as interface in the
909 encompassing scope. */
910 if (sym->attr.generic != 0
911 && (sym->attr.subroutine || sym->attr.function)
912 && !sym->attr.mod_proc)
913 gfc_error_now ("Name '%s' at %C is already defined"
914 " as a generic interface at %L",
915 name, &sym->declared_at);
917 /* Trap declarations of attributes in encompassing scope. The
918 signature for this is that ts.kind is set. Legitimate
919 references only set ts.type. */
920 if (sym->ts.kind != 0
921 && !sym->attr.implicit_type
922 && sym->attr.proc == 0
923 && gfc_current_ns->parent != NULL
924 && sym->attr.access == 0
925 && !module_fcn_entry)
926 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
927 "and must not have attributes declared at %L",
928 name, &sym->declared_at);
931 if (gfc_current_ns->parent == NULL || *result == NULL)
932 return rc;
934 /* Module function entries will already have a symtree in
935 the current namespace but will need one at module level. */
936 if (module_fcn_entry)
938 /* Present if entry is declared to be a module procedure. */
939 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
940 if (st == NULL)
941 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
943 else
944 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
946 st->n.sym = sym;
947 sym->refs++;
949 /* See if the procedure should be a module procedure. */
951 if (((sym->ns->proc_name != NULL
952 && sym->ns->proc_name->attr.flavor == FL_MODULE
953 && sym->attr.proc != PROC_MODULE)
954 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
955 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
956 rc = 2;
958 return rc;
962 /* Verify that the given symbol representing a parameter is C
963 interoperable, by checking to see if it was marked as such after
964 its declaration. If the given symbol is not interoperable, a
965 warning is reported, thus removing the need to return the status to
966 the calling function. The standard does not require the user use
967 one of the iso_c_binding named constants to declare an
968 interoperable parameter, but we can't be sure if the param is C
969 interop or not if the user doesn't. For example, integer(4) may be
970 legal Fortran, but doesn't have meaning in C. It may interop with
971 a number of the C types, which causes a problem because the
972 compiler can't know which one. This code is almost certainly not
973 portable, and the user will get what they deserve if the C type
974 across platforms isn't always interoperable with integer(4). If
975 the user had used something like integer(c_int) or integer(c_long),
976 the compiler could have automatically handled the varying sizes
977 across platforms. */
979 bool
980 gfc_verify_c_interop_param (gfc_symbol *sym)
982 int is_c_interop = 0;
983 bool retval = true;
985 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
986 Don't repeat the checks here. */
987 if (sym->attr.implicit_type)
988 return true;
990 /* For subroutines or functions that are passed to a BIND(C) procedure,
991 they're interoperable if they're BIND(C) and their params are all
992 interoperable. */
993 if (sym->attr.flavor == FL_PROCEDURE)
995 if (sym->attr.is_bind_c == 0)
997 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
998 "attribute to be C interoperable", sym->name,
999 &(sym->declared_at));
1001 return false;
1003 else
1005 if (sym->attr.is_c_interop == 1)
1006 /* We've already checked this procedure; don't check it again. */
1007 return true;
1008 else
1009 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1010 sym->common_block);
1014 /* See if we've stored a reference to a procedure that owns sym. */
1015 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1017 if (sym->ns->proc_name->attr.is_bind_c == 1)
1019 is_c_interop = (gfc_verify_c_interop(&(sym->ts)) ? 1 : 0);
1021 if (is_c_interop != 1)
1023 /* Make personalized messages to give better feedback. */
1024 if (sym->ts.type == BT_DERIVED)
1025 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1026 "BIND(C) procedure '%s' but is not C interoperable "
1027 "because derived type '%s' is not C interoperable",
1028 sym->name, &(sym->declared_at),
1029 sym->ns->proc_name->name,
1030 sym->ts.u.derived->name);
1031 else if (sym->ts.type == BT_CLASS)
1032 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1033 "BIND(C) procedure '%s' but is not C interoperable "
1034 "because it is polymorphic",
1035 sym->name, &(sym->declared_at),
1036 sym->ns->proc_name->name);
1037 else if (gfc_option.warn_c_binding_type)
1038 gfc_warning ("Variable '%s' at %L is a dummy argument of the "
1039 "BIND(C) procedure '%s' but may not be C "
1040 "interoperable",
1041 sym->name, &(sym->declared_at),
1042 sym->ns->proc_name->name);
1045 /* Character strings are only C interoperable if they have a
1046 length of 1. */
1047 if (sym->ts.type == BT_CHARACTER)
1049 gfc_charlen *cl = sym->ts.u.cl;
1050 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1051 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1053 gfc_error ("Character argument '%s' at %L "
1054 "must be length 1 because "
1055 "procedure '%s' is BIND(C)",
1056 sym->name, &sym->declared_at,
1057 sym->ns->proc_name->name);
1058 retval = false;
1062 /* We have to make sure that any param to a bind(c) routine does
1063 not have the allocatable, pointer, or optional attributes,
1064 according to J3/04-007, section 5.1. */
1065 if (sym->attr.allocatable == 1
1066 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable '%s' at %L with "
1067 "ALLOCATABLE attribute in procedure '%s' "
1068 "with BIND(C)", sym->name,
1069 &(sym->declared_at),
1070 sym->ns->proc_name->name))
1071 retval = false;
1073 if (sym->attr.pointer == 1
1074 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable '%s' at %L with "
1075 "POINTER attribute in procedure '%s' "
1076 "with BIND(C)", sym->name,
1077 &(sym->declared_at),
1078 sym->ns->proc_name->name))
1079 retval = false;
1081 if ((sym->attr.allocatable || sym->attr.pointer) && !sym->as)
1083 gfc_error ("Scalar variable '%s' at %L with POINTER or "
1084 "ALLOCATABLE in procedure '%s' with BIND(C) is not yet"
1085 " supported", sym->name, &(sym->declared_at),
1086 sym->ns->proc_name->name);
1087 retval = false;
1090 if (sym->attr.optional == 1 && sym->attr.value)
1092 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1093 "and the VALUE attribute because procedure '%s' "
1094 "is BIND(C)", sym->name, &(sym->declared_at),
1095 sym->ns->proc_name->name);
1096 retval = false;
1098 else if (sym->attr.optional == 1
1099 && !gfc_notify_std (GFC_STD_F2008_TS, "Variable '%s' "
1100 "at %L with OPTIONAL attribute in "
1101 "procedure '%s' which is BIND(C)",
1102 sym->name, &(sym->declared_at),
1103 sym->ns->proc_name->name))
1104 retval = false;
1106 /* Make sure that if it has the dimension attribute, that it is
1107 either assumed size or explicit shape. Deferred shape is already
1108 covered by the pointer/allocatable attribute. */
1109 if (sym->as != NULL && sym->as->type == AS_ASSUMED_SHAPE
1110 && !gfc_notify_std (GFC_STD_F2008_TS, "Assumed-shape array '%s' "
1111 "at %L as dummy argument to the BIND(C) "
1112 "procedure '%s' at %L", sym->name,
1113 &(sym->declared_at),
1114 sym->ns->proc_name->name,
1115 &(sym->ns->proc_name->declared_at)))
1116 retval = false;
1120 return retval;
1125 /* Function called by variable_decl() that adds a name to the symbol table. */
1127 static bool
1128 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1129 gfc_array_spec **as, locus *var_locus)
1131 symbol_attribute attr;
1132 gfc_symbol *sym;
1134 if (gfc_get_symbol (name, NULL, &sym))
1135 return false;
1137 /* Start updating the symbol table. Add basic type attribute if present. */
1138 if (current_ts.type != BT_UNKNOWN
1139 && (sym->attr.implicit_type == 0
1140 || !gfc_compare_types (&sym->ts, &current_ts))
1141 && !gfc_add_type (sym, &current_ts, var_locus))
1142 return false;
1144 if (sym->ts.type == BT_CHARACTER)
1146 sym->ts.u.cl = cl;
1147 sym->ts.deferred = cl_deferred;
1150 /* Add dimension attribute if present. */
1151 if (!gfc_set_array_spec (sym, *as, var_locus))
1152 return false;
1153 *as = NULL;
1155 /* Add attribute to symbol. The copy is so that we can reset the
1156 dimension attribute. */
1157 attr = current_attr;
1158 attr.dimension = 0;
1159 attr.codimension = 0;
1161 if (!gfc_copy_attr (&sym->attr, &attr, var_locus))
1162 return false;
1164 /* Finish any work that may need to be done for the binding label,
1165 if it's a bind(c). The bind(c) attr is found before the symbol
1166 is made, and before the symbol name (for data decls), so the
1167 current_ts is holding the binding label, or nothing if the
1168 name= attr wasn't given. Therefore, test here if we're dealing
1169 with a bind(c) and make sure the binding label is set correctly. */
1170 if (sym->attr.is_bind_c == 1)
1172 if (!sym->binding_label)
1174 /* Set the binding label and verify that if a NAME= was specified
1175 then only one identifier was in the entity-decl-list. */
1176 if (!set_binding_label (&sym->binding_label, sym->name,
1177 num_idents_on_line))
1178 return false;
1182 /* See if we know we're in a common block, and if it's a bind(c)
1183 common then we need to make sure we're an interoperable type. */
1184 if (sym->attr.in_common == 1)
1186 /* Test the common block object. */
1187 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1188 && sym->ts.is_c_interop != 1)
1190 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1191 "must be declared with a C interoperable "
1192 "kind since common block '%s' is BIND(C)",
1193 sym->name, sym->common_block->name,
1194 sym->common_block->name);
1195 gfc_clear_error ();
1199 sym->attr.implied_index = 0;
1201 if (sym->ts.type == BT_CLASS)
1202 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as);
1204 return true;
1208 /* Set character constant to the given length. The constant will be padded or
1209 truncated. If we're inside an array constructor without a typespec, we
1210 additionally check that all elements have the same length; check_len -1
1211 means no checking. */
1213 void
1214 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1216 gfc_char_t *s;
1217 int slen;
1219 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1220 gcc_assert (expr->ts.type == BT_CHARACTER);
1222 slen = expr->value.character.length;
1223 if (len != slen)
1225 s = gfc_get_wide_string (len + 1);
1226 memcpy (s, expr->value.character.string,
1227 MIN (len, slen) * sizeof (gfc_char_t));
1228 if (len > slen)
1229 gfc_wide_memset (&s[slen], ' ', len - slen);
1231 if (gfc_option.warn_character_truncation && slen > len)
1232 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1233 "(%d/%d)", &expr->where, slen, len);
1235 /* Apply the standard by 'hand' otherwise it gets cleared for
1236 initializers. */
1237 if (check_len != -1 && slen != check_len
1238 && !(gfc_option.allow_std & GFC_STD_GNU))
1239 gfc_error_now ("The CHARACTER elements of the array constructor "
1240 "at %L must have the same length (%d/%d)",
1241 &expr->where, slen, check_len);
1243 s[len] = '\0';
1244 free (expr->value.character.string);
1245 expr->value.character.string = s;
1246 expr->value.character.length = len;
1251 /* Function to create and update the enumerator history
1252 using the information passed as arguments.
1253 Pointer "max_enum" is also updated, to point to
1254 enum history node containing largest initializer.
1256 SYM points to the symbol node of enumerator.
1257 INIT points to its enumerator value. */
1259 static void
1260 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1262 enumerator_history *new_enum_history;
1263 gcc_assert (sym != NULL && init != NULL);
1265 new_enum_history = XCNEW (enumerator_history);
1267 new_enum_history->sym = sym;
1268 new_enum_history->initializer = init;
1269 new_enum_history->next = NULL;
1271 if (enum_history == NULL)
1273 enum_history = new_enum_history;
1274 max_enum = enum_history;
1276 else
1278 new_enum_history->next = enum_history;
1279 enum_history = new_enum_history;
1281 if (mpz_cmp (max_enum->initializer->value.integer,
1282 new_enum_history->initializer->value.integer) < 0)
1283 max_enum = new_enum_history;
1288 /* Function to free enum kind history. */
1290 void
1291 gfc_free_enum_history (void)
1293 enumerator_history *current = enum_history;
1294 enumerator_history *next;
1296 while (current != NULL)
1298 next = current->next;
1299 free (current);
1300 current = next;
1302 max_enum = NULL;
1303 enum_history = NULL;
1307 /* Function called by variable_decl() that adds an initialization
1308 expression to a symbol. */
1310 static bool
1311 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1313 symbol_attribute attr;
1314 gfc_symbol *sym;
1315 gfc_expr *init;
1317 init = *initp;
1318 if (find_special (name, &sym, false))
1319 return false;
1321 attr = sym->attr;
1323 /* If this symbol is confirming an implicit parameter type,
1324 then an initialization expression is not allowed. */
1325 if (attr.flavor == FL_PARAMETER
1326 && sym->value != NULL
1327 && *initp != NULL)
1329 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1330 sym->name);
1331 return false;
1334 if (init == NULL)
1336 /* An initializer is required for PARAMETER declarations. */
1337 if (attr.flavor == FL_PARAMETER)
1339 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1340 return false;
1343 else
1345 /* If a variable appears in a DATA block, it cannot have an
1346 initializer. */
1347 if (sym->attr.data)
1349 gfc_error ("Variable '%s' at %C with an initializer already "
1350 "appears in a DATA statement", sym->name);
1351 return false;
1354 /* Check if the assignment can happen. This has to be put off
1355 until later for derived type variables and procedure pointers. */
1356 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1357 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1358 && !sym->attr.proc_pointer
1359 && !gfc_check_assign_symbol (sym, NULL, init))
1360 return false;
1362 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1363 && init->ts.type == BT_CHARACTER)
1365 /* Update symbol character length according initializer. */
1366 if (!gfc_check_assign_symbol (sym, NULL, init))
1367 return false;
1369 if (sym->ts.u.cl->length == NULL)
1371 int clen;
1372 /* If there are multiple CHARACTER variables declared on the
1373 same line, we don't want them to share the same length. */
1374 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1376 if (sym->attr.flavor == FL_PARAMETER)
1378 if (init->expr_type == EXPR_CONSTANT)
1380 clen = init->value.character.length;
1381 sym->ts.u.cl->length
1382 = gfc_get_int_expr (gfc_default_integer_kind,
1383 NULL, clen);
1385 else if (init->expr_type == EXPR_ARRAY)
1387 gfc_constructor *c;
1388 c = gfc_constructor_first (init->value.constructor);
1389 clen = c->expr->value.character.length;
1390 sym->ts.u.cl->length
1391 = gfc_get_int_expr (gfc_default_integer_kind,
1392 NULL, clen);
1394 else if (init->ts.u.cl && init->ts.u.cl->length)
1395 sym->ts.u.cl->length =
1396 gfc_copy_expr (sym->value->ts.u.cl->length);
1399 /* Update initializer character length according symbol. */
1400 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1402 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1404 if (init->expr_type == EXPR_CONSTANT)
1405 gfc_set_constant_character_len (len, init, -1);
1406 else if (init->expr_type == EXPR_ARRAY)
1408 gfc_constructor *c;
1410 /* Build a new charlen to prevent simplification from
1411 deleting the length before it is resolved. */
1412 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1413 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1415 for (c = gfc_constructor_first (init->value.constructor);
1416 c; c = gfc_constructor_next (c))
1417 gfc_set_constant_character_len (len, c->expr, -1);
1422 /* If sym is implied-shape, set its upper bounds from init. */
1423 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1424 && sym->as->type == AS_IMPLIED_SHAPE)
1426 int dim;
1428 if (init->rank == 0)
1430 gfc_error ("Can't initialize implied-shape array at %L"
1431 " with scalar", &sym->declared_at);
1432 return false;
1434 gcc_assert (sym->as->rank == init->rank);
1436 /* Shape should be present, we get an initialization expression. */
1437 gcc_assert (init->shape);
1439 for (dim = 0; dim < sym->as->rank; ++dim)
1441 int k;
1442 gfc_expr* lower;
1443 gfc_expr* e;
1445 lower = sym->as->lower[dim];
1446 if (lower->expr_type != EXPR_CONSTANT)
1448 gfc_error ("Non-constant lower bound in implied-shape"
1449 " declaration at %L", &lower->where);
1450 return false;
1453 /* All dimensions must be without upper bound. */
1454 gcc_assert (!sym->as->upper[dim]);
1456 k = lower->ts.kind;
1457 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1458 mpz_add (e->value.integer,
1459 lower->value.integer, init->shape[dim]);
1460 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1461 sym->as->upper[dim] = e;
1464 sym->as->type = AS_EXPLICIT;
1467 /* Need to check if the expression we initialized this
1468 to was one of the iso_c_binding named constants. If so,
1469 and we're a parameter (constant), let it be iso_c.
1470 For example:
1471 integer(c_int), parameter :: my_int = c_int
1472 integer(my_int) :: my_int_2
1473 If we mark my_int as iso_c (since we can see it's value
1474 is equal to one of the named constants), then my_int_2
1475 will be considered C interoperable. */
1476 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1478 sym->ts.is_iso_c |= init->ts.is_iso_c;
1479 sym->ts.is_c_interop |= init->ts.is_c_interop;
1480 /* attr bits needed for module files. */
1481 sym->attr.is_iso_c |= init->ts.is_iso_c;
1482 sym->attr.is_c_interop |= init->ts.is_c_interop;
1483 if (init->ts.is_iso_c)
1484 sym->ts.f90_type = init->ts.f90_type;
1487 /* Add initializer. Make sure we keep the ranks sane. */
1488 if (sym->attr.dimension && init->rank == 0)
1490 mpz_t size;
1491 gfc_expr *array;
1492 int n;
1493 if (sym->attr.flavor == FL_PARAMETER
1494 && init->expr_type == EXPR_CONSTANT
1495 && spec_size (sym->as, &size)
1496 && mpz_cmp_si (size, 0) > 0)
1498 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1499 &init->where);
1500 for (n = 0; n < (int)mpz_get_si (size); n++)
1501 gfc_constructor_append_expr (&array->value.constructor,
1502 n == 0
1503 ? init
1504 : gfc_copy_expr (init),
1505 &init->where);
1507 array->shape = gfc_get_shape (sym->as->rank);
1508 for (n = 0; n < sym->as->rank; n++)
1509 spec_dimen_size (sym->as, n, &array->shape[n]);
1511 init = array;
1512 mpz_clear (size);
1514 init->rank = sym->as->rank;
1517 sym->value = init;
1518 if (sym->attr.save == SAVE_NONE)
1519 sym->attr.save = SAVE_IMPLICIT;
1520 *initp = NULL;
1523 return true;
1527 /* Function called by variable_decl() that adds a name to a structure
1528 being built. */
1530 static bool
1531 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1532 gfc_array_spec **as)
1534 gfc_component *c;
1535 bool t = true;
1537 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1538 constructing, it must have the pointer attribute. */
1539 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1540 && current_ts.u.derived == gfc_current_block ()
1541 && current_attr.pointer == 0)
1543 gfc_error ("Component at %C must have the POINTER attribute");
1544 return false;
1547 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1549 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1551 gfc_error ("Array component of structure at %C must have explicit "
1552 "or deferred shape");
1553 return false;
1557 if (!gfc_add_component (gfc_current_block(), name, &c))
1558 return false;
1560 c->ts = current_ts;
1561 if (c->ts.type == BT_CHARACTER)
1562 c->ts.u.cl = cl;
1563 c->attr = current_attr;
1565 c->initializer = *init;
1566 *init = NULL;
1568 c->as = *as;
1569 if (c->as != NULL)
1571 if (c->as->corank)
1572 c->attr.codimension = 1;
1573 if (c->as->rank)
1574 c->attr.dimension = 1;
1576 *as = NULL;
1578 /* Should this ever get more complicated, combine with similar section
1579 in add_init_expr_to_sym into a separate function. */
1580 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1581 && c->ts.u.cl
1582 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1584 int len;
1586 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1587 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1588 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1590 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1592 if (c->initializer->expr_type == EXPR_CONSTANT)
1593 gfc_set_constant_character_len (len, c->initializer, -1);
1594 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1595 c->initializer->ts.u.cl->length->value.integer))
1597 gfc_constructor *ctor;
1598 ctor = gfc_constructor_first (c->initializer->value.constructor);
1600 if (ctor)
1602 int first_len;
1603 bool has_ts = (c->initializer->ts.u.cl
1604 && c->initializer->ts.u.cl->length_from_typespec);
1606 /* Remember the length of the first element for checking
1607 that all elements *in the constructor* have the same
1608 length. This need not be the length of the LHS! */
1609 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1610 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1611 first_len = ctor->expr->value.character.length;
1613 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1614 if (ctor->expr->expr_type == EXPR_CONSTANT)
1616 gfc_set_constant_character_len (len, ctor->expr,
1617 has_ts ? -1 : first_len);
1618 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1624 /* Check array components. */
1625 if (!c->attr.dimension)
1626 goto scalar;
1628 if (c->attr.pointer)
1630 if (c->as->type != AS_DEFERRED)
1632 gfc_error ("Pointer array component of structure at %C must have a "
1633 "deferred shape");
1634 t = false;
1637 else if (c->attr.allocatable)
1639 if (c->as->type != AS_DEFERRED)
1641 gfc_error ("Allocatable component of structure at %C must have a "
1642 "deferred shape");
1643 t = false;
1646 else
1648 if (c->as->type != AS_EXPLICIT)
1650 gfc_error ("Array component of structure at %C must have an "
1651 "explicit shape");
1652 t = false;
1656 scalar:
1657 if (c->ts.type == BT_CLASS)
1659 bool t2 = gfc_build_class_symbol (&c->ts, &c->attr, &c->as);
1661 if (t)
1662 t = t2;
1665 return t;
1669 /* Match a 'NULL()', and possibly take care of some side effects. */
1671 match
1672 gfc_match_null (gfc_expr **result)
1674 gfc_symbol *sym;
1675 match m, m2 = MATCH_NO;
1677 if ((m = gfc_match (" null ( )")) == MATCH_ERROR)
1678 return MATCH_ERROR;
1680 if (m == MATCH_NO)
1682 locus old_loc;
1683 char name[GFC_MAX_SYMBOL_LEN + 1];
1685 if ((m2 = gfc_match (" null (")) != MATCH_YES)
1686 return m2;
1688 old_loc = gfc_current_locus;
1689 if ((m2 = gfc_match (" %n ) ", name)) == MATCH_ERROR)
1690 return MATCH_ERROR;
1691 if (m2 != MATCH_YES
1692 && ((m2 = gfc_match (" mold = %n )", name)) == MATCH_ERROR))
1693 return MATCH_ERROR;
1694 if (m2 == MATCH_NO)
1696 gfc_current_locus = old_loc;
1697 return MATCH_NO;
1701 /* The NULL symbol now has to be/become an intrinsic function. */
1702 if (gfc_get_symbol ("null", NULL, &sym))
1704 gfc_error ("NULL() initialization at %C is ambiguous");
1705 return MATCH_ERROR;
1708 gfc_intrinsic_symbol (sym);
1710 if (sym->attr.proc != PROC_INTRINSIC
1711 && !(sym->attr.use_assoc && sym->attr.intrinsic)
1712 && (!gfc_add_procedure(&sym->attr, PROC_INTRINSIC, sym->name, NULL)
1713 || !gfc_add_function (&sym->attr, sym->name, NULL)))
1714 return MATCH_ERROR;
1716 *result = gfc_get_null_expr (&gfc_current_locus);
1718 /* Invalid per F2008, C512. */
1719 if (m2 == MATCH_YES)
1721 gfc_error ("NULL() initialization at %C may not have MOLD");
1722 return MATCH_ERROR;
1725 return MATCH_YES;
1729 /* Match the initialization expr for a data pointer or procedure pointer. */
1731 static match
1732 match_pointer_init (gfc_expr **init, int procptr)
1734 match m;
1736 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1738 gfc_error ("Initialization of pointer at %C is not allowed in "
1739 "a PURE procedure");
1740 return MATCH_ERROR;
1743 /* Match NULL() initialization. */
1744 m = gfc_match_null (init);
1745 if (m != MATCH_NO)
1746 return m;
1748 /* Match non-NULL initialization. */
1749 gfc_matching_ptr_assignment = !procptr;
1750 gfc_matching_procptr_assignment = procptr;
1751 m = gfc_match_rvalue (init);
1752 gfc_matching_ptr_assignment = 0;
1753 gfc_matching_procptr_assignment = 0;
1754 if (m == MATCH_ERROR)
1755 return MATCH_ERROR;
1756 else if (m == MATCH_NO)
1758 gfc_error ("Error in pointer initialization at %C");
1759 return MATCH_ERROR;
1762 if (!procptr)
1763 gfc_resolve_expr (*init);
1765 if (!gfc_notify_std (GFC_STD_F2008, "non-NULL pointer "
1766 "initialization at %C"))
1767 return MATCH_ERROR;
1769 return MATCH_YES;
1773 static bool
1774 check_function_name (char *name)
1776 /* In functions that have a RESULT variable defined, the function name always
1777 refers to function calls. Therefore, the name is not allowed to appear in
1778 specification statements. When checking this, be careful about
1779 'hidden' procedure pointer results ('ppr@'). */
1781 if (gfc_current_state () == COMP_FUNCTION)
1783 gfc_symbol *block = gfc_current_block ();
1784 if (block && block->result && block->result != block
1785 && strcmp (block->result->name, "ppr@") != 0
1786 && strcmp (block->name, name) == 0)
1788 gfc_error ("Function name '%s' not allowed at %C", name);
1789 return false;
1793 return true;
1797 /* Match a variable name with an optional initializer. When this
1798 subroutine is called, a variable is expected to be parsed next.
1799 Depending on what is happening at the moment, updates either the
1800 symbol table or the current interface. */
1802 static match
1803 variable_decl (int elem)
1805 char name[GFC_MAX_SYMBOL_LEN + 1];
1806 gfc_expr *initializer, *char_len;
1807 gfc_array_spec *as;
1808 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1809 gfc_charlen *cl;
1810 bool cl_deferred;
1811 locus var_locus;
1812 match m;
1813 bool t;
1814 gfc_symbol *sym;
1816 initializer = NULL;
1817 as = NULL;
1818 cp_as = NULL;
1820 /* When we get here, we've just matched a list of attributes and
1821 maybe a type and a double colon. The next thing we expect to see
1822 is the name of the symbol. */
1823 m = gfc_match_name (name);
1824 if (m != MATCH_YES)
1825 goto cleanup;
1827 var_locus = gfc_current_locus;
1829 /* Now we could see the optional array spec. or character length. */
1830 m = gfc_match_array_spec (&as, true, true);
1831 if (m == MATCH_ERROR)
1832 goto cleanup;
1834 if (m == MATCH_NO)
1835 as = gfc_copy_array_spec (current_as);
1836 else if (current_as
1837 && !merge_array_spec (current_as, as, true))
1839 m = MATCH_ERROR;
1840 goto cleanup;
1843 if (gfc_option.flag_cray_pointer)
1844 cp_as = gfc_copy_array_spec (as);
1846 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1847 determine (and check) whether it can be implied-shape. If it
1848 was parsed as assumed-size, change it because PARAMETERs can not
1849 be assumed-size. */
1850 if (as)
1852 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1854 m = MATCH_ERROR;
1855 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1856 name, &var_locus);
1857 goto cleanup;
1860 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1861 && current_attr.flavor == FL_PARAMETER)
1862 as->type = AS_IMPLIED_SHAPE;
1864 if (as->type == AS_IMPLIED_SHAPE
1865 && !gfc_notify_std (GFC_STD_F2008, "Implied-shape array at %L",
1866 &var_locus))
1868 m = MATCH_ERROR;
1869 goto cleanup;
1873 char_len = NULL;
1874 cl = NULL;
1875 cl_deferred = false;
1877 if (current_ts.type == BT_CHARACTER)
1879 switch (match_char_length (&char_len, &cl_deferred, false))
1881 case MATCH_YES:
1882 cl = gfc_new_charlen (gfc_current_ns, NULL);
1884 cl->length = char_len;
1885 break;
1887 /* Non-constant lengths need to be copied after the first
1888 element. Also copy assumed lengths. */
1889 case MATCH_NO:
1890 if (elem > 1
1891 && (current_ts.u.cl->length == NULL
1892 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1894 cl = gfc_new_charlen (gfc_current_ns, NULL);
1895 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1897 else
1898 cl = current_ts.u.cl;
1900 cl_deferred = current_ts.deferred;
1902 break;
1904 case MATCH_ERROR:
1905 goto cleanup;
1909 /* If this symbol has already shown up in a Cray Pointer declaration,
1910 then we want to set the type & bail out. */
1911 if (gfc_option.flag_cray_pointer)
1913 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1914 if (sym != NULL && sym->attr.cray_pointee)
1916 sym->ts.type = current_ts.type;
1917 sym->ts.kind = current_ts.kind;
1918 sym->ts.u.cl = cl;
1919 sym->ts.u.derived = current_ts.u.derived;
1920 sym->ts.is_c_interop = current_ts.is_c_interop;
1921 sym->ts.is_iso_c = current_ts.is_iso_c;
1922 m = MATCH_YES;
1924 /* Check to see if we have an array specification. */
1925 if (cp_as != NULL)
1927 if (sym->as != NULL)
1929 gfc_error ("Duplicate array spec for Cray pointee at %C");
1930 gfc_free_array_spec (cp_as);
1931 m = MATCH_ERROR;
1932 goto cleanup;
1934 else
1936 if (!gfc_set_array_spec (sym, cp_as, &var_locus))
1937 gfc_internal_error ("Couldn't set pointee array spec.");
1939 /* Fix the array spec. */
1940 m = gfc_mod_pointee_as (sym->as);
1941 if (m == MATCH_ERROR)
1942 goto cleanup;
1945 goto cleanup;
1947 else
1949 gfc_free_array_spec (cp_as);
1953 /* Procedure pointer as function result. */
1954 if (gfc_current_state () == COMP_FUNCTION
1955 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1956 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1957 strcpy (name, "ppr@");
1959 if (gfc_current_state () == COMP_FUNCTION
1960 && strcmp (name, gfc_current_block ()->name) == 0
1961 && gfc_current_block ()->result
1962 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1963 strcpy (name, "ppr@");
1965 /* OK, we've successfully matched the declaration. Now put the
1966 symbol in the current namespace, because it might be used in the
1967 optional initialization expression for this symbol, e.g. this is
1968 perfectly legal:
1970 integer, parameter :: i = huge(i)
1972 This is only true for parameters or variables of a basic type.
1973 For components of derived types, it is not true, so we don't
1974 create a symbol for those yet. If we fail to create the symbol,
1975 bail out. */
1976 if (gfc_current_state () != COMP_DERIVED
1977 && !build_sym (name, cl, cl_deferred, &as, &var_locus))
1979 m = MATCH_ERROR;
1980 goto cleanup;
1983 if (!check_function_name (name))
1985 m = MATCH_ERROR;
1986 goto cleanup;
1989 /* We allow old-style initializations of the form
1990 integer i /2/, j(4) /3*3, 1/
1991 (if no colon has been seen). These are different from data
1992 statements in that initializers are only allowed to apply to the
1993 variable immediately preceding, i.e.
1994 integer i, j /1, 2/
1995 is not allowed. Therefore we have to do some work manually, that
1996 could otherwise be left to the matchers for DATA statements. */
1998 if (!colon_seen && gfc_match (" /") == MATCH_YES)
2000 if (!gfc_notify_std (GFC_STD_GNU, "Old-style "
2001 "initialization at %C"))
2002 return MATCH_ERROR;
2004 return match_old_style_init (name);
2007 /* The double colon must be present in order to have initializers.
2008 Otherwise the statement is ambiguous with an assignment statement. */
2009 if (colon_seen)
2011 if (gfc_match (" =>") == MATCH_YES)
2013 if (!current_attr.pointer)
2015 gfc_error ("Initialization at %C isn't for a pointer variable");
2016 m = MATCH_ERROR;
2017 goto cleanup;
2020 m = match_pointer_init (&initializer, 0);
2021 if (m != MATCH_YES)
2022 goto cleanup;
2024 else if (gfc_match_char ('=') == MATCH_YES)
2026 if (current_attr.pointer)
2028 gfc_error ("Pointer initialization at %C requires '=>', "
2029 "not '='");
2030 m = MATCH_ERROR;
2031 goto cleanup;
2034 m = gfc_match_init_expr (&initializer);
2035 if (m == MATCH_NO)
2037 gfc_error ("Expected an initialization expression at %C");
2038 m = MATCH_ERROR;
2041 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2042 && gfc_state_stack->state != COMP_DERIVED)
2044 gfc_error ("Initialization of variable at %C is not allowed in "
2045 "a PURE procedure");
2046 m = MATCH_ERROR;
2049 if (m != MATCH_YES)
2050 goto cleanup;
2054 if (initializer != NULL && current_attr.allocatable
2055 && gfc_current_state () == COMP_DERIVED)
2057 gfc_error ("Initialization of allocatable component at %C is not "
2058 "allowed");
2059 m = MATCH_ERROR;
2060 goto cleanup;
2063 /* Add the initializer. Note that it is fine if initializer is
2064 NULL here, because we sometimes also need to check if a
2065 declaration *must* have an initialization expression. */
2066 if (gfc_current_state () != COMP_DERIVED)
2067 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2068 else
2070 if (current_ts.type == BT_DERIVED
2071 && !current_attr.pointer && !initializer)
2072 initializer = gfc_default_initializer (&current_ts);
2073 t = build_struct (name, cl, &initializer, &as);
2076 m = (t) ? MATCH_YES : MATCH_ERROR;
2078 cleanup:
2079 /* Free stuff up and return. */
2080 gfc_free_expr (initializer);
2081 gfc_free_array_spec (as);
2083 return m;
2087 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2088 This assumes that the byte size is equal to the kind number for
2089 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2091 match
2092 gfc_match_old_kind_spec (gfc_typespec *ts)
2094 match m;
2095 int original_kind;
2097 if (gfc_match_char ('*') != MATCH_YES)
2098 return MATCH_NO;
2100 m = gfc_match_small_literal_int (&ts->kind, NULL);
2101 if (m != MATCH_YES)
2102 return MATCH_ERROR;
2104 original_kind = ts->kind;
2106 /* Massage the kind numbers for complex types. */
2107 if (ts->type == BT_COMPLEX)
2109 if (ts->kind % 2)
2111 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2112 gfc_basic_typename (ts->type), original_kind);
2113 return MATCH_ERROR;
2115 ts->kind /= 2;
2119 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2120 ts->kind = 8;
2122 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2124 if (ts->kind == 4)
2126 if (gfc_option.flag_real4_kind == 8)
2127 ts->kind = 8;
2128 if (gfc_option.flag_real4_kind == 10)
2129 ts->kind = 10;
2130 if (gfc_option.flag_real4_kind == 16)
2131 ts->kind = 16;
2134 if (ts->kind == 8)
2136 if (gfc_option.flag_real8_kind == 4)
2137 ts->kind = 4;
2138 if (gfc_option.flag_real8_kind == 10)
2139 ts->kind = 10;
2140 if (gfc_option.flag_real8_kind == 16)
2141 ts->kind = 16;
2145 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2147 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2148 gfc_basic_typename (ts->type), original_kind);
2149 return MATCH_ERROR;
2152 if (!gfc_notify_std (GFC_STD_GNU,
2153 "Nonstandard type declaration %s*%d at %C",
2154 gfc_basic_typename(ts->type), original_kind))
2155 return MATCH_ERROR;
2157 return MATCH_YES;
2161 /* Match a kind specification. Since kinds are generally optional, we
2162 usually return MATCH_NO if something goes wrong. If a "kind="
2163 string is found, then we know we have an error. */
2165 match
2166 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2168 locus where, loc;
2169 gfc_expr *e;
2170 match m, n;
2171 char c;
2172 const char *msg;
2174 m = MATCH_NO;
2175 n = MATCH_YES;
2176 e = NULL;
2178 where = loc = gfc_current_locus;
2180 if (kind_expr_only)
2181 goto kind_expr;
2183 if (gfc_match_char ('(') == MATCH_NO)
2184 return MATCH_NO;
2186 /* Also gobbles optional text. */
2187 if (gfc_match (" kind = ") == MATCH_YES)
2188 m = MATCH_ERROR;
2190 loc = gfc_current_locus;
2192 kind_expr:
2193 n = gfc_match_init_expr (&e);
2195 if (n != MATCH_YES)
2197 if (gfc_matching_function)
2199 /* The function kind expression might include use associated or
2200 imported parameters and try again after the specification
2201 expressions..... */
2202 if (gfc_match_char (')') != MATCH_YES)
2204 gfc_error ("Missing right parenthesis at %C");
2205 m = MATCH_ERROR;
2206 goto no_match;
2209 gfc_free_expr (e);
2210 gfc_undo_symbols ();
2211 return MATCH_YES;
2213 else
2215 /* ....or else, the match is real. */
2216 if (n == MATCH_NO)
2217 gfc_error ("Expected initialization expression at %C");
2218 if (n != MATCH_YES)
2219 return MATCH_ERROR;
2223 if (e->rank != 0)
2225 gfc_error ("Expected scalar initialization expression at %C");
2226 m = MATCH_ERROR;
2227 goto no_match;
2230 msg = gfc_extract_int (e, &ts->kind);
2232 if (msg != NULL)
2234 gfc_error (msg);
2235 m = MATCH_ERROR;
2236 goto no_match;
2239 /* Before throwing away the expression, let's see if we had a
2240 C interoperable kind (and store the fact). */
2241 if (e->ts.is_c_interop == 1)
2243 /* Mark this as C interoperable if being declared with one
2244 of the named constants from iso_c_binding. */
2245 ts->is_c_interop = e->ts.is_iso_c;
2246 ts->f90_type = e->ts.f90_type;
2249 gfc_free_expr (e);
2250 e = NULL;
2252 /* Ignore errors to this point, if we've gotten here. This means
2253 we ignore the m=MATCH_ERROR from above. */
2254 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2256 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2257 gfc_basic_typename (ts->type));
2258 gfc_current_locus = where;
2259 return MATCH_ERROR;
2262 /* Warn if, e.g., c_int is used for a REAL variable, but not
2263 if, e.g., c_double is used for COMPLEX as the standard
2264 explicitly says that the kind type parameter for complex and real
2265 variable is the same, i.e. c_float == c_float_complex. */
2266 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2267 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2268 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2269 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2270 "is %s", gfc_basic_typename (ts->f90_type), &where,
2271 gfc_basic_typename (ts->type));
2273 gfc_gobble_whitespace ();
2274 if ((c = gfc_next_ascii_char ()) != ')'
2275 && (ts->type != BT_CHARACTER || c != ','))
2277 if (ts->type == BT_CHARACTER)
2278 gfc_error ("Missing right parenthesis or comma at %C");
2279 else
2280 gfc_error ("Missing right parenthesis at %C");
2281 m = MATCH_ERROR;
2283 else
2284 /* All tests passed. */
2285 m = MATCH_YES;
2287 if(m == MATCH_ERROR)
2288 gfc_current_locus = where;
2290 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2291 ts->kind = 8;
2293 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2295 if (ts->kind == 4)
2297 if (gfc_option.flag_real4_kind == 8)
2298 ts->kind = 8;
2299 if (gfc_option.flag_real4_kind == 10)
2300 ts->kind = 10;
2301 if (gfc_option.flag_real4_kind == 16)
2302 ts->kind = 16;
2305 if (ts->kind == 8)
2307 if (gfc_option.flag_real8_kind == 4)
2308 ts->kind = 4;
2309 if (gfc_option.flag_real8_kind == 10)
2310 ts->kind = 10;
2311 if (gfc_option.flag_real8_kind == 16)
2312 ts->kind = 16;
2316 /* Return what we know from the test(s). */
2317 return m;
2319 no_match:
2320 gfc_free_expr (e);
2321 gfc_current_locus = where;
2322 return m;
2326 static match
2327 match_char_kind (int * kind, int * is_iso_c)
2329 locus where;
2330 gfc_expr *e;
2331 match m, n;
2332 const char *msg;
2334 m = MATCH_NO;
2335 e = NULL;
2336 where = gfc_current_locus;
2338 n = gfc_match_init_expr (&e);
2340 if (n != MATCH_YES && gfc_matching_function)
2342 /* The expression might include use-associated or imported
2343 parameters and try again after the specification
2344 expressions. */
2345 gfc_free_expr (e);
2346 gfc_undo_symbols ();
2347 return MATCH_YES;
2350 if (n == MATCH_NO)
2351 gfc_error ("Expected initialization expression at %C");
2352 if (n != MATCH_YES)
2353 return MATCH_ERROR;
2355 if (e->rank != 0)
2357 gfc_error ("Expected scalar initialization expression at %C");
2358 m = MATCH_ERROR;
2359 goto no_match;
2362 msg = gfc_extract_int (e, kind);
2363 *is_iso_c = e->ts.is_iso_c;
2364 if (msg != NULL)
2366 gfc_error (msg);
2367 m = MATCH_ERROR;
2368 goto no_match;
2371 gfc_free_expr (e);
2373 /* Ignore errors to this point, if we've gotten here. This means
2374 we ignore the m=MATCH_ERROR from above. */
2375 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2377 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2378 m = MATCH_ERROR;
2380 else
2381 /* All tests passed. */
2382 m = MATCH_YES;
2384 if (m == MATCH_ERROR)
2385 gfc_current_locus = where;
2387 /* Return what we know from the test(s). */
2388 return m;
2390 no_match:
2391 gfc_free_expr (e);
2392 gfc_current_locus = where;
2393 return m;
2397 /* Match the various kind/length specifications in a CHARACTER
2398 declaration. We don't return MATCH_NO. */
2400 match
2401 gfc_match_char_spec (gfc_typespec *ts)
2403 int kind, seen_length, is_iso_c;
2404 gfc_charlen *cl;
2405 gfc_expr *len;
2406 match m;
2407 bool deferred;
2409 len = NULL;
2410 seen_length = 0;
2411 kind = 0;
2412 is_iso_c = 0;
2413 deferred = false;
2415 /* Try the old-style specification first. */
2416 old_char_selector = 0;
2418 m = match_char_length (&len, &deferred, true);
2419 if (m != MATCH_NO)
2421 if (m == MATCH_YES)
2422 old_char_selector = 1;
2423 seen_length = 1;
2424 goto done;
2427 m = gfc_match_char ('(');
2428 if (m != MATCH_YES)
2430 m = MATCH_YES; /* Character without length is a single char. */
2431 goto done;
2434 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2435 if (gfc_match (" kind =") == MATCH_YES)
2437 m = match_char_kind (&kind, &is_iso_c);
2439 if (m == MATCH_ERROR)
2440 goto done;
2441 if (m == MATCH_NO)
2442 goto syntax;
2444 if (gfc_match (" , len =") == MATCH_NO)
2445 goto rparen;
2447 m = char_len_param_value (&len, &deferred);
2448 if (m == MATCH_NO)
2449 goto syntax;
2450 if (m == MATCH_ERROR)
2451 goto done;
2452 seen_length = 1;
2454 goto rparen;
2457 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2458 if (gfc_match (" len =") == MATCH_YES)
2460 m = char_len_param_value (&len, &deferred);
2461 if (m == MATCH_NO)
2462 goto syntax;
2463 if (m == MATCH_ERROR)
2464 goto done;
2465 seen_length = 1;
2467 if (gfc_match_char (')') == MATCH_YES)
2468 goto done;
2470 if (gfc_match (" , kind =") != MATCH_YES)
2471 goto syntax;
2473 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2474 goto done;
2476 goto rparen;
2479 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2480 m = char_len_param_value (&len, &deferred);
2481 if (m == MATCH_NO)
2482 goto syntax;
2483 if (m == MATCH_ERROR)
2484 goto done;
2485 seen_length = 1;
2487 m = gfc_match_char (')');
2488 if (m == MATCH_YES)
2489 goto done;
2491 if (gfc_match_char (',') != MATCH_YES)
2492 goto syntax;
2494 gfc_match (" kind ="); /* Gobble optional text. */
2496 m = match_char_kind (&kind, &is_iso_c);
2497 if (m == MATCH_ERROR)
2498 goto done;
2499 if (m == MATCH_NO)
2500 goto syntax;
2502 rparen:
2503 /* Require a right-paren at this point. */
2504 m = gfc_match_char (')');
2505 if (m == MATCH_YES)
2506 goto done;
2508 syntax:
2509 gfc_error ("Syntax error in CHARACTER declaration at %C");
2510 m = MATCH_ERROR;
2511 gfc_free_expr (len);
2512 return m;
2514 done:
2515 /* Deal with character functions after USE and IMPORT statements. */
2516 if (gfc_matching_function)
2518 gfc_free_expr (len);
2519 gfc_undo_symbols ();
2520 return MATCH_YES;
2523 if (m != MATCH_YES)
2525 gfc_free_expr (len);
2526 return m;
2529 /* Do some final massaging of the length values. */
2530 cl = gfc_new_charlen (gfc_current_ns, NULL);
2532 if (seen_length == 0)
2533 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2534 else
2535 cl->length = len;
2537 ts->u.cl = cl;
2538 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2539 ts->deferred = deferred;
2541 /* We have to know if it was a C interoperable kind so we can
2542 do accurate type checking of bind(c) procs, etc. */
2543 if (kind != 0)
2544 /* Mark this as C interoperable if being declared with one
2545 of the named constants from iso_c_binding. */
2546 ts->is_c_interop = is_iso_c;
2547 else if (len != NULL)
2548 /* Here, we might have parsed something such as: character(c_char)
2549 In this case, the parsing code above grabs the c_char when
2550 looking for the length (line 1690, roughly). it's the last
2551 testcase for parsing the kind params of a character variable.
2552 However, it's not actually the length. this seems like it
2553 could be an error.
2554 To see if the user used a C interop kind, test the expr
2555 of the so called length, and see if it's C interoperable. */
2556 ts->is_c_interop = len->ts.is_iso_c;
2558 return MATCH_YES;
2562 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2563 structure to the matched specification. This is necessary for FUNCTION and
2564 IMPLICIT statements.
2566 If implicit_flag is nonzero, then we don't check for the optional
2567 kind specification. Not doing so is needed for matching an IMPLICIT
2568 statement correctly. */
2570 match
2571 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2573 char name[GFC_MAX_SYMBOL_LEN + 1];
2574 gfc_symbol *sym, *dt_sym;
2575 match m;
2576 char c;
2577 bool seen_deferred_kind, matched_type;
2578 const char *dt_name;
2580 /* A belt and braces check that the typespec is correctly being treated
2581 as a deferred characteristic association. */
2582 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2583 && (gfc_current_block ()->result->ts.kind == -1)
2584 && (ts->kind == -1);
2585 gfc_clear_ts (ts);
2586 if (seen_deferred_kind)
2587 ts->kind = -1;
2589 /* Clear the current binding label, in case one is given. */
2590 curr_binding_label = NULL;
2592 if (gfc_match (" byte") == MATCH_YES)
2594 if (!gfc_notify_std (GFC_STD_GNU, "BYTE type at %C"))
2595 return MATCH_ERROR;
2597 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2599 gfc_error ("BYTE type used at %C "
2600 "is not available on the target machine");
2601 return MATCH_ERROR;
2604 ts->type = BT_INTEGER;
2605 ts->kind = 1;
2606 return MATCH_YES;
2610 m = gfc_match (" type (");
2611 matched_type = (m == MATCH_YES);
2612 if (matched_type)
2614 gfc_gobble_whitespace ();
2615 if (gfc_peek_ascii_char () == '*')
2617 if ((m = gfc_match ("*)")) != MATCH_YES)
2618 return m;
2619 if (gfc_current_state () == COMP_DERIVED)
2621 gfc_error ("Assumed type at %C is not allowed for components");
2622 return MATCH_ERROR;
2624 if (!gfc_notify_std (GFC_STD_F2008_TS, "Assumed type "
2625 "at %C"))
2626 return MATCH_ERROR;
2627 ts->type = BT_ASSUMED;
2628 return MATCH_YES;
2631 m = gfc_match ("%n", name);
2632 matched_type = (m == MATCH_YES);
2635 if ((matched_type && strcmp ("integer", name) == 0)
2636 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2638 ts->type = BT_INTEGER;
2639 ts->kind = gfc_default_integer_kind;
2640 goto get_kind;
2643 if ((matched_type && strcmp ("character", name) == 0)
2644 || (!matched_type && gfc_match (" character") == MATCH_YES))
2646 if (matched_type
2647 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2648 "intrinsic-type-spec at %C"))
2649 return MATCH_ERROR;
2651 ts->type = BT_CHARACTER;
2652 if (implicit_flag == 0)
2653 m = gfc_match_char_spec (ts);
2654 else
2655 m = MATCH_YES;
2657 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2658 m = MATCH_ERROR;
2660 return m;
2663 if ((matched_type && strcmp ("real", name) == 0)
2664 || (!matched_type && gfc_match (" real") == MATCH_YES))
2666 ts->type = BT_REAL;
2667 ts->kind = gfc_default_real_kind;
2668 goto get_kind;
2671 if ((matched_type
2672 && (strcmp ("doubleprecision", name) == 0
2673 || (strcmp ("double", name) == 0
2674 && gfc_match (" precision") == MATCH_YES)))
2675 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2677 if (matched_type
2678 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2679 "intrinsic-type-spec at %C"))
2680 return MATCH_ERROR;
2681 if (matched_type && gfc_match_char (')') != MATCH_YES)
2682 return MATCH_ERROR;
2684 ts->type = BT_REAL;
2685 ts->kind = gfc_default_double_kind;
2686 return MATCH_YES;
2689 if ((matched_type && strcmp ("complex", name) == 0)
2690 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2692 ts->type = BT_COMPLEX;
2693 ts->kind = gfc_default_complex_kind;
2694 goto get_kind;
2697 if ((matched_type
2698 && (strcmp ("doublecomplex", name) == 0
2699 || (strcmp ("double", name) == 0
2700 && gfc_match (" complex") == MATCH_YES)))
2701 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2703 if (!gfc_notify_std (GFC_STD_GNU, "DOUBLE COMPLEX at %C"))
2704 return MATCH_ERROR;
2706 if (matched_type
2707 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2708 "intrinsic-type-spec at %C"))
2709 return MATCH_ERROR;
2711 if (matched_type && gfc_match_char (')') != MATCH_YES)
2712 return MATCH_ERROR;
2714 ts->type = BT_COMPLEX;
2715 ts->kind = gfc_default_double_kind;
2716 return MATCH_YES;
2719 if ((matched_type && strcmp ("logical", name) == 0)
2720 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2722 ts->type = BT_LOGICAL;
2723 ts->kind = gfc_default_logical_kind;
2724 goto get_kind;
2727 if (matched_type)
2728 m = gfc_match_char (')');
2730 if (m == MATCH_YES)
2731 ts->type = BT_DERIVED;
2732 else
2734 /* Match CLASS declarations. */
2735 m = gfc_match (" class ( * )");
2736 if (m == MATCH_ERROR)
2737 return MATCH_ERROR;
2738 else if (m == MATCH_YES)
2740 gfc_symbol *upe;
2741 gfc_symtree *st;
2742 ts->type = BT_CLASS;
2743 gfc_find_symbol ("STAR", gfc_current_ns, 1, &upe);
2744 if (upe == NULL)
2746 upe = gfc_new_symbol ("STAR", gfc_current_ns);
2747 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2748 st->n.sym = upe;
2749 gfc_set_sym_referenced (upe);
2750 upe->refs++;
2751 upe->ts.type = BT_VOID;
2752 upe->attr.unlimited_polymorphic = 1;
2753 /* This is essential to force the construction of
2754 unlimited polymorphic component class containers. */
2755 upe->attr.zero_comp = 1;
2756 if (!gfc_add_flavor (&upe->attr, FL_DERIVED, NULL,
2757 &gfc_current_locus))
2758 return MATCH_ERROR;
2760 else
2762 st = gfc_find_symtree (gfc_current_ns->sym_root, "STAR");
2763 if (st == NULL)
2764 st = gfc_new_symtree (&gfc_current_ns->sym_root, "STAR");
2765 st->n.sym = upe;
2766 upe->refs++;
2768 ts->u.derived = upe;
2769 return m;
2772 m = gfc_match (" class ( %n )", name);
2773 if (m != MATCH_YES)
2774 return m;
2775 ts->type = BT_CLASS;
2777 if (!gfc_notify_std (GFC_STD_F2003, "CLASS statement at %C"))
2778 return MATCH_ERROR;
2781 /* Defer association of the derived type until the end of the
2782 specification block. However, if the derived type can be
2783 found, add it to the typespec. */
2784 if (gfc_matching_function)
2786 ts->u.derived = NULL;
2787 if (gfc_current_state () != COMP_INTERFACE
2788 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2790 sym = gfc_find_dt_in_generic (sym);
2791 ts->u.derived = sym;
2793 return MATCH_YES;
2796 /* Search for the name but allow the components to be defined later. If
2797 type = -1, this typespec has been seen in a function declaration but
2798 the type could not be accessed at that point. The actual derived type is
2799 stored in a symtree with the first letter of the name capitalized; the
2800 symtree with the all lower-case name contains the associated
2801 generic function. */
2802 dt_name = gfc_get_string ("%c%s",
2803 (char) TOUPPER ((unsigned char) name[0]),
2804 (const char*)&name[1]);
2805 sym = NULL;
2806 dt_sym = NULL;
2807 if (ts->kind != -1)
2809 gfc_get_ha_symbol (name, &sym);
2810 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2812 gfc_error ("Type name '%s' at %C is ambiguous", name);
2813 return MATCH_ERROR;
2815 if (sym->generic && !dt_sym)
2816 dt_sym = gfc_find_dt_in_generic (sym);
2818 else if (ts->kind == -1)
2820 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2821 || gfc_current_ns->has_import_set;
2822 gfc_find_symbol (name, NULL, iface, &sym);
2823 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2825 gfc_error ("Type name '%s' at %C is ambiguous", name);
2826 return MATCH_ERROR;
2828 if (sym && sym->generic && !dt_sym)
2829 dt_sym = gfc_find_dt_in_generic (sym);
2831 ts->kind = 0;
2832 if (sym == NULL)
2833 return MATCH_NO;
2836 if ((sym->attr.flavor != FL_UNKNOWN
2837 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2838 || sym->attr.subroutine)
2840 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2841 "entity at %L, which has the same name", name,
2842 &sym->declared_at);
2843 return MATCH_ERROR;
2846 gfc_set_sym_referenced (sym);
2847 if (!sym->attr.generic
2848 && !gfc_add_generic (&sym->attr, sym->name, NULL))
2849 return MATCH_ERROR;
2851 if (!sym->attr.function
2852 && !gfc_add_function (&sym->attr, sym->name, NULL))
2853 return MATCH_ERROR;
2855 if (!dt_sym)
2857 gfc_interface *intr, *head;
2859 /* Use upper case to save the actual derived-type symbol. */
2860 gfc_get_symbol (dt_name, NULL, &dt_sym);
2861 dt_sym->name = gfc_get_string (sym->name);
2862 head = sym->generic;
2863 intr = gfc_get_interface ();
2864 intr->sym = dt_sym;
2865 intr->where = gfc_current_locus;
2866 intr->next = head;
2867 sym->generic = intr;
2868 sym->attr.if_source = IFSRC_DECL;
2871 gfc_set_sym_referenced (dt_sym);
2873 if (dt_sym->attr.flavor != FL_DERIVED
2874 && !gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL))
2875 return MATCH_ERROR;
2877 ts->u.derived = dt_sym;
2879 return MATCH_YES;
2881 get_kind:
2882 if (matched_type
2883 && !gfc_notify_std (GFC_STD_F2008, "TYPE with "
2884 "intrinsic-type-spec at %C"))
2885 return MATCH_ERROR;
2887 /* For all types except double, derived and character, look for an
2888 optional kind specifier. MATCH_NO is actually OK at this point. */
2889 if (implicit_flag == 1)
2891 if (matched_type && gfc_match_char (')') != MATCH_YES)
2892 return MATCH_ERROR;
2894 return MATCH_YES;
2897 if (gfc_current_form == FORM_FREE)
2899 c = gfc_peek_ascii_char ();
2900 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2901 && c != ':' && c != ',')
2903 if (matched_type && c == ')')
2905 gfc_next_ascii_char ();
2906 return MATCH_YES;
2908 return MATCH_NO;
2912 m = gfc_match_kind_spec (ts, false);
2913 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2914 m = gfc_match_old_kind_spec (ts);
2916 if (matched_type && gfc_match_char (')') != MATCH_YES)
2917 return MATCH_ERROR;
2919 /* Defer association of the KIND expression of function results
2920 until after USE and IMPORT statements. */
2921 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2922 || gfc_matching_function)
2923 return MATCH_YES;
2925 if (m == MATCH_NO)
2926 m = MATCH_YES; /* No kind specifier found. */
2928 return m;
2932 /* Match an IMPLICIT NONE statement. Actually, this statement is
2933 already matched in parse.c, or we would not end up here in the
2934 first place. So the only thing we need to check, is if there is
2935 trailing garbage. If not, the match is successful. */
2937 match
2938 gfc_match_implicit_none (void)
2940 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2944 /* Match the letter range(s) of an IMPLICIT statement. */
2946 static match
2947 match_implicit_range (void)
2949 char c, c1, c2;
2950 int inner;
2951 locus cur_loc;
2953 cur_loc = gfc_current_locus;
2955 gfc_gobble_whitespace ();
2956 c = gfc_next_ascii_char ();
2957 if (c != '(')
2959 gfc_error ("Missing character range in IMPLICIT at %C");
2960 goto bad;
2963 inner = 1;
2964 while (inner)
2966 gfc_gobble_whitespace ();
2967 c1 = gfc_next_ascii_char ();
2968 if (!ISALPHA (c1))
2969 goto bad;
2971 gfc_gobble_whitespace ();
2972 c = gfc_next_ascii_char ();
2974 switch (c)
2976 case ')':
2977 inner = 0; /* Fall through. */
2979 case ',':
2980 c2 = c1;
2981 break;
2983 case '-':
2984 gfc_gobble_whitespace ();
2985 c2 = gfc_next_ascii_char ();
2986 if (!ISALPHA (c2))
2987 goto bad;
2989 gfc_gobble_whitespace ();
2990 c = gfc_next_ascii_char ();
2992 if ((c != ',') && (c != ')'))
2993 goto bad;
2994 if (c == ')')
2995 inner = 0;
2997 break;
2999 default:
3000 goto bad;
3003 if (c1 > c2)
3005 gfc_error ("Letters must be in alphabetic order in "
3006 "IMPLICIT statement at %C");
3007 goto bad;
3010 /* See if we can add the newly matched range to the pending
3011 implicits from this IMPLICIT statement. We do not check for
3012 conflicts with whatever earlier IMPLICIT statements may have
3013 set. This is done when we've successfully finished matching
3014 the current one. */
3015 if (!gfc_add_new_implicit_range (c1, c2))
3016 goto bad;
3019 return MATCH_YES;
3021 bad:
3022 gfc_syntax_error (ST_IMPLICIT);
3024 gfc_current_locus = cur_loc;
3025 return MATCH_ERROR;
3029 /* Match an IMPLICIT statement, storing the types for
3030 gfc_set_implicit() if the statement is accepted by the parser.
3031 There is a strange looking, but legal syntactic construction
3032 possible. It looks like:
3034 IMPLICIT INTEGER (a-b) (c-d)
3036 This is legal if "a-b" is a constant expression that happens to
3037 equal one of the legal kinds for integers. The real problem
3038 happens with an implicit specification that looks like:
3040 IMPLICIT INTEGER (a-b)
3042 In this case, a typespec matcher that is "greedy" (as most of the
3043 matchers are) gobbles the character range as a kindspec, leaving
3044 nothing left. We therefore have to go a bit more slowly in the
3045 matching process by inhibiting the kindspec checking during
3046 typespec matching and checking for a kind later. */
3048 match
3049 gfc_match_implicit (void)
3051 gfc_typespec ts;
3052 locus cur_loc;
3053 char c;
3054 match m;
3056 gfc_clear_ts (&ts);
3058 /* We don't allow empty implicit statements. */
3059 if (gfc_match_eos () == MATCH_YES)
3061 gfc_error ("Empty IMPLICIT statement at %C");
3062 return MATCH_ERROR;
3067 /* First cleanup. */
3068 gfc_clear_new_implicit ();
3070 /* A basic type is mandatory here. */
3071 m = gfc_match_decl_type_spec (&ts, 1);
3072 if (m == MATCH_ERROR)
3073 goto error;
3074 if (m == MATCH_NO)
3075 goto syntax;
3077 cur_loc = gfc_current_locus;
3078 m = match_implicit_range ();
3080 if (m == MATCH_YES)
3082 /* We may have <TYPE> (<RANGE>). */
3083 gfc_gobble_whitespace ();
3084 c = gfc_next_ascii_char ();
3085 if ((c == '\n') || (c == ','))
3087 /* Check for CHARACTER with no length parameter. */
3088 if (ts.type == BT_CHARACTER && !ts.u.cl)
3090 ts.kind = gfc_default_character_kind;
3091 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3092 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3093 NULL, 1);
3096 /* Record the Successful match. */
3097 if (!gfc_merge_new_implicit (&ts))
3098 return MATCH_ERROR;
3099 continue;
3102 gfc_current_locus = cur_loc;
3105 /* Discard the (incorrectly) matched range. */
3106 gfc_clear_new_implicit ();
3108 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3109 if (ts.type == BT_CHARACTER)
3110 m = gfc_match_char_spec (&ts);
3111 else
3113 m = gfc_match_kind_spec (&ts, false);
3114 if (m == MATCH_NO)
3116 m = gfc_match_old_kind_spec (&ts);
3117 if (m == MATCH_ERROR)
3118 goto error;
3119 if (m == MATCH_NO)
3120 goto syntax;
3123 if (m == MATCH_ERROR)
3124 goto error;
3126 m = match_implicit_range ();
3127 if (m == MATCH_ERROR)
3128 goto error;
3129 if (m == MATCH_NO)
3130 goto syntax;
3132 gfc_gobble_whitespace ();
3133 c = gfc_next_ascii_char ();
3134 if ((c != '\n') && (c != ','))
3135 goto syntax;
3137 if (!gfc_merge_new_implicit (&ts))
3138 return MATCH_ERROR;
3140 while (c == ',');
3142 return MATCH_YES;
3144 syntax:
3145 gfc_syntax_error (ST_IMPLICIT);
3147 error:
3148 return MATCH_ERROR;
3152 match
3153 gfc_match_import (void)
3155 char name[GFC_MAX_SYMBOL_LEN + 1];
3156 match m;
3157 gfc_symbol *sym;
3158 gfc_symtree *st;
3160 if (gfc_current_ns->proc_name == NULL
3161 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3163 gfc_error ("IMPORT statement at %C only permitted in "
3164 "an INTERFACE body");
3165 return MATCH_ERROR;
3168 if (!gfc_notify_std (GFC_STD_F2003, "IMPORT statement at %C"))
3169 return MATCH_ERROR;
3171 if (gfc_match_eos () == MATCH_YES)
3173 /* All host variables should be imported. */
3174 gfc_current_ns->has_import_set = 1;
3175 return MATCH_YES;
3178 if (gfc_match (" ::") == MATCH_YES)
3180 if (gfc_match_eos () == MATCH_YES)
3182 gfc_error ("Expecting list of named entities at %C");
3183 return MATCH_ERROR;
3187 for(;;)
3189 sym = NULL;
3190 m = gfc_match (" %n", name);
3191 switch (m)
3193 case MATCH_YES:
3194 if (gfc_current_ns->parent != NULL
3195 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3197 gfc_error ("Type name '%s' at %C is ambiguous", name);
3198 return MATCH_ERROR;
3200 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3201 && gfc_find_symbol (name,
3202 gfc_current_ns->proc_name->ns->parent,
3203 1, &sym))
3205 gfc_error ("Type name '%s' at %C is ambiguous", name);
3206 return MATCH_ERROR;
3209 if (sym == NULL)
3211 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3212 "at %C - does not exist.", name);
3213 return MATCH_ERROR;
3216 if (gfc_find_symtree (gfc_current_ns->sym_root, name))
3218 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3219 "at %C.", name);
3220 goto next_item;
3223 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
3224 st->n.sym = sym;
3225 sym->refs++;
3226 sym->attr.imported = 1;
3228 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3230 /* The actual derived type is stored in a symtree with the first
3231 letter of the name capitalized; the symtree with the all
3232 lower-case name contains the associated generic function. */
3233 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3234 gfc_get_string ("%c%s",
3235 (char) TOUPPER ((unsigned char) name[0]),
3236 &name[1]));
3237 st->n.sym = sym;
3238 sym->refs++;
3239 sym->attr.imported = 1;
3242 goto next_item;
3244 case MATCH_NO:
3245 break;
3247 case MATCH_ERROR:
3248 return MATCH_ERROR;
3251 next_item:
3252 if (gfc_match_eos () == MATCH_YES)
3253 break;
3254 if (gfc_match_char (',') != MATCH_YES)
3255 goto syntax;
3258 return MATCH_YES;
3260 syntax:
3261 gfc_error ("Syntax error in IMPORT statement at %C");
3262 return MATCH_ERROR;
3266 /* A minimal implementation of gfc_match without whitespace, escape
3267 characters or variable arguments. Returns true if the next
3268 characters match the TARGET template exactly. */
3270 static bool
3271 match_string_p (const char *target)
3273 const char *p;
3275 for (p = target; *p; p++)
3276 if ((char) gfc_next_ascii_char () != *p)
3277 return false;
3278 return true;
3281 /* Matches an attribute specification including array specs. If
3282 successful, leaves the variables current_attr and current_as
3283 holding the specification. Also sets the colon_seen variable for
3284 later use by matchers associated with initializations.
3286 This subroutine is a little tricky in the sense that we don't know
3287 if we really have an attr-spec until we hit the double colon.
3288 Until that time, we can only return MATCH_NO. This forces us to
3289 check for duplicate specification at this level. */
3291 static match
3292 match_attr_spec (void)
3294 /* Modifiers that can exist in a type statement. */
3295 enum
3296 { GFC_DECL_BEGIN = 0,
3297 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3298 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3299 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3300 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3301 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3302 DECL_NONE, GFC_DECL_END /* Sentinel */
3305 /* GFC_DECL_END is the sentinel, index starts at 0. */
3306 #define NUM_DECL GFC_DECL_END
3308 locus start, seen_at[NUM_DECL];
3309 int seen[NUM_DECL];
3310 unsigned int d;
3311 const char *attr;
3312 match m;
3313 bool t;
3315 gfc_clear_attr (&current_attr);
3316 start = gfc_current_locus;
3318 current_as = NULL;
3319 colon_seen = 0;
3321 /* See if we get all of the keywords up to the final double colon. */
3322 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3323 seen[d] = 0;
3325 for (;;)
3327 char ch;
3329 d = DECL_NONE;
3330 gfc_gobble_whitespace ();
3332 ch = gfc_next_ascii_char ();
3333 if (ch == ':')
3335 /* This is the successful exit condition for the loop. */
3336 if (gfc_next_ascii_char () == ':')
3337 break;
3339 else if (ch == ',')
3341 gfc_gobble_whitespace ();
3342 switch (gfc_peek_ascii_char ())
3344 case 'a':
3345 gfc_next_ascii_char ();
3346 switch (gfc_next_ascii_char ())
3348 case 'l':
3349 if (match_string_p ("locatable"))
3351 /* Matched "allocatable". */
3352 d = DECL_ALLOCATABLE;
3354 break;
3356 case 's':
3357 if (match_string_p ("ynchronous"))
3359 /* Matched "asynchronous". */
3360 d = DECL_ASYNCHRONOUS;
3362 break;
3364 break;
3366 case 'b':
3367 /* Try and match the bind(c). */
3368 m = gfc_match_bind_c (NULL, true);
3369 if (m == MATCH_YES)
3370 d = DECL_IS_BIND_C;
3371 else if (m == MATCH_ERROR)
3372 goto cleanup;
3373 break;
3375 case 'c':
3376 gfc_next_ascii_char ();
3377 if ('o' != gfc_next_ascii_char ())
3378 break;
3379 switch (gfc_next_ascii_char ())
3381 case 'd':
3382 if (match_string_p ("imension"))
3384 d = DECL_CODIMENSION;
3385 break;
3387 case 'n':
3388 if (match_string_p ("tiguous"))
3390 d = DECL_CONTIGUOUS;
3391 break;
3394 break;
3396 case 'd':
3397 if (match_string_p ("dimension"))
3398 d = DECL_DIMENSION;
3399 break;
3401 case 'e':
3402 if (match_string_p ("external"))
3403 d = DECL_EXTERNAL;
3404 break;
3406 case 'i':
3407 if (match_string_p ("int"))
3409 ch = gfc_next_ascii_char ();
3410 if (ch == 'e')
3412 if (match_string_p ("nt"))
3414 /* Matched "intent". */
3415 /* TODO: Call match_intent_spec from here. */
3416 if (gfc_match (" ( in out )") == MATCH_YES)
3417 d = DECL_INOUT;
3418 else if (gfc_match (" ( in )") == MATCH_YES)
3419 d = DECL_IN;
3420 else if (gfc_match (" ( out )") == MATCH_YES)
3421 d = DECL_OUT;
3424 else if (ch == 'r')
3426 if (match_string_p ("insic"))
3428 /* Matched "intrinsic". */
3429 d = DECL_INTRINSIC;
3433 break;
3435 case 'o':
3436 if (match_string_p ("optional"))
3437 d = DECL_OPTIONAL;
3438 break;
3440 case 'p':
3441 gfc_next_ascii_char ();
3442 switch (gfc_next_ascii_char ())
3444 case 'a':
3445 if (match_string_p ("rameter"))
3447 /* Matched "parameter". */
3448 d = DECL_PARAMETER;
3450 break;
3452 case 'o':
3453 if (match_string_p ("inter"))
3455 /* Matched "pointer". */
3456 d = DECL_POINTER;
3458 break;
3460 case 'r':
3461 ch = gfc_next_ascii_char ();
3462 if (ch == 'i')
3464 if (match_string_p ("vate"))
3466 /* Matched "private". */
3467 d = DECL_PRIVATE;
3470 else if (ch == 'o')
3472 if (match_string_p ("tected"))
3474 /* Matched "protected". */
3475 d = DECL_PROTECTED;
3478 break;
3480 case 'u':
3481 if (match_string_p ("blic"))
3483 /* Matched "public". */
3484 d = DECL_PUBLIC;
3486 break;
3488 break;
3490 case 's':
3491 if (match_string_p ("save"))
3492 d = DECL_SAVE;
3493 break;
3495 case 't':
3496 if (match_string_p ("target"))
3497 d = DECL_TARGET;
3498 break;
3500 case 'v':
3501 gfc_next_ascii_char ();
3502 ch = gfc_next_ascii_char ();
3503 if (ch == 'a')
3505 if (match_string_p ("lue"))
3507 /* Matched "value". */
3508 d = DECL_VALUE;
3511 else if (ch == 'o')
3513 if (match_string_p ("latile"))
3515 /* Matched "volatile". */
3516 d = DECL_VOLATILE;
3519 break;
3523 /* No double colon and no recognizable decl_type, so assume that
3524 we've been looking at something else the whole time. */
3525 if (d == DECL_NONE)
3527 m = MATCH_NO;
3528 goto cleanup;
3531 /* Check to make sure any parens are paired up correctly. */
3532 if (gfc_match_parens () == MATCH_ERROR)
3534 m = MATCH_ERROR;
3535 goto cleanup;
3538 seen[d]++;
3539 seen_at[d] = gfc_current_locus;
3541 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3543 gfc_array_spec *as = NULL;
3545 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3546 d == DECL_CODIMENSION);
3548 if (current_as == NULL)
3549 current_as = as;
3550 else if (m == MATCH_YES)
3552 if (!merge_array_spec (as, current_as, false))
3553 m = MATCH_ERROR;
3554 free (as);
3557 if (m == MATCH_NO)
3559 if (d == DECL_CODIMENSION)
3560 gfc_error ("Missing codimension specification at %C");
3561 else
3562 gfc_error ("Missing dimension specification at %C");
3563 m = MATCH_ERROR;
3566 if (m == MATCH_ERROR)
3567 goto cleanup;
3571 /* Since we've seen a double colon, we have to be looking at an
3572 attr-spec. This means that we can now issue errors. */
3573 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3574 if (seen[d] > 1)
3576 switch (d)
3578 case DECL_ALLOCATABLE:
3579 attr = "ALLOCATABLE";
3580 break;
3581 case DECL_ASYNCHRONOUS:
3582 attr = "ASYNCHRONOUS";
3583 break;
3584 case DECL_CODIMENSION:
3585 attr = "CODIMENSION";
3586 break;
3587 case DECL_CONTIGUOUS:
3588 attr = "CONTIGUOUS";
3589 break;
3590 case DECL_DIMENSION:
3591 attr = "DIMENSION";
3592 break;
3593 case DECL_EXTERNAL:
3594 attr = "EXTERNAL";
3595 break;
3596 case DECL_IN:
3597 attr = "INTENT (IN)";
3598 break;
3599 case DECL_OUT:
3600 attr = "INTENT (OUT)";
3601 break;
3602 case DECL_INOUT:
3603 attr = "INTENT (IN OUT)";
3604 break;
3605 case DECL_INTRINSIC:
3606 attr = "INTRINSIC";
3607 break;
3608 case DECL_OPTIONAL:
3609 attr = "OPTIONAL";
3610 break;
3611 case DECL_PARAMETER:
3612 attr = "PARAMETER";
3613 break;
3614 case DECL_POINTER:
3615 attr = "POINTER";
3616 break;
3617 case DECL_PROTECTED:
3618 attr = "PROTECTED";
3619 break;
3620 case DECL_PRIVATE:
3621 attr = "PRIVATE";
3622 break;
3623 case DECL_PUBLIC:
3624 attr = "PUBLIC";
3625 break;
3626 case DECL_SAVE:
3627 attr = "SAVE";
3628 break;
3629 case DECL_TARGET:
3630 attr = "TARGET";
3631 break;
3632 case DECL_IS_BIND_C:
3633 attr = "IS_BIND_C";
3634 break;
3635 case DECL_VALUE:
3636 attr = "VALUE";
3637 break;
3638 case DECL_VOLATILE:
3639 attr = "VOLATILE";
3640 break;
3641 default:
3642 attr = NULL; /* This shouldn't happen. */
3645 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3646 m = MATCH_ERROR;
3647 goto cleanup;
3650 /* Now that we've dealt with duplicate attributes, add the attributes
3651 to the current attribute. */
3652 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3654 if (seen[d] == 0)
3655 continue;
3657 if (gfc_current_state () == COMP_DERIVED
3658 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3659 && d != DECL_POINTER && d != DECL_PRIVATE
3660 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3662 if (d == DECL_ALLOCATABLE)
3664 if (!gfc_notify_std (GFC_STD_F2003, "ALLOCATABLE "
3665 "attribute at %C in a TYPE definition"))
3667 m = MATCH_ERROR;
3668 goto cleanup;
3671 else
3673 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3674 &seen_at[d]);
3675 m = MATCH_ERROR;
3676 goto cleanup;
3680 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3681 && gfc_current_state () != COMP_MODULE)
3683 if (d == DECL_PRIVATE)
3684 attr = "PRIVATE";
3685 else
3686 attr = "PUBLIC";
3687 if (gfc_current_state () == COMP_DERIVED
3688 && gfc_state_stack->previous
3689 && gfc_state_stack->previous->state == COMP_MODULE)
3691 if (!gfc_notify_std (GFC_STD_F2003, "Attribute %s "
3692 "at %L in a TYPE definition", attr,
3693 &seen_at[d]))
3695 m = MATCH_ERROR;
3696 goto cleanup;
3699 else
3701 gfc_error ("%s attribute at %L is not allowed outside of the "
3702 "specification part of a module", attr, &seen_at[d]);
3703 m = MATCH_ERROR;
3704 goto cleanup;
3708 switch (d)
3710 case DECL_ALLOCATABLE:
3711 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3712 break;
3714 case DECL_ASYNCHRONOUS:
3715 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS attribute at %C"))
3716 t = false;
3717 else
3718 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3719 break;
3721 case DECL_CODIMENSION:
3722 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3723 break;
3725 case DECL_CONTIGUOUS:
3726 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS attribute at %C"))
3727 t = false;
3728 else
3729 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3730 break;
3732 case DECL_DIMENSION:
3733 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3734 break;
3736 case DECL_EXTERNAL:
3737 t = gfc_add_external (&current_attr, &seen_at[d]);
3738 break;
3740 case DECL_IN:
3741 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3742 break;
3744 case DECL_OUT:
3745 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3746 break;
3748 case DECL_INOUT:
3749 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3750 break;
3752 case DECL_INTRINSIC:
3753 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3754 break;
3756 case DECL_OPTIONAL:
3757 t = gfc_add_optional (&current_attr, &seen_at[d]);
3758 break;
3760 case DECL_PARAMETER:
3761 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3762 break;
3764 case DECL_POINTER:
3765 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3766 break;
3768 case DECL_PROTECTED:
3769 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3771 gfc_error ("PROTECTED at %C only allowed in specification "
3772 "part of a module");
3773 t = false;
3774 break;
3777 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED attribute at %C"))
3778 t = false;
3779 else
3780 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3781 break;
3783 case DECL_PRIVATE:
3784 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3785 &seen_at[d]);
3786 break;
3788 case DECL_PUBLIC:
3789 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3790 &seen_at[d]);
3791 break;
3793 case DECL_SAVE:
3794 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3795 break;
3797 case DECL_TARGET:
3798 t = gfc_add_target (&current_attr, &seen_at[d]);
3799 break;
3801 case DECL_IS_BIND_C:
3802 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3803 break;
3805 case DECL_VALUE:
3806 if (!gfc_notify_std (GFC_STD_F2003, "VALUE attribute at %C"))
3807 t = false;
3808 else
3809 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3810 break;
3812 case DECL_VOLATILE:
3813 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE attribute at %C"))
3814 t = false;
3815 else
3816 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3817 break;
3819 default:
3820 gfc_internal_error ("match_attr_spec(): Bad attribute");
3823 if (!t)
3825 m = MATCH_ERROR;
3826 goto cleanup;
3830 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3831 if (gfc_current_state () == COMP_MODULE && !current_attr.save
3832 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
3833 current_attr.save = SAVE_IMPLICIT;
3835 colon_seen = 1;
3836 return MATCH_YES;
3838 cleanup:
3839 gfc_current_locus = start;
3840 gfc_free_array_spec (current_as);
3841 current_as = NULL;
3842 return m;
3846 /* Set the binding label, dest_label, either with the binding label
3847 stored in the given gfc_typespec, ts, or if none was provided, it
3848 will be the symbol name in all lower case, as required by the draft
3849 (J3/04-007, section 15.4.1). If a binding label was given and
3850 there is more than one argument (num_idents), it is an error. */
3852 static bool
3853 set_binding_label (const char **dest_label, const char *sym_name,
3854 int num_idents)
3856 if (num_idents > 1 && has_name_equals)
3858 gfc_error ("Multiple identifiers provided with "
3859 "single NAME= specifier at %C");
3860 return false;
3863 if (curr_binding_label)
3864 /* Binding label given; store in temp holder till have sym. */
3865 *dest_label = curr_binding_label;
3866 else
3868 /* No binding label given, and the NAME= specifier did not exist,
3869 which means there was no NAME="". */
3870 if (sym_name != NULL && has_name_equals == 0)
3871 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
3874 return true;
3878 /* Set the status of the given common block as being BIND(C) or not,
3879 depending on the given parameter, is_bind_c. */
3881 void
3882 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3884 com_block->is_bind_c = is_bind_c;
3885 return;
3889 /* Verify that the given gfc_typespec is for a C interoperable type. */
3891 bool
3892 gfc_verify_c_interop (gfc_typespec *ts)
3894 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3895 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3896 ? true : false;
3897 else if (ts->type == BT_CLASS)
3898 return false;
3899 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
3900 return false;
3902 return true;
3906 /* Verify that the variables of a given common block, which has been
3907 defined with the attribute specifier bind(c), to be of a C
3908 interoperable type. Errors will be reported here, if
3909 encountered. */
3911 bool
3912 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3914 gfc_symbol *curr_sym = NULL;
3915 bool retval = true;
3917 curr_sym = com_block->head;
3919 /* Make sure we have at least one symbol. */
3920 if (curr_sym == NULL)
3921 return retval;
3923 /* Here we know we have a symbol, so we'll execute this loop
3924 at least once. */
3927 /* The second to last param, 1, says this is in a common block. */
3928 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3929 curr_sym = curr_sym->common_next;
3930 } while (curr_sym != NULL);
3932 return retval;
3936 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3937 an appropriate error message is reported. */
3939 bool
3940 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3941 int is_in_common, gfc_common_head *com_block)
3943 bool bind_c_function = false;
3944 bool retval = true;
3946 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3947 bind_c_function = true;
3949 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3951 tmp_sym = tmp_sym->result;
3952 /* Make sure it wasn't an implicitly typed result. */
3953 if (tmp_sym->attr.implicit_type && gfc_option.warn_c_binding_type)
3955 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3956 "%L may not be C interoperable", tmp_sym->name,
3957 &tmp_sym->declared_at);
3958 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3959 /* Mark it as C interoperable to prevent duplicate warnings. */
3960 tmp_sym->ts.is_c_interop = 1;
3961 tmp_sym->attr.is_c_interop = 1;
3965 /* Here, we know we have the bind(c) attribute, so if we have
3966 enough type info, then verify that it's a C interop kind.
3967 The info could be in the symbol already, or possibly still in
3968 the given ts (current_ts), so look in both. */
3969 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3971 if (!gfc_verify_c_interop (&(tmp_sym->ts)))
3973 /* See if we're dealing with a sym in a common block or not. */
3974 if (is_in_common == 1 && gfc_option.warn_c_binding_type)
3976 gfc_warning ("Variable '%s' in common block '%s' at %L "
3977 "may not be a C interoperable "
3978 "kind though common block '%s' is BIND(C)",
3979 tmp_sym->name, com_block->name,
3980 &(tmp_sym->declared_at), com_block->name);
3982 else
3984 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3985 gfc_error ("Type declaration '%s' at %L is not C "
3986 "interoperable but it is BIND(C)",
3987 tmp_sym->name, &(tmp_sym->declared_at));
3988 else if (gfc_option.warn_c_binding_type)
3989 gfc_warning ("Variable '%s' at %L "
3990 "may not be a C interoperable "
3991 "kind but it is bind(c)",
3992 tmp_sym->name, &(tmp_sym->declared_at));
3996 /* Variables declared w/in a common block can't be bind(c)
3997 since there's no way for C to see these variables, so there's
3998 semantically no reason for the attribute. */
3999 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
4001 gfc_error ("Variable '%s' in common block '%s' at "
4002 "%L cannot be declared with BIND(C) "
4003 "since it is not a global",
4004 tmp_sym->name, com_block->name,
4005 &(tmp_sym->declared_at));
4006 retval = false;
4009 /* Scalar variables that are bind(c) can not have the pointer
4010 or allocatable attributes. */
4011 if (tmp_sym->attr.is_bind_c == 1)
4013 if (tmp_sym->attr.pointer == 1)
4015 gfc_error ("Variable '%s' at %L cannot have both the "
4016 "POINTER and BIND(C) attributes",
4017 tmp_sym->name, &(tmp_sym->declared_at));
4018 retval = false;
4021 if (tmp_sym->attr.allocatable == 1)
4023 gfc_error ("Variable '%s' at %L cannot have both the "
4024 "ALLOCATABLE and BIND(C) attributes",
4025 tmp_sym->name, &(tmp_sym->declared_at));
4026 retval = false;
4031 /* If it is a BIND(C) function, make sure the return value is a
4032 scalar value. The previous tests in this function made sure
4033 the type is interoperable. */
4034 if (bind_c_function && tmp_sym->as != NULL)
4035 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4036 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
4038 /* BIND(C) functions can not return a character string. */
4039 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
4040 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
4041 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
4042 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4043 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4044 "be a character string", tmp_sym->name,
4045 &(tmp_sym->declared_at));
4048 /* See if the symbol has been marked as private. If it has, make sure
4049 there is no binding label and warn the user if there is one. */
4050 if (tmp_sym->attr.access == ACCESS_PRIVATE
4051 && tmp_sym->binding_label)
4052 /* Use gfc_warning_now because we won't say that the symbol fails
4053 just because of this. */
4054 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
4055 "given the binding label '%s'", tmp_sym->name,
4056 &(tmp_sym->declared_at), tmp_sym->binding_label);
4058 return retval;
4062 /* Set the appropriate fields for a symbol that's been declared as
4063 BIND(C) (the is_bind_c flag and the binding label), and verify that
4064 the type is C interoperable. Errors are reported by the functions
4065 used to set/test these fields. */
4067 bool
4068 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4070 bool retval = true;
4072 /* TODO: Do we need to make sure the vars aren't marked private? */
4074 /* Set the is_bind_c bit in symbol_attribute. */
4075 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4077 if (!set_binding_label (&tmp_sym->binding_label, tmp_sym->name, num_idents))
4078 return false;
4080 return retval;
4084 /* Set the fields marking the given common block as BIND(C), including
4085 a binding label, and report any errors encountered. */
4087 bool
4088 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4090 bool retval = true;
4092 /* destLabel, common name, typespec (which may have binding label). */
4093 if (!set_binding_label (&com_block->binding_label, com_block->name,
4094 num_idents))
4095 return false;
4097 /* Set the given common block (com_block) to being bind(c) (1). */
4098 set_com_block_bind_c (com_block, 1);
4100 return retval;
4104 /* Retrieve the list of one or more identifiers that the given bind(c)
4105 attribute applies to. */
4107 bool
4108 get_bind_c_idents (void)
4110 char name[GFC_MAX_SYMBOL_LEN + 1];
4111 int num_idents = 0;
4112 gfc_symbol *tmp_sym = NULL;
4113 match found_id;
4114 gfc_common_head *com_block = NULL;
4116 if (gfc_match_name (name) == MATCH_YES)
4118 found_id = MATCH_YES;
4119 gfc_get_ha_symbol (name, &tmp_sym);
4121 else if (match_common_name (name) == MATCH_YES)
4123 found_id = MATCH_YES;
4124 com_block = gfc_get_common (name, 0);
4126 else
4128 gfc_error ("Need either entity or common block name for "
4129 "attribute specification statement at %C");
4130 return false;
4133 /* Save the current identifier and look for more. */
4136 /* Increment the number of identifiers found for this spec stmt. */
4137 num_idents++;
4139 /* Make sure we have a sym or com block, and verify that it can
4140 be bind(c). Set the appropriate field(s) and look for more
4141 identifiers. */
4142 if (tmp_sym != NULL || com_block != NULL)
4144 if (tmp_sym != NULL)
4146 if (!set_verify_bind_c_sym (tmp_sym, num_idents))
4147 return false;
4149 else
4151 if (!set_verify_bind_c_com_block (com_block, num_idents))
4152 return false;
4155 /* Look to see if we have another identifier. */
4156 tmp_sym = NULL;
4157 if (gfc_match_eos () == MATCH_YES)
4158 found_id = MATCH_NO;
4159 else if (gfc_match_char (',') != MATCH_YES)
4160 found_id = MATCH_NO;
4161 else if (gfc_match_name (name) == MATCH_YES)
4163 found_id = MATCH_YES;
4164 gfc_get_ha_symbol (name, &tmp_sym);
4166 else if (match_common_name (name) == MATCH_YES)
4168 found_id = MATCH_YES;
4169 com_block = gfc_get_common (name, 0);
4171 else
4173 gfc_error ("Missing entity or common block name for "
4174 "attribute specification statement at %C");
4175 return false;
4178 else
4180 gfc_internal_error ("Missing symbol");
4182 } while (found_id == MATCH_YES);
4184 /* if we get here we were successful */
4185 return true;
4189 /* Try and match a BIND(C) attribute specification statement. */
4191 match
4192 gfc_match_bind_c_stmt (void)
4194 match found_match = MATCH_NO;
4195 gfc_typespec *ts;
4197 ts = &current_ts;
4199 /* This may not be necessary. */
4200 gfc_clear_ts (ts);
4201 /* Clear the temporary binding label holder. */
4202 curr_binding_label = NULL;
4204 /* Look for the bind(c). */
4205 found_match = gfc_match_bind_c (NULL, true);
4207 if (found_match == MATCH_YES)
4209 if (!gfc_notify_std (GFC_STD_F2003, "BIND(C) statement at %C"))
4210 return MATCH_ERROR;
4212 /* Look for the :: now, but it is not required. */
4213 gfc_match (" :: ");
4215 /* Get the identifier(s) that needs to be updated. This may need to
4216 change to hand the flag(s) for the attr specified so all identifiers
4217 found can have all appropriate parts updated (assuming that the same
4218 spec stmt can have multiple attrs, such as both bind(c) and
4219 allocatable...). */
4220 if (!get_bind_c_idents ())
4221 /* Error message should have printed already. */
4222 return MATCH_ERROR;
4225 return found_match;
4229 /* Match a data declaration statement. */
4231 match
4232 gfc_match_data_decl (void)
4234 gfc_symbol *sym;
4235 match m;
4236 int elem;
4238 num_idents_on_line = 0;
4240 m = gfc_match_decl_type_spec (&current_ts, 0);
4241 if (m != MATCH_YES)
4242 return m;
4244 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4245 && gfc_current_state () != COMP_DERIVED)
4247 sym = gfc_use_derived (current_ts.u.derived);
4249 if (sym == NULL)
4251 m = MATCH_ERROR;
4252 goto cleanup;
4255 current_ts.u.derived = sym;
4258 m = match_attr_spec ();
4259 if (m == MATCH_ERROR)
4261 m = MATCH_NO;
4262 goto cleanup;
4265 if (current_ts.type == BT_CLASS
4266 && current_ts.u.derived->attr.unlimited_polymorphic)
4267 goto ok;
4269 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4270 && current_ts.u.derived->components == NULL
4271 && !current_ts.u.derived->attr.zero_comp)
4274 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4275 goto ok;
4277 gfc_find_symbol (current_ts.u.derived->name,
4278 current_ts.u.derived->ns, 1, &sym);
4280 /* Any symbol that we find had better be a type definition
4281 which has its components defined. */
4282 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4283 && (current_ts.u.derived->components != NULL
4284 || current_ts.u.derived->attr.zero_comp))
4285 goto ok;
4287 gfc_error ("Derived type at %C has not been previously defined "
4288 "and so cannot appear in a derived type definition");
4289 m = MATCH_ERROR;
4290 goto cleanup;
4294 /* If we have an old-style character declaration, and no new-style
4295 attribute specifications, then there a comma is optional between
4296 the type specification and the variable list. */
4297 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4298 gfc_match_char (',');
4300 /* Give the types/attributes to symbols that follow. Give the element
4301 a number so that repeat character length expressions can be copied. */
4302 elem = 1;
4303 for (;;)
4305 num_idents_on_line++;
4306 m = variable_decl (elem++);
4307 if (m == MATCH_ERROR)
4308 goto cleanup;
4309 if (m == MATCH_NO)
4310 break;
4312 if (gfc_match_eos () == MATCH_YES)
4313 goto cleanup;
4314 if (gfc_match_char (',') != MATCH_YES)
4315 break;
4318 if (gfc_error_flag_test () == 0)
4319 gfc_error ("Syntax error in data declaration at %C");
4320 m = MATCH_ERROR;
4322 gfc_free_data_all (gfc_current_ns);
4324 cleanup:
4325 gfc_free_array_spec (current_as);
4326 current_as = NULL;
4327 return m;
4331 /* Match a prefix associated with a function or subroutine
4332 declaration. If the typespec pointer is nonnull, then a typespec
4333 can be matched. Note that if nothing matches, MATCH_YES is
4334 returned (the null string was matched). */
4336 match
4337 gfc_match_prefix (gfc_typespec *ts)
4339 bool seen_type;
4340 bool seen_impure;
4341 bool found_prefix;
4343 gfc_clear_attr (&current_attr);
4344 seen_type = false;
4345 seen_impure = false;
4347 gcc_assert (!gfc_matching_prefix);
4348 gfc_matching_prefix = true;
4352 found_prefix = false;
4354 if (!seen_type && ts != NULL
4355 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4356 && gfc_match_space () == MATCH_YES)
4359 seen_type = true;
4360 found_prefix = true;
4363 if (gfc_match ("elemental% ") == MATCH_YES)
4365 if (!gfc_add_elemental (&current_attr, NULL))
4366 goto error;
4368 found_prefix = true;
4371 if (gfc_match ("pure% ") == MATCH_YES)
4373 if (!gfc_add_pure (&current_attr, NULL))
4374 goto error;
4376 found_prefix = true;
4379 if (gfc_match ("recursive% ") == MATCH_YES)
4381 if (!gfc_add_recursive (&current_attr, NULL))
4382 goto error;
4384 found_prefix = true;
4387 /* IMPURE is a somewhat special case, as it needs not set an actual
4388 attribute but rather only prevents ELEMENTAL routines from being
4389 automatically PURE. */
4390 if (gfc_match ("impure% ") == MATCH_YES)
4392 if (!gfc_notify_std (GFC_STD_F2008, "IMPURE procedure at %C"))
4393 goto error;
4395 seen_impure = true;
4396 found_prefix = true;
4399 while (found_prefix);
4401 /* IMPURE and PURE must not both appear, of course. */
4402 if (seen_impure && current_attr.pure)
4404 gfc_error ("PURE and IMPURE must not appear both at %C");
4405 goto error;
4408 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4409 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4411 if (!gfc_add_pure (&current_attr, NULL))
4412 goto error;
4415 /* At this point, the next item is not a prefix. */
4416 gcc_assert (gfc_matching_prefix);
4417 gfc_matching_prefix = false;
4418 return MATCH_YES;
4420 error:
4421 gcc_assert (gfc_matching_prefix);
4422 gfc_matching_prefix = false;
4423 return MATCH_ERROR;
4427 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4429 static bool
4430 copy_prefix (symbol_attribute *dest, locus *where)
4432 if (current_attr.pure && !gfc_add_pure (dest, where))
4433 return false;
4435 if (current_attr.elemental && !gfc_add_elemental (dest, where))
4436 return false;
4438 if (current_attr.recursive && !gfc_add_recursive (dest, where))
4439 return false;
4441 return true;
4445 /* Match a formal argument list. */
4447 match
4448 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4450 gfc_formal_arglist *head, *tail, *p, *q;
4451 char name[GFC_MAX_SYMBOL_LEN + 1];
4452 gfc_symbol *sym;
4453 match m;
4455 head = tail = NULL;
4457 if (gfc_match_char ('(') != MATCH_YES)
4459 if (null_flag)
4460 goto ok;
4461 return MATCH_NO;
4464 if (gfc_match_char (')') == MATCH_YES)
4465 goto ok;
4467 for (;;)
4469 if (gfc_match_char ('*') == MATCH_YES)
4471 sym = NULL;
4472 if (!gfc_notify_std (GFC_STD_F95_OBS, "Alternate-return argument "
4473 "at %C"))
4475 m = MATCH_ERROR;
4476 goto cleanup;
4479 else
4481 m = gfc_match_name (name);
4482 if (m != MATCH_YES)
4483 goto cleanup;
4485 if (gfc_get_symbol (name, NULL, &sym))
4486 goto cleanup;
4489 p = gfc_get_formal_arglist ();
4491 if (head == NULL)
4492 head = tail = p;
4493 else
4495 tail->next = p;
4496 tail = p;
4499 tail->sym = sym;
4501 /* We don't add the VARIABLE flavor because the name could be a
4502 dummy procedure. We don't apply these attributes to formal
4503 arguments of statement functions. */
4504 if (sym != NULL && !st_flag
4505 && (!gfc_add_dummy(&sym->attr, sym->name, NULL)
4506 || !gfc_missing_attr (&sym->attr, NULL)))
4508 m = MATCH_ERROR;
4509 goto cleanup;
4512 /* The name of a program unit can be in a different namespace,
4513 so check for it explicitly. After the statement is accepted,
4514 the name is checked for especially in gfc_get_symbol(). */
4515 if (gfc_new_block != NULL && sym != NULL
4516 && strcmp (sym->name, gfc_new_block->name) == 0)
4518 gfc_error ("Name '%s' at %C is the name of the procedure",
4519 sym->name);
4520 m = MATCH_ERROR;
4521 goto cleanup;
4524 if (gfc_match_char (')') == MATCH_YES)
4525 goto ok;
4527 m = gfc_match_char (',');
4528 if (m != MATCH_YES)
4530 gfc_error ("Unexpected junk in formal argument list at %C");
4531 goto cleanup;
4536 /* Check for duplicate symbols in the formal argument list. */
4537 if (head != NULL)
4539 for (p = head; p->next; p = p->next)
4541 if (p->sym == NULL)
4542 continue;
4544 for (q = p->next; q; q = q->next)
4545 if (p->sym == q->sym)
4547 gfc_error ("Duplicate symbol '%s' in formal argument list "
4548 "at %C", p->sym->name);
4550 m = MATCH_ERROR;
4551 goto cleanup;
4556 if (!gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL))
4558 m = MATCH_ERROR;
4559 goto cleanup;
4562 return MATCH_YES;
4564 cleanup:
4565 gfc_free_formal_arglist (head);
4566 return m;
4570 /* Match a RESULT specification following a function declaration or
4571 ENTRY statement. Also matches the end-of-statement. */
4573 static match
4574 match_result (gfc_symbol *function, gfc_symbol **result)
4576 char name[GFC_MAX_SYMBOL_LEN + 1];
4577 gfc_symbol *r;
4578 match m;
4580 if (gfc_match (" result (") != MATCH_YES)
4581 return MATCH_NO;
4583 m = gfc_match_name (name);
4584 if (m != MATCH_YES)
4585 return m;
4587 /* Get the right paren, and that's it because there could be the
4588 bind(c) attribute after the result clause. */
4589 if (gfc_match_char (')') != MATCH_YES)
4591 /* TODO: should report the missing right paren here. */
4592 return MATCH_ERROR;
4595 if (strcmp (function->name, name) == 0)
4597 gfc_error ("RESULT variable at %C must be different than function name");
4598 return MATCH_ERROR;
4601 if (gfc_get_symbol (name, NULL, &r))
4602 return MATCH_ERROR;
4604 if (!gfc_add_result (&r->attr, r->name, NULL))
4605 return MATCH_ERROR;
4607 *result = r;
4609 return MATCH_YES;
4613 /* Match a function suffix, which could be a combination of a result
4614 clause and BIND(C), either one, or neither. The draft does not
4615 require them to come in a specific order. */
4617 match
4618 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4620 match is_bind_c; /* Found bind(c). */
4621 match is_result; /* Found result clause. */
4622 match found_match; /* Status of whether we've found a good match. */
4623 char peek_char; /* Character we're going to peek at. */
4624 bool allow_binding_name;
4626 /* Initialize to having found nothing. */
4627 found_match = MATCH_NO;
4628 is_bind_c = MATCH_NO;
4629 is_result = MATCH_NO;
4631 /* Get the next char to narrow between result and bind(c). */
4632 gfc_gobble_whitespace ();
4633 peek_char = gfc_peek_ascii_char ();
4635 /* C binding names are not allowed for internal procedures. */
4636 if (gfc_current_state () == COMP_CONTAINS
4637 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4638 allow_binding_name = false;
4639 else
4640 allow_binding_name = true;
4642 switch (peek_char)
4644 case 'r':
4645 /* Look for result clause. */
4646 is_result = match_result (sym, result);
4647 if (is_result == MATCH_YES)
4649 /* Now see if there is a bind(c) after it. */
4650 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4651 /* We've found the result clause and possibly bind(c). */
4652 found_match = MATCH_YES;
4654 else
4655 /* This should only be MATCH_ERROR. */
4656 found_match = is_result;
4657 break;
4658 case 'b':
4659 /* Look for bind(c) first. */
4660 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4661 if (is_bind_c == MATCH_YES)
4663 /* Now see if a result clause followed it. */
4664 is_result = match_result (sym, result);
4665 found_match = MATCH_YES;
4667 else
4669 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4670 found_match = MATCH_ERROR;
4672 break;
4673 default:
4674 gfc_error ("Unexpected junk after function declaration at %C");
4675 found_match = MATCH_ERROR;
4676 break;
4679 if (is_bind_c == MATCH_YES)
4681 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4682 if (gfc_current_state () == COMP_CONTAINS
4683 && sym->ns->proc_name->attr.flavor != FL_MODULE
4684 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
4685 "at %L may not be specified for an internal "
4686 "procedure", &gfc_current_locus))
4687 return MATCH_ERROR;
4689 if (!gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1))
4690 return MATCH_ERROR;
4693 return found_match;
4697 /* Procedure pointer return value without RESULT statement:
4698 Add "hidden" result variable named "ppr@". */
4700 static bool
4701 add_hidden_procptr_result (gfc_symbol *sym)
4703 bool case1,case2;
4705 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4706 return false;
4708 /* First usage case: PROCEDURE and EXTERNAL statements. */
4709 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4710 && strcmp (gfc_current_block ()->name, sym->name) == 0
4711 && sym->attr.external;
4712 /* Second usage case: INTERFACE statements. */
4713 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4714 && gfc_state_stack->previous->state == COMP_FUNCTION
4715 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4717 if (case1 || case2)
4719 gfc_symtree *stree;
4720 if (case1)
4721 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4722 else if (case2)
4724 gfc_symtree *st2;
4725 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4726 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4727 st2->n.sym = stree->n.sym;
4729 sym->result = stree->n.sym;
4731 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4732 sym->result->attr.pointer = sym->attr.pointer;
4733 sym->result->attr.external = sym->attr.external;
4734 sym->result->attr.referenced = sym->attr.referenced;
4735 sym->result->ts = sym->ts;
4736 sym->attr.proc_pointer = 0;
4737 sym->attr.pointer = 0;
4738 sym->attr.external = 0;
4739 if (sym->result->attr.external && sym->result->attr.pointer)
4741 sym->result->attr.pointer = 0;
4742 sym->result->attr.proc_pointer = 1;
4745 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4747 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4748 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4749 && sym->result && sym->result != sym && sym->result->attr.external
4750 && sym == gfc_current_ns->proc_name
4751 && sym == sym->result->ns->proc_name
4752 && strcmp ("ppr@", sym->result->name) == 0)
4754 sym->result->attr.proc_pointer = 1;
4755 sym->attr.pointer = 0;
4756 return true;
4758 else
4759 return false;
4763 /* Match the interface for a PROCEDURE declaration,
4764 including brackets (R1212). */
4766 static match
4767 match_procedure_interface (gfc_symbol **proc_if)
4769 match m;
4770 gfc_symtree *st;
4771 locus old_loc, entry_loc;
4772 gfc_namespace *old_ns = gfc_current_ns;
4773 char name[GFC_MAX_SYMBOL_LEN + 1];
4775 old_loc = entry_loc = gfc_current_locus;
4776 gfc_clear_ts (&current_ts);
4778 if (gfc_match (" (") != MATCH_YES)
4780 gfc_current_locus = entry_loc;
4781 return MATCH_NO;
4784 /* Get the type spec. for the procedure interface. */
4785 old_loc = gfc_current_locus;
4786 m = gfc_match_decl_type_spec (&current_ts, 0);
4787 gfc_gobble_whitespace ();
4788 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4789 goto got_ts;
4791 if (m == MATCH_ERROR)
4792 return m;
4794 /* Procedure interface is itself a procedure. */
4795 gfc_current_locus = old_loc;
4796 m = gfc_match_name (name);
4798 /* First look to see if it is already accessible in the current
4799 namespace because it is use associated or contained. */
4800 st = NULL;
4801 if (gfc_find_sym_tree (name, NULL, 0, &st))
4802 return MATCH_ERROR;
4804 /* If it is still not found, then try the parent namespace, if it
4805 exists and create the symbol there if it is still not found. */
4806 if (gfc_current_ns->parent)
4807 gfc_current_ns = gfc_current_ns->parent;
4808 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4809 return MATCH_ERROR;
4811 gfc_current_ns = old_ns;
4812 *proc_if = st->n.sym;
4814 if (*proc_if)
4816 (*proc_if)->refs++;
4817 /* Resolve interface if possible. That way, attr.procedure is only set
4818 if it is declared by a later procedure-declaration-stmt, which is
4819 invalid per F08:C1216 (cf. resolve_procedure_interface). */
4820 while ((*proc_if)->ts.interface)
4821 *proc_if = (*proc_if)->ts.interface;
4823 if ((*proc_if)->attr.flavor == FL_UNKNOWN
4824 && (*proc_if)->ts.type == BT_UNKNOWN
4825 && !gfc_add_flavor (&(*proc_if)->attr, FL_PROCEDURE,
4826 (*proc_if)->name, NULL))
4827 return MATCH_ERROR;
4830 got_ts:
4831 if (gfc_match (" )") != MATCH_YES)
4833 gfc_current_locus = entry_loc;
4834 return MATCH_NO;
4837 return MATCH_YES;
4841 /* Match a PROCEDURE declaration (R1211). */
4843 static match
4844 match_procedure_decl (void)
4846 match m;
4847 gfc_symbol *sym, *proc_if = NULL;
4848 int num;
4849 gfc_expr *initializer = NULL;
4851 /* Parse interface (with brackets). */
4852 m = match_procedure_interface (&proc_if);
4853 if (m != MATCH_YES)
4854 return m;
4856 /* Parse attributes (with colons). */
4857 m = match_attr_spec();
4858 if (m == MATCH_ERROR)
4859 return MATCH_ERROR;
4861 if (proc_if && proc_if->attr.is_bind_c && !current_attr.is_bind_c)
4863 current_attr.is_bind_c = 1;
4864 has_name_equals = 0;
4865 curr_binding_label = NULL;
4868 /* Get procedure symbols. */
4869 for(num=1;;num++)
4871 m = gfc_match_symbol (&sym, 0);
4872 if (m == MATCH_NO)
4873 goto syntax;
4874 else if (m == MATCH_ERROR)
4875 return m;
4877 /* Add current_attr to the symbol attributes. */
4878 if (!gfc_copy_attr (&sym->attr, &current_attr, NULL))
4879 return MATCH_ERROR;
4881 if (sym->attr.is_bind_c)
4883 /* Check for C1218. */
4884 if (!proc_if || !proc_if->attr.is_bind_c)
4886 gfc_error ("BIND(C) attribute at %C requires "
4887 "an interface with BIND(C)");
4888 return MATCH_ERROR;
4890 /* Check for C1217. */
4891 if (has_name_equals && sym->attr.pointer)
4893 gfc_error ("BIND(C) procedure with NAME may not have "
4894 "POINTER attribute at %C");
4895 return MATCH_ERROR;
4897 if (has_name_equals && sym->attr.dummy)
4899 gfc_error ("Dummy procedure at %C may not have "
4900 "BIND(C) attribute with NAME");
4901 return MATCH_ERROR;
4903 /* Set binding label for BIND(C). */
4904 if (!set_binding_label (&sym->binding_label, sym->name, num))
4905 return MATCH_ERROR;
4908 if (!gfc_add_external (&sym->attr, NULL))
4909 return MATCH_ERROR;
4911 if (add_hidden_procptr_result (sym))
4912 sym = sym->result;
4914 if (!gfc_add_proc (&sym->attr, sym->name, NULL))
4915 return MATCH_ERROR;
4917 /* Set interface. */
4918 if (proc_if != NULL)
4920 if (sym->ts.type != BT_UNKNOWN)
4922 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4923 sym->name, &gfc_current_locus,
4924 gfc_basic_typename (sym->ts.type));
4925 return MATCH_ERROR;
4927 sym->ts.interface = proc_if;
4928 sym->attr.untyped = 1;
4929 sym->attr.if_source = IFSRC_IFBODY;
4931 else if (current_ts.type != BT_UNKNOWN)
4933 if (!gfc_add_type (sym, &current_ts, &gfc_current_locus))
4934 return MATCH_ERROR;
4935 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4936 sym->ts.interface->ts = current_ts;
4937 sym->ts.interface->attr.flavor = FL_PROCEDURE;
4938 sym->ts.interface->attr.function = 1;
4939 sym->attr.function = 1;
4940 sym->attr.if_source = IFSRC_UNKNOWN;
4943 if (gfc_match (" =>") == MATCH_YES)
4945 if (!current_attr.pointer)
4947 gfc_error ("Initialization at %C isn't for a pointer variable");
4948 m = MATCH_ERROR;
4949 goto cleanup;
4952 m = match_pointer_init (&initializer, 1);
4953 if (m != MATCH_YES)
4954 goto cleanup;
4956 if (!add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus))
4957 goto cleanup;
4961 if (gfc_match_eos () == MATCH_YES)
4962 return MATCH_YES;
4963 if (gfc_match_char (',') != MATCH_YES)
4964 goto syntax;
4967 syntax:
4968 gfc_error ("Syntax error in PROCEDURE statement at %C");
4969 return MATCH_ERROR;
4971 cleanup:
4972 /* Free stuff up and return. */
4973 gfc_free_expr (initializer);
4974 return m;
4978 static match
4979 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4982 /* Match a procedure pointer component declaration (R445). */
4984 static match
4985 match_ppc_decl (void)
4987 match m;
4988 gfc_symbol *proc_if = NULL;
4989 gfc_typespec ts;
4990 int num;
4991 gfc_component *c;
4992 gfc_expr *initializer = NULL;
4993 gfc_typebound_proc* tb;
4994 char name[GFC_MAX_SYMBOL_LEN + 1];
4996 /* Parse interface (with brackets). */
4997 m = match_procedure_interface (&proc_if);
4998 if (m != MATCH_YES)
4999 goto syntax;
5001 /* Parse attributes. */
5002 tb = XCNEW (gfc_typebound_proc);
5003 tb->where = gfc_current_locus;
5004 m = match_binding_attributes (tb, false, true);
5005 if (m == MATCH_ERROR)
5006 return m;
5008 gfc_clear_attr (&current_attr);
5009 current_attr.procedure = 1;
5010 current_attr.proc_pointer = 1;
5011 current_attr.access = tb->access;
5012 current_attr.flavor = FL_PROCEDURE;
5014 /* Match the colons (required). */
5015 if (gfc_match (" ::") != MATCH_YES)
5017 gfc_error ("Expected '::' after binding-attributes at %C");
5018 return MATCH_ERROR;
5021 /* Check for C450. */
5022 if (!tb->nopass && proc_if == NULL)
5024 gfc_error("NOPASS or explicit interface required at %C");
5025 return MATCH_ERROR;
5028 if (!gfc_notify_std (GFC_STD_F2003, "Procedure pointer component at %C"))
5029 return MATCH_ERROR;
5031 /* Match PPC names. */
5032 ts = current_ts;
5033 for(num=1;;num++)
5035 m = gfc_match_name (name);
5036 if (m == MATCH_NO)
5037 goto syntax;
5038 else if (m == MATCH_ERROR)
5039 return m;
5041 if (!gfc_add_component (gfc_current_block(), name, &c))
5042 return MATCH_ERROR;
5044 /* Add current_attr to the symbol attributes. */
5045 if (!gfc_copy_attr (&c->attr, &current_attr, NULL))
5046 return MATCH_ERROR;
5048 if (!gfc_add_external (&c->attr, NULL))
5049 return MATCH_ERROR;
5051 if (!gfc_add_proc (&c->attr, name, NULL))
5052 return MATCH_ERROR;
5054 if (num == 1)
5055 c->tb = tb;
5056 else
5058 c->tb = XCNEW (gfc_typebound_proc);
5059 c->tb->where = gfc_current_locus;
5060 *c->tb = *tb;
5063 /* Set interface. */
5064 if (proc_if != NULL)
5066 c->ts.interface = proc_if;
5067 c->attr.untyped = 1;
5068 c->attr.if_source = IFSRC_IFBODY;
5070 else if (ts.type != BT_UNKNOWN)
5072 c->ts = ts;
5073 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5074 c->ts.interface->result = c->ts.interface;
5075 c->ts.interface->ts = ts;
5076 c->ts.interface->attr.flavor = FL_PROCEDURE;
5077 c->ts.interface->attr.function = 1;
5078 c->attr.function = 1;
5079 c->attr.if_source = IFSRC_UNKNOWN;
5082 if (gfc_match (" =>") == MATCH_YES)
5084 m = match_pointer_init (&initializer, 1);
5085 if (m != MATCH_YES)
5087 gfc_free_expr (initializer);
5088 return m;
5090 c->initializer = initializer;
5093 if (gfc_match_eos () == MATCH_YES)
5094 return MATCH_YES;
5095 if (gfc_match_char (',') != MATCH_YES)
5096 goto syntax;
5099 syntax:
5100 gfc_error ("Syntax error in procedure pointer component at %C");
5101 return MATCH_ERROR;
5105 /* Match a PROCEDURE declaration inside an interface (R1206). */
5107 static match
5108 match_procedure_in_interface (void)
5110 match m;
5111 gfc_symbol *sym;
5112 char name[GFC_MAX_SYMBOL_LEN + 1];
5113 locus old_locus;
5115 if (current_interface.type == INTERFACE_NAMELESS
5116 || current_interface.type == INTERFACE_ABSTRACT)
5118 gfc_error ("PROCEDURE at %C must be in a generic interface");
5119 return MATCH_ERROR;
5122 /* Check if the F2008 optional double colon appears. */
5123 gfc_gobble_whitespace ();
5124 old_locus = gfc_current_locus;
5125 if (gfc_match ("::") == MATCH_YES)
5127 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
5128 "MODULE PROCEDURE statement at %L", &old_locus))
5129 return MATCH_ERROR;
5131 else
5132 gfc_current_locus = old_locus;
5134 for(;;)
5136 m = gfc_match_name (name);
5137 if (m == MATCH_NO)
5138 goto syntax;
5139 else if (m == MATCH_ERROR)
5140 return m;
5141 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5142 return MATCH_ERROR;
5144 if (!gfc_add_interface (sym))
5145 return MATCH_ERROR;
5147 if (gfc_match_eos () == MATCH_YES)
5148 break;
5149 if (gfc_match_char (',') != MATCH_YES)
5150 goto syntax;
5153 return MATCH_YES;
5155 syntax:
5156 gfc_error ("Syntax error in PROCEDURE statement at %C");
5157 return MATCH_ERROR;
5161 /* General matcher for PROCEDURE declarations. */
5163 static match match_procedure_in_type (void);
5165 match
5166 gfc_match_procedure (void)
5168 match m;
5170 switch (gfc_current_state ())
5172 case COMP_NONE:
5173 case COMP_PROGRAM:
5174 case COMP_MODULE:
5175 case COMP_SUBROUTINE:
5176 case COMP_FUNCTION:
5177 case COMP_BLOCK:
5178 m = match_procedure_decl ();
5179 break;
5180 case COMP_INTERFACE:
5181 m = match_procedure_in_interface ();
5182 break;
5183 case COMP_DERIVED:
5184 m = match_ppc_decl ();
5185 break;
5186 case COMP_DERIVED_CONTAINS:
5187 m = match_procedure_in_type ();
5188 break;
5189 default:
5190 return MATCH_NO;
5193 if (m != MATCH_YES)
5194 return m;
5196 if (!gfc_notify_std (GFC_STD_F2003, "PROCEDURE statement at %C"))
5197 return MATCH_ERROR;
5199 return m;
5203 /* Warn if a matched procedure has the same name as an intrinsic; this is
5204 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5205 parser-state-stack to find out whether we're in a module. */
5207 static void
5208 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5210 bool in_module;
5212 in_module = (gfc_state_stack->previous
5213 && gfc_state_stack->previous->state == COMP_MODULE);
5215 gfc_warn_intrinsic_shadow (sym, in_module, func);
5219 /* Match a function declaration. */
5221 match
5222 gfc_match_function_decl (void)
5224 char name[GFC_MAX_SYMBOL_LEN + 1];
5225 gfc_symbol *sym, *result;
5226 locus old_loc;
5227 match m;
5228 match suffix_match;
5229 match found_match; /* Status returned by match func. */
5231 if (gfc_current_state () != COMP_NONE
5232 && gfc_current_state () != COMP_INTERFACE
5233 && gfc_current_state () != COMP_CONTAINS)
5234 return MATCH_NO;
5236 gfc_clear_ts (&current_ts);
5238 old_loc = gfc_current_locus;
5240 m = gfc_match_prefix (&current_ts);
5241 if (m != MATCH_YES)
5243 gfc_current_locus = old_loc;
5244 return m;
5247 if (gfc_match ("function% %n", name) != MATCH_YES)
5249 gfc_current_locus = old_loc;
5250 return MATCH_NO;
5252 if (get_proc_name (name, &sym, false))
5253 return MATCH_ERROR;
5255 if (add_hidden_procptr_result (sym))
5256 sym = sym->result;
5258 gfc_new_block = sym;
5260 m = gfc_match_formal_arglist (sym, 0, 0);
5261 if (m == MATCH_NO)
5263 gfc_error ("Expected formal argument list in function "
5264 "definition at %C");
5265 m = MATCH_ERROR;
5266 goto cleanup;
5268 else if (m == MATCH_ERROR)
5269 goto cleanup;
5271 result = NULL;
5273 /* According to the draft, the bind(c) and result clause can
5274 come in either order after the formal_arg_list (i.e., either
5275 can be first, both can exist together or by themselves or neither
5276 one). Therefore, the match_result can't match the end of the
5277 string, and check for the bind(c) or result clause in either order. */
5278 found_match = gfc_match_eos ();
5280 /* Make sure that it isn't already declared as BIND(C). If it is, it
5281 must have been marked BIND(C) with a BIND(C) attribute and that is
5282 not allowed for procedures. */
5283 if (sym->attr.is_bind_c == 1)
5285 sym->attr.is_bind_c = 0;
5286 if (sym->old_symbol != NULL)
5287 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5288 "variables or common blocks",
5289 &(sym->old_symbol->declared_at));
5290 else
5291 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5292 "variables or common blocks", &gfc_current_locus);
5295 if (found_match != MATCH_YES)
5297 /* If we haven't found the end-of-statement, look for a suffix. */
5298 suffix_match = gfc_match_suffix (sym, &result);
5299 if (suffix_match == MATCH_YES)
5300 /* Need to get the eos now. */
5301 found_match = gfc_match_eos ();
5302 else
5303 found_match = suffix_match;
5306 if(found_match != MATCH_YES)
5307 m = MATCH_ERROR;
5308 else
5310 /* Make changes to the symbol. */
5311 m = MATCH_ERROR;
5313 if (!gfc_add_function (&sym->attr, sym->name, NULL))
5314 goto cleanup;
5316 if (!gfc_missing_attr (&sym->attr, NULL)
5317 || !copy_prefix (&sym->attr, &sym->declared_at))
5318 goto cleanup;
5320 /* Delay matching the function characteristics until after the
5321 specification block by signalling kind=-1. */
5322 sym->declared_at = old_loc;
5323 if (current_ts.type != BT_UNKNOWN)
5324 current_ts.kind = -1;
5325 else
5326 current_ts.kind = 0;
5328 if (result == NULL)
5330 if (current_ts.type != BT_UNKNOWN
5331 && !gfc_add_type (sym, &current_ts, &gfc_current_locus))
5332 goto cleanup;
5333 sym->result = sym;
5335 else
5337 if (current_ts.type != BT_UNKNOWN
5338 && !gfc_add_type (result, &current_ts, &gfc_current_locus))
5339 goto cleanup;
5340 sym->result = result;
5343 /* Warn if this procedure has the same name as an intrinsic. */
5344 warn_intrinsic_shadow (sym, true);
5346 return MATCH_YES;
5349 cleanup:
5350 gfc_current_locus = old_loc;
5351 return m;
5355 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5356 pass the name of the entry, rather than the gfc_current_block name, and
5357 to return false upon finding an existing global entry. */
5359 static bool
5360 add_global_entry (const char *name, const char *binding_label, bool sub,
5361 locus *where)
5363 gfc_gsymbol *s;
5364 enum gfc_symbol_type type;
5366 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5368 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5369 name is a global identifier. */
5370 if (!binding_label || gfc_notification_std (GFC_STD_F2008))
5372 s = gfc_get_gsymbol (name);
5374 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5376 gfc_global_used (s, where);
5377 return false;
5379 else
5381 s->type = type;
5382 s->sym_name = name;
5383 s->where = *where;
5384 s->defined = 1;
5385 s->ns = gfc_current_ns;
5389 /* Don't add the symbol multiple times. */
5390 if (binding_label
5391 && (!gfc_notification_std (GFC_STD_F2008)
5392 || strcmp (name, binding_label) != 0))
5394 s = gfc_get_gsymbol (binding_label);
5396 if (s->defined || (s->type != GSYM_UNKNOWN && s->type != type))
5398 gfc_global_used (s, where);
5399 return false;
5401 else
5403 s->type = type;
5404 s->sym_name = name;
5405 s->binding_label = binding_label;
5406 s->where = *where;
5407 s->defined = 1;
5408 s->ns = gfc_current_ns;
5412 return true;
5416 /* Match an ENTRY statement. */
5418 match
5419 gfc_match_entry (void)
5421 gfc_symbol *proc;
5422 gfc_symbol *result;
5423 gfc_symbol *entry;
5424 char name[GFC_MAX_SYMBOL_LEN + 1];
5425 gfc_compile_state state;
5426 match m;
5427 gfc_entry_list *el;
5428 locus old_loc;
5429 bool module_procedure;
5430 char peek_char;
5431 match is_bind_c;
5433 m = gfc_match_name (name);
5434 if (m != MATCH_YES)
5435 return m;
5437 if (!gfc_notify_std (GFC_STD_F2008_OBS, "ENTRY statement at %C"))
5438 return MATCH_ERROR;
5440 state = gfc_current_state ();
5441 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5443 switch (state)
5445 case COMP_PROGRAM:
5446 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5447 break;
5448 case COMP_MODULE:
5449 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5450 break;
5451 case COMP_BLOCK_DATA:
5452 gfc_error ("ENTRY statement at %C cannot appear within "
5453 "a BLOCK DATA");
5454 break;
5455 case COMP_INTERFACE:
5456 gfc_error ("ENTRY statement at %C cannot appear within "
5457 "an INTERFACE");
5458 break;
5459 case COMP_DERIVED:
5460 gfc_error ("ENTRY statement at %C cannot appear within "
5461 "a DERIVED TYPE block");
5462 break;
5463 case COMP_IF:
5464 gfc_error ("ENTRY statement at %C cannot appear within "
5465 "an IF-THEN block");
5466 break;
5467 case COMP_DO:
5468 case COMP_DO_CONCURRENT:
5469 gfc_error ("ENTRY statement at %C cannot appear within "
5470 "a DO block");
5471 break;
5472 case COMP_SELECT:
5473 gfc_error ("ENTRY statement at %C cannot appear within "
5474 "a SELECT block");
5475 break;
5476 case COMP_FORALL:
5477 gfc_error ("ENTRY statement at %C cannot appear within "
5478 "a FORALL block");
5479 break;
5480 case COMP_WHERE:
5481 gfc_error ("ENTRY statement at %C cannot appear within "
5482 "a WHERE block");
5483 break;
5484 case COMP_CONTAINS:
5485 gfc_error ("ENTRY statement at %C cannot appear within "
5486 "a contained subprogram");
5487 break;
5488 default:
5489 gfc_internal_error ("gfc_match_entry(): Bad state");
5491 return MATCH_ERROR;
5494 module_procedure = gfc_current_ns->parent != NULL
5495 && gfc_current_ns->parent->proc_name
5496 && gfc_current_ns->parent->proc_name->attr.flavor
5497 == FL_MODULE;
5499 if (gfc_current_ns->parent != NULL
5500 && gfc_current_ns->parent->proc_name
5501 && !module_procedure)
5503 gfc_error("ENTRY statement at %C cannot appear in a "
5504 "contained procedure");
5505 return MATCH_ERROR;
5508 /* Module function entries need special care in get_proc_name
5509 because previous references within the function will have
5510 created symbols attached to the current namespace. */
5511 if (get_proc_name (name, &entry,
5512 gfc_current_ns->parent != NULL
5513 && module_procedure))
5514 return MATCH_ERROR;
5516 proc = gfc_current_block ();
5518 /* Make sure that it isn't already declared as BIND(C). If it is, it
5519 must have been marked BIND(C) with a BIND(C) attribute and that is
5520 not allowed for procedures. */
5521 if (entry->attr.is_bind_c == 1)
5523 entry->attr.is_bind_c = 0;
5524 if (entry->old_symbol != NULL)
5525 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5526 "variables or common blocks",
5527 &(entry->old_symbol->declared_at));
5528 else
5529 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5530 "variables or common blocks", &gfc_current_locus);
5533 /* Check what next non-whitespace character is so we can tell if there
5534 is the required parens if we have a BIND(C). */
5535 old_loc = gfc_current_locus;
5536 gfc_gobble_whitespace ();
5537 peek_char = gfc_peek_ascii_char ();
5539 if (state == COMP_SUBROUTINE)
5541 m = gfc_match_formal_arglist (entry, 0, 1);
5542 if (m != MATCH_YES)
5543 return MATCH_ERROR;
5545 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5546 never be an internal procedure. */
5547 is_bind_c = gfc_match_bind_c (entry, true);
5548 if (is_bind_c == MATCH_ERROR)
5549 return MATCH_ERROR;
5550 if (is_bind_c == MATCH_YES)
5552 if (peek_char != '(')
5554 gfc_error ("Missing required parentheses before BIND(C) at %C");
5555 return MATCH_ERROR;
5557 if (!gfc_add_is_bind_c (&(entry->attr), entry->name,
5558 &(entry->declared_at), 1))
5559 return MATCH_ERROR;
5562 if (!gfc_current_ns->parent
5563 && !add_global_entry (name, entry->binding_label, true,
5564 &old_loc))
5565 return MATCH_ERROR;
5567 /* An entry in a subroutine. */
5568 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5569 || !gfc_add_subroutine (&entry->attr, entry->name, NULL))
5570 return MATCH_ERROR;
5572 else
5574 /* An entry in a function.
5575 We need to take special care because writing
5576 ENTRY f()
5578 ENTRY f
5579 is allowed, whereas
5580 ENTRY f() RESULT (r)
5581 can't be written as
5582 ENTRY f RESULT (r). */
5583 if (gfc_match_eos () == MATCH_YES)
5585 gfc_current_locus = old_loc;
5586 /* Match the empty argument list, and add the interface to
5587 the symbol. */
5588 m = gfc_match_formal_arglist (entry, 0, 1);
5590 else
5591 m = gfc_match_formal_arglist (entry, 0, 0);
5593 if (m != MATCH_YES)
5594 return MATCH_ERROR;
5596 result = NULL;
5598 if (gfc_match_eos () == MATCH_YES)
5600 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5601 || !gfc_add_function (&entry->attr, entry->name, NULL))
5602 return MATCH_ERROR;
5604 entry->result = entry;
5606 else
5608 m = gfc_match_suffix (entry, &result);
5609 if (m == MATCH_NO)
5610 gfc_syntax_error (ST_ENTRY);
5611 if (m != MATCH_YES)
5612 return MATCH_ERROR;
5614 if (result)
5616 if (!gfc_add_result (&result->attr, result->name, NULL)
5617 || !gfc_add_entry (&entry->attr, result->name, NULL)
5618 || !gfc_add_function (&entry->attr, result->name, NULL))
5619 return MATCH_ERROR;
5620 entry->result = result;
5622 else
5624 if (!gfc_add_entry (&entry->attr, entry->name, NULL)
5625 || !gfc_add_function (&entry->attr, entry->name, NULL))
5626 return MATCH_ERROR;
5627 entry->result = entry;
5631 if (!gfc_current_ns->parent
5632 && !add_global_entry (name, entry->binding_label, false,
5633 &old_loc))
5634 return MATCH_ERROR;
5637 if (gfc_match_eos () != MATCH_YES)
5639 gfc_syntax_error (ST_ENTRY);
5640 return MATCH_ERROR;
5643 entry->attr.recursive = proc->attr.recursive;
5644 entry->attr.elemental = proc->attr.elemental;
5645 entry->attr.pure = proc->attr.pure;
5647 el = gfc_get_entry_list ();
5648 el->sym = entry;
5649 el->next = gfc_current_ns->entries;
5650 gfc_current_ns->entries = el;
5651 if (el->next)
5652 el->id = el->next->id + 1;
5653 else
5654 el->id = 1;
5656 new_st.op = EXEC_ENTRY;
5657 new_st.ext.entry = el;
5659 return MATCH_YES;
5663 /* Match a subroutine statement, including optional prefixes. */
5665 match
5666 gfc_match_subroutine (void)
5668 char name[GFC_MAX_SYMBOL_LEN + 1];
5669 gfc_symbol *sym;
5670 match m;
5671 match is_bind_c;
5672 char peek_char;
5673 bool allow_binding_name;
5675 if (gfc_current_state () != COMP_NONE
5676 && gfc_current_state () != COMP_INTERFACE
5677 && gfc_current_state () != COMP_CONTAINS)
5678 return MATCH_NO;
5680 m = gfc_match_prefix (NULL);
5681 if (m != MATCH_YES)
5682 return m;
5684 m = gfc_match ("subroutine% %n", name);
5685 if (m != MATCH_YES)
5686 return m;
5688 if (get_proc_name (name, &sym, false))
5689 return MATCH_ERROR;
5691 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5692 the symbol existed before. */
5693 sym->declared_at = gfc_current_locus;
5695 if (add_hidden_procptr_result (sym))
5696 sym = sym->result;
5698 gfc_new_block = sym;
5700 /* Check what next non-whitespace character is so we can tell if there
5701 is the required parens if we have a BIND(C). */
5702 gfc_gobble_whitespace ();
5703 peek_char = gfc_peek_ascii_char ();
5705 if (!gfc_add_subroutine (&sym->attr, sym->name, NULL))
5706 return MATCH_ERROR;
5708 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5709 return MATCH_ERROR;
5711 /* Make sure that it isn't already declared as BIND(C). If it is, it
5712 must have been marked BIND(C) with a BIND(C) attribute and that is
5713 not allowed for procedures. */
5714 if (sym->attr.is_bind_c == 1)
5716 sym->attr.is_bind_c = 0;
5717 if (sym->old_symbol != NULL)
5718 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5719 "variables or common blocks",
5720 &(sym->old_symbol->declared_at));
5721 else
5722 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5723 "variables or common blocks", &gfc_current_locus);
5726 /* C binding names are not allowed for internal procedures. */
5727 if (gfc_current_state () == COMP_CONTAINS
5728 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5729 allow_binding_name = false;
5730 else
5731 allow_binding_name = true;
5733 /* Here, we are just checking if it has the bind(c) attribute, and if
5734 so, then we need to make sure it's all correct. If it doesn't,
5735 we still need to continue matching the rest of the subroutine line. */
5736 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5737 if (is_bind_c == MATCH_ERROR)
5739 /* There was an attempt at the bind(c), but it was wrong. An
5740 error message should have been printed w/in the gfc_match_bind_c
5741 so here we'll just return the MATCH_ERROR. */
5742 return MATCH_ERROR;
5745 if (is_bind_c == MATCH_YES)
5747 /* The following is allowed in the Fortran 2008 draft. */
5748 if (gfc_current_state () == COMP_CONTAINS
5749 && sym->ns->proc_name->attr.flavor != FL_MODULE
5750 && !gfc_notify_std (GFC_STD_F2008, "BIND(C) attribute "
5751 "at %L may not be specified for an internal "
5752 "procedure", &gfc_current_locus))
5753 return MATCH_ERROR;
5755 if (peek_char != '(')
5757 gfc_error ("Missing required parentheses before BIND(C) at %C");
5758 return MATCH_ERROR;
5760 if (!gfc_add_is_bind_c (&(sym->attr), sym->name,
5761 &(sym->declared_at), 1))
5762 return MATCH_ERROR;
5765 if (gfc_match_eos () != MATCH_YES)
5767 gfc_syntax_error (ST_SUBROUTINE);
5768 return MATCH_ERROR;
5771 if (!copy_prefix (&sym->attr, &sym->declared_at))
5772 return MATCH_ERROR;
5774 /* Warn if it has the same name as an intrinsic. */
5775 warn_intrinsic_shadow (sym, false);
5777 return MATCH_YES;
5781 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5782 given, and set the binding label in either the given symbol (if not
5783 NULL), or in the current_ts. The symbol may be NULL because we may
5784 encounter the BIND(C) before the declaration itself. Return
5785 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5786 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5787 or MATCH_YES if the specifier was correct and the binding label and
5788 bind(c) fields were set correctly for the given symbol or the
5789 current_ts. If allow_binding_name is false, no binding name may be
5790 given. */
5792 match
5793 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5795 /* binding label, if exists */
5796 const char* binding_label = NULL;
5797 match double_quote;
5798 match single_quote;
5800 /* Initialize the flag that specifies whether we encountered a NAME=
5801 specifier or not. */
5802 has_name_equals = 0;
5804 /* This much we have to be able to match, in this order, if
5805 there is a bind(c) label. */
5806 if (gfc_match (" bind ( c ") != MATCH_YES)
5807 return MATCH_NO;
5809 /* Now see if there is a binding label, or if we've reached the
5810 end of the bind(c) attribute without one. */
5811 if (gfc_match_char (',') == MATCH_YES)
5813 if (gfc_match (" name = ") != MATCH_YES)
5815 gfc_error ("Syntax error in NAME= specifier for binding label "
5816 "at %C");
5817 /* should give an error message here */
5818 return MATCH_ERROR;
5821 has_name_equals = 1;
5823 /* Get the opening quote. */
5824 double_quote = MATCH_YES;
5825 single_quote = MATCH_YES;
5826 double_quote = gfc_match_char ('"');
5827 if (double_quote != MATCH_YES)
5828 single_quote = gfc_match_char ('\'');
5829 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5831 gfc_error ("Syntax error in NAME= specifier for binding label "
5832 "at %C");
5833 return MATCH_ERROR;
5836 /* Grab the binding label, using functions that will not lower
5837 case the names automatically. */
5838 if (gfc_match_name_C (&binding_label) != MATCH_YES)
5839 return MATCH_ERROR;
5841 /* Get the closing quotation. */
5842 if (double_quote == MATCH_YES)
5844 if (gfc_match_char ('"') != MATCH_YES)
5846 gfc_error ("Missing closing quote '\"' for binding label at %C");
5847 /* User started string with '"' so looked to match it. */
5848 return MATCH_ERROR;
5851 else
5853 if (gfc_match_char ('\'') != MATCH_YES)
5855 gfc_error ("Missing closing quote '\'' for binding label at %C");
5856 /* User started string with "'" char. */
5857 return MATCH_ERROR;
5862 /* Get the required right paren. */
5863 if (gfc_match_char (')') != MATCH_YES)
5865 gfc_error ("Missing closing paren for binding label at %C");
5866 return MATCH_ERROR;
5869 if (has_name_equals && !allow_binding_name)
5871 gfc_error ("No binding name is allowed in BIND(C) at %C");
5872 return MATCH_ERROR;
5875 if (has_name_equals && sym != NULL && sym->attr.dummy)
5877 gfc_error ("For dummy procedure %s, no binding name is "
5878 "allowed in BIND(C) at %C", sym->name);
5879 return MATCH_ERROR;
5883 /* Save the binding label to the symbol. If sym is null, we're
5884 probably matching the typespec attributes of a declaration and
5885 haven't gotten the name yet, and therefore, no symbol yet. */
5886 if (binding_label)
5888 if (sym != NULL)
5889 sym->binding_label = binding_label;
5890 else
5891 curr_binding_label = binding_label;
5893 else if (allow_binding_name)
5895 /* No binding label, but if symbol isn't null, we
5896 can set the label for it here.
5897 If name="" or allow_binding_name is false, no C binding name is
5898 created. */
5899 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5900 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
5903 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5904 && current_interface.type == INTERFACE_ABSTRACT)
5906 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5907 return MATCH_ERROR;
5910 return MATCH_YES;
5914 /* Return nonzero if we're currently compiling a contained procedure. */
5916 static int
5917 contained_procedure (void)
5919 gfc_state_data *s = gfc_state_stack;
5921 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5922 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5923 return 1;
5925 return 0;
5928 /* Set the kind of each enumerator. The kind is selected such that it is
5929 interoperable with the corresponding C enumeration type, making
5930 sure that -fshort-enums is honored. */
5932 static void
5933 set_enum_kind(void)
5935 enumerator_history *current_history = NULL;
5936 int kind;
5937 int i;
5939 if (max_enum == NULL || enum_history == NULL)
5940 return;
5942 if (!flag_short_enums)
5943 return;
5945 i = 0;
5948 kind = gfc_integer_kinds[i++].kind;
5950 while (kind < gfc_c_int_kind
5951 && gfc_check_integer_range (max_enum->initializer->value.integer,
5952 kind) != ARITH_OK);
5954 current_history = enum_history;
5955 while (current_history != NULL)
5957 current_history->sym->ts.kind = kind;
5958 current_history = current_history->next;
5963 /* Match any of the various end-block statements. Returns the type of
5964 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5965 and END BLOCK statements cannot be replaced by a single END statement. */
5967 match
5968 gfc_match_end (gfc_statement *st)
5970 char name[GFC_MAX_SYMBOL_LEN + 1];
5971 gfc_compile_state state;
5972 locus old_loc;
5973 const char *block_name;
5974 const char *target;
5975 int eos_ok;
5976 match m;
5977 gfc_namespace *parent_ns, *ns, *prev_ns;
5978 gfc_namespace **nsp;
5980 old_loc = gfc_current_locus;
5981 if (gfc_match ("end") != MATCH_YES)
5982 return MATCH_NO;
5984 state = gfc_current_state ();
5985 block_name = gfc_current_block () == NULL
5986 ? NULL : gfc_current_block ()->name;
5988 switch (state)
5990 case COMP_ASSOCIATE:
5991 case COMP_BLOCK:
5992 if (!strncmp (block_name, "block@", strlen("block@")))
5993 block_name = NULL;
5994 break;
5996 case COMP_CONTAINS:
5997 case COMP_DERIVED_CONTAINS:
5998 state = gfc_state_stack->previous->state;
5999 block_name = gfc_state_stack->previous->sym == NULL
6000 ? NULL : gfc_state_stack->previous->sym->name;
6001 break;
6003 default:
6004 break;
6007 switch (state)
6009 case COMP_NONE:
6010 case COMP_PROGRAM:
6011 *st = ST_END_PROGRAM;
6012 target = " program";
6013 eos_ok = 1;
6014 break;
6016 case COMP_SUBROUTINE:
6017 *st = ST_END_SUBROUTINE;
6018 target = " subroutine";
6019 eos_ok = !contained_procedure ();
6020 break;
6022 case COMP_FUNCTION:
6023 *st = ST_END_FUNCTION;
6024 target = " function";
6025 eos_ok = !contained_procedure ();
6026 break;
6028 case COMP_BLOCK_DATA:
6029 *st = ST_END_BLOCK_DATA;
6030 target = " block data";
6031 eos_ok = 1;
6032 break;
6034 case COMP_MODULE:
6035 *st = ST_END_MODULE;
6036 target = " module";
6037 eos_ok = 1;
6038 break;
6040 case COMP_INTERFACE:
6041 *st = ST_END_INTERFACE;
6042 target = " interface";
6043 eos_ok = 0;
6044 break;
6046 case COMP_DERIVED:
6047 case COMP_DERIVED_CONTAINS:
6048 *st = ST_END_TYPE;
6049 target = " type";
6050 eos_ok = 0;
6051 break;
6053 case COMP_ASSOCIATE:
6054 *st = ST_END_ASSOCIATE;
6055 target = " associate";
6056 eos_ok = 0;
6057 break;
6059 case COMP_BLOCK:
6060 *st = ST_END_BLOCK;
6061 target = " block";
6062 eos_ok = 0;
6063 break;
6065 case COMP_IF:
6066 *st = ST_ENDIF;
6067 target = " if";
6068 eos_ok = 0;
6069 break;
6071 case COMP_DO:
6072 case COMP_DO_CONCURRENT:
6073 *st = ST_ENDDO;
6074 target = " do";
6075 eos_ok = 0;
6076 break;
6078 case COMP_CRITICAL:
6079 *st = ST_END_CRITICAL;
6080 target = " critical";
6081 eos_ok = 0;
6082 break;
6084 case COMP_SELECT:
6085 case COMP_SELECT_TYPE:
6086 *st = ST_END_SELECT;
6087 target = " select";
6088 eos_ok = 0;
6089 break;
6091 case COMP_FORALL:
6092 *st = ST_END_FORALL;
6093 target = " forall";
6094 eos_ok = 0;
6095 break;
6097 case COMP_WHERE:
6098 *st = ST_END_WHERE;
6099 target = " where";
6100 eos_ok = 0;
6101 break;
6103 case COMP_ENUM:
6104 *st = ST_END_ENUM;
6105 target = " enum";
6106 eos_ok = 0;
6107 last_initializer = NULL;
6108 set_enum_kind ();
6109 gfc_free_enum_history ();
6110 break;
6112 default:
6113 gfc_error ("Unexpected END statement at %C");
6114 goto cleanup;
6117 old_loc = gfc_current_locus;
6118 if (gfc_match_eos () == MATCH_YES)
6120 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6122 if (!gfc_notify_std (GFC_STD_F2008, "END statement "
6123 "instead of %s statement at %L",
6124 gfc_ascii_statement(*st), &old_loc))
6125 goto cleanup;
6127 else if (!eos_ok)
6129 /* We would have required END [something]. */
6130 gfc_error ("%s statement expected at %L",
6131 gfc_ascii_statement (*st), &old_loc);
6132 goto cleanup;
6135 return MATCH_YES;
6138 /* Verify that we've got the sort of end-block that we're expecting. */
6139 if (gfc_match (target) != MATCH_YES)
6141 gfc_error ("Expecting %s statement at %L", gfc_ascii_statement (*st),
6142 &old_loc);
6143 goto cleanup;
6146 old_loc = gfc_current_locus;
6147 /* If we're at the end, make sure a block name wasn't required. */
6148 if (gfc_match_eos () == MATCH_YES)
6151 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6152 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6153 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6154 return MATCH_YES;
6156 if (!block_name)
6157 return MATCH_YES;
6159 gfc_error ("Expected block name of '%s' in %s statement at %L",
6160 block_name, gfc_ascii_statement (*st), &old_loc);
6162 return MATCH_ERROR;
6165 /* END INTERFACE has a special handler for its several possible endings. */
6166 if (*st == ST_END_INTERFACE)
6167 return gfc_match_end_interface ();
6169 /* We haven't hit the end of statement, so what is left must be an
6170 end-name. */
6171 m = gfc_match_space ();
6172 if (m == MATCH_YES)
6173 m = gfc_match_name (name);
6175 if (m == MATCH_NO)
6176 gfc_error ("Expected terminating name at %C");
6177 if (m != MATCH_YES)
6178 goto cleanup;
6180 if (block_name == NULL)
6181 goto syntax;
6183 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6185 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
6186 gfc_ascii_statement (*st));
6187 goto cleanup;
6189 /* Procedure pointer as function result. */
6190 else if (strcmp (block_name, "ppr@") == 0
6191 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6193 gfc_error ("Expected label '%s' for %s statement at %C",
6194 gfc_current_block ()->ns->proc_name->name,
6195 gfc_ascii_statement (*st));
6196 goto cleanup;
6199 if (gfc_match_eos () == MATCH_YES)
6200 return MATCH_YES;
6202 syntax:
6203 gfc_syntax_error (*st);
6205 cleanup:
6206 gfc_current_locus = old_loc;
6208 /* If we are missing an END BLOCK, we created a half-ready namespace.
6209 Remove it from the parent namespace's sibling list. */
6211 if (state == COMP_BLOCK)
6213 parent_ns = gfc_current_ns->parent;
6215 nsp = &(gfc_state_stack->previous->tail->ext.block.ns);
6217 prev_ns = NULL;
6218 ns = *nsp;
6219 while (ns)
6221 if (ns == gfc_current_ns)
6223 if (prev_ns == NULL)
6224 *nsp = NULL;
6225 else
6226 prev_ns->sibling = ns->sibling;
6228 prev_ns = ns;
6229 ns = ns->sibling;
6232 gfc_free_namespace (gfc_current_ns);
6233 gfc_current_ns = parent_ns;
6236 return MATCH_ERROR;
6241 /***************** Attribute declaration statements ****************/
6243 /* Set the attribute of a single variable. */
6245 static match
6246 attr_decl1 (void)
6248 char name[GFC_MAX_SYMBOL_LEN + 1];
6249 gfc_array_spec *as;
6250 gfc_symbol *sym;
6251 locus var_locus;
6252 match m;
6254 as = NULL;
6256 m = gfc_match_name (name);
6257 if (m != MATCH_YES)
6258 goto cleanup;
6260 if (find_special (name, &sym, false))
6261 return MATCH_ERROR;
6263 if (!check_function_name (name))
6265 m = MATCH_ERROR;
6266 goto cleanup;
6269 var_locus = gfc_current_locus;
6271 /* Deal with possible array specification for certain attributes. */
6272 if (current_attr.dimension
6273 || current_attr.codimension
6274 || current_attr.allocatable
6275 || current_attr.pointer
6276 || current_attr.target)
6278 m = gfc_match_array_spec (&as, !current_attr.codimension,
6279 !current_attr.dimension
6280 && !current_attr.pointer
6281 && !current_attr.target);
6282 if (m == MATCH_ERROR)
6283 goto cleanup;
6285 if (current_attr.dimension && m == MATCH_NO)
6287 gfc_error ("Missing array specification at %L in DIMENSION "
6288 "statement", &var_locus);
6289 m = MATCH_ERROR;
6290 goto cleanup;
6293 if (current_attr.dimension && sym->value)
6295 gfc_error ("Dimensions specified for %s at %L after its "
6296 "initialisation", sym->name, &var_locus);
6297 m = MATCH_ERROR;
6298 goto cleanup;
6301 if (current_attr.codimension && m == MATCH_NO)
6303 gfc_error ("Missing array specification at %L in CODIMENSION "
6304 "statement", &var_locus);
6305 m = MATCH_ERROR;
6306 goto cleanup;
6309 if ((current_attr.allocatable || current_attr.pointer)
6310 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6312 gfc_error ("Array specification must be deferred at %L", &var_locus);
6313 m = MATCH_ERROR;
6314 goto cleanup;
6318 /* Update symbol table. DIMENSION attribute is set in
6319 gfc_set_array_spec(). For CLASS variables, this must be applied
6320 to the first component, or '_data' field. */
6321 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6323 if (!gfc_copy_attr (&CLASS_DATA(sym)->attr, &current_attr, &var_locus))
6325 m = MATCH_ERROR;
6326 goto cleanup;
6329 else
6331 if (current_attr.dimension == 0 && current_attr.codimension == 0
6332 && !gfc_copy_attr (&sym->attr, &current_attr, &var_locus))
6334 m = MATCH_ERROR;
6335 goto cleanup;
6339 if (sym->ts.type == BT_CLASS
6340 && !gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as))
6342 m = MATCH_ERROR;
6343 goto cleanup;
6346 if (!gfc_set_array_spec (sym, as, &var_locus))
6348 m = MATCH_ERROR;
6349 goto cleanup;
6352 if (sym->attr.cray_pointee && sym->as != NULL)
6354 /* Fix the array spec. */
6355 m = gfc_mod_pointee_as (sym->as);
6356 if (m == MATCH_ERROR)
6357 goto cleanup;
6360 if (!gfc_add_attribute (&sym->attr, &var_locus))
6362 m = MATCH_ERROR;
6363 goto cleanup;
6366 if ((current_attr.external || current_attr.intrinsic)
6367 && sym->attr.flavor != FL_PROCEDURE
6368 && !gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL))
6370 m = MATCH_ERROR;
6371 goto cleanup;
6374 add_hidden_procptr_result (sym);
6376 return MATCH_YES;
6378 cleanup:
6379 gfc_free_array_spec (as);
6380 return m;
6384 /* Generic attribute declaration subroutine. Used for attributes that
6385 just have a list of names. */
6387 static match
6388 attr_decl (void)
6390 match m;
6392 /* Gobble the optional double colon, by simply ignoring the result
6393 of gfc_match(). */
6394 gfc_match (" ::");
6396 for (;;)
6398 m = attr_decl1 ();
6399 if (m != MATCH_YES)
6400 break;
6402 if (gfc_match_eos () == MATCH_YES)
6404 m = MATCH_YES;
6405 break;
6408 if (gfc_match_char (',') != MATCH_YES)
6410 gfc_error ("Unexpected character in variable list at %C");
6411 m = MATCH_ERROR;
6412 break;
6416 return m;
6420 /* This routine matches Cray Pointer declarations of the form:
6421 pointer ( <pointer>, <pointee> )
6423 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6424 The pointer, if already declared, should be an integer. Otherwise, we
6425 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6426 be either a scalar, or an array declaration. No space is allocated for
6427 the pointee. For the statement
6428 pointer (ipt, ar(10))
6429 any subsequent uses of ar will be translated (in C-notation) as
6430 ar(i) => ((<type> *) ipt)(i)
6431 After gimplification, pointee variable will disappear in the code. */
6433 static match
6434 cray_pointer_decl (void)
6436 match m;
6437 gfc_array_spec *as = NULL;
6438 gfc_symbol *cptr; /* Pointer symbol. */
6439 gfc_symbol *cpte; /* Pointee symbol. */
6440 locus var_locus;
6441 bool done = false;
6443 while (!done)
6445 if (gfc_match_char ('(') != MATCH_YES)
6447 gfc_error ("Expected '(' at %C");
6448 return MATCH_ERROR;
6451 /* Match pointer. */
6452 var_locus = gfc_current_locus;
6453 gfc_clear_attr (&current_attr);
6454 gfc_add_cray_pointer (&current_attr, &var_locus);
6455 current_ts.type = BT_INTEGER;
6456 current_ts.kind = gfc_index_integer_kind;
6458 m = gfc_match_symbol (&cptr, 0);
6459 if (m != MATCH_YES)
6461 gfc_error ("Expected variable name at %C");
6462 return m;
6465 if (!gfc_add_cray_pointer (&cptr->attr, &var_locus))
6466 return MATCH_ERROR;
6468 gfc_set_sym_referenced (cptr);
6470 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6472 cptr->ts.type = BT_INTEGER;
6473 cptr->ts.kind = gfc_index_integer_kind;
6475 else if (cptr->ts.type != BT_INTEGER)
6477 gfc_error ("Cray pointer at %C must be an integer");
6478 return MATCH_ERROR;
6480 else if (cptr->ts.kind < gfc_index_integer_kind)
6481 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6482 " memory addresses require %d bytes",
6483 cptr->ts.kind, gfc_index_integer_kind);
6485 if (gfc_match_char (',') != MATCH_YES)
6487 gfc_error ("Expected \",\" at %C");
6488 return MATCH_ERROR;
6491 /* Match Pointee. */
6492 var_locus = gfc_current_locus;
6493 gfc_clear_attr (&current_attr);
6494 gfc_add_cray_pointee (&current_attr, &var_locus);
6495 current_ts.type = BT_UNKNOWN;
6496 current_ts.kind = 0;
6498 m = gfc_match_symbol (&cpte, 0);
6499 if (m != MATCH_YES)
6501 gfc_error ("Expected variable name at %C");
6502 return m;
6505 /* Check for an optional array spec. */
6506 m = gfc_match_array_spec (&as, true, false);
6507 if (m == MATCH_ERROR)
6509 gfc_free_array_spec (as);
6510 return m;
6512 else if (m == MATCH_NO)
6514 gfc_free_array_spec (as);
6515 as = NULL;
6518 if (!gfc_add_cray_pointee (&cpte->attr, &var_locus))
6519 return MATCH_ERROR;
6521 gfc_set_sym_referenced (cpte);
6523 if (cpte->as == NULL)
6525 if (!gfc_set_array_spec (cpte, as, &var_locus))
6526 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6528 else if (as != NULL)
6530 gfc_error ("Duplicate array spec for Cray pointee at %C");
6531 gfc_free_array_spec (as);
6532 return MATCH_ERROR;
6535 as = NULL;
6537 if (cpte->as != NULL)
6539 /* Fix array spec. */
6540 m = gfc_mod_pointee_as (cpte->as);
6541 if (m == MATCH_ERROR)
6542 return m;
6545 /* Point the Pointee at the Pointer. */
6546 cpte->cp_pointer = cptr;
6548 if (gfc_match_char (')') != MATCH_YES)
6550 gfc_error ("Expected \")\" at %C");
6551 return MATCH_ERROR;
6553 m = gfc_match_char (',');
6554 if (m != MATCH_YES)
6555 done = true; /* Stop searching for more declarations. */
6559 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6560 || gfc_match_eos () != MATCH_YES)
6562 gfc_error ("Expected \",\" or end of statement at %C");
6563 return MATCH_ERROR;
6565 return MATCH_YES;
6569 match
6570 gfc_match_external (void)
6573 gfc_clear_attr (&current_attr);
6574 current_attr.external = 1;
6576 return attr_decl ();
6580 match
6581 gfc_match_intent (void)
6583 sym_intent intent;
6585 /* This is not allowed within a BLOCK construct! */
6586 if (gfc_current_state () == COMP_BLOCK)
6588 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6589 return MATCH_ERROR;
6592 intent = match_intent_spec ();
6593 if (intent == INTENT_UNKNOWN)
6594 return MATCH_ERROR;
6596 gfc_clear_attr (&current_attr);
6597 current_attr.intent = intent;
6599 return attr_decl ();
6603 match
6604 gfc_match_intrinsic (void)
6607 gfc_clear_attr (&current_attr);
6608 current_attr.intrinsic = 1;
6610 return attr_decl ();
6614 match
6615 gfc_match_optional (void)
6617 /* This is not allowed within a BLOCK construct! */
6618 if (gfc_current_state () == COMP_BLOCK)
6620 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6621 return MATCH_ERROR;
6624 gfc_clear_attr (&current_attr);
6625 current_attr.optional = 1;
6627 return attr_decl ();
6631 match
6632 gfc_match_pointer (void)
6634 gfc_gobble_whitespace ();
6635 if (gfc_peek_ascii_char () == '(')
6637 if (!gfc_option.flag_cray_pointer)
6639 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6640 "flag");
6641 return MATCH_ERROR;
6643 return cray_pointer_decl ();
6645 else
6647 gfc_clear_attr (&current_attr);
6648 current_attr.pointer = 1;
6650 return attr_decl ();
6655 match
6656 gfc_match_allocatable (void)
6658 gfc_clear_attr (&current_attr);
6659 current_attr.allocatable = 1;
6661 return attr_decl ();
6665 match
6666 gfc_match_codimension (void)
6668 gfc_clear_attr (&current_attr);
6669 current_attr.codimension = 1;
6671 return attr_decl ();
6675 match
6676 gfc_match_contiguous (void)
6678 if (!gfc_notify_std (GFC_STD_F2008, "CONTIGUOUS statement at %C"))
6679 return MATCH_ERROR;
6681 gfc_clear_attr (&current_attr);
6682 current_attr.contiguous = 1;
6684 return attr_decl ();
6688 match
6689 gfc_match_dimension (void)
6691 gfc_clear_attr (&current_attr);
6692 current_attr.dimension = 1;
6694 return attr_decl ();
6698 match
6699 gfc_match_target (void)
6701 gfc_clear_attr (&current_attr);
6702 current_attr.target = 1;
6704 return attr_decl ();
6708 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6709 statement. */
6711 static match
6712 access_attr_decl (gfc_statement st)
6714 char name[GFC_MAX_SYMBOL_LEN + 1];
6715 interface_type type;
6716 gfc_user_op *uop;
6717 gfc_symbol *sym, *dt_sym;
6718 gfc_intrinsic_op op;
6719 match m;
6721 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6722 goto done;
6724 for (;;)
6726 m = gfc_match_generic_spec (&type, name, &op);
6727 if (m == MATCH_NO)
6728 goto syntax;
6729 if (m == MATCH_ERROR)
6730 return MATCH_ERROR;
6732 switch (type)
6734 case INTERFACE_NAMELESS:
6735 case INTERFACE_ABSTRACT:
6736 goto syntax;
6738 case INTERFACE_GENERIC:
6739 if (gfc_get_symbol (name, NULL, &sym))
6740 goto done;
6742 if (!gfc_add_access (&sym->attr,
6743 (st == ST_PUBLIC)
6744 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6745 sym->name, NULL))
6746 return MATCH_ERROR;
6748 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
6749 && !gfc_add_access (&dt_sym->attr,
6750 (st == ST_PUBLIC)
6751 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6752 sym->name, NULL))
6753 return MATCH_ERROR;
6755 break;
6757 case INTERFACE_INTRINSIC_OP:
6758 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6760 gfc_intrinsic_op other_op;
6762 gfc_current_ns->operator_access[op] =
6763 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6765 /* Handle the case if there is another op with the same
6766 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6767 other_op = gfc_equivalent_op (op);
6769 if (other_op != INTRINSIC_NONE)
6770 gfc_current_ns->operator_access[other_op] =
6771 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6774 else
6776 gfc_error ("Access specification of the %s operator at %C has "
6777 "already been specified", gfc_op2string (op));
6778 goto done;
6781 break;
6783 case INTERFACE_USER_OP:
6784 uop = gfc_get_uop (name);
6786 if (uop->access == ACCESS_UNKNOWN)
6788 uop->access = (st == ST_PUBLIC)
6789 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6791 else
6793 gfc_error ("Access specification of the .%s. operator at %C "
6794 "has already been specified", sym->name);
6795 goto done;
6798 break;
6801 if (gfc_match_char (',') == MATCH_NO)
6802 break;
6805 if (gfc_match_eos () != MATCH_YES)
6806 goto syntax;
6807 return MATCH_YES;
6809 syntax:
6810 gfc_syntax_error (st);
6812 done:
6813 return MATCH_ERROR;
6817 match
6818 gfc_match_protected (void)
6820 gfc_symbol *sym;
6821 match m;
6823 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6825 gfc_error ("PROTECTED at %C only allowed in specification "
6826 "part of a module");
6827 return MATCH_ERROR;
6831 if (!gfc_notify_std (GFC_STD_F2003, "PROTECTED statement at %C"))
6832 return MATCH_ERROR;
6834 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6836 return MATCH_ERROR;
6839 if (gfc_match_eos () == MATCH_YES)
6840 goto syntax;
6842 for(;;)
6844 m = gfc_match_symbol (&sym, 0);
6845 switch (m)
6847 case MATCH_YES:
6848 if (!gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus))
6849 return MATCH_ERROR;
6850 goto next_item;
6852 case MATCH_NO:
6853 break;
6855 case MATCH_ERROR:
6856 return MATCH_ERROR;
6859 next_item:
6860 if (gfc_match_eos () == MATCH_YES)
6861 break;
6862 if (gfc_match_char (',') != MATCH_YES)
6863 goto syntax;
6866 return MATCH_YES;
6868 syntax:
6869 gfc_error ("Syntax error in PROTECTED statement at %C");
6870 return MATCH_ERROR;
6874 /* The PRIVATE statement is a bit weird in that it can be an attribute
6875 declaration, but also works as a standalone statement inside of a
6876 type declaration or a module. */
6878 match
6879 gfc_match_private (gfc_statement *st)
6882 if (gfc_match ("private") != MATCH_YES)
6883 return MATCH_NO;
6885 if (gfc_current_state () != COMP_MODULE
6886 && !(gfc_current_state () == COMP_DERIVED
6887 && gfc_state_stack->previous
6888 && gfc_state_stack->previous->state == COMP_MODULE)
6889 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6890 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6891 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6893 gfc_error ("PRIVATE statement at %C is only allowed in the "
6894 "specification part of a module");
6895 return MATCH_ERROR;
6898 if (gfc_current_state () == COMP_DERIVED)
6900 if (gfc_match_eos () == MATCH_YES)
6902 *st = ST_PRIVATE;
6903 return MATCH_YES;
6906 gfc_syntax_error (ST_PRIVATE);
6907 return MATCH_ERROR;
6910 if (gfc_match_eos () == MATCH_YES)
6912 *st = ST_PRIVATE;
6913 return MATCH_YES;
6916 *st = ST_ATTR_DECL;
6917 return access_attr_decl (ST_PRIVATE);
6921 match
6922 gfc_match_public (gfc_statement *st)
6925 if (gfc_match ("public") != MATCH_YES)
6926 return MATCH_NO;
6928 if (gfc_current_state () != COMP_MODULE)
6930 gfc_error ("PUBLIC statement at %C is only allowed in the "
6931 "specification part of a module");
6932 return MATCH_ERROR;
6935 if (gfc_match_eos () == MATCH_YES)
6937 *st = ST_PUBLIC;
6938 return MATCH_YES;
6941 *st = ST_ATTR_DECL;
6942 return access_attr_decl (ST_PUBLIC);
6946 /* Workhorse for gfc_match_parameter. */
6948 static match
6949 do_parm (void)
6951 gfc_symbol *sym;
6952 gfc_expr *init;
6953 match m;
6954 bool t;
6956 m = gfc_match_symbol (&sym, 0);
6957 if (m == MATCH_NO)
6958 gfc_error ("Expected variable name at %C in PARAMETER statement");
6960 if (m != MATCH_YES)
6961 return m;
6963 if (gfc_match_char ('=') == MATCH_NO)
6965 gfc_error ("Expected = sign in PARAMETER statement at %C");
6966 return MATCH_ERROR;
6969 m = gfc_match_init_expr (&init);
6970 if (m == MATCH_NO)
6971 gfc_error ("Expected expression at %C in PARAMETER statement");
6972 if (m != MATCH_YES)
6973 return m;
6975 if (sym->ts.type == BT_UNKNOWN
6976 && !gfc_set_default_type (sym, 1, NULL))
6978 m = MATCH_ERROR;
6979 goto cleanup;
6982 if (!gfc_check_assign_symbol (sym, NULL, init)
6983 || !gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL))
6985 m = MATCH_ERROR;
6986 goto cleanup;
6989 if (sym->value)
6991 gfc_error ("Initializing already initialized variable at %C");
6992 m = MATCH_ERROR;
6993 goto cleanup;
6996 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
6997 return (t) ? MATCH_YES : MATCH_ERROR;
6999 cleanup:
7000 gfc_free_expr (init);
7001 return m;
7005 /* Match a parameter statement, with the weird syntax that these have. */
7007 match
7008 gfc_match_parameter (void)
7010 match m;
7012 if (gfc_match_char ('(') == MATCH_NO)
7013 return MATCH_NO;
7015 for (;;)
7017 m = do_parm ();
7018 if (m != MATCH_YES)
7019 break;
7021 if (gfc_match (" )%t") == MATCH_YES)
7022 break;
7024 if (gfc_match_char (',') != MATCH_YES)
7026 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7027 m = MATCH_ERROR;
7028 break;
7032 return m;
7036 /* Save statements have a special syntax. */
7038 match
7039 gfc_match_save (void)
7041 char n[GFC_MAX_SYMBOL_LEN+1];
7042 gfc_common_head *c;
7043 gfc_symbol *sym;
7044 match m;
7046 if (gfc_match_eos () == MATCH_YES)
7048 if (gfc_current_ns->seen_save)
7050 if (!gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
7051 "follows previous SAVE statement"))
7052 return MATCH_ERROR;
7055 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
7056 return MATCH_YES;
7059 if (gfc_current_ns->save_all)
7061 if (!gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
7062 "blanket SAVE statement"))
7063 return MATCH_ERROR;
7066 gfc_match (" ::");
7068 for (;;)
7070 m = gfc_match_symbol (&sym, 0);
7071 switch (m)
7073 case MATCH_YES:
7074 if (!gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
7075 &gfc_current_locus))
7076 return MATCH_ERROR;
7077 goto next_item;
7079 case MATCH_NO:
7080 break;
7082 case MATCH_ERROR:
7083 return MATCH_ERROR;
7086 m = gfc_match (" / %n /", &n);
7087 if (m == MATCH_ERROR)
7088 return MATCH_ERROR;
7089 if (m == MATCH_NO)
7090 goto syntax;
7092 c = gfc_get_common (n, 0);
7093 c->saved = 1;
7095 gfc_current_ns->seen_save = 1;
7097 next_item:
7098 if (gfc_match_eos () == MATCH_YES)
7099 break;
7100 if (gfc_match_char (',') != MATCH_YES)
7101 goto syntax;
7104 return MATCH_YES;
7106 syntax:
7107 gfc_error ("Syntax error in SAVE statement at %C");
7108 return MATCH_ERROR;
7112 match
7113 gfc_match_value (void)
7115 gfc_symbol *sym;
7116 match m;
7118 /* This is not allowed within a BLOCK construct! */
7119 if (gfc_current_state () == COMP_BLOCK)
7121 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7122 return MATCH_ERROR;
7125 if (!gfc_notify_std (GFC_STD_F2003, "VALUE statement at %C"))
7126 return MATCH_ERROR;
7128 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7130 return MATCH_ERROR;
7133 if (gfc_match_eos () == MATCH_YES)
7134 goto syntax;
7136 for(;;)
7138 m = gfc_match_symbol (&sym, 0);
7139 switch (m)
7141 case MATCH_YES:
7142 if (!gfc_add_value (&sym->attr, sym->name, &gfc_current_locus))
7143 return MATCH_ERROR;
7144 goto next_item;
7146 case MATCH_NO:
7147 break;
7149 case MATCH_ERROR:
7150 return MATCH_ERROR;
7153 next_item:
7154 if (gfc_match_eos () == MATCH_YES)
7155 break;
7156 if (gfc_match_char (',') != MATCH_YES)
7157 goto syntax;
7160 return MATCH_YES;
7162 syntax:
7163 gfc_error ("Syntax error in VALUE statement at %C");
7164 return MATCH_ERROR;
7168 match
7169 gfc_match_volatile (void)
7171 gfc_symbol *sym;
7172 match m;
7174 if (!gfc_notify_std (GFC_STD_F2003, "VOLATILE statement at %C"))
7175 return MATCH_ERROR;
7177 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7179 return MATCH_ERROR;
7182 if (gfc_match_eos () == MATCH_YES)
7183 goto syntax;
7185 for(;;)
7187 /* VOLATILE is special because it can be added to host-associated
7188 symbols locally. Except for coarrays. */
7189 m = gfc_match_symbol (&sym, 1);
7190 switch (m)
7192 case MATCH_YES:
7193 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7194 for variable in a BLOCK which is defined outside of the BLOCK. */
7195 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7197 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
7198 "%C, which is use-/host-associated", sym->name);
7199 return MATCH_ERROR;
7201 if (!gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus))
7202 return MATCH_ERROR;
7203 goto next_item;
7205 case MATCH_NO:
7206 break;
7208 case MATCH_ERROR:
7209 return MATCH_ERROR;
7212 next_item:
7213 if (gfc_match_eos () == MATCH_YES)
7214 break;
7215 if (gfc_match_char (',') != MATCH_YES)
7216 goto syntax;
7219 return MATCH_YES;
7221 syntax:
7222 gfc_error ("Syntax error in VOLATILE statement at %C");
7223 return MATCH_ERROR;
7227 match
7228 gfc_match_asynchronous (void)
7230 gfc_symbol *sym;
7231 match m;
7233 if (!gfc_notify_std (GFC_STD_F2003, "ASYNCHRONOUS statement at %C"))
7234 return MATCH_ERROR;
7236 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7238 return MATCH_ERROR;
7241 if (gfc_match_eos () == MATCH_YES)
7242 goto syntax;
7244 for(;;)
7246 /* ASYNCHRONOUS is special because it can be added to host-associated
7247 symbols locally. */
7248 m = gfc_match_symbol (&sym, 1);
7249 switch (m)
7251 case MATCH_YES:
7252 if (!gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus))
7253 return MATCH_ERROR;
7254 goto next_item;
7256 case MATCH_NO:
7257 break;
7259 case MATCH_ERROR:
7260 return MATCH_ERROR;
7263 next_item:
7264 if (gfc_match_eos () == MATCH_YES)
7265 break;
7266 if (gfc_match_char (',') != MATCH_YES)
7267 goto syntax;
7270 return MATCH_YES;
7272 syntax:
7273 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7274 return MATCH_ERROR;
7278 /* Match a module procedure statement. Note that we have to modify
7279 symbols in the parent's namespace because the current one was there
7280 to receive symbols that are in an interface's formal argument list. */
7282 match
7283 gfc_match_modproc (void)
7285 char name[GFC_MAX_SYMBOL_LEN + 1];
7286 gfc_symbol *sym;
7287 match m;
7288 locus old_locus;
7289 gfc_namespace *module_ns;
7290 gfc_interface *old_interface_head, *interface;
7292 if (gfc_state_stack->state != COMP_INTERFACE
7293 || gfc_state_stack->previous == NULL
7294 || current_interface.type == INTERFACE_NAMELESS
7295 || current_interface.type == INTERFACE_ABSTRACT)
7297 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7298 "interface");
7299 return MATCH_ERROR;
7302 module_ns = gfc_current_ns->parent;
7303 for (; module_ns; module_ns = module_ns->parent)
7304 if (module_ns->proc_name->attr.flavor == FL_MODULE
7305 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7306 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7307 && !module_ns->proc_name->attr.contained))
7308 break;
7310 if (module_ns == NULL)
7311 return MATCH_ERROR;
7313 /* Store the current state of the interface. We will need it if we
7314 end up with a syntax error and need to recover. */
7315 old_interface_head = gfc_current_interface_head ();
7317 /* Check if the F2008 optional double colon appears. */
7318 gfc_gobble_whitespace ();
7319 old_locus = gfc_current_locus;
7320 if (gfc_match ("::") == MATCH_YES)
7322 if (!gfc_notify_std (GFC_STD_F2008, "double colon in "
7323 "MODULE PROCEDURE statement at %L", &old_locus))
7324 return MATCH_ERROR;
7326 else
7327 gfc_current_locus = old_locus;
7329 for (;;)
7331 bool last = false;
7332 old_locus = gfc_current_locus;
7334 m = gfc_match_name (name);
7335 if (m == MATCH_NO)
7336 goto syntax;
7337 if (m != MATCH_YES)
7338 return MATCH_ERROR;
7340 /* Check for syntax error before starting to add symbols to the
7341 current namespace. */
7342 if (gfc_match_eos () == MATCH_YES)
7343 last = true;
7345 if (!last && gfc_match_char (',') != MATCH_YES)
7346 goto syntax;
7348 /* Now we're sure the syntax is valid, we process this item
7349 further. */
7350 if (gfc_get_symbol (name, module_ns, &sym))
7351 return MATCH_ERROR;
7353 if (sym->attr.intrinsic)
7355 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7356 "PROCEDURE", &old_locus);
7357 return MATCH_ERROR;
7360 if (sym->attr.proc != PROC_MODULE
7361 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
7362 return MATCH_ERROR;
7364 if (!gfc_add_interface (sym))
7365 return MATCH_ERROR;
7367 sym->attr.mod_proc = 1;
7368 sym->declared_at = old_locus;
7370 if (last)
7371 break;
7374 return MATCH_YES;
7376 syntax:
7377 /* Restore the previous state of the interface. */
7378 interface = gfc_current_interface_head ();
7379 gfc_set_current_interface_head (old_interface_head);
7381 /* Free the new interfaces. */
7382 while (interface != old_interface_head)
7384 gfc_interface *i = interface->next;
7385 free (interface);
7386 interface = i;
7389 /* And issue a syntax error. */
7390 gfc_syntax_error (ST_MODULE_PROC);
7391 return MATCH_ERROR;
7395 /* Check a derived type that is being extended. */
7397 static gfc_symbol*
7398 check_extended_derived_type (char *name)
7400 gfc_symbol *extended;
7402 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7404 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7405 return NULL;
7408 extended = gfc_find_dt_in_generic (extended);
7410 /* F08:C428. */
7411 if (!extended)
7413 gfc_error ("Symbol '%s' at %C has not been previously defined", name);
7414 return NULL;
7417 if (extended->attr.flavor != FL_DERIVED)
7419 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7420 "derived type", name);
7421 return NULL;
7424 if (extended->attr.is_bind_c)
7426 gfc_error ("'%s' cannot be extended at %C because it "
7427 "is BIND(C)", extended->name);
7428 return NULL;
7431 if (extended->attr.sequence)
7433 gfc_error ("'%s' cannot be extended at %C because it "
7434 "is a SEQUENCE type", extended->name);
7435 return NULL;
7438 return extended;
7442 /* Match the optional attribute specifiers for a type declaration.
7443 Return MATCH_ERROR if an error is encountered in one of the handled
7444 attributes (public, private, bind(c)), MATCH_NO if what's found is
7445 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7446 checking on attribute conflicts needs to be done. */
7448 match
7449 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7451 /* See if the derived type is marked as private. */
7452 if (gfc_match (" , private") == MATCH_YES)
7454 if (gfc_current_state () != COMP_MODULE)
7456 gfc_error ("Derived type at %C can only be PRIVATE in the "
7457 "specification part of a module");
7458 return MATCH_ERROR;
7461 if (!gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL))
7462 return MATCH_ERROR;
7464 else if (gfc_match (" , public") == MATCH_YES)
7466 if (gfc_current_state () != COMP_MODULE)
7468 gfc_error ("Derived type at %C can only be PUBLIC in the "
7469 "specification part of a module");
7470 return MATCH_ERROR;
7473 if (!gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL))
7474 return MATCH_ERROR;
7476 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7478 /* If the type is defined to be bind(c) it then needs to make
7479 sure that all fields are interoperable. This will
7480 need to be a semantic check on the finished derived type.
7481 See 15.2.3 (lines 9-12) of F2003 draft. */
7482 if (!gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0))
7483 return MATCH_ERROR;
7485 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7487 else if (gfc_match (" , abstract") == MATCH_YES)
7489 if (!gfc_notify_std (GFC_STD_F2003, "ABSTRACT type at %C"))
7490 return MATCH_ERROR;
7492 if (!gfc_add_abstract (attr, &gfc_current_locus))
7493 return MATCH_ERROR;
7495 else if (name && gfc_match (" , extends ( %n )", name) == MATCH_YES)
7497 if (!gfc_add_extension (attr, &gfc_current_locus))
7498 return MATCH_ERROR;
7500 else
7501 return MATCH_NO;
7503 /* If we get here, something matched. */
7504 return MATCH_YES;
7508 /* Match the beginning of a derived type declaration. If a type name
7509 was the result of a function, then it is possible to have a symbol
7510 already to be known as a derived type yet have no components. */
7512 match
7513 gfc_match_derived_decl (void)
7515 char name[GFC_MAX_SYMBOL_LEN + 1];
7516 char parent[GFC_MAX_SYMBOL_LEN + 1];
7517 symbol_attribute attr;
7518 gfc_symbol *sym, *gensym;
7519 gfc_symbol *extended;
7520 match m;
7521 match is_type_attr_spec = MATCH_NO;
7522 bool seen_attr = false;
7523 gfc_interface *intr = NULL, *head;
7525 if (gfc_current_state () == COMP_DERIVED)
7526 return MATCH_NO;
7528 name[0] = '\0';
7529 parent[0] = '\0';
7530 gfc_clear_attr (&attr);
7531 extended = NULL;
7535 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7536 if (is_type_attr_spec == MATCH_ERROR)
7537 return MATCH_ERROR;
7538 if (is_type_attr_spec == MATCH_YES)
7539 seen_attr = true;
7540 } while (is_type_attr_spec == MATCH_YES);
7542 /* Deal with derived type extensions. The extension attribute has
7543 been added to 'attr' but now the parent type must be found and
7544 checked. */
7545 if (parent[0])
7546 extended = check_extended_derived_type (parent);
7548 if (parent[0] && !extended)
7549 return MATCH_ERROR;
7551 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7553 gfc_error ("Expected :: in TYPE definition at %C");
7554 return MATCH_ERROR;
7557 m = gfc_match (" %n%t", name);
7558 if (m != MATCH_YES)
7559 return m;
7561 /* Make sure the name is not the name of an intrinsic type. */
7562 if (gfc_is_intrinsic_typename (name))
7564 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7565 "type", name);
7566 return MATCH_ERROR;
7569 if (gfc_get_symbol (name, NULL, &gensym))
7570 return MATCH_ERROR;
7572 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
7574 gfc_error ("Derived type name '%s' at %C already has a basic type "
7575 "of %s", gensym->name, gfc_typename (&gensym->ts));
7576 return MATCH_ERROR;
7579 if (!gensym->attr.generic
7580 && !gfc_add_generic (&gensym->attr, gensym->name, NULL))
7581 return MATCH_ERROR;
7583 if (!gensym->attr.function
7584 && !gfc_add_function (&gensym->attr, gensym->name, NULL))
7585 return MATCH_ERROR;
7587 sym = gfc_find_dt_in_generic (gensym);
7589 if (sym && (sym->components != NULL || sym->attr.zero_comp))
7591 gfc_error ("Derived type definition of '%s' at %C has already been "
7592 "defined", sym->name);
7593 return MATCH_ERROR;
7596 if (!sym)
7598 /* Use upper case to save the actual derived-type symbol. */
7599 gfc_get_symbol (gfc_get_string ("%c%s",
7600 (char) TOUPPER ((unsigned char) gensym->name[0]),
7601 &gensym->name[1]), NULL, &sym);
7602 sym->name = gfc_get_string (gensym->name);
7603 head = gensym->generic;
7604 intr = gfc_get_interface ();
7605 intr->sym = sym;
7606 intr->where = gfc_current_locus;
7607 intr->sym->declared_at = gfc_current_locus;
7608 intr->next = head;
7609 gensym->generic = intr;
7610 gensym->attr.if_source = IFSRC_DECL;
7613 /* The symbol may already have the derived attribute without the
7614 components. The ways this can happen is via a function
7615 definition, an INTRINSIC statement or a subtype in another
7616 derived type that is a pointer. The first part of the AND clause
7617 is true if the symbol is not the return value of a function. */
7618 if (sym->attr.flavor != FL_DERIVED
7619 && !gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL))
7620 return MATCH_ERROR;
7622 if (attr.access != ACCESS_UNKNOWN
7623 && !gfc_add_access (&sym->attr, attr.access, sym->name, NULL))
7624 return MATCH_ERROR;
7625 else if (sym->attr.access == ACCESS_UNKNOWN
7626 && gensym->attr.access != ACCESS_UNKNOWN
7627 && !gfc_add_access (&sym->attr, gensym->attr.access,
7628 sym->name, NULL))
7629 return MATCH_ERROR;
7631 if (sym->attr.access != ACCESS_UNKNOWN
7632 && gensym->attr.access == ACCESS_UNKNOWN)
7633 gensym->attr.access = sym->attr.access;
7635 /* See if the derived type was labeled as bind(c). */
7636 if (attr.is_bind_c != 0)
7637 sym->attr.is_bind_c = attr.is_bind_c;
7639 /* Construct the f2k_derived namespace if it is not yet there. */
7640 if (!sym->f2k_derived)
7641 sym->f2k_derived = gfc_get_namespace (NULL, 0);
7643 if (extended && !sym->components)
7645 gfc_component *p;
7646 gfc_symtree *st;
7648 /* Add the extended derived type as the first component. */
7649 gfc_add_component (sym, parent, &p);
7650 extended->refs++;
7651 gfc_set_sym_referenced (extended);
7653 p->ts.type = BT_DERIVED;
7654 p->ts.u.derived = extended;
7655 p->initializer = gfc_default_initializer (&p->ts);
7657 /* Set extension level. */
7658 if (extended->attr.extension == 255)
7660 /* Since the extension field is 8 bit wide, we can only have
7661 up to 255 extension levels. */
7662 gfc_error ("Maximum extension level reached with type '%s' at %L",
7663 extended->name, &extended->declared_at);
7664 return MATCH_ERROR;
7666 sym->attr.extension = extended->attr.extension + 1;
7668 /* Provide the links between the extended type and its extension. */
7669 if (!extended->f2k_derived)
7670 extended->f2k_derived = gfc_get_namespace (NULL, 0);
7671 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7672 st->n.sym = sym;
7675 if (!sym->hash_value)
7676 /* Set the hash for the compound name for this type. */
7677 sym->hash_value = gfc_hash_value (sym);
7679 /* Take over the ABSTRACT attribute. */
7680 sym->attr.abstract = attr.abstract;
7682 gfc_new_block = sym;
7684 return MATCH_YES;
7688 /* Cray Pointees can be declared as:
7689 pointer (ipt, a (n,m,...,*)) */
7691 match
7692 gfc_mod_pointee_as (gfc_array_spec *as)
7694 as->cray_pointee = true; /* This will be useful to know later. */
7695 if (as->type == AS_ASSUMED_SIZE)
7696 as->cp_was_assumed = true;
7697 else if (as->type == AS_ASSUMED_SHAPE)
7699 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7700 return MATCH_ERROR;
7702 return MATCH_YES;
7706 /* Match the enum definition statement, here we are trying to match
7707 the first line of enum definition statement.
7708 Returns MATCH_YES if match is found. */
7710 match
7711 gfc_match_enum (void)
7713 match m;
7715 m = gfc_match_eos ();
7716 if (m != MATCH_YES)
7717 return m;
7719 if (!gfc_notify_std (GFC_STD_F2003, "ENUM and ENUMERATOR at %C"))
7720 return MATCH_ERROR;
7722 return MATCH_YES;
7726 /* Returns an initializer whose value is one higher than the value of the
7727 LAST_INITIALIZER argument. If the argument is NULL, the
7728 initializers value will be set to zero. The initializer's kind
7729 will be set to gfc_c_int_kind.
7731 If -fshort-enums is given, the appropriate kind will be selected
7732 later after all enumerators have been parsed. A warning is issued
7733 here if an initializer exceeds gfc_c_int_kind. */
7735 static gfc_expr *
7736 enum_initializer (gfc_expr *last_initializer, locus where)
7738 gfc_expr *result;
7739 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7741 mpz_init (result->value.integer);
7743 if (last_initializer != NULL)
7745 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7746 result->where = last_initializer->where;
7748 if (gfc_check_integer_range (result->value.integer,
7749 gfc_c_int_kind) != ARITH_OK)
7751 gfc_error ("Enumerator exceeds the C integer type at %C");
7752 return NULL;
7755 else
7757 /* Control comes here, if it's the very first enumerator and no
7758 initializer has been given. It will be initialized to zero. */
7759 mpz_set_si (result->value.integer, 0);
7762 return result;
7766 /* Match a variable name with an optional initializer. When this
7767 subroutine is called, a variable is expected to be parsed next.
7768 Depending on what is happening at the moment, updates either the
7769 symbol table or the current interface. */
7771 static match
7772 enumerator_decl (void)
7774 char name[GFC_MAX_SYMBOL_LEN + 1];
7775 gfc_expr *initializer;
7776 gfc_array_spec *as = NULL;
7777 gfc_symbol *sym;
7778 locus var_locus;
7779 match m;
7780 bool t;
7781 locus old_locus;
7783 initializer = NULL;
7784 old_locus = gfc_current_locus;
7786 /* When we get here, we've just matched a list of attributes and
7787 maybe a type and a double colon. The next thing we expect to see
7788 is the name of the symbol. */
7789 m = gfc_match_name (name);
7790 if (m != MATCH_YES)
7791 goto cleanup;
7793 var_locus = gfc_current_locus;
7795 /* OK, we've successfully matched the declaration. Now put the
7796 symbol in the current namespace. If we fail to create the symbol,
7797 bail out. */
7798 if (!build_sym (name, NULL, false, &as, &var_locus))
7800 m = MATCH_ERROR;
7801 goto cleanup;
7804 /* The double colon must be present in order to have initializers.
7805 Otherwise the statement is ambiguous with an assignment statement. */
7806 if (colon_seen)
7808 if (gfc_match_char ('=') == MATCH_YES)
7810 m = gfc_match_init_expr (&initializer);
7811 if (m == MATCH_NO)
7813 gfc_error ("Expected an initialization expression at %C");
7814 m = MATCH_ERROR;
7817 if (m != MATCH_YES)
7818 goto cleanup;
7822 /* If we do not have an initializer, the initialization value of the
7823 previous enumerator (stored in last_initializer) is incremented
7824 by 1 and is used to initialize the current enumerator. */
7825 if (initializer == NULL)
7826 initializer = enum_initializer (last_initializer, old_locus);
7828 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7830 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7831 &var_locus);
7832 m = MATCH_ERROR;
7833 goto cleanup;
7836 /* Store this current initializer, for the next enumerator variable
7837 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7838 use last_initializer below. */
7839 last_initializer = initializer;
7840 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7842 /* Maintain enumerator history. */
7843 gfc_find_symbol (name, NULL, 0, &sym);
7844 create_enum_history (sym, last_initializer);
7846 return (t) ? MATCH_YES : MATCH_ERROR;
7848 cleanup:
7849 /* Free stuff up and return. */
7850 gfc_free_expr (initializer);
7852 return m;
7856 /* Match the enumerator definition statement. */
7858 match
7859 gfc_match_enumerator_def (void)
7861 match m;
7862 bool t;
7864 gfc_clear_ts (&current_ts);
7866 m = gfc_match (" enumerator");
7867 if (m != MATCH_YES)
7868 return m;
7870 m = gfc_match (" :: ");
7871 if (m == MATCH_ERROR)
7872 return m;
7874 colon_seen = (m == MATCH_YES);
7876 if (gfc_current_state () != COMP_ENUM)
7878 gfc_error ("ENUM definition statement expected before %C");
7879 gfc_free_enum_history ();
7880 return MATCH_ERROR;
7883 (&current_ts)->type = BT_INTEGER;
7884 (&current_ts)->kind = gfc_c_int_kind;
7886 gfc_clear_attr (&current_attr);
7887 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
7888 if (!t)
7890 m = MATCH_ERROR;
7891 goto cleanup;
7894 for (;;)
7896 m = enumerator_decl ();
7897 if (m == MATCH_ERROR)
7899 gfc_free_enum_history ();
7900 goto cleanup;
7902 if (m == MATCH_NO)
7903 break;
7905 if (gfc_match_eos () == MATCH_YES)
7906 goto cleanup;
7907 if (gfc_match_char (',') != MATCH_YES)
7908 break;
7911 if (gfc_current_state () == COMP_ENUM)
7913 gfc_free_enum_history ();
7914 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7915 m = MATCH_ERROR;
7918 cleanup:
7919 gfc_free_array_spec (current_as);
7920 current_as = NULL;
7921 return m;
7926 /* Match binding attributes. */
7928 static match
7929 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7931 bool found_passing = false;
7932 bool seen_ptr = false;
7933 match m = MATCH_YES;
7935 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
7936 this case the defaults are in there. */
7937 ba->access = ACCESS_UNKNOWN;
7938 ba->pass_arg = NULL;
7939 ba->pass_arg_num = 0;
7940 ba->nopass = 0;
7941 ba->non_overridable = 0;
7942 ba->deferred = 0;
7943 ba->ppc = ppc;
7945 /* If we find a comma, we believe there are binding attributes. */
7946 m = gfc_match_char (',');
7947 if (m == MATCH_NO)
7948 goto done;
7952 /* Access specifier. */
7954 m = gfc_match (" public");
7955 if (m == MATCH_ERROR)
7956 goto error;
7957 if (m == MATCH_YES)
7959 if (ba->access != ACCESS_UNKNOWN)
7961 gfc_error ("Duplicate access-specifier at %C");
7962 goto error;
7965 ba->access = ACCESS_PUBLIC;
7966 continue;
7969 m = gfc_match (" private");
7970 if (m == MATCH_ERROR)
7971 goto error;
7972 if (m == MATCH_YES)
7974 if (ba->access != ACCESS_UNKNOWN)
7976 gfc_error ("Duplicate access-specifier at %C");
7977 goto error;
7980 ba->access = ACCESS_PRIVATE;
7981 continue;
7984 /* If inside GENERIC, the following is not allowed. */
7985 if (!generic)
7988 /* NOPASS flag. */
7989 m = gfc_match (" nopass");
7990 if (m == MATCH_ERROR)
7991 goto error;
7992 if (m == MATCH_YES)
7994 if (found_passing)
7996 gfc_error ("Binding attributes already specify passing,"
7997 " illegal NOPASS at %C");
7998 goto error;
8001 found_passing = true;
8002 ba->nopass = 1;
8003 continue;
8006 /* PASS possibly including argument. */
8007 m = gfc_match (" pass");
8008 if (m == MATCH_ERROR)
8009 goto error;
8010 if (m == MATCH_YES)
8012 char arg[GFC_MAX_SYMBOL_LEN + 1];
8014 if (found_passing)
8016 gfc_error ("Binding attributes already specify passing,"
8017 " illegal PASS at %C");
8018 goto error;
8021 m = gfc_match (" ( %n )", arg);
8022 if (m == MATCH_ERROR)
8023 goto error;
8024 if (m == MATCH_YES)
8025 ba->pass_arg = gfc_get_string (arg);
8026 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
8028 found_passing = true;
8029 ba->nopass = 0;
8030 continue;
8033 if (ppc)
8035 /* POINTER flag. */
8036 m = gfc_match (" pointer");
8037 if (m == MATCH_ERROR)
8038 goto error;
8039 if (m == MATCH_YES)
8041 if (seen_ptr)
8043 gfc_error ("Duplicate POINTER attribute at %C");
8044 goto error;
8047 seen_ptr = true;
8048 continue;
8051 else
8053 /* NON_OVERRIDABLE flag. */
8054 m = gfc_match (" non_overridable");
8055 if (m == MATCH_ERROR)
8056 goto error;
8057 if (m == MATCH_YES)
8059 if (ba->non_overridable)
8061 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8062 goto error;
8065 ba->non_overridable = 1;
8066 continue;
8069 /* DEFERRED flag. */
8070 m = gfc_match (" deferred");
8071 if (m == MATCH_ERROR)
8072 goto error;
8073 if (m == MATCH_YES)
8075 if (ba->deferred)
8077 gfc_error ("Duplicate DEFERRED at %C");
8078 goto error;
8081 ba->deferred = 1;
8082 continue;
8088 /* Nothing matching found. */
8089 if (generic)
8090 gfc_error ("Expected access-specifier at %C");
8091 else
8092 gfc_error ("Expected binding attribute at %C");
8093 goto error;
8095 while (gfc_match_char (',') == MATCH_YES);
8097 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8098 if (ba->non_overridable && ba->deferred)
8100 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8101 goto error;
8104 m = MATCH_YES;
8106 done:
8107 if (ba->access == ACCESS_UNKNOWN)
8108 ba->access = gfc_typebound_default_access;
8110 if (ppc && !seen_ptr)
8112 gfc_error ("POINTER attribute is required for procedure pointer component"
8113 " at %C");
8114 goto error;
8117 return m;
8119 error:
8120 return MATCH_ERROR;
8124 /* Match a PROCEDURE specific binding inside a derived type. */
8126 static match
8127 match_procedure_in_type (void)
8129 char name[GFC_MAX_SYMBOL_LEN + 1];
8130 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
8131 char* target = NULL, *ifc = NULL;
8132 gfc_typebound_proc tb;
8133 bool seen_colons;
8134 bool seen_attrs;
8135 match m;
8136 gfc_symtree* stree;
8137 gfc_namespace* ns;
8138 gfc_symbol* block;
8139 int num;
8141 /* Check current state. */
8142 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8143 block = gfc_state_stack->previous->sym;
8144 gcc_assert (block);
8146 /* Try to match PROCEDURE(interface). */
8147 if (gfc_match (" (") == MATCH_YES)
8149 m = gfc_match_name (target_buf);
8150 if (m == MATCH_ERROR)
8151 return m;
8152 if (m != MATCH_YES)
8154 gfc_error ("Interface-name expected after '(' at %C");
8155 return MATCH_ERROR;
8158 if (gfc_match (" )") != MATCH_YES)
8160 gfc_error ("')' expected at %C");
8161 return MATCH_ERROR;
8164 ifc = target_buf;
8167 /* Construct the data structure. */
8168 memset (&tb, 0, sizeof (tb));
8169 tb.where = gfc_current_locus;
8171 /* Match binding attributes. */
8172 m = match_binding_attributes (&tb, false, false);
8173 if (m == MATCH_ERROR)
8174 return m;
8175 seen_attrs = (m == MATCH_YES);
8177 /* Check that attribute DEFERRED is given if an interface is specified. */
8178 if (tb.deferred && !ifc)
8180 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8181 return MATCH_ERROR;
8183 if (ifc && !tb.deferred)
8185 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8186 return MATCH_ERROR;
8189 /* Match the colons. */
8190 m = gfc_match (" ::");
8191 if (m == MATCH_ERROR)
8192 return m;
8193 seen_colons = (m == MATCH_YES);
8194 if (seen_attrs && !seen_colons)
8196 gfc_error ("Expected '::' after binding-attributes at %C");
8197 return MATCH_ERROR;
8200 /* Match the binding names. */
8201 for(num=1;;num++)
8203 m = gfc_match_name (name);
8204 if (m == MATCH_ERROR)
8205 return m;
8206 if (m == MATCH_NO)
8208 gfc_error ("Expected binding name at %C");
8209 return MATCH_ERROR;
8212 if (num>1 && !gfc_notify_std (GFC_STD_F2008, "PROCEDURE list at %C"))
8213 return MATCH_ERROR;
8215 /* Try to match the '=> target', if it's there. */
8216 target = ifc;
8217 m = gfc_match (" =>");
8218 if (m == MATCH_ERROR)
8219 return m;
8220 if (m == MATCH_YES)
8222 if (tb.deferred)
8224 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
8225 return MATCH_ERROR;
8228 if (!seen_colons)
8230 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
8231 " at %C");
8232 return MATCH_ERROR;
8235 m = gfc_match_name (target_buf);
8236 if (m == MATCH_ERROR)
8237 return m;
8238 if (m == MATCH_NO)
8240 gfc_error ("Expected binding target after '=>' at %C");
8241 return MATCH_ERROR;
8243 target = target_buf;
8246 /* If no target was found, it has the same name as the binding. */
8247 if (!target)
8248 target = name;
8250 /* Get the namespace to insert the symbols into. */
8251 ns = block->f2k_derived;
8252 gcc_assert (ns);
8254 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8255 if (tb.deferred && !block->attr.abstract)
8257 gfc_error ("Type '%s' containing DEFERRED binding at %C "
8258 "is not ABSTRACT", block->name);
8259 return MATCH_ERROR;
8262 /* See if we already have a binding with this name in the symtree which
8263 would be an error. If a GENERIC already targeted this binding, it may
8264 be already there but then typebound is still NULL. */
8265 stree = gfc_find_symtree (ns->tb_sym_root, name);
8266 if (stree && stree->n.tb)
8268 gfc_error ("There is already a procedure with binding name '%s' for "
8269 "the derived type '%s' at %C", name, block->name);
8270 return MATCH_ERROR;
8273 /* Insert it and set attributes. */
8275 if (!stree)
8277 stree = gfc_new_symtree (&ns->tb_sym_root, name);
8278 gcc_assert (stree);
8280 stree->n.tb = gfc_get_typebound_proc (&tb);
8282 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8283 false))
8284 return MATCH_ERROR;
8285 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
8287 if (gfc_match_eos () == MATCH_YES)
8288 return MATCH_YES;
8289 if (gfc_match_char (',') != MATCH_YES)
8290 goto syntax;
8293 syntax:
8294 gfc_error ("Syntax error in PROCEDURE statement at %C");
8295 return MATCH_ERROR;
8299 /* Match a GENERIC procedure binding inside a derived type. */
8301 match
8302 gfc_match_generic (void)
8304 char name[GFC_MAX_SYMBOL_LEN + 1];
8305 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
8306 gfc_symbol* block;
8307 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8308 gfc_typebound_proc* tb;
8309 gfc_namespace* ns;
8310 interface_type op_type;
8311 gfc_intrinsic_op op;
8312 match m;
8314 /* Check current state. */
8315 if (gfc_current_state () == COMP_DERIVED)
8317 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8318 return MATCH_ERROR;
8320 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8321 return MATCH_NO;
8322 block = gfc_state_stack->previous->sym;
8323 ns = block->f2k_derived;
8324 gcc_assert (block && ns);
8326 memset (&tbattr, 0, sizeof (tbattr));
8327 tbattr.where = gfc_current_locus;
8329 /* See if we get an access-specifier. */
8330 m = match_binding_attributes (&tbattr, true, false);
8331 if (m == MATCH_ERROR)
8332 goto error;
8334 /* Now the colons, those are required. */
8335 if (gfc_match (" ::") != MATCH_YES)
8337 gfc_error ("Expected '::' at %C");
8338 goto error;
8341 /* Match the binding name; depending on type (operator / generic) format
8342 it for future error messages into bind_name. */
8344 m = gfc_match_generic_spec (&op_type, name, &op);
8345 if (m == MATCH_ERROR)
8346 return MATCH_ERROR;
8347 if (m == MATCH_NO)
8349 gfc_error ("Expected generic name or operator descriptor at %C");
8350 goto error;
8353 switch (op_type)
8355 case INTERFACE_GENERIC:
8356 snprintf (bind_name, sizeof (bind_name), "%s", name);
8357 break;
8359 case INTERFACE_USER_OP:
8360 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8361 break;
8363 case INTERFACE_INTRINSIC_OP:
8364 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8365 gfc_op2string (op));
8366 break;
8368 default:
8369 gcc_unreachable ();
8372 /* Match the required =>. */
8373 if (gfc_match (" =>") != MATCH_YES)
8375 gfc_error ("Expected '=>' at %C");
8376 goto error;
8379 /* Try to find existing GENERIC binding with this name / for this operator;
8380 if there is something, check that it is another GENERIC and then extend
8381 it rather than building a new node. Otherwise, create it and put it
8382 at the right position. */
8384 switch (op_type)
8386 case INTERFACE_USER_OP:
8387 case INTERFACE_GENERIC:
8389 const bool is_op = (op_type == INTERFACE_USER_OP);
8390 gfc_symtree* st;
8392 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8393 if (st)
8395 tb = st->n.tb;
8396 gcc_assert (tb);
8398 else
8399 tb = NULL;
8401 break;
8404 case INTERFACE_INTRINSIC_OP:
8405 tb = ns->tb_op[op];
8406 break;
8408 default:
8409 gcc_unreachable ();
8412 if (tb)
8414 if (!tb->is_generic)
8416 gcc_assert (op_type == INTERFACE_GENERIC);
8417 gfc_error ("There's already a non-generic procedure with binding name"
8418 " '%s' for the derived type '%s' at %C",
8419 bind_name, block->name);
8420 goto error;
8423 if (tb->access != tbattr.access)
8425 gfc_error ("Binding at %C must have the same access as already"
8426 " defined binding '%s'", bind_name);
8427 goto error;
8430 else
8432 tb = gfc_get_typebound_proc (NULL);
8433 tb->where = gfc_current_locus;
8434 tb->access = tbattr.access;
8435 tb->is_generic = 1;
8436 tb->u.generic = NULL;
8438 switch (op_type)
8440 case INTERFACE_GENERIC:
8441 case INTERFACE_USER_OP:
8443 const bool is_op = (op_type == INTERFACE_USER_OP);
8444 gfc_symtree* st;
8446 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8447 name);
8448 gcc_assert (st);
8449 st->n.tb = tb;
8451 break;
8454 case INTERFACE_INTRINSIC_OP:
8455 ns->tb_op[op] = tb;
8456 break;
8458 default:
8459 gcc_unreachable ();
8463 /* Now, match all following names as specific targets. */
8466 gfc_symtree* target_st;
8467 gfc_tbp_generic* target;
8469 m = gfc_match_name (name);
8470 if (m == MATCH_ERROR)
8471 goto error;
8472 if (m == MATCH_NO)
8474 gfc_error ("Expected specific binding name at %C");
8475 goto error;
8478 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8480 /* See if this is a duplicate specification. */
8481 for (target = tb->u.generic; target; target = target->next)
8482 if (target_st == target->specific_st)
8484 gfc_error ("'%s' already defined as specific binding for the"
8485 " generic '%s' at %C", name, bind_name);
8486 goto error;
8489 target = gfc_get_tbp_generic ();
8490 target->specific_st = target_st;
8491 target->specific = NULL;
8492 target->next = tb->u.generic;
8493 target->is_operator = ((op_type == INTERFACE_USER_OP)
8494 || (op_type == INTERFACE_INTRINSIC_OP));
8495 tb->u.generic = target;
8497 while (gfc_match (" ,") == MATCH_YES);
8499 /* Here should be the end. */
8500 if (gfc_match_eos () != MATCH_YES)
8502 gfc_error ("Junk after GENERIC binding at %C");
8503 goto error;
8506 return MATCH_YES;
8508 error:
8509 return MATCH_ERROR;
8513 /* Match a FINAL declaration inside a derived type. */
8515 match
8516 gfc_match_final_decl (void)
8518 char name[GFC_MAX_SYMBOL_LEN + 1];
8519 gfc_symbol* sym;
8520 match m;
8521 gfc_namespace* module_ns;
8522 bool first, last;
8523 gfc_symbol* block;
8525 if (gfc_current_form == FORM_FREE)
8527 char c = gfc_peek_ascii_char ();
8528 if (!gfc_is_whitespace (c) && c != ':')
8529 return MATCH_NO;
8532 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8534 if (gfc_current_form == FORM_FIXED)
8535 return MATCH_NO;
8537 gfc_error ("FINAL declaration at %C must be inside a derived type "
8538 "CONTAINS section");
8539 return MATCH_ERROR;
8542 block = gfc_state_stack->previous->sym;
8543 gcc_assert (block);
8545 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8546 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8548 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8549 " specification part of a MODULE");
8550 return MATCH_ERROR;
8553 module_ns = gfc_current_ns;
8554 gcc_assert (module_ns);
8555 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8557 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8558 if (gfc_match (" ::") == MATCH_ERROR)
8559 return MATCH_ERROR;
8561 /* Match the sequence of procedure names. */
8562 first = true;
8563 last = false;
8566 gfc_finalizer* f;
8568 if (first && gfc_match_eos () == MATCH_YES)
8570 gfc_error ("Empty FINAL at %C");
8571 return MATCH_ERROR;
8574 m = gfc_match_name (name);
8575 if (m == MATCH_NO)
8577 gfc_error ("Expected module procedure name at %C");
8578 return MATCH_ERROR;
8580 else if (m != MATCH_YES)
8581 return MATCH_ERROR;
8583 if (gfc_match_eos () == MATCH_YES)
8584 last = true;
8585 if (!last && gfc_match_char (',') != MATCH_YES)
8587 gfc_error ("Expected ',' at %C");
8588 return MATCH_ERROR;
8591 if (gfc_get_symbol (name, module_ns, &sym))
8593 gfc_error ("Unknown procedure name \"%s\" at %C", name);
8594 return MATCH_ERROR;
8597 /* Mark the symbol as module procedure. */
8598 if (sym->attr.proc != PROC_MODULE
8599 && !gfc_add_procedure (&sym->attr, PROC_MODULE, sym->name, NULL))
8600 return MATCH_ERROR;
8602 /* Check if we already have this symbol in the list, this is an error. */
8603 for (f = block->f2k_derived->finalizers; f; f = f->next)
8604 if (f->proc_sym == sym)
8606 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8607 name);
8608 return MATCH_ERROR;
8611 /* Add this symbol to the list of finalizers. */
8612 gcc_assert (block->f2k_derived);
8613 ++sym->refs;
8614 f = XCNEW (gfc_finalizer);
8615 f->proc_sym = sym;
8616 f->proc_tree = NULL;
8617 f->where = gfc_current_locus;
8618 f->next = block->f2k_derived->finalizers;
8619 block->f2k_derived->finalizers = f;
8621 first = false;
8623 while (!last);
8625 return MATCH_YES;
8629 const ext_attr_t ext_attr_list[] = {
8630 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8631 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8632 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
8633 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
8634 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
8635 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK, NULL },
8636 { NULL, EXT_ATTR_LAST, NULL }
8639 /* Match a !GCC$ ATTRIBUTES statement of the form:
8640 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8641 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8643 TODO: We should support all GCC attributes using the same syntax for
8644 the attribute list, i.e. the list in C
8645 __attributes(( attribute-list ))
8646 matches then
8647 !GCC$ ATTRIBUTES attribute-list ::
8648 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8649 saved into a TREE.
8651 As there is absolutely no risk of confusion, we should never return
8652 MATCH_NO. */
8653 match
8654 gfc_match_gcc_attributes (void)
8656 symbol_attribute attr;
8657 char name[GFC_MAX_SYMBOL_LEN + 1];
8658 unsigned id;
8659 gfc_symbol *sym;
8660 match m;
8662 gfc_clear_attr (&attr);
8663 for(;;)
8665 char ch;
8667 if (gfc_match_name (name) != MATCH_YES)
8668 return MATCH_ERROR;
8670 for (id = 0; id < EXT_ATTR_LAST; id++)
8671 if (strcmp (name, ext_attr_list[id].name) == 0)
8672 break;
8674 if (id == EXT_ATTR_LAST)
8676 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8677 return MATCH_ERROR;
8680 if (!gfc_add_ext_attribute (&attr, (ext_attr_id_t)id, &gfc_current_locus))
8681 return MATCH_ERROR;
8683 gfc_gobble_whitespace ();
8684 ch = gfc_next_ascii_char ();
8685 if (ch == ':')
8687 /* This is the successful exit condition for the loop. */
8688 if (gfc_next_ascii_char () == ':')
8689 break;
8692 if (ch == ',')
8693 continue;
8695 goto syntax;
8698 if (gfc_match_eos () == MATCH_YES)
8699 goto syntax;
8701 for(;;)
8703 m = gfc_match_name (name);
8704 if (m != MATCH_YES)
8705 return m;
8707 if (find_special (name, &sym, true))
8708 return MATCH_ERROR;
8710 sym->attr.ext_attr |= attr.ext_attr;
8712 if (gfc_match_eos () == MATCH_YES)
8713 break;
8715 if (gfc_match_char (',') != MATCH_YES)
8716 goto syntax;
8719 return MATCH_YES;
8721 syntax:
8722 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
8723 return MATCH_ERROR;