2012-06-13 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / fortran / decl.c
blob26b5059cd9f17cace9fe39543986df0de38031ef
1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
11 version.
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 for more details.
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 #include "config.h"
23 #include "system.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"
31 /* Macros to access allocate memory for gfc_data_variable,
32 gfc_data_value and gfc_data. */
33 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
34 #define gfc_get_data_value() XCNEW (gfc_data_value)
35 #define gfc_get_data() XCNEW (gfc_data)
38 static gfc_try set_binding_label (const char **, const char *, int);
41 /* This flag is set if an old-style length selector is matched
42 during a type-declaration statement. */
44 static int old_char_selector;
46 /* When variables acquire types and attributes from a declaration
47 statement, they get them from the following static variables. The
48 first part of a declaration sets these variables and the second
49 part copies these into symbol structures. */
51 static gfc_typespec current_ts;
53 static symbol_attribute current_attr;
54 static gfc_array_spec *current_as;
55 static int colon_seen;
57 /* The current binding label (if any). */
58 static const char* curr_binding_label;
59 /* Need to know how many identifiers are on the current data declaration
60 line in case we're given the BIND(C) attribute with a NAME= specifier. */
61 static int num_idents_on_line;
62 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
63 can supply a name if the curr_binding_label is nil and NAME= was not. */
64 static int has_name_equals = 0;
66 /* Initializer of the previous enumerator. */
68 static gfc_expr *last_initializer;
70 /* History of all the enumerators is maintained, so that
71 kind values of all the enumerators could be updated depending
72 upon the maximum initialized value. */
74 typedef struct enumerator_history
76 gfc_symbol *sym;
77 gfc_expr *initializer;
78 struct enumerator_history *next;
80 enumerator_history;
82 /* Header of enum history chain. */
84 static enumerator_history *enum_history = NULL;
86 /* Pointer of enum history node containing largest initializer. */
88 static enumerator_history *max_enum = NULL;
90 /* gfc_new_block points to the symbol of a newly matched block. */
92 gfc_symbol *gfc_new_block;
94 bool gfc_matching_function;
97 /********************* DATA statement subroutines *********************/
99 static bool in_match_data = false;
101 bool
102 gfc_in_match_data (void)
104 return in_match_data;
107 static void
108 set_in_match_data (bool set_value)
110 in_match_data = set_value;
113 /* Free a gfc_data_variable structure and everything beneath it. */
115 static void
116 free_variable (gfc_data_variable *p)
118 gfc_data_variable *q;
120 for (; p; p = q)
122 q = p->next;
123 gfc_free_expr (p->expr);
124 gfc_free_iterator (&p->iter, 0);
125 free_variable (p->list);
126 free (p);
131 /* Free a gfc_data_value structure and everything beneath it. */
133 static void
134 free_value (gfc_data_value *p)
136 gfc_data_value *q;
138 for (; p; p = q)
140 q = p->next;
141 mpz_clear (p->repeat);
142 gfc_free_expr (p->expr);
143 free (p);
148 /* Free a list of gfc_data structures. */
150 void
151 gfc_free_data (gfc_data *p)
153 gfc_data *q;
155 for (; p; p = q)
157 q = p->next;
158 free_variable (p->var);
159 free_value (p->value);
160 free (p);
165 /* Free all data in a namespace. */
167 static void
168 gfc_free_data_all (gfc_namespace *ns)
170 gfc_data *d;
172 for (;ns->data;)
174 d = ns->data->next;
175 free (ns->data);
176 ns->data = d;
181 static match var_element (gfc_data_variable *);
183 /* Match a list of variables terminated by an iterator and a right
184 parenthesis. */
186 static match
187 var_list (gfc_data_variable *parent)
189 gfc_data_variable *tail, var;
190 match m;
192 m = var_element (&var);
193 if (m == MATCH_ERROR)
194 return MATCH_ERROR;
195 if (m == MATCH_NO)
196 goto syntax;
198 tail = gfc_get_data_variable ();
199 *tail = var;
201 parent->list = tail;
203 for (;;)
205 if (gfc_match_char (',') != MATCH_YES)
206 goto syntax;
208 m = gfc_match_iterator (&parent->iter, 1);
209 if (m == MATCH_YES)
210 break;
211 if (m == MATCH_ERROR)
212 return MATCH_ERROR;
214 m = var_element (&var);
215 if (m == MATCH_ERROR)
216 return MATCH_ERROR;
217 if (m == MATCH_NO)
218 goto syntax;
220 tail->next = gfc_get_data_variable ();
221 tail = tail->next;
223 *tail = var;
226 if (gfc_match_char (')') != MATCH_YES)
227 goto syntax;
228 return MATCH_YES;
230 syntax:
231 gfc_syntax_error (ST_DATA);
232 return MATCH_ERROR;
236 /* Match a single element in a data variable list, which can be a
237 variable-iterator list. */
239 static match
240 var_element (gfc_data_variable *new_var)
242 match m;
243 gfc_symbol *sym;
245 memset (new_var, 0, sizeof (gfc_data_variable));
247 if (gfc_match_char ('(') == MATCH_YES)
248 return var_list (new_var);
250 m = gfc_match_variable (&new_var->expr, 0);
251 if (m != MATCH_YES)
252 return m;
254 sym = new_var->expr->symtree->n.sym;
256 /* Symbol should already have an associated type. */
257 if (gfc_check_symbol_typed (sym, gfc_current_ns,
258 false, gfc_current_locus) == FAILURE)
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, "Extension: initialization of "
272 "common block variable '%s' in DATA statement at %C",
273 sym->name) == FAILURE)
274 return MATCH_ERROR;
276 if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
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) == FAILURE)
360 m = MATCH_ERROR;
361 return m;
364 gfc_current_locus = old_loc;
366 m = gfc_match_name (name);
367 if (m != MATCH_YES)
368 return m;
370 if (gfc_find_symbol (name, NULL, 1, &sym))
371 return MATCH_ERROR;
373 if (sym && sym->attr.generic)
374 dt_sym = gfc_find_dt_in_generic (sym);
376 if (sym == NULL
377 || (sym->attr.flavor != FL_PARAMETER
378 && (!dt_sym || dt_sym->attr.flavor != FL_DERIVED)))
380 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
381 name);
382 return MATCH_ERROR;
384 else if (dt_sym && dt_sym->attr.flavor == FL_DERIVED)
385 return gfc_match_structure_constructor (dt_sym, result);
387 /* Check to see if the value is an initialization array expression. */
388 if (sym->value->expr_type == EXPR_ARRAY)
390 gfc_current_locus = old_loc;
392 m = gfc_match_init_expr (result);
393 if (m == MATCH_ERROR)
394 return m;
396 if (m == MATCH_YES)
398 if (gfc_simplify_expr (*result, 0) == FAILURE)
399 m = MATCH_ERROR;
401 if ((*result)->expr_type == EXPR_CONSTANT)
402 return m;
403 else
405 gfc_error ("Invalid initializer %s in Data statement at %C", name);
406 return MATCH_ERROR;
411 *result = gfc_copy_expr (sym->value);
412 return MATCH_YES;
416 /* Match a list of values in a DATA statement. The leading '/' has
417 already been seen at this point. */
419 static match
420 top_val_list (gfc_data *data)
422 gfc_data_value *new_val, *tail;
423 gfc_expr *expr;
424 match m;
426 tail = NULL;
428 for (;;)
430 m = match_data_constant (&expr);
431 if (m == MATCH_NO)
432 goto syntax;
433 if (m == MATCH_ERROR)
434 return MATCH_ERROR;
436 new_val = gfc_get_data_value ();
437 mpz_init (new_val->repeat);
439 if (tail == NULL)
440 data->value = new_val;
441 else
442 tail->next = new_val;
444 tail = new_val;
446 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
448 tail->expr = expr;
449 mpz_set_ui (tail->repeat, 1);
451 else
453 if (expr->ts.type == BT_INTEGER)
454 mpz_set (tail->repeat, expr->value.integer);
455 gfc_free_expr (expr);
457 m = match_data_constant (&tail->expr);
458 if (m == MATCH_NO)
459 goto syntax;
460 if (m == MATCH_ERROR)
461 return MATCH_ERROR;
464 if (gfc_match_char ('/') == MATCH_YES)
465 break;
466 if (gfc_match_char (',') == MATCH_NO)
467 goto syntax;
470 return MATCH_YES;
472 syntax:
473 gfc_syntax_error (ST_DATA);
474 gfc_free_data_all (gfc_current_ns);
475 return MATCH_ERROR;
479 /* Matches an old style initialization. */
481 static match
482 match_old_style_init (const char *name)
484 match m;
485 gfc_symtree *st;
486 gfc_symbol *sym;
487 gfc_data *newdata;
489 /* Set up data structure to hold initializers. */
490 gfc_find_sym_tree (name, NULL, 0, &st);
491 sym = st->n.sym;
493 newdata = gfc_get_data ();
494 newdata->var = gfc_get_data_variable ();
495 newdata->var->expr = gfc_get_variable_expr (st);
496 newdata->where = gfc_current_locus;
498 /* Match initial value list. This also eats the terminal '/'. */
499 m = top_val_list (newdata);
500 if (m != MATCH_YES)
502 free (newdata);
503 return m;
506 if (gfc_pure (NULL))
508 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
509 free (newdata);
510 return MATCH_ERROR;
513 if (gfc_implicit_pure (NULL))
514 gfc_current_ns->proc_name->attr.implicit_pure = 0;
516 /* Mark the variable as having appeared in a data statement. */
517 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
519 free (newdata);
520 return MATCH_ERROR;
523 /* Chain in namespace list of DATA initializers. */
524 newdata->next = gfc_current_ns->data;
525 gfc_current_ns->data = newdata;
527 return m;
531 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
532 we are matching a DATA statement and are therefore issuing an error
533 if we encounter something unexpected, if not, we're trying to match
534 an old-style initialization expression of the form INTEGER I /2/. */
536 match
537 gfc_match_data (void)
539 gfc_data *new_data;
540 match m;
542 set_in_match_data (true);
544 for (;;)
546 new_data = gfc_get_data ();
547 new_data->where = gfc_current_locus;
549 m = top_var_list (new_data);
550 if (m != MATCH_YES)
551 goto cleanup;
553 m = top_val_list (new_data);
554 if (m != MATCH_YES)
555 goto cleanup;
557 new_data->next = gfc_current_ns->data;
558 gfc_current_ns->data = new_data;
560 if (gfc_match_eos () == MATCH_YES)
561 break;
563 gfc_match_char (','); /* Optional comma */
566 set_in_match_data (false);
568 if (gfc_pure (NULL))
570 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
571 return MATCH_ERROR;
574 if (gfc_implicit_pure (NULL))
575 gfc_current_ns->proc_name->attr.implicit_pure = 0;
577 return MATCH_YES;
579 cleanup:
580 set_in_match_data (false);
581 gfc_free_data (new_data);
582 return MATCH_ERROR;
586 /************************ Declaration statements *********************/
589 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
591 static void
592 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
594 int i;
596 if (to->rank == 0 && from->rank > 0)
598 to->rank = from->rank;
599 to->type = from->type;
600 to->cray_pointee = from->cray_pointee;
601 to->cp_was_assumed = from->cp_was_assumed;
603 for (i = 0; i < to->corank; i++)
605 to->lower[from->rank + i] = to->lower[i];
606 to->upper[from->rank + i] = to->upper[i];
608 for (i = 0; i < from->rank; i++)
610 if (copy)
612 to->lower[i] = gfc_copy_expr (from->lower[i]);
613 to->upper[i] = gfc_copy_expr (from->upper[i]);
615 else
617 to->lower[i] = from->lower[i];
618 to->upper[i] = from->upper[i];
622 else if (to->corank == 0 && from->corank > 0)
624 to->corank = from->corank;
625 to->cotype = from->cotype;
627 for (i = 0; i < from->corank; i++)
629 if (copy)
631 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
632 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
634 else
636 to->lower[to->rank + i] = from->lower[i];
637 to->upper[to->rank + i] = from->upper[i];
644 /* Match an intent specification. Since this can only happen after an
645 INTENT word, a legal intent-spec must follow. */
647 static sym_intent
648 match_intent_spec (void)
651 if (gfc_match (" ( in out )") == MATCH_YES)
652 return INTENT_INOUT;
653 if (gfc_match (" ( in )") == MATCH_YES)
654 return INTENT_IN;
655 if (gfc_match (" ( out )") == MATCH_YES)
656 return INTENT_OUT;
658 gfc_error ("Bad INTENT specification at %C");
659 return INTENT_UNKNOWN;
663 /* Matches a character length specification, which is either a
664 specification expression, '*', or ':'. */
666 static match
667 char_len_param_value (gfc_expr **expr, bool *deferred)
669 match m;
671 *expr = NULL;
672 *deferred = false;
674 if (gfc_match_char ('*') == MATCH_YES)
675 return MATCH_YES;
677 if (gfc_match_char (':') == MATCH_YES)
679 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: deferred type "
680 "parameter at %C") == FAILURE)
681 return MATCH_ERROR;
683 *deferred = true;
685 return MATCH_YES;
688 m = gfc_match_expr (expr);
690 if (m == MATCH_YES
691 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
692 return MATCH_ERROR;
694 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
696 if ((*expr)->value.function.actual
697 && (*expr)->value.function.actual->expr->symtree)
699 gfc_expr *e;
700 e = (*expr)->value.function.actual->expr;
701 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
702 && e->expr_type == EXPR_VARIABLE)
704 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
705 goto syntax;
706 if (e->symtree->n.sym->ts.type == BT_CHARACTER
707 && e->symtree->n.sym->ts.u.cl
708 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
709 goto syntax;
713 return m;
715 syntax:
716 gfc_error ("Conflict in attributes of function argument at %C");
717 return MATCH_ERROR;
721 /* A character length is a '*' followed by a literal integer or a
722 char_len_param_value in parenthesis. */
724 static match
725 match_char_length (gfc_expr **expr, bool *deferred)
727 int length;
728 match m;
730 *deferred = false;
731 m = gfc_match_char ('*');
732 if (m != MATCH_YES)
733 return m;
735 m = gfc_match_small_literal_int (&length, NULL);
736 if (m == MATCH_ERROR)
737 return m;
739 if (m == MATCH_YES)
741 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
742 "Old-style character length at %C") == FAILURE)
743 return MATCH_ERROR;
744 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
745 return m;
748 if (gfc_match_char ('(') == MATCH_NO)
749 goto syntax;
751 m = char_len_param_value (expr, deferred);
752 if (m != MATCH_YES && gfc_matching_function)
754 gfc_undo_symbols ();
755 m = MATCH_YES;
758 if (m == MATCH_ERROR)
759 return m;
760 if (m == MATCH_NO)
761 goto syntax;
763 if (gfc_match_char (')') == MATCH_NO)
765 gfc_free_expr (*expr);
766 *expr = NULL;
767 goto syntax;
770 return MATCH_YES;
772 syntax:
773 gfc_error ("Syntax error in character length specification at %C");
774 return MATCH_ERROR;
778 /* Special subroutine for finding a symbol. Check if the name is found
779 in the current name space. If not, and we're compiling a function or
780 subroutine and the parent compilation unit is an interface, then check
781 to see if the name we've been given is the name of the interface
782 (located in another namespace). */
784 static int
785 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
787 gfc_state_data *s;
788 gfc_symtree *st;
789 int i;
791 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
792 if (i == 0)
794 *result = st ? st->n.sym : NULL;
795 goto end;
798 if (gfc_current_state () != COMP_SUBROUTINE
799 && gfc_current_state () != COMP_FUNCTION)
800 goto end;
802 s = gfc_state_stack->previous;
803 if (s == NULL)
804 goto end;
806 if (s->state != COMP_INTERFACE)
807 goto end;
808 if (s->sym == NULL)
809 goto end; /* Nameless interface. */
811 if (strcmp (name, s->sym->name) == 0)
813 *result = s->sym;
814 return 0;
817 end:
818 return i;
822 /* Special subroutine for getting a symbol node associated with a
823 procedure name, used in SUBROUTINE and FUNCTION statements. The
824 symbol is created in the parent using with symtree node in the
825 child unit pointing to the symbol. If the current namespace has no
826 parent, then the symbol is just created in the current unit. */
828 static int
829 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
831 gfc_symtree *st;
832 gfc_symbol *sym;
833 int rc = 0;
835 /* Module functions have to be left in their own namespace because
836 they have potentially (almost certainly!) already been referenced.
837 In this sense, they are rather like external functions. This is
838 fixed up in resolve.c(resolve_entries), where the symbol name-
839 space is set to point to the master function, so that the fake
840 result mechanism can work. */
841 if (module_fcn_entry)
843 /* Present if entry is declared to be a module procedure. */
844 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
846 if (*result == NULL)
847 rc = gfc_get_symbol (name, NULL, result);
848 else if (!gfc_get_symbol (name, NULL, &sym) && sym
849 && (*result)->ts.type == BT_UNKNOWN
850 && sym->attr.flavor == FL_UNKNOWN)
851 /* Pick up the typespec for the entry, if declared in the function
852 body. Note that this symbol is FL_UNKNOWN because it will
853 only have appeared in a type declaration. The local symtree
854 is set to point to the module symbol and a unique symtree
855 to the local version. This latter ensures a correct clearing
856 of the symbols. */
858 /* If the ENTRY proceeds its specification, we need to ensure
859 that this does not raise a "has no IMPLICIT type" error. */
860 if (sym->ts.type == BT_UNKNOWN)
861 sym->attr.untyped = 1;
863 (*result)->ts = sym->ts;
865 /* Put the symbol in the procedure namespace so that, should
866 the ENTRY precede its specification, the specification
867 can be applied. */
868 (*result)->ns = gfc_current_ns;
870 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
871 st->n.sym = *result;
872 st = gfc_get_unique_symtree (gfc_current_ns);
873 st->n.sym = sym;
876 else
877 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
879 if (rc)
880 return rc;
882 sym = *result;
883 gfc_current_ns->refs++;
885 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
887 /* Trap another encompassed procedure with the same name. All
888 these conditions are necessary to avoid picking up an entry
889 whose name clashes with that of the encompassing procedure;
890 this is handled using gsymbols to register unique,globally
891 accessible names. */
892 if (sym->attr.flavor != 0
893 && sym->attr.proc != 0
894 && (sym->attr.subroutine || sym->attr.function)
895 && sym->attr.if_source != IFSRC_UNKNOWN)
896 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
897 name, &sym->declared_at);
899 /* Trap a procedure with a name the same as interface in the
900 encompassing scope. */
901 if (sym->attr.generic != 0
902 && (sym->attr.subroutine || sym->attr.function)
903 && !sym->attr.mod_proc)
904 gfc_error_now ("Name '%s' at %C is already defined"
905 " as a generic interface at %L",
906 name, &sym->declared_at);
908 /* Trap declarations of attributes in encompassing scope. The
909 signature for this is that ts.kind is set. Legitimate
910 references only set ts.type. */
911 if (sym->ts.kind != 0
912 && !sym->attr.implicit_type
913 && sym->attr.proc == 0
914 && gfc_current_ns->parent != NULL
915 && sym->attr.access == 0
916 && !module_fcn_entry)
917 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
918 "and must not have attributes declared at %L",
919 name, &sym->declared_at);
922 if (gfc_current_ns->parent == NULL || *result == NULL)
923 return rc;
925 /* Module function entries will already have a symtree in
926 the current namespace but will need one at module level. */
927 if (module_fcn_entry)
929 /* Present if entry is declared to be a module procedure. */
930 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
931 if (st == NULL)
932 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
934 else
935 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
937 st->n.sym = sym;
938 sym->refs++;
940 /* See if the procedure should be a module procedure. */
942 if (((sym->ns->proc_name != NULL
943 && sym->ns->proc_name->attr.flavor == FL_MODULE
944 && sym->attr.proc != PROC_MODULE)
945 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
946 && gfc_add_procedure (&sym->attr, PROC_MODULE,
947 sym->name, NULL) == FAILURE)
948 rc = 2;
950 return rc;
954 /* Verify that the given symbol representing a parameter is C
955 interoperable, by checking to see if it was marked as such after
956 its declaration. If the given symbol is not interoperable, a
957 warning is reported, thus removing the need to return the status to
958 the calling function. The standard does not require the user use
959 one of the iso_c_binding named constants to declare an
960 interoperable parameter, but we can't be sure if the param is C
961 interop or not if the user doesn't. For example, integer(4) may be
962 legal Fortran, but doesn't have meaning in C. It may interop with
963 a number of the C types, which causes a problem because the
964 compiler can't know which one. This code is almost certainly not
965 portable, and the user will get what they deserve if the C type
966 across platforms isn't always interoperable with integer(4). If
967 the user had used something like integer(c_int) or integer(c_long),
968 the compiler could have automatically handled the varying sizes
969 across platforms. */
971 gfc_try
972 gfc_verify_c_interop_param (gfc_symbol *sym)
974 int is_c_interop = 0;
975 gfc_try retval = SUCCESS;
977 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
978 Don't repeat the checks here. */
979 if (sym->attr.implicit_type)
980 return SUCCESS;
982 /* For subroutines or functions that are passed to a BIND(C) procedure,
983 they're interoperable if they're BIND(C) and their params are all
984 interoperable. */
985 if (sym->attr.flavor == FL_PROCEDURE)
987 if (sym->attr.is_bind_c == 0)
989 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
990 "attribute to be C interoperable", sym->name,
991 &(sym->declared_at));
993 return FAILURE;
995 else
997 if (sym->attr.is_c_interop == 1)
998 /* We've already checked this procedure; don't check it again. */
999 return SUCCESS;
1000 else
1001 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
1002 sym->common_block);
1006 /* See if we've stored a reference to a procedure that owns sym. */
1007 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1009 if (sym->ns->proc_name->attr.is_bind_c == 1)
1011 is_c_interop = (gfc_verify_c_interop (&(sym->ts)) == SUCCESS ? 1 : 0);
1013 if (is_c_interop != 1)
1015 /* Make personalized messages to give better feedback. */
1016 if (sym->ts.type == BT_DERIVED)
1017 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1018 "BIND(C) procedure '%s' but is not C interoperable "
1019 "because derived type '%s' is not C interoperable",
1020 sym->name, &(sym->declared_at),
1021 sym->ns->proc_name->name,
1022 sym->ts.u.derived->name);
1023 else if (sym->ts.type == BT_CLASS)
1024 gfc_error ("Variable '%s' at %L is a dummy argument to the "
1025 "BIND(C) procedure '%s' but is not C interoperable "
1026 "because it is polymorphic",
1027 sym->name, &(sym->declared_at),
1028 sym->ns->proc_name->name);
1029 else
1030 gfc_warning ("Variable '%s' at %L is a parameter to the "
1031 "BIND(C) procedure '%s' but may not be C "
1032 "interoperable",
1033 sym->name, &(sym->declared_at),
1034 sym->ns->proc_name->name);
1037 /* Character strings are only C interoperable if they have a
1038 length of 1. */
1039 if (sym->ts.type == BT_CHARACTER)
1041 gfc_charlen *cl = sym->ts.u.cl;
1042 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1043 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1045 gfc_error ("Character argument '%s' at %L "
1046 "must be length 1 because "
1047 "procedure '%s' is BIND(C)",
1048 sym->name, &sym->declared_at,
1049 sym->ns->proc_name->name);
1050 retval = FAILURE;
1054 /* We have to make sure that any param to a bind(c) routine does
1055 not have the allocatable, pointer, or optional attributes,
1056 according to J3/04-007, section 5.1. */
1057 if (sym->attr.allocatable == 1)
1059 gfc_error ("Variable '%s' at %L cannot have the "
1060 "ALLOCATABLE attribute because procedure '%s'"
1061 " is BIND(C)", sym->name, &(sym->declared_at),
1062 sym->ns->proc_name->name);
1063 retval = FAILURE;
1066 if (sym->attr.pointer == 1)
1068 gfc_error ("Variable '%s' at %L cannot have the "
1069 "POINTER attribute because procedure '%s'"
1070 " is BIND(C)", sym->name, &(sym->declared_at),
1071 sym->ns->proc_name->name);
1072 retval = FAILURE;
1075 if (sym->attr.optional == 1 && sym->attr.value)
1077 gfc_error ("Variable '%s' at %L cannot have both the OPTIONAL "
1078 "and the VALUE attribute because procedure '%s' "
1079 "is BIND(C)", sym->name, &(sym->declared_at),
1080 sym->ns->proc_name->name);
1081 retval = FAILURE;
1083 else if (sym->attr.optional == 1
1084 && gfc_notify_std (GFC_STD_F2008_TS, "TS29113: Variable '%s' "
1085 "at %L with OPTIONAL attribute in "
1086 "procedure '%s' which is BIND(C)",
1087 sym->name, &(sym->declared_at),
1088 sym->ns->proc_name->name)
1089 == FAILURE)
1090 retval = FAILURE;
1092 /* Make sure that if it has the dimension attribute, that it is
1093 either assumed size or explicit shape. */
1094 if (sym->as != NULL)
1096 if (sym->as->type == AS_ASSUMED_SHAPE)
1098 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1099 "argument to the procedure '%s' at %L because "
1100 "the procedure is BIND(C)", sym->name,
1101 &(sym->declared_at), sym->ns->proc_name->name,
1102 &(sym->ns->proc_name->declared_at));
1103 retval = FAILURE;
1106 if (sym->as->type == AS_DEFERRED)
1108 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1109 "argument to the procedure '%s' at %L because "
1110 "the procedure is BIND(C)", sym->name,
1111 &(sym->declared_at), sym->ns->proc_name->name,
1112 &(sym->ns->proc_name->declared_at));
1113 retval = FAILURE;
1119 return retval;
1124 /* Function called by variable_decl() that adds a name to the symbol table. */
1126 static gfc_try
1127 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1128 gfc_array_spec **as, locus *var_locus)
1130 symbol_attribute attr;
1131 gfc_symbol *sym;
1133 if (gfc_get_symbol (name, NULL, &sym))
1134 return FAILURE;
1136 /* Start updating the symbol table. Add basic type attribute if present. */
1137 if (current_ts.type != BT_UNKNOWN
1138 && (sym->attr.implicit_type == 0
1139 || !gfc_compare_types (&sym->ts, &current_ts))
1140 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
1141 return FAILURE;
1143 if (sym->ts.type == BT_CHARACTER)
1145 sym->ts.u.cl = cl;
1146 sym->ts.deferred = cl_deferred;
1149 /* Add dimension attribute if present. */
1150 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1151 return FAILURE;
1152 *as = NULL;
1154 /* Add attribute to symbol. The copy is so that we can reset the
1155 dimension attribute. */
1156 attr = current_attr;
1157 attr.dimension = 0;
1158 attr.codimension = 0;
1160 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1161 return FAILURE;
1163 /* Finish any work that may need to be done for the binding label,
1164 if it's a bind(c). The bind(c) attr is found before the symbol
1165 is made, and before the symbol name (for data decls), so the
1166 current_ts is holding the binding label, or nothing if the
1167 name= attr wasn't given. Therefore, test here if we're dealing
1168 with a bind(c) and make sure the binding label is set correctly. */
1169 if (sym->attr.is_bind_c == 1)
1171 if (!sym->binding_label)
1173 /* Set the binding label and verify that if a NAME= was specified
1174 then only one identifier was in the entity-decl-list. */
1175 if (set_binding_label (&sym->binding_label, sym->name,
1176 num_idents_on_line) == FAILURE)
1177 return FAILURE;
1181 /* See if we know we're in a common block, and if it's a bind(c)
1182 common then we need to make sure we're an interoperable type. */
1183 if (sym->attr.in_common == 1)
1185 /* Test the common block object. */
1186 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1187 && sym->ts.is_c_interop != 1)
1189 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1190 "must be declared with a C interoperable "
1191 "kind since common block '%s' is BIND(C)",
1192 sym->name, sym->common_block->name,
1193 sym->common_block->name);
1194 gfc_clear_error ();
1198 sym->attr.implied_index = 0;
1200 if (sym->ts.type == BT_CLASS)
1201 return gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1203 return SUCCESS;
1207 /* Set character constant to the given length. The constant will be padded or
1208 truncated. If we're inside an array constructor without a typespec, we
1209 additionally check that all elements have the same length; check_len -1
1210 means no checking. */
1212 void
1213 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1215 gfc_char_t *s;
1216 int slen;
1218 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1219 gcc_assert (expr->ts.type == BT_CHARACTER);
1221 slen = expr->value.character.length;
1222 if (len != slen)
1224 s = gfc_get_wide_string (len + 1);
1225 memcpy (s, expr->value.character.string,
1226 MIN (len, slen) * sizeof (gfc_char_t));
1227 if (len > slen)
1228 gfc_wide_memset (&s[slen], ' ', len - slen);
1230 if (gfc_option.warn_character_truncation && slen > len)
1231 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1232 "(%d/%d)", &expr->where, slen, len);
1234 /* Apply the standard by 'hand' otherwise it gets cleared for
1235 initializers. */
1236 if (check_len != -1 && slen != check_len
1237 && !(gfc_option.allow_std & GFC_STD_GNU))
1238 gfc_error_now ("The CHARACTER elements of the array constructor "
1239 "at %L must have the same length (%d/%d)",
1240 &expr->where, slen, check_len);
1242 s[len] = '\0';
1243 free (expr->value.character.string);
1244 expr->value.character.string = s;
1245 expr->value.character.length = len;
1250 /* Function to create and update the enumerator history
1251 using the information passed as arguments.
1252 Pointer "max_enum" is also updated, to point to
1253 enum history node containing largest initializer.
1255 SYM points to the symbol node of enumerator.
1256 INIT points to its enumerator value. */
1258 static void
1259 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1261 enumerator_history *new_enum_history;
1262 gcc_assert (sym != NULL && init != NULL);
1264 new_enum_history = XCNEW (enumerator_history);
1266 new_enum_history->sym = sym;
1267 new_enum_history->initializer = init;
1268 new_enum_history->next = NULL;
1270 if (enum_history == NULL)
1272 enum_history = new_enum_history;
1273 max_enum = enum_history;
1275 else
1277 new_enum_history->next = enum_history;
1278 enum_history = new_enum_history;
1280 if (mpz_cmp (max_enum->initializer->value.integer,
1281 new_enum_history->initializer->value.integer) < 0)
1282 max_enum = new_enum_history;
1287 /* Function to free enum kind history. */
1289 void
1290 gfc_free_enum_history (void)
1292 enumerator_history *current = enum_history;
1293 enumerator_history *next;
1295 while (current != NULL)
1297 next = current->next;
1298 free (current);
1299 current = next;
1301 max_enum = NULL;
1302 enum_history = NULL;
1306 /* Function called by variable_decl() that adds an initialization
1307 expression to a symbol. */
1309 static gfc_try
1310 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1312 symbol_attribute attr;
1313 gfc_symbol *sym;
1314 gfc_expr *init;
1316 init = *initp;
1317 if (find_special (name, &sym, false))
1318 return FAILURE;
1320 attr = sym->attr;
1322 /* If this symbol is confirming an implicit parameter type,
1323 then an initialization expression is not allowed. */
1324 if (attr.flavor == FL_PARAMETER
1325 && sym->value != NULL
1326 && *initp != NULL)
1328 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1329 sym->name);
1330 return FAILURE;
1333 if (init == NULL)
1335 /* An initializer is required for PARAMETER declarations. */
1336 if (attr.flavor == FL_PARAMETER)
1338 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1339 return FAILURE;
1342 else
1344 /* If a variable appears in a DATA block, it cannot have an
1345 initializer. */
1346 if (sym->attr.data)
1348 gfc_error ("Variable '%s' at %C with an initializer already "
1349 "appears in a DATA statement", sym->name);
1350 return FAILURE;
1353 /* Check if the assignment can happen. This has to be put off
1354 until later for derived type variables and procedure pointers. */
1355 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1356 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1357 && !sym->attr.proc_pointer
1358 && gfc_check_assign_symbol (sym, init) == FAILURE)
1359 return FAILURE;
1361 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1362 && init->ts.type == BT_CHARACTER)
1364 /* Update symbol character length according initializer. */
1365 if (gfc_check_assign_symbol (sym, init) == FAILURE)
1366 return FAILURE;
1368 if (sym->ts.u.cl->length == NULL)
1370 int clen;
1371 /* If there are multiple CHARACTER variables declared on the
1372 same line, we don't want them to share the same length. */
1373 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1375 if (sym->attr.flavor == FL_PARAMETER)
1377 if (init->expr_type == EXPR_CONSTANT)
1379 clen = init->value.character.length;
1380 sym->ts.u.cl->length
1381 = gfc_get_int_expr (gfc_default_integer_kind,
1382 NULL, clen);
1384 else if (init->expr_type == EXPR_ARRAY)
1386 gfc_constructor *c;
1387 c = gfc_constructor_first (init->value.constructor);
1388 clen = c->expr->value.character.length;
1389 sym->ts.u.cl->length
1390 = gfc_get_int_expr (gfc_default_integer_kind,
1391 NULL, clen);
1393 else if (init->ts.u.cl && init->ts.u.cl->length)
1394 sym->ts.u.cl->length =
1395 gfc_copy_expr (sym->value->ts.u.cl->length);
1398 /* Update initializer character length according symbol. */
1399 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1401 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1403 if (init->expr_type == EXPR_CONSTANT)
1404 gfc_set_constant_character_len (len, init, -1);
1405 else if (init->expr_type == EXPR_ARRAY)
1407 gfc_constructor *c;
1409 /* Build a new charlen to prevent simplification from
1410 deleting the length before it is resolved. */
1411 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1412 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1414 for (c = gfc_constructor_first (init->value.constructor);
1415 c; c = gfc_constructor_next (c))
1416 gfc_set_constant_character_len (len, c->expr, -1);
1421 /* If sym is implied-shape, set its upper bounds from init. */
1422 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1423 && sym->as->type == AS_IMPLIED_SHAPE)
1425 int dim;
1427 if (init->rank == 0)
1429 gfc_error ("Can't initialize implied-shape array at %L"
1430 " with scalar", &sym->declared_at);
1431 return FAILURE;
1433 gcc_assert (sym->as->rank == init->rank);
1435 /* Shape should be present, we get an initialization expression. */
1436 gcc_assert (init->shape);
1438 for (dim = 0; dim < sym->as->rank; ++dim)
1440 int k;
1441 gfc_expr* lower;
1442 gfc_expr* e;
1444 lower = sym->as->lower[dim];
1445 if (lower->expr_type != EXPR_CONSTANT)
1447 gfc_error ("Non-constant lower bound in implied-shape"
1448 " declaration at %L", &lower->where);
1449 return FAILURE;
1452 /* All dimensions must be without upper bound. */
1453 gcc_assert (!sym->as->upper[dim]);
1455 k = lower->ts.kind;
1456 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1457 mpz_add (e->value.integer,
1458 lower->value.integer, init->shape[dim]);
1459 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1460 sym->as->upper[dim] = e;
1463 sym->as->type = AS_EXPLICIT;
1466 /* Need to check if the expression we initialized this
1467 to was one of the iso_c_binding named constants. If so,
1468 and we're a parameter (constant), let it be iso_c.
1469 For example:
1470 integer(c_int), parameter :: my_int = c_int
1471 integer(my_int) :: my_int_2
1472 If we mark my_int as iso_c (since we can see it's value
1473 is equal to one of the named constants), then my_int_2
1474 will be considered C interoperable. */
1475 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1477 sym->ts.is_iso_c |= init->ts.is_iso_c;
1478 sym->ts.is_c_interop |= init->ts.is_c_interop;
1479 /* attr bits needed for module files. */
1480 sym->attr.is_iso_c |= init->ts.is_iso_c;
1481 sym->attr.is_c_interop |= init->ts.is_c_interop;
1482 if (init->ts.is_iso_c)
1483 sym->ts.f90_type = init->ts.f90_type;
1486 /* Add initializer. Make sure we keep the ranks sane. */
1487 if (sym->attr.dimension && init->rank == 0)
1489 mpz_t size;
1490 gfc_expr *array;
1491 int n;
1492 if (sym->attr.flavor == FL_PARAMETER
1493 && init->expr_type == EXPR_CONSTANT
1494 && spec_size (sym->as, &size) == SUCCESS
1495 && mpz_cmp_si (size, 0) > 0)
1497 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1498 &init->where);
1499 for (n = 0; n < (int)mpz_get_si (size); n++)
1500 gfc_constructor_append_expr (&array->value.constructor,
1501 n == 0
1502 ? init
1503 : gfc_copy_expr (init),
1504 &init->where);
1506 array->shape = gfc_get_shape (sym->as->rank);
1507 for (n = 0; n < sym->as->rank; n++)
1508 spec_dimen_size (sym->as, n, &array->shape[n]);
1510 init = array;
1511 mpz_clear (size);
1513 init->rank = sym->as->rank;
1516 sym->value = init;
1517 if (sym->attr.save == SAVE_NONE)
1518 sym->attr.save = SAVE_IMPLICIT;
1519 *initp = NULL;
1522 return SUCCESS;
1526 /* Function called by variable_decl() that adds a name to a structure
1527 being built. */
1529 static gfc_try
1530 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1531 gfc_array_spec **as)
1533 gfc_component *c;
1534 gfc_try t = SUCCESS;
1536 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1537 constructing, it must have the pointer attribute. */
1538 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1539 && current_ts.u.derived == gfc_current_block ()
1540 && current_attr.pointer == 0)
1542 gfc_error ("Component at %C must have the POINTER attribute");
1543 return FAILURE;
1546 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1548 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1550 gfc_error ("Array component of structure at %C must have explicit "
1551 "or deferred shape");
1552 return FAILURE;
1556 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1557 return FAILURE;
1559 c->ts = current_ts;
1560 if (c->ts.type == BT_CHARACTER)
1561 c->ts.u.cl = cl;
1562 c->attr = current_attr;
1564 c->initializer = *init;
1565 *init = NULL;
1567 c->as = *as;
1568 if (c->as != NULL)
1570 if (c->as->corank)
1571 c->attr.codimension = 1;
1572 if (c->as->rank)
1573 c->attr.dimension = 1;
1575 *as = NULL;
1577 /* Should this ever get more complicated, combine with similar section
1578 in add_init_expr_to_sym into a separate function. */
1579 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer
1580 && c->ts.u.cl
1581 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1583 int len;
1585 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1586 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1587 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1589 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1591 if (c->initializer->expr_type == EXPR_CONSTANT)
1592 gfc_set_constant_character_len (len, c->initializer, -1);
1593 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1594 c->initializer->ts.u.cl->length->value.integer))
1596 gfc_constructor *ctor;
1597 ctor = gfc_constructor_first (c->initializer->value.constructor);
1599 if (ctor)
1601 int first_len;
1602 bool has_ts = (c->initializer->ts.u.cl
1603 && c->initializer->ts.u.cl->length_from_typespec);
1605 /* Remember the length of the first element for checking
1606 that all elements *in the constructor* have the same
1607 length. This need not be the length of the LHS! */
1608 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1609 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1610 first_len = ctor->expr->value.character.length;
1612 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1613 if (ctor->expr->expr_type == EXPR_CONSTANT)
1615 gfc_set_constant_character_len (len, ctor->expr,
1616 has_ts ? -1 : first_len);
1617 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1623 /* Check array components. */
1624 if (!c->attr.dimension)
1625 goto scalar;
1627 if (c->attr.pointer)
1629 if (c->as->type != AS_DEFERRED)
1631 gfc_error ("Pointer array component of structure at %C must have a "
1632 "deferred shape");
1633 t = FAILURE;
1636 else if (c->attr.allocatable)
1638 if (c->as->type != AS_DEFERRED)
1640 gfc_error ("Allocatable component of structure at %C must have a "
1641 "deferred shape");
1642 t = FAILURE;
1645 else
1647 if (c->as->type != AS_EXPLICIT)
1649 gfc_error ("Array component of structure at %C must have an "
1650 "explicit shape");
1651 t = FAILURE;
1655 scalar:
1656 if (c->ts.type == BT_CLASS)
1658 bool delayed = (gfc_state_stack->sym == c->ts.u.derived)
1659 || (!c->ts.u.derived->components
1660 && !c->ts.u.derived->attr.zero_comp);
1661 gfc_try t2 = gfc_build_class_symbol (&c->ts, &c->attr, &c->as, delayed);
1663 if (t != FAILURE)
1664 t = t2;
1667 return t;
1671 /* Match a 'NULL()', and possibly take care of some side effects. */
1673 match
1674 gfc_match_null (gfc_expr **result)
1676 gfc_symbol *sym;
1677 match m;
1679 m = gfc_match (" null ( )");
1680 if (m != MATCH_YES)
1681 return m;
1683 /* The NULL symbol now has to be/become an intrinsic function. */
1684 if (gfc_get_symbol ("null", NULL, &sym))
1686 gfc_error ("NULL() initialization at %C is ambiguous");
1687 return MATCH_ERROR;
1690 gfc_intrinsic_symbol (sym);
1692 if (sym->attr.proc != PROC_INTRINSIC
1693 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1694 sym->name, NULL) == FAILURE
1695 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1696 return MATCH_ERROR;
1698 *result = gfc_get_null_expr (&gfc_current_locus);
1700 return MATCH_YES;
1704 /* Match the initialization expr for a data pointer or procedure pointer. */
1706 static match
1707 match_pointer_init (gfc_expr **init, int procptr)
1709 match m;
1711 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1713 gfc_error ("Initialization of pointer at %C is not allowed in "
1714 "a PURE procedure");
1715 return MATCH_ERROR;
1718 /* Match NULL() initialization. */
1719 m = gfc_match_null (init);
1720 if (m != MATCH_NO)
1721 return m;
1723 /* Match non-NULL initialization. */
1724 gfc_matching_ptr_assignment = !procptr;
1725 gfc_matching_procptr_assignment = procptr;
1726 m = gfc_match_rvalue (init);
1727 gfc_matching_ptr_assignment = 0;
1728 gfc_matching_procptr_assignment = 0;
1729 if (m == MATCH_ERROR)
1730 return MATCH_ERROR;
1731 else if (m == MATCH_NO)
1733 gfc_error ("Error in pointer initialization at %C");
1734 return MATCH_ERROR;
1737 if (!procptr)
1738 gfc_resolve_expr (*init);
1740 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: non-NULL pointer "
1741 "initialization at %C") == FAILURE)
1742 return MATCH_ERROR;
1744 return MATCH_YES;
1748 static gfc_try
1749 check_function_name (char *name)
1751 /* In functions that have a RESULT variable defined, the function name always
1752 refers to function calls. Therefore, the name is not allowed to appear in
1753 specification statements. When checking this, be careful about
1754 'hidden' procedure pointer results ('ppr@'). */
1756 if (gfc_current_state () == COMP_FUNCTION)
1758 gfc_symbol *block = gfc_current_block ();
1759 if (block && block->result && block->result != block
1760 && strcmp (block->result->name, "ppr@") != 0
1761 && strcmp (block->name, name) == 0)
1763 gfc_error ("Function name '%s' not allowed at %C", name);
1764 return FAILURE;
1768 return SUCCESS;
1772 /* Match a variable name with an optional initializer. When this
1773 subroutine is called, a variable is expected to be parsed next.
1774 Depending on what is happening at the moment, updates either the
1775 symbol table or the current interface. */
1777 static match
1778 variable_decl (int elem)
1780 char name[GFC_MAX_SYMBOL_LEN + 1];
1781 gfc_expr *initializer, *char_len;
1782 gfc_array_spec *as;
1783 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1784 gfc_charlen *cl;
1785 bool cl_deferred;
1786 locus var_locus;
1787 match m;
1788 gfc_try t;
1789 gfc_symbol *sym;
1791 initializer = NULL;
1792 as = NULL;
1793 cp_as = NULL;
1795 /* When we get here, we've just matched a list of attributes and
1796 maybe a type and a double colon. The next thing we expect to see
1797 is the name of the symbol. */
1798 m = gfc_match_name (name);
1799 if (m != MATCH_YES)
1800 goto cleanup;
1802 var_locus = gfc_current_locus;
1804 /* Now we could see the optional array spec. or character length. */
1805 m = gfc_match_array_spec (&as, true, true);
1806 if (m == MATCH_ERROR)
1807 goto cleanup;
1809 if (m == MATCH_NO)
1810 as = gfc_copy_array_spec (current_as);
1811 else if (current_as)
1812 merge_array_spec (current_as, as, true);
1814 if (gfc_option.flag_cray_pointer)
1815 cp_as = gfc_copy_array_spec (as);
1817 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1818 determine (and check) whether it can be implied-shape. If it
1819 was parsed as assumed-size, change it because PARAMETERs can not
1820 be assumed-size. */
1821 if (as)
1823 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1825 m = MATCH_ERROR;
1826 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1827 name, &var_locus);
1828 goto cleanup;
1831 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1832 && current_attr.flavor == FL_PARAMETER)
1833 as->type = AS_IMPLIED_SHAPE;
1835 if (as->type == AS_IMPLIED_SHAPE
1836 && gfc_notify_std (GFC_STD_F2008,
1837 "Fortran 2008: Implied-shape array at %L",
1838 &var_locus) == FAILURE)
1840 m = MATCH_ERROR;
1841 goto cleanup;
1845 char_len = NULL;
1846 cl = NULL;
1847 cl_deferred = false;
1849 if (current_ts.type == BT_CHARACTER)
1851 switch (match_char_length (&char_len, &cl_deferred))
1853 case MATCH_YES:
1854 cl = gfc_new_charlen (gfc_current_ns, NULL);
1856 cl->length = char_len;
1857 break;
1859 /* Non-constant lengths need to be copied after the first
1860 element. Also copy assumed lengths. */
1861 case MATCH_NO:
1862 if (elem > 1
1863 && (current_ts.u.cl->length == NULL
1864 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1866 cl = gfc_new_charlen (gfc_current_ns, NULL);
1867 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1869 else
1870 cl = current_ts.u.cl;
1872 cl_deferred = current_ts.deferred;
1874 break;
1876 case MATCH_ERROR:
1877 goto cleanup;
1881 /* If this symbol has already shown up in a Cray Pointer declaration,
1882 then we want to set the type & bail out. */
1883 if (gfc_option.flag_cray_pointer)
1885 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1886 if (sym != NULL && sym->attr.cray_pointee)
1888 sym->ts.type = current_ts.type;
1889 sym->ts.kind = current_ts.kind;
1890 sym->ts.u.cl = cl;
1891 sym->ts.u.derived = current_ts.u.derived;
1892 sym->ts.is_c_interop = current_ts.is_c_interop;
1893 sym->ts.is_iso_c = current_ts.is_iso_c;
1894 m = MATCH_YES;
1896 /* Check to see if we have an array specification. */
1897 if (cp_as != NULL)
1899 if (sym->as != NULL)
1901 gfc_error ("Duplicate array spec for Cray pointee at %C");
1902 gfc_free_array_spec (cp_as);
1903 m = MATCH_ERROR;
1904 goto cleanup;
1906 else
1908 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1909 gfc_internal_error ("Couldn't set pointee array spec.");
1911 /* Fix the array spec. */
1912 m = gfc_mod_pointee_as (sym->as);
1913 if (m == MATCH_ERROR)
1914 goto cleanup;
1917 goto cleanup;
1919 else
1921 gfc_free_array_spec (cp_as);
1925 /* Procedure pointer as function result. */
1926 if (gfc_current_state () == COMP_FUNCTION
1927 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1928 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1929 strcpy (name, "ppr@");
1931 if (gfc_current_state () == COMP_FUNCTION
1932 && strcmp (name, gfc_current_block ()->name) == 0
1933 && gfc_current_block ()->result
1934 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1935 strcpy (name, "ppr@");
1937 /* OK, we've successfully matched the declaration. Now put the
1938 symbol in the current namespace, because it might be used in the
1939 optional initialization expression for this symbol, e.g. this is
1940 perfectly legal:
1942 integer, parameter :: i = huge(i)
1944 This is only true for parameters or variables of a basic type.
1945 For components of derived types, it is not true, so we don't
1946 create a symbol for those yet. If we fail to create the symbol,
1947 bail out. */
1948 if (gfc_current_state () != COMP_DERIVED
1949 && build_sym (name, cl, cl_deferred, &as, &var_locus) == FAILURE)
1951 m = MATCH_ERROR;
1952 goto cleanup;
1955 /* An interface body specifies all of the procedure's
1956 characteristics and these shall be consistent with those
1957 specified in the procedure definition, except that the interface
1958 may specify a procedure that is not pure if the procedure is
1959 defined to be pure(12.3.2). */
1960 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1961 && gfc_current_ns->proc_name
1962 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1963 && current_ts.u.derived->ns != gfc_current_ns)
1965 gfc_symtree *st;
1966 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.u.derived->name);
1967 if (!(current_ts.u.derived->attr.imported
1968 && st != NULL
1969 && gfc_find_dt_in_generic (st->n.sym) == current_ts.u.derived)
1970 && !gfc_current_ns->has_import_set)
1972 gfc_error ("The type of '%s' at %C has not been declared within the "
1973 "interface", name);
1974 m = MATCH_ERROR;
1975 goto cleanup;
1979 if (check_function_name (name) == FAILURE)
1981 m = MATCH_ERROR;
1982 goto cleanup;
1985 /* We allow old-style initializations of the form
1986 integer i /2/, j(4) /3*3, 1/
1987 (if no colon has been seen). These are different from data
1988 statements in that initializers are only allowed to apply to the
1989 variable immediately preceding, i.e.
1990 integer i, j /1, 2/
1991 is not allowed. Therefore we have to do some work manually, that
1992 could otherwise be left to the matchers for DATA statements. */
1994 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1996 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1997 "initialization at %C") == FAILURE)
1998 return MATCH_ERROR;
2000 return match_old_style_init (name);
2003 /* The double colon must be present in order to have initializers.
2004 Otherwise the statement is ambiguous with an assignment statement. */
2005 if (colon_seen)
2007 if (gfc_match (" =>") == MATCH_YES)
2009 if (!current_attr.pointer)
2011 gfc_error ("Initialization at %C isn't for a pointer variable");
2012 m = MATCH_ERROR;
2013 goto cleanup;
2016 m = match_pointer_init (&initializer, 0);
2017 if (m != MATCH_YES)
2018 goto cleanup;
2020 else if (gfc_match_char ('=') == MATCH_YES)
2022 if (current_attr.pointer)
2024 gfc_error ("Pointer initialization at %C requires '=>', "
2025 "not '='");
2026 m = MATCH_ERROR;
2027 goto cleanup;
2030 m = gfc_match_init_expr (&initializer);
2031 if (m == MATCH_NO)
2033 gfc_error ("Expected an initialization expression at %C");
2034 m = MATCH_ERROR;
2037 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2038 && gfc_state_stack->state != COMP_DERIVED)
2040 gfc_error ("Initialization of variable at %C is not allowed in "
2041 "a PURE procedure");
2042 m = MATCH_ERROR;
2045 if (m != MATCH_YES)
2046 goto cleanup;
2050 if (initializer != NULL && current_attr.allocatable
2051 && gfc_current_state () == COMP_DERIVED)
2053 gfc_error ("Initialization of allocatable component at %C is not "
2054 "allowed");
2055 m = MATCH_ERROR;
2056 goto cleanup;
2059 /* Add the initializer. Note that it is fine if initializer is
2060 NULL here, because we sometimes also need to check if a
2061 declaration *must* have an initialization expression. */
2062 if (gfc_current_state () != COMP_DERIVED)
2063 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2064 else
2066 if (current_ts.type == BT_DERIVED
2067 && !current_attr.pointer && !initializer)
2068 initializer = gfc_default_initializer (&current_ts);
2069 t = build_struct (name, cl, &initializer, &as);
2072 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
2074 cleanup:
2075 /* Free stuff up and return. */
2076 gfc_free_expr (initializer);
2077 gfc_free_array_spec (as);
2079 return m;
2083 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2084 This assumes that the byte size is equal to the kind number for
2085 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2087 match
2088 gfc_match_old_kind_spec (gfc_typespec *ts)
2090 match m;
2091 int original_kind;
2093 if (gfc_match_char ('*') != MATCH_YES)
2094 return MATCH_NO;
2096 m = gfc_match_small_literal_int (&ts->kind, NULL);
2097 if (m != MATCH_YES)
2098 return MATCH_ERROR;
2100 original_kind = ts->kind;
2102 /* Massage the kind numbers for complex types. */
2103 if (ts->type == BT_COMPLEX)
2105 if (ts->kind % 2)
2107 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2108 gfc_basic_typename (ts->type), original_kind);
2109 return MATCH_ERROR;
2111 ts->kind /= 2;
2115 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2116 ts->kind = 8;
2118 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2120 if (ts->kind == 4)
2122 if (gfc_option.flag_real4_kind == 8)
2123 ts->kind = 8;
2124 if (gfc_option.flag_real4_kind == 10)
2125 ts->kind = 10;
2126 if (gfc_option.flag_real4_kind == 16)
2127 ts->kind = 16;
2130 if (ts->kind == 8)
2132 if (gfc_option.flag_real8_kind == 4)
2133 ts->kind = 4;
2134 if (gfc_option.flag_real8_kind == 10)
2135 ts->kind = 10;
2136 if (gfc_option.flag_real8_kind == 16)
2137 ts->kind = 16;
2141 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2143 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2144 gfc_basic_typename (ts->type), original_kind);
2145 return MATCH_ERROR;
2148 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2149 gfc_basic_typename (ts->type), original_kind) == FAILURE)
2150 return MATCH_ERROR;
2152 return MATCH_YES;
2156 /* Match a kind specification. Since kinds are generally optional, we
2157 usually return MATCH_NO if something goes wrong. If a "kind="
2158 string is found, then we know we have an error. */
2160 match
2161 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2163 locus where, loc;
2164 gfc_expr *e;
2165 match m, n;
2166 char c;
2167 const char *msg;
2169 m = MATCH_NO;
2170 n = MATCH_YES;
2171 e = NULL;
2173 where = loc = gfc_current_locus;
2175 if (kind_expr_only)
2176 goto kind_expr;
2178 if (gfc_match_char ('(') == MATCH_NO)
2179 return MATCH_NO;
2181 /* Also gobbles optional text. */
2182 if (gfc_match (" kind = ") == MATCH_YES)
2183 m = MATCH_ERROR;
2185 loc = gfc_current_locus;
2187 kind_expr:
2188 n = gfc_match_init_expr (&e);
2190 if (n != MATCH_YES)
2192 if (gfc_matching_function)
2194 /* The function kind expression might include use associated or
2195 imported parameters and try again after the specification
2196 expressions..... */
2197 if (gfc_match_char (')') != MATCH_YES)
2199 gfc_error ("Missing right parenthesis at %C");
2200 m = MATCH_ERROR;
2201 goto no_match;
2204 gfc_free_expr (e);
2205 gfc_undo_symbols ();
2206 return MATCH_YES;
2208 else
2210 /* ....or else, the match is real. */
2211 if (n == MATCH_NO)
2212 gfc_error ("Expected initialization expression at %C");
2213 if (n != MATCH_YES)
2214 return MATCH_ERROR;
2218 if (e->rank != 0)
2220 gfc_error ("Expected scalar initialization expression at %C");
2221 m = MATCH_ERROR;
2222 goto no_match;
2225 msg = gfc_extract_int (e, &ts->kind);
2227 if (msg != NULL)
2229 gfc_error (msg);
2230 m = MATCH_ERROR;
2231 goto no_match;
2234 /* Before throwing away the expression, let's see if we had a
2235 C interoperable kind (and store the fact). */
2236 if (e->ts.is_c_interop == 1)
2238 /* Mark this as C interoperable if being declared with one
2239 of the named constants from iso_c_binding. */
2240 ts->is_c_interop = e->ts.is_iso_c;
2241 ts->f90_type = e->ts.f90_type;
2244 gfc_free_expr (e);
2245 e = NULL;
2247 /* Ignore errors to this point, if we've gotten here. This means
2248 we ignore the m=MATCH_ERROR from above. */
2249 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2251 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2252 gfc_basic_typename (ts->type));
2253 gfc_current_locus = where;
2254 return MATCH_ERROR;
2257 /* Warn if, e.g., c_int is used for a REAL variable, but not
2258 if, e.g., c_double is used for COMPLEX as the standard
2259 explicitly says that the kind type parameter for complex and real
2260 variable is the same, i.e. c_float == c_float_complex. */
2261 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2262 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2263 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2264 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2265 "is %s", gfc_basic_typename (ts->f90_type), &where,
2266 gfc_basic_typename (ts->type));
2268 gfc_gobble_whitespace ();
2269 if ((c = gfc_next_ascii_char ()) != ')'
2270 && (ts->type != BT_CHARACTER || c != ','))
2272 if (ts->type == BT_CHARACTER)
2273 gfc_error ("Missing right parenthesis or comma at %C");
2274 else
2275 gfc_error ("Missing right parenthesis at %C");
2276 m = MATCH_ERROR;
2278 else
2279 /* All tests passed. */
2280 m = MATCH_YES;
2282 if(m == MATCH_ERROR)
2283 gfc_current_locus = where;
2285 if (ts->type == BT_INTEGER && ts->kind == 4 && gfc_option.flag_integer4_kind == 8)
2286 ts->kind = 8;
2288 if (ts->type == BT_REAL || ts->type == BT_COMPLEX)
2290 if (ts->kind == 4)
2292 if (gfc_option.flag_real4_kind == 8)
2293 ts->kind = 8;
2294 if (gfc_option.flag_real4_kind == 10)
2295 ts->kind = 10;
2296 if (gfc_option.flag_real4_kind == 16)
2297 ts->kind = 16;
2300 if (ts->kind == 8)
2302 if (gfc_option.flag_real8_kind == 4)
2303 ts->kind = 4;
2304 if (gfc_option.flag_real8_kind == 10)
2305 ts->kind = 10;
2306 if (gfc_option.flag_real8_kind == 16)
2307 ts->kind = 16;
2311 /* Return what we know from the test(s). */
2312 return m;
2314 no_match:
2315 gfc_free_expr (e);
2316 gfc_current_locus = where;
2317 return m;
2321 static match
2322 match_char_kind (int * kind, int * is_iso_c)
2324 locus where;
2325 gfc_expr *e;
2326 match m, n;
2327 const char *msg;
2329 m = MATCH_NO;
2330 e = NULL;
2331 where = gfc_current_locus;
2333 n = gfc_match_init_expr (&e);
2335 if (n != MATCH_YES && gfc_matching_function)
2337 /* The expression might include use-associated or imported
2338 parameters and try again after the specification
2339 expressions. */
2340 gfc_free_expr (e);
2341 gfc_undo_symbols ();
2342 return MATCH_YES;
2345 if (n == MATCH_NO)
2346 gfc_error ("Expected initialization expression at %C");
2347 if (n != MATCH_YES)
2348 return MATCH_ERROR;
2350 if (e->rank != 0)
2352 gfc_error ("Expected scalar initialization expression at %C");
2353 m = MATCH_ERROR;
2354 goto no_match;
2357 msg = gfc_extract_int (e, kind);
2358 *is_iso_c = e->ts.is_iso_c;
2359 if (msg != NULL)
2361 gfc_error (msg);
2362 m = MATCH_ERROR;
2363 goto no_match;
2366 gfc_free_expr (e);
2368 /* Ignore errors to this point, if we've gotten here. This means
2369 we ignore the m=MATCH_ERROR from above. */
2370 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2372 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2373 m = MATCH_ERROR;
2375 else
2376 /* All tests passed. */
2377 m = MATCH_YES;
2379 if (m == MATCH_ERROR)
2380 gfc_current_locus = where;
2382 /* Return what we know from the test(s). */
2383 return m;
2385 no_match:
2386 gfc_free_expr (e);
2387 gfc_current_locus = where;
2388 return m;
2392 /* Match the various kind/length specifications in a CHARACTER
2393 declaration. We don't return MATCH_NO. */
2395 match
2396 gfc_match_char_spec (gfc_typespec *ts)
2398 int kind, seen_length, is_iso_c;
2399 gfc_charlen *cl;
2400 gfc_expr *len;
2401 match m;
2402 bool deferred;
2404 len = NULL;
2405 seen_length = 0;
2406 kind = 0;
2407 is_iso_c = 0;
2408 deferred = false;
2410 /* Try the old-style specification first. */
2411 old_char_selector = 0;
2413 m = match_char_length (&len, &deferred);
2414 if (m != MATCH_NO)
2416 if (m == MATCH_YES)
2417 old_char_selector = 1;
2418 seen_length = 1;
2419 goto done;
2422 m = gfc_match_char ('(');
2423 if (m != MATCH_YES)
2425 m = MATCH_YES; /* Character without length is a single char. */
2426 goto done;
2429 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2430 if (gfc_match (" kind =") == MATCH_YES)
2432 m = match_char_kind (&kind, &is_iso_c);
2434 if (m == MATCH_ERROR)
2435 goto done;
2436 if (m == MATCH_NO)
2437 goto syntax;
2439 if (gfc_match (" , len =") == MATCH_NO)
2440 goto rparen;
2442 m = char_len_param_value (&len, &deferred);
2443 if (m == MATCH_NO)
2444 goto syntax;
2445 if (m == MATCH_ERROR)
2446 goto done;
2447 seen_length = 1;
2449 goto rparen;
2452 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2453 if (gfc_match (" len =") == MATCH_YES)
2455 m = char_len_param_value (&len, &deferred);
2456 if (m == MATCH_NO)
2457 goto syntax;
2458 if (m == MATCH_ERROR)
2459 goto done;
2460 seen_length = 1;
2462 if (gfc_match_char (')') == MATCH_YES)
2463 goto done;
2465 if (gfc_match (" , kind =") != MATCH_YES)
2466 goto syntax;
2468 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2469 goto done;
2471 goto rparen;
2474 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2475 m = char_len_param_value (&len, &deferred);
2476 if (m == MATCH_NO)
2477 goto syntax;
2478 if (m == MATCH_ERROR)
2479 goto done;
2480 seen_length = 1;
2482 m = gfc_match_char (')');
2483 if (m == MATCH_YES)
2484 goto done;
2486 if (gfc_match_char (',') != MATCH_YES)
2487 goto syntax;
2489 gfc_match (" kind ="); /* Gobble optional text. */
2491 m = match_char_kind (&kind, &is_iso_c);
2492 if (m == MATCH_ERROR)
2493 goto done;
2494 if (m == MATCH_NO)
2495 goto syntax;
2497 rparen:
2498 /* Require a right-paren at this point. */
2499 m = gfc_match_char (')');
2500 if (m == MATCH_YES)
2501 goto done;
2503 syntax:
2504 gfc_error ("Syntax error in CHARACTER declaration at %C");
2505 m = MATCH_ERROR;
2506 gfc_free_expr (len);
2507 return m;
2509 done:
2510 /* Deal with character functions after USE and IMPORT statements. */
2511 if (gfc_matching_function)
2513 gfc_free_expr (len);
2514 gfc_undo_symbols ();
2515 return MATCH_YES;
2518 if (m != MATCH_YES)
2520 gfc_free_expr (len);
2521 return m;
2524 /* Do some final massaging of the length values. */
2525 cl = gfc_new_charlen (gfc_current_ns, NULL);
2527 if (seen_length == 0)
2528 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2529 else
2530 cl->length = len;
2532 ts->u.cl = cl;
2533 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2534 ts->deferred = deferred;
2536 /* We have to know if it was a C interoperable kind so we can
2537 do accurate type checking of bind(c) procs, etc. */
2538 if (kind != 0)
2539 /* Mark this as C interoperable if being declared with one
2540 of the named constants from iso_c_binding. */
2541 ts->is_c_interop = is_iso_c;
2542 else if (len != NULL)
2543 /* Here, we might have parsed something such as: character(c_char)
2544 In this case, the parsing code above grabs the c_char when
2545 looking for the length (line 1690, roughly). it's the last
2546 testcase for parsing the kind params of a character variable.
2547 However, it's not actually the length. this seems like it
2548 could be an error.
2549 To see if the user used a C interop kind, test the expr
2550 of the so called length, and see if it's C interoperable. */
2551 ts->is_c_interop = len->ts.is_iso_c;
2553 return MATCH_YES;
2557 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2558 structure to the matched specification. This is necessary for FUNCTION and
2559 IMPLICIT statements.
2561 If implicit_flag is nonzero, then we don't check for the optional
2562 kind specification. Not doing so is needed for matching an IMPLICIT
2563 statement correctly. */
2565 match
2566 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2568 char name[GFC_MAX_SYMBOL_LEN + 1];
2569 gfc_symbol *sym, *dt_sym;
2570 match m;
2571 char c;
2572 bool seen_deferred_kind, matched_type;
2573 const char *dt_name;
2575 /* A belt and braces check that the typespec is correctly being treated
2576 as a deferred characteristic association. */
2577 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2578 && (gfc_current_block ()->result->ts.kind == -1)
2579 && (ts->kind == -1);
2580 gfc_clear_ts (ts);
2581 if (seen_deferred_kind)
2582 ts->kind = -1;
2584 /* Clear the current binding label, in case one is given. */
2585 curr_binding_label = NULL;
2587 if (gfc_match (" byte") == MATCH_YES)
2589 if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2590 == FAILURE)
2591 return MATCH_ERROR;
2593 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2595 gfc_error ("BYTE type used at %C "
2596 "is not available on the target machine");
2597 return MATCH_ERROR;
2600 ts->type = BT_INTEGER;
2601 ts->kind = 1;
2602 return MATCH_YES;
2606 m = gfc_match (" type (");
2607 matched_type = (m == MATCH_YES);
2608 if (matched_type)
2610 gfc_gobble_whitespace ();
2611 if (gfc_peek_ascii_char () == '*')
2613 if ((m = gfc_match ("*)")) != MATCH_YES)
2614 return m;
2615 if (gfc_current_state () == COMP_DERIVED)
2617 gfc_error ("Assumed type at %C is not allowed for components");
2618 return MATCH_ERROR;
2620 if (gfc_notify_std (GFC_STD_F2008_TS, "TS 29113: Assumed type "
2621 "at %C") == FAILURE)
2622 return MATCH_ERROR;
2623 ts->type = BT_ASSUMED;
2624 return MATCH_YES;
2627 m = gfc_match ("%n", name);
2628 matched_type = (m == MATCH_YES);
2631 if ((matched_type && strcmp ("integer", name) == 0)
2632 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2634 ts->type = BT_INTEGER;
2635 ts->kind = gfc_default_integer_kind;
2636 goto get_kind;
2639 if ((matched_type && strcmp ("character", name) == 0)
2640 || (!matched_type && gfc_match (" character") == MATCH_YES))
2642 if (matched_type
2643 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2644 "intrinsic-type-spec at %C") == FAILURE)
2645 return MATCH_ERROR;
2647 ts->type = BT_CHARACTER;
2648 if (implicit_flag == 0)
2649 m = gfc_match_char_spec (ts);
2650 else
2651 m = MATCH_YES;
2653 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2654 m = MATCH_ERROR;
2656 return m;
2659 if ((matched_type && strcmp ("real", name) == 0)
2660 || (!matched_type && gfc_match (" real") == MATCH_YES))
2662 ts->type = BT_REAL;
2663 ts->kind = gfc_default_real_kind;
2664 goto get_kind;
2667 if ((matched_type
2668 && (strcmp ("doubleprecision", name) == 0
2669 || (strcmp ("double", name) == 0
2670 && gfc_match (" precision") == MATCH_YES)))
2671 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2673 if (matched_type
2674 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2675 "intrinsic-type-spec at %C") == FAILURE)
2676 return MATCH_ERROR;
2677 if (matched_type && gfc_match_char (')') != MATCH_YES)
2678 return MATCH_ERROR;
2680 ts->type = BT_REAL;
2681 ts->kind = gfc_default_double_kind;
2682 return MATCH_YES;
2685 if ((matched_type && strcmp ("complex", name) == 0)
2686 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2688 ts->type = BT_COMPLEX;
2689 ts->kind = gfc_default_complex_kind;
2690 goto get_kind;
2693 if ((matched_type
2694 && (strcmp ("doublecomplex", name) == 0
2695 || (strcmp ("double", name) == 0
2696 && gfc_match (" complex") == MATCH_YES)))
2697 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2699 if (gfc_notify_std (GFC_STD_GNU, "Extension: DOUBLE COMPLEX at %C")
2700 == FAILURE)
2701 return MATCH_ERROR;
2703 if (matched_type
2704 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2705 "intrinsic-type-spec at %C") == FAILURE)
2706 return MATCH_ERROR;
2708 if (matched_type && gfc_match_char (')') != MATCH_YES)
2709 return MATCH_ERROR;
2711 ts->type = BT_COMPLEX;
2712 ts->kind = gfc_default_double_kind;
2713 return MATCH_YES;
2716 if ((matched_type && strcmp ("logical", name) == 0)
2717 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2719 ts->type = BT_LOGICAL;
2720 ts->kind = gfc_default_logical_kind;
2721 goto get_kind;
2724 if (matched_type)
2725 m = gfc_match_char (')');
2727 if (m == MATCH_YES)
2728 ts->type = BT_DERIVED;
2729 else
2731 /* Match CLASS declarations. */
2732 m = gfc_match (" class ( * )");
2733 if (m == MATCH_ERROR)
2734 return MATCH_ERROR;
2735 else if (m == MATCH_YES)
2737 gfc_fatal_error ("Unlimited polymorphism at %C not yet supported");
2738 return MATCH_ERROR;
2741 m = gfc_match (" class ( %n )", name);
2742 if (m != MATCH_YES)
2743 return m;
2744 ts->type = BT_CLASS;
2746 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2747 == FAILURE)
2748 return MATCH_ERROR;
2751 /* Defer association of the derived type until the end of the
2752 specification block. However, if the derived type can be
2753 found, add it to the typespec. */
2754 if (gfc_matching_function)
2756 ts->u.derived = NULL;
2757 if (gfc_current_state () != COMP_INTERFACE
2758 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2760 sym = gfc_find_dt_in_generic (sym);
2761 ts->u.derived = sym;
2763 return MATCH_YES;
2766 /* Search for the name but allow the components to be defined later. If
2767 type = -1, this typespec has been seen in a function declaration but
2768 the type could not be accessed at that point. The actual derived type is
2769 stored in a symtree with the first letter of the name capitalized; the
2770 symtree with the all lower-case name contains the associated
2771 generic function. */
2772 dt_name = gfc_get_string ("%c%s",
2773 (char) TOUPPER ((unsigned char) name[0]),
2774 (const char*)&name[1]);
2775 sym = NULL;
2776 dt_sym = NULL;
2777 if (ts->kind != -1)
2779 gfc_get_ha_symbol (name, &sym);
2780 if (sym->generic && gfc_find_symbol (dt_name, NULL, 0, &dt_sym))
2782 gfc_error ("Type name '%s' at %C is ambiguous", name);
2783 return MATCH_ERROR;
2785 if (sym->generic && !dt_sym)
2786 dt_sym = gfc_find_dt_in_generic (sym);
2788 else if (ts->kind == -1)
2790 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2791 || gfc_current_ns->has_import_set;
2792 gfc_find_symbol (name, NULL, iface, &sym);
2793 if (sym && sym->generic && gfc_find_symbol (dt_name, NULL, 1, &dt_sym))
2795 gfc_error ("Type name '%s' at %C is ambiguous", name);
2796 return MATCH_ERROR;
2798 if (sym && sym->generic && !dt_sym)
2799 dt_sym = gfc_find_dt_in_generic (sym);
2801 ts->kind = 0;
2802 if (sym == NULL)
2803 return MATCH_NO;
2806 if ((sym->attr.flavor != FL_UNKNOWN
2807 && !(sym->attr.flavor == FL_PROCEDURE && sym->attr.generic))
2808 || sym->attr.subroutine)
2810 gfc_error ("Type name '%s' at %C conflicts with previously declared "
2811 "entity at %L, which has the same name", name,
2812 &sym->declared_at);
2813 return MATCH_ERROR;
2816 gfc_set_sym_referenced (sym);
2817 if (!sym->attr.generic
2818 && gfc_add_generic (&sym->attr, sym->name, NULL) == FAILURE)
2819 return MATCH_ERROR;
2821 if (!sym->attr.function
2822 && gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
2823 return MATCH_ERROR;
2825 if (!dt_sym)
2827 gfc_interface *intr, *head;
2829 /* Use upper case to save the actual derived-type symbol. */
2830 gfc_get_symbol (dt_name, NULL, &dt_sym);
2831 dt_sym->name = gfc_get_string (sym->name);
2832 head = sym->generic;
2833 intr = gfc_get_interface ();
2834 intr->sym = dt_sym;
2835 intr->where = gfc_current_locus;
2836 intr->next = head;
2837 sym->generic = intr;
2838 sym->attr.if_source = IFSRC_DECL;
2841 gfc_set_sym_referenced (dt_sym);
2843 if (dt_sym->attr.flavor != FL_DERIVED
2844 && gfc_add_flavor (&dt_sym->attr, FL_DERIVED, sym->name, NULL)
2845 == FAILURE)
2846 return MATCH_ERROR;
2848 ts->u.derived = dt_sym;
2850 return MATCH_YES;
2852 get_kind:
2853 if (matched_type
2854 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2855 "intrinsic-type-spec at %C") == FAILURE)
2856 return MATCH_ERROR;
2858 /* For all types except double, derived and character, look for an
2859 optional kind specifier. MATCH_NO is actually OK at this point. */
2860 if (implicit_flag == 1)
2862 if (matched_type && gfc_match_char (')') != MATCH_YES)
2863 return MATCH_ERROR;
2865 return MATCH_YES;
2868 if (gfc_current_form == FORM_FREE)
2870 c = gfc_peek_ascii_char ();
2871 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2872 && c != ':' && c != ',')
2874 if (matched_type && c == ')')
2876 gfc_next_ascii_char ();
2877 return MATCH_YES;
2879 return MATCH_NO;
2883 m = gfc_match_kind_spec (ts, false);
2884 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2885 m = gfc_match_old_kind_spec (ts);
2887 if (matched_type && gfc_match_char (')') != MATCH_YES)
2888 return MATCH_ERROR;
2890 /* Defer association of the KIND expression of function results
2891 until after USE and IMPORT statements. */
2892 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2893 || gfc_matching_function)
2894 return MATCH_YES;
2896 if (m == MATCH_NO)
2897 m = MATCH_YES; /* No kind specifier found. */
2899 return m;
2903 /* Match an IMPLICIT NONE statement. Actually, this statement is
2904 already matched in parse.c, or we would not end up here in the
2905 first place. So the only thing we need to check, is if there is
2906 trailing garbage. If not, the match is successful. */
2908 match
2909 gfc_match_implicit_none (void)
2911 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2915 /* Match the letter range(s) of an IMPLICIT statement. */
2917 static match
2918 match_implicit_range (void)
2920 char c, c1, c2;
2921 int inner;
2922 locus cur_loc;
2924 cur_loc = gfc_current_locus;
2926 gfc_gobble_whitespace ();
2927 c = gfc_next_ascii_char ();
2928 if (c != '(')
2930 gfc_error ("Missing character range in IMPLICIT at %C");
2931 goto bad;
2934 inner = 1;
2935 while (inner)
2937 gfc_gobble_whitespace ();
2938 c1 = gfc_next_ascii_char ();
2939 if (!ISALPHA (c1))
2940 goto bad;
2942 gfc_gobble_whitespace ();
2943 c = gfc_next_ascii_char ();
2945 switch (c)
2947 case ')':
2948 inner = 0; /* Fall through. */
2950 case ',':
2951 c2 = c1;
2952 break;
2954 case '-':
2955 gfc_gobble_whitespace ();
2956 c2 = gfc_next_ascii_char ();
2957 if (!ISALPHA (c2))
2958 goto bad;
2960 gfc_gobble_whitespace ();
2961 c = gfc_next_ascii_char ();
2963 if ((c != ',') && (c != ')'))
2964 goto bad;
2965 if (c == ')')
2966 inner = 0;
2968 break;
2970 default:
2971 goto bad;
2974 if (c1 > c2)
2976 gfc_error ("Letters must be in alphabetic order in "
2977 "IMPLICIT statement at %C");
2978 goto bad;
2981 /* See if we can add the newly matched range to the pending
2982 implicits from this IMPLICIT statement. We do not check for
2983 conflicts with whatever earlier IMPLICIT statements may have
2984 set. This is done when we've successfully finished matching
2985 the current one. */
2986 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2987 goto bad;
2990 return MATCH_YES;
2992 bad:
2993 gfc_syntax_error (ST_IMPLICIT);
2995 gfc_current_locus = cur_loc;
2996 return MATCH_ERROR;
3000 /* Match an IMPLICIT statement, storing the types for
3001 gfc_set_implicit() if the statement is accepted by the parser.
3002 There is a strange looking, but legal syntactic construction
3003 possible. It looks like:
3005 IMPLICIT INTEGER (a-b) (c-d)
3007 This is legal if "a-b" is a constant expression that happens to
3008 equal one of the legal kinds for integers. The real problem
3009 happens with an implicit specification that looks like:
3011 IMPLICIT INTEGER (a-b)
3013 In this case, a typespec matcher that is "greedy" (as most of the
3014 matchers are) gobbles the character range as a kindspec, leaving
3015 nothing left. We therefore have to go a bit more slowly in the
3016 matching process by inhibiting the kindspec checking during
3017 typespec matching and checking for a kind later. */
3019 match
3020 gfc_match_implicit (void)
3022 gfc_typespec ts;
3023 locus cur_loc;
3024 char c;
3025 match m;
3027 gfc_clear_ts (&ts);
3029 /* We don't allow empty implicit statements. */
3030 if (gfc_match_eos () == MATCH_YES)
3032 gfc_error ("Empty IMPLICIT statement at %C");
3033 return MATCH_ERROR;
3038 /* First cleanup. */
3039 gfc_clear_new_implicit ();
3041 /* A basic type is mandatory here. */
3042 m = gfc_match_decl_type_spec (&ts, 1);
3043 if (m == MATCH_ERROR)
3044 goto error;
3045 if (m == MATCH_NO)
3046 goto syntax;
3048 cur_loc = gfc_current_locus;
3049 m = match_implicit_range ();
3051 if (m == MATCH_YES)
3053 /* We may have <TYPE> (<RANGE>). */
3054 gfc_gobble_whitespace ();
3055 c = gfc_next_ascii_char ();
3056 if ((c == '\n') || (c == ','))
3058 /* Check for CHARACTER with no length parameter. */
3059 if (ts.type == BT_CHARACTER && !ts.u.cl)
3061 ts.kind = gfc_default_character_kind;
3062 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
3063 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
3064 NULL, 1);
3067 /* Record the Successful match. */
3068 if (gfc_merge_new_implicit (&ts) != SUCCESS)
3069 return MATCH_ERROR;
3070 continue;
3073 gfc_current_locus = cur_loc;
3076 /* Discard the (incorrectly) matched range. */
3077 gfc_clear_new_implicit ();
3079 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3080 if (ts.type == BT_CHARACTER)
3081 m = gfc_match_char_spec (&ts);
3082 else
3084 m = gfc_match_kind_spec (&ts, false);
3085 if (m == MATCH_NO)
3087 m = gfc_match_old_kind_spec (&ts);
3088 if (m == MATCH_ERROR)
3089 goto error;
3090 if (m == MATCH_NO)
3091 goto syntax;
3094 if (m == MATCH_ERROR)
3095 goto error;
3097 m = match_implicit_range ();
3098 if (m == MATCH_ERROR)
3099 goto error;
3100 if (m == MATCH_NO)
3101 goto syntax;
3103 gfc_gobble_whitespace ();
3104 c = gfc_next_ascii_char ();
3105 if ((c != '\n') && (c != ','))
3106 goto syntax;
3108 if (gfc_merge_new_implicit (&ts) != SUCCESS)
3109 return MATCH_ERROR;
3111 while (c == ',');
3113 return MATCH_YES;
3115 syntax:
3116 gfc_syntax_error (ST_IMPLICIT);
3118 error:
3119 return MATCH_ERROR;
3123 match
3124 gfc_match_import (void)
3126 char name[GFC_MAX_SYMBOL_LEN + 1];
3127 match m;
3128 gfc_symbol *sym;
3129 gfc_symtree *st;
3131 if (gfc_current_ns->proc_name == NULL
3132 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
3134 gfc_error ("IMPORT statement at %C only permitted in "
3135 "an INTERFACE body");
3136 return MATCH_ERROR;
3139 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
3140 == FAILURE)
3141 return MATCH_ERROR;
3143 if (gfc_match_eos () == MATCH_YES)
3145 /* All host variables should be imported. */
3146 gfc_current_ns->has_import_set = 1;
3147 return MATCH_YES;
3150 if (gfc_match (" ::") == MATCH_YES)
3152 if (gfc_match_eos () == MATCH_YES)
3154 gfc_error ("Expecting list of named entities at %C");
3155 return MATCH_ERROR;
3159 for(;;)
3161 sym = NULL;
3162 m = gfc_match (" %n", name);
3163 switch (m)
3165 case MATCH_YES:
3166 if (gfc_current_ns->parent != NULL
3167 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
3169 gfc_error ("Type name '%s' at %C is ambiguous", name);
3170 return MATCH_ERROR;
3172 else if (!sym && gfc_current_ns->proc_name->ns->parent != NULL
3173 && gfc_find_symbol (name,
3174 gfc_current_ns->proc_name->ns->parent,
3175 1, &sym))
3177 gfc_error ("Type name '%s' at %C is ambiguous", name);
3178 return MATCH_ERROR;
3181 if (sym == NULL)
3183 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3184 "at %C - does not exist.", name);
3185 return MATCH_ERROR;
3188 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
3190 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3191 "at %C.", name);
3192 goto next_item;
3195 st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
3196 st->n.sym = sym;
3197 sym->refs++;
3198 sym->attr.imported = 1;
3200 if (sym->attr.generic && (sym = gfc_find_dt_in_generic (sym)))
3202 /* The actual derived type is stored in a symtree with the first
3203 letter of the name capitalized; the symtree with the all
3204 lower-case name contains the associated generic function. */
3205 st = gfc_new_symtree (&gfc_current_ns->sym_root,
3206 gfc_get_string ("%c%s",
3207 (char) TOUPPER ((unsigned char) sym->name[0]),
3208 &sym->name[1]));
3209 st->n.sym = sym;
3210 sym->refs++;
3211 sym->attr.imported = 1;
3214 goto next_item;
3216 case MATCH_NO:
3217 break;
3219 case MATCH_ERROR:
3220 return MATCH_ERROR;
3223 next_item:
3224 if (gfc_match_eos () == MATCH_YES)
3225 break;
3226 if (gfc_match_char (',') != MATCH_YES)
3227 goto syntax;
3230 return MATCH_YES;
3232 syntax:
3233 gfc_error ("Syntax error in IMPORT statement at %C");
3234 return MATCH_ERROR;
3238 /* A minimal implementation of gfc_match without whitespace, escape
3239 characters or variable arguments. Returns true if the next
3240 characters match the TARGET template exactly. */
3242 static bool
3243 match_string_p (const char *target)
3245 const char *p;
3247 for (p = target; *p; p++)
3248 if ((char) gfc_next_ascii_char () != *p)
3249 return false;
3250 return true;
3253 /* Matches an attribute specification including array specs. If
3254 successful, leaves the variables current_attr and current_as
3255 holding the specification. Also sets the colon_seen variable for
3256 later use by matchers associated with initializations.
3258 This subroutine is a little tricky in the sense that we don't know
3259 if we really have an attr-spec until we hit the double colon.
3260 Until that time, we can only return MATCH_NO. This forces us to
3261 check for duplicate specification at this level. */
3263 static match
3264 match_attr_spec (void)
3266 /* Modifiers that can exist in a type statement. */
3267 enum
3268 { GFC_DECL_BEGIN = 0,
3269 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3270 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3271 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3272 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3273 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3274 DECL_NONE, GFC_DECL_END /* Sentinel */
3277 /* GFC_DECL_END is the sentinel, index starts at 0. */
3278 #define NUM_DECL GFC_DECL_END
3280 locus start, seen_at[NUM_DECL];
3281 int seen[NUM_DECL];
3282 unsigned int d;
3283 const char *attr;
3284 match m;
3285 gfc_try t;
3287 gfc_clear_attr (&current_attr);
3288 start = gfc_current_locus;
3290 current_as = NULL;
3291 colon_seen = 0;
3293 /* See if we get all of the keywords up to the final double colon. */
3294 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3295 seen[d] = 0;
3297 for (;;)
3299 char ch;
3301 d = DECL_NONE;
3302 gfc_gobble_whitespace ();
3304 ch = gfc_next_ascii_char ();
3305 if (ch == ':')
3307 /* This is the successful exit condition for the loop. */
3308 if (gfc_next_ascii_char () == ':')
3309 break;
3311 else if (ch == ',')
3313 gfc_gobble_whitespace ();
3314 switch (gfc_peek_ascii_char ())
3316 case 'a':
3317 gfc_next_ascii_char ();
3318 switch (gfc_next_ascii_char ())
3320 case 'l':
3321 if (match_string_p ("locatable"))
3323 /* Matched "allocatable". */
3324 d = DECL_ALLOCATABLE;
3326 break;
3328 case 's':
3329 if (match_string_p ("ynchronous"))
3331 /* Matched "asynchronous". */
3332 d = DECL_ASYNCHRONOUS;
3334 break;
3336 break;
3338 case 'b':
3339 /* Try and match the bind(c). */
3340 m = gfc_match_bind_c (NULL, true);
3341 if (m == MATCH_YES)
3342 d = DECL_IS_BIND_C;
3343 else if (m == MATCH_ERROR)
3344 goto cleanup;
3345 break;
3347 case 'c':
3348 gfc_next_ascii_char ();
3349 if ('o' != gfc_next_ascii_char ())
3350 break;
3351 switch (gfc_next_ascii_char ())
3353 case 'd':
3354 if (match_string_p ("imension"))
3356 d = DECL_CODIMENSION;
3357 break;
3359 case 'n':
3360 if (match_string_p ("tiguous"))
3362 d = DECL_CONTIGUOUS;
3363 break;
3366 break;
3368 case 'd':
3369 if (match_string_p ("dimension"))
3370 d = DECL_DIMENSION;
3371 break;
3373 case 'e':
3374 if (match_string_p ("external"))
3375 d = DECL_EXTERNAL;
3376 break;
3378 case 'i':
3379 if (match_string_p ("int"))
3381 ch = gfc_next_ascii_char ();
3382 if (ch == 'e')
3384 if (match_string_p ("nt"))
3386 /* Matched "intent". */
3387 /* TODO: Call match_intent_spec from here. */
3388 if (gfc_match (" ( in out )") == MATCH_YES)
3389 d = DECL_INOUT;
3390 else if (gfc_match (" ( in )") == MATCH_YES)
3391 d = DECL_IN;
3392 else if (gfc_match (" ( out )") == MATCH_YES)
3393 d = DECL_OUT;
3396 else if (ch == 'r')
3398 if (match_string_p ("insic"))
3400 /* Matched "intrinsic". */
3401 d = DECL_INTRINSIC;
3405 break;
3407 case 'o':
3408 if (match_string_p ("optional"))
3409 d = DECL_OPTIONAL;
3410 break;
3412 case 'p':
3413 gfc_next_ascii_char ();
3414 switch (gfc_next_ascii_char ())
3416 case 'a':
3417 if (match_string_p ("rameter"))
3419 /* Matched "parameter". */
3420 d = DECL_PARAMETER;
3422 break;
3424 case 'o':
3425 if (match_string_p ("inter"))
3427 /* Matched "pointer". */
3428 d = DECL_POINTER;
3430 break;
3432 case 'r':
3433 ch = gfc_next_ascii_char ();
3434 if (ch == 'i')
3436 if (match_string_p ("vate"))
3438 /* Matched "private". */
3439 d = DECL_PRIVATE;
3442 else if (ch == 'o')
3444 if (match_string_p ("tected"))
3446 /* Matched "protected". */
3447 d = DECL_PROTECTED;
3450 break;
3452 case 'u':
3453 if (match_string_p ("blic"))
3455 /* Matched "public". */
3456 d = DECL_PUBLIC;
3458 break;
3460 break;
3462 case 's':
3463 if (match_string_p ("save"))
3464 d = DECL_SAVE;
3465 break;
3467 case 't':
3468 if (match_string_p ("target"))
3469 d = DECL_TARGET;
3470 break;
3472 case 'v':
3473 gfc_next_ascii_char ();
3474 ch = gfc_next_ascii_char ();
3475 if (ch == 'a')
3477 if (match_string_p ("lue"))
3479 /* Matched "value". */
3480 d = DECL_VALUE;
3483 else if (ch == 'o')
3485 if (match_string_p ("latile"))
3487 /* Matched "volatile". */
3488 d = DECL_VOLATILE;
3491 break;
3495 /* No double colon and no recognizable decl_type, so assume that
3496 we've been looking at something else the whole time. */
3497 if (d == DECL_NONE)
3499 m = MATCH_NO;
3500 goto cleanup;
3503 /* Check to make sure any parens are paired up correctly. */
3504 if (gfc_match_parens () == MATCH_ERROR)
3506 m = MATCH_ERROR;
3507 goto cleanup;
3510 seen[d]++;
3511 seen_at[d] = gfc_current_locus;
3513 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3515 gfc_array_spec *as = NULL;
3517 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3518 d == DECL_CODIMENSION);
3520 if (current_as == NULL)
3521 current_as = as;
3522 else if (m == MATCH_YES)
3524 merge_array_spec (as, current_as, false);
3525 free (as);
3528 if (m == MATCH_NO)
3530 if (d == DECL_CODIMENSION)
3531 gfc_error ("Missing codimension specification at %C");
3532 else
3533 gfc_error ("Missing dimension specification at %C");
3534 m = MATCH_ERROR;
3537 if (m == MATCH_ERROR)
3538 goto cleanup;
3542 /* Since we've seen a double colon, we have to be looking at an
3543 attr-spec. This means that we can now issue errors. */
3544 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3545 if (seen[d] > 1)
3547 switch (d)
3549 case DECL_ALLOCATABLE:
3550 attr = "ALLOCATABLE";
3551 break;
3552 case DECL_ASYNCHRONOUS:
3553 attr = "ASYNCHRONOUS";
3554 break;
3555 case DECL_CODIMENSION:
3556 attr = "CODIMENSION";
3557 break;
3558 case DECL_CONTIGUOUS:
3559 attr = "CONTIGUOUS";
3560 break;
3561 case DECL_DIMENSION:
3562 attr = "DIMENSION";
3563 break;
3564 case DECL_EXTERNAL:
3565 attr = "EXTERNAL";
3566 break;
3567 case DECL_IN:
3568 attr = "INTENT (IN)";
3569 break;
3570 case DECL_OUT:
3571 attr = "INTENT (OUT)";
3572 break;
3573 case DECL_INOUT:
3574 attr = "INTENT (IN OUT)";
3575 break;
3576 case DECL_INTRINSIC:
3577 attr = "INTRINSIC";
3578 break;
3579 case DECL_OPTIONAL:
3580 attr = "OPTIONAL";
3581 break;
3582 case DECL_PARAMETER:
3583 attr = "PARAMETER";
3584 break;
3585 case DECL_POINTER:
3586 attr = "POINTER";
3587 break;
3588 case DECL_PROTECTED:
3589 attr = "PROTECTED";
3590 break;
3591 case DECL_PRIVATE:
3592 attr = "PRIVATE";
3593 break;
3594 case DECL_PUBLIC:
3595 attr = "PUBLIC";
3596 break;
3597 case DECL_SAVE:
3598 attr = "SAVE";
3599 break;
3600 case DECL_TARGET:
3601 attr = "TARGET";
3602 break;
3603 case DECL_IS_BIND_C:
3604 attr = "IS_BIND_C";
3605 break;
3606 case DECL_VALUE:
3607 attr = "VALUE";
3608 break;
3609 case DECL_VOLATILE:
3610 attr = "VOLATILE";
3611 break;
3612 default:
3613 attr = NULL; /* This shouldn't happen. */
3616 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3617 m = MATCH_ERROR;
3618 goto cleanup;
3621 /* Now that we've dealt with duplicate attributes, add the attributes
3622 to the current attribute. */
3623 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3625 if (seen[d] == 0)
3626 continue;
3628 if (gfc_current_state () == COMP_DERIVED
3629 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3630 && d != DECL_POINTER && d != DECL_PRIVATE
3631 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3633 if (d == DECL_ALLOCATABLE)
3635 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3636 "attribute at %C in a TYPE definition")
3637 == FAILURE)
3639 m = MATCH_ERROR;
3640 goto cleanup;
3643 else
3645 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3646 &seen_at[d]);
3647 m = MATCH_ERROR;
3648 goto cleanup;
3652 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3653 && gfc_current_state () != COMP_MODULE)
3655 if (d == DECL_PRIVATE)
3656 attr = "PRIVATE";
3657 else
3658 attr = "PUBLIC";
3659 if (gfc_current_state () == COMP_DERIVED
3660 && gfc_state_stack->previous
3661 && gfc_state_stack->previous->state == COMP_MODULE)
3663 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3664 "at %L in a TYPE definition", attr,
3665 &seen_at[d])
3666 == FAILURE)
3668 m = MATCH_ERROR;
3669 goto cleanup;
3672 else
3674 gfc_error ("%s attribute at %L is not allowed outside of the "
3675 "specification part of a module", attr, &seen_at[d]);
3676 m = MATCH_ERROR;
3677 goto cleanup;
3681 switch (d)
3683 case DECL_ALLOCATABLE:
3684 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3685 break;
3687 case DECL_ASYNCHRONOUS:
3688 if (gfc_notify_std (GFC_STD_F2003,
3689 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3690 == FAILURE)
3691 t = FAILURE;
3692 else
3693 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3694 break;
3696 case DECL_CODIMENSION:
3697 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3698 break;
3700 case DECL_CONTIGUOUS:
3701 if (gfc_notify_std (GFC_STD_F2008,
3702 "Fortran 2008: CONTIGUOUS attribute at %C")
3703 == FAILURE)
3704 t = FAILURE;
3705 else
3706 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3707 break;
3709 case DECL_DIMENSION:
3710 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3711 break;
3713 case DECL_EXTERNAL:
3714 t = gfc_add_external (&current_attr, &seen_at[d]);
3715 break;
3717 case DECL_IN:
3718 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3719 break;
3721 case DECL_OUT:
3722 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3723 break;
3725 case DECL_INOUT:
3726 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3727 break;
3729 case DECL_INTRINSIC:
3730 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3731 break;
3733 case DECL_OPTIONAL:
3734 t = gfc_add_optional (&current_attr, &seen_at[d]);
3735 break;
3737 case DECL_PARAMETER:
3738 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3739 break;
3741 case DECL_POINTER:
3742 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3743 break;
3745 case DECL_PROTECTED:
3746 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3748 gfc_error ("PROTECTED at %C only allowed in specification "
3749 "part of a module");
3750 t = FAILURE;
3751 break;
3754 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3755 "attribute at %C")
3756 == FAILURE)
3757 t = FAILURE;
3758 else
3759 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3760 break;
3762 case DECL_PRIVATE:
3763 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3764 &seen_at[d]);
3765 break;
3767 case DECL_PUBLIC:
3768 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3769 &seen_at[d]);
3770 break;
3772 case DECL_SAVE:
3773 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3774 break;
3776 case DECL_TARGET:
3777 t = gfc_add_target (&current_attr, &seen_at[d]);
3778 break;
3780 case DECL_IS_BIND_C:
3781 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3782 break;
3784 case DECL_VALUE:
3785 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3786 "at %C")
3787 == FAILURE)
3788 t = FAILURE;
3789 else
3790 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3791 break;
3793 case DECL_VOLATILE:
3794 if (gfc_notify_std (GFC_STD_F2003,
3795 "Fortran 2003: VOLATILE attribute at %C")
3796 == FAILURE)
3797 t = FAILURE;
3798 else
3799 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3800 break;
3802 default:
3803 gfc_internal_error ("match_attr_spec(): Bad attribute");
3806 if (t == FAILURE)
3808 m = MATCH_ERROR;
3809 goto cleanup;
3813 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
3814 if (gfc_current_state () == COMP_MODULE && !current_attr.save
3815 && (gfc_option.allow_std & GFC_STD_F2008) != 0)
3816 current_attr.save = SAVE_IMPLICIT;
3818 colon_seen = 1;
3819 return MATCH_YES;
3821 cleanup:
3822 gfc_current_locus = start;
3823 gfc_free_array_spec (current_as);
3824 current_as = NULL;
3825 return m;
3829 /* Set the binding label, dest_label, either with the binding label
3830 stored in the given gfc_typespec, ts, or if none was provided, it
3831 will be the symbol name in all lower case, as required by the draft
3832 (J3/04-007, section 15.4.1). If a binding label was given and
3833 there is more than one argument (num_idents), it is an error. */
3835 static gfc_try
3836 set_binding_label (const char **dest_label, const char *sym_name,
3837 int num_idents)
3839 if (num_idents > 1 && has_name_equals)
3841 gfc_error ("Multiple identifiers provided with "
3842 "single NAME= specifier at %C");
3843 return FAILURE;
3846 if (curr_binding_label)
3847 /* Binding label given; store in temp holder till have sym. */
3848 *dest_label = curr_binding_label;
3849 else
3851 /* No binding label given, and the NAME= specifier did not exist,
3852 which means there was no NAME="". */
3853 if (sym_name != NULL && has_name_equals == 0)
3854 *dest_label = IDENTIFIER_POINTER (get_identifier (sym_name));
3857 return SUCCESS;
3861 /* Set the status of the given common block as being BIND(C) or not,
3862 depending on the given parameter, is_bind_c. */
3864 void
3865 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3867 com_block->is_bind_c = is_bind_c;
3868 return;
3872 /* Verify that the given gfc_typespec is for a C interoperable type. */
3874 gfc_try
3875 gfc_verify_c_interop (gfc_typespec *ts)
3877 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3878 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3879 ? SUCCESS : FAILURE;
3880 else if (ts->type == BT_CLASS)
3881 return FAILURE;
3882 else if (ts->is_c_interop != 1 && ts->type != BT_ASSUMED)
3883 return FAILURE;
3885 return SUCCESS;
3889 /* Verify that the variables of a given common block, which has been
3890 defined with the attribute specifier bind(c), to be of a C
3891 interoperable type. Errors will be reported here, if
3892 encountered. */
3894 gfc_try
3895 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3897 gfc_symbol *curr_sym = NULL;
3898 gfc_try retval = SUCCESS;
3900 curr_sym = com_block->head;
3902 /* Make sure we have at least one symbol. */
3903 if (curr_sym == NULL)
3904 return retval;
3906 /* Here we know we have a symbol, so we'll execute this loop
3907 at least once. */
3910 /* The second to last param, 1, says this is in a common block. */
3911 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3912 curr_sym = curr_sym->common_next;
3913 } while (curr_sym != NULL);
3915 return retval;
3919 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3920 an appropriate error message is reported. */
3922 gfc_try
3923 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3924 int is_in_common, gfc_common_head *com_block)
3926 bool bind_c_function = false;
3927 gfc_try retval = SUCCESS;
3929 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3930 bind_c_function = true;
3932 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3934 tmp_sym = tmp_sym->result;
3935 /* Make sure it wasn't an implicitly typed result. */
3936 if (tmp_sym->attr.implicit_type && gfc_option.warn_c_binding_type)
3938 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3939 "%L may not be C interoperable", tmp_sym->name,
3940 &tmp_sym->declared_at);
3941 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3942 /* Mark it as C interoperable to prevent duplicate warnings. */
3943 tmp_sym->ts.is_c_interop = 1;
3944 tmp_sym->attr.is_c_interop = 1;
3948 /* Here, we know we have the bind(c) attribute, so if we have
3949 enough type info, then verify that it's a C interop kind.
3950 The info could be in the symbol already, or possibly still in
3951 the given ts (current_ts), so look in both. */
3952 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3954 if (gfc_verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3956 /* See if we're dealing with a sym in a common block or not. */
3957 if (is_in_common == 1 && gfc_option.warn_c_binding_type)
3959 gfc_warning ("Variable '%s' in common block '%s' at %L "
3960 "may not be a C interoperable "
3961 "kind though common block '%s' is BIND(C)",
3962 tmp_sym->name, com_block->name,
3963 &(tmp_sym->declared_at), com_block->name);
3965 else
3967 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3968 gfc_error ("Type declaration '%s' at %L is not C "
3969 "interoperable but it is BIND(C)",
3970 tmp_sym->name, &(tmp_sym->declared_at));
3971 else if (gfc_option.warn_c_binding_type)
3972 gfc_warning ("Variable '%s' at %L "
3973 "may not be a C interoperable "
3974 "kind but it is bind(c)",
3975 tmp_sym->name, &(tmp_sym->declared_at));
3979 /* Variables declared w/in a common block can't be bind(c)
3980 since there's no way for C to see these variables, so there's
3981 semantically no reason for the attribute. */
3982 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3984 gfc_error ("Variable '%s' in common block '%s' at "
3985 "%L cannot be declared with BIND(C) "
3986 "since it is not a global",
3987 tmp_sym->name, com_block->name,
3988 &(tmp_sym->declared_at));
3989 retval = FAILURE;
3992 /* Scalar variables that are bind(c) can not have the pointer
3993 or allocatable attributes. */
3994 if (tmp_sym->attr.is_bind_c == 1)
3996 if (tmp_sym->attr.pointer == 1)
3998 gfc_error ("Variable '%s' at %L cannot have both the "
3999 "POINTER and BIND(C) attributes",
4000 tmp_sym->name, &(tmp_sym->declared_at));
4001 retval = FAILURE;
4004 if (tmp_sym->attr.allocatable == 1)
4006 gfc_error ("Variable '%s' at %L cannot have both the "
4007 "ALLOCATABLE and BIND(C) attributes",
4008 tmp_sym->name, &(tmp_sym->declared_at));
4009 retval = FAILURE;
4014 /* If it is a BIND(C) function, make sure the return value is a
4015 scalar value. The previous tests in this function made sure
4016 the type is interoperable. */
4017 if (bind_c_function && tmp_sym->as != NULL)
4018 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4019 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
4021 /* BIND(C) functions can not return a character string. */
4022 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
4023 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
4024 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
4025 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
4026 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
4027 "be a character string", tmp_sym->name,
4028 &(tmp_sym->declared_at));
4031 /* See if the symbol has been marked as private. If it has, make sure
4032 there is no binding label and warn the user if there is one. */
4033 if (tmp_sym->attr.access == ACCESS_PRIVATE
4034 && tmp_sym->binding_label)
4035 /* Use gfc_warning_now because we won't say that the symbol fails
4036 just because of this. */
4037 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
4038 "given the binding label '%s'", tmp_sym->name,
4039 &(tmp_sym->declared_at), tmp_sym->binding_label);
4041 return retval;
4045 /* Set the appropriate fields for a symbol that's been declared as
4046 BIND(C) (the is_bind_c flag and the binding label), and verify that
4047 the type is C interoperable. Errors are reported by the functions
4048 used to set/test these fields. */
4050 gfc_try
4051 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
4053 gfc_try retval = SUCCESS;
4055 /* TODO: Do we need to make sure the vars aren't marked private? */
4057 /* Set the is_bind_c bit in symbol_attribute. */
4058 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
4060 if (set_binding_label (&tmp_sym->binding_label, tmp_sym->name,
4061 num_idents) != SUCCESS)
4062 return FAILURE;
4064 return retval;
4068 /* Set the fields marking the given common block as BIND(C), including
4069 a binding label, and report any errors encountered. */
4071 gfc_try
4072 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
4074 gfc_try retval = SUCCESS;
4076 /* destLabel, common name, typespec (which may have binding label). */
4077 if (set_binding_label (&com_block->binding_label, com_block->name,
4078 num_idents)
4079 != SUCCESS)
4080 return FAILURE;
4082 /* Set the given common block (com_block) to being bind(c) (1). */
4083 set_com_block_bind_c (com_block, 1);
4085 return retval;
4089 /* Retrieve the list of one or more identifiers that the given bind(c)
4090 attribute applies to. */
4092 gfc_try
4093 get_bind_c_idents (void)
4095 char name[GFC_MAX_SYMBOL_LEN + 1];
4096 int num_idents = 0;
4097 gfc_symbol *tmp_sym = NULL;
4098 match found_id;
4099 gfc_common_head *com_block = NULL;
4101 if (gfc_match_name (name) == MATCH_YES)
4103 found_id = MATCH_YES;
4104 gfc_get_ha_symbol (name, &tmp_sym);
4106 else if (match_common_name (name) == MATCH_YES)
4108 found_id = MATCH_YES;
4109 com_block = gfc_get_common (name, 0);
4111 else
4113 gfc_error ("Need either entity or common block name for "
4114 "attribute specification statement at %C");
4115 return FAILURE;
4118 /* Save the current identifier and look for more. */
4121 /* Increment the number of identifiers found for this spec stmt. */
4122 num_idents++;
4124 /* Make sure we have a sym or com block, and verify that it can
4125 be bind(c). Set the appropriate field(s) and look for more
4126 identifiers. */
4127 if (tmp_sym != NULL || com_block != NULL)
4129 if (tmp_sym != NULL)
4131 if (set_verify_bind_c_sym (tmp_sym, num_idents)
4132 != SUCCESS)
4133 return FAILURE;
4135 else
4137 if (set_verify_bind_c_com_block(com_block, num_idents)
4138 != SUCCESS)
4139 return FAILURE;
4142 /* Look to see if we have another identifier. */
4143 tmp_sym = NULL;
4144 if (gfc_match_eos () == MATCH_YES)
4145 found_id = MATCH_NO;
4146 else if (gfc_match_char (',') != MATCH_YES)
4147 found_id = MATCH_NO;
4148 else if (gfc_match_name (name) == MATCH_YES)
4150 found_id = MATCH_YES;
4151 gfc_get_ha_symbol (name, &tmp_sym);
4153 else if (match_common_name (name) == MATCH_YES)
4155 found_id = MATCH_YES;
4156 com_block = gfc_get_common (name, 0);
4158 else
4160 gfc_error ("Missing entity or common block name for "
4161 "attribute specification statement at %C");
4162 return FAILURE;
4165 else
4167 gfc_internal_error ("Missing symbol");
4169 } while (found_id == MATCH_YES);
4171 /* if we get here we were successful */
4172 return SUCCESS;
4176 /* Try and match a BIND(C) attribute specification statement. */
4178 match
4179 gfc_match_bind_c_stmt (void)
4181 match found_match = MATCH_NO;
4182 gfc_typespec *ts;
4184 ts = &current_ts;
4186 /* This may not be necessary. */
4187 gfc_clear_ts (ts);
4188 /* Clear the temporary binding label holder. */
4189 curr_binding_label = NULL;
4191 /* Look for the bind(c). */
4192 found_match = gfc_match_bind_c (NULL, true);
4194 if (found_match == MATCH_YES)
4196 /* Look for the :: now, but it is not required. */
4197 gfc_match (" :: ");
4199 /* Get the identifier(s) that needs to be updated. This may need to
4200 change to hand the flag(s) for the attr specified so all identifiers
4201 found can have all appropriate parts updated (assuming that the same
4202 spec stmt can have multiple attrs, such as both bind(c) and
4203 allocatable...). */
4204 if (get_bind_c_idents () != SUCCESS)
4205 /* Error message should have printed already. */
4206 return MATCH_ERROR;
4209 return found_match;
4213 /* Match a data declaration statement. */
4215 match
4216 gfc_match_data_decl (void)
4218 gfc_symbol *sym;
4219 match m;
4220 int elem;
4222 num_idents_on_line = 0;
4224 m = gfc_match_decl_type_spec (&current_ts, 0);
4225 if (m != MATCH_YES)
4226 return m;
4228 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4229 && gfc_current_state () != COMP_DERIVED)
4231 sym = gfc_use_derived (current_ts.u.derived);
4233 if (sym == NULL)
4235 m = MATCH_ERROR;
4236 goto cleanup;
4239 current_ts.u.derived = sym;
4242 m = match_attr_spec ();
4243 if (m == MATCH_ERROR)
4245 m = MATCH_NO;
4246 goto cleanup;
4249 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4250 && current_ts.u.derived->components == NULL
4251 && !current_ts.u.derived->attr.zero_comp)
4254 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4255 goto ok;
4257 gfc_find_symbol (current_ts.u.derived->name,
4258 current_ts.u.derived->ns->parent, 1, &sym);
4260 /* Any symbol that we find had better be a type definition
4261 which has its components defined. */
4262 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4263 && (current_ts.u.derived->components != NULL
4264 || current_ts.u.derived->attr.zero_comp))
4265 goto ok;
4267 /* Now we have an error, which we signal, and then fix up
4268 because the knock-on is plain and simple confusing. */
4269 gfc_error_now ("Derived type at %C has not been previously defined "
4270 "and so cannot appear in a derived type definition");
4271 current_attr.pointer = 1;
4272 goto ok;
4276 /* If we have an old-style character declaration, and no new-style
4277 attribute specifications, then there a comma is optional between
4278 the type specification and the variable list. */
4279 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4280 gfc_match_char (',');
4282 /* Give the types/attributes to symbols that follow. Give the element
4283 a number so that repeat character length expressions can be copied. */
4284 elem = 1;
4285 for (;;)
4287 num_idents_on_line++;
4288 m = variable_decl (elem++);
4289 if (m == MATCH_ERROR)
4290 goto cleanup;
4291 if (m == MATCH_NO)
4292 break;
4294 if (gfc_match_eos () == MATCH_YES)
4295 goto cleanup;
4296 if (gfc_match_char (',') != MATCH_YES)
4297 break;
4300 if (gfc_error_flag_test () == 0)
4301 gfc_error ("Syntax error in data declaration at %C");
4302 m = MATCH_ERROR;
4304 gfc_free_data_all (gfc_current_ns);
4306 cleanup:
4307 gfc_free_array_spec (current_as);
4308 current_as = NULL;
4309 return m;
4313 /* Match a prefix associated with a function or subroutine
4314 declaration. If the typespec pointer is nonnull, then a typespec
4315 can be matched. Note that if nothing matches, MATCH_YES is
4316 returned (the null string was matched). */
4318 match
4319 gfc_match_prefix (gfc_typespec *ts)
4321 bool seen_type;
4322 bool seen_impure;
4323 bool found_prefix;
4325 gfc_clear_attr (&current_attr);
4326 seen_type = false;
4327 seen_impure = false;
4329 gcc_assert (!gfc_matching_prefix);
4330 gfc_matching_prefix = true;
4334 found_prefix = false;
4336 if (!seen_type && ts != NULL
4337 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4338 && gfc_match_space () == MATCH_YES)
4341 seen_type = true;
4342 found_prefix = true;
4345 if (gfc_match ("elemental% ") == MATCH_YES)
4347 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
4348 goto error;
4350 found_prefix = true;
4353 if (gfc_match ("pure% ") == MATCH_YES)
4355 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
4356 goto error;
4358 found_prefix = true;
4361 if (gfc_match ("recursive% ") == MATCH_YES)
4363 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
4364 goto error;
4366 found_prefix = true;
4369 /* IMPURE is a somewhat special case, as it needs not set an actual
4370 attribute but rather only prevents ELEMENTAL routines from being
4371 automatically PURE. */
4372 if (gfc_match ("impure% ") == MATCH_YES)
4374 if (gfc_notify_std (GFC_STD_F2008,
4375 "Fortran 2008: IMPURE procedure at %C")
4376 == FAILURE)
4377 goto error;
4379 seen_impure = true;
4380 found_prefix = true;
4383 while (found_prefix);
4385 /* IMPURE and PURE must not both appear, of course. */
4386 if (seen_impure && current_attr.pure)
4388 gfc_error ("PURE and IMPURE must not appear both at %C");
4389 goto error;
4392 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4393 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4395 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
4396 goto error;
4399 /* At this point, the next item is not a prefix. */
4400 gcc_assert (gfc_matching_prefix);
4401 gfc_matching_prefix = false;
4402 return MATCH_YES;
4404 error:
4405 gcc_assert (gfc_matching_prefix);
4406 gfc_matching_prefix = false;
4407 return MATCH_ERROR;
4411 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4413 static gfc_try
4414 copy_prefix (symbol_attribute *dest, locus *where)
4416 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
4417 return FAILURE;
4419 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
4420 return FAILURE;
4422 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
4423 return FAILURE;
4425 return SUCCESS;
4429 /* Match a formal argument list. */
4431 match
4432 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4434 gfc_formal_arglist *head, *tail, *p, *q;
4435 char name[GFC_MAX_SYMBOL_LEN + 1];
4436 gfc_symbol *sym;
4437 match m;
4439 head = tail = NULL;
4441 if (gfc_match_char ('(') != MATCH_YES)
4443 if (null_flag)
4444 goto ok;
4445 return MATCH_NO;
4448 if (gfc_match_char (')') == MATCH_YES)
4449 goto ok;
4451 for (;;)
4453 if (gfc_match_char ('*') == MATCH_YES)
4454 sym = NULL;
4455 else
4457 m = gfc_match_name (name);
4458 if (m != MATCH_YES)
4459 goto cleanup;
4461 if (gfc_get_symbol (name, NULL, &sym))
4462 goto cleanup;
4465 p = gfc_get_formal_arglist ();
4467 if (head == NULL)
4468 head = tail = p;
4469 else
4471 tail->next = p;
4472 tail = p;
4475 tail->sym = sym;
4477 /* We don't add the VARIABLE flavor because the name could be a
4478 dummy procedure. We don't apply these attributes to formal
4479 arguments of statement functions. */
4480 if (sym != NULL && !st_flag
4481 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
4482 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
4484 m = MATCH_ERROR;
4485 goto cleanup;
4488 /* The name of a program unit can be in a different namespace,
4489 so check for it explicitly. After the statement is accepted,
4490 the name is checked for especially in gfc_get_symbol(). */
4491 if (gfc_new_block != NULL && sym != NULL
4492 && strcmp (sym->name, gfc_new_block->name) == 0)
4494 gfc_error ("Name '%s' at %C is the name of the procedure",
4495 sym->name);
4496 m = MATCH_ERROR;
4497 goto cleanup;
4500 if (gfc_match_char (')') == MATCH_YES)
4501 goto ok;
4503 m = gfc_match_char (',');
4504 if (m != MATCH_YES)
4506 gfc_error ("Unexpected junk in formal argument list at %C");
4507 goto cleanup;
4512 /* Check for duplicate symbols in the formal argument list. */
4513 if (head != NULL)
4515 for (p = head; p->next; p = p->next)
4517 if (p->sym == NULL)
4518 continue;
4520 for (q = p->next; q; q = q->next)
4521 if (p->sym == q->sym)
4523 gfc_error ("Duplicate symbol '%s' in formal argument list "
4524 "at %C", p->sym->name);
4526 m = MATCH_ERROR;
4527 goto cleanup;
4532 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
4533 == FAILURE)
4535 m = MATCH_ERROR;
4536 goto cleanup;
4539 return MATCH_YES;
4541 cleanup:
4542 gfc_free_formal_arglist (head);
4543 return m;
4547 /* Match a RESULT specification following a function declaration or
4548 ENTRY statement. Also matches the end-of-statement. */
4550 static match
4551 match_result (gfc_symbol *function, gfc_symbol **result)
4553 char name[GFC_MAX_SYMBOL_LEN + 1];
4554 gfc_symbol *r;
4555 match m;
4557 if (gfc_match (" result (") != MATCH_YES)
4558 return MATCH_NO;
4560 m = gfc_match_name (name);
4561 if (m != MATCH_YES)
4562 return m;
4564 /* Get the right paren, and that's it because there could be the
4565 bind(c) attribute after the result clause. */
4566 if (gfc_match_char(')') != MATCH_YES)
4568 /* TODO: should report the missing right paren here. */
4569 return MATCH_ERROR;
4572 if (strcmp (function->name, name) == 0)
4574 gfc_error ("RESULT variable at %C must be different than function name");
4575 return MATCH_ERROR;
4578 if (gfc_get_symbol (name, NULL, &r))
4579 return MATCH_ERROR;
4581 if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4582 return MATCH_ERROR;
4584 *result = r;
4586 return MATCH_YES;
4590 /* Match a function suffix, which could be a combination of a result
4591 clause and BIND(C), either one, or neither. The draft does not
4592 require them to come in a specific order. */
4594 match
4595 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4597 match is_bind_c; /* Found bind(c). */
4598 match is_result; /* Found result clause. */
4599 match found_match; /* Status of whether we've found a good match. */
4600 char peek_char; /* Character we're going to peek at. */
4601 bool allow_binding_name;
4603 /* Initialize to having found nothing. */
4604 found_match = MATCH_NO;
4605 is_bind_c = MATCH_NO;
4606 is_result = MATCH_NO;
4608 /* Get the next char to narrow between result and bind(c). */
4609 gfc_gobble_whitespace ();
4610 peek_char = gfc_peek_ascii_char ();
4612 /* C binding names are not allowed for internal procedures. */
4613 if (gfc_current_state () == COMP_CONTAINS
4614 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4615 allow_binding_name = false;
4616 else
4617 allow_binding_name = true;
4619 switch (peek_char)
4621 case 'r':
4622 /* Look for result clause. */
4623 is_result = match_result (sym, result);
4624 if (is_result == MATCH_YES)
4626 /* Now see if there is a bind(c) after it. */
4627 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4628 /* We've found the result clause and possibly bind(c). */
4629 found_match = MATCH_YES;
4631 else
4632 /* This should only be MATCH_ERROR. */
4633 found_match = is_result;
4634 break;
4635 case 'b':
4636 /* Look for bind(c) first. */
4637 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4638 if (is_bind_c == MATCH_YES)
4640 /* Now see if a result clause followed it. */
4641 is_result = match_result (sym, result);
4642 found_match = MATCH_YES;
4644 else
4646 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4647 found_match = MATCH_ERROR;
4649 break;
4650 default:
4651 gfc_error ("Unexpected junk after function declaration at %C");
4652 found_match = MATCH_ERROR;
4653 break;
4656 if (is_bind_c == MATCH_YES)
4658 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4659 if (gfc_current_state () == COMP_CONTAINS
4660 && sym->ns->proc_name->attr.flavor != FL_MODULE
4661 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4662 "at %L may not be specified for an internal "
4663 "procedure", &gfc_current_locus)
4664 == FAILURE)
4665 return MATCH_ERROR;
4667 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4668 == FAILURE)
4669 return MATCH_ERROR;
4672 return found_match;
4676 /* Procedure pointer return value without RESULT statement:
4677 Add "hidden" result variable named "ppr@". */
4679 static gfc_try
4680 add_hidden_procptr_result (gfc_symbol *sym)
4682 bool case1,case2;
4684 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4685 return FAILURE;
4687 /* First usage case: PROCEDURE and EXTERNAL statements. */
4688 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4689 && strcmp (gfc_current_block ()->name, sym->name) == 0
4690 && sym->attr.external;
4691 /* Second usage case: INTERFACE statements. */
4692 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4693 && gfc_state_stack->previous->state == COMP_FUNCTION
4694 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4696 if (case1 || case2)
4698 gfc_symtree *stree;
4699 if (case1)
4700 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4701 else if (case2)
4703 gfc_symtree *st2;
4704 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4705 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4706 st2->n.sym = stree->n.sym;
4708 sym->result = stree->n.sym;
4710 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4711 sym->result->attr.pointer = sym->attr.pointer;
4712 sym->result->attr.external = sym->attr.external;
4713 sym->result->attr.referenced = sym->attr.referenced;
4714 sym->result->ts = sym->ts;
4715 sym->attr.proc_pointer = 0;
4716 sym->attr.pointer = 0;
4717 sym->attr.external = 0;
4718 if (sym->result->attr.external && sym->result->attr.pointer)
4720 sym->result->attr.pointer = 0;
4721 sym->result->attr.proc_pointer = 1;
4724 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4726 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4727 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4728 && sym->result && sym->result != sym && sym->result->attr.external
4729 && sym == gfc_current_ns->proc_name
4730 && sym == sym->result->ns->proc_name
4731 && strcmp ("ppr@", sym->result->name) == 0)
4733 sym->result->attr.proc_pointer = 1;
4734 sym->attr.pointer = 0;
4735 return SUCCESS;
4737 else
4738 return FAILURE;
4742 /* Match the interface for a PROCEDURE declaration,
4743 including brackets (R1212). */
4745 static match
4746 match_procedure_interface (gfc_symbol **proc_if)
4748 match m;
4749 gfc_symtree *st;
4750 locus old_loc, entry_loc;
4751 gfc_namespace *old_ns = gfc_current_ns;
4752 char name[GFC_MAX_SYMBOL_LEN + 1];
4754 old_loc = entry_loc = gfc_current_locus;
4755 gfc_clear_ts (&current_ts);
4757 if (gfc_match (" (") != MATCH_YES)
4759 gfc_current_locus = entry_loc;
4760 return MATCH_NO;
4763 /* Get the type spec. for the procedure interface. */
4764 old_loc = gfc_current_locus;
4765 m = gfc_match_decl_type_spec (&current_ts, 0);
4766 gfc_gobble_whitespace ();
4767 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4768 goto got_ts;
4770 if (m == MATCH_ERROR)
4771 return m;
4773 /* Procedure interface is itself a procedure. */
4774 gfc_current_locus = old_loc;
4775 m = gfc_match_name (name);
4777 /* First look to see if it is already accessible in the current
4778 namespace because it is use associated or contained. */
4779 st = NULL;
4780 if (gfc_find_sym_tree (name, NULL, 0, &st))
4781 return MATCH_ERROR;
4783 /* If it is still not found, then try the parent namespace, if it
4784 exists and create the symbol there if it is still not found. */
4785 if (gfc_current_ns->parent)
4786 gfc_current_ns = gfc_current_ns->parent;
4787 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4788 return MATCH_ERROR;
4790 gfc_current_ns = old_ns;
4791 *proc_if = st->n.sym;
4793 /* Various interface checks. */
4794 if (*proc_if)
4796 (*proc_if)->refs++;
4797 /* Resolve interface if possible. That way, attr.procedure is only set
4798 if it is declared by a later procedure-declaration-stmt, which is
4799 invalid per C1212. */
4800 while ((*proc_if)->ts.interface)
4801 *proc_if = (*proc_if)->ts.interface;
4803 if ((*proc_if)->generic)
4805 gfc_error ("Interface '%s' at %C may not be generic",
4806 (*proc_if)->name);
4807 return MATCH_ERROR;
4809 if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
4811 gfc_error ("Interface '%s' at %C may not be a statement function",
4812 (*proc_if)->name);
4813 return MATCH_ERROR;
4815 /* Handle intrinsic procedures. */
4816 if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4817 || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4818 && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4819 || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4820 (*proc_if)->attr.intrinsic = 1;
4821 if ((*proc_if)->attr.intrinsic
4822 && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
4824 gfc_error ("Intrinsic procedure '%s' not allowed "
4825 "in PROCEDURE statement at %C", (*proc_if)->name);
4826 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) == FAILURE)
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 != SUCCESS)
4906 return MATCH_ERROR;
4909 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4910 return MATCH_ERROR;
4912 if (add_hidden_procptr_result (sym) == SUCCESS)
4913 sym = sym->result;
4915 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4916 return MATCH_ERROR;
4918 /* Set interface. */
4919 if (proc_if != NULL)
4921 if (sym->ts.type != BT_UNKNOWN)
4923 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4924 sym->name, &gfc_current_locus,
4925 gfc_basic_typename (sym->ts.type));
4926 return MATCH_ERROR;
4928 sym->ts.interface = proc_if;
4929 sym->attr.untyped = 1;
4930 sym->attr.if_source = IFSRC_IFBODY;
4932 else if (current_ts.type != BT_UNKNOWN)
4934 if (gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
4935 return MATCH_ERROR;
4936 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4937 sym->ts.interface->ts = current_ts;
4938 sym->ts.interface->attr.flavor = FL_PROCEDURE;
4939 sym->ts.interface->attr.function = 1;
4940 sym->attr.function = 1;
4941 sym->attr.if_source = IFSRC_UNKNOWN;
4944 if (gfc_match (" =>") == MATCH_YES)
4946 if (!current_attr.pointer)
4948 gfc_error ("Initialization at %C isn't for a pointer variable");
4949 m = MATCH_ERROR;
4950 goto cleanup;
4953 m = match_pointer_init (&initializer, 1);
4954 if (m != MATCH_YES)
4955 goto cleanup;
4957 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4958 != SUCCESS)
4959 goto cleanup;
4963 gfc_set_sym_referenced (sym);
4965 if (gfc_match_eos () == MATCH_YES)
4966 return MATCH_YES;
4967 if (gfc_match_char (',') != MATCH_YES)
4968 goto syntax;
4971 syntax:
4972 gfc_error ("Syntax error in PROCEDURE statement at %C");
4973 return MATCH_ERROR;
4975 cleanup:
4976 /* Free stuff up and return. */
4977 gfc_free_expr (initializer);
4978 return m;
4982 static match
4983 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4986 /* Match a procedure pointer component declaration (R445). */
4988 static match
4989 match_ppc_decl (void)
4991 match m;
4992 gfc_symbol *proc_if = NULL;
4993 gfc_typespec ts;
4994 int num;
4995 gfc_component *c;
4996 gfc_expr *initializer = NULL;
4997 gfc_typebound_proc* tb;
4998 char name[GFC_MAX_SYMBOL_LEN + 1];
5000 /* Parse interface (with brackets). */
5001 m = match_procedure_interface (&proc_if);
5002 if (m != MATCH_YES)
5003 goto syntax;
5005 /* Parse attributes. */
5006 tb = XCNEW (gfc_typebound_proc);
5007 tb->where = gfc_current_locus;
5008 m = match_binding_attributes (tb, false, true);
5009 if (m == MATCH_ERROR)
5010 return m;
5012 gfc_clear_attr (&current_attr);
5013 current_attr.procedure = 1;
5014 current_attr.proc_pointer = 1;
5015 current_attr.access = tb->access;
5016 current_attr.flavor = FL_PROCEDURE;
5018 /* Match the colons (required). */
5019 if (gfc_match (" ::") != MATCH_YES)
5021 gfc_error ("Expected '::' after binding-attributes at %C");
5022 return MATCH_ERROR;
5025 /* Check for C450. */
5026 if (!tb->nopass && proc_if == NULL)
5028 gfc_error("NOPASS or explicit interface required at %C");
5029 return MATCH_ERROR;
5032 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure pointer "
5033 "component at %C") == FAILURE)
5034 return MATCH_ERROR;
5036 /* Match PPC names. */
5037 ts = current_ts;
5038 for(num=1;;num++)
5040 m = gfc_match_name (name);
5041 if (m == MATCH_NO)
5042 goto syntax;
5043 else if (m == MATCH_ERROR)
5044 return m;
5046 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
5047 return MATCH_ERROR;
5049 /* Add current_attr to the symbol attributes. */
5050 if (gfc_copy_attr (&c->attr, &current_attr, NULL) == FAILURE)
5051 return MATCH_ERROR;
5053 if (gfc_add_external (&c->attr, NULL) == FAILURE)
5054 return MATCH_ERROR;
5056 if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
5057 return MATCH_ERROR;
5059 c->tb = tb;
5061 /* Set interface. */
5062 if (proc_if != NULL)
5064 c->ts.interface = proc_if;
5065 c->attr.untyped = 1;
5066 c->attr.if_source = IFSRC_IFBODY;
5068 else if (ts.type != BT_UNKNOWN)
5070 c->ts = ts;
5071 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
5072 c->ts.interface->ts = ts;
5073 c->ts.interface->attr.flavor = FL_PROCEDURE;
5074 c->ts.interface->attr.function = 1;
5075 c->attr.function = 1;
5076 c->attr.if_source = IFSRC_UNKNOWN;
5079 if (gfc_match (" =>") == MATCH_YES)
5081 m = match_pointer_init (&initializer, 1);
5082 if (m != MATCH_YES)
5084 gfc_free_expr (initializer);
5085 return m;
5087 c->initializer = initializer;
5090 if (gfc_match_eos () == MATCH_YES)
5091 return MATCH_YES;
5092 if (gfc_match_char (',') != MATCH_YES)
5093 goto syntax;
5096 syntax:
5097 gfc_error ("Syntax error in procedure pointer component at %C");
5098 return MATCH_ERROR;
5102 /* Match a PROCEDURE declaration inside an interface (R1206). */
5104 static match
5105 match_procedure_in_interface (void)
5107 match m;
5108 gfc_symbol *sym;
5109 char name[GFC_MAX_SYMBOL_LEN + 1];
5111 if (current_interface.type == INTERFACE_NAMELESS
5112 || current_interface.type == INTERFACE_ABSTRACT)
5114 gfc_error ("PROCEDURE at %C must be in a generic interface");
5115 return MATCH_ERROR;
5118 for(;;)
5120 m = gfc_match_name (name);
5121 if (m == MATCH_NO)
5122 goto syntax;
5123 else if (m == MATCH_ERROR)
5124 return m;
5125 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
5126 return MATCH_ERROR;
5128 if (gfc_add_interface (sym) == FAILURE)
5129 return MATCH_ERROR;
5131 if (gfc_match_eos () == MATCH_YES)
5132 break;
5133 if (gfc_match_char (',') != MATCH_YES)
5134 goto syntax;
5137 return MATCH_YES;
5139 syntax:
5140 gfc_error ("Syntax error in PROCEDURE statement at %C");
5141 return MATCH_ERROR;
5145 /* General matcher for PROCEDURE declarations. */
5147 static match match_procedure_in_type (void);
5149 match
5150 gfc_match_procedure (void)
5152 match m;
5154 switch (gfc_current_state ())
5156 case COMP_NONE:
5157 case COMP_PROGRAM:
5158 case COMP_MODULE:
5159 case COMP_SUBROUTINE:
5160 case COMP_FUNCTION:
5161 case COMP_BLOCK:
5162 m = match_procedure_decl ();
5163 break;
5164 case COMP_INTERFACE:
5165 m = match_procedure_in_interface ();
5166 break;
5167 case COMP_DERIVED:
5168 m = match_ppc_decl ();
5169 break;
5170 case COMP_DERIVED_CONTAINS:
5171 m = match_procedure_in_type ();
5172 break;
5173 default:
5174 return MATCH_NO;
5177 if (m != MATCH_YES)
5178 return m;
5180 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
5181 == FAILURE)
5182 return MATCH_ERROR;
5184 return m;
5188 /* Warn if a matched procedure has the same name as an intrinsic; this is
5189 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5190 parser-state-stack to find out whether we're in a module. */
5192 static void
5193 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
5195 bool in_module;
5197 in_module = (gfc_state_stack->previous
5198 && gfc_state_stack->previous->state == COMP_MODULE);
5200 gfc_warn_intrinsic_shadow (sym, in_module, func);
5204 /* Match a function declaration. */
5206 match
5207 gfc_match_function_decl (void)
5209 char name[GFC_MAX_SYMBOL_LEN + 1];
5210 gfc_symbol *sym, *result;
5211 locus old_loc;
5212 match m;
5213 match suffix_match;
5214 match found_match; /* Status returned by match func. */
5216 if (gfc_current_state () != COMP_NONE
5217 && gfc_current_state () != COMP_INTERFACE
5218 && gfc_current_state () != COMP_CONTAINS)
5219 return MATCH_NO;
5221 gfc_clear_ts (&current_ts);
5223 old_loc = gfc_current_locus;
5225 m = gfc_match_prefix (&current_ts);
5226 if (m != MATCH_YES)
5228 gfc_current_locus = old_loc;
5229 return m;
5232 if (gfc_match ("function% %n", name) != MATCH_YES)
5234 gfc_current_locus = old_loc;
5235 return MATCH_NO;
5237 if (get_proc_name (name, &sym, false))
5238 return MATCH_ERROR;
5240 if (add_hidden_procptr_result (sym) == SUCCESS)
5241 sym = sym->result;
5243 gfc_new_block = sym;
5245 m = gfc_match_formal_arglist (sym, 0, 0);
5246 if (m == MATCH_NO)
5248 gfc_error ("Expected formal argument list in function "
5249 "definition at %C");
5250 m = MATCH_ERROR;
5251 goto cleanup;
5253 else if (m == MATCH_ERROR)
5254 goto cleanup;
5256 result = NULL;
5258 /* According to the draft, the bind(c) and result clause can
5259 come in either order after the formal_arg_list (i.e., either
5260 can be first, both can exist together or by themselves or neither
5261 one). Therefore, the match_result can't match the end of the
5262 string, and check for the bind(c) or result clause in either order. */
5263 found_match = gfc_match_eos ();
5265 /* Make sure that it isn't already declared as BIND(C). If it is, it
5266 must have been marked BIND(C) with a BIND(C) attribute and that is
5267 not allowed for procedures. */
5268 if (sym->attr.is_bind_c == 1)
5270 sym->attr.is_bind_c = 0;
5271 if (sym->old_symbol != NULL)
5272 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5273 "variables or common blocks",
5274 &(sym->old_symbol->declared_at));
5275 else
5276 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5277 "variables or common blocks", &gfc_current_locus);
5280 if (found_match != MATCH_YES)
5282 /* If we haven't found the end-of-statement, look for a suffix. */
5283 suffix_match = gfc_match_suffix (sym, &result);
5284 if (suffix_match == MATCH_YES)
5285 /* Need to get the eos now. */
5286 found_match = gfc_match_eos ();
5287 else
5288 found_match = suffix_match;
5291 if(found_match != MATCH_YES)
5292 m = MATCH_ERROR;
5293 else
5295 /* Make changes to the symbol. */
5296 m = MATCH_ERROR;
5298 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
5299 goto cleanup;
5301 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
5302 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5303 goto cleanup;
5305 /* Delay matching the function characteristics until after the
5306 specification block by signalling kind=-1. */
5307 sym->declared_at = old_loc;
5308 if (current_ts.type != BT_UNKNOWN)
5309 current_ts.kind = -1;
5310 else
5311 current_ts.kind = 0;
5313 if (result == NULL)
5315 if (current_ts.type != BT_UNKNOWN
5316 && gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
5317 goto cleanup;
5318 sym->result = sym;
5320 else
5322 if (current_ts.type != BT_UNKNOWN
5323 && gfc_add_type (result, &current_ts, &gfc_current_locus)
5324 == FAILURE)
5325 goto cleanup;
5326 sym->result = result;
5329 /* Warn if this procedure has the same name as an intrinsic. */
5330 warn_intrinsic_shadow (sym, true);
5332 return MATCH_YES;
5335 cleanup:
5336 gfc_current_locus = old_loc;
5337 return m;
5341 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5342 pass the name of the entry, rather than the gfc_current_block name, and
5343 to return false upon finding an existing global entry. */
5345 static bool
5346 add_global_entry (const char *name, int sub)
5348 gfc_gsymbol *s;
5349 enum gfc_symbol_type type;
5351 s = gfc_get_gsymbol(name);
5352 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5354 if (s->defined
5355 || (s->type != GSYM_UNKNOWN
5356 && s->type != type))
5357 gfc_global_used(s, NULL);
5358 else
5360 s->type = type;
5361 s->where = gfc_current_locus;
5362 s->defined = 1;
5363 s->ns = gfc_current_ns;
5364 return true;
5366 return false;
5370 /* Match an ENTRY statement. */
5372 match
5373 gfc_match_entry (void)
5375 gfc_symbol *proc;
5376 gfc_symbol *result;
5377 gfc_symbol *entry;
5378 char name[GFC_MAX_SYMBOL_LEN + 1];
5379 gfc_compile_state state;
5380 match m;
5381 gfc_entry_list *el;
5382 locus old_loc;
5383 bool module_procedure;
5384 char peek_char;
5385 match is_bind_c;
5387 m = gfc_match_name (name);
5388 if (m != MATCH_YES)
5389 return m;
5391 if (gfc_notify_std (GFC_STD_F2008_OBS, "Fortran 2008 obsolescent feature: "
5392 "ENTRY statement at %C") == FAILURE)
5393 return MATCH_ERROR;
5395 state = gfc_current_state ();
5396 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5398 switch (state)
5400 case COMP_PROGRAM:
5401 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5402 break;
5403 case COMP_MODULE:
5404 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5405 break;
5406 case COMP_BLOCK_DATA:
5407 gfc_error ("ENTRY statement at %C cannot appear within "
5408 "a BLOCK DATA");
5409 break;
5410 case COMP_INTERFACE:
5411 gfc_error ("ENTRY statement at %C cannot appear within "
5412 "an INTERFACE");
5413 break;
5414 case COMP_DERIVED:
5415 gfc_error ("ENTRY statement at %C cannot appear within "
5416 "a DERIVED TYPE block");
5417 break;
5418 case COMP_IF:
5419 gfc_error ("ENTRY statement at %C cannot appear within "
5420 "an IF-THEN block");
5421 break;
5422 case COMP_DO:
5423 case COMP_DO_CONCURRENT:
5424 gfc_error ("ENTRY statement at %C cannot appear within "
5425 "a DO block");
5426 break;
5427 case COMP_SELECT:
5428 gfc_error ("ENTRY statement at %C cannot appear within "
5429 "a SELECT block");
5430 break;
5431 case COMP_FORALL:
5432 gfc_error ("ENTRY statement at %C cannot appear within "
5433 "a FORALL block");
5434 break;
5435 case COMP_WHERE:
5436 gfc_error ("ENTRY statement at %C cannot appear within "
5437 "a WHERE block");
5438 break;
5439 case COMP_CONTAINS:
5440 gfc_error ("ENTRY statement at %C cannot appear within "
5441 "a contained subprogram");
5442 break;
5443 default:
5444 gfc_internal_error ("gfc_match_entry(): Bad state");
5446 return MATCH_ERROR;
5449 module_procedure = gfc_current_ns->parent != NULL
5450 && gfc_current_ns->parent->proc_name
5451 && gfc_current_ns->parent->proc_name->attr.flavor
5452 == FL_MODULE;
5454 if (gfc_current_ns->parent != NULL
5455 && gfc_current_ns->parent->proc_name
5456 && !module_procedure)
5458 gfc_error("ENTRY statement at %C cannot appear in a "
5459 "contained procedure");
5460 return MATCH_ERROR;
5463 /* Module function entries need special care in get_proc_name
5464 because previous references within the function will have
5465 created symbols attached to the current namespace. */
5466 if (get_proc_name (name, &entry,
5467 gfc_current_ns->parent != NULL
5468 && module_procedure))
5469 return MATCH_ERROR;
5471 proc = gfc_current_block ();
5473 /* Make sure that it isn't already declared as BIND(C). If it is, it
5474 must have been marked BIND(C) with a BIND(C) attribute and that is
5475 not allowed for procedures. */
5476 if (entry->attr.is_bind_c == 1)
5478 entry->attr.is_bind_c = 0;
5479 if (entry->old_symbol != NULL)
5480 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5481 "variables or common blocks",
5482 &(entry->old_symbol->declared_at));
5483 else
5484 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5485 "variables or common blocks", &gfc_current_locus);
5488 /* Check what next non-whitespace character is so we can tell if there
5489 is the required parens if we have a BIND(C). */
5490 gfc_gobble_whitespace ();
5491 peek_char = gfc_peek_ascii_char ();
5493 if (state == COMP_SUBROUTINE)
5495 /* An entry in a subroutine. */
5496 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
5497 return MATCH_ERROR;
5499 m = gfc_match_formal_arglist (entry, 0, 1);
5500 if (m != MATCH_YES)
5501 return MATCH_ERROR;
5503 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5504 never be an internal procedure. */
5505 is_bind_c = gfc_match_bind_c (entry, true);
5506 if (is_bind_c == MATCH_ERROR)
5507 return MATCH_ERROR;
5508 if (is_bind_c == MATCH_YES)
5510 if (peek_char != '(')
5512 gfc_error ("Missing required parentheses before BIND(C) at %C");
5513 return MATCH_ERROR;
5515 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
5516 == FAILURE)
5517 return MATCH_ERROR;
5520 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5521 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
5522 return MATCH_ERROR;
5524 else
5526 /* An entry in a function.
5527 We need to take special care because writing
5528 ENTRY f()
5530 ENTRY f
5531 is allowed, whereas
5532 ENTRY f() RESULT (r)
5533 can't be written as
5534 ENTRY f RESULT (r). */
5535 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
5536 return MATCH_ERROR;
5538 old_loc = gfc_current_locus;
5539 if (gfc_match_eos () == MATCH_YES)
5541 gfc_current_locus = old_loc;
5542 /* Match the empty argument list, and add the interface to
5543 the symbol. */
5544 m = gfc_match_formal_arglist (entry, 0, 1);
5546 else
5547 m = gfc_match_formal_arglist (entry, 0, 0);
5549 if (m != MATCH_YES)
5550 return MATCH_ERROR;
5552 result = NULL;
5554 if (gfc_match_eos () == MATCH_YES)
5556 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5557 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5558 return MATCH_ERROR;
5560 entry->result = entry;
5562 else
5564 m = gfc_match_suffix (entry, &result);
5565 if (m == MATCH_NO)
5566 gfc_syntax_error (ST_ENTRY);
5567 if (m != MATCH_YES)
5568 return MATCH_ERROR;
5570 if (result)
5572 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
5573 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
5574 || gfc_add_function (&entry->attr, result->name, NULL)
5575 == FAILURE)
5576 return MATCH_ERROR;
5577 entry->result = result;
5579 else
5581 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5582 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5583 return MATCH_ERROR;
5584 entry->result = entry;
5589 if (gfc_match_eos () != MATCH_YES)
5591 gfc_syntax_error (ST_ENTRY);
5592 return MATCH_ERROR;
5595 entry->attr.recursive = proc->attr.recursive;
5596 entry->attr.elemental = proc->attr.elemental;
5597 entry->attr.pure = proc->attr.pure;
5599 el = gfc_get_entry_list ();
5600 el->sym = entry;
5601 el->next = gfc_current_ns->entries;
5602 gfc_current_ns->entries = el;
5603 if (el->next)
5604 el->id = el->next->id + 1;
5605 else
5606 el->id = 1;
5608 new_st.op = EXEC_ENTRY;
5609 new_st.ext.entry = el;
5611 return MATCH_YES;
5615 /* Match a subroutine statement, including optional prefixes. */
5617 match
5618 gfc_match_subroutine (void)
5620 char name[GFC_MAX_SYMBOL_LEN + 1];
5621 gfc_symbol *sym;
5622 match m;
5623 match is_bind_c;
5624 char peek_char;
5625 bool allow_binding_name;
5627 if (gfc_current_state () != COMP_NONE
5628 && gfc_current_state () != COMP_INTERFACE
5629 && gfc_current_state () != COMP_CONTAINS)
5630 return MATCH_NO;
5632 m = gfc_match_prefix (NULL);
5633 if (m != MATCH_YES)
5634 return m;
5636 m = gfc_match ("subroutine% %n", name);
5637 if (m != MATCH_YES)
5638 return m;
5640 if (get_proc_name (name, &sym, false))
5641 return MATCH_ERROR;
5643 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5644 the symbol existed before. */
5645 sym->declared_at = gfc_current_locus;
5647 if (add_hidden_procptr_result (sym) == SUCCESS)
5648 sym = sym->result;
5650 gfc_new_block = sym;
5652 /* Check what next non-whitespace character is so we can tell if there
5653 is the required parens if we have a BIND(C). */
5654 gfc_gobble_whitespace ();
5655 peek_char = gfc_peek_ascii_char ();
5657 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5658 return MATCH_ERROR;
5660 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5661 return MATCH_ERROR;
5663 /* Make sure that it isn't already declared as BIND(C). If it is, it
5664 must have been marked BIND(C) with a BIND(C) attribute and that is
5665 not allowed for procedures. */
5666 if (sym->attr.is_bind_c == 1)
5668 sym->attr.is_bind_c = 0;
5669 if (sym->old_symbol != NULL)
5670 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5671 "variables or common blocks",
5672 &(sym->old_symbol->declared_at));
5673 else
5674 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5675 "variables or common blocks", &gfc_current_locus);
5678 /* C binding names are not allowed for internal procedures. */
5679 if (gfc_current_state () == COMP_CONTAINS
5680 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5681 allow_binding_name = false;
5682 else
5683 allow_binding_name = true;
5685 /* Here, we are just checking if it has the bind(c) attribute, and if
5686 so, then we need to make sure it's all correct. If it doesn't,
5687 we still need to continue matching the rest of the subroutine line. */
5688 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5689 if (is_bind_c == MATCH_ERROR)
5691 /* There was an attempt at the bind(c), but it was wrong. An
5692 error message should have been printed w/in the gfc_match_bind_c
5693 so here we'll just return the MATCH_ERROR. */
5694 return MATCH_ERROR;
5697 if (is_bind_c == MATCH_YES)
5699 /* The following is allowed in the Fortran 2008 draft. */
5700 if (gfc_current_state () == COMP_CONTAINS
5701 && sym->ns->proc_name->attr.flavor != FL_MODULE
5702 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5703 "at %L may not be specified for an internal "
5704 "procedure", &gfc_current_locus)
5705 == FAILURE)
5706 return MATCH_ERROR;
5708 if (peek_char != '(')
5710 gfc_error ("Missing required parentheses before BIND(C) at %C");
5711 return MATCH_ERROR;
5713 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5714 == FAILURE)
5715 return MATCH_ERROR;
5718 if (gfc_match_eos () != MATCH_YES)
5720 gfc_syntax_error (ST_SUBROUTINE);
5721 return MATCH_ERROR;
5724 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5725 return MATCH_ERROR;
5727 /* Warn if it has the same name as an intrinsic. */
5728 warn_intrinsic_shadow (sym, false);
5730 return MATCH_YES;
5734 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5735 given, and set the binding label in either the given symbol (if not
5736 NULL), or in the current_ts. The symbol may be NULL because we may
5737 encounter the BIND(C) before the declaration itself. Return
5738 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5739 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5740 or MATCH_YES if the specifier was correct and the binding label and
5741 bind(c) fields were set correctly for the given symbol or the
5742 current_ts. If allow_binding_name is false, no binding name may be
5743 given. */
5745 match
5746 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5748 /* binding label, if exists */
5749 const char* binding_label = NULL;
5750 match double_quote;
5751 match single_quote;
5753 /* Initialize the flag that specifies whether we encountered a NAME=
5754 specifier or not. */
5755 has_name_equals = 0;
5757 /* This much we have to be able to match, in this order, if
5758 there is a bind(c) label. */
5759 if (gfc_match (" bind ( c ") != MATCH_YES)
5760 return MATCH_NO;
5762 /* Now see if there is a binding label, or if we've reached the
5763 end of the bind(c) attribute without one. */
5764 if (gfc_match_char (',') == MATCH_YES)
5766 if (gfc_match (" name = ") != MATCH_YES)
5768 gfc_error ("Syntax error in NAME= specifier for binding label "
5769 "at %C");
5770 /* should give an error message here */
5771 return MATCH_ERROR;
5774 has_name_equals = 1;
5776 /* Get the opening quote. */
5777 double_quote = MATCH_YES;
5778 single_quote = MATCH_YES;
5779 double_quote = gfc_match_char ('"');
5780 if (double_quote != MATCH_YES)
5781 single_quote = gfc_match_char ('\'');
5782 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5784 gfc_error ("Syntax error in NAME= specifier for binding label "
5785 "at %C");
5786 return MATCH_ERROR;
5789 /* Grab the binding label, using functions that will not lower
5790 case the names automatically. */
5791 if (gfc_match_name_C (&binding_label) != MATCH_YES)
5792 return MATCH_ERROR;
5794 /* Get the closing quotation. */
5795 if (double_quote == MATCH_YES)
5797 if (gfc_match_char ('"') != MATCH_YES)
5799 gfc_error ("Missing closing quote '\"' for binding label at %C");
5800 /* User started string with '"' so looked to match it. */
5801 return MATCH_ERROR;
5804 else
5806 if (gfc_match_char ('\'') != MATCH_YES)
5808 gfc_error ("Missing closing quote '\'' for binding label at %C");
5809 /* User started string with "'" char. */
5810 return MATCH_ERROR;
5815 /* Get the required right paren. */
5816 if (gfc_match_char (')') != MATCH_YES)
5818 gfc_error ("Missing closing paren for binding label at %C");
5819 return MATCH_ERROR;
5822 if (has_name_equals && !allow_binding_name)
5824 gfc_error ("No binding name is allowed in BIND(C) at %C");
5825 return MATCH_ERROR;
5828 if (has_name_equals && sym != NULL && sym->attr.dummy)
5830 gfc_error ("For dummy procedure %s, no binding name is "
5831 "allowed in BIND(C) at %C", sym->name);
5832 return MATCH_ERROR;
5836 /* Save the binding label to the symbol. If sym is null, we're
5837 probably matching the typespec attributes of a declaration and
5838 haven't gotten the name yet, and therefore, no symbol yet. */
5839 if (binding_label)
5841 if (sym != NULL)
5842 sym->binding_label = binding_label;
5843 else
5844 curr_binding_label = binding_label;
5846 else if (allow_binding_name)
5848 /* No binding label, but if symbol isn't null, we
5849 can set the label for it here.
5850 If name="" or allow_binding_name is false, no C binding name is
5851 created. */
5852 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5853 sym->binding_label = IDENTIFIER_POINTER (get_identifier (sym->name));
5856 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5857 && current_interface.type == INTERFACE_ABSTRACT)
5859 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5860 return MATCH_ERROR;
5863 return MATCH_YES;
5867 /* Return nonzero if we're currently compiling a contained procedure. */
5869 static int
5870 contained_procedure (void)
5872 gfc_state_data *s = gfc_state_stack;
5874 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5875 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5876 return 1;
5878 return 0;
5881 /* Set the kind of each enumerator. The kind is selected such that it is
5882 interoperable with the corresponding C enumeration type, making
5883 sure that -fshort-enums is honored. */
5885 static void
5886 set_enum_kind(void)
5888 enumerator_history *current_history = NULL;
5889 int kind;
5890 int i;
5892 if (max_enum == NULL || enum_history == NULL)
5893 return;
5895 if (!flag_short_enums)
5896 return;
5898 i = 0;
5901 kind = gfc_integer_kinds[i++].kind;
5903 while (kind < gfc_c_int_kind
5904 && gfc_check_integer_range (max_enum->initializer->value.integer,
5905 kind) != ARITH_OK);
5907 current_history = enum_history;
5908 while (current_history != NULL)
5910 current_history->sym->ts.kind = kind;
5911 current_history = current_history->next;
5916 /* Match any of the various end-block statements. Returns the type of
5917 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5918 and END BLOCK statements cannot be replaced by a single END statement. */
5920 match
5921 gfc_match_end (gfc_statement *st)
5923 char name[GFC_MAX_SYMBOL_LEN + 1];
5924 gfc_compile_state state;
5925 locus old_loc;
5926 const char *block_name;
5927 const char *target;
5928 int eos_ok;
5929 match m;
5931 old_loc = gfc_current_locus;
5932 if (gfc_match ("end") != MATCH_YES)
5933 return MATCH_NO;
5935 state = gfc_current_state ();
5936 block_name = gfc_current_block () == NULL
5937 ? NULL : gfc_current_block ()->name;
5939 switch (state)
5941 case COMP_ASSOCIATE:
5942 case COMP_BLOCK:
5943 if (!strncmp (block_name, "block@", strlen("block@")))
5944 block_name = NULL;
5945 break;
5947 case COMP_CONTAINS:
5948 case COMP_DERIVED_CONTAINS:
5949 state = gfc_state_stack->previous->state;
5950 block_name = gfc_state_stack->previous->sym == NULL
5951 ? NULL : gfc_state_stack->previous->sym->name;
5952 break;
5954 default:
5955 break;
5958 switch (state)
5960 case COMP_NONE:
5961 case COMP_PROGRAM:
5962 *st = ST_END_PROGRAM;
5963 target = " program";
5964 eos_ok = 1;
5965 break;
5967 case COMP_SUBROUTINE:
5968 *st = ST_END_SUBROUTINE;
5969 target = " subroutine";
5970 eos_ok = !contained_procedure ();
5971 break;
5973 case COMP_FUNCTION:
5974 *st = ST_END_FUNCTION;
5975 target = " function";
5976 eos_ok = !contained_procedure ();
5977 break;
5979 case COMP_BLOCK_DATA:
5980 *st = ST_END_BLOCK_DATA;
5981 target = " block data";
5982 eos_ok = 1;
5983 break;
5985 case COMP_MODULE:
5986 *st = ST_END_MODULE;
5987 target = " module";
5988 eos_ok = 1;
5989 break;
5991 case COMP_INTERFACE:
5992 *st = ST_END_INTERFACE;
5993 target = " interface";
5994 eos_ok = 0;
5995 break;
5997 case COMP_DERIVED:
5998 case COMP_DERIVED_CONTAINS:
5999 *st = ST_END_TYPE;
6000 target = " type";
6001 eos_ok = 0;
6002 break;
6004 case COMP_ASSOCIATE:
6005 *st = ST_END_ASSOCIATE;
6006 target = " associate";
6007 eos_ok = 0;
6008 break;
6010 case COMP_BLOCK:
6011 *st = ST_END_BLOCK;
6012 target = " block";
6013 eos_ok = 0;
6014 break;
6016 case COMP_IF:
6017 *st = ST_ENDIF;
6018 target = " if";
6019 eos_ok = 0;
6020 break;
6022 case COMP_DO:
6023 case COMP_DO_CONCURRENT:
6024 *st = ST_ENDDO;
6025 target = " do";
6026 eos_ok = 0;
6027 break;
6029 case COMP_CRITICAL:
6030 *st = ST_END_CRITICAL;
6031 target = " critical";
6032 eos_ok = 0;
6033 break;
6035 case COMP_SELECT:
6036 case COMP_SELECT_TYPE:
6037 *st = ST_END_SELECT;
6038 target = " select";
6039 eos_ok = 0;
6040 break;
6042 case COMP_FORALL:
6043 *st = ST_END_FORALL;
6044 target = " forall";
6045 eos_ok = 0;
6046 break;
6048 case COMP_WHERE:
6049 *st = ST_END_WHERE;
6050 target = " where";
6051 eos_ok = 0;
6052 break;
6054 case COMP_ENUM:
6055 *st = ST_END_ENUM;
6056 target = " enum";
6057 eos_ok = 0;
6058 last_initializer = NULL;
6059 set_enum_kind ();
6060 gfc_free_enum_history ();
6061 break;
6063 default:
6064 gfc_error ("Unexpected END statement at %C");
6065 goto cleanup;
6068 if (gfc_match_eos () == MATCH_YES)
6070 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
6072 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: END statement "
6073 "instead of %s statement at %L",
6074 gfc_ascii_statement (*st), &old_loc) == FAILURE)
6075 goto cleanup;
6077 else if (!eos_ok)
6079 /* We would have required END [something]. */
6080 gfc_error ("%s statement expected at %L",
6081 gfc_ascii_statement (*st), &old_loc);
6082 goto cleanup;
6085 return MATCH_YES;
6088 /* Verify that we've got the sort of end-block that we're expecting. */
6089 if (gfc_match (target) != MATCH_YES)
6091 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
6092 goto cleanup;
6095 /* If we're at the end, make sure a block name wasn't required. */
6096 if (gfc_match_eos () == MATCH_YES)
6099 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
6100 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
6101 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
6102 return MATCH_YES;
6104 if (!block_name)
6105 return MATCH_YES;
6107 gfc_error ("Expected block name of '%s' in %s statement at %C",
6108 block_name, gfc_ascii_statement (*st));
6110 return MATCH_ERROR;
6113 /* END INTERFACE has a special handler for its several possible endings. */
6114 if (*st == ST_END_INTERFACE)
6115 return gfc_match_end_interface ();
6117 /* We haven't hit the end of statement, so what is left must be an
6118 end-name. */
6119 m = gfc_match_space ();
6120 if (m == MATCH_YES)
6121 m = gfc_match_name (name);
6123 if (m == MATCH_NO)
6124 gfc_error ("Expected terminating name at %C");
6125 if (m != MATCH_YES)
6126 goto cleanup;
6128 if (block_name == NULL)
6129 goto syntax;
6131 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
6133 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
6134 gfc_ascii_statement (*st));
6135 goto cleanup;
6137 /* Procedure pointer as function result. */
6138 else if (strcmp (block_name, "ppr@") == 0
6139 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
6141 gfc_error ("Expected label '%s' for %s statement at %C",
6142 gfc_current_block ()->ns->proc_name->name,
6143 gfc_ascii_statement (*st));
6144 goto cleanup;
6147 if (gfc_match_eos () == MATCH_YES)
6148 return MATCH_YES;
6150 syntax:
6151 gfc_syntax_error (*st);
6153 cleanup:
6154 gfc_current_locus = old_loc;
6155 return MATCH_ERROR;
6160 /***************** Attribute declaration statements ****************/
6162 /* Set the attribute of a single variable. */
6164 static match
6165 attr_decl1 (void)
6167 char name[GFC_MAX_SYMBOL_LEN + 1];
6168 gfc_array_spec *as;
6169 gfc_symbol *sym;
6170 locus var_locus;
6171 match m;
6173 as = NULL;
6175 m = gfc_match_name (name);
6176 if (m != MATCH_YES)
6177 goto cleanup;
6179 if (find_special (name, &sym, false))
6180 return MATCH_ERROR;
6182 if (check_function_name (name) == FAILURE)
6184 m = MATCH_ERROR;
6185 goto cleanup;
6188 var_locus = gfc_current_locus;
6190 /* Deal with possible array specification for certain attributes. */
6191 if (current_attr.dimension
6192 || current_attr.codimension
6193 || current_attr.allocatable
6194 || current_attr.pointer
6195 || current_attr.target)
6197 m = gfc_match_array_spec (&as, !current_attr.codimension,
6198 !current_attr.dimension
6199 && !current_attr.pointer
6200 && !current_attr.target);
6201 if (m == MATCH_ERROR)
6202 goto cleanup;
6204 if (current_attr.dimension && m == MATCH_NO)
6206 gfc_error ("Missing array specification at %L in DIMENSION "
6207 "statement", &var_locus);
6208 m = MATCH_ERROR;
6209 goto cleanup;
6212 if (current_attr.dimension && sym->value)
6214 gfc_error ("Dimensions specified for %s at %L after its "
6215 "initialisation", sym->name, &var_locus);
6216 m = MATCH_ERROR;
6217 goto cleanup;
6220 if (current_attr.codimension && m == MATCH_NO)
6222 gfc_error ("Missing array specification at %L in CODIMENSION "
6223 "statement", &var_locus);
6224 m = MATCH_ERROR;
6225 goto cleanup;
6228 if ((current_attr.allocatable || current_attr.pointer)
6229 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6231 gfc_error ("Array specification must be deferred at %L", &var_locus);
6232 m = MATCH_ERROR;
6233 goto cleanup;
6237 /* Update symbol table. DIMENSION attribute is set in
6238 gfc_set_array_spec(). For CLASS variables, this must be applied
6239 to the first component, or '_data' field. */
6240 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6242 if (gfc_copy_attr (&CLASS_DATA (sym)->attr, &current_attr, &var_locus)
6243 == FAILURE)
6245 m = MATCH_ERROR;
6246 goto cleanup;
6249 else
6251 if (current_attr.dimension == 0 && current_attr.codimension == 0
6252 && gfc_copy_attr (&sym->attr, &current_attr, &var_locus) == FAILURE)
6254 m = MATCH_ERROR;
6255 goto cleanup;
6259 if (sym->ts.type == BT_CLASS
6260 && gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false) == FAILURE)
6262 m = MATCH_ERROR;
6263 goto cleanup;
6266 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
6268 m = MATCH_ERROR;
6269 goto cleanup;
6272 if (sym->attr.cray_pointee && sym->as != NULL)
6274 /* Fix the array spec. */
6275 m = gfc_mod_pointee_as (sym->as);
6276 if (m == MATCH_ERROR)
6277 goto cleanup;
6280 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
6282 m = MATCH_ERROR;
6283 goto cleanup;
6286 if ((current_attr.external || current_attr.intrinsic)
6287 && sym->attr.flavor != FL_PROCEDURE
6288 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6290 m = MATCH_ERROR;
6291 goto cleanup;
6294 add_hidden_procptr_result (sym);
6296 return MATCH_YES;
6298 cleanup:
6299 gfc_free_array_spec (as);
6300 return m;
6304 /* Generic attribute declaration subroutine. Used for attributes that
6305 just have a list of names. */
6307 static match
6308 attr_decl (void)
6310 match m;
6312 /* Gobble the optional double colon, by simply ignoring the result
6313 of gfc_match(). */
6314 gfc_match (" ::");
6316 for (;;)
6318 m = attr_decl1 ();
6319 if (m != MATCH_YES)
6320 break;
6322 if (gfc_match_eos () == MATCH_YES)
6324 m = MATCH_YES;
6325 break;
6328 if (gfc_match_char (',') != MATCH_YES)
6330 gfc_error ("Unexpected character in variable list at %C");
6331 m = MATCH_ERROR;
6332 break;
6336 return m;
6340 /* This routine matches Cray Pointer declarations of the form:
6341 pointer ( <pointer>, <pointee> )
6343 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6344 The pointer, if already declared, should be an integer. Otherwise, we
6345 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6346 be either a scalar, or an array declaration. No space is allocated for
6347 the pointee. For the statement
6348 pointer (ipt, ar(10))
6349 any subsequent uses of ar will be translated (in C-notation) as
6350 ar(i) => ((<type> *) ipt)(i)
6351 After gimplification, pointee variable will disappear in the code. */
6353 static match
6354 cray_pointer_decl (void)
6356 match m;
6357 gfc_array_spec *as = NULL;
6358 gfc_symbol *cptr; /* Pointer symbol. */
6359 gfc_symbol *cpte; /* Pointee symbol. */
6360 locus var_locus;
6361 bool done = false;
6363 while (!done)
6365 if (gfc_match_char ('(') != MATCH_YES)
6367 gfc_error ("Expected '(' at %C");
6368 return MATCH_ERROR;
6371 /* Match pointer. */
6372 var_locus = gfc_current_locus;
6373 gfc_clear_attr (&current_attr);
6374 gfc_add_cray_pointer (&current_attr, &var_locus);
6375 current_ts.type = BT_INTEGER;
6376 current_ts.kind = gfc_index_integer_kind;
6378 m = gfc_match_symbol (&cptr, 0);
6379 if (m != MATCH_YES)
6381 gfc_error ("Expected variable name at %C");
6382 return m;
6385 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
6386 return MATCH_ERROR;
6388 gfc_set_sym_referenced (cptr);
6390 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6392 cptr->ts.type = BT_INTEGER;
6393 cptr->ts.kind = gfc_index_integer_kind;
6395 else if (cptr->ts.type != BT_INTEGER)
6397 gfc_error ("Cray pointer at %C must be an integer");
6398 return MATCH_ERROR;
6400 else if (cptr->ts.kind < gfc_index_integer_kind)
6401 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6402 " memory addresses require %d bytes",
6403 cptr->ts.kind, gfc_index_integer_kind);
6405 if (gfc_match_char (',') != MATCH_YES)
6407 gfc_error ("Expected \",\" at %C");
6408 return MATCH_ERROR;
6411 /* Match Pointee. */
6412 var_locus = gfc_current_locus;
6413 gfc_clear_attr (&current_attr);
6414 gfc_add_cray_pointee (&current_attr, &var_locus);
6415 current_ts.type = BT_UNKNOWN;
6416 current_ts.kind = 0;
6418 m = gfc_match_symbol (&cpte, 0);
6419 if (m != MATCH_YES)
6421 gfc_error ("Expected variable name at %C");
6422 return m;
6425 /* Check for an optional array spec. */
6426 m = gfc_match_array_spec (&as, true, false);
6427 if (m == MATCH_ERROR)
6429 gfc_free_array_spec (as);
6430 return m;
6432 else if (m == MATCH_NO)
6434 gfc_free_array_spec (as);
6435 as = NULL;
6438 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
6439 return MATCH_ERROR;
6441 gfc_set_sym_referenced (cpte);
6443 if (cpte->as == NULL)
6445 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
6446 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6448 else if (as != NULL)
6450 gfc_error ("Duplicate array spec for Cray pointee at %C");
6451 gfc_free_array_spec (as);
6452 return MATCH_ERROR;
6455 as = NULL;
6457 if (cpte->as != NULL)
6459 /* Fix array spec. */
6460 m = gfc_mod_pointee_as (cpte->as);
6461 if (m == MATCH_ERROR)
6462 return m;
6465 /* Point the Pointee at the Pointer. */
6466 cpte->cp_pointer = cptr;
6468 if (gfc_match_char (')') != MATCH_YES)
6470 gfc_error ("Expected \")\" at %C");
6471 return MATCH_ERROR;
6473 m = gfc_match_char (',');
6474 if (m != MATCH_YES)
6475 done = true; /* Stop searching for more declarations. */
6479 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6480 || gfc_match_eos () != MATCH_YES)
6482 gfc_error ("Expected \",\" or end of statement at %C");
6483 return MATCH_ERROR;
6485 return MATCH_YES;
6489 match
6490 gfc_match_external (void)
6493 gfc_clear_attr (&current_attr);
6494 current_attr.external = 1;
6496 return attr_decl ();
6500 match
6501 gfc_match_intent (void)
6503 sym_intent intent;
6505 /* This is not allowed within a BLOCK construct! */
6506 if (gfc_current_state () == COMP_BLOCK)
6508 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6509 return MATCH_ERROR;
6512 intent = match_intent_spec ();
6513 if (intent == INTENT_UNKNOWN)
6514 return MATCH_ERROR;
6516 gfc_clear_attr (&current_attr);
6517 current_attr.intent = intent;
6519 return attr_decl ();
6523 match
6524 gfc_match_intrinsic (void)
6527 gfc_clear_attr (&current_attr);
6528 current_attr.intrinsic = 1;
6530 return attr_decl ();
6534 match
6535 gfc_match_optional (void)
6537 /* This is not allowed within a BLOCK construct! */
6538 if (gfc_current_state () == COMP_BLOCK)
6540 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6541 return MATCH_ERROR;
6544 gfc_clear_attr (&current_attr);
6545 current_attr.optional = 1;
6547 return attr_decl ();
6551 match
6552 gfc_match_pointer (void)
6554 gfc_gobble_whitespace ();
6555 if (gfc_peek_ascii_char () == '(')
6557 if (!gfc_option.flag_cray_pointer)
6559 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6560 "flag");
6561 return MATCH_ERROR;
6563 return cray_pointer_decl ();
6565 else
6567 gfc_clear_attr (&current_attr);
6568 current_attr.pointer = 1;
6570 return attr_decl ();
6575 match
6576 gfc_match_allocatable (void)
6578 gfc_clear_attr (&current_attr);
6579 current_attr.allocatable = 1;
6581 return attr_decl ();
6585 match
6586 gfc_match_codimension (void)
6588 gfc_clear_attr (&current_attr);
6589 current_attr.codimension = 1;
6591 return attr_decl ();
6595 match
6596 gfc_match_contiguous (void)
6598 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTIGUOUS statement at %C")
6599 == FAILURE)
6600 return MATCH_ERROR;
6602 gfc_clear_attr (&current_attr);
6603 current_attr.contiguous = 1;
6605 return attr_decl ();
6609 match
6610 gfc_match_dimension (void)
6612 gfc_clear_attr (&current_attr);
6613 current_attr.dimension = 1;
6615 return attr_decl ();
6619 match
6620 gfc_match_target (void)
6622 gfc_clear_attr (&current_attr);
6623 current_attr.target = 1;
6625 return attr_decl ();
6629 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6630 statement. */
6632 static match
6633 access_attr_decl (gfc_statement st)
6635 char name[GFC_MAX_SYMBOL_LEN + 1];
6636 interface_type type;
6637 gfc_user_op *uop;
6638 gfc_symbol *sym, *dt_sym;
6639 gfc_intrinsic_op op;
6640 match m;
6642 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6643 goto done;
6645 for (;;)
6647 m = gfc_match_generic_spec (&type, name, &op);
6648 if (m == MATCH_NO)
6649 goto syntax;
6650 if (m == MATCH_ERROR)
6651 return MATCH_ERROR;
6653 switch (type)
6655 case INTERFACE_NAMELESS:
6656 case INTERFACE_ABSTRACT:
6657 goto syntax;
6659 case INTERFACE_GENERIC:
6660 if (gfc_get_symbol (name, NULL, &sym))
6661 goto done;
6663 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
6664 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6665 sym->name, NULL) == FAILURE)
6666 return MATCH_ERROR;
6668 if (sym->attr.generic && (dt_sym = gfc_find_dt_in_generic (sym))
6669 && gfc_add_access (&dt_sym->attr,
6670 (st == ST_PUBLIC) ? ACCESS_PUBLIC
6671 : ACCESS_PRIVATE,
6672 sym->name, NULL) == FAILURE)
6673 return MATCH_ERROR;
6675 break;
6677 case INTERFACE_INTRINSIC_OP:
6678 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6680 gfc_intrinsic_op other_op;
6682 gfc_current_ns->operator_access[op] =
6683 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6685 /* Handle the case if there is another op with the same
6686 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
6687 other_op = gfc_equivalent_op (op);
6689 if (other_op != INTRINSIC_NONE)
6690 gfc_current_ns->operator_access[other_op] =
6691 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6694 else
6696 gfc_error ("Access specification of the %s operator at %C has "
6697 "already been specified", gfc_op2string (op));
6698 goto done;
6701 break;
6703 case INTERFACE_USER_OP:
6704 uop = gfc_get_uop (name);
6706 if (uop->access == ACCESS_UNKNOWN)
6708 uop->access = (st == ST_PUBLIC)
6709 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6711 else
6713 gfc_error ("Access specification of the .%s. operator at %C "
6714 "has already been specified", sym->name);
6715 goto done;
6718 break;
6721 if (gfc_match_char (',') == MATCH_NO)
6722 break;
6725 if (gfc_match_eos () != MATCH_YES)
6726 goto syntax;
6727 return MATCH_YES;
6729 syntax:
6730 gfc_syntax_error (st);
6732 done:
6733 return MATCH_ERROR;
6737 match
6738 gfc_match_protected (void)
6740 gfc_symbol *sym;
6741 match m;
6743 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6745 gfc_error ("PROTECTED at %C only allowed in specification "
6746 "part of a module");
6747 return MATCH_ERROR;
6751 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
6752 == FAILURE)
6753 return MATCH_ERROR;
6755 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6757 return MATCH_ERROR;
6760 if (gfc_match_eos () == MATCH_YES)
6761 goto syntax;
6763 for(;;)
6765 m = gfc_match_symbol (&sym, 0);
6766 switch (m)
6768 case MATCH_YES:
6769 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6770 == FAILURE)
6771 return MATCH_ERROR;
6772 goto next_item;
6774 case MATCH_NO:
6775 break;
6777 case MATCH_ERROR:
6778 return MATCH_ERROR;
6781 next_item:
6782 if (gfc_match_eos () == MATCH_YES)
6783 break;
6784 if (gfc_match_char (',') != MATCH_YES)
6785 goto syntax;
6788 return MATCH_YES;
6790 syntax:
6791 gfc_error ("Syntax error in PROTECTED statement at %C");
6792 return MATCH_ERROR;
6796 /* The PRIVATE statement is a bit weird in that it can be an attribute
6797 declaration, but also works as a standalone statement inside of a
6798 type declaration or a module. */
6800 match
6801 gfc_match_private (gfc_statement *st)
6804 if (gfc_match ("private") != MATCH_YES)
6805 return MATCH_NO;
6807 if (gfc_current_state () != COMP_MODULE
6808 && !(gfc_current_state () == COMP_DERIVED
6809 && gfc_state_stack->previous
6810 && gfc_state_stack->previous->state == COMP_MODULE)
6811 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6812 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6813 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6815 gfc_error ("PRIVATE statement at %C is only allowed in the "
6816 "specification part of a module");
6817 return MATCH_ERROR;
6820 if (gfc_current_state () == COMP_DERIVED)
6822 if (gfc_match_eos () == MATCH_YES)
6824 *st = ST_PRIVATE;
6825 return MATCH_YES;
6828 gfc_syntax_error (ST_PRIVATE);
6829 return MATCH_ERROR;
6832 if (gfc_match_eos () == MATCH_YES)
6834 *st = ST_PRIVATE;
6835 return MATCH_YES;
6838 *st = ST_ATTR_DECL;
6839 return access_attr_decl (ST_PRIVATE);
6843 match
6844 gfc_match_public (gfc_statement *st)
6847 if (gfc_match ("public") != MATCH_YES)
6848 return MATCH_NO;
6850 if (gfc_current_state () != COMP_MODULE)
6852 gfc_error ("PUBLIC statement at %C is only allowed in the "
6853 "specification part of a module");
6854 return MATCH_ERROR;
6857 if (gfc_match_eos () == MATCH_YES)
6859 *st = ST_PUBLIC;
6860 return MATCH_YES;
6863 *st = ST_ATTR_DECL;
6864 return access_attr_decl (ST_PUBLIC);
6868 /* Workhorse for gfc_match_parameter. */
6870 static match
6871 do_parm (void)
6873 gfc_symbol *sym;
6874 gfc_expr *init;
6875 match m;
6876 gfc_try t;
6878 m = gfc_match_symbol (&sym, 0);
6879 if (m == MATCH_NO)
6880 gfc_error ("Expected variable name at %C in PARAMETER statement");
6882 if (m != MATCH_YES)
6883 return m;
6885 if (gfc_match_char ('=') == MATCH_NO)
6887 gfc_error ("Expected = sign in PARAMETER statement at %C");
6888 return MATCH_ERROR;
6891 m = gfc_match_init_expr (&init);
6892 if (m == MATCH_NO)
6893 gfc_error ("Expected expression at %C in PARAMETER statement");
6894 if (m != MATCH_YES)
6895 return m;
6897 if (sym->ts.type == BT_UNKNOWN
6898 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6900 m = MATCH_ERROR;
6901 goto cleanup;
6904 if (gfc_check_assign_symbol (sym, init) == FAILURE
6905 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6907 m = MATCH_ERROR;
6908 goto cleanup;
6911 if (sym->value)
6913 gfc_error ("Initializing already initialized variable at %C");
6914 m = MATCH_ERROR;
6915 goto cleanup;
6918 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
6919 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6921 cleanup:
6922 gfc_free_expr (init);
6923 return m;
6927 /* Match a parameter statement, with the weird syntax that these have. */
6929 match
6930 gfc_match_parameter (void)
6932 match m;
6934 if (gfc_match_char ('(') == MATCH_NO)
6935 return MATCH_NO;
6937 for (;;)
6939 m = do_parm ();
6940 if (m != MATCH_YES)
6941 break;
6943 if (gfc_match (" )%t") == MATCH_YES)
6944 break;
6946 if (gfc_match_char (',') != MATCH_YES)
6948 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6949 m = MATCH_ERROR;
6950 break;
6954 return m;
6958 /* Save statements have a special syntax. */
6960 match
6961 gfc_match_save (void)
6963 char n[GFC_MAX_SYMBOL_LEN+1];
6964 gfc_common_head *c;
6965 gfc_symbol *sym;
6966 match m;
6968 if (gfc_match_eos () == MATCH_YES)
6970 if (gfc_current_ns->seen_save)
6972 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6973 "follows previous SAVE statement")
6974 == FAILURE)
6975 return MATCH_ERROR;
6978 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6979 return MATCH_YES;
6982 if (gfc_current_ns->save_all)
6984 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6985 "blanket SAVE statement")
6986 == FAILURE)
6987 return MATCH_ERROR;
6990 gfc_match (" ::");
6992 for (;;)
6994 m = gfc_match_symbol (&sym, 0);
6995 switch (m)
6997 case MATCH_YES:
6998 if (gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
6999 &gfc_current_locus) == FAILURE)
7000 return MATCH_ERROR;
7001 goto next_item;
7003 case MATCH_NO:
7004 break;
7006 case MATCH_ERROR:
7007 return MATCH_ERROR;
7010 m = gfc_match (" / %n /", &n);
7011 if (m == MATCH_ERROR)
7012 return MATCH_ERROR;
7013 if (m == MATCH_NO)
7014 goto syntax;
7016 c = gfc_get_common (n, 0);
7017 c->saved = 1;
7019 gfc_current_ns->seen_save = 1;
7021 next_item:
7022 if (gfc_match_eos () == MATCH_YES)
7023 break;
7024 if (gfc_match_char (',') != MATCH_YES)
7025 goto syntax;
7028 return MATCH_YES;
7030 syntax:
7031 gfc_error ("Syntax error in SAVE statement at %C");
7032 return MATCH_ERROR;
7036 match
7037 gfc_match_value (void)
7039 gfc_symbol *sym;
7040 match m;
7042 /* This is not allowed within a BLOCK construct! */
7043 if (gfc_current_state () == COMP_BLOCK)
7045 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7046 return MATCH_ERROR;
7049 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
7050 == FAILURE)
7051 return MATCH_ERROR;
7053 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7055 return MATCH_ERROR;
7058 if (gfc_match_eos () == MATCH_YES)
7059 goto syntax;
7061 for(;;)
7063 m = gfc_match_symbol (&sym, 0);
7064 switch (m)
7066 case MATCH_YES:
7067 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
7068 == FAILURE)
7069 return MATCH_ERROR;
7070 goto next_item;
7072 case MATCH_NO:
7073 break;
7075 case MATCH_ERROR:
7076 return MATCH_ERROR;
7079 next_item:
7080 if (gfc_match_eos () == MATCH_YES)
7081 break;
7082 if (gfc_match_char (',') != MATCH_YES)
7083 goto syntax;
7086 return MATCH_YES;
7088 syntax:
7089 gfc_error ("Syntax error in VALUE statement at %C");
7090 return MATCH_ERROR;
7094 match
7095 gfc_match_volatile (void)
7097 gfc_symbol *sym;
7098 match m;
7100 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
7101 == FAILURE)
7102 return MATCH_ERROR;
7104 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7106 return MATCH_ERROR;
7109 if (gfc_match_eos () == MATCH_YES)
7110 goto syntax;
7112 for(;;)
7114 /* VOLATILE is special because it can be added to host-associated
7115 symbols locally. Except for coarrays. */
7116 m = gfc_match_symbol (&sym, 1);
7117 switch (m)
7119 case MATCH_YES:
7120 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7121 for variable in a BLOCK which is defined outside of the BLOCK. */
7122 if (sym->ns != gfc_current_ns && sym->attr.codimension)
7124 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
7125 "%C, which is use-/host-associated", sym->name);
7126 return MATCH_ERROR;
7128 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
7129 == FAILURE)
7130 return MATCH_ERROR;
7131 goto next_item;
7133 case MATCH_NO:
7134 break;
7136 case MATCH_ERROR:
7137 return MATCH_ERROR;
7140 next_item:
7141 if (gfc_match_eos () == MATCH_YES)
7142 break;
7143 if (gfc_match_char (',') != MATCH_YES)
7144 goto syntax;
7147 return MATCH_YES;
7149 syntax:
7150 gfc_error ("Syntax error in VOLATILE statement at %C");
7151 return MATCH_ERROR;
7155 match
7156 gfc_match_asynchronous (void)
7158 gfc_symbol *sym;
7159 match m;
7161 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS statement at %C")
7162 == FAILURE)
7163 return MATCH_ERROR;
7165 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
7167 return MATCH_ERROR;
7170 if (gfc_match_eos () == MATCH_YES)
7171 goto syntax;
7173 for(;;)
7175 /* ASYNCHRONOUS is special because it can be added to host-associated
7176 symbols locally. */
7177 m = gfc_match_symbol (&sym, 1);
7178 switch (m)
7180 case MATCH_YES:
7181 if (gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus)
7182 == FAILURE)
7183 return MATCH_ERROR;
7184 goto next_item;
7186 case MATCH_NO:
7187 break;
7189 case MATCH_ERROR:
7190 return MATCH_ERROR;
7193 next_item:
7194 if (gfc_match_eos () == MATCH_YES)
7195 break;
7196 if (gfc_match_char (',') != MATCH_YES)
7197 goto syntax;
7200 return MATCH_YES;
7202 syntax:
7203 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7204 return MATCH_ERROR;
7208 /* Match a module procedure statement. Note that we have to modify
7209 symbols in the parent's namespace because the current one was there
7210 to receive symbols that are in an interface's formal argument list. */
7212 match
7213 gfc_match_modproc (void)
7215 char name[GFC_MAX_SYMBOL_LEN + 1];
7216 gfc_symbol *sym;
7217 match m;
7218 locus old_locus;
7219 gfc_namespace *module_ns;
7220 gfc_interface *old_interface_head, *interface;
7222 if (gfc_state_stack->state != COMP_INTERFACE
7223 || gfc_state_stack->previous == NULL
7224 || current_interface.type == INTERFACE_NAMELESS
7225 || current_interface.type == INTERFACE_ABSTRACT)
7227 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7228 "interface");
7229 return MATCH_ERROR;
7232 module_ns = gfc_current_ns->parent;
7233 for (; module_ns; module_ns = module_ns->parent)
7234 if (module_ns->proc_name->attr.flavor == FL_MODULE
7235 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7236 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7237 && !module_ns->proc_name->attr.contained))
7238 break;
7240 if (module_ns == NULL)
7241 return MATCH_ERROR;
7243 /* Store the current state of the interface. We will need it if we
7244 end up with a syntax error and need to recover. */
7245 old_interface_head = gfc_current_interface_head ();
7247 /* Check if the F2008 optional double colon appears. */
7248 gfc_gobble_whitespace ();
7249 old_locus = gfc_current_locus;
7250 if (gfc_match ("::") == MATCH_YES)
7252 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: double colon in "
7253 "MODULE PROCEDURE statement at %L", &old_locus)
7254 == FAILURE)
7255 return MATCH_ERROR;
7257 else
7258 gfc_current_locus = old_locus;
7260 for (;;)
7262 bool last = false;
7263 old_locus = gfc_current_locus;
7265 m = gfc_match_name (name);
7266 if (m == MATCH_NO)
7267 goto syntax;
7268 if (m != MATCH_YES)
7269 return MATCH_ERROR;
7271 /* Check for syntax error before starting to add symbols to the
7272 current namespace. */
7273 if (gfc_match_eos () == MATCH_YES)
7274 last = true;
7276 if (!last && gfc_match_char (',') != MATCH_YES)
7277 goto syntax;
7279 /* Now we're sure the syntax is valid, we process this item
7280 further. */
7281 if (gfc_get_symbol (name, module_ns, &sym))
7282 return MATCH_ERROR;
7284 if (sym->attr.intrinsic)
7286 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7287 "PROCEDURE", &old_locus);
7288 return MATCH_ERROR;
7291 if (sym->attr.proc != PROC_MODULE
7292 && gfc_add_procedure (&sym->attr, PROC_MODULE,
7293 sym->name, NULL) == FAILURE)
7294 return MATCH_ERROR;
7296 if (gfc_add_interface (sym) == FAILURE)
7297 return MATCH_ERROR;
7299 sym->attr.mod_proc = 1;
7300 sym->declared_at = old_locus;
7302 if (last)
7303 break;
7306 return MATCH_YES;
7308 syntax:
7309 /* Restore the previous state of the interface. */
7310 interface = gfc_current_interface_head ();
7311 gfc_set_current_interface_head (old_interface_head);
7313 /* Free the new interfaces. */
7314 while (interface != old_interface_head)
7316 gfc_interface *i = interface->next;
7317 free (interface);
7318 interface = i;
7321 /* And issue a syntax error. */
7322 gfc_syntax_error (ST_MODULE_PROC);
7323 return MATCH_ERROR;
7327 /* Check a derived type that is being extended. */
7328 static gfc_symbol*
7329 check_extended_derived_type (char *name)
7331 gfc_symbol *extended;
7333 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7335 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7336 return NULL;
7339 if (!extended)
7341 gfc_error ("No such symbol in TYPE definition at %C");
7342 return NULL;
7345 extended = gfc_find_dt_in_generic (extended);
7347 if (extended->attr.flavor != FL_DERIVED)
7349 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7350 "derived type", name);
7351 return NULL;
7354 if (extended->attr.is_bind_c)
7356 gfc_error ("'%s' cannot be extended at %C because it "
7357 "is BIND(C)", extended->name);
7358 return NULL;
7361 if (extended->attr.sequence)
7363 gfc_error ("'%s' cannot be extended at %C because it "
7364 "is a SEQUENCE type", extended->name);
7365 return NULL;
7368 return extended;
7372 /* Match the optional attribute specifiers for a type declaration.
7373 Return MATCH_ERROR if an error is encountered in one of the handled
7374 attributes (public, private, bind(c)), MATCH_NO if what's found is
7375 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7376 checking on attribute conflicts needs to be done. */
7378 match
7379 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7381 /* See if the derived type is marked as private. */
7382 if (gfc_match (" , private") == MATCH_YES)
7384 if (gfc_current_state () != COMP_MODULE)
7386 gfc_error ("Derived type at %C can only be PRIVATE in the "
7387 "specification part of a module");
7388 return MATCH_ERROR;
7391 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
7392 return MATCH_ERROR;
7394 else if (gfc_match (" , public") == MATCH_YES)
7396 if (gfc_current_state () != COMP_MODULE)
7398 gfc_error ("Derived type at %C can only be PUBLIC in the "
7399 "specification part of a module");
7400 return MATCH_ERROR;
7403 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
7404 return MATCH_ERROR;
7406 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7408 /* If the type is defined to be bind(c) it then needs to make
7409 sure that all fields are interoperable. This will
7410 need to be a semantic check on the finished derived type.
7411 See 15.2.3 (lines 9-12) of F2003 draft. */
7412 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
7413 return MATCH_ERROR;
7415 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7417 else if (gfc_match (" , abstract") == MATCH_YES)
7419 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
7420 == FAILURE)
7421 return MATCH_ERROR;
7423 if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
7424 return MATCH_ERROR;
7426 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
7428 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
7429 return MATCH_ERROR;
7431 else
7432 return MATCH_NO;
7434 /* If we get here, something matched. */
7435 return MATCH_YES;
7439 /* Match the beginning of a derived type declaration. If a type name
7440 was the result of a function, then it is possible to have a symbol
7441 already to be known as a derived type yet have no components. */
7443 match
7444 gfc_match_derived_decl (void)
7446 char name[GFC_MAX_SYMBOL_LEN + 1];
7447 char parent[GFC_MAX_SYMBOL_LEN + 1];
7448 symbol_attribute attr;
7449 gfc_symbol *sym, *gensym;
7450 gfc_symbol *extended;
7451 match m;
7452 match is_type_attr_spec = MATCH_NO;
7453 bool seen_attr = false;
7454 gfc_interface *intr = NULL, *head;
7456 if (gfc_current_state () == COMP_DERIVED)
7457 return MATCH_NO;
7459 name[0] = '\0';
7460 parent[0] = '\0';
7461 gfc_clear_attr (&attr);
7462 extended = NULL;
7466 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7467 if (is_type_attr_spec == MATCH_ERROR)
7468 return MATCH_ERROR;
7469 if (is_type_attr_spec == MATCH_YES)
7470 seen_attr = true;
7471 } while (is_type_attr_spec == MATCH_YES);
7473 /* Deal with derived type extensions. The extension attribute has
7474 been added to 'attr' but now the parent type must be found and
7475 checked. */
7476 if (parent[0])
7477 extended = check_extended_derived_type (parent);
7479 if (parent[0] && !extended)
7480 return MATCH_ERROR;
7482 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7484 gfc_error ("Expected :: in TYPE definition at %C");
7485 return MATCH_ERROR;
7488 m = gfc_match (" %n%t", name);
7489 if (m != MATCH_YES)
7490 return m;
7492 /* Make sure the name is not the name of an intrinsic type. */
7493 if (gfc_is_intrinsic_typename (name))
7495 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7496 "type", name);
7497 return MATCH_ERROR;
7500 if (gfc_get_symbol (name, NULL, &gensym))
7501 return MATCH_ERROR;
7503 if (!gensym->attr.generic && gensym->ts.type != BT_UNKNOWN)
7505 gfc_error ("Derived type name '%s' at %C already has a basic type "
7506 "of %s", gensym->name, gfc_typename (&gensym->ts));
7507 return MATCH_ERROR;
7510 if (!gensym->attr.generic
7511 && gfc_add_generic (&gensym->attr, gensym->name, NULL) == FAILURE)
7512 return MATCH_ERROR;
7514 if (!gensym->attr.function
7515 && gfc_add_function (&gensym->attr, gensym->name, NULL) == FAILURE)
7516 return MATCH_ERROR;
7518 sym = gfc_find_dt_in_generic (gensym);
7520 if (sym && (sym->components != NULL || sym->attr.zero_comp))
7522 gfc_error ("Derived type definition of '%s' at %C has already been "
7523 "defined", sym->name);
7524 return MATCH_ERROR;
7527 if (!sym)
7529 /* Use upper case to save the actual derived-type symbol. */
7530 gfc_get_symbol (gfc_get_string ("%c%s",
7531 (char) TOUPPER ((unsigned char) gensym->name[0]),
7532 &gensym->name[1]), NULL, &sym);
7533 sym->name = gfc_get_string (gensym->name);
7534 head = gensym->generic;
7535 intr = gfc_get_interface ();
7536 intr->sym = sym;
7537 intr->where = gfc_current_locus;
7538 intr->sym->declared_at = gfc_current_locus;
7539 intr->next = head;
7540 gensym->generic = intr;
7541 gensym->attr.if_source = IFSRC_DECL;
7544 /* The symbol may already have the derived attribute without the
7545 components. The ways this can happen is via a function
7546 definition, an INTRINSIC statement or a subtype in another
7547 derived type that is a pointer. The first part of the AND clause
7548 is true if the symbol is not the return value of a function. */
7549 if (sym->attr.flavor != FL_DERIVED
7550 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
7551 return MATCH_ERROR;
7553 if (attr.access != ACCESS_UNKNOWN
7554 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
7555 return MATCH_ERROR;
7556 else if (sym->attr.access == ACCESS_UNKNOWN
7557 && gensym->attr.access != ACCESS_UNKNOWN
7558 && gfc_add_access (&sym->attr, gensym->attr.access, sym->name, NULL)
7559 == FAILURE)
7560 return MATCH_ERROR;
7562 if (sym->attr.access != ACCESS_UNKNOWN
7563 && gensym->attr.access == ACCESS_UNKNOWN)
7564 gensym->attr.access = sym->attr.access;
7566 /* See if the derived type was labeled as bind(c). */
7567 if (attr.is_bind_c != 0)
7568 sym->attr.is_bind_c = attr.is_bind_c;
7570 /* Construct the f2k_derived namespace if it is not yet there. */
7571 if (!sym->f2k_derived)
7572 sym->f2k_derived = gfc_get_namespace (NULL, 0);
7574 if (extended && !sym->components)
7576 gfc_component *p;
7577 gfc_symtree *st;
7579 /* Add the extended derived type as the first component. */
7580 gfc_add_component (sym, parent, &p);
7581 extended->refs++;
7582 gfc_set_sym_referenced (extended);
7584 p->ts.type = BT_DERIVED;
7585 p->ts.u.derived = extended;
7586 p->initializer = gfc_default_initializer (&p->ts);
7588 /* Set extension level. */
7589 if (extended->attr.extension == 255)
7591 /* Since the extension field is 8 bit wide, we can only have
7592 up to 255 extension levels. */
7593 gfc_error ("Maximum extension level reached with type '%s' at %L",
7594 extended->name, &extended->declared_at);
7595 return MATCH_ERROR;
7597 sym->attr.extension = extended->attr.extension + 1;
7599 /* Provide the links between the extended type and its extension. */
7600 if (!extended->f2k_derived)
7601 extended->f2k_derived = gfc_get_namespace (NULL, 0);
7602 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7603 st->n.sym = sym;
7606 if (!sym->hash_value)
7607 /* Set the hash for the compound name for this type. */
7608 sym->hash_value = gfc_hash_value (sym);
7610 /* Take over the ABSTRACT attribute. */
7611 sym->attr.abstract = attr.abstract;
7613 gfc_new_block = sym;
7615 return MATCH_YES;
7619 /* Cray Pointees can be declared as:
7620 pointer (ipt, a (n,m,...,*)) */
7622 match
7623 gfc_mod_pointee_as (gfc_array_spec *as)
7625 as->cray_pointee = true; /* This will be useful to know later. */
7626 if (as->type == AS_ASSUMED_SIZE)
7627 as->cp_was_assumed = true;
7628 else if (as->type == AS_ASSUMED_SHAPE)
7630 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7631 return MATCH_ERROR;
7633 return MATCH_YES;
7637 /* Match the enum definition statement, here we are trying to match
7638 the first line of enum definition statement.
7639 Returns MATCH_YES if match is found. */
7641 match
7642 gfc_match_enum (void)
7644 match m;
7646 m = gfc_match_eos ();
7647 if (m != MATCH_YES)
7648 return m;
7650 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
7651 == FAILURE)
7652 return MATCH_ERROR;
7654 return MATCH_YES;
7658 /* Returns an initializer whose value is one higher than the value of the
7659 LAST_INITIALIZER argument. If the argument is NULL, the
7660 initializers value will be set to zero. The initializer's kind
7661 will be set to gfc_c_int_kind.
7663 If -fshort-enums is given, the appropriate kind will be selected
7664 later after all enumerators have been parsed. A warning is issued
7665 here if an initializer exceeds gfc_c_int_kind. */
7667 static gfc_expr *
7668 enum_initializer (gfc_expr *last_initializer, locus where)
7670 gfc_expr *result;
7671 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7673 mpz_init (result->value.integer);
7675 if (last_initializer != NULL)
7677 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7678 result->where = last_initializer->where;
7680 if (gfc_check_integer_range (result->value.integer,
7681 gfc_c_int_kind) != ARITH_OK)
7683 gfc_error ("Enumerator exceeds the C integer type at %C");
7684 return NULL;
7687 else
7689 /* Control comes here, if it's the very first enumerator and no
7690 initializer has been given. It will be initialized to zero. */
7691 mpz_set_si (result->value.integer, 0);
7694 return result;
7698 /* Match a variable name with an optional initializer. When this
7699 subroutine is called, a variable is expected to be parsed next.
7700 Depending on what is happening at the moment, updates either the
7701 symbol table or the current interface. */
7703 static match
7704 enumerator_decl (void)
7706 char name[GFC_MAX_SYMBOL_LEN + 1];
7707 gfc_expr *initializer;
7708 gfc_array_spec *as = NULL;
7709 gfc_symbol *sym;
7710 locus var_locus;
7711 match m;
7712 gfc_try t;
7713 locus old_locus;
7715 initializer = NULL;
7716 old_locus = gfc_current_locus;
7718 /* When we get here, we've just matched a list of attributes and
7719 maybe a type and a double colon. The next thing we expect to see
7720 is the name of the symbol. */
7721 m = gfc_match_name (name);
7722 if (m != MATCH_YES)
7723 goto cleanup;
7725 var_locus = gfc_current_locus;
7727 /* OK, we've successfully matched the declaration. Now put the
7728 symbol in the current namespace. If we fail to create the symbol,
7729 bail out. */
7730 if (build_sym (name, NULL, false, &as, &var_locus) == FAILURE)
7732 m = MATCH_ERROR;
7733 goto cleanup;
7736 /* The double colon must be present in order to have initializers.
7737 Otherwise the statement is ambiguous with an assignment statement. */
7738 if (colon_seen)
7740 if (gfc_match_char ('=') == MATCH_YES)
7742 m = gfc_match_init_expr (&initializer);
7743 if (m == MATCH_NO)
7745 gfc_error ("Expected an initialization expression at %C");
7746 m = MATCH_ERROR;
7749 if (m != MATCH_YES)
7750 goto cleanup;
7754 /* If we do not have an initializer, the initialization value of the
7755 previous enumerator (stored in last_initializer) is incremented
7756 by 1 and is used to initialize the current enumerator. */
7757 if (initializer == NULL)
7758 initializer = enum_initializer (last_initializer, old_locus);
7760 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7762 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7763 &var_locus);
7764 m = MATCH_ERROR;
7765 goto cleanup;
7768 /* Store this current initializer, for the next enumerator variable
7769 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7770 use last_initializer below. */
7771 last_initializer = initializer;
7772 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7774 /* Maintain enumerator history. */
7775 gfc_find_symbol (name, NULL, 0, &sym);
7776 create_enum_history (sym, last_initializer);
7778 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
7780 cleanup:
7781 /* Free stuff up and return. */
7782 gfc_free_expr (initializer);
7784 return m;
7788 /* Match the enumerator definition statement. */
7790 match
7791 gfc_match_enumerator_def (void)
7793 match m;
7794 gfc_try t;
7796 gfc_clear_ts (&current_ts);
7798 m = gfc_match (" enumerator");
7799 if (m != MATCH_YES)
7800 return m;
7802 m = gfc_match (" :: ");
7803 if (m == MATCH_ERROR)
7804 return m;
7806 colon_seen = (m == MATCH_YES);
7808 if (gfc_current_state () != COMP_ENUM)
7810 gfc_error ("ENUM definition statement expected before %C");
7811 gfc_free_enum_history ();
7812 return MATCH_ERROR;
7815 (&current_ts)->type = BT_INTEGER;
7816 (&current_ts)->kind = gfc_c_int_kind;
7818 gfc_clear_attr (&current_attr);
7819 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
7820 if (t == FAILURE)
7822 m = MATCH_ERROR;
7823 goto cleanup;
7826 for (;;)
7828 m = enumerator_decl ();
7829 if (m == MATCH_ERROR)
7831 gfc_free_enum_history ();
7832 goto cleanup;
7834 if (m == MATCH_NO)
7835 break;
7837 if (gfc_match_eos () == MATCH_YES)
7838 goto cleanup;
7839 if (gfc_match_char (',') != MATCH_YES)
7840 break;
7843 if (gfc_current_state () == COMP_ENUM)
7845 gfc_free_enum_history ();
7846 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7847 m = MATCH_ERROR;
7850 cleanup:
7851 gfc_free_array_spec (current_as);
7852 current_as = NULL;
7853 return m;
7858 /* Match binding attributes. */
7860 static match
7861 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7863 bool found_passing = false;
7864 bool seen_ptr = false;
7865 match m = MATCH_YES;
7867 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
7868 this case the defaults are in there. */
7869 ba->access = ACCESS_UNKNOWN;
7870 ba->pass_arg = NULL;
7871 ba->pass_arg_num = 0;
7872 ba->nopass = 0;
7873 ba->non_overridable = 0;
7874 ba->deferred = 0;
7875 ba->ppc = ppc;
7877 /* If we find a comma, we believe there are binding attributes. */
7878 m = gfc_match_char (',');
7879 if (m == MATCH_NO)
7880 goto done;
7884 /* Access specifier. */
7886 m = gfc_match (" public");
7887 if (m == MATCH_ERROR)
7888 goto error;
7889 if (m == MATCH_YES)
7891 if (ba->access != ACCESS_UNKNOWN)
7893 gfc_error ("Duplicate access-specifier at %C");
7894 goto error;
7897 ba->access = ACCESS_PUBLIC;
7898 continue;
7901 m = gfc_match (" private");
7902 if (m == MATCH_ERROR)
7903 goto error;
7904 if (m == MATCH_YES)
7906 if (ba->access != ACCESS_UNKNOWN)
7908 gfc_error ("Duplicate access-specifier at %C");
7909 goto error;
7912 ba->access = ACCESS_PRIVATE;
7913 continue;
7916 /* If inside GENERIC, the following is not allowed. */
7917 if (!generic)
7920 /* NOPASS flag. */
7921 m = gfc_match (" nopass");
7922 if (m == MATCH_ERROR)
7923 goto error;
7924 if (m == MATCH_YES)
7926 if (found_passing)
7928 gfc_error ("Binding attributes already specify passing,"
7929 " illegal NOPASS at %C");
7930 goto error;
7933 found_passing = true;
7934 ba->nopass = 1;
7935 continue;
7938 /* PASS possibly including argument. */
7939 m = gfc_match (" pass");
7940 if (m == MATCH_ERROR)
7941 goto error;
7942 if (m == MATCH_YES)
7944 char arg[GFC_MAX_SYMBOL_LEN + 1];
7946 if (found_passing)
7948 gfc_error ("Binding attributes already specify passing,"
7949 " illegal PASS at %C");
7950 goto error;
7953 m = gfc_match (" ( %n )", arg);
7954 if (m == MATCH_ERROR)
7955 goto error;
7956 if (m == MATCH_YES)
7957 ba->pass_arg = gfc_get_string (arg);
7958 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7960 found_passing = true;
7961 ba->nopass = 0;
7962 continue;
7965 if (ppc)
7967 /* POINTER flag. */
7968 m = gfc_match (" pointer");
7969 if (m == MATCH_ERROR)
7970 goto error;
7971 if (m == MATCH_YES)
7973 if (seen_ptr)
7975 gfc_error ("Duplicate POINTER attribute at %C");
7976 goto error;
7979 seen_ptr = true;
7980 continue;
7983 else
7985 /* NON_OVERRIDABLE flag. */
7986 m = gfc_match (" non_overridable");
7987 if (m == MATCH_ERROR)
7988 goto error;
7989 if (m == MATCH_YES)
7991 if (ba->non_overridable)
7993 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7994 goto error;
7997 ba->non_overridable = 1;
7998 continue;
8001 /* DEFERRED flag. */
8002 m = gfc_match (" deferred");
8003 if (m == MATCH_ERROR)
8004 goto error;
8005 if (m == MATCH_YES)
8007 if (ba->deferred)
8009 gfc_error ("Duplicate DEFERRED at %C");
8010 goto error;
8013 ba->deferred = 1;
8014 continue;
8020 /* Nothing matching found. */
8021 if (generic)
8022 gfc_error ("Expected access-specifier at %C");
8023 else
8024 gfc_error ("Expected binding attribute at %C");
8025 goto error;
8027 while (gfc_match_char (',') == MATCH_YES);
8029 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8030 if (ba->non_overridable && ba->deferred)
8032 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8033 goto error;
8036 m = MATCH_YES;
8038 done:
8039 if (ba->access == ACCESS_UNKNOWN)
8040 ba->access = gfc_typebound_default_access;
8042 if (ppc && !seen_ptr)
8044 gfc_error ("POINTER attribute is required for procedure pointer component"
8045 " at %C");
8046 goto error;
8049 return m;
8051 error:
8052 return MATCH_ERROR;
8056 /* Match a PROCEDURE specific binding inside a derived type. */
8058 static match
8059 match_procedure_in_type (void)
8061 char name[GFC_MAX_SYMBOL_LEN + 1];
8062 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
8063 char* target = NULL, *ifc = NULL;
8064 gfc_typebound_proc tb;
8065 bool seen_colons;
8066 bool seen_attrs;
8067 match m;
8068 gfc_symtree* stree;
8069 gfc_namespace* ns;
8070 gfc_symbol* block;
8071 int num;
8073 /* Check current state. */
8074 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
8075 block = gfc_state_stack->previous->sym;
8076 gcc_assert (block);
8078 /* Try to match PROCEDURE(interface). */
8079 if (gfc_match (" (") == MATCH_YES)
8081 m = gfc_match_name (target_buf);
8082 if (m == MATCH_ERROR)
8083 return m;
8084 if (m != MATCH_YES)
8086 gfc_error ("Interface-name expected after '(' at %C");
8087 return MATCH_ERROR;
8090 if (gfc_match (" )") != MATCH_YES)
8092 gfc_error ("')' expected at %C");
8093 return MATCH_ERROR;
8096 ifc = target_buf;
8099 /* Construct the data structure. */
8100 memset (&tb, 0, sizeof (tb));
8101 tb.where = gfc_current_locus;
8103 /* Match binding attributes. */
8104 m = match_binding_attributes (&tb, false, false);
8105 if (m == MATCH_ERROR)
8106 return m;
8107 seen_attrs = (m == MATCH_YES);
8109 /* Check that attribute DEFERRED is given if an interface is specified. */
8110 if (tb.deferred && !ifc)
8112 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8113 return MATCH_ERROR;
8115 if (ifc && !tb.deferred)
8117 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8118 return MATCH_ERROR;
8121 /* Match the colons. */
8122 m = gfc_match (" ::");
8123 if (m == MATCH_ERROR)
8124 return m;
8125 seen_colons = (m == MATCH_YES);
8126 if (seen_attrs && !seen_colons)
8128 gfc_error ("Expected '::' after binding-attributes at %C");
8129 return MATCH_ERROR;
8132 /* Match the binding names. */
8133 for(num=1;;num++)
8135 m = gfc_match_name (name);
8136 if (m == MATCH_ERROR)
8137 return m;
8138 if (m == MATCH_NO)
8140 gfc_error ("Expected binding name at %C");
8141 return MATCH_ERROR;
8144 if (num>1 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: PROCEDURE list"
8145 " at %C") == FAILURE)
8146 return MATCH_ERROR;
8148 /* Try to match the '=> target', if it's there. */
8149 target = ifc;
8150 m = gfc_match (" =>");
8151 if (m == MATCH_ERROR)
8152 return m;
8153 if (m == MATCH_YES)
8155 if (tb.deferred)
8157 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
8158 return MATCH_ERROR;
8161 if (!seen_colons)
8163 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
8164 " at %C");
8165 return MATCH_ERROR;
8168 m = gfc_match_name (target_buf);
8169 if (m == MATCH_ERROR)
8170 return m;
8171 if (m == MATCH_NO)
8173 gfc_error ("Expected binding target after '=>' at %C");
8174 return MATCH_ERROR;
8176 target = target_buf;
8179 /* If no target was found, it has the same name as the binding. */
8180 if (!target)
8181 target = name;
8183 /* Get the namespace to insert the symbols into. */
8184 ns = block->f2k_derived;
8185 gcc_assert (ns);
8187 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8188 if (tb.deferred && !block->attr.abstract)
8190 gfc_error ("Type '%s' containing DEFERRED binding at %C "
8191 "is not ABSTRACT", block->name);
8192 return MATCH_ERROR;
8195 /* See if we already have a binding with this name in the symtree which
8196 would be an error. If a GENERIC already targetted this binding, it may
8197 be already there but then typebound is still NULL. */
8198 stree = gfc_find_symtree (ns->tb_sym_root, name);
8199 if (stree && stree->n.tb)
8201 gfc_error ("There is already a procedure with binding name '%s' for "
8202 "the derived type '%s' at %C", name, block->name);
8203 return MATCH_ERROR;
8206 /* Insert it and set attributes. */
8208 if (!stree)
8210 stree = gfc_new_symtree (&ns->tb_sym_root, name);
8211 gcc_assert (stree);
8213 stree->n.tb = gfc_get_typebound_proc (&tb);
8215 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
8216 false))
8217 return MATCH_ERROR;
8218 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
8220 if (gfc_match_eos () == MATCH_YES)
8221 return MATCH_YES;
8222 if (gfc_match_char (',') != MATCH_YES)
8223 goto syntax;
8226 syntax:
8227 gfc_error ("Syntax error in PROCEDURE statement at %C");
8228 return MATCH_ERROR;
8232 /* Match a GENERIC procedure binding inside a derived type. */
8234 match
8235 gfc_match_generic (void)
8237 char name[GFC_MAX_SYMBOL_LEN + 1];
8238 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
8239 gfc_symbol* block;
8240 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
8241 gfc_typebound_proc* tb;
8242 gfc_namespace* ns;
8243 interface_type op_type;
8244 gfc_intrinsic_op op;
8245 match m;
8247 /* Check current state. */
8248 if (gfc_current_state () == COMP_DERIVED)
8250 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8251 return MATCH_ERROR;
8253 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
8254 return MATCH_NO;
8255 block = gfc_state_stack->previous->sym;
8256 ns = block->f2k_derived;
8257 gcc_assert (block && ns);
8259 memset (&tbattr, 0, sizeof (tbattr));
8260 tbattr.where = gfc_current_locus;
8262 /* See if we get an access-specifier. */
8263 m = match_binding_attributes (&tbattr, true, false);
8264 if (m == MATCH_ERROR)
8265 goto error;
8267 /* Now the colons, those are required. */
8268 if (gfc_match (" ::") != MATCH_YES)
8270 gfc_error ("Expected '::' at %C");
8271 goto error;
8274 /* Match the binding name; depending on type (operator / generic) format
8275 it for future error messages into bind_name. */
8277 m = gfc_match_generic_spec (&op_type, name, &op);
8278 if (m == MATCH_ERROR)
8279 return MATCH_ERROR;
8280 if (m == MATCH_NO)
8282 gfc_error ("Expected generic name or operator descriptor at %C");
8283 goto error;
8286 switch (op_type)
8288 case INTERFACE_GENERIC:
8289 snprintf (bind_name, sizeof (bind_name), "%s", name);
8290 break;
8292 case INTERFACE_USER_OP:
8293 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8294 break;
8296 case INTERFACE_INTRINSIC_OP:
8297 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8298 gfc_op2string (op));
8299 break;
8301 default:
8302 gcc_unreachable ();
8305 /* Match the required =>. */
8306 if (gfc_match (" =>") != MATCH_YES)
8308 gfc_error ("Expected '=>' at %C");
8309 goto error;
8312 /* Try to find existing GENERIC binding with this name / for this operator;
8313 if there is something, check that it is another GENERIC and then extend
8314 it rather than building a new node. Otherwise, create it and put it
8315 at the right position. */
8317 switch (op_type)
8319 case INTERFACE_USER_OP:
8320 case INTERFACE_GENERIC:
8322 const bool is_op = (op_type == INTERFACE_USER_OP);
8323 gfc_symtree* st;
8325 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8326 if (st)
8328 tb = st->n.tb;
8329 gcc_assert (tb);
8331 else
8332 tb = NULL;
8334 break;
8337 case INTERFACE_INTRINSIC_OP:
8338 tb = ns->tb_op[op];
8339 break;
8341 default:
8342 gcc_unreachable ();
8345 if (tb)
8347 if (!tb->is_generic)
8349 gcc_assert (op_type == INTERFACE_GENERIC);
8350 gfc_error ("There's already a non-generic procedure with binding name"
8351 " '%s' for the derived type '%s' at %C",
8352 bind_name, block->name);
8353 goto error;
8356 if (tb->access != tbattr.access)
8358 gfc_error ("Binding at %C must have the same access as already"
8359 " defined binding '%s'", bind_name);
8360 goto error;
8363 else
8365 tb = gfc_get_typebound_proc (NULL);
8366 tb->where = gfc_current_locus;
8367 tb->access = tbattr.access;
8368 tb->is_generic = 1;
8369 tb->u.generic = NULL;
8371 switch (op_type)
8373 case INTERFACE_GENERIC:
8374 case INTERFACE_USER_OP:
8376 const bool is_op = (op_type == INTERFACE_USER_OP);
8377 gfc_symtree* st;
8379 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8380 name);
8381 gcc_assert (st);
8382 st->n.tb = tb;
8384 break;
8387 case INTERFACE_INTRINSIC_OP:
8388 ns->tb_op[op] = tb;
8389 break;
8391 default:
8392 gcc_unreachable ();
8396 /* Now, match all following names as specific targets. */
8399 gfc_symtree* target_st;
8400 gfc_tbp_generic* target;
8402 m = gfc_match_name (name);
8403 if (m == MATCH_ERROR)
8404 goto error;
8405 if (m == MATCH_NO)
8407 gfc_error ("Expected specific binding name at %C");
8408 goto error;
8411 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8413 /* See if this is a duplicate specification. */
8414 for (target = tb->u.generic; target; target = target->next)
8415 if (target_st == target->specific_st)
8417 gfc_error ("'%s' already defined as specific binding for the"
8418 " generic '%s' at %C", name, bind_name);
8419 goto error;
8422 target = gfc_get_tbp_generic ();
8423 target->specific_st = target_st;
8424 target->specific = NULL;
8425 target->next = tb->u.generic;
8426 target->is_operator = ((op_type == INTERFACE_USER_OP)
8427 || (op_type == INTERFACE_INTRINSIC_OP));
8428 tb->u.generic = target;
8430 while (gfc_match (" ,") == MATCH_YES);
8432 /* Here should be the end. */
8433 if (gfc_match_eos () != MATCH_YES)
8435 gfc_error ("Junk after GENERIC binding at %C");
8436 goto error;
8439 return MATCH_YES;
8441 error:
8442 return MATCH_ERROR;
8446 /* Match a FINAL declaration inside a derived type. */
8448 match
8449 gfc_match_final_decl (void)
8451 char name[GFC_MAX_SYMBOL_LEN + 1];
8452 gfc_symbol* sym;
8453 match m;
8454 gfc_namespace* module_ns;
8455 bool first, last;
8456 gfc_symbol* block;
8458 if (gfc_current_form == FORM_FREE)
8460 char c = gfc_peek_ascii_char ();
8461 if (!gfc_is_whitespace (c) && c != ':')
8462 return MATCH_NO;
8465 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8467 if (gfc_current_form == FORM_FIXED)
8468 return MATCH_NO;
8470 gfc_error ("FINAL declaration at %C must be inside a derived type "
8471 "CONTAINS section");
8472 return MATCH_ERROR;
8475 block = gfc_state_stack->previous->sym;
8476 gcc_assert (block);
8478 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8479 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8481 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8482 " specification part of a MODULE");
8483 return MATCH_ERROR;
8486 module_ns = gfc_current_ns;
8487 gcc_assert (module_ns);
8488 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8490 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8491 if (gfc_match (" ::") == MATCH_ERROR)
8492 return MATCH_ERROR;
8494 /* Match the sequence of procedure names. */
8495 first = true;
8496 last = false;
8499 gfc_finalizer* f;
8501 if (first && gfc_match_eos () == MATCH_YES)
8503 gfc_error ("Empty FINAL at %C");
8504 return MATCH_ERROR;
8507 m = gfc_match_name (name);
8508 if (m == MATCH_NO)
8510 gfc_error ("Expected module procedure name at %C");
8511 return MATCH_ERROR;
8513 else if (m != MATCH_YES)
8514 return MATCH_ERROR;
8516 if (gfc_match_eos () == MATCH_YES)
8517 last = true;
8518 if (!last && gfc_match_char (',') != MATCH_YES)
8520 gfc_error ("Expected ',' at %C");
8521 return MATCH_ERROR;
8524 if (gfc_get_symbol (name, module_ns, &sym))
8526 gfc_error ("Unknown procedure name \"%s\" at %C", name);
8527 return MATCH_ERROR;
8530 /* Mark the symbol as module procedure. */
8531 if (sym->attr.proc != PROC_MODULE
8532 && gfc_add_procedure (&sym->attr, PROC_MODULE,
8533 sym->name, NULL) == FAILURE)
8534 return MATCH_ERROR;
8536 /* Check if we already have this symbol in the list, this is an error. */
8537 for (f = block->f2k_derived->finalizers; f; f = f->next)
8538 if (f->proc_sym == sym)
8540 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8541 name);
8542 return MATCH_ERROR;
8545 /* Add this symbol to the list of finalizers. */
8546 gcc_assert (block->f2k_derived);
8547 ++sym->refs;
8548 f = XCNEW (gfc_finalizer);
8549 f->proc_sym = sym;
8550 f->proc_tree = NULL;
8551 f->where = gfc_current_locus;
8552 f->next = block->f2k_derived->finalizers;
8553 block->f2k_derived->finalizers = f;
8555 first = false;
8557 while (!last);
8559 return MATCH_YES;
8563 const ext_attr_t ext_attr_list[] = {
8564 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8565 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8566 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
8567 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
8568 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
8569 { NULL, EXT_ATTR_LAST, NULL }
8572 /* Match a !GCC$ ATTRIBUTES statement of the form:
8573 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8574 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8576 TODO: We should support all GCC attributes using the same syntax for
8577 the attribute list, i.e. the list in C
8578 __attributes(( attribute-list ))
8579 matches then
8580 !GCC$ ATTRIBUTES attribute-list ::
8581 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8582 saved into a TREE.
8584 As there is absolutely no risk of confusion, we should never return
8585 MATCH_NO. */
8586 match
8587 gfc_match_gcc_attributes (void)
8589 symbol_attribute attr;
8590 char name[GFC_MAX_SYMBOL_LEN + 1];
8591 unsigned id;
8592 gfc_symbol *sym;
8593 match m;
8595 gfc_clear_attr (&attr);
8596 for(;;)
8598 char ch;
8600 if (gfc_match_name (name) != MATCH_YES)
8601 return MATCH_ERROR;
8603 for (id = 0; id < EXT_ATTR_LAST; id++)
8604 if (strcmp (name, ext_attr_list[id].name) == 0)
8605 break;
8607 if (id == EXT_ATTR_LAST)
8609 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8610 return MATCH_ERROR;
8613 if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
8614 == FAILURE)
8615 return MATCH_ERROR;
8617 gfc_gobble_whitespace ();
8618 ch = gfc_next_ascii_char ();
8619 if (ch == ':')
8621 /* This is the successful exit condition for the loop. */
8622 if (gfc_next_ascii_char () == ':')
8623 break;
8626 if (ch == ',')
8627 continue;
8629 goto syntax;
8632 if (gfc_match_eos () == MATCH_YES)
8633 goto syntax;
8635 for(;;)
8637 m = gfc_match_name (name);
8638 if (m != MATCH_YES)
8639 return m;
8641 if (find_special (name, &sym, true))
8642 return MATCH_ERROR;
8644 sym->attr.ext_attr |= attr.ext_attr;
8646 if (gfc_match_eos () == MATCH_YES)
8647 break;
8649 if (gfc_match_char (',') != MATCH_YES)
8650 goto syntax;
8653 return MATCH_YES;
8655 syntax:
8656 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
8657 return MATCH_ERROR;