2011-01-29 Tobias Burnus <burnus@net-b.de>
[official-gcc.git] / gcc / fortran / decl.c
blob638a7386d156cb950953cc9816dcebd6c0f6163e
1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
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"
30 /* Macros to access allocate memory for gfc_data_variable,
31 gfc_data_value and gfc_data. */
32 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
33 #define gfc_get_data_value() XCNEW (gfc_data_value)
34 #define gfc_get_data() XCNEW (gfc_data)
37 /* This flag is set if an old-style length selector is matched
38 during a type-declaration statement. */
40 static int old_char_selector;
42 /* When variables acquire types and attributes from a declaration
43 statement, they get them from the following static variables. The
44 first part of a declaration sets these variables and the second
45 part copies these into symbol structures. */
47 static gfc_typespec current_ts;
49 static symbol_attribute current_attr;
50 static gfc_array_spec *current_as;
51 static int colon_seen;
53 /* The current binding label (if any). */
54 static char curr_binding_label[GFC_MAX_BINDING_LABEL_LEN + 1];
55 /* Need to know how many identifiers are on the current data declaration
56 line in case we're given the BIND(C) attribute with a NAME= specifier. */
57 static int num_idents_on_line;
58 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
59 can supply a name if the curr_binding_label is nil and NAME= was not. */
60 static int has_name_equals = 0;
62 /* Initializer of the previous enumerator. */
64 static gfc_expr *last_initializer;
66 /* History of all the enumerators is maintained, so that
67 kind values of all the enumerators could be updated depending
68 upon the maximum initialized value. */
70 typedef struct enumerator_history
72 gfc_symbol *sym;
73 gfc_expr *initializer;
74 struct enumerator_history *next;
76 enumerator_history;
78 /* Header of enum history chain. */
80 static enumerator_history *enum_history = NULL;
82 /* Pointer of enum history node containing largest initializer. */
84 static enumerator_history *max_enum = NULL;
86 /* gfc_new_block points to the symbol of a newly matched block. */
88 gfc_symbol *gfc_new_block;
90 bool gfc_matching_function;
93 /********************* DATA statement subroutines *********************/
95 static bool in_match_data = false;
97 bool
98 gfc_in_match_data (void)
100 return in_match_data;
103 static void
104 set_in_match_data (bool set_value)
106 in_match_data = set_value;
109 /* Free a gfc_data_variable structure and everything beneath it. */
111 static void
112 free_variable (gfc_data_variable *p)
114 gfc_data_variable *q;
116 for (; p; p = q)
118 q = p->next;
119 gfc_free_expr (p->expr);
120 gfc_free_iterator (&p->iter, 0);
121 free_variable (p->list);
122 gfc_free (p);
127 /* Free a gfc_data_value structure and everything beneath it. */
129 static void
130 free_value (gfc_data_value *p)
132 gfc_data_value *q;
134 for (; p; p = q)
136 q = p->next;
137 mpz_clear (p->repeat);
138 gfc_free_expr (p->expr);
139 gfc_free (p);
144 /* Free a list of gfc_data structures. */
146 void
147 gfc_free_data (gfc_data *p)
149 gfc_data *q;
151 for (; p; p = q)
153 q = p->next;
154 free_variable (p->var);
155 free_value (p->value);
156 gfc_free (p);
161 /* Free all data in a namespace. */
163 static void
164 gfc_free_data_all (gfc_namespace *ns)
166 gfc_data *d;
168 for (;ns->data;)
170 d = ns->data->next;
171 gfc_free (ns->data);
172 ns->data = d;
177 static match var_element (gfc_data_variable *);
179 /* Match a list of variables terminated by an iterator and a right
180 parenthesis. */
182 static match
183 var_list (gfc_data_variable *parent)
185 gfc_data_variable *tail, var;
186 match m;
188 m = var_element (&var);
189 if (m == MATCH_ERROR)
190 return MATCH_ERROR;
191 if (m == MATCH_NO)
192 goto syntax;
194 tail = gfc_get_data_variable ();
195 *tail = var;
197 parent->list = tail;
199 for (;;)
201 if (gfc_match_char (',') != MATCH_YES)
202 goto syntax;
204 m = gfc_match_iterator (&parent->iter, 1);
205 if (m == MATCH_YES)
206 break;
207 if (m == MATCH_ERROR)
208 return MATCH_ERROR;
210 m = var_element (&var);
211 if (m == MATCH_ERROR)
212 return MATCH_ERROR;
213 if (m == MATCH_NO)
214 goto syntax;
216 tail->next = gfc_get_data_variable ();
217 tail = tail->next;
219 *tail = var;
222 if (gfc_match_char (')') != MATCH_YES)
223 goto syntax;
224 return MATCH_YES;
226 syntax:
227 gfc_syntax_error (ST_DATA);
228 return MATCH_ERROR;
232 /* Match a single element in a data variable list, which can be a
233 variable-iterator list. */
235 static match
236 var_element (gfc_data_variable *new_var)
238 match m;
239 gfc_symbol *sym;
241 memset (new_var, 0, sizeof (gfc_data_variable));
243 if (gfc_match_char ('(') == MATCH_YES)
244 return var_list (new_var);
246 m = gfc_match_variable (&new_var->expr, 0);
247 if (m != MATCH_YES)
248 return m;
250 sym = new_var->expr->symtree->n.sym;
252 /* Symbol should already have an associated type. */
253 if (gfc_check_symbol_typed (sym, gfc_current_ns,
254 false, gfc_current_locus) == FAILURE)
255 return MATCH_ERROR;
257 if (!sym->attr.function && gfc_current_ns->parent
258 && gfc_current_ns->parent == sym->ns)
260 gfc_error ("Host associated variable '%s' may not be in the DATA "
261 "statement at %C", sym->name);
262 return MATCH_ERROR;
265 if (gfc_current_state () != COMP_BLOCK_DATA
266 && sym->attr.in_common
267 && gfc_notify_std (GFC_STD_GNU, "Extension: initialization of "
268 "common block variable '%s' in DATA statement at %C",
269 sym->name) == FAILURE)
270 return MATCH_ERROR;
272 if (gfc_add_data (&sym->attr, sym->name, &new_var->expr->where) == FAILURE)
273 return MATCH_ERROR;
275 return MATCH_YES;
279 /* Match the top-level list of data variables. */
281 static match
282 top_var_list (gfc_data *d)
284 gfc_data_variable var, *tail, *new_var;
285 match m;
287 tail = NULL;
289 for (;;)
291 m = var_element (&var);
292 if (m == MATCH_NO)
293 goto syntax;
294 if (m == MATCH_ERROR)
295 return MATCH_ERROR;
297 new_var = gfc_get_data_variable ();
298 *new_var = var;
300 if (tail == NULL)
301 d->var = new_var;
302 else
303 tail->next = new_var;
305 tail = new_var;
307 if (gfc_match_char ('/') == MATCH_YES)
308 break;
309 if (gfc_match_char (',') != MATCH_YES)
310 goto syntax;
313 return MATCH_YES;
315 syntax:
316 gfc_syntax_error (ST_DATA);
317 gfc_free_data_all (gfc_current_ns);
318 return MATCH_ERROR;
322 static match
323 match_data_constant (gfc_expr **result)
325 char name[GFC_MAX_SYMBOL_LEN + 1];
326 gfc_symbol *sym;
327 gfc_expr *expr;
328 match m;
329 locus old_loc;
331 m = gfc_match_literal_constant (&expr, 1);
332 if (m == MATCH_YES)
334 *result = expr;
335 return MATCH_YES;
338 if (m == MATCH_ERROR)
339 return MATCH_ERROR;
341 m = gfc_match_null (result);
342 if (m != MATCH_NO)
343 return m;
345 old_loc = gfc_current_locus;
347 /* Should this be a structure component, try to match it
348 before matching a name. */
349 m = gfc_match_rvalue (result);
350 if (m == MATCH_ERROR)
351 return m;
353 if (m == MATCH_YES && (*result)->expr_type == EXPR_STRUCTURE)
355 if (gfc_simplify_expr (*result, 0) == FAILURE)
356 m = MATCH_ERROR;
357 return m;
360 gfc_current_locus = old_loc;
362 m = gfc_match_name (name);
363 if (m != MATCH_YES)
364 return m;
366 if (gfc_find_symbol (name, NULL, 1, &sym))
367 return MATCH_ERROR;
369 if (sym == NULL
370 || (sym->attr.flavor != FL_PARAMETER && sym->attr.flavor != FL_DERIVED))
372 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
373 name);
374 return MATCH_ERROR;
376 else if (sym->attr.flavor == FL_DERIVED)
377 return gfc_match_structure_constructor (sym, result, false);
379 /* Check to see if the value is an initialization array expression. */
380 if (sym->value->expr_type == EXPR_ARRAY)
382 gfc_current_locus = old_loc;
384 m = gfc_match_init_expr (result);
385 if (m == MATCH_ERROR)
386 return m;
388 if (m == MATCH_YES)
390 if (gfc_simplify_expr (*result, 0) == FAILURE)
391 m = MATCH_ERROR;
393 if ((*result)->expr_type == EXPR_CONSTANT)
394 return m;
395 else
397 gfc_error ("Invalid initializer %s in Data statement at %C", name);
398 return MATCH_ERROR;
403 *result = gfc_copy_expr (sym->value);
404 return MATCH_YES;
408 /* Match a list of values in a DATA statement. The leading '/' has
409 already been seen at this point. */
411 static match
412 top_val_list (gfc_data *data)
414 gfc_data_value *new_val, *tail;
415 gfc_expr *expr;
416 match m;
418 tail = NULL;
420 for (;;)
422 m = match_data_constant (&expr);
423 if (m == MATCH_NO)
424 goto syntax;
425 if (m == MATCH_ERROR)
426 return MATCH_ERROR;
428 new_val = gfc_get_data_value ();
429 mpz_init (new_val->repeat);
431 if (tail == NULL)
432 data->value = new_val;
433 else
434 tail->next = new_val;
436 tail = new_val;
438 if (expr->ts.type != BT_INTEGER || gfc_match_char ('*') != MATCH_YES)
440 tail->expr = expr;
441 mpz_set_ui (tail->repeat, 1);
443 else
445 if (expr->ts.type == BT_INTEGER)
446 mpz_set (tail->repeat, expr->value.integer);
447 gfc_free_expr (expr);
449 m = match_data_constant (&tail->expr);
450 if (m == MATCH_NO)
451 goto syntax;
452 if (m == MATCH_ERROR)
453 return MATCH_ERROR;
456 if (gfc_match_char ('/') == MATCH_YES)
457 break;
458 if (gfc_match_char (',') == MATCH_NO)
459 goto syntax;
462 return MATCH_YES;
464 syntax:
465 gfc_syntax_error (ST_DATA);
466 gfc_free_data_all (gfc_current_ns);
467 return MATCH_ERROR;
471 /* Matches an old style initialization. */
473 static match
474 match_old_style_init (const char *name)
476 match m;
477 gfc_symtree *st;
478 gfc_symbol *sym;
479 gfc_data *newdata;
481 /* Set up data structure to hold initializers. */
482 gfc_find_sym_tree (name, NULL, 0, &st);
483 sym = st->n.sym;
485 newdata = gfc_get_data ();
486 newdata->var = gfc_get_data_variable ();
487 newdata->var->expr = gfc_get_variable_expr (st);
488 newdata->where = gfc_current_locus;
490 /* Match initial value list. This also eats the terminal '/'. */
491 m = top_val_list (newdata);
492 if (m != MATCH_YES)
494 gfc_free (newdata);
495 return m;
498 if (gfc_pure (NULL))
500 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
501 gfc_free (newdata);
502 return MATCH_ERROR;
505 if (gfc_implicit_pure (NULL))
506 gfc_current_ns->proc_name->attr.implicit_pure = 0;
508 /* Mark the variable as having appeared in a data statement. */
509 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
511 gfc_free (newdata);
512 return MATCH_ERROR;
515 /* Chain in namespace list of DATA initializers. */
516 newdata->next = gfc_current_ns->data;
517 gfc_current_ns->data = newdata;
519 return m;
523 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
524 we are matching a DATA statement and are therefore issuing an error
525 if we encounter something unexpected, if not, we're trying to match
526 an old-style initialization expression of the form INTEGER I /2/. */
528 match
529 gfc_match_data (void)
531 gfc_data *new_data;
532 match m;
534 set_in_match_data (true);
536 for (;;)
538 new_data = gfc_get_data ();
539 new_data->where = gfc_current_locus;
541 m = top_var_list (new_data);
542 if (m != MATCH_YES)
543 goto cleanup;
545 m = top_val_list (new_data);
546 if (m != MATCH_YES)
547 goto cleanup;
549 new_data->next = gfc_current_ns->data;
550 gfc_current_ns->data = new_data;
552 if (gfc_match_eos () == MATCH_YES)
553 break;
555 gfc_match_char (','); /* Optional comma */
558 set_in_match_data (false);
560 if (gfc_pure (NULL))
562 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
563 return MATCH_ERROR;
566 if (gfc_implicit_pure (NULL))
567 gfc_current_ns->proc_name->attr.implicit_pure = 0;
569 return MATCH_YES;
571 cleanup:
572 set_in_match_data (false);
573 gfc_free_data (new_data);
574 return MATCH_ERROR;
578 /************************ Declaration statements *********************/
581 /* Auxilliary function to merge DIMENSION and CODIMENSION array specs. */
583 static void
584 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
586 int i;
588 if (to->rank == 0 && from->rank > 0)
590 to->rank = from->rank;
591 to->type = from->type;
592 to->cray_pointee = from->cray_pointee;
593 to->cp_was_assumed = from->cp_was_assumed;
595 for (i = 0; i < to->corank; i++)
597 to->lower[from->rank + i] = to->lower[i];
598 to->upper[from->rank + i] = to->upper[i];
600 for (i = 0; i < from->rank; i++)
602 if (copy)
604 to->lower[i] = gfc_copy_expr (from->lower[i]);
605 to->upper[i] = gfc_copy_expr (from->upper[i]);
607 else
609 to->lower[i] = from->lower[i];
610 to->upper[i] = from->upper[i];
614 else if (to->corank == 0 && from->corank > 0)
616 to->corank = from->corank;
617 to->cotype = from->cotype;
619 for (i = 0; i < from->corank; i++)
621 if (copy)
623 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
624 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
626 else
628 to->lower[to->rank + i] = from->lower[i];
629 to->upper[to->rank + i] = from->upper[i];
636 /* Match an intent specification. Since this can only happen after an
637 INTENT word, a legal intent-spec must follow. */
639 static sym_intent
640 match_intent_spec (void)
643 if (gfc_match (" ( in out )") == MATCH_YES)
644 return INTENT_INOUT;
645 if (gfc_match (" ( in )") == MATCH_YES)
646 return INTENT_IN;
647 if (gfc_match (" ( out )") == MATCH_YES)
648 return INTENT_OUT;
650 gfc_error ("Bad INTENT specification at %C");
651 return INTENT_UNKNOWN;
655 /* Matches a character length specification, which is either a
656 specification expression, '*', or ':'. */
658 static match
659 char_len_param_value (gfc_expr **expr, bool *deferred)
661 match m;
663 *expr = NULL;
664 *deferred = false;
666 if (gfc_match_char ('*') == MATCH_YES)
667 return MATCH_YES;
669 if (gfc_match_char (':') == MATCH_YES)
671 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: deferred type "
672 "parameter at %C") == FAILURE)
673 return MATCH_ERROR;
675 *deferred = true;
677 return MATCH_YES;
680 m = gfc_match_expr (expr);
682 if (m == MATCH_YES
683 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
684 return MATCH_ERROR;
686 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
688 if ((*expr)->value.function.actual
689 && (*expr)->value.function.actual->expr->symtree)
691 gfc_expr *e;
692 e = (*expr)->value.function.actual->expr;
693 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
694 && e->expr_type == EXPR_VARIABLE)
696 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
697 goto syntax;
698 if (e->symtree->n.sym->ts.type == BT_CHARACTER
699 && e->symtree->n.sym->ts.u.cl
700 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
701 goto syntax;
705 return m;
707 syntax:
708 gfc_error ("Conflict in attributes of function argument at %C");
709 return MATCH_ERROR;
713 /* A character length is a '*' followed by a literal integer or a
714 char_len_param_value in parenthesis. */
716 static match
717 match_char_length (gfc_expr **expr, bool *deferred)
719 int length;
720 match m;
722 *deferred = false;
723 m = gfc_match_char ('*');
724 if (m != MATCH_YES)
725 return m;
727 m = gfc_match_small_literal_int (&length, NULL);
728 if (m == MATCH_ERROR)
729 return m;
731 if (m == MATCH_YES)
733 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
734 "Old-style character length at %C") == FAILURE)
735 return MATCH_ERROR;
736 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
737 return m;
740 if (gfc_match_char ('(') == MATCH_NO)
741 goto syntax;
743 m = char_len_param_value (expr, deferred);
744 if (m != MATCH_YES && gfc_matching_function)
746 gfc_undo_symbols ();
747 m = MATCH_YES;
750 if (m == MATCH_ERROR)
751 return m;
752 if (m == MATCH_NO)
753 goto syntax;
755 if (gfc_match_char (')') == MATCH_NO)
757 gfc_free_expr (*expr);
758 *expr = NULL;
759 goto syntax;
762 return MATCH_YES;
764 syntax:
765 gfc_error ("Syntax error in character length specification at %C");
766 return MATCH_ERROR;
770 /* Special subroutine for finding a symbol. Check if the name is found
771 in the current name space. If not, and we're compiling a function or
772 subroutine and the parent compilation unit is an interface, then check
773 to see if the name we've been given is the name of the interface
774 (located in another namespace). */
776 static int
777 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
779 gfc_state_data *s;
780 gfc_symtree *st;
781 int i;
783 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
784 if (i == 0)
786 *result = st ? st->n.sym : NULL;
787 goto end;
790 if (gfc_current_state () != COMP_SUBROUTINE
791 && gfc_current_state () != COMP_FUNCTION)
792 goto end;
794 s = gfc_state_stack->previous;
795 if (s == NULL)
796 goto end;
798 if (s->state != COMP_INTERFACE)
799 goto end;
800 if (s->sym == NULL)
801 goto end; /* Nameless interface. */
803 if (strcmp (name, s->sym->name) == 0)
805 *result = s->sym;
806 return 0;
809 end:
810 return i;
814 /* Special subroutine for getting a symbol node associated with a
815 procedure name, used in SUBROUTINE and FUNCTION statements. The
816 symbol is created in the parent using with symtree node in the
817 child unit pointing to the symbol. If the current namespace has no
818 parent, then the symbol is just created in the current unit. */
820 static int
821 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
823 gfc_symtree *st;
824 gfc_symbol *sym;
825 int rc = 0;
827 /* Module functions have to be left in their own namespace because
828 they have potentially (almost certainly!) already been referenced.
829 In this sense, they are rather like external functions. This is
830 fixed up in resolve.c(resolve_entries), where the symbol name-
831 space is set to point to the master function, so that the fake
832 result mechanism can work. */
833 if (module_fcn_entry)
835 /* Present if entry is declared to be a module procedure. */
836 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
838 if (*result == NULL)
839 rc = gfc_get_symbol (name, NULL, result);
840 else if (!gfc_get_symbol (name, NULL, &sym) && sym
841 && (*result)->ts.type == BT_UNKNOWN
842 && sym->attr.flavor == FL_UNKNOWN)
843 /* Pick up the typespec for the entry, if declared in the function
844 body. Note that this symbol is FL_UNKNOWN because it will
845 only have appeared in a type declaration. The local symtree
846 is set to point to the module symbol and a unique symtree
847 to the local version. This latter ensures a correct clearing
848 of the symbols. */
850 /* If the ENTRY proceeds its specification, we need to ensure
851 that this does not raise a "has no IMPLICIT type" error. */
852 if (sym->ts.type == BT_UNKNOWN)
853 sym->attr.untyped = 1;
855 (*result)->ts = sym->ts;
857 /* Put the symbol in the procedure namespace so that, should
858 the ENTRY precede its specification, the specification
859 can be applied. */
860 (*result)->ns = gfc_current_ns;
862 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
863 st->n.sym = *result;
864 st = gfc_get_unique_symtree (gfc_current_ns);
865 st->n.sym = sym;
868 else
869 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
871 if (rc)
872 return rc;
874 sym = *result;
875 gfc_current_ns->refs++;
877 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
879 /* Trap another encompassed procedure with the same name. All
880 these conditions are necessary to avoid picking up an entry
881 whose name clashes with that of the encompassing procedure;
882 this is handled using gsymbols to register unique,globally
883 accessible names. */
884 if (sym->attr.flavor != 0
885 && sym->attr.proc != 0
886 && (sym->attr.subroutine || sym->attr.function)
887 && sym->attr.if_source != IFSRC_UNKNOWN)
888 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
889 name, &sym->declared_at);
891 /* Trap a procedure with a name the same as interface in the
892 encompassing scope. */
893 if (sym->attr.generic != 0
894 && (sym->attr.subroutine || sym->attr.function)
895 && !sym->attr.mod_proc)
896 gfc_error_now ("Name '%s' at %C is already defined"
897 " as a generic interface at %L",
898 name, &sym->declared_at);
900 /* Trap declarations of attributes in encompassing scope. The
901 signature for this is that ts.kind is set. Legitimate
902 references only set ts.type. */
903 if (sym->ts.kind != 0
904 && !sym->attr.implicit_type
905 && sym->attr.proc == 0
906 && gfc_current_ns->parent != NULL
907 && sym->attr.access == 0
908 && !module_fcn_entry)
909 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
910 "and must not have attributes declared at %L",
911 name, &sym->declared_at);
914 if (gfc_current_ns->parent == NULL || *result == NULL)
915 return rc;
917 /* Module function entries will already have a symtree in
918 the current namespace but will need one at module level. */
919 if (module_fcn_entry)
921 /* Present if entry is declared to be a module procedure. */
922 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
923 if (st == NULL)
924 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
926 else
927 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
929 st->n.sym = sym;
930 sym->refs++;
932 /* See if the procedure should be a module procedure. */
934 if (((sym->ns->proc_name != NULL
935 && sym->ns->proc_name->attr.flavor == FL_MODULE
936 && sym->attr.proc != PROC_MODULE)
937 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
938 && gfc_add_procedure (&sym->attr, PROC_MODULE,
939 sym->name, NULL) == FAILURE)
940 rc = 2;
942 return rc;
946 /* Verify that the given symbol representing a parameter is C
947 interoperable, by checking to see if it was marked as such after
948 its declaration. If the given symbol is not interoperable, a
949 warning is reported, thus removing the need to return the status to
950 the calling function. The standard does not require the user use
951 one of the iso_c_binding named constants to declare an
952 interoperable parameter, but we can't be sure if the param is C
953 interop or not if the user doesn't. For example, integer(4) may be
954 legal Fortran, but doesn't have meaning in C. It may interop with
955 a number of the C types, which causes a problem because the
956 compiler can't know which one. This code is almost certainly not
957 portable, and the user will get what they deserve if the C type
958 across platforms isn't always interoperable with integer(4). If
959 the user had used something like integer(c_int) or integer(c_long),
960 the compiler could have automatically handled the varying sizes
961 across platforms. */
963 gfc_try
964 verify_c_interop_param (gfc_symbol *sym)
966 int is_c_interop = 0;
967 gfc_try retval = SUCCESS;
969 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
970 Don't repeat the checks here. */
971 if (sym->attr.implicit_type)
972 return SUCCESS;
974 /* For subroutines or functions that are passed to a BIND(C) procedure,
975 they're interoperable if they're BIND(C) and their params are all
976 interoperable. */
977 if (sym->attr.flavor == FL_PROCEDURE)
979 if (sym->attr.is_bind_c == 0)
981 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
982 "attribute to be C interoperable", sym->name,
983 &(sym->declared_at));
985 return FAILURE;
987 else
989 if (sym->attr.is_c_interop == 1)
990 /* We've already checked this procedure; don't check it again. */
991 return SUCCESS;
992 else
993 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
994 sym->common_block);
998 /* See if we've stored a reference to a procedure that owns sym. */
999 if (sym->ns != NULL && sym->ns->proc_name != NULL)
1001 if (sym->ns->proc_name->attr.is_bind_c == 1)
1003 is_c_interop =
1004 (verify_c_interop (&(sym->ts))
1005 == SUCCESS ? 1 : 0);
1007 if (is_c_interop != 1)
1009 /* Make personalized messages to give better feedback. */
1010 if (sym->ts.type == BT_DERIVED)
1011 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
1012 "procedure '%s' but is not C interoperable "
1013 "because derived type '%s' is not C interoperable",
1014 sym->name, &(sym->declared_at),
1015 sym->ns->proc_name->name,
1016 sym->ts.u.derived->name);
1017 else
1018 gfc_warning ("Variable '%s' at %L is a parameter to the "
1019 "BIND(C) procedure '%s' but may not be C "
1020 "interoperable",
1021 sym->name, &(sym->declared_at),
1022 sym->ns->proc_name->name);
1025 /* Character strings are only C interoperable if they have a
1026 length of 1. */
1027 if (sym->ts.type == BT_CHARACTER)
1029 gfc_charlen *cl = sym->ts.u.cl;
1030 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1031 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1033 gfc_error ("Character argument '%s' at %L "
1034 "must be length 1 because "
1035 "procedure '%s' is BIND(C)",
1036 sym->name, &sym->declared_at,
1037 sym->ns->proc_name->name);
1038 retval = FAILURE;
1042 /* We have to make sure that any param to a bind(c) routine does
1043 not have the allocatable, pointer, or optional attributes,
1044 according to J3/04-007, section 5.1. */
1045 if (sym->attr.allocatable == 1)
1047 gfc_error ("Variable '%s' at %L cannot have the "
1048 "ALLOCATABLE attribute because procedure '%s'"
1049 " is BIND(C)", sym->name, &(sym->declared_at),
1050 sym->ns->proc_name->name);
1051 retval = FAILURE;
1054 if (sym->attr.pointer == 1)
1056 gfc_error ("Variable '%s' at %L cannot have the "
1057 "POINTER attribute because procedure '%s'"
1058 " is BIND(C)", sym->name, &(sym->declared_at),
1059 sym->ns->proc_name->name);
1060 retval = FAILURE;
1063 if (sym->attr.optional == 1)
1065 gfc_error ("Variable '%s' at %L cannot have the "
1066 "OPTIONAL attribute because procedure '%s'"
1067 " is BIND(C)", sym->name, &(sym->declared_at),
1068 sym->ns->proc_name->name);
1069 retval = FAILURE;
1072 /* Make sure that if it has the dimension attribute, that it is
1073 either assumed size or explicit shape. */
1074 if (sym->as != NULL)
1076 if (sym->as->type == AS_ASSUMED_SHAPE)
1078 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1079 "argument to the procedure '%s' at %L because "
1080 "the procedure is BIND(C)", sym->name,
1081 &(sym->declared_at), sym->ns->proc_name->name,
1082 &(sym->ns->proc_name->declared_at));
1083 retval = FAILURE;
1086 if (sym->as->type == AS_DEFERRED)
1088 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1089 "argument to the procedure '%s' at %L because "
1090 "the procedure is BIND(C)", sym->name,
1091 &(sym->declared_at), sym->ns->proc_name->name,
1092 &(sym->ns->proc_name->declared_at));
1093 retval = FAILURE;
1099 return retval;
1104 /* Function called by variable_decl() that adds a name to the symbol table. */
1106 static gfc_try
1107 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1108 gfc_array_spec **as, locus *var_locus)
1110 symbol_attribute attr;
1111 gfc_symbol *sym;
1113 if (gfc_get_symbol (name, NULL, &sym))
1114 return FAILURE;
1116 /* Start updating the symbol table. Add basic type attribute if present. */
1117 if (current_ts.type != BT_UNKNOWN
1118 && (sym->attr.implicit_type == 0
1119 || !gfc_compare_types (&sym->ts, &current_ts))
1120 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
1121 return FAILURE;
1123 if (sym->ts.type == BT_CHARACTER)
1125 sym->ts.u.cl = cl;
1126 sym->ts.deferred = cl_deferred;
1129 /* Add dimension attribute if present. */
1130 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1131 return FAILURE;
1132 *as = NULL;
1134 /* Add attribute to symbol. The copy is so that we can reset the
1135 dimension attribute. */
1136 attr = current_attr;
1137 attr.dimension = 0;
1138 attr.codimension = 0;
1140 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1141 return FAILURE;
1143 /* Finish any work that may need to be done for the binding label,
1144 if it's a bind(c). The bind(c) attr is found before the symbol
1145 is made, and before the symbol name (for data decls), so the
1146 current_ts is holding the binding label, or nothing if the
1147 name= attr wasn't given. Therefore, test here if we're dealing
1148 with a bind(c) and make sure the binding label is set correctly. */
1149 if (sym->attr.is_bind_c == 1)
1151 if (sym->binding_label[0] == '\0')
1153 /* Set the binding label and verify that if a NAME= was specified
1154 then only one identifier was in the entity-decl-list. */
1155 if (set_binding_label (sym->binding_label, sym->name,
1156 num_idents_on_line) == FAILURE)
1157 return FAILURE;
1161 /* See if we know we're in a common block, and if it's a bind(c)
1162 common then we need to make sure we're an interoperable type. */
1163 if (sym->attr.in_common == 1)
1165 /* Test the common block object. */
1166 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1167 && sym->ts.is_c_interop != 1)
1169 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1170 "must be declared with a C interoperable "
1171 "kind since common block '%s' is BIND(C)",
1172 sym->name, sym->common_block->name,
1173 sym->common_block->name);
1174 gfc_clear_error ();
1178 sym->attr.implied_index = 0;
1180 if (sym->ts.type == BT_CLASS
1181 && (sym->attr.class_ok = sym->attr.dummy || sym->attr.pointer
1182 || sym->attr.allocatable))
1183 gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1185 return SUCCESS;
1189 /* Set character constant to the given length. The constant will be padded or
1190 truncated. If we're inside an array constructor without a typespec, we
1191 additionally check that all elements have the same length; check_len -1
1192 means no checking. */
1194 void
1195 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1197 gfc_char_t *s;
1198 int slen;
1200 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1201 gcc_assert (expr->ts.type == BT_CHARACTER);
1203 slen = expr->value.character.length;
1204 if (len != slen)
1206 s = gfc_get_wide_string (len + 1);
1207 memcpy (s, expr->value.character.string,
1208 MIN (len, slen) * sizeof (gfc_char_t));
1209 if (len > slen)
1210 gfc_wide_memset (&s[slen], ' ', len - slen);
1212 if (gfc_option.warn_character_truncation && slen > len)
1213 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1214 "(%d/%d)", &expr->where, slen, len);
1216 /* Apply the standard by 'hand' otherwise it gets cleared for
1217 initializers. */
1218 if (check_len != -1 && slen != check_len
1219 && !(gfc_option.allow_std & GFC_STD_GNU))
1220 gfc_error_now ("The CHARACTER elements of the array constructor "
1221 "at %L must have the same length (%d/%d)",
1222 &expr->where, slen, check_len);
1224 s[len] = '\0';
1225 gfc_free (expr->value.character.string);
1226 expr->value.character.string = s;
1227 expr->value.character.length = len;
1232 /* Function to create and update the enumerator history
1233 using the information passed as arguments.
1234 Pointer "max_enum" is also updated, to point to
1235 enum history node containing largest initializer.
1237 SYM points to the symbol node of enumerator.
1238 INIT points to its enumerator value. */
1240 static void
1241 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1243 enumerator_history *new_enum_history;
1244 gcc_assert (sym != NULL && init != NULL);
1246 new_enum_history = XCNEW (enumerator_history);
1248 new_enum_history->sym = sym;
1249 new_enum_history->initializer = init;
1250 new_enum_history->next = NULL;
1252 if (enum_history == NULL)
1254 enum_history = new_enum_history;
1255 max_enum = enum_history;
1257 else
1259 new_enum_history->next = enum_history;
1260 enum_history = new_enum_history;
1262 if (mpz_cmp (max_enum->initializer->value.integer,
1263 new_enum_history->initializer->value.integer) < 0)
1264 max_enum = new_enum_history;
1269 /* Function to free enum kind history. */
1271 void
1272 gfc_free_enum_history (void)
1274 enumerator_history *current = enum_history;
1275 enumerator_history *next;
1277 while (current != NULL)
1279 next = current->next;
1280 gfc_free (current);
1281 current = next;
1283 max_enum = NULL;
1284 enum_history = NULL;
1288 /* Function called by variable_decl() that adds an initialization
1289 expression to a symbol. */
1291 static gfc_try
1292 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1294 symbol_attribute attr;
1295 gfc_symbol *sym;
1296 gfc_expr *init;
1298 init = *initp;
1299 if (find_special (name, &sym, false))
1300 return FAILURE;
1302 attr = sym->attr;
1304 /* If this symbol is confirming an implicit parameter type,
1305 then an initialization expression is not allowed. */
1306 if (attr.flavor == FL_PARAMETER
1307 && sym->value != NULL
1308 && *initp != NULL)
1310 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1311 sym->name);
1312 return FAILURE;
1315 if (init == NULL)
1317 /* An initializer is required for PARAMETER declarations. */
1318 if (attr.flavor == FL_PARAMETER)
1320 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1321 return FAILURE;
1324 else
1326 /* If a variable appears in a DATA block, it cannot have an
1327 initializer. */
1328 if (sym->attr.data)
1330 gfc_error ("Variable '%s' at %C with an initializer already "
1331 "appears in a DATA statement", sym->name);
1332 return FAILURE;
1335 /* Check if the assignment can happen. This has to be put off
1336 until later for derived type variables and procedure pointers. */
1337 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1338 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1339 && !sym->attr.proc_pointer
1340 && gfc_check_assign_symbol (sym, init) == FAILURE)
1341 return FAILURE;
1343 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1344 && init->ts.type == BT_CHARACTER)
1346 /* Update symbol character length according initializer. */
1347 if (gfc_check_assign_symbol (sym, init) == FAILURE)
1348 return FAILURE;
1350 if (sym->ts.u.cl->length == NULL)
1352 int clen;
1353 /* If there are multiple CHARACTER variables declared on the
1354 same line, we don't want them to share the same length. */
1355 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1357 if (sym->attr.flavor == FL_PARAMETER)
1359 if (init->expr_type == EXPR_CONSTANT)
1361 clen = init->value.character.length;
1362 sym->ts.u.cl->length
1363 = gfc_get_int_expr (gfc_default_integer_kind,
1364 NULL, clen);
1366 else if (init->expr_type == EXPR_ARRAY)
1368 gfc_constructor *c;
1369 c = gfc_constructor_first (init->value.constructor);
1370 clen = c->expr->value.character.length;
1371 sym->ts.u.cl->length
1372 = gfc_get_int_expr (gfc_default_integer_kind,
1373 NULL, clen);
1375 else if (init->ts.u.cl && init->ts.u.cl->length)
1376 sym->ts.u.cl->length =
1377 gfc_copy_expr (sym->value->ts.u.cl->length);
1380 /* Update initializer character length according symbol. */
1381 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1383 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1385 if (init->expr_type == EXPR_CONSTANT)
1386 gfc_set_constant_character_len (len, init, -1);
1387 else if (init->expr_type == EXPR_ARRAY)
1389 gfc_constructor *c;
1391 /* Build a new charlen to prevent simplification from
1392 deleting the length before it is resolved. */
1393 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1394 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1396 for (c = gfc_constructor_first (init->value.constructor);
1397 c; c = gfc_constructor_next (c))
1398 gfc_set_constant_character_len (len, c->expr, -1);
1403 /* If sym is implied-shape, set its upper bounds from init. */
1404 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1405 && sym->as->type == AS_IMPLIED_SHAPE)
1407 int dim;
1409 if (init->rank == 0)
1411 gfc_error ("Can't initialize implied-shape array at %L"
1412 " with scalar", &sym->declared_at);
1413 return FAILURE;
1415 gcc_assert (sym->as->rank == init->rank);
1417 /* Shape should be present, we get an initialization expression. */
1418 gcc_assert (init->shape);
1420 for (dim = 0; dim < sym->as->rank; ++dim)
1422 int k;
1423 gfc_expr* lower;
1424 gfc_expr* e;
1426 lower = sym->as->lower[dim];
1427 if (lower->expr_type != EXPR_CONSTANT)
1429 gfc_error ("Non-constant lower bound in implied-shape"
1430 " declaration at %L", &lower->where);
1431 return FAILURE;
1434 /* All dimensions must be without upper bound. */
1435 gcc_assert (!sym->as->upper[dim]);
1437 k = lower->ts.kind;
1438 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1439 mpz_add (e->value.integer,
1440 lower->value.integer, init->shape[dim]);
1441 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1442 sym->as->upper[dim] = e;
1445 sym->as->type = AS_EXPLICIT;
1448 /* Need to check if the expression we initialized this
1449 to was one of the iso_c_binding named constants. If so,
1450 and we're a parameter (constant), let it be iso_c.
1451 For example:
1452 integer(c_int), parameter :: my_int = c_int
1453 integer(my_int) :: my_int_2
1454 If we mark my_int as iso_c (since we can see it's value
1455 is equal to one of the named constants), then my_int_2
1456 will be considered C interoperable. */
1457 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1459 sym->ts.is_iso_c |= init->ts.is_iso_c;
1460 sym->ts.is_c_interop |= init->ts.is_c_interop;
1461 /* attr bits needed for module files. */
1462 sym->attr.is_iso_c |= init->ts.is_iso_c;
1463 sym->attr.is_c_interop |= init->ts.is_c_interop;
1464 if (init->ts.is_iso_c)
1465 sym->ts.f90_type = init->ts.f90_type;
1468 /* Add initializer. Make sure we keep the ranks sane. */
1469 if (sym->attr.dimension && init->rank == 0)
1471 mpz_t size;
1472 gfc_expr *array;
1473 int n;
1474 if (sym->attr.flavor == FL_PARAMETER
1475 && init->expr_type == EXPR_CONSTANT
1476 && spec_size (sym->as, &size) == SUCCESS
1477 && mpz_cmp_si (size, 0) > 0)
1479 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1480 &init->where);
1481 for (n = 0; n < (int)mpz_get_si (size); n++)
1482 gfc_constructor_append_expr (&array->value.constructor,
1483 n == 0
1484 ? init
1485 : gfc_copy_expr (init),
1486 &init->where);
1488 array->shape = gfc_get_shape (sym->as->rank);
1489 for (n = 0; n < sym->as->rank; n++)
1490 spec_dimen_size (sym->as, n, &array->shape[n]);
1492 init = array;
1493 mpz_clear (size);
1495 init->rank = sym->as->rank;
1498 sym->value = init;
1499 if (sym->attr.save == SAVE_NONE)
1500 sym->attr.save = SAVE_IMPLICIT;
1501 *initp = NULL;
1504 return SUCCESS;
1508 /* Function called by variable_decl() that adds a name to a structure
1509 being built. */
1511 static gfc_try
1512 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1513 gfc_array_spec **as)
1515 gfc_component *c;
1516 gfc_try t = SUCCESS;
1518 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1519 constructing, it must have the pointer attribute. */
1520 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1521 && current_ts.u.derived == gfc_current_block ()
1522 && current_attr.pointer == 0)
1524 gfc_error ("Component at %C must have the POINTER attribute");
1525 return FAILURE;
1528 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1530 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1532 gfc_error ("Array component of structure at %C must have explicit "
1533 "or deferred shape");
1534 return FAILURE;
1538 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1539 return FAILURE;
1541 c->ts = current_ts;
1542 if (c->ts.type == BT_CHARACTER)
1543 c->ts.u.cl = cl;
1544 c->attr = current_attr;
1546 c->initializer = *init;
1547 *init = NULL;
1549 c->as = *as;
1550 if (c->as != NULL)
1552 if (c->as->corank)
1553 c->attr.codimension = 1;
1554 if (c->as->rank)
1555 c->attr.dimension = 1;
1557 *as = NULL;
1559 /* Should this ever get more complicated, combine with similar section
1560 in add_init_expr_to_sym into a separate function. */
1561 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer && c->ts.u.cl
1562 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1564 int len;
1566 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1567 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1568 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1570 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1572 if (c->initializer->expr_type == EXPR_CONSTANT)
1573 gfc_set_constant_character_len (len, c->initializer, -1);
1574 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1575 c->initializer->ts.u.cl->length->value.integer))
1577 gfc_constructor *ctor;
1578 ctor = gfc_constructor_first (c->initializer->value.constructor);
1580 if (ctor)
1582 int first_len;
1583 bool has_ts = (c->initializer->ts.u.cl
1584 && c->initializer->ts.u.cl->length_from_typespec);
1586 /* Remember the length of the first element for checking
1587 that all elements *in the constructor* have the same
1588 length. This need not be the length of the LHS! */
1589 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1590 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1591 first_len = ctor->expr->value.character.length;
1593 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1594 if (ctor->expr->expr_type == EXPR_CONSTANT)
1596 gfc_set_constant_character_len (len, ctor->expr,
1597 has_ts ? -1 : first_len);
1598 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1604 /* Check array components. */
1605 if (!c->attr.dimension)
1606 goto scalar;
1608 if (c->attr.pointer)
1610 if (c->as->type != AS_DEFERRED)
1612 gfc_error ("Pointer array component of structure at %C must have a "
1613 "deferred shape");
1614 t = FAILURE;
1617 else if (c->attr.allocatable)
1619 if (c->as->type != AS_DEFERRED)
1621 gfc_error ("Allocatable component of structure at %C must have a "
1622 "deferred shape");
1623 t = FAILURE;
1626 else
1628 if (c->as->type != AS_EXPLICIT)
1630 gfc_error ("Array component of structure at %C must have an "
1631 "explicit shape");
1632 t = FAILURE;
1636 scalar:
1637 if (c->ts.type == BT_CLASS)
1639 bool delayed = (gfc_state_stack->sym == c->ts.u.derived)
1640 || (!c->ts.u.derived->components
1641 && !c->ts.u.derived->attr.zero_comp);
1642 gfc_build_class_symbol (&c->ts, &c->attr, &c->as, delayed);
1646 return t;
1650 /* Match a 'NULL()', and possibly take care of some side effects. */
1652 match
1653 gfc_match_null (gfc_expr **result)
1655 gfc_symbol *sym;
1656 match m;
1658 m = gfc_match (" null ( )");
1659 if (m != MATCH_YES)
1660 return m;
1662 /* The NULL symbol now has to be/become an intrinsic function. */
1663 if (gfc_get_symbol ("null", NULL, &sym))
1665 gfc_error ("NULL() initialization at %C is ambiguous");
1666 return MATCH_ERROR;
1669 gfc_intrinsic_symbol (sym);
1671 if (sym->attr.proc != PROC_INTRINSIC
1672 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1673 sym->name, NULL) == FAILURE
1674 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1675 return MATCH_ERROR;
1677 *result = gfc_get_null_expr (&gfc_current_locus);
1679 return MATCH_YES;
1683 /* Match the initialization expr for a data pointer or procedure pointer. */
1685 static match
1686 match_pointer_init (gfc_expr **init, int procptr)
1688 match m;
1690 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1692 gfc_error ("Initialization of pointer at %C is not allowed in "
1693 "a PURE procedure");
1694 return MATCH_ERROR;
1697 /* Match NULL() initilization. */
1698 m = gfc_match_null (init);
1699 if (m != MATCH_NO)
1700 return m;
1702 /* Match non-NULL initialization. */
1703 gfc_matching_ptr_assignment = !procptr;
1704 gfc_matching_procptr_assignment = procptr;
1705 m = gfc_match_rvalue (init);
1706 gfc_matching_ptr_assignment = 0;
1707 gfc_matching_procptr_assignment = 0;
1708 if (m == MATCH_ERROR)
1709 return MATCH_ERROR;
1710 else if (m == MATCH_NO)
1712 gfc_error ("Error in pointer initialization at %C");
1713 return MATCH_ERROR;
1716 if (!procptr)
1717 gfc_resolve_expr (*init);
1719 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: non-NULL pointer "
1720 "initialization at %C") == FAILURE)
1721 return MATCH_ERROR;
1723 return MATCH_YES;
1727 /* Match a variable name with an optional initializer. When this
1728 subroutine is called, a variable is expected to be parsed next.
1729 Depending on what is happening at the moment, updates either the
1730 symbol table or the current interface. */
1732 static match
1733 variable_decl (int elem)
1735 char name[GFC_MAX_SYMBOL_LEN + 1];
1736 gfc_expr *initializer, *char_len;
1737 gfc_array_spec *as;
1738 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1739 gfc_charlen *cl;
1740 bool cl_deferred;
1741 locus var_locus;
1742 match m;
1743 gfc_try t;
1744 gfc_symbol *sym;
1746 initializer = NULL;
1747 as = NULL;
1748 cp_as = NULL;
1750 /* When we get here, we've just matched a list of attributes and
1751 maybe a type and a double colon. The next thing we expect to see
1752 is the name of the symbol. */
1753 m = gfc_match_name (name);
1754 if (m != MATCH_YES)
1755 goto cleanup;
1757 var_locus = gfc_current_locus;
1759 /* Now we could see the optional array spec. or character length. */
1760 m = gfc_match_array_spec (&as, true, true);
1761 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1762 cp_as = gfc_copy_array_spec (as);
1763 else if (m == MATCH_ERROR)
1764 goto cleanup;
1766 if (m == MATCH_NO)
1767 as = gfc_copy_array_spec (current_as);
1768 else if (current_as)
1769 merge_array_spec (current_as, as, true);
1771 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1772 determine (and check) whether it can be implied-shape. If it
1773 was parsed as assumed-size, change it because PARAMETERs can not
1774 be assumed-size. */
1775 if (as)
1777 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1779 m = MATCH_ERROR;
1780 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1781 name, &var_locus);
1782 goto cleanup;
1785 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1786 && current_attr.flavor == FL_PARAMETER)
1787 as->type = AS_IMPLIED_SHAPE;
1789 if (as->type == AS_IMPLIED_SHAPE
1790 && gfc_notify_std (GFC_STD_F2008,
1791 "Fortran 2008: Implied-shape array at %L",
1792 &var_locus) == FAILURE)
1794 m = MATCH_ERROR;
1795 goto cleanup;
1799 char_len = NULL;
1800 cl = NULL;
1801 cl_deferred = false;
1803 if (current_ts.type == BT_CHARACTER)
1805 switch (match_char_length (&char_len, &cl_deferred))
1807 case MATCH_YES:
1808 cl = gfc_new_charlen (gfc_current_ns, NULL);
1810 cl->length = char_len;
1811 break;
1813 /* Non-constant lengths need to be copied after the first
1814 element. Also copy assumed lengths. */
1815 case MATCH_NO:
1816 if (elem > 1
1817 && (current_ts.u.cl->length == NULL
1818 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1820 cl = gfc_new_charlen (gfc_current_ns, NULL);
1821 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1823 else
1824 cl = current_ts.u.cl;
1826 cl_deferred = current_ts.deferred;
1828 break;
1830 case MATCH_ERROR:
1831 goto cleanup;
1835 /* If this symbol has already shown up in a Cray Pointer declaration,
1836 then we want to set the type & bail out. */
1837 if (gfc_option.flag_cray_pointer)
1839 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1840 if (sym != NULL && sym->attr.cray_pointee)
1842 sym->ts.type = current_ts.type;
1843 sym->ts.kind = current_ts.kind;
1844 sym->ts.u.cl = cl;
1845 sym->ts.u.derived = current_ts.u.derived;
1846 sym->ts.is_c_interop = current_ts.is_c_interop;
1847 sym->ts.is_iso_c = current_ts.is_iso_c;
1848 m = MATCH_YES;
1850 /* Check to see if we have an array specification. */
1851 if (cp_as != NULL)
1853 if (sym->as != NULL)
1855 gfc_error ("Duplicate array spec for Cray pointee at %C");
1856 gfc_free_array_spec (cp_as);
1857 m = MATCH_ERROR;
1858 goto cleanup;
1860 else
1862 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1863 gfc_internal_error ("Couldn't set pointee array spec.");
1865 /* Fix the array spec. */
1866 m = gfc_mod_pointee_as (sym->as);
1867 if (m == MATCH_ERROR)
1868 goto cleanup;
1871 goto cleanup;
1873 else
1875 gfc_free_array_spec (cp_as);
1879 /* Procedure pointer as function result. */
1880 if (gfc_current_state () == COMP_FUNCTION
1881 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1882 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1883 strcpy (name, "ppr@");
1885 if (gfc_current_state () == COMP_FUNCTION
1886 && strcmp (name, gfc_current_block ()->name) == 0
1887 && gfc_current_block ()->result
1888 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1889 strcpy (name, "ppr@");
1891 /* OK, we've successfully matched the declaration. Now put the
1892 symbol in the current namespace, because it might be used in the
1893 optional initialization expression for this symbol, e.g. this is
1894 perfectly legal:
1896 integer, parameter :: i = huge(i)
1898 This is only true for parameters or variables of a basic type.
1899 For components of derived types, it is not true, so we don't
1900 create a symbol for those yet. If we fail to create the symbol,
1901 bail out. */
1902 if (gfc_current_state () != COMP_DERIVED
1903 && build_sym (name, cl, cl_deferred, &as, &var_locus) == FAILURE)
1905 m = MATCH_ERROR;
1906 goto cleanup;
1909 /* An interface body specifies all of the procedure's
1910 characteristics and these shall be consistent with those
1911 specified in the procedure definition, except that the interface
1912 may specify a procedure that is not pure if the procedure is
1913 defined to be pure(12.3.2). */
1914 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1915 && gfc_current_ns->proc_name
1916 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1917 && current_ts.u.derived->ns != gfc_current_ns)
1919 gfc_symtree *st;
1920 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.u.derived->name);
1921 if (!(current_ts.u.derived->attr.imported
1922 && st != NULL
1923 && st->n.sym == current_ts.u.derived)
1924 && !gfc_current_ns->has_import_set)
1926 gfc_error ("the type of '%s' at %C has not been declared within the "
1927 "interface", name);
1928 m = MATCH_ERROR;
1929 goto cleanup;
1933 /* In functions that have a RESULT variable defined, the function
1934 name always refers to function calls. Therefore, the name is
1935 not allowed to appear in specification statements. */
1936 if (gfc_current_state () == COMP_FUNCTION
1937 && gfc_current_block () != NULL
1938 && gfc_current_block ()->result != NULL
1939 && gfc_current_block ()->result != gfc_current_block ()
1940 && strcmp (gfc_current_block ()->name, name) == 0)
1942 gfc_error ("Function name '%s' not allowed at %C", name);
1943 m = MATCH_ERROR;
1944 goto cleanup;
1947 /* We allow old-style initializations of the form
1948 integer i /2/, j(4) /3*3, 1/
1949 (if no colon has been seen). These are different from data
1950 statements in that initializers are only allowed to apply to the
1951 variable immediately preceding, i.e.
1952 integer i, j /1, 2/
1953 is not allowed. Therefore we have to do some work manually, that
1954 could otherwise be left to the matchers for DATA statements. */
1956 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1958 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1959 "initialization at %C") == FAILURE)
1960 return MATCH_ERROR;
1962 return match_old_style_init (name);
1965 /* The double colon must be present in order to have initializers.
1966 Otherwise the statement is ambiguous with an assignment statement. */
1967 if (colon_seen)
1969 if (gfc_match (" =>") == MATCH_YES)
1971 if (!current_attr.pointer)
1973 gfc_error ("Initialization at %C isn't for a pointer variable");
1974 m = MATCH_ERROR;
1975 goto cleanup;
1978 m = match_pointer_init (&initializer, 0);
1979 if (m != MATCH_YES)
1980 goto cleanup;
1982 else if (gfc_match_char ('=') == MATCH_YES)
1984 if (current_attr.pointer)
1986 gfc_error ("Pointer initialization at %C requires '=>', "
1987 "not '='");
1988 m = MATCH_ERROR;
1989 goto cleanup;
1992 m = gfc_match_init_expr (&initializer);
1993 if (m == MATCH_NO)
1995 gfc_error ("Expected an initialization expression at %C");
1996 m = MATCH_ERROR;
1999 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
2000 && gfc_state_stack->state != COMP_DERIVED)
2002 gfc_error ("Initialization of variable at %C is not allowed in "
2003 "a PURE procedure");
2004 m = MATCH_ERROR;
2007 if (m != MATCH_YES)
2008 goto cleanup;
2012 if (initializer != NULL && current_attr.allocatable
2013 && gfc_current_state () == COMP_DERIVED)
2015 gfc_error ("Initialization of allocatable component at %C is not "
2016 "allowed");
2017 m = MATCH_ERROR;
2018 goto cleanup;
2021 /* Add the initializer. Note that it is fine if initializer is
2022 NULL here, because we sometimes also need to check if a
2023 declaration *must* have an initialization expression. */
2024 if (gfc_current_state () != COMP_DERIVED)
2025 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2026 else
2028 if (current_ts.type == BT_DERIVED
2029 && !current_attr.pointer && !initializer)
2030 initializer = gfc_default_initializer (&current_ts);
2031 t = build_struct (name, cl, &initializer, &as);
2034 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
2036 cleanup:
2037 /* Free stuff up and return. */
2038 gfc_free_expr (initializer);
2039 gfc_free_array_spec (as);
2041 return m;
2045 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2046 This assumes that the byte size is equal to the kind number for
2047 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2049 match
2050 gfc_match_old_kind_spec (gfc_typespec *ts)
2052 match m;
2053 int original_kind;
2055 if (gfc_match_char ('*') != MATCH_YES)
2056 return MATCH_NO;
2058 m = gfc_match_small_literal_int (&ts->kind, NULL);
2059 if (m != MATCH_YES)
2060 return MATCH_ERROR;
2062 original_kind = ts->kind;
2064 /* Massage the kind numbers for complex types. */
2065 if (ts->type == BT_COMPLEX)
2067 if (ts->kind % 2)
2069 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2070 gfc_basic_typename (ts->type), original_kind);
2071 return MATCH_ERROR;
2073 ts->kind /= 2;
2076 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2078 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2079 gfc_basic_typename (ts->type), original_kind);
2080 return MATCH_ERROR;
2083 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2084 gfc_basic_typename (ts->type), original_kind) == FAILURE)
2085 return MATCH_ERROR;
2087 return MATCH_YES;
2091 /* Match a kind specification. Since kinds are generally optional, we
2092 usually return MATCH_NO if something goes wrong. If a "kind="
2093 string is found, then we know we have an error. */
2095 match
2096 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2098 locus where, loc;
2099 gfc_expr *e;
2100 match m, n;
2101 char c;
2102 const char *msg;
2104 m = MATCH_NO;
2105 n = MATCH_YES;
2106 e = NULL;
2108 where = loc = gfc_current_locus;
2110 if (kind_expr_only)
2111 goto kind_expr;
2113 if (gfc_match_char ('(') == MATCH_NO)
2114 return MATCH_NO;
2116 /* Also gobbles optional text. */
2117 if (gfc_match (" kind = ") == MATCH_YES)
2118 m = MATCH_ERROR;
2120 loc = gfc_current_locus;
2122 kind_expr:
2123 n = gfc_match_init_expr (&e);
2125 if (n != MATCH_YES)
2127 if (gfc_matching_function)
2129 /* The function kind expression might include use associated or
2130 imported parameters and try again after the specification
2131 expressions..... */
2132 if (gfc_match_char (')') != MATCH_YES)
2134 gfc_error ("Missing right parenthesis at %C");
2135 m = MATCH_ERROR;
2136 goto no_match;
2139 gfc_free_expr (e);
2140 gfc_undo_symbols ();
2141 return MATCH_YES;
2143 else
2145 /* ....or else, the match is real. */
2146 if (n == MATCH_NO)
2147 gfc_error ("Expected initialization expression at %C");
2148 if (n != MATCH_YES)
2149 return MATCH_ERROR;
2153 if (e->rank != 0)
2155 gfc_error ("Expected scalar initialization expression at %C");
2156 m = MATCH_ERROR;
2157 goto no_match;
2160 msg = gfc_extract_int (e, &ts->kind);
2162 if (msg != NULL)
2164 gfc_error (msg);
2165 m = MATCH_ERROR;
2166 goto no_match;
2169 /* Before throwing away the expression, let's see if we had a
2170 C interoperable kind (and store the fact). */
2171 if (e->ts.is_c_interop == 1)
2173 /* Mark this as c interoperable if being declared with one
2174 of the named constants from iso_c_binding. */
2175 ts->is_c_interop = e->ts.is_iso_c;
2176 ts->f90_type = e->ts.f90_type;
2179 gfc_free_expr (e);
2180 e = NULL;
2182 /* Ignore errors to this point, if we've gotten here. This means
2183 we ignore the m=MATCH_ERROR from above. */
2184 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2186 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2187 gfc_basic_typename (ts->type));
2188 gfc_current_locus = where;
2189 return MATCH_ERROR;
2192 /* Warn if, e.g., c_int is used for a REAL variable, but not
2193 if, e.g., c_double is used for COMPLEX as the standard
2194 explicitly says that the kind type parameter for complex and real
2195 variable is the same, i.e. c_float == c_float_complex. */
2196 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2197 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2198 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2199 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2200 "is %s", gfc_basic_typename (ts->f90_type), &where,
2201 gfc_basic_typename (ts->type));
2203 gfc_gobble_whitespace ();
2204 if ((c = gfc_next_ascii_char ()) != ')'
2205 && (ts->type != BT_CHARACTER || c != ','))
2207 if (ts->type == BT_CHARACTER)
2208 gfc_error ("Missing right parenthesis or comma at %C");
2209 else
2210 gfc_error ("Missing right parenthesis at %C");
2211 m = MATCH_ERROR;
2213 else
2214 /* All tests passed. */
2215 m = MATCH_YES;
2217 if(m == MATCH_ERROR)
2218 gfc_current_locus = where;
2220 /* Return what we know from the test(s). */
2221 return m;
2223 no_match:
2224 gfc_free_expr (e);
2225 gfc_current_locus = where;
2226 return m;
2230 static match
2231 match_char_kind (int * kind, int * is_iso_c)
2233 locus where;
2234 gfc_expr *e;
2235 match m, n;
2236 const char *msg;
2238 m = MATCH_NO;
2239 e = NULL;
2240 where = gfc_current_locus;
2242 n = gfc_match_init_expr (&e);
2244 if (n != MATCH_YES && gfc_matching_function)
2246 /* The expression might include use-associated or imported
2247 parameters and try again after the specification
2248 expressions. */
2249 gfc_free_expr (e);
2250 gfc_undo_symbols ();
2251 return MATCH_YES;
2254 if (n == MATCH_NO)
2255 gfc_error ("Expected initialization expression at %C");
2256 if (n != MATCH_YES)
2257 return MATCH_ERROR;
2259 if (e->rank != 0)
2261 gfc_error ("Expected scalar initialization expression at %C");
2262 m = MATCH_ERROR;
2263 goto no_match;
2266 msg = gfc_extract_int (e, kind);
2267 *is_iso_c = e->ts.is_iso_c;
2268 if (msg != NULL)
2270 gfc_error (msg);
2271 m = MATCH_ERROR;
2272 goto no_match;
2275 gfc_free_expr (e);
2277 /* Ignore errors to this point, if we've gotten here. This means
2278 we ignore the m=MATCH_ERROR from above. */
2279 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2281 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2282 m = MATCH_ERROR;
2284 else
2285 /* All tests passed. */
2286 m = MATCH_YES;
2288 if (m == MATCH_ERROR)
2289 gfc_current_locus = where;
2291 /* Return what we know from the test(s). */
2292 return m;
2294 no_match:
2295 gfc_free_expr (e);
2296 gfc_current_locus = where;
2297 return m;
2301 /* Match the various kind/length specifications in a CHARACTER
2302 declaration. We don't return MATCH_NO. */
2304 match
2305 gfc_match_char_spec (gfc_typespec *ts)
2307 int kind, seen_length, is_iso_c;
2308 gfc_charlen *cl;
2309 gfc_expr *len;
2310 match m;
2311 bool deferred;
2313 len = NULL;
2314 seen_length = 0;
2315 kind = 0;
2316 is_iso_c = 0;
2317 deferred = false;
2319 /* Try the old-style specification first. */
2320 old_char_selector = 0;
2322 m = match_char_length (&len, &deferred);
2323 if (m != MATCH_NO)
2325 if (m == MATCH_YES)
2326 old_char_selector = 1;
2327 seen_length = 1;
2328 goto done;
2331 m = gfc_match_char ('(');
2332 if (m != MATCH_YES)
2334 m = MATCH_YES; /* Character without length is a single char. */
2335 goto done;
2338 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2339 if (gfc_match (" kind =") == MATCH_YES)
2341 m = match_char_kind (&kind, &is_iso_c);
2343 if (m == MATCH_ERROR)
2344 goto done;
2345 if (m == MATCH_NO)
2346 goto syntax;
2348 if (gfc_match (" , len =") == MATCH_NO)
2349 goto rparen;
2351 m = char_len_param_value (&len, &deferred);
2352 if (m == MATCH_NO)
2353 goto syntax;
2354 if (m == MATCH_ERROR)
2355 goto done;
2356 seen_length = 1;
2358 goto rparen;
2361 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2362 if (gfc_match (" len =") == MATCH_YES)
2364 m = char_len_param_value (&len, &deferred);
2365 if (m == MATCH_NO)
2366 goto syntax;
2367 if (m == MATCH_ERROR)
2368 goto done;
2369 seen_length = 1;
2371 if (gfc_match_char (')') == MATCH_YES)
2372 goto done;
2374 if (gfc_match (" , kind =") != MATCH_YES)
2375 goto syntax;
2377 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2378 goto done;
2380 goto rparen;
2383 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2384 m = char_len_param_value (&len, &deferred);
2385 if (m == MATCH_NO)
2386 goto syntax;
2387 if (m == MATCH_ERROR)
2388 goto done;
2389 seen_length = 1;
2391 m = gfc_match_char (')');
2392 if (m == MATCH_YES)
2393 goto done;
2395 if (gfc_match_char (',') != MATCH_YES)
2396 goto syntax;
2398 gfc_match (" kind ="); /* Gobble optional text. */
2400 m = match_char_kind (&kind, &is_iso_c);
2401 if (m == MATCH_ERROR)
2402 goto done;
2403 if (m == MATCH_NO)
2404 goto syntax;
2406 rparen:
2407 /* Require a right-paren at this point. */
2408 m = gfc_match_char (')');
2409 if (m == MATCH_YES)
2410 goto done;
2412 syntax:
2413 gfc_error ("Syntax error in CHARACTER declaration at %C");
2414 m = MATCH_ERROR;
2415 gfc_free_expr (len);
2416 return m;
2418 done:
2419 /* Deal with character functions after USE and IMPORT statements. */
2420 if (gfc_matching_function)
2422 gfc_free_expr (len);
2423 gfc_undo_symbols ();
2424 return MATCH_YES;
2427 if (m != MATCH_YES)
2429 gfc_free_expr (len);
2430 return m;
2433 /* Do some final massaging of the length values. */
2434 cl = gfc_new_charlen (gfc_current_ns, NULL);
2436 if (seen_length == 0)
2437 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2438 else
2439 cl->length = len;
2441 ts->u.cl = cl;
2442 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2443 ts->deferred = deferred;
2445 /* We have to know if it was a c interoperable kind so we can
2446 do accurate type checking of bind(c) procs, etc. */
2447 if (kind != 0)
2448 /* Mark this as c interoperable if being declared with one
2449 of the named constants from iso_c_binding. */
2450 ts->is_c_interop = is_iso_c;
2451 else if (len != NULL)
2452 /* Here, we might have parsed something such as: character(c_char)
2453 In this case, the parsing code above grabs the c_char when
2454 looking for the length (line 1690, roughly). it's the last
2455 testcase for parsing the kind params of a character variable.
2456 However, it's not actually the length. this seems like it
2457 could be an error.
2458 To see if the user used a C interop kind, test the expr
2459 of the so called length, and see if it's C interoperable. */
2460 ts->is_c_interop = len->ts.is_iso_c;
2462 return MATCH_YES;
2466 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2467 structure to the matched specification. This is necessary for FUNCTION and
2468 IMPLICIT statements.
2470 If implicit_flag is nonzero, then we don't check for the optional
2471 kind specification. Not doing so is needed for matching an IMPLICIT
2472 statement correctly. */
2474 match
2475 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2477 char name[GFC_MAX_SYMBOL_LEN + 1];
2478 gfc_symbol *sym;
2479 match m;
2480 char c;
2481 bool seen_deferred_kind, matched_type;
2483 /* A belt and braces check that the typespec is correctly being treated
2484 as a deferred characteristic association. */
2485 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2486 && (gfc_current_block ()->result->ts.kind == -1)
2487 && (ts->kind == -1);
2488 gfc_clear_ts (ts);
2489 if (seen_deferred_kind)
2490 ts->kind = -1;
2492 /* Clear the current binding label, in case one is given. */
2493 curr_binding_label[0] = '\0';
2495 if (gfc_match (" byte") == MATCH_YES)
2497 if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2498 == FAILURE)
2499 return MATCH_ERROR;
2501 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2503 gfc_error ("BYTE type used at %C "
2504 "is not available on the target machine");
2505 return MATCH_ERROR;
2508 ts->type = BT_INTEGER;
2509 ts->kind = 1;
2510 return MATCH_YES;
2514 m = gfc_match (" type ( %n", name);
2515 matched_type = (m == MATCH_YES);
2517 if ((matched_type && strcmp ("integer", name) == 0)
2518 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2520 ts->type = BT_INTEGER;
2521 ts->kind = gfc_default_integer_kind;
2522 goto get_kind;
2525 if ((matched_type && strcmp ("character", name) == 0)
2526 || (!matched_type && gfc_match (" character") == MATCH_YES))
2528 if (matched_type
2529 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2530 "intrinsic-type-spec at %C") == FAILURE)
2531 return MATCH_ERROR;
2533 ts->type = BT_CHARACTER;
2534 if (implicit_flag == 0)
2535 m = gfc_match_char_spec (ts);
2536 else
2537 m = MATCH_YES;
2539 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2540 m = MATCH_ERROR;
2542 return m;
2545 if ((matched_type && strcmp ("real", name) == 0)
2546 || (!matched_type && gfc_match (" real") == MATCH_YES))
2548 ts->type = BT_REAL;
2549 ts->kind = gfc_default_real_kind;
2550 goto get_kind;
2553 if ((matched_type
2554 && (strcmp ("doubleprecision", name) == 0
2555 || (strcmp ("double", name) == 0
2556 && gfc_match (" precision") == MATCH_YES)))
2557 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2559 if (matched_type
2560 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2561 "intrinsic-type-spec at %C") == FAILURE)
2562 return MATCH_ERROR;
2563 if (matched_type && gfc_match_char (')') != MATCH_YES)
2564 return MATCH_ERROR;
2566 ts->type = BT_REAL;
2567 ts->kind = gfc_default_double_kind;
2568 return MATCH_YES;
2571 if ((matched_type && strcmp ("complex", name) == 0)
2572 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2574 ts->type = BT_COMPLEX;
2575 ts->kind = gfc_default_complex_kind;
2576 goto get_kind;
2579 if ((matched_type
2580 && (strcmp ("doublecomplex", name) == 0
2581 || (strcmp ("double", name) == 0
2582 && gfc_match (" complex") == MATCH_YES)))
2583 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2585 if (gfc_notify_std (GFC_STD_GNU, "Extension: DOUBLE COMPLEX at %C")
2586 == FAILURE)
2587 return MATCH_ERROR;
2589 if (matched_type
2590 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2591 "intrinsic-type-spec at %C") == FAILURE)
2592 return MATCH_ERROR;
2594 if (matched_type && gfc_match_char (')') != MATCH_YES)
2595 return MATCH_ERROR;
2597 ts->type = BT_COMPLEX;
2598 ts->kind = gfc_default_double_kind;
2599 return MATCH_YES;
2602 if ((matched_type && strcmp ("logical", name) == 0)
2603 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2605 ts->type = BT_LOGICAL;
2606 ts->kind = gfc_default_logical_kind;
2607 goto get_kind;
2610 if (matched_type)
2611 m = gfc_match_char (')');
2613 if (m == MATCH_YES)
2614 ts->type = BT_DERIVED;
2615 else
2617 m = gfc_match (" class ( %n )", name);
2618 if (m != MATCH_YES)
2619 return m;
2620 ts->type = BT_CLASS;
2622 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2623 == FAILURE)
2624 return MATCH_ERROR;
2627 /* Defer association of the derived type until the end of the
2628 specification block. However, if the derived type can be
2629 found, add it to the typespec. */
2630 if (gfc_matching_function)
2632 ts->u.derived = NULL;
2633 if (gfc_current_state () != COMP_INTERFACE
2634 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2635 ts->u.derived = sym;
2636 return MATCH_YES;
2639 /* Search for the name but allow the components to be defined later. If
2640 type = -1, this typespec has been seen in a function declaration but
2641 the type could not be accessed at that point. */
2642 sym = NULL;
2643 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2645 gfc_error ("Type name '%s' at %C is ambiguous", name);
2646 return MATCH_ERROR;
2648 else if (ts->kind == -1)
2650 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2651 || gfc_current_ns->has_import_set;
2652 if (gfc_find_symbol (name, NULL, iface, &sym))
2654 gfc_error ("Type name '%s' at %C is ambiguous", name);
2655 return MATCH_ERROR;
2658 ts->kind = 0;
2659 if (sym == NULL)
2660 return MATCH_NO;
2663 if (sym->attr.flavor != FL_DERIVED
2664 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2665 return MATCH_ERROR;
2667 gfc_set_sym_referenced (sym);
2668 ts->u.derived = sym;
2670 return MATCH_YES;
2672 get_kind:
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;
2678 /* For all types except double, derived and character, look for an
2679 optional kind specifier. MATCH_NO is actually OK at this point. */
2680 if (implicit_flag == 1)
2682 if (matched_type && gfc_match_char (')') != MATCH_YES)
2683 return MATCH_ERROR;
2685 return MATCH_YES;
2688 if (gfc_current_form == FORM_FREE)
2690 c = gfc_peek_ascii_char ();
2691 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2692 && c != ':' && c != ',')
2694 if (matched_type && c == ')')
2696 gfc_next_ascii_char ();
2697 return MATCH_YES;
2699 return MATCH_NO;
2703 m = gfc_match_kind_spec (ts, false);
2704 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2705 m = gfc_match_old_kind_spec (ts);
2707 if (matched_type && gfc_match_char (')') != MATCH_YES)
2708 return MATCH_ERROR;
2710 /* Defer association of the KIND expression of function results
2711 until after USE and IMPORT statements. */
2712 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2713 || gfc_matching_function)
2714 return MATCH_YES;
2716 if (m == MATCH_NO)
2717 m = MATCH_YES; /* No kind specifier found. */
2719 return m;
2723 /* Match an IMPLICIT NONE statement. Actually, this statement is
2724 already matched in parse.c, or we would not end up here in the
2725 first place. So the only thing we need to check, is if there is
2726 trailing garbage. If not, the match is successful. */
2728 match
2729 gfc_match_implicit_none (void)
2731 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2735 /* Match the letter range(s) of an IMPLICIT statement. */
2737 static match
2738 match_implicit_range (void)
2740 char c, c1, c2;
2741 int inner;
2742 locus cur_loc;
2744 cur_loc = gfc_current_locus;
2746 gfc_gobble_whitespace ();
2747 c = gfc_next_ascii_char ();
2748 if (c != '(')
2750 gfc_error ("Missing character range in IMPLICIT at %C");
2751 goto bad;
2754 inner = 1;
2755 while (inner)
2757 gfc_gobble_whitespace ();
2758 c1 = gfc_next_ascii_char ();
2759 if (!ISALPHA (c1))
2760 goto bad;
2762 gfc_gobble_whitespace ();
2763 c = gfc_next_ascii_char ();
2765 switch (c)
2767 case ')':
2768 inner = 0; /* Fall through. */
2770 case ',':
2771 c2 = c1;
2772 break;
2774 case '-':
2775 gfc_gobble_whitespace ();
2776 c2 = gfc_next_ascii_char ();
2777 if (!ISALPHA (c2))
2778 goto bad;
2780 gfc_gobble_whitespace ();
2781 c = gfc_next_ascii_char ();
2783 if ((c != ',') && (c != ')'))
2784 goto bad;
2785 if (c == ')')
2786 inner = 0;
2788 break;
2790 default:
2791 goto bad;
2794 if (c1 > c2)
2796 gfc_error ("Letters must be in alphabetic order in "
2797 "IMPLICIT statement at %C");
2798 goto bad;
2801 /* See if we can add the newly matched range to the pending
2802 implicits from this IMPLICIT statement. We do not check for
2803 conflicts with whatever earlier IMPLICIT statements may have
2804 set. This is done when we've successfully finished matching
2805 the current one. */
2806 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2807 goto bad;
2810 return MATCH_YES;
2812 bad:
2813 gfc_syntax_error (ST_IMPLICIT);
2815 gfc_current_locus = cur_loc;
2816 return MATCH_ERROR;
2820 /* Match an IMPLICIT statement, storing the types for
2821 gfc_set_implicit() if the statement is accepted by the parser.
2822 There is a strange looking, but legal syntactic construction
2823 possible. It looks like:
2825 IMPLICIT INTEGER (a-b) (c-d)
2827 This is legal if "a-b" is a constant expression that happens to
2828 equal one of the legal kinds for integers. The real problem
2829 happens with an implicit specification that looks like:
2831 IMPLICIT INTEGER (a-b)
2833 In this case, a typespec matcher that is "greedy" (as most of the
2834 matchers are) gobbles the character range as a kindspec, leaving
2835 nothing left. We therefore have to go a bit more slowly in the
2836 matching process by inhibiting the kindspec checking during
2837 typespec matching and checking for a kind later. */
2839 match
2840 gfc_match_implicit (void)
2842 gfc_typespec ts;
2843 locus cur_loc;
2844 char c;
2845 match m;
2847 gfc_clear_ts (&ts);
2849 /* We don't allow empty implicit statements. */
2850 if (gfc_match_eos () == MATCH_YES)
2852 gfc_error ("Empty IMPLICIT statement at %C");
2853 return MATCH_ERROR;
2858 /* First cleanup. */
2859 gfc_clear_new_implicit ();
2861 /* A basic type is mandatory here. */
2862 m = gfc_match_decl_type_spec (&ts, 1);
2863 if (m == MATCH_ERROR)
2864 goto error;
2865 if (m == MATCH_NO)
2866 goto syntax;
2868 cur_loc = gfc_current_locus;
2869 m = match_implicit_range ();
2871 if (m == MATCH_YES)
2873 /* We may have <TYPE> (<RANGE>). */
2874 gfc_gobble_whitespace ();
2875 c = gfc_next_ascii_char ();
2876 if ((c == '\n') || (c == ','))
2878 /* Check for CHARACTER with no length parameter. */
2879 if (ts.type == BT_CHARACTER && !ts.u.cl)
2881 ts.kind = gfc_default_character_kind;
2882 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2883 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
2884 NULL, 1);
2887 /* Record the Successful match. */
2888 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2889 return MATCH_ERROR;
2890 continue;
2893 gfc_current_locus = cur_loc;
2896 /* Discard the (incorrectly) matched range. */
2897 gfc_clear_new_implicit ();
2899 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2900 if (ts.type == BT_CHARACTER)
2901 m = gfc_match_char_spec (&ts);
2902 else
2904 m = gfc_match_kind_spec (&ts, false);
2905 if (m == MATCH_NO)
2907 m = gfc_match_old_kind_spec (&ts);
2908 if (m == MATCH_ERROR)
2909 goto error;
2910 if (m == MATCH_NO)
2911 goto syntax;
2914 if (m == MATCH_ERROR)
2915 goto error;
2917 m = match_implicit_range ();
2918 if (m == MATCH_ERROR)
2919 goto error;
2920 if (m == MATCH_NO)
2921 goto syntax;
2923 gfc_gobble_whitespace ();
2924 c = gfc_next_ascii_char ();
2925 if ((c != '\n') && (c != ','))
2926 goto syntax;
2928 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2929 return MATCH_ERROR;
2931 while (c == ',');
2933 return MATCH_YES;
2935 syntax:
2936 gfc_syntax_error (ST_IMPLICIT);
2938 error:
2939 return MATCH_ERROR;
2943 match
2944 gfc_match_import (void)
2946 char name[GFC_MAX_SYMBOL_LEN + 1];
2947 match m;
2948 gfc_symbol *sym;
2949 gfc_symtree *st;
2951 if (gfc_current_ns->proc_name == NULL
2952 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2954 gfc_error ("IMPORT statement at %C only permitted in "
2955 "an INTERFACE body");
2956 return MATCH_ERROR;
2959 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2960 == FAILURE)
2961 return MATCH_ERROR;
2963 if (gfc_match_eos () == MATCH_YES)
2965 /* All host variables should be imported. */
2966 gfc_current_ns->has_import_set = 1;
2967 return MATCH_YES;
2970 if (gfc_match (" ::") == MATCH_YES)
2972 if (gfc_match_eos () == MATCH_YES)
2974 gfc_error ("Expecting list of named entities at %C");
2975 return MATCH_ERROR;
2979 for(;;)
2981 m = gfc_match (" %n", name);
2982 switch (m)
2984 case MATCH_YES:
2985 if (gfc_current_ns->parent != NULL
2986 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2988 gfc_error ("Type name '%s' at %C is ambiguous", name);
2989 return MATCH_ERROR;
2991 else if (gfc_current_ns->proc_name->ns->parent != NULL
2992 && gfc_find_symbol (name,
2993 gfc_current_ns->proc_name->ns->parent,
2994 1, &sym))
2996 gfc_error ("Type name '%s' at %C is ambiguous", name);
2997 return MATCH_ERROR;
3000 if (sym == NULL)
3002 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
3003 "at %C - does not exist.", name);
3004 return MATCH_ERROR;
3007 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
3009 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3010 "at %C.", name);
3011 goto next_item;
3014 st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
3015 st->n.sym = sym;
3016 sym->refs++;
3017 sym->attr.imported = 1;
3019 goto next_item;
3021 case MATCH_NO:
3022 break;
3024 case MATCH_ERROR:
3025 return MATCH_ERROR;
3028 next_item:
3029 if (gfc_match_eos () == MATCH_YES)
3030 break;
3031 if (gfc_match_char (',') != MATCH_YES)
3032 goto syntax;
3035 return MATCH_YES;
3037 syntax:
3038 gfc_error ("Syntax error in IMPORT statement at %C");
3039 return MATCH_ERROR;
3043 /* A minimal implementation of gfc_match without whitespace, escape
3044 characters or variable arguments. Returns true if the next
3045 characters match the TARGET template exactly. */
3047 static bool
3048 match_string_p (const char *target)
3050 const char *p;
3052 for (p = target; *p; p++)
3053 if ((char) gfc_next_ascii_char () != *p)
3054 return false;
3055 return true;
3058 /* Matches an attribute specification including array specs. If
3059 successful, leaves the variables current_attr and current_as
3060 holding the specification. Also sets the colon_seen variable for
3061 later use by matchers associated with initializations.
3063 This subroutine is a little tricky in the sense that we don't know
3064 if we really have an attr-spec until we hit the double colon.
3065 Until that time, we can only return MATCH_NO. This forces us to
3066 check for duplicate specification at this level. */
3068 static match
3069 match_attr_spec (void)
3071 /* Modifiers that can exist in a type statement. */
3072 typedef enum
3073 { GFC_DECL_BEGIN = 0,
3074 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3075 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3076 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3077 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3078 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3079 DECL_NONE, GFC_DECL_END /* Sentinel */
3081 decl_types;
3083 /* GFC_DECL_END is the sentinel, index starts at 0. */
3084 #define NUM_DECL GFC_DECL_END
3086 locus start, seen_at[NUM_DECL];
3087 int seen[NUM_DECL];
3088 unsigned int d;
3089 const char *attr;
3090 match m;
3091 gfc_try t;
3093 gfc_clear_attr (&current_attr);
3094 start = gfc_current_locus;
3096 current_as = NULL;
3097 colon_seen = 0;
3099 /* See if we get all of the keywords up to the final double colon. */
3100 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3101 seen[d] = 0;
3103 for (;;)
3105 char ch;
3107 d = DECL_NONE;
3108 gfc_gobble_whitespace ();
3110 ch = gfc_next_ascii_char ();
3111 if (ch == ':')
3113 /* This is the successful exit condition for the loop. */
3114 if (gfc_next_ascii_char () == ':')
3115 break;
3117 else if (ch == ',')
3119 gfc_gobble_whitespace ();
3120 switch (gfc_peek_ascii_char ())
3122 case 'a':
3123 gfc_next_ascii_char ();
3124 switch (gfc_next_ascii_char ())
3126 case 'l':
3127 if (match_string_p ("locatable"))
3129 /* Matched "allocatable". */
3130 d = DECL_ALLOCATABLE;
3132 break;
3134 case 's':
3135 if (match_string_p ("ynchronous"))
3137 /* Matched "asynchronous". */
3138 d = DECL_ASYNCHRONOUS;
3140 break;
3142 break;
3144 case 'b':
3145 /* Try and match the bind(c). */
3146 m = gfc_match_bind_c (NULL, true);
3147 if (m == MATCH_YES)
3148 d = DECL_IS_BIND_C;
3149 else if (m == MATCH_ERROR)
3150 goto cleanup;
3151 break;
3153 case 'c':
3154 gfc_next_ascii_char ();
3155 if ('o' != gfc_next_ascii_char ())
3156 break;
3157 switch (gfc_next_ascii_char ())
3159 case 'd':
3160 if (match_string_p ("imension"))
3162 d = DECL_CODIMENSION;
3163 break;
3165 case 'n':
3166 if (match_string_p ("tiguous"))
3168 d = DECL_CONTIGUOUS;
3169 break;
3172 break;
3174 case 'd':
3175 if (match_string_p ("dimension"))
3176 d = DECL_DIMENSION;
3177 break;
3179 case 'e':
3180 if (match_string_p ("external"))
3181 d = DECL_EXTERNAL;
3182 break;
3184 case 'i':
3185 if (match_string_p ("int"))
3187 ch = gfc_next_ascii_char ();
3188 if (ch == 'e')
3190 if (match_string_p ("nt"))
3192 /* Matched "intent". */
3193 /* TODO: Call match_intent_spec from here. */
3194 if (gfc_match (" ( in out )") == MATCH_YES)
3195 d = DECL_INOUT;
3196 else if (gfc_match (" ( in )") == MATCH_YES)
3197 d = DECL_IN;
3198 else if (gfc_match (" ( out )") == MATCH_YES)
3199 d = DECL_OUT;
3202 else if (ch == 'r')
3204 if (match_string_p ("insic"))
3206 /* Matched "intrinsic". */
3207 d = DECL_INTRINSIC;
3211 break;
3213 case 'o':
3214 if (match_string_p ("optional"))
3215 d = DECL_OPTIONAL;
3216 break;
3218 case 'p':
3219 gfc_next_ascii_char ();
3220 switch (gfc_next_ascii_char ())
3222 case 'a':
3223 if (match_string_p ("rameter"))
3225 /* Matched "parameter". */
3226 d = DECL_PARAMETER;
3228 break;
3230 case 'o':
3231 if (match_string_p ("inter"))
3233 /* Matched "pointer". */
3234 d = DECL_POINTER;
3236 break;
3238 case 'r':
3239 ch = gfc_next_ascii_char ();
3240 if (ch == 'i')
3242 if (match_string_p ("vate"))
3244 /* Matched "private". */
3245 d = DECL_PRIVATE;
3248 else if (ch == 'o')
3250 if (match_string_p ("tected"))
3252 /* Matched "protected". */
3253 d = DECL_PROTECTED;
3256 break;
3258 case 'u':
3259 if (match_string_p ("blic"))
3261 /* Matched "public". */
3262 d = DECL_PUBLIC;
3264 break;
3266 break;
3268 case 's':
3269 if (match_string_p ("save"))
3270 d = DECL_SAVE;
3271 break;
3273 case 't':
3274 if (match_string_p ("target"))
3275 d = DECL_TARGET;
3276 break;
3278 case 'v':
3279 gfc_next_ascii_char ();
3280 ch = gfc_next_ascii_char ();
3281 if (ch == 'a')
3283 if (match_string_p ("lue"))
3285 /* Matched "value". */
3286 d = DECL_VALUE;
3289 else if (ch == 'o')
3291 if (match_string_p ("latile"))
3293 /* Matched "volatile". */
3294 d = DECL_VOLATILE;
3297 break;
3301 /* No double colon and no recognizable decl_type, so assume that
3302 we've been looking at something else the whole time. */
3303 if (d == DECL_NONE)
3305 m = MATCH_NO;
3306 goto cleanup;
3309 /* Check to make sure any parens are paired up correctly. */
3310 if (gfc_match_parens () == MATCH_ERROR)
3312 m = MATCH_ERROR;
3313 goto cleanup;
3316 seen[d]++;
3317 seen_at[d] = gfc_current_locus;
3319 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3321 gfc_array_spec *as = NULL;
3323 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3324 d == DECL_CODIMENSION);
3326 if (current_as == NULL)
3327 current_as = as;
3328 else if (m == MATCH_YES)
3330 merge_array_spec (as, current_as, false);
3331 gfc_free (as);
3334 if (m == MATCH_NO)
3336 if (d == DECL_CODIMENSION)
3337 gfc_error ("Missing codimension specification at %C");
3338 else
3339 gfc_error ("Missing dimension specification at %C");
3340 m = MATCH_ERROR;
3343 if (m == MATCH_ERROR)
3344 goto cleanup;
3348 /* Since we've seen a double colon, we have to be looking at an
3349 attr-spec. This means that we can now issue errors. */
3350 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3351 if (seen[d] > 1)
3353 switch (d)
3355 case DECL_ALLOCATABLE:
3356 attr = "ALLOCATABLE";
3357 break;
3358 case DECL_ASYNCHRONOUS:
3359 attr = "ASYNCHRONOUS";
3360 break;
3361 case DECL_CODIMENSION:
3362 attr = "CODIMENSION";
3363 break;
3364 case DECL_CONTIGUOUS:
3365 attr = "CONTIGUOUS";
3366 break;
3367 case DECL_DIMENSION:
3368 attr = "DIMENSION";
3369 break;
3370 case DECL_EXTERNAL:
3371 attr = "EXTERNAL";
3372 break;
3373 case DECL_IN:
3374 attr = "INTENT (IN)";
3375 break;
3376 case DECL_OUT:
3377 attr = "INTENT (OUT)";
3378 break;
3379 case DECL_INOUT:
3380 attr = "INTENT (IN OUT)";
3381 break;
3382 case DECL_INTRINSIC:
3383 attr = "INTRINSIC";
3384 break;
3385 case DECL_OPTIONAL:
3386 attr = "OPTIONAL";
3387 break;
3388 case DECL_PARAMETER:
3389 attr = "PARAMETER";
3390 break;
3391 case DECL_POINTER:
3392 attr = "POINTER";
3393 break;
3394 case DECL_PROTECTED:
3395 attr = "PROTECTED";
3396 break;
3397 case DECL_PRIVATE:
3398 attr = "PRIVATE";
3399 break;
3400 case DECL_PUBLIC:
3401 attr = "PUBLIC";
3402 break;
3403 case DECL_SAVE:
3404 attr = "SAVE";
3405 break;
3406 case DECL_TARGET:
3407 attr = "TARGET";
3408 break;
3409 case DECL_IS_BIND_C:
3410 attr = "IS_BIND_C";
3411 break;
3412 case DECL_VALUE:
3413 attr = "VALUE";
3414 break;
3415 case DECL_VOLATILE:
3416 attr = "VOLATILE";
3417 break;
3418 default:
3419 attr = NULL; /* This shouldn't happen. */
3422 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3423 m = MATCH_ERROR;
3424 goto cleanup;
3427 /* Now that we've dealt with duplicate attributes, add the attributes
3428 to the current attribute. */
3429 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3431 if (seen[d] == 0)
3432 continue;
3434 if (gfc_current_state () == COMP_DERIVED
3435 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3436 && d != DECL_POINTER && d != DECL_PRIVATE
3437 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3439 if (d == DECL_ALLOCATABLE)
3441 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3442 "attribute at %C in a TYPE definition")
3443 == FAILURE)
3445 m = MATCH_ERROR;
3446 goto cleanup;
3449 else
3451 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3452 &seen_at[d]);
3453 m = MATCH_ERROR;
3454 goto cleanup;
3458 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3459 && gfc_current_state () != COMP_MODULE)
3461 if (d == DECL_PRIVATE)
3462 attr = "PRIVATE";
3463 else
3464 attr = "PUBLIC";
3465 if (gfc_current_state () == COMP_DERIVED
3466 && gfc_state_stack->previous
3467 && gfc_state_stack->previous->state == COMP_MODULE)
3469 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3470 "at %L in a TYPE definition", attr,
3471 &seen_at[d])
3472 == FAILURE)
3474 m = MATCH_ERROR;
3475 goto cleanup;
3478 else
3480 gfc_error ("%s attribute at %L is not allowed outside of the "
3481 "specification part of a module", attr, &seen_at[d]);
3482 m = MATCH_ERROR;
3483 goto cleanup;
3487 switch (d)
3489 case DECL_ALLOCATABLE:
3490 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3491 break;
3493 case DECL_ASYNCHRONOUS:
3494 if (gfc_notify_std (GFC_STD_F2003,
3495 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3496 == FAILURE)
3497 t = FAILURE;
3498 else
3499 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3500 break;
3502 case DECL_CODIMENSION:
3503 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3504 break;
3506 case DECL_CONTIGUOUS:
3507 if (gfc_notify_std (GFC_STD_F2008,
3508 "Fortran 2008: CONTIGUOUS attribute at %C")
3509 == FAILURE)
3510 t = FAILURE;
3511 else
3512 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3513 break;
3515 case DECL_DIMENSION:
3516 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3517 break;
3519 case DECL_EXTERNAL:
3520 t = gfc_add_external (&current_attr, &seen_at[d]);
3521 break;
3523 case DECL_IN:
3524 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3525 break;
3527 case DECL_OUT:
3528 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3529 break;
3531 case DECL_INOUT:
3532 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3533 break;
3535 case DECL_INTRINSIC:
3536 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3537 break;
3539 case DECL_OPTIONAL:
3540 t = gfc_add_optional (&current_attr, &seen_at[d]);
3541 break;
3543 case DECL_PARAMETER:
3544 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3545 break;
3547 case DECL_POINTER:
3548 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3549 break;
3551 case DECL_PROTECTED:
3552 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3554 gfc_error ("PROTECTED at %C only allowed in specification "
3555 "part of a module");
3556 t = FAILURE;
3557 break;
3560 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3561 "attribute at %C")
3562 == FAILURE)
3563 t = FAILURE;
3564 else
3565 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3566 break;
3568 case DECL_PRIVATE:
3569 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3570 &seen_at[d]);
3571 break;
3573 case DECL_PUBLIC:
3574 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3575 &seen_at[d]);
3576 break;
3578 case DECL_SAVE:
3579 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3580 break;
3582 case DECL_TARGET:
3583 t = gfc_add_target (&current_attr, &seen_at[d]);
3584 break;
3586 case DECL_IS_BIND_C:
3587 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3588 break;
3590 case DECL_VALUE:
3591 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3592 "at %C")
3593 == FAILURE)
3594 t = FAILURE;
3595 else
3596 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3597 break;
3599 case DECL_VOLATILE:
3600 if (gfc_notify_std (GFC_STD_F2003,
3601 "Fortran 2003: VOLATILE attribute at %C")
3602 == FAILURE)
3603 t = FAILURE;
3604 else
3605 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3606 break;
3608 default:
3609 gfc_internal_error ("match_attr_spec(): Bad attribute");
3612 if (t == FAILURE)
3614 m = MATCH_ERROR;
3615 goto cleanup;
3619 /* Module variables implicitly have the SAVE attribute. */
3620 if (gfc_current_state () == COMP_MODULE && !current_attr.save)
3621 current_attr.save = SAVE_IMPLICIT;
3623 colon_seen = 1;
3624 return MATCH_YES;
3626 cleanup:
3627 gfc_current_locus = start;
3628 gfc_free_array_spec (current_as);
3629 current_as = NULL;
3630 return m;
3634 /* Set the binding label, dest_label, either with the binding label
3635 stored in the given gfc_typespec, ts, or if none was provided, it
3636 will be the symbol name in all lower case, as required by the draft
3637 (J3/04-007, section 15.4.1). If a binding label was given and
3638 there is more than one argument (num_idents), it is an error. */
3640 gfc_try
3641 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3643 if (num_idents > 1 && has_name_equals)
3645 gfc_error ("Multiple identifiers provided with "
3646 "single NAME= specifier at %C");
3647 return FAILURE;
3650 if (curr_binding_label[0] != '\0')
3652 /* Binding label given; store in temp holder til have sym. */
3653 strcpy (dest_label, curr_binding_label);
3655 else
3657 /* No binding label given, and the NAME= specifier did not exist,
3658 which means there was no NAME="". */
3659 if (sym_name != NULL && has_name_equals == 0)
3660 strcpy (dest_label, sym_name);
3663 return SUCCESS;
3667 /* Set the status of the given common block as being BIND(C) or not,
3668 depending on the given parameter, is_bind_c. */
3670 void
3671 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3673 com_block->is_bind_c = is_bind_c;
3674 return;
3678 /* Verify that the given gfc_typespec is for a C interoperable type. */
3680 gfc_try
3681 verify_c_interop (gfc_typespec *ts)
3683 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3684 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3685 ? SUCCESS : FAILURE;
3686 else if (ts->is_c_interop != 1)
3687 return FAILURE;
3689 return SUCCESS;
3693 /* Verify that the variables of a given common block, which has been
3694 defined with the attribute specifier bind(c), to be of a C
3695 interoperable type. Errors will be reported here, if
3696 encountered. */
3698 gfc_try
3699 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3701 gfc_symbol *curr_sym = NULL;
3702 gfc_try retval = SUCCESS;
3704 curr_sym = com_block->head;
3706 /* Make sure we have at least one symbol. */
3707 if (curr_sym == NULL)
3708 return retval;
3710 /* Here we know we have a symbol, so we'll execute this loop
3711 at least once. */
3714 /* The second to last param, 1, says this is in a common block. */
3715 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3716 curr_sym = curr_sym->common_next;
3717 } while (curr_sym != NULL);
3719 return retval;
3723 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3724 an appropriate error message is reported. */
3726 gfc_try
3727 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3728 int is_in_common, gfc_common_head *com_block)
3730 bool bind_c_function = false;
3731 gfc_try retval = SUCCESS;
3733 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3734 bind_c_function = true;
3736 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3738 tmp_sym = tmp_sym->result;
3739 /* Make sure it wasn't an implicitly typed result. */
3740 if (tmp_sym->attr.implicit_type)
3742 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3743 "%L may not be C interoperable", tmp_sym->name,
3744 &tmp_sym->declared_at);
3745 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3746 /* Mark it as C interoperable to prevent duplicate warnings. */
3747 tmp_sym->ts.is_c_interop = 1;
3748 tmp_sym->attr.is_c_interop = 1;
3752 /* Here, we know we have the bind(c) attribute, so if we have
3753 enough type info, then verify that it's a C interop kind.
3754 The info could be in the symbol already, or possibly still in
3755 the given ts (current_ts), so look in both. */
3756 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3758 if (verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3760 /* See if we're dealing with a sym in a common block or not. */
3761 if (is_in_common == 1)
3763 gfc_warning ("Variable '%s' in common block '%s' at %L "
3764 "may not be a C interoperable "
3765 "kind though common block '%s' is BIND(C)",
3766 tmp_sym->name, com_block->name,
3767 &(tmp_sym->declared_at), com_block->name);
3769 else
3771 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3772 gfc_error ("Type declaration '%s' at %L is not C "
3773 "interoperable but it is BIND(C)",
3774 tmp_sym->name, &(tmp_sym->declared_at));
3775 else
3776 gfc_warning ("Variable '%s' at %L "
3777 "may not be a C interoperable "
3778 "kind but it is bind(c)",
3779 tmp_sym->name, &(tmp_sym->declared_at));
3783 /* Variables declared w/in a common block can't be bind(c)
3784 since there's no way for C to see these variables, so there's
3785 semantically no reason for the attribute. */
3786 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3788 gfc_error ("Variable '%s' in common block '%s' at "
3789 "%L cannot be declared with BIND(C) "
3790 "since it is not a global",
3791 tmp_sym->name, com_block->name,
3792 &(tmp_sym->declared_at));
3793 retval = FAILURE;
3796 /* Scalar variables that are bind(c) can not have the pointer
3797 or allocatable attributes. */
3798 if (tmp_sym->attr.is_bind_c == 1)
3800 if (tmp_sym->attr.pointer == 1)
3802 gfc_error ("Variable '%s' at %L cannot have both the "
3803 "POINTER and BIND(C) attributes",
3804 tmp_sym->name, &(tmp_sym->declared_at));
3805 retval = FAILURE;
3808 if (tmp_sym->attr.allocatable == 1)
3810 gfc_error ("Variable '%s' at %L cannot have both the "
3811 "ALLOCATABLE and BIND(C) attributes",
3812 tmp_sym->name, &(tmp_sym->declared_at));
3813 retval = FAILURE;
3818 /* If it is a BIND(C) function, make sure the return value is a
3819 scalar value. The previous tests in this function made sure
3820 the type is interoperable. */
3821 if (bind_c_function && tmp_sym->as != NULL)
3822 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3823 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3825 /* BIND(C) functions can not return a character string. */
3826 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3827 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
3828 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
3829 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
3830 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3831 "be a character string", tmp_sym->name,
3832 &(tmp_sym->declared_at));
3835 /* See if the symbol has been marked as private. If it has, make sure
3836 there is no binding label and warn the user if there is one. */
3837 if (tmp_sym->attr.access == ACCESS_PRIVATE
3838 && tmp_sym->binding_label[0] != '\0')
3839 /* Use gfc_warning_now because we won't say that the symbol fails
3840 just because of this. */
3841 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3842 "given the binding label '%s'", tmp_sym->name,
3843 &(tmp_sym->declared_at), tmp_sym->binding_label);
3845 return retval;
3849 /* Set the appropriate fields for a symbol that's been declared as
3850 BIND(C) (the is_bind_c flag and the binding label), and verify that
3851 the type is C interoperable. Errors are reported by the functions
3852 used to set/test these fields. */
3854 gfc_try
3855 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3857 gfc_try retval = SUCCESS;
3859 /* TODO: Do we need to make sure the vars aren't marked private? */
3861 /* Set the is_bind_c bit in symbol_attribute. */
3862 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3864 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3865 num_idents) != SUCCESS)
3866 return FAILURE;
3868 return retval;
3872 /* Set the fields marking the given common block as BIND(C), including
3873 a binding label, and report any errors encountered. */
3875 gfc_try
3876 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3878 gfc_try retval = SUCCESS;
3880 /* destLabel, common name, typespec (which may have binding label). */
3881 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3882 != SUCCESS)
3883 return FAILURE;
3885 /* Set the given common block (com_block) to being bind(c) (1). */
3886 set_com_block_bind_c (com_block, 1);
3888 return retval;
3892 /* Retrieve the list of one or more identifiers that the given bind(c)
3893 attribute applies to. */
3895 gfc_try
3896 get_bind_c_idents (void)
3898 char name[GFC_MAX_SYMBOL_LEN + 1];
3899 int num_idents = 0;
3900 gfc_symbol *tmp_sym = NULL;
3901 match found_id;
3902 gfc_common_head *com_block = NULL;
3904 if (gfc_match_name (name) == MATCH_YES)
3906 found_id = MATCH_YES;
3907 gfc_get_ha_symbol (name, &tmp_sym);
3909 else if (match_common_name (name) == MATCH_YES)
3911 found_id = MATCH_YES;
3912 com_block = gfc_get_common (name, 0);
3914 else
3916 gfc_error ("Need either entity or common block name for "
3917 "attribute specification statement at %C");
3918 return FAILURE;
3921 /* Save the current identifier and look for more. */
3924 /* Increment the number of identifiers found for this spec stmt. */
3925 num_idents++;
3927 /* Make sure we have a sym or com block, and verify that it can
3928 be bind(c). Set the appropriate field(s) and look for more
3929 identifiers. */
3930 if (tmp_sym != NULL || com_block != NULL)
3932 if (tmp_sym != NULL)
3934 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3935 != SUCCESS)
3936 return FAILURE;
3938 else
3940 if (set_verify_bind_c_com_block(com_block, num_idents)
3941 != SUCCESS)
3942 return FAILURE;
3945 /* Look to see if we have another identifier. */
3946 tmp_sym = NULL;
3947 if (gfc_match_eos () == MATCH_YES)
3948 found_id = MATCH_NO;
3949 else if (gfc_match_char (',') != MATCH_YES)
3950 found_id = MATCH_NO;
3951 else if (gfc_match_name (name) == MATCH_YES)
3953 found_id = MATCH_YES;
3954 gfc_get_ha_symbol (name, &tmp_sym);
3956 else if (match_common_name (name) == MATCH_YES)
3958 found_id = MATCH_YES;
3959 com_block = gfc_get_common (name, 0);
3961 else
3963 gfc_error ("Missing entity or common block name for "
3964 "attribute specification statement at %C");
3965 return FAILURE;
3968 else
3970 gfc_internal_error ("Missing symbol");
3972 } while (found_id == MATCH_YES);
3974 /* if we get here we were successful */
3975 return SUCCESS;
3979 /* Try and match a BIND(C) attribute specification statement. */
3981 match
3982 gfc_match_bind_c_stmt (void)
3984 match found_match = MATCH_NO;
3985 gfc_typespec *ts;
3987 ts = &current_ts;
3989 /* This may not be necessary. */
3990 gfc_clear_ts (ts);
3991 /* Clear the temporary binding label holder. */
3992 curr_binding_label[0] = '\0';
3994 /* Look for the bind(c). */
3995 found_match = gfc_match_bind_c (NULL, true);
3997 if (found_match == MATCH_YES)
3999 /* Look for the :: now, but it is not required. */
4000 gfc_match (" :: ");
4002 /* Get the identifier(s) that needs to be updated. This may need to
4003 change to hand the flag(s) for the attr specified so all identifiers
4004 found can have all appropriate parts updated (assuming that the same
4005 spec stmt can have multiple attrs, such as both bind(c) and
4006 allocatable...). */
4007 if (get_bind_c_idents () != SUCCESS)
4008 /* Error message should have printed already. */
4009 return MATCH_ERROR;
4012 return found_match;
4016 /* Match a data declaration statement. */
4018 match
4019 gfc_match_data_decl (void)
4021 gfc_symbol *sym;
4022 match m;
4023 int elem;
4025 num_idents_on_line = 0;
4027 m = gfc_match_decl_type_spec (&current_ts, 0);
4028 if (m != MATCH_YES)
4029 return m;
4031 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4032 && gfc_current_state () != COMP_DERIVED)
4034 sym = gfc_use_derived (current_ts.u.derived);
4036 if (sym == NULL)
4038 m = MATCH_ERROR;
4039 goto cleanup;
4042 current_ts.u.derived = sym;
4045 m = match_attr_spec ();
4046 if (m == MATCH_ERROR)
4048 m = MATCH_NO;
4049 goto cleanup;
4052 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4053 && current_ts.u.derived->components == NULL
4054 && !current_ts.u.derived->attr.zero_comp)
4057 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4058 goto ok;
4060 gfc_find_symbol (current_ts.u.derived->name,
4061 current_ts.u.derived->ns->parent, 1, &sym);
4063 /* Any symbol that we find had better be a type definition
4064 which has its components defined. */
4065 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4066 && (current_ts.u.derived->components != NULL
4067 || current_ts.u.derived->attr.zero_comp))
4068 goto ok;
4070 /* Now we have an error, which we signal, and then fix up
4071 because the knock-on is plain and simple confusing. */
4072 gfc_error_now ("Derived type at %C has not been previously defined "
4073 "and so cannot appear in a derived type definition");
4074 current_attr.pointer = 1;
4075 goto ok;
4079 /* If we have an old-style character declaration, and no new-style
4080 attribute specifications, then there a comma is optional between
4081 the type specification and the variable list. */
4082 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4083 gfc_match_char (',');
4085 /* Give the types/attributes to symbols that follow. Give the element
4086 a number so that repeat character length expressions can be copied. */
4087 elem = 1;
4088 for (;;)
4090 num_idents_on_line++;
4091 m = variable_decl (elem++);
4092 if (m == MATCH_ERROR)
4093 goto cleanup;
4094 if (m == MATCH_NO)
4095 break;
4097 if (gfc_match_eos () == MATCH_YES)
4098 goto cleanup;
4099 if (gfc_match_char (',') != MATCH_YES)
4100 break;
4103 if (gfc_error_flag_test () == 0)
4104 gfc_error ("Syntax error in data declaration at %C");
4105 m = MATCH_ERROR;
4107 gfc_free_data_all (gfc_current_ns);
4109 cleanup:
4110 gfc_free_array_spec (current_as);
4111 current_as = NULL;
4112 return m;
4116 /* Match a prefix associated with a function or subroutine
4117 declaration. If the typespec pointer is nonnull, then a typespec
4118 can be matched. Note that if nothing matches, MATCH_YES is
4119 returned (the null string was matched). */
4121 match
4122 gfc_match_prefix (gfc_typespec *ts)
4124 bool seen_type;
4125 bool seen_impure;
4126 bool found_prefix;
4128 gfc_clear_attr (&current_attr);
4129 seen_type = false;
4130 seen_impure = false;
4132 gcc_assert (!gfc_matching_prefix);
4133 gfc_matching_prefix = true;
4137 found_prefix = false;
4139 if (!seen_type && ts != NULL
4140 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4141 && gfc_match_space () == MATCH_YES)
4144 seen_type = true;
4145 found_prefix = true;
4148 if (gfc_match ("elemental% ") == MATCH_YES)
4150 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
4151 goto error;
4153 found_prefix = true;
4156 if (gfc_match ("pure% ") == MATCH_YES)
4158 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
4159 goto error;
4161 found_prefix = true;
4164 if (gfc_match ("recursive% ") == MATCH_YES)
4166 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
4167 goto error;
4169 found_prefix = true;
4172 /* IMPURE is a somewhat special case, as it needs not set an actual
4173 attribute but rather only prevents ELEMENTAL routines from being
4174 automatically PURE. */
4175 if (gfc_match ("impure% ") == MATCH_YES)
4177 if (gfc_notify_std (GFC_STD_F2008,
4178 "Fortran 2008: IMPURE procedure at %C")
4179 == FAILURE)
4180 goto error;
4182 seen_impure = true;
4183 found_prefix = true;
4186 while (found_prefix);
4188 /* IMPURE and PURE must not both appear, of course. */
4189 if (seen_impure && current_attr.pure)
4191 gfc_error ("PURE and IMPURE must not appear both at %C");
4192 goto error;
4195 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4196 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4198 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
4199 goto error;
4202 /* At this point, the next item is not a prefix. */
4203 gcc_assert (gfc_matching_prefix);
4204 gfc_matching_prefix = false;
4205 return MATCH_YES;
4207 error:
4208 gcc_assert (gfc_matching_prefix);
4209 gfc_matching_prefix = false;
4210 return MATCH_ERROR;
4214 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4216 static gfc_try
4217 copy_prefix (symbol_attribute *dest, locus *where)
4219 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
4220 return FAILURE;
4222 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
4223 return FAILURE;
4225 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
4226 return FAILURE;
4228 return SUCCESS;
4232 /* Match a formal argument list. */
4234 match
4235 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4237 gfc_formal_arglist *head, *tail, *p, *q;
4238 char name[GFC_MAX_SYMBOL_LEN + 1];
4239 gfc_symbol *sym;
4240 match m;
4242 head = tail = NULL;
4244 if (gfc_match_char ('(') != MATCH_YES)
4246 if (null_flag)
4247 goto ok;
4248 return MATCH_NO;
4251 if (gfc_match_char (')') == MATCH_YES)
4252 goto ok;
4254 for (;;)
4256 if (gfc_match_char ('*') == MATCH_YES)
4257 sym = NULL;
4258 else
4260 m = gfc_match_name (name);
4261 if (m != MATCH_YES)
4262 goto cleanup;
4264 if (gfc_get_symbol (name, NULL, &sym))
4265 goto cleanup;
4268 p = gfc_get_formal_arglist ();
4270 if (head == NULL)
4271 head = tail = p;
4272 else
4274 tail->next = p;
4275 tail = p;
4278 tail->sym = sym;
4280 /* We don't add the VARIABLE flavor because the name could be a
4281 dummy procedure. We don't apply these attributes to formal
4282 arguments of statement functions. */
4283 if (sym != NULL && !st_flag
4284 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
4285 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
4287 m = MATCH_ERROR;
4288 goto cleanup;
4291 /* The name of a program unit can be in a different namespace,
4292 so check for it explicitly. After the statement is accepted,
4293 the name is checked for especially in gfc_get_symbol(). */
4294 if (gfc_new_block != NULL && sym != NULL
4295 && strcmp (sym->name, gfc_new_block->name) == 0)
4297 gfc_error ("Name '%s' at %C is the name of the procedure",
4298 sym->name);
4299 m = MATCH_ERROR;
4300 goto cleanup;
4303 if (gfc_match_char (')') == MATCH_YES)
4304 goto ok;
4306 m = gfc_match_char (',');
4307 if (m != MATCH_YES)
4309 gfc_error ("Unexpected junk in formal argument list at %C");
4310 goto cleanup;
4315 /* Check for duplicate symbols in the formal argument list. */
4316 if (head != NULL)
4318 for (p = head; p->next; p = p->next)
4320 if (p->sym == NULL)
4321 continue;
4323 for (q = p->next; q; q = q->next)
4324 if (p->sym == q->sym)
4326 gfc_error ("Duplicate symbol '%s' in formal argument list "
4327 "at %C", p->sym->name);
4329 m = MATCH_ERROR;
4330 goto cleanup;
4335 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
4336 == FAILURE)
4338 m = MATCH_ERROR;
4339 goto cleanup;
4342 return MATCH_YES;
4344 cleanup:
4345 gfc_free_formal_arglist (head);
4346 return m;
4350 /* Match a RESULT specification following a function declaration or
4351 ENTRY statement. Also matches the end-of-statement. */
4353 static match
4354 match_result (gfc_symbol *function, gfc_symbol **result)
4356 char name[GFC_MAX_SYMBOL_LEN + 1];
4357 gfc_symbol *r;
4358 match m;
4360 if (gfc_match (" result (") != MATCH_YES)
4361 return MATCH_NO;
4363 m = gfc_match_name (name);
4364 if (m != MATCH_YES)
4365 return m;
4367 /* Get the right paren, and that's it because there could be the
4368 bind(c) attribute after the result clause. */
4369 if (gfc_match_char(')') != MATCH_YES)
4371 /* TODO: should report the missing right paren here. */
4372 return MATCH_ERROR;
4375 if (strcmp (function->name, name) == 0)
4377 gfc_error ("RESULT variable at %C must be different than function name");
4378 return MATCH_ERROR;
4381 if (gfc_get_symbol (name, NULL, &r))
4382 return MATCH_ERROR;
4384 if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4385 return MATCH_ERROR;
4387 *result = r;
4389 return MATCH_YES;
4393 /* Match a function suffix, which could be a combination of a result
4394 clause and BIND(C), either one, or neither. The draft does not
4395 require them to come in a specific order. */
4397 match
4398 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4400 match is_bind_c; /* Found bind(c). */
4401 match is_result; /* Found result clause. */
4402 match found_match; /* Status of whether we've found a good match. */
4403 char peek_char; /* Character we're going to peek at. */
4404 bool allow_binding_name;
4406 /* Initialize to having found nothing. */
4407 found_match = MATCH_NO;
4408 is_bind_c = MATCH_NO;
4409 is_result = MATCH_NO;
4411 /* Get the next char to narrow between result and bind(c). */
4412 gfc_gobble_whitespace ();
4413 peek_char = gfc_peek_ascii_char ();
4415 /* C binding names are not allowed for internal procedures. */
4416 if (gfc_current_state () == COMP_CONTAINS
4417 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4418 allow_binding_name = false;
4419 else
4420 allow_binding_name = true;
4422 switch (peek_char)
4424 case 'r':
4425 /* Look for result clause. */
4426 is_result = match_result (sym, result);
4427 if (is_result == MATCH_YES)
4429 /* Now see if there is a bind(c) after it. */
4430 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4431 /* We've found the result clause and possibly bind(c). */
4432 found_match = MATCH_YES;
4434 else
4435 /* This should only be MATCH_ERROR. */
4436 found_match = is_result;
4437 break;
4438 case 'b':
4439 /* Look for bind(c) first. */
4440 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4441 if (is_bind_c == MATCH_YES)
4443 /* Now see if a result clause followed it. */
4444 is_result = match_result (sym, result);
4445 found_match = MATCH_YES;
4447 else
4449 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4450 found_match = MATCH_ERROR;
4452 break;
4453 default:
4454 gfc_error ("Unexpected junk after function declaration at %C");
4455 found_match = MATCH_ERROR;
4456 break;
4459 if (is_bind_c == MATCH_YES)
4461 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4462 if (gfc_current_state () == COMP_CONTAINS
4463 && sym->ns->proc_name->attr.flavor != FL_MODULE
4464 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4465 "at %L may not be specified for an internal "
4466 "procedure", &gfc_current_locus)
4467 == FAILURE)
4468 return MATCH_ERROR;
4470 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4471 == FAILURE)
4472 return MATCH_ERROR;
4475 return found_match;
4479 /* Procedure pointer return value without RESULT statement:
4480 Add "hidden" result variable named "ppr@". */
4482 static gfc_try
4483 add_hidden_procptr_result (gfc_symbol *sym)
4485 bool case1,case2;
4487 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4488 return FAILURE;
4490 /* First usage case: PROCEDURE and EXTERNAL statements. */
4491 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4492 && strcmp (gfc_current_block ()->name, sym->name) == 0
4493 && sym->attr.external;
4494 /* Second usage case: INTERFACE statements. */
4495 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4496 && gfc_state_stack->previous->state == COMP_FUNCTION
4497 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4499 if (case1 || case2)
4501 gfc_symtree *stree;
4502 if (case1)
4503 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4504 else if (case2)
4506 gfc_symtree *st2;
4507 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4508 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4509 st2->n.sym = stree->n.sym;
4511 sym->result = stree->n.sym;
4513 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4514 sym->result->attr.pointer = sym->attr.pointer;
4515 sym->result->attr.external = sym->attr.external;
4516 sym->result->attr.referenced = sym->attr.referenced;
4517 sym->result->ts = sym->ts;
4518 sym->attr.proc_pointer = 0;
4519 sym->attr.pointer = 0;
4520 sym->attr.external = 0;
4521 if (sym->result->attr.external && sym->result->attr.pointer)
4523 sym->result->attr.pointer = 0;
4524 sym->result->attr.proc_pointer = 1;
4527 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4529 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4530 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4531 && sym->result && sym->result != sym && sym->result->attr.external
4532 && sym == gfc_current_ns->proc_name
4533 && sym == sym->result->ns->proc_name
4534 && strcmp ("ppr@", sym->result->name) == 0)
4536 sym->result->attr.proc_pointer = 1;
4537 sym->attr.pointer = 0;
4538 return SUCCESS;
4540 else
4541 return FAILURE;
4545 /* Match the interface for a PROCEDURE declaration,
4546 including brackets (R1212). */
4548 static match
4549 match_procedure_interface (gfc_symbol **proc_if)
4551 match m;
4552 gfc_symtree *st;
4553 locus old_loc, entry_loc;
4554 gfc_namespace *old_ns = gfc_current_ns;
4555 char name[GFC_MAX_SYMBOL_LEN + 1];
4557 old_loc = entry_loc = gfc_current_locus;
4558 gfc_clear_ts (&current_ts);
4560 if (gfc_match (" (") != MATCH_YES)
4562 gfc_current_locus = entry_loc;
4563 return MATCH_NO;
4566 /* Get the type spec. for the procedure interface. */
4567 old_loc = gfc_current_locus;
4568 m = gfc_match_decl_type_spec (&current_ts, 0);
4569 gfc_gobble_whitespace ();
4570 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4571 goto got_ts;
4573 if (m == MATCH_ERROR)
4574 return m;
4576 /* Procedure interface is itself a procedure. */
4577 gfc_current_locus = old_loc;
4578 m = gfc_match_name (name);
4580 /* First look to see if it is already accessible in the current
4581 namespace because it is use associated or contained. */
4582 st = NULL;
4583 if (gfc_find_sym_tree (name, NULL, 0, &st))
4584 return MATCH_ERROR;
4586 /* If it is still not found, then try the parent namespace, if it
4587 exists and create the symbol there if it is still not found. */
4588 if (gfc_current_ns->parent)
4589 gfc_current_ns = gfc_current_ns->parent;
4590 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4591 return MATCH_ERROR;
4593 gfc_current_ns = old_ns;
4594 *proc_if = st->n.sym;
4596 /* Various interface checks. */
4597 if (*proc_if)
4599 (*proc_if)->refs++;
4600 /* Resolve interface if possible. That way, attr.procedure is only set
4601 if it is declared by a later procedure-declaration-stmt, which is
4602 invalid per C1212. */
4603 while ((*proc_if)->ts.interface)
4604 *proc_if = (*proc_if)->ts.interface;
4606 if ((*proc_if)->generic)
4608 gfc_error ("Interface '%s' at %C may not be generic",
4609 (*proc_if)->name);
4610 return MATCH_ERROR;
4612 if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
4614 gfc_error ("Interface '%s' at %C may not be a statement function",
4615 (*proc_if)->name);
4616 return MATCH_ERROR;
4618 /* Handle intrinsic procedures. */
4619 if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4620 || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4621 && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4622 || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4623 (*proc_if)->attr.intrinsic = 1;
4624 if ((*proc_if)->attr.intrinsic
4625 && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
4627 gfc_error ("Intrinsic procedure '%s' not allowed "
4628 "in PROCEDURE statement at %C", (*proc_if)->name);
4629 return MATCH_ERROR;
4633 got_ts:
4634 if (gfc_match (" )") != MATCH_YES)
4636 gfc_current_locus = entry_loc;
4637 return MATCH_NO;
4640 return MATCH_YES;
4644 /* Match a PROCEDURE declaration (R1211). */
4646 static match
4647 match_procedure_decl (void)
4649 match m;
4650 gfc_symbol *sym, *proc_if = NULL;
4651 int num;
4652 gfc_expr *initializer = NULL;
4654 /* Parse interface (with brackets). */
4655 m = match_procedure_interface (&proc_if);
4656 if (m != MATCH_YES)
4657 return m;
4659 /* Parse attributes (with colons). */
4660 m = match_attr_spec();
4661 if (m == MATCH_ERROR)
4662 return MATCH_ERROR;
4664 /* Get procedure symbols. */
4665 for(num=1;;num++)
4667 m = gfc_match_symbol (&sym, 0);
4668 if (m == MATCH_NO)
4669 goto syntax;
4670 else if (m == MATCH_ERROR)
4671 return m;
4673 /* Add current_attr to the symbol attributes. */
4674 if (gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4675 return MATCH_ERROR;
4677 if (sym->attr.is_bind_c)
4679 /* Check for C1218. */
4680 if (!proc_if || !proc_if->attr.is_bind_c)
4682 gfc_error ("BIND(C) attribute at %C requires "
4683 "an interface with BIND(C)");
4684 return MATCH_ERROR;
4686 /* Check for C1217. */
4687 if (has_name_equals && sym->attr.pointer)
4689 gfc_error ("BIND(C) procedure with NAME may not have "
4690 "POINTER attribute at %C");
4691 return MATCH_ERROR;
4693 if (has_name_equals && sym->attr.dummy)
4695 gfc_error ("Dummy procedure at %C may not have "
4696 "BIND(C) attribute with NAME");
4697 return MATCH_ERROR;
4699 /* Set binding label for BIND(C). */
4700 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4701 return MATCH_ERROR;
4704 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4705 return MATCH_ERROR;
4707 if (add_hidden_procptr_result (sym) == SUCCESS)
4708 sym = sym->result;
4710 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4711 return MATCH_ERROR;
4713 /* Set interface. */
4714 if (proc_if != NULL)
4716 if (sym->ts.type != BT_UNKNOWN)
4718 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4719 sym->name, &gfc_current_locus,
4720 gfc_basic_typename (sym->ts.type));
4721 return MATCH_ERROR;
4723 sym->ts.interface = proc_if;
4724 sym->attr.untyped = 1;
4725 sym->attr.if_source = IFSRC_IFBODY;
4727 else if (current_ts.type != BT_UNKNOWN)
4729 if (gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
4730 return MATCH_ERROR;
4731 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4732 sym->ts.interface->ts = current_ts;
4733 sym->ts.interface->attr.function = 1;
4734 sym->attr.function = sym->ts.interface->attr.function;
4735 sym->attr.if_source = IFSRC_UNKNOWN;
4738 if (gfc_match (" =>") == MATCH_YES)
4740 if (!current_attr.pointer)
4742 gfc_error ("Initialization at %C isn't for a pointer variable");
4743 m = MATCH_ERROR;
4744 goto cleanup;
4747 m = match_pointer_init (&initializer, 1);
4748 if (m != MATCH_YES)
4749 goto cleanup;
4751 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4752 != SUCCESS)
4753 goto cleanup;
4757 gfc_set_sym_referenced (sym);
4759 if (gfc_match_eos () == MATCH_YES)
4760 return MATCH_YES;
4761 if (gfc_match_char (',') != MATCH_YES)
4762 goto syntax;
4765 syntax:
4766 gfc_error ("Syntax error in PROCEDURE statement at %C");
4767 return MATCH_ERROR;
4769 cleanup:
4770 /* Free stuff up and return. */
4771 gfc_free_expr (initializer);
4772 return m;
4776 static match
4777 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4780 /* Match a procedure pointer component declaration (R445). */
4782 static match
4783 match_ppc_decl (void)
4785 match m;
4786 gfc_symbol *proc_if = NULL;
4787 gfc_typespec ts;
4788 int num;
4789 gfc_component *c;
4790 gfc_expr *initializer = NULL;
4791 gfc_typebound_proc* tb;
4792 char name[GFC_MAX_SYMBOL_LEN + 1];
4794 /* Parse interface (with brackets). */
4795 m = match_procedure_interface (&proc_if);
4796 if (m != MATCH_YES)
4797 goto syntax;
4799 /* Parse attributes. */
4800 tb = XCNEW (gfc_typebound_proc);
4801 tb->where = gfc_current_locus;
4802 m = match_binding_attributes (tb, false, true);
4803 if (m == MATCH_ERROR)
4804 return m;
4806 gfc_clear_attr (&current_attr);
4807 current_attr.procedure = 1;
4808 current_attr.proc_pointer = 1;
4809 current_attr.access = tb->access;
4810 current_attr.flavor = FL_PROCEDURE;
4812 /* Match the colons (required). */
4813 if (gfc_match (" ::") != MATCH_YES)
4815 gfc_error ("Expected '::' after binding-attributes at %C");
4816 return MATCH_ERROR;
4819 /* Check for C450. */
4820 if (!tb->nopass && proc_if == NULL)
4822 gfc_error("NOPASS or explicit interface required at %C");
4823 return MATCH_ERROR;
4826 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure pointer "
4827 "component at %C") == FAILURE)
4828 return MATCH_ERROR;
4830 /* Match PPC names. */
4831 ts = current_ts;
4832 for(num=1;;num++)
4834 m = gfc_match_name (name);
4835 if (m == MATCH_NO)
4836 goto syntax;
4837 else if (m == MATCH_ERROR)
4838 return m;
4840 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
4841 return MATCH_ERROR;
4843 /* Add current_attr to the symbol attributes. */
4844 if (gfc_copy_attr (&c->attr, &current_attr, NULL) == FAILURE)
4845 return MATCH_ERROR;
4847 if (gfc_add_external (&c->attr, NULL) == FAILURE)
4848 return MATCH_ERROR;
4850 if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
4851 return MATCH_ERROR;
4853 c->tb = tb;
4855 /* Set interface. */
4856 if (proc_if != NULL)
4858 c->ts.interface = proc_if;
4859 c->attr.untyped = 1;
4860 c->attr.if_source = IFSRC_IFBODY;
4862 else if (ts.type != BT_UNKNOWN)
4864 c->ts = ts;
4865 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4866 c->ts.interface->ts = ts;
4867 c->ts.interface->attr.function = 1;
4868 c->attr.function = c->ts.interface->attr.function;
4869 c->attr.if_source = IFSRC_UNKNOWN;
4872 if (gfc_match (" =>") == MATCH_YES)
4874 m = match_pointer_init (&initializer, 1);
4875 if (m != MATCH_YES)
4877 gfc_free_expr (initializer);
4878 return m;
4880 c->initializer = initializer;
4883 if (gfc_match_eos () == MATCH_YES)
4884 return MATCH_YES;
4885 if (gfc_match_char (',') != MATCH_YES)
4886 goto syntax;
4889 syntax:
4890 gfc_error ("Syntax error in procedure pointer component at %C");
4891 return MATCH_ERROR;
4895 /* Match a PROCEDURE declaration inside an interface (R1206). */
4897 static match
4898 match_procedure_in_interface (void)
4900 match m;
4901 gfc_symbol *sym;
4902 char name[GFC_MAX_SYMBOL_LEN + 1];
4904 if (current_interface.type == INTERFACE_NAMELESS
4905 || current_interface.type == INTERFACE_ABSTRACT)
4907 gfc_error ("PROCEDURE at %C must be in a generic interface");
4908 return MATCH_ERROR;
4911 for(;;)
4913 m = gfc_match_name (name);
4914 if (m == MATCH_NO)
4915 goto syntax;
4916 else if (m == MATCH_ERROR)
4917 return m;
4918 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4919 return MATCH_ERROR;
4921 if (gfc_add_interface (sym) == FAILURE)
4922 return MATCH_ERROR;
4924 if (gfc_match_eos () == MATCH_YES)
4925 break;
4926 if (gfc_match_char (',') != MATCH_YES)
4927 goto syntax;
4930 return MATCH_YES;
4932 syntax:
4933 gfc_error ("Syntax error in PROCEDURE statement at %C");
4934 return MATCH_ERROR;
4938 /* General matcher for PROCEDURE declarations. */
4940 static match match_procedure_in_type (void);
4942 match
4943 gfc_match_procedure (void)
4945 match m;
4947 switch (gfc_current_state ())
4949 case COMP_NONE:
4950 case COMP_PROGRAM:
4951 case COMP_MODULE:
4952 case COMP_SUBROUTINE:
4953 case COMP_FUNCTION:
4954 m = match_procedure_decl ();
4955 break;
4956 case COMP_INTERFACE:
4957 m = match_procedure_in_interface ();
4958 break;
4959 case COMP_DERIVED:
4960 m = match_ppc_decl ();
4961 break;
4962 case COMP_DERIVED_CONTAINS:
4963 m = match_procedure_in_type ();
4964 break;
4965 default:
4966 return MATCH_NO;
4969 if (m != MATCH_YES)
4970 return m;
4972 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4973 == FAILURE)
4974 return MATCH_ERROR;
4976 return m;
4980 /* Warn if a matched procedure has the same name as an intrinsic; this is
4981 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4982 parser-state-stack to find out whether we're in a module. */
4984 static void
4985 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
4987 bool in_module;
4989 in_module = (gfc_state_stack->previous
4990 && gfc_state_stack->previous->state == COMP_MODULE);
4992 gfc_warn_intrinsic_shadow (sym, in_module, func);
4996 /* Match a function declaration. */
4998 match
4999 gfc_match_function_decl (void)
5001 char name[GFC_MAX_SYMBOL_LEN + 1];
5002 gfc_symbol *sym, *result;
5003 locus old_loc;
5004 match m;
5005 match suffix_match;
5006 match found_match; /* Status returned by match func. */
5008 if (gfc_current_state () != COMP_NONE
5009 && gfc_current_state () != COMP_INTERFACE
5010 && gfc_current_state () != COMP_CONTAINS)
5011 return MATCH_NO;
5013 gfc_clear_ts (&current_ts);
5015 old_loc = gfc_current_locus;
5017 m = gfc_match_prefix (&current_ts);
5018 if (m != MATCH_YES)
5020 gfc_current_locus = old_loc;
5021 return m;
5024 if (gfc_match ("function% %n", name) != MATCH_YES)
5026 gfc_current_locus = old_loc;
5027 return MATCH_NO;
5029 if (get_proc_name (name, &sym, false))
5030 return MATCH_ERROR;
5032 if (add_hidden_procptr_result (sym) == SUCCESS)
5033 sym = sym->result;
5035 gfc_new_block = sym;
5037 m = gfc_match_formal_arglist (sym, 0, 0);
5038 if (m == MATCH_NO)
5040 gfc_error ("Expected formal argument list in function "
5041 "definition at %C");
5042 m = MATCH_ERROR;
5043 goto cleanup;
5045 else if (m == MATCH_ERROR)
5046 goto cleanup;
5048 result = NULL;
5050 /* According to the draft, the bind(c) and result clause can
5051 come in either order after the formal_arg_list (i.e., either
5052 can be first, both can exist together or by themselves or neither
5053 one). Therefore, the match_result can't match the end of the
5054 string, and check for the bind(c) or result clause in either order. */
5055 found_match = gfc_match_eos ();
5057 /* Make sure that it isn't already declared as BIND(C). If it is, it
5058 must have been marked BIND(C) with a BIND(C) attribute and that is
5059 not allowed for procedures. */
5060 if (sym->attr.is_bind_c == 1)
5062 sym->attr.is_bind_c = 0;
5063 if (sym->old_symbol != NULL)
5064 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5065 "variables or common blocks",
5066 &(sym->old_symbol->declared_at));
5067 else
5068 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5069 "variables or common blocks", &gfc_current_locus);
5072 if (found_match != MATCH_YES)
5074 /* If we haven't found the end-of-statement, look for a suffix. */
5075 suffix_match = gfc_match_suffix (sym, &result);
5076 if (suffix_match == MATCH_YES)
5077 /* Need to get the eos now. */
5078 found_match = gfc_match_eos ();
5079 else
5080 found_match = suffix_match;
5083 if(found_match != MATCH_YES)
5084 m = MATCH_ERROR;
5085 else
5087 /* Make changes to the symbol. */
5088 m = MATCH_ERROR;
5090 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
5091 goto cleanup;
5093 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
5094 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5095 goto cleanup;
5097 /* Delay matching the function characteristics until after the
5098 specification block by signalling kind=-1. */
5099 sym->declared_at = old_loc;
5100 if (current_ts.type != BT_UNKNOWN)
5101 current_ts.kind = -1;
5102 else
5103 current_ts.kind = 0;
5105 if (result == NULL)
5107 if (current_ts.type != BT_UNKNOWN
5108 && gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
5109 goto cleanup;
5110 sym->result = sym;
5112 else
5114 if (current_ts.type != BT_UNKNOWN
5115 && gfc_add_type (result, &current_ts, &gfc_current_locus)
5116 == FAILURE)
5117 goto cleanup;
5118 sym->result = result;
5121 /* Warn if this procedure has the same name as an intrinsic. */
5122 warn_intrinsic_shadow (sym, true);
5124 return MATCH_YES;
5127 cleanup:
5128 gfc_current_locus = old_loc;
5129 return m;
5133 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5134 pass the name of the entry, rather than the gfc_current_block name, and
5135 to return false upon finding an existing global entry. */
5137 static bool
5138 add_global_entry (const char *name, int sub)
5140 gfc_gsymbol *s;
5141 enum gfc_symbol_type type;
5143 s = gfc_get_gsymbol(name);
5144 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5146 if (s->defined
5147 || (s->type != GSYM_UNKNOWN
5148 && s->type != type))
5149 gfc_global_used(s, NULL);
5150 else
5152 s->type = type;
5153 s->where = gfc_current_locus;
5154 s->defined = 1;
5155 s->ns = gfc_current_ns;
5156 return true;
5158 return false;
5162 /* Match an ENTRY statement. */
5164 match
5165 gfc_match_entry (void)
5167 gfc_symbol *proc;
5168 gfc_symbol *result;
5169 gfc_symbol *entry;
5170 char name[GFC_MAX_SYMBOL_LEN + 1];
5171 gfc_compile_state state;
5172 match m;
5173 gfc_entry_list *el;
5174 locus old_loc;
5175 bool module_procedure;
5176 char peek_char;
5177 match is_bind_c;
5179 m = gfc_match_name (name);
5180 if (m != MATCH_YES)
5181 return m;
5183 if (gfc_notify_std (GFC_STD_F2008_OBS, "Fortran 2008 obsolescent feature: "
5184 "ENTRY statement at %C") == FAILURE)
5185 return MATCH_ERROR;
5187 state = gfc_current_state ();
5188 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5190 switch (state)
5192 case COMP_PROGRAM:
5193 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5194 break;
5195 case COMP_MODULE:
5196 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5197 break;
5198 case COMP_BLOCK_DATA:
5199 gfc_error ("ENTRY statement at %C cannot appear within "
5200 "a BLOCK DATA");
5201 break;
5202 case COMP_INTERFACE:
5203 gfc_error ("ENTRY statement at %C cannot appear within "
5204 "an INTERFACE");
5205 break;
5206 case COMP_DERIVED:
5207 gfc_error ("ENTRY statement at %C cannot appear within "
5208 "a DERIVED TYPE block");
5209 break;
5210 case COMP_IF:
5211 gfc_error ("ENTRY statement at %C cannot appear within "
5212 "an IF-THEN block");
5213 break;
5214 case COMP_DO:
5215 gfc_error ("ENTRY statement at %C cannot appear within "
5216 "a DO block");
5217 break;
5218 case COMP_SELECT:
5219 gfc_error ("ENTRY statement at %C cannot appear within "
5220 "a SELECT block");
5221 break;
5222 case COMP_FORALL:
5223 gfc_error ("ENTRY statement at %C cannot appear within "
5224 "a FORALL block");
5225 break;
5226 case COMP_WHERE:
5227 gfc_error ("ENTRY statement at %C cannot appear within "
5228 "a WHERE block");
5229 break;
5230 case COMP_CONTAINS:
5231 gfc_error ("ENTRY statement at %C cannot appear within "
5232 "a contained subprogram");
5233 break;
5234 default:
5235 gfc_internal_error ("gfc_match_entry(): Bad state");
5237 return MATCH_ERROR;
5240 module_procedure = gfc_current_ns->parent != NULL
5241 && gfc_current_ns->parent->proc_name
5242 && gfc_current_ns->parent->proc_name->attr.flavor
5243 == FL_MODULE;
5245 if (gfc_current_ns->parent != NULL
5246 && gfc_current_ns->parent->proc_name
5247 && !module_procedure)
5249 gfc_error("ENTRY statement at %C cannot appear in a "
5250 "contained procedure");
5251 return MATCH_ERROR;
5254 /* Module function entries need special care in get_proc_name
5255 because previous references within the function will have
5256 created symbols attached to the current namespace. */
5257 if (get_proc_name (name, &entry,
5258 gfc_current_ns->parent != NULL
5259 && module_procedure))
5260 return MATCH_ERROR;
5262 proc = gfc_current_block ();
5264 /* Make sure that it isn't already declared as BIND(C). If it is, it
5265 must have been marked BIND(C) with a BIND(C) attribute and that is
5266 not allowed for procedures. */
5267 if (entry->attr.is_bind_c == 1)
5269 entry->attr.is_bind_c = 0;
5270 if (entry->old_symbol != NULL)
5271 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5272 "variables or common blocks",
5273 &(entry->old_symbol->declared_at));
5274 else
5275 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5276 "variables or common blocks", &gfc_current_locus);
5279 /* Check what next non-whitespace character is so we can tell if there
5280 is the required parens if we have a BIND(C). */
5281 gfc_gobble_whitespace ();
5282 peek_char = gfc_peek_ascii_char ();
5284 if (state == COMP_SUBROUTINE)
5286 /* An entry in a subroutine. */
5287 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
5288 return MATCH_ERROR;
5290 m = gfc_match_formal_arglist (entry, 0, 1);
5291 if (m != MATCH_YES)
5292 return MATCH_ERROR;
5294 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5295 never be an internal procedure. */
5296 is_bind_c = gfc_match_bind_c (entry, true);
5297 if (is_bind_c == MATCH_ERROR)
5298 return MATCH_ERROR;
5299 if (is_bind_c == MATCH_YES)
5301 if (peek_char != '(')
5303 gfc_error ("Missing required parentheses before BIND(C) at %C");
5304 return MATCH_ERROR;
5306 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
5307 == FAILURE)
5308 return MATCH_ERROR;
5311 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5312 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
5313 return MATCH_ERROR;
5315 else
5317 /* An entry in a function.
5318 We need to take special care because writing
5319 ENTRY f()
5321 ENTRY f
5322 is allowed, whereas
5323 ENTRY f() RESULT (r)
5324 can't be written as
5325 ENTRY f RESULT (r). */
5326 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
5327 return MATCH_ERROR;
5329 old_loc = gfc_current_locus;
5330 if (gfc_match_eos () == MATCH_YES)
5332 gfc_current_locus = old_loc;
5333 /* Match the empty argument list, and add the interface to
5334 the symbol. */
5335 m = gfc_match_formal_arglist (entry, 0, 1);
5337 else
5338 m = gfc_match_formal_arglist (entry, 0, 0);
5340 if (m != MATCH_YES)
5341 return MATCH_ERROR;
5343 result = NULL;
5345 if (gfc_match_eos () == MATCH_YES)
5347 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5348 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5349 return MATCH_ERROR;
5351 entry->result = entry;
5353 else
5355 m = gfc_match_suffix (entry, &result);
5356 if (m == MATCH_NO)
5357 gfc_syntax_error (ST_ENTRY);
5358 if (m != MATCH_YES)
5359 return MATCH_ERROR;
5361 if (result)
5363 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
5364 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
5365 || gfc_add_function (&entry->attr, result->name, NULL)
5366 == FAILURE)
5367 return MATCH_ERROR;
5368 entry->result = result;
5370 else
5372 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5373 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5374 return MATCH_ERROR;
5375 entry->result = entry;
5380 if (gfc_match_eos () != MATCH_YES)
5382 gfc_syntax_error (ST_ENTRY);
5383 return MATCH_ERROR;
5386 entry->attr.recursive = proc->attr.recursive;
5387 entry->attr.elemental = proc->attr.elemental;
5388 entry->attr.pure = proc->attr.pure;
5390 el = gfc_get_entry_list ();
5391 el->sym = entry;
5392 el->next = gfc_current_ns->entries;
5393 gfc_current_ns->entries = el;
5394 if (el->next)
5395 el->id = el->next->id + 1;
5396 else
5397 el->id = 1;
5399 new_st.op = EXEC_ENTRY;
5400 new_st.ext.entry = el;
5402 return MATCH_YES;
5406 /* Match a subroutine statement, including optional prefixes. */
5408 match
5409 gfc_match_subroutine (void)
5411 char name[GFC_MAX_SYMBOL_LEN + 1];
5412 gfc_symbol *sym;
5413 match m;
5414 match is_bind_c;
5415 char peek_char;
5416 bool allow_binding_name;
5418 if (gfc_current_state () != COMP_NONE
5419 && gfc_current_state () != COMP_INTERFACE
5420 && gfc_current_state () != COMP_CONTAINS)
5421 return MATCH_NO;
5423 m = gfc_match_prefix (NULL);
5424 if (m != MATCH_YES)
5425 return m;
5427 m = gfc_match ("subroutine% %n", name);
5428 if (m != MATCH_YES)
5429 return m;
5431 if (get_proc_name (name, &sym, false))
5432 return MATCH_ERROR;
5434 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5435 the symbol existed before. */
5436 sym->declared_at = gfc_current_locus;
5438 if (add_hidden_procptr_result (sym) == SUCCESS)
5439 sym = sym->result;
5441 gfc_new_block = sym;
5443 /* Check what next non-whitespace character is so we can tell if there
5444 is the required parens if we have a BIND(C). */
5445 gfc_gobble_whitespace ();
5446 peek_char = gfc_peek_ascii_char ();
5448 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5449 return MATCH_ERROR;
5451 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5452 return MATCH_ERROR;
5454 /* Make sure that it isn't already declared as BIND(C). If it is, it
5455 must have been marked BIND(C) with a BIND(C) attribute and that is
5456 not allowed for procedures. */
5457 if (sym->attr.is_bind_c == 1)
5459 sym->attr.is_bind_c = 0;
5460 if (sym->old_symbol != NULL)
5461 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5462 "variables or common blocks",
5463 &(sym->old_symbol->declared_at));
5464 else
5465 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5466 "variables or common blocks", &gfc_current_locus);
5469 /* C binding names are not allowed for internal procedures. */
5470 if (gfc_current_state () == COMP_CONTAINS
5471 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5472 allow_binding_name = false;
5473 else
5474 allow_binding_name = true;
5476 /* Here, we are just checking if it has the bind(c) attribute, and if
5477 so, then we need to make sure it's all correct. If it doesn't,
5478 we still need to continue matching the rest of the subroutine line. */
5479 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5480 if (is_bind_c == MATCH_ERROR)
5482 /* There was an attempt at the bind(c), but it was wrong. An
5483 error message should have been printed w/in the gfc_match_bind_c
5484 so here we'll just return the MATCH_ERROR. */
5485 return MATCH_ERROR;
5488 if (is_bind_c == MATCH_YES)
5490 /* The following is allowed in the Fortran 2008 draft. */
5491 if (gfc_current_state () == COMP_CONTAINS
5492 && sym->ns->proc_name->attr.flavor != FL_MODULE
5493 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5494 "at %L may not be specified for an internal "
5495 "procedure", &gfc_current_locus)
5496 == FAILURE)
5497 return MATCH_ERROR;
5499 if (peek_char != '(')
5501 gfc_error ("Missing required parentheses before BIND(C) at %C");
5502 return MATCH_ERROR;
5504 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5505 == FAILURE)
5506 return MATCH_ERROR;
5509 if (gfc_match_eos () != MATCH_YES)
5511 gfc_syntax_error (ST_SUBROUTINE);
5512 return MATCH_ERROR;
5515 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5516 return MATCH_ERROR;
5518 /* Warn if it has the same name as an intrinsic. */
5519 warn_intrinsic_shadow (sym, false);
5521 return MATCH_YES;
5525 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5526 given, and set the binding label in either the given symbol (if not
5527 NULL), or in the current_ts. The symbol may be NULL because we may
5528 encounter the BIND(C) before the declaration itself. Return
5529 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5530 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5531 or MATCH_YES if the specifier was correct and the binding label and
5532 bind(c) fields were set correctly for the given symbol or the
5533 current_ts. If allow_binding_name is false, no binding name may be
5534 given. */
5536 match
5537 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5539 /* binding label, if exists */
5540 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
5541 match double_quote;
5542 match single_quote;
5544 /* Initialize the flag that specifies whether we encountered a NAME=
5545 specifier or not. */
5546 has_name_equals = 0;
5548 /* Init the first char to nil so we can catch if we don't have
5549 the label (name attr) or the symbol name yet. */
5550 binding_label[0] = '\0';
5552 /* This much we have to be able to match, in this order, if
5553 there is a bind(c) label. */
5554 if (gfc_match (" bind ( c ") != MATCH_YES)
5555 return MATCH_NO;
5557 /* Now see if there is a binding label, or if we've reached the
5558 end of the bind(c) attribute without one. */
5559 if (gfc_match_char (',') == MATCH_YES)
5561 if (gfc_match (" name = ") != MATCH_YES)
5563 gfc_error ("Syntax error in NAME= specifier for binding label "
5564 "at %C");
5565 /* should give an error message here */
5566 return MATCH_ERROR;
5569 has_name_equals = 1;
5571 /* Get the opening quote. */
5572 double_quote = MATCH_YES;
5573 single_quote = MATCH_YES;
5574 double_quote = gfc_match_char ('"');
5575 if (double_quote != MATCH_YES)
5576 single_quote = gfc_match_char ('\'');
5577 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5579 gfc_error ("Syntax error in NAME= specifier for binding label "
5580 "at %C");
5581 return MATCH_ERROR;
5584 /* Grab the binding label, using functions that will not lower
5585 case the names automatically. */
5586 if (gfc_match_name_C (binding_label) != MATCH_YES)
5587 return MATCH_ERROR;
5589 /* Get the closing quotation. */
5590 if (double_quote == MATCH_YES)
5592 if (gfc_match_char ('"') != MATCH_YES)
5594 gfc_error ("Missing closing quote '\"' for binding label at %C");
5595 /* User started string with '"' so looked to match it. */
5596 return MATCH_ERROR;
5599 else
5601 if (gfc_match_char ('\'') != MATCH_YES)
5603 gfc_error ("Missing closing quote '\'' for binding label at %C");
5604 /* User started string with "'" char. */
5605 return MATCH_ERROR;
5610 /* Get the required right paren. */
5611 if (gfc_match_char (')') != MATCH_YES)
5613 gfc_error ("Missing closing paren for binding label at %C");
5614 return MATCH_ERROR;
5617 if (has_name_equals && !allow_binding_name)
5619 gfc_error ("No binding name is allowed in BIND(C) at %C");
5620 return MATCH_ERROR;
5623 if (has_name_equals && sym != NULL && sym->attr.dummy)
5625 gfc_error ("For dummy procedure %s, no binding name is "
5626 "allowed in BIND(C) at %C", sym->name);
5627 return MATCH_ERROR;
5631 /* Save the binding label to the symbol. If sym is null, we're
5632 probably matching the typespec attributes of a declaration and
5633 haven't gotten the name yet, and therefore, no symbol yet. */
5634 if (binding_label[0] != '\0')
5636 if (sym != NULL)
5638 strcpy (sym->binding_label, binding_label);
5640 else
5641 strcpy (curr_binding_label, binding_label);
5643 else if (allow_binding_name)
5645 /* No binding label, but if symbol isn't null, we
5646 can set the label for it here.
5647 If name="" or allow_binding_name is false, no C binding name is
5648 created. */
5649 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5650 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
5653 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5654 && current_interface.type == INTERFACE_ABSTRACT)
5656 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5657 return MATCH_ERROR;
5660 return MATCH_YES;
5664 /* Return nonzero if we're currently compiling a contained procedure. */
5666 static int
5667 contained_procedure (void)
5669 gfc_state_data *s = gfc_state_stack;
5671 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5672 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5673 return 1;
5675 return 0;
5678 /* Set the kind of each enumerator. The kind is selected such that it is
5679 interoperable with the corresponding C enumeration type, making
5680 sure that -fshort-enums is honored. */
5682 static void
5683 set_enum_kind(void)
5685 enumerator_history *current_history = NULL;
5686 int kind;
5687 int i;
5689 if (max_enum == NULL || enum_history == NULL)
5690 return;
5692 if (!flag_short_enums)
5693 return;
5695 i = 0;
5698 kind = gfc_integer_kinds[i++].kind;
5700 while (kind < gfc_c_int_kind
5701 && gfc_check_integer_range (max_enum->initializer->value.integer,
5702 kind) != ARITH_OK);
5704 current_history = enum_history;
5705 while (current_history != NULL)
5707 current_history->sym->ts.kind = kind;
5708 current_history = current_history->next;
5713 /* Match any of the various end-block statements. Returns the type of
5714 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5715 and END BLOCK statements cannot be replaced by a single END statement. */
5717 match
5718 gfc_match_end (gfc_statement *st)
5720 char name[GFC_MAX_SYMBOL_LEN + 1];
5721 gfc_compile_state state;
5722 locus old_loc;
5723 const char *block_name;
5724 const char *target;
5725 int eos_ok;
5726 match m;
5728 old_loc = gfc_current_locus;
5729 if (gfc_match ("end") != MATCH_YES)
5730 return MATCH_NO;
5732 state = gfc_current_state ();
5733 block_name = gfc_current_block () == NULL
5734 ? NULL : gfc_current_block ()->name;
5736 switch (state)
5738 case COMP_ASSOCIATE:
5739 case COMP_BLOCK:
5740 if (!strcmp (block_name, "block@"))
5741 block_name = NULL;
5742 break;
5744 case COMP_CONTAINS:
5745 case COMP_DERIVED_CONTAINS:
5746 state = gfc_state_stack->previous->state;
5747 block_name = gfc_state_stack->previous->sym == NULL
5748 ? NULL : gfc_state_stack->previous->sym->name;
5749 break;
5751 default:
5752 break;
5755 switch (state)
5757 case COMP_NONE:
5758 case COMP_PROGRAM:
5759 *st = ST_END_PROGRAM;
5760 target = " program";
5761 eos_ok = 1;
5762 break;
5764 case COMP_SUBROUTINE:
5765 *st = ST_END_SUBROUTINE;
5766 target = " subroutine";
5767 eos_ok = !contained_procedure ();
5768 break;
5770 case COMP_FUNCTION:
5771 *st = ST_END_FUNCTION;
5772 target = " function";
5773 eos_ok = !contained_procedure ();
5774 break;
5776 case COMP_BLOCK_DATA:
5777 *st = ST_END_BLOCK_DATA;
5778 target = " block data";
5779 eos_ok = 1;
5780 break;
5782 case COMP_MODULE:
5783 *st = ST_END_MODULE;
5784 target = " module";
5785 eos_ok = 1;
5786 break;
5788 case COMP_INTERFACE:
5789 *st = ST_END_INTERFACE;
5790 target = " interface";
5791 eos_ok = 0;
5792 break;
5794 case COMP_DERIVED:
5795 case COMP_DERIVED_CONTAINS:
5796 *st = ST_END_TYPE;
5797 target = " type";
5798 eos_ok = 0;
5799 break;
5801 case COMP_ASSOCIATE:
5802 *st = ST_END_ASSOCIATE;
5803 target = " associate";
5804 eos_ok = 0;
5805 break;
5807 case COMP_BLOCK:
5808 *st = ST_END_BLOCK;
5809 target = " block";
5810 eos_ok = 0;
5811 break;
5813 case COMP_IF:
5814 *st = ST_ENDIF;
5815 target = " if";
5816 eos_ok = 0;
5817 break;
5819 case COMP_DO:
5820 *st = ST_ENDDO;
5821 target = " do";
5822 eos_ok = 0;
5823 break;
5825 case COMP_CRITICAL:
5826 *st = ST_END_CRITICAL;
5827 target = " critical";
5828 eos_ok = 0;
5829 break;
5831 case COMP_SELECT:
5832 case COMP_SELECT_TYPE:
5833 *st = ST_END_SELECT;
5834 target = " select";
5835 eos_ok = 0;
5836 break;
5838 case COMP_FORALL:
5839 *st = ST_END_FORALL;
5840 target = " forall";
5841 eos_ok = 0;
5842 break;
5844 case COMP_WHERE:
5845 *st = ST_END_WHERE;
5846 target = " where";
5847 eos_ok = 0;
5848 break;
5850 case COMP_ENUM:
5851 *st = ST_END_ENUM;
5852 target = " enum";
5853 eos_ok = 0;
5854 last_initializer = NULL;
5855 set_enum_kind ();
5856 gfc_free_enum_history ();
5857 break;
5859 default:
5860 gfc_error ("Unexpected END statement at %C");
5861 goto cleanup;
5864 if (gfc_match_eos () == MATCH_YES)
5866 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
5868 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: END statement "
5869 "instead of %s statement at %L",
5870 gfc_ascii_statement (*st), &old_loc) == FAILURE)
5871 goto cleanup;
5873 else if (!eos_ok)
5875 /* We would have required END [something]. */
5876 gfc_error ("%s statement expected at %L",
5877 gfc_ascii_statement (*st), &old_loc);
5878 goto cleanup;
5881 return MATCH_YES;
5884 /* Verify that we've got the sort of end-block that we're expecting. */
5885 if (gfc_match (target) != MATCH_YES)
5887 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5888 goto cleanup;
5891 /* If we're at the end, make sure a block name wasn't required. */
5892 if (gfc_match_eos () == MATCH_YES)
5895 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5896 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
5897 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
5898 return MATCH_YES;
5900 if (!block_name)
5901 return MATCH_YES;
5903 gfc_error ("Expected block name of '%s' in %s statement at %C",
5904 block_name, gfc_ascii_statement (*st));
5906 return MATCH_ERROR;
5909 /* END INTERFACE has a special handler for its several possible endings. */
5910 if (*st == ST_END_INTERFACE)
5911 return gfc_match_end_interface ();
5913 /* We haven't hit the end of statement, so what is left must be an
5914 end-name. */
5915 m = gfc_match_space ();
5916 if (m == MATCH_YES)
5917 m = gfc_match_name (name);
5919 if (m == MATCH_NO)
5920 gfc_error ("Expected terminating name at %C");
5921 if (m != MATCH_YES)
5922 goto cleanup;
5924 if (block_name == NULL)
5925 goto syntax;
5927 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
5929 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5930 gfc_ascii_statement (*st));
5931 goto cleanup;
5933 /* Procedure pointer as function result. */
5934 else if (strcmp (block_name, "ppr@") == 0
5935 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
5937 gfc_error ("Expected label '%s' for %s statement at %C",
5938 gfc_current_block ()->ns->proc_name->name,
5939 gfc_ascii_statement (*st));
5940 goto cleanup;
5943 if (gfc_match_eos () == MATCH_YES)
5944 return MATCH_YES;
5946 syntax:
5947 gfc_syntax_error (*st);
5949 cleanup:
5950 gfc_current_locus = old_loc;
5951 return MATCH_ERROR;
5956 /***************** Attribute declaration statements ****************/
5958 /* Set the attribute of a single variable. */
5960 static match
5961 attr_decl1 (void)
5963 char name[GFC_MAX_SYMBOL_LEN + 1];
5964 gfc_array_spec *as;
5965 gfc_symbol *sym;
5966 locus var_locus;
5967 match m;
5969 as = NULL;
5971 m = gfc_match_name (name);
5972 if (m != MATCH_YES)
5973 goto cleanup;
5975 if (find_special (name, &sym, false))
5976 return MATCH_ERROR;
5978 var_locus = gfc_current_locus;
5980 /* Deal with possible array specification for certain attributes. */
5981 if (current_attr.dimension
5982 || current_attr.codimension
5983 || current_attr.allocatable
5984 || current_attr.pointer
5985 || current_attr.target)
5987 m = gfc_match_array_spec (&as, !current_attr.codimension,
5988 !current_attr.dimension
5989 && !current_attr.pointer
5990 && !current_attr.target);
5991 if (m == MATCH_ERROR)
5992 goto cleanup;
5994 if (current_attr.dimension && m == MATCH_NO)
5996 gfc_error ("Missing array specification at %L in DIMENSION "
5997 "statement", &var_locus);
5998 m = MATCH_ERROR;
5999 goto cleanup;
6002 if (current_attr.dimension && sym->value)
6004 gfc_error ("Dimensions specified for %s at %L after its "
6005 "initialisation", sym->name, &var_locus);
6006 m = MATCH_ERROR;
6007 goto cleanup;
6010 if (current_attr.codimension && m == MATCH_NO)
6012 gfc_error ("Missing array specification at %L in CODIMENSION "
6013 "statement", &var_locus);
6014 m = MATCH_ERROR;
6015 goto cleanup;
6018 if ((current_attr.allocatable || current_attr.pointer)
6019 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6021 gfc_error ("Array specification must be deferred at %L", &var_locus);
6022 m = MATCH_ERROR;
6023 goto cleanup;
6027 /* Update symbol table. DIMENSION attribute is set in
6028 gfc_set_array_spec(). For CLASS variables, this must be applied
6029 to the first component, or '_data' field. */
6030 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6032 if (gfc_copy_attr (&CLASS_DATA (sym)->attr, &current_attr, &var_locus)
6033 == FAILURE)
6035 m = MATCH_ERROR;
6036 goto cleanup;
6039 else
6041 if (current_attr.dimension == 0 && current_attr.codimension == 0
6042 && gfc_copy_attr (&sym->attr, &current_attr, &var_locus) == FAILURE)
6044 m = MATCH_ERROR;
6045 goto cleanup;
6049 if (sym->ts.type == BT_CLASS && !sym->attr.class_ok
6050 && (sym->attr.class_ok = sym->attr.class_ok || current_attr.allocatable
6051 || current_attr.pointer))
6052 gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
6054 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
6056 m = MATCH_ERROR;
6057 goto cleanup;
6060 if (sym->attr.cray_pointee && sym->as != NULL)
6062 /* Fix the array spec. */
6063 m = gfc_mod_pointee_as (sym->as);
6064 if (m == MATCH_ERROR)
6065 goto cleanup;
6068 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
6070 m = MATCH_ERROR;
6071 goto cleanup;
6074 if ((current_attr.external || current_attr.intrinsic)
6075 && sym->attr.flavor != FL_PROCEDURE
6076 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6078 m = MATCH_ERROR;
6079 goto cleanup;
6082 add_hidden_procptr_result (sym);
6084 return MATCH_YES;
6086 cleanup:
6087 gfc_free_array_spec (as);
6088 return m;
6092 /* Generic attribute declaration subroutine. Used for attributes that
6093 just have a list of names. */
6095 static match
6096 attr_decl (void)
6098 match m;
6100 /* Gobble the optional double colon, by simply ignoring the result
6101 of gfc_match(). */
6102 gfc_match (" ::");
6104 for (;;)
6106 m = attr_decl1 ();
6107 if (m != MATCH_YES)
6108 break;
6110 if (gfc_match_eos () == MATCH_YES)
6112 m = MATCH_YES;
6113 break;
6116 if (gfc_match_char (',') != MATCH_YES)
6118 gfc_error ("Unexpected character in variable list at %C");
6119 m = MATCH_ERROR;
6120 break;
6124 return m;
6128 /* This routine matches Cray Pointer declarations of the form:
6129 pointer ( <pointer>, <pointee> )
6131 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6132 The pointer, if already declared, should be an integer. Otherwise, we
6133 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6134 be either a scalar, or an array declaration. No space is allocated for
6135 the pointee. For the statement
6136 pointer (ipt, ar(10))
6137 any subsequent uses of ar will be translated (in C-notation) as
6138 ar(i) => ((<type> *) ipt)(i)
6139 After gimplification, pointee variable will disappear in the code. */
6141 static match
6142 cray_pointer_decl (void)
6144 match m;
6145 gfc_array_spec *as = NULL;
6146 gfc_symbol *cptr; /* Pointer symbol. */
6147 gfc_symbol *cpte; /* Pointee symbol. */
6148 locus var_locus;
6149 bool done = false;
6151 while (!done)
6153 if (gfc_match_char ('(') != MATCH_YES)
6155 gfc_error ("Expected '(' at %C");
6156 return MATCH_ERROR;
6159 /* Match pointer. */
6160 var_locus = gfc_current_locus;
6161 gfc_clear_attr (&current_attr);
6162 gfc_add_cray_pointer (&current_attr, &var_locus);
6163 current_ts.type = BT_INTEGER;
6164 current_ts.kind = gfc_index_integer_kind;
6166 m = gfc_match_symbol (&cptr, 0);
6167 if (m != MATCH_YES)
6169 gfc_error ("Expected variable name at %C");
6170 return m;
6173 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
6174 return MATCH_ERROR;
6176 gfc_set_sym_referenced (cptr);
6178 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6180 cptr->ts.type = BT_INTEGER;
6181 cptr->ts.kind = gfc_index_integer_kind;
6183 else if (cptr->ts.type != BT_INTEGER)
6185 gfc_error ("Cray pointer at %C must be an integer");
6186 return MATCH_ERROR;
6188 else if (cptr->ts.kind < gfc_index_integer_kind)
6189 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6190 " memory addresses require %d bytes",
6191 cptr->ts.kind, gfc_index_integer_kind);
6193 if (gfc_match_char (',') != MATCH_YES)
6195 gfc_error ("Expected \",\" at %C");
6196 return MATCH_ERROR;
6199 /* Match Pointee. */
6200 var_locus = gfc_current_locus;
6201 gfc_clear_attr (&current_attr);
6202 gfc_add_cray_pointee (&current_attr, &var_locus);
6203 current_ts.type = BT_UNKNOWN;
6204 current_ts.kind = 0;
6206 m = gfc_match_symbol (&cpte, 0);
6207 if (m != MATCH_YES)
6209 gfc_error ("Expected variable name at %C");
6210 return m;
6213 /* Check for an optional array spec. */
6214 m = gfc_match_array_spec (&as, true, false);
6215 if (m == MATCH_ERROR)
6217 gfc_free_array_spec (as);
6218 return m;
6220 else if (m == MATCH_NO)
6222 gfc_free_array_spec (as);
6223 as = NULL;
6226 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
6227 return MATCH_ERROR;
6229 gfc_set_sym_referenced (cpte);
6231 if (cpte->as == NULL)
6233 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
6234 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6236 else if (as != NULL)
6238 gfc_error ("Duplicate array spec for Cray pointee at %C");
6239 gfc_free_array_spec (as);
6240 return MATCH_ERROR;
6243 as = NULL;
6245 if (cpte->as != NULL)
6247 /* Fix array spec. */
6248 m = gfc_mod_pointee_as (cpte->as);
6249 if (m == MATCH_ERROR)
6250 return m;
6253 /* Point the Pointee at the Pointer. */
6254 cpte->cp_pointer = cptr;
6256 if (gfc_match_char (')') != MATCH_YES)
6258 gfc_error ("Expected \")\" at %C");
6259 return MATCH_ERROR;
6261 m = gfc_match_char (',');
6262 if (m != MATCH_YES)
6263 done = true; /* Stop searching for more declarations. */
6267 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6268 || gfc_match_eos () != MATCH_YES)
6270 gfc_error ("Expected \",\" or end of statement at %C");
6271 return MATCH_ERROR;
6273 return MATCH_YES;
6277 match
6278 gfc_match_external (void)
6281 gfc_clear_attr (&current_attr);
6282 current_attr.external = 1;
6284 return attr_decl ();
6288 match
6289 gfc_match_intent (void)
6291 sym_intent intent;
6293 /* This is not allowed within a BLOCK construct! */
6294 if (gfc_current_state () == COMP_BLOCK)
6296 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6297 return MATCH_ERROR;
6300 intent = match_intent_spec ();
6301 if (intent == INTENT_UNKNOWN)
6302 return MATCH_ERROR;
6304 gfc_clear_attr (&current_attr);
6305 current_attr.intent = intent;
6307 return attr_decl ();
6311 match
6312 gfc_match_intrinsic (void)
6315 gfc_clear_attr (&current_attr);
6316 current_attr.intrinsic = 1;
6318 return attr_decl ();
6322 match
6323 gfc_match_optional (void)
6325 /* This is not allowed within a BLOCK construct! */
6326 if (gfc_current_state () == COMP_BLOCK)
6328 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6329 return MATCH_ERROR;
6332 gfc_clear_attr (&current_attr);
6333 current_attr.optional = 1;
6335 return attr_decl ();
6339 match
6340 gfc_match_pointer (void)
6342 gfc_gobble_whitespace ();
6343 if (gfc_peek_ascii_char () == '(')
6345 if (!gfc_option.flag_cray_pointer)
6347 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6348 "flag");
6349 return MATCH_ERROR;
6351 return cray_pointer_decl ();
6353 else
6355 gfc_clear_attr (&current_attr);
6356 current_attr.pointer = 1;
6358 return attr_decl ();
6363 match
6364 gfc_match_allocatable (void)
6366 gfc_clear_attr (&current_attr);
6367 current_attr.allocatable = 1;
6369 return attr_decl ();
6373 match
6374 gfc_match_codimension (void)
6376 gfc_clear_attr (&current_attr);
6377 current_attr.codimension = 1;
6379 return attr_decl ();
6383 match
6384 gfc_match_contiguous (void)
6386 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTIGUOUS statement at %C")
6387 == FAILURE)
6388 return MATCH_ERROR;
6390 gfc_clear_attr (&current_attr);
6391 current_attr.contiguous = 1;
6393 return attr_decl ();
6397 match
6398 gfc_match_dimension (void)
6400 gfc_clear_attr (&current_attr);
6401 current_attr.dimension = 1;
6403 return attr_decl ();
6407 match
6408 gfc_match_target (void)
6410 gfc_clear_attr (&current_attr);
6411 current_attr.target = 1;
6413 return attr_decl ();
6417 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6418 statement. */
6420 static match
6421 access_attr_decl (gfc_statement st)
6423 char name[GFC_MAX_SYMBOL_LEN + 1];
6424 interface_type type;
6425 gfc_user_op *uop;
6426 gfc_symbol *sym;
6427 gfc_intrinsic_op op;
6428 match m;
6430 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6431 goto done;
6433 for (;;)
6435 m = gfc_match_generic_spec (&type, name, &op);
6436 if (m == MATCH_NO)
6437 goto syntax;
6438 if (m == MATCH_ERROR)
6439 return MATCH_ERROR;
6441 switch (type)
6443 case INTERFACE_NAMELESS:
6444 case INTERFACE_ABSTRACT:
6445 goto syntax;
6447 case INTERFACE_GENERIC:
6448 if (gfc_get_symbol (name, NULL, &sym))
6449 goto done;
6451 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
6452 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6453 sym->name, NULL) == FAILURE)
6454 return MATCH_ERROR;
6456 break;
6458 case INTERFACE_INTRINSIC_OP:
6459 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6461 gfc_current_ns->operator_access[op] =
6462 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6464 else
6466 gfc_error ("Access specification of the %s operator at %C has "
6467 "already been specified", gfc_op2string (op));
6468 goto done;
6471 break;
6473 case INTERFACE_USER_OP:
6474 uop = gfc_get_uop (name);
6476 if (uop->access == ACCESS_UNKNOWN)
6478 uop->access = (st == ST_PUBLIC)
6479 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6481 else
6483 gfc_error ("Access specification of the .%s. operator at %C "
6484 "has already been specified", sym->name);
6485 goto done;
6488 break;
6491 if (gfc_match_char (',') == MATCH_NO)
6492 break;
6495 if (gfc_match_eos () != MATCH_YES)
6496 goto syntax;
6497 return MATCH_YES;
6499 syntax:
6500 gfc_syntax_error (st);
6502 done:
6503 return MATCH_ERROR;
6507 match
6508 gfc_match_protected (void)
6510 gfc_symbol *sym;
6511 match m;
6513 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6515 gfc_error ("PROTECTED at %C only allowed in specification "
6516 "part of a module");
6517 return MATCH_ERROR;
6521 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
6522 == FAILURE)
6523 return MATCH_ERROR;
6525 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6527 return MATCH_ERROR;
6530 if (gfc_match_eos () == MATCH_YES)
6531 goto syntax;
6533 for(;;)
6535 m = gfc_match_symbol (&sym, 0);
6536 switch (m)
6538 case MATCH_YES:
6539 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6540 == FAILURE)
6541 return MATCH_ERROR;
6542 goto next_item;
6544 case MATCH_NO:
6545 break;
6547 case MATCH_ERROR:
6548 return MATCH_ERROR;
6551 next_item:
6552 if (gfc_match_eos () == MATCH_YES)
6553 break;
6554 if (gfc_match_char (',') != MATCH_YES)
6555 goto syntax;
6558 return MATCH_YES;
6560 syntax:
6561 gfc_error ("Syntax error in PROTECTED statement at %C");
6562 return MATCH_ERROR;
6566 /* The PRIVATE statement is a bit weird in that it can be an attribute
6567 declaration, but also works as a standalone statement inside of a
6568 type declaration or a module. */
6570 match
6571 gfc_match_private (gfc_statement *st)
6574 if (gfc_match ("private") != MATCH_YES)
6575 return MATCH_NO;
6577 if (gfc_current_state () != COMP_MODULE
6578 && !(gfc_current_state () == COMP_DERIVED
6579 && gfc_state_stack->previous
6580 && gfc_state_stack->previous->state == COMP_MODULE)
6581 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6582 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6583 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6585 gfc_error ("PRIVATE statement at %C is only allowed in the "
6586 "specification part of a module");
6587 return MATCH_ERROR;
6590 if (gfc_current_state () == COMP_DERIVED)
6592 if (gfc_match_eos () == MATCH_YES)
6594 *st = ST_PRIVATE;
6595 return MATCH_YES;
6598 gfc_syntax_error (ST_PRIVATE);
6599 return MATCH_ERROR;
6602 if (gfc_match_eos () == MATCH_YES)
6604 *st = ST_PRIVATE;
6605 return MATCH_YES;
6608 *st = ST_ATTR_DECL;
6609 return access_attr_decl (ST_PRIVATE);
6613 match
6614 gfc_match_public (gfc_statement *st)
6617 if (gfc_match ("public") != MATCH_YES)
6618 return MATCH_NO;
6620 if (gfc_current_state () != COMP_MODULE)
6622 gfc_error ("PUBLIC statement at %C is only allowed in the "
6623 "specification part of a module");
6624 return MATCH_ERROR;
6627 if (gfc_match_eos () == MATCH_YES)
6629 *st = ST_PUBLIC;
6630 return MATCH_YES;
6633 *st = ST_ATTR_DECL;
6634 return access_attr_decl (ST_PUBLIC);
6638 /* Workhorse for gfc_match_parameter. */
6640 static match
6641 do_parm (void)
6643 gfc_symbol *sym;
6644 gfc_expr *init;
6645 match m;
6646 gfc_try t;
6648 m = gfc_match_symbol (&sym, 0);
6649 if (m == MATCH_NO)
6650 gfc_error ("Expected variable name at %C in PARAMETER statement");
6652 if (m != MATCH_YES)
6653 return m;
6655 if (gfc_match_char ('=') == MATCH_NO)
6657 gfc_error ("Expected = sign in PARAMETER statement at %C");
6658 return MATCH_ERROR;
6661 m = gfc_match_init_expr (&init);
6662 if (m == MATCH_NO)
6663 gfc_error ("Expected expression at %C in PARAMETER statement");
6664 if (m != MATCH_YES)
6665 return m;
6667 if (sym->ts.type == BT_UNKNOWN
6668 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6670 m = MATCH_ERROR;
6671 goto cleanup;
6674 if (gfc_check_assign_symbol (sym, init) == FAILURE
6675 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6677 m = MATCH_ERROR;
6678 goto cleanup;
6681 if (sym->value)
6683 gfc_error ("Initializing already initialized variable at %C");
6684 m = MATCH_ERROR;
6685 goto cleanup;
6688 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
6689 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6691 cleanup:
6692 gfc_free_expr (init);
6693 return m;
6697 /* Match a parameter statement, with the weird syntax that these have. */
6699 match
6700 gfc_match_parameter (void)
6702 match m;
6704 if (gfc_match_char ('(') == MATCH_NO)
6705 return MATCH_NO;
6707 for (;;)
6709 m = do_parm ();
6710 if (m != MATCH_YES)
6711 break;
6713 if (gfc_match (" )%t") == MATCH_YES)
6714 break;
6716 if (gfc_match_char (',') != MATCH_YES)
6718 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6719 m = MATCH_ERROR;
6720 break;
6724 return m;
6728 /* Save statements have a special syntax. */
6730 match
6731 gfc_match_save (void)
6733 char n[GFC_MAX_SYMBOL_LEN+1];
6734 gfc_common_head *c;
6735 gfc_symbol *sym;
6736 match m;
6738 if (gfc_match_eos () == MATCH_YES)
6740 if (gfc_current_ns->seen_save)
6742 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6743 "follows previous SAVE statement")
6744 == FAILURE)
6745 return MATCH_ERROR;
6748 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6749 return MATCH_YES;
6752 if (gfc_current_ns->save_all)
6754 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6755 "blanket SAVE statement")
6756 == FAILURE)
6757 return MATCH_ERROR;
6760 gfc_match (" ::");
6762 for (;;)
6764 m = gfc_match_symbol (&sym, 0);
6765 switch (m)
6767 case MATCH_YES:
6768 if (gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
6769 &gfc_current_locus) == FAILURE)
6770 return MATCH_ERROR;
6771 goto next_item;
6773 case MATCH_NO:
6774 break;
6776 case MATCH_ERROR:
6777 return MATCH_ERROR;
6780 m = gfc_match (" / %n /", &n);
6781 if (m == MATCH_ERROR)
6782 return MATCH_ERROR;
6783 if (m == MATCH_NO)
6784 goto syntax;
6786 c = gfc_get_common (n, 0);
6787 c->saved = 1;
6789 gfc_current_ns->seen_save = 1;
6791 next_item:
6792 if (gfc_match_eos () == MATCH_YES)
6793 break;
6794 if (gfc_match_char (',') != MATCH_YES)
6795 goto syntax;
6798 return MATCH_YES;
6800 syntax:
6801 gfc_error ("Syntax error in SAVE statement at %C");
6802 return MATCH_ERROR;
6806 match
6807 gfc_match_value (void)
6809 gfc_symbol *sym;
6810 match m;
6812 /* This is not allowed within a BLOCK construct! */
6813 if (gfc_current_state () == COMP_BLOCK)
6815 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
6816 return MATCH_ERROR;
6819 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
6820 == FAILURE)
6821 return MATCH_ERROR;
6823 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6825 return MATCH_ERROR;
6828 if (gfc_match_eos () == MATCH_YES)
6829 goto syntax;
6831 for(;;)
6833 m = gfc_match_symbol (&sym, 0);
6834 switch (m)
6836 case MATCH_YES:
6837 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
6838 == FAILURE)
6839 return MATCH_ERROR;
6840 goto next_item;
6842 case MATCH_NO:
6843 break;
6845 case MATCH_ERROR:
6846 return MATCH_ERROR;
6849 next_item:
6850 if (gfc_match_eos () == MATCH_YES)
6851 break;
6852 if (gfc_match_char (',') != MATCH_YES)
6853 goto syntax;
6856 return MATCH_YES;
6858 syntax:
6859 gfc_error ("Syntax error in VALUE statement at %C");
6860 return MATCH_ERROR;
6864 match
6865 gfc_match_volatile (void)
6867 gfc_symbol *sym;
6868 match m;
6870 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
6871 == FAILURE)
6872 return MATCH_ERROR;
6874 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6876 return MATCH_ERROR;
6879 if (gfc_match_eos () == MATCH_YES)
6880 goto syntax;
6882 for(;;)
6884 /* VOLATILE is special because it can be added to host-associated
6885 symbols locally. Except for coarrays. */
6886 m = gfc_match_symbol (&sym, 1);
6887 switch (m)
6889 case MATCH_YES:
6890 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
6891 for variable in a BLOCK which is defined outside of the BLOCK. */
6892 if (sym->ns != gfc_current_ns && sym->attr.codimension)
6894 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
6895 "%C, which is use-/host-associated", sym->name);
6896 return MATCH_ERROR;
6898 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6899 == FAILURE)
6900 return MATCH_ERROR;
6901 goto next_item;
6903 case MATCH_NO:
6904 break;
6906 case MATCH_ERROR:
6907 return MATCH_ERROR;
6910 next_item:
6911 if (gfc_match_eos () == MATCH_YES)
6912 break;
6913 if (gfc_match_char (',') != MATCH_YES)
6914 goto syntax;
6917 return MATCH_YES;
6919 syntax:
6920 gfc_error ("Syntax error in VOLATILE statement at %C");
6921 return MATCH_ERROR;
6925 match
6926 gfc_match_asynchronous (void)
6928 gfc_symbol *sym;
6929 match m;
6931 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS statement at %C")
6932 == FAILURE)
6933 return MATCH_ERROR;
6935 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6937 return MATCH_ERROR;
6940 if (gfc_match_eos () == MATCH_YES)
6941 goto syntax;
6943 for(;;)
6945 /* ASYNCHRONOUS is special because it can be added to host-associated
6946 symbols locally. */
6947 m = gfc_match_symbol (&sym, 1);
6948 switch (m)
6950 case MATCH_YES:
6951 if (gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus)
6952 == FAILURE)
6953 return MATCH_ERROR;
6954 goto next_item;
6956 case MATCH_NO:
6957 break;
6959 case MATCH_ERROR:
6960 return MATCH_ERROR;
6963 next_item:
6964 if (gfc_match_eos () == MATCH_YES)
6965 break;
6966 if (gfc_match_char (',') != MATCH_YES)
6967 goto syntax;
6970 return MATCH_YES;
6972 syntax:
6973 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
6974 return MATCH_ERROR;
6978 /* Match a module procedure statement. Note that we have to modify
6979 symbols in the parent's namespace because the current one was there
6980 to receive symbols that are in an interface's formal argument list. */
6982 match
6983 gfc_match_modproc (void)
6985 char name[GFC_MAX_SYMBOL_LEN + 1];
6986 gfc_symbol *sym;
6987 match m;
6988 gfc_namespace *module_ns;
6989 gfc_interface *old_interface_head, *interface;
6991 if (gfc_state_stack->state != COMP_INTERFACE
6992 || gfc_state_stack->previous == NULL
6993 || current_interface.type == INTERFACE_NAMELESS
6994 || current_interface.type == INTERFACE_ABSTRACT)
6996 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6997 "interface");
6998 return MATCH_ERROR;
7001 module_ns = gfc_current_ns->parent;
7002 for (; module_ns; module_ns = module_ns->parent)
7003 if (module_ns->proc_name->attr.flavor == FL_MODULE
7004 || module_ns->proc_name->attr.flavor == FL_PROGRAM
7005 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7006 && !module_ns->proc_name->attr.contained))
7007 break;
7009 if (module_ns == NULL)
7010 return MATCH_ERROR;
7012 /* Store the current state of the interface. We will need it if we
7013 end up with a syntax error and need to recover. */
7014 old_interface_head = gfc_current_interface_head ();
7016 for (;;)
7018 locus old_locus = gfc_current_locus;
7019 bool last = false;
7021 m = gfc_match_name (name);
7022 if (m == MATCH_NO)
7023 goto syntax;
7024 if (m != MATCH_YES)
7025 return MATCH_ERROR;
7027 /* Check for syntax error before starting to add symbols to the
7028 current namespace. */
7029 if (gfc_match_eos () == MATCH_YES)
7030 last = true;
7031 if (!last && gfc_match_char (',') != MATCH_YES)
7032 goto syntax;
7034 /* Now we're sure the syntax is valid, we process this item
7035 further. */
7036 if (gfc_get_symbol (name, module_ns, &sym))
7037 return MATCH_ERROR;
7039 if (sym->attr.intrinsic)
7041 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7042 "PROCEDURE", &old_locus);
7043 return MATCH_ERROR;
7046 if (sym->attr.proc != PROC_MODULE
7047 && gfc_add_procedure (&sym->attr, PROC_MODULE,
7048 sym->name, NULL) == FAILURE)
7049 return MATCH_ERROR;
7051 if (gfc_add_interface (sym) == FAILURE)
7052 return MATCH_ERROR;
7054 sym->attr.mod_proc = 1;
7055 sym->declared_at = old_locus;
7057 if (last)
7058 break;
7061 return MATCH_YES;
7063 syntax:
7064 /* Restore the previous state of the interface. */
7065 interface = gfc_current_interface_head ();
7066 gfc_set_current_interface_head (old_interface_head);
7068 /* Free the new interfaces. */
7069 while (interface != old_interface_head)
7071 gfc_interface *i = interface->next;
7072 gfc_free (interface);
7073 interface = i;
7076 /* And issue a syntax error. */
7077 gfc_syntax_error (ST_MODULE_PROC);
7078 return MATCH_ERROR;
7082 /* Check a derived type that is being extended. */
7083 static gfc_symbol*
7084 check_extended_derived_type (char *name)
7086 gfc_symbol *extended;
7088 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7090 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7091 return NULL;
7094 if (!extended)
7096 gfc_error ("No such symbol in TYPE definition at %C");
7097 return NULL;
7100 if (extended->attr.flavor != FL_DERIVED)
7102 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7103 "derived type", name);
7104 return NULL;
7107 if (extended->attr.is_bind_c)
7109 gfc_error ("'%s' cannot be extended at %C because it "
7110 "is BIND(C)", extended->name);
7111 return NULL;
7114 if (extended->attr.sequence)
7116 gfc_error ("'%s' cannot be extended at %C because it "
7117 "is a SEQUENCE type", extended->name);
7118 return NULL;
7121 return extended;
7125 /* Match the optional attribute specifiers for a type declaration.
7126 Return MATCH_ERROR if an error is encountered in one of the handled
7127 attributes (public, private, bind(c)), MATCH_NO if what's found is
7128 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7129 checking on attribute conflicts needs to be done. */
7131 match
7132 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7134 /* See if the derived type is marked as private. */
7135 if (gfc_match (" , private") == MATCH_YES)
7137 if (gfc_current_state () != COMP_MODULE)
7139 gfc_error ("Derived type at %C can only be PRIVATE in the "
7140 "specification part of a module");
7141 return MATCH_ERROR;
7144 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
7145 return MATCH_ERROR;
7147 else if (gfc_match (" , public") == MATCH_YES)
7149 if (gfc_current_state () != COMP_MODULE)
7151 gfc_error ("Derived type at %C can only be PUBLIC in the "
7152 "specification part of a module");
7153 return MATCH_ERROR;
7156 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
7157 return MATCH_ERROR;
7159 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7161 /* If the type is defined to be bind(c) it then needs to make
7162 sure that all fields are interoperable. This will
7163 need to be a semantic check on the finished derived type.
7164 See 15.2.3 (lines 9-12) of F2003 draft. */
7165 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
7166 return MATCH_ERROR;
7168 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7170 else if (gfc_match (" , abstract") == MATCH_YES)
7172 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
7173 == FAILURE)
7174 return MATCH_ERROR;
7176 if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
7177 return MATCH_ERROR;
7179 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
7181 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
7182 return MATCH_ERROR;
7184 else
7185 return MATCH_NO;
7187 /* If we get here, something matched. */
7188 return MATCH_YES;
7192 /* Match the beginning of a derived type declaration. If a type name
7193 was the result of a function, then it is possible to have a symbol
7194 already to be known as a derived type yet have no components. */
7196 match
7197 gfc_match_derived_decl (void)
7199 char name[GFC_MAX_SYMBOL_LEN + 1];
7200 char parent[GFC_MAX_SYMBOL_LEN + 1];
7201 symbol_attribute attr;
7202 gfc_symbol *sym;
7203 gfc_symbol *extended;
7204 match m;
7205 match is_type_attr_spec = MATCH_NO;
7206 bool seen_attr = false;
7208 if (gfc_current_state () == COMP_DERIVED)
7209 return MATCH_NO;
7211 name[0] = '\0';
7212 parent[0] = '\0';
7213 gfc_clear_attr (&attr);
7214 extended = NULL;
7218 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7219 if (is_type_attr_spec == MATCH_ERROR)
7220 return MATCH_ERROR;
7221 if (is_type_attr_spec == MATCH_YES)
7222 seen_attr = true;
7223 } while (is_type_attr_spec == MATCH_YES);
7225 /* Deal with derived type extensions. The extension attribute has
7226 been added to 'attr' but now the parent type must be found and
7227 checked. */
7228 if (parent[0])
7229 extended = check_extended_derived_type (parent);
7231 if (parent[0] && !extended)
7232 return MATCH_ERROR;
7234 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7236 gfc_error ("Expected :: in TYPE definition at %C");
7237 return MATCH_ERROR;
7240 m = gfc_match (" %n%t", name);
7241 if (m != MATCH_YES)
7242 return m;
7244 /* Make sure the name is not the name of an intrinsic type. */
7245 if (gfc_is_intrinsic_typename (name))
7247 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7248 "type", name);
7249 return MATCH_ERROR;
7252 if (gfc_get_symbol (name, NULL, &sym))
7253 return MATCH_ERROR;
7255 if (sym->ts.type != BT_UNKNOWN)
7257 gfc_error ("Derived type name '%s' at %C already has a basic type "
7258 "of %s", sym->name, gfc_typename (&sym->ts));
7259 return MATCH_ERROR;
7262 /* The symbol may already have the derived attribute without the
7263 components. The ways this can happen is via a function
7264 definition, an INTRINSIC statement or a subtype in another
7265 derived type that is a pointer. The first part of the AND clause
7266 is true if the symbol is not the return value of a function. */
7267 if (sym->attr.flavor != FL_DERIVED
7268 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
7269 return MATCH_ERROR;
7271 if (sym->components != NULL || sym->attr.zero_comp)
7273 gfc_error ("Derived type definition of '%s' at %C has already been "
7274 "defined", sym->name);
7275 return MATCH_ERROR;
7278 if (attr.access != ACCESS_UNKNOWN
7279 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
7280 return MATCH_ERROR;
7282 /* See if the derived type was labeled as bind(c). */
7283 if (attr.is_bind_c != 0)
7284 sym->attr.is_bind_c = attr.is_bind_c;
7286 /* Construct the f2k_derived namespace if it is not yet there. */
7287 if (!sym->f2k_derived)
7288 sym->f2k_derived = gfc_get_namespace (NULL, 0);
7290 if (extended && !sym->components)
7292 gfc_component *p;
7293 gfc_symtree *st;
7295 /* Add the extended derived type as the first component. */
7296 gfc_add_component (sym, parent, &p);
7297 extended->refs++;
7298 gfc_set_sym_referenced (extended);
7300 p->ts.type = BT_DERIVED;
7301 p->ts.u.derived = extended;
7302 p->initializer = gfc_default_initializer (&p->ts);
7304 /* Set extension level. */
7305 if (extended->attr.extension == 255)
7307 /* Since the extension field is 8 bit wide, we can only have
7308 up to 255 extension levels. */
7309 gfc_error ("Maximum extension level reached with type '%s' at %L",
7310 extended->name, &extended->declared_at);
7311 return MATCH_ERROR;
7313 sym->attr.extension = extended->attr.extension + 1;
7315 /* Provide the links between the extended type and its extension. */
7316 if (!extended->f2k_derived)
7317 extended->f2k_derived = gfc_get_namespace (NULL, 0);
7318 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7319 st->n.sym = sym;
7322 if (!sym->hash_value)
7323 /* Set the hash for the compound name for this type. */
7324 sym->hash_value = gfc_hash_value (sym);
7326 /* Take over the ABSTRACT attribute. */
7327 sym->attr.abstract = attr.abstract;
7329 gfc_new_block = sym;
7331 return MATCH_YES;
7335 /* Cray Pointees can be declared as:
7336 pointer (ipt, a (n,m,...,*)) */
7338 match
7339 gfc_mod_pointee_as (gfc_array_spec *as)
7341 as->cray_pointee = true; /* This will be useful to know later. */
7342 if (as->type == AS_ASSUMED_SIZE)
7343 as->cp_was_assumed = true;
7344 else if (as->type == AS_ASSUMED_SHAPE)
7346 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7347 return MATCH_ERROR;
7349 return MATCH_YES;
7353 /* Match the enum definition statement, here we are trying to match
7354 the first line of enum definition statement.
7355 Returns MATCH_YES if match is found. */
7357 match
7358 gfc_match_enum (void)
7360 match m;
7362 m = gfc_match_eos ();
7363 if (m != MATCH_YES)
7364 return m;
7366 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
7367 == FAILURE)
7368 return MATCH_ERROR;
7370 return MATCH_YES;
7374 /* Returns an initializer whose value is one higher than the value of the
7375 LAST_INITIALIZER argument. If the argument is NULL, the
7376 initializers value will be set to zero. The initializer's kind
7377 will be set to gfc_c_int_kind.
7379 If -fshort-enums is given, the appropriate kind will be selected
7380 later after all enumerators have been parsed. A warning is issued
7381 here if an initializer exceeds gfc_c_int_kind. */
7383 static gfc_expr *
7384 enum_initializer (gfc_expr *last_initializer, locus where)
7386 gfc_expr *result;
7387 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7389 mpz_init (result->value.integer);
7391 if (last_initializer != NULL)
7393 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7394 result->where = last_initializer->where;
7396 if (gfc_check_integer_range (result->value.integer,
7397 gfc_c_int_kind) != ARITH_OK)
7399 gfc_error ("Enumerator exceeds the C integer type at %C");
7400 return NULL;
7403 else
7405 /* Control comes here, if it's the very first enumerator and no
7406 initializer has been given. It will be initialized to zero. */
7407 mpz_set_si (result->value.integer, 0);
7410 return result;
7414 /* Match a variable name with an optional initializer. When this
7415 subroutine is called, a variable is expected to be parsed next.
7416 Depending on what is happening at the moment, updates either the
7417 symbol table or the current interface. */
7419 static match
7420 enumerator_decl (void)
7422 char name[GFC_MAX_SYMBOL_LEN + 1];
7423 gfc_expr *initializer;
7424 gfc_array_spec *as = NULL;
7425 gfc_symbol *sym;
7426 locus var_locus;
7427 match m;
7428 gfc_try t;
7429 locus old_locus;
7431 initializer = NULL;
7432 old_locus = gfc_current_locus;
7434 /* When we get here, we've just matched a list of attributes and
7435 maybe a type and a double colon. The next thing we expect to see
7436 is the name of the symbol. */
7437 m = gfc_match_name (name);
7438 if (m != MATCH_YES)
7439 goto cleanup;
7441 var_locus = gfc_current_locus;
7443 /* OK, we've successfully matched the declaration. Now put the
7444 symbol in the current namespace. If we fail to create the symbol,
7445 bail out. */
7446 if (build_sym (name, NULL, false, &as, &var_locus) == FAILURE)
7448 m = MATCH_ERROR;
7449 goto cleanup;
7452 /* The double colon must be present in order to have initializers.
7453 Otherwise the statement is ambiguous with an assignment statement. */
7454 if (colon_seen)
7456 if (gfc_match_char ('=') == MATCH_YES)
7458 m = gfc_match_init_expr (&initializer);
7459 if (m == MATCH_NO)
7461 gfc_error ("Expected an initialization expression at %C");
7462 m = MATCH_ERROR;
7465 if (m != MATCH_YES)
7466 goto cleanup;
7470 /* If we do not have an initializer, the initialization value of the
7471 previous enumerator (stored in last_initializer) is incremented
7472 by 1 and is used to initialize the current enumerator. */
7473 if (initializer == NULL)
7474 initializer = enum_initializer (last_initializer, old_locus);
7476 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7478 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7479 &var_locus);
7480 m = MATCH_ERROR;
7481 goto cleanup;
7484 /* Store this current initializer, for the next enumerator variable
7485 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7486 use last_initializer below. */
7487 last_initializer = initializer;
7488 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7490 /* Maintain enumerator history. */
7491 gfc_find_symbol (name, NULL, 0, &sym);
7492 create_enum_history (sym, last_initializer);
7494 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
7496 cleanup:
7497 /* Free stuff up and return. */
7498 gfc_free_expr (initializer);
7500 return m;
7504 /* Match the enumerator definition statement. */
7506 match
7507 gfc_match_enumerator_def (void)
7509 match m;
7510 gfc_try t;
7512 gfc_clear_ts (&current_ts);
7514 m = gfc_match (" enumerator");
7515 if (m != MATCH_YES)
7516 return m;
7518 m = gfc_match (" :: ");
7519 if (m == MATCH_ERROR)
7520 return m;
7522 colon_seen = (m == MATCH_YES);
7524 if (gfc_current_state () != COMP_ENUM)
7526 gfc_error ("ENUM definition statement expected before %C");
7527 gfc_free_enum_history ();
7528 return MATCH_ERROR;
7531 (&current_ts)->type = BT_INTEGER;
7532 (&current_ts)->kind = gfc_c_int_kind;
7534 gfc_clear_attr (&current_attr);
7535 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
7536 if (t == FAILURE)
7538 m = MATCH_ERROR;
7539 goto cleanup;
7542 for (;;)
7544 m = enumerator_decl ();
7545 if (m == MATCH_ERROR)
7547 gfc_free_enum_history ();
7548 goto cleanup;
7550 if (m == MATCH_NO)
7551 break;
7553 if (gfc_match_eos () == MATCH_YES)
7554 goto cleanup;
7555 if (gfc_match_char (',') != MATCH_YES)
7556 break;
7559 if (gfc_current_state () == COMP_ENUM)
7561 gfc_free_enum_history ();
7562 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7563 m = MATCH_ERROR;
7566 cleanup:
7567 gfc_free_array_spec (current_as);
7568 current_as = NULL;
7569 return m;
7574 /* Match binding attributes. */
7576 static match
7577 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7579 bool found_passing = false;
7580 bool seen_ptr = false;
7581 match m = MATCH_YES;
7583 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7584 this case the defaults are in there. */
7585 ba->access = ACCESS_UNKNOWN;
7586 ba->pass_arg = NULL;
7587 ba->pass_arg_num = 0;
7588 ba->nopass = 0;
7589 ba->non_overridable = 0;
7590 ba->deferred = 0;
7591 ba->ppc = ppc;
7593 /* If we find a comma, we believe there are binding attributes. */
7594 m = gfc_match_char (',');
7595 if (m == MATCH_NO)
7596 goto done;
7600 /* Access specifier. */
7602 m = gfc_match (" public");
7603 if (m == MATCH_ERROR)
7604 goto error;
7605 if (m == MATCH_YES)
7607 if (ba->access != ACCESS_UNKNOWN)
7609 gfc_error ("Duplicate access-specifier at %C");
7610 goto error;
7613 ba->access = ACCESS_PUBLIC;
7614 continue;
7617 m = gfc_match (" private");
7618 if (m == MATCH_ERROR)
7619 goto error;
7620 if (m == MATCH_YES)
7622 if (ba->access != ACCESS_UNKNOWN)
7624 gfc_error ("Duplicate access-specifier at %C");
7625 goto error;
7628 ba->access = ACCESS_PRIVATE;
7629 continue;
7632 /* If inside GENERIC, the following is not allowed. */
7633 if (!generic)
7636 /* NOPASS flag. */
7637 m = gfc_match (" nopass");
7638 if (m == MATCH_ERROR)
7639 goto error;
7640 if (m == MATCH_YES)
7642 if (found_passing)
7644 gfc_error ("Binding attributes already specify passing,"
7645 " illegal NOPASS at %C");
7646 goto error;
7649 found_passing = true;
7650 ba->nopass = 1;
7651 continue;
7654 /* PASS possibly including argument. */
7655 m = gfc_match (" pass");
7656 if (m == MATCH_ERROR)
7657 goto error;
7658 if (m == MATCH_YES)
7660 char arg[GFC_MAX_SYMBOL_LEN + 1];
7662 if (found_passing)
7664 gfc_error ("Binding attributes already specify passing,"
7665 " illegal PASS at %C");
7666 goto error;
7669 m = gfc_match (" ( %n )", arg);
7670 if (m == MATCH_ERROR)
7671 goto error;
7672 if (m == MATCH_YES)
7673 ba->pass_arg = gfc_get_string (arg);
7674 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7676 found_passing = true;
7677 ba->nopass = 0;
7678 continue;
7681 if (ppc)
7683 /* POINTER flag. */
7684 m = gfc_match (" pointer");
7685 if (m == MATCH_ERROR)
7686 goto error;
7687 if (m == MATCH_YES)
7689 if (seen_ptr)
7691 gfc_error ("Duplicate POINTER attribute at %C");
7692 goto error;
7695 seen_ptr = true;
7696 continue;
7699 else
7701 /* NON_OVERRIDABLE flag. */
7702 m = gfc_match (" non_overridable");
7703 if (m == MATCH_ERROR)
7704 goto error;
7705 if (m == MATCH_YES)
7707 if (ba->non_overridable)
7709 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7710 goto error;
7713 ba->non_overridable = 1;
7714 continue;
7717 /* DEFERRED flag. */
7718 m = gfc_match (" deferred");
7719 if (m == MATCH_ERROR)
7720 goto error;
7721 if (m == MATCH_YES)
7723 if (ba->deferred)
7725 gfc_error ("Duplicate DEFERRED at %C");
7726 goto error;
7729 ba->deferred = 1;
7730 continue;
7736 /* Nothing matching found. */
7737 if (generic)
7738 gfc_error ("Expected access-specifier at %C");
7739 else
7740 gfc_error ("Expected binding attribute at %C");
7741 goto error;
7743 while (gfc_match_char (',') == MATCH_YES);
7745 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
7746 if (ba->non_overridable && ba->deferred)
7748 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7749 goto error;
7752 m = MATCH_YES;
7754 done:
7755 if (ba->access == ACCESS_UNKNOWN)
7756 ba->access = gfc_typebound_default_access;
7758 if (ppc && !seen_ptr)
7760 gfc_error ("POINTER attribute is required for procedure pointer component"
7761 " at %C");
7762 goto error;
7765 return m;
7767 error:
7768 return MATCH_ERROR;
7772 /* Match a PROCEDURE specific binding inside a derived type. */
7774 static match
7775 match_procedure_in_type (void)
7777 char name[GFC_MAX_SYMBOL_LEN + 1];
7778 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
7779 char* target = NULL, *ifc = NULL;
7780 gfc_typebound_proc tb;
7781 bool seen_colons;
7782 bool seen_attrs;
7783 match m;
7784 gfc_symtree* stree;
7785 gfc_namespace* ns;
7786 gfc_symbol* block;
7787 int num;
7789 /* Check current state. */
7790 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
7791 block = gfc_state_stack->previous->sym;
7792 gcc_assert (block);
7794 /* Try to match PROCEDURE(interface). */
7795 if (gfc_match (" (") == MATCH_YES)
7797 m = gfc_match_name (target_buf);
7798 if (m == MATCH_ERROR)
7799 return m;
7800 if (m != MATCH_YES)
7802 gfc_error ("Interface-name expected after '(' at %C");
7803 return MATCH_ERROR;
7806 if (gfc_match (" )") != MATCH_YES)
7808 gfc_error ("')' expected at %C");
7809 return MATCH_ERROR;
7812 ifc = target_buf;
7815 /* Construct the data structure. */
7816 memset (&tb, 0, sizeof (tb));
7817 tb.where = gfc_current_locus;
7819 /* Match binding attributes. */
7820 m = match_binding_attributes (&tb, false, false);
7821 if (m == MATCH_ERROR)
7822 return m;
7823 seen_attrs = (m == MATCH_YES);
7825 /* Check that attribute DEFERRED is given if an interface is specified. */
7826 if (tb.deferred && !ifc)
7828 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7829 return MATCH_ERROR;
7831 if (ifc && !tb.deferred)
7833 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7834 return MATCH_ERROR;
7837 /* Match the colons. */
7838 m = gfc_match (" ::");
7839 if (m == MATCH_ERROR)
7840 return m;
7841 seen_colons = (m == MATCH_YES);
7842 if (seen_attrs && !seen_colons)
7844 gfc_error ("Expected '::' after binding-attributes at %C");
7845 return MATCH_ERROR;
7848 /* Match the binding names. */
7849 for(num=1;;num++)
7851 m = gfc_match_name (name);
7852 if (m == MATCH_ERROR)
7853 return m;
7854 if (m == MATCH_NO)
7856 gfc_error ("Expected binding name at %C");
7857 return MATCH_ERROR;
7860 if (num>1 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: PROCEDURE list"
7861 " at %C") == FAILURE)
7862 return MATCH_ERROR;
7864 /* Try to match the '=> target', if it's there. */
7865 target = ifc;
7866 m = gfc_match (" =>");
7867 if (m == MATCH_ERROR)
7868 return m;
7869 if (m == MATCH_YES)
7871 if (tb.deferred)
7873 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7874 return MATCH_ERROR;
7877 if (!seen_colons)
7879 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7880 " at %C");
7881 return MATCH_ERROR;
7884 m = gfc_match_name (target_buf);
7885 if (m == MATCH_ERROR)
7886 return m;
7887 if (m == MATCH_NO)
7889 gfc_error ("Expected binding target after '=>' at %C");
7890 return MATCH_ERROR;
7892 target = target_buf;
7895 /* If no target was found, it has the same name as the binding. */
7896 if (!target)
7897 target = name;
7899 /* Get the namespace to insert the symbols into. */
7900 ns = block->f2k_derived;
7901 gcc_assert (ns);
7903 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7904 if (tb.deferred && !block->attr.abstract)
7906 gfc_error ("Type '%s' containing DEFERRED binding at %C "
7907 "is not ABSTRACT", block->name);
7908 return MATCH_ERROR;
7911 /* See if we already have a binding with this name in the symtree which
7912 would be an error. If a GENERIC already targetted this binding, it may
7913 be already there but then typebound is still NULL. */
7914 stree = gfc_find_symtree (ns->tb_sym_root, name);
7915 if (stree && stree->n.tb)
7917 gfc_error ("There is already a procedure with binding name '%s' for "
7918 "the derived type '%s' at %C", name, block->name);
7919 return MATCH_ERROR;
7922 /* Insert it and set attributes. */
7924 if (!stree)
7926 stree = gfc_new_symtree (&ns->tb_sym_root, name);
7927 gcc_assert (stree);
7929 stree->n.tb = gfc_get_typebound_proc (&tb);
7931 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
7932 false))
7933 return MATCH_ERROR;
7934 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
7936 if (gfc_match_eos () == MATCH_YES)
7937 return MATCH_YES;
7938 if (gfc_match_char (',') != MATCH_YES)
7939 goto syntax;
7942 syntax:
7943 gfc_error ("Syntax error in PROCEDURE statement at %C");
7944 return MATCH_ERROR;
7948 /* Match a GENERIC procedure binding inside a derived type. */
7950 match
7951 gfc_match_generic (void)
7953 char name[GFC_MAX_SYMBOL_LEN + 1];
7954 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
7955 gfc_symbol* block;
7956 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
7957 gfc_typebound_proc* tb;
7958 gfc_namespace* ns;
7959 interface_type op_type;
7960 gfc_intrinsic_op op;
7961 match m;
7963 /* Check current state. */
7964 if (gfc_current_state () == COMP_DERIVED)
7966 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
7967 return MATCH_ERROR;
7969 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
7970 return MATCH_NO;
7971 block = gfc_state_stack->previous->sym;
7972 ns = block->f2k_derived;
7973 gcc_assert (block && ns);
7975 memset (&tbattr, 0, sizeof (tbattr));
7976 tbattr.where = gfc_current_locus;
7978 /* See if we get an access-specifier. */
7979 m = match_binding_attributes (&tbattr, true, false);
7980 if (m == MATCH_ERROR)
7981 goto error;
7983 /* Now the colons, those are required. */
7984 if (gfc_match (" ::") != MATCH_YES)
7986 gfc_error ("Expected '::' at %C");
7987 goto error;
7990 /* Match the binding name; depending on type (operator / generic) format
7991 it for future error messages into bind_name. */
7993 m = gfc_match_generic_spec (&op_type, name, &op);
7994 if (m == MATCH_ERROR)
7995 return MATCH_ERROR;
7996 if (m == MATCH_NO)
7998 gfc_error ("Expected generic name or operator descriptor at %C");
7999 goto error;
8002 switch (op_type)
8004 case INTERFACE_GENERIC:
8005 snprintf (bind_name, sizeof (bind_name), "%s", name);
8006 break;
8008 case INTERFACE_USER_OP:
8009 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8010 break;
8012 case INTERFACE_INTRINSIC_OP:
8013 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8014 gfc_op2string (op));
8015 break;
8017 default:
8018 gcc_unreachable ();
8021 /* Match the required =>. */
8022 if (gfc_match (" =>") != MATCH_YES)
8024 gfc_error ("Expected '=>' at %C");
8025 goto error;
8028 /* Try to find existing GENERIC binding with this name / for this operator;
8029 if there is something, check that it is another GENERIC and then extend
8030 it rather than building a new node. Otherwise, create it and put it
8031 at the right position. */
8033 switch (op_type)
8035 case INTERFACE_USER_OP:
8036 case INTERFACE_GENERIC:
8038 const bool is_op = (op_type == INTERFACE_USER_OP);
8039 gfc_symtree* st;
8041 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8042 if (st)
8044 tb = st->n.tb;
8045 gcc_assert (tb);
8047 else
8048 tb = NULL;
8050 break;
8053 case INTERFACE_INTRINSIC_OP:
8054 tb = ns->tb_op[op];
8055 break;
8057 default:
8058 gcc_unreachable ();
8061 if (tb)
8063 if (!tb->is_generic)
8065 gcc_assert (op_type == INTERFACE_GENERIC);
8066 gfc_error ("There's already a non-generic procedure with binding name"
8067 " '%s' for the derived type '%s' at %C",
8068 bind_name, block->name);
8069 goto error;
8072 if (tb->access != tbattr.access)
8074 gfc_error ("Binding at %C must have the same access as already"
8075 " defined binding '%s'", bind_name);
8076 goto error;
8079 else
8081 tb = gfc_get_typebound_proc (NULL);
8082 tb->where = gfc_current_locus;
8083 tb->access = tbattr.access;
8084 tb->is_generic = 1;
8085 tb->u.generic = NULL;
8087 switch (op_type)
8089 case INTERFACE_GENERIC:
8090 case INTERFACE_USER_OP:
8092 const bool is_op = (op_type == INTERFACE_USER_OP);
8093 gfc_symtree* st;
8095 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8096 name);
8097 gcc_assert (st);
8098 st->n.tb = tb;
8100 break;
8103 case INTERFACE_INTRINSIC_OP:
8104 ns->tb_op[op] = tb;
8105 break;
8107 default:
8108 gcc_unreachable ();
8112 /* Now, match all following names as specific targets. */
8115 gfc_symtree* target_st;
8116 gfc_tbp_generic* target;
8118 m = gfc_match_name (name);
8119 if (m == MATCH_ERROR)
8120 goto error;
8121 if (m == MATCH_NO)
8123 gfc_error ("Expected specific binding name at %C");
8124 goto error;
8127 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8129 /* See if this is a duplicate specification. */
8130 for (target = tb->u.generic; target; target = target->next)
8131 if (target_st == target->specific_st)
8133 gfc_error ("'%s' already defined as specific binding for the"
8134 " generic '%s' at %C", name, bind_name);
8135 goto error;
8138 target = gfc_get_tbp_generic ();
8139 target->specific_st = target_st;
8140 target->specific = NULL;
8141 target->next = tb->u.generic;
8142 tb->u.generic = target;
8144 while (gfc_match (" ,") == MATCH_YES);
8146 /* Here should be the end. */
8147 if (gfc_match_eos () != MATCH_YES)
8149 gfc_error ("Junk after GENERIC binding at %C");
8150 goto error;
8153 return MATCH_YES;
8155 error:
8156 return MATCH_ERROR;
8160 /* Match a FINAL declaration inside a derived type. */
8162 match
8163 gfc_match_final_decl (void)
8165 char name[GFC_MAX_SYMBOL_LEN + 1];
8166 gfc_symbol* sym;
8167 match m;
8168 gfc_namespace* module_ns;
8169 bool first, last;
8170 gfc_symbol* block;
8172 if (gfc_current_form == FORM_FREE)
8174 char c = gfc_peek_ascii_char ();
8175 if (!gfc_is_whitespace (c) && c != ':')
8176 return MATCH_NO;
8179 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8181 if (gfc_current_form == FORM_FIXED)
8182 return MATCH_NO;
8184 gfc_error ("FINAL declaration at %C must be inside a derived type "
8185 "CONTAINS section");
8186 return MATCH_ERROR;
8189 block = gfc_state_stack->previous->sym;
8190 gcc_assert (block);
8192 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8193 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8195 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8196 " specification part of a MODULE");
8197 return MATCH_ERROR;
8200 module_ns = gfc_current_ns;
8201 gcc_assert (module_ns);
8202 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8204 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8205 if (gfc_match (" ::") == MATCH_ERROR)
8206 return MATCH_ERROR;
8208 /* Match the sequence of procedure names. */
8209 first = true;
8210 last = false;
8213 gfc_finalizer* f;
8215 if (first && gfc_match_eos () == MATCH_YES)
8217 gfc_error ("Empty FINAL at %C");
8218 return MATCH_ERROR;
8221 m = gfc_match_name (name);
8222 if (m == MATCH_NO)
8224 gfc_error ("Expected module procedure name at %C");
8225 return MATCH_ERROR;
8227 else if (m != MATCH_YES)
8228 return MATCH_ERROR;
8230 if (gfc_match_eos () == MATCH_YES)
8231 last = true;
8232 if (!last && gfc_match_char (',') != MATCH_YES)
8234 gfc_error ("Expected ',' at %C");
8235 return MATCH_ERROR;
8238 if (gfc_get_symbol (name, module_ns, &sym))
8240 gfc_error ("Unknown procedure name \"%s\" at %C", name);
8241 return MATCH_ERROR;
8244 /* Mark the symbol as module procedure. */
8245 if (sym->attr.proc != PROC_MODULE
8246 && gfc_add_procedure (&sym->attr, PROC_MODULE,
8247 sym->name, NULL) == FAILURE)
8248 return MATCH_ERROR;
8250 /* Check if we already have this symbol in the list, this is an error. */
8251 for (f = block->f2k_derived->finalizers; f; f = f->next)
8252 if (f->proc_sym == sym)
8254 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8255 name);
8256 return MATCH_ERROR;
8259 /* Add this symbol to the list of finalizers. */
8260 gcc_assert (block->f2k_derived);
8261 ++sym->refs;
8262 f = XCNEW (gfc_finalizer);
8263 f->proc_sym = sym;
8264 f->proc_tree = NULL;
8265 f->where = gfc_current_locus;
8266 f->next = block->f2k_derived->finalizers;
8267 block->f2k_derived->finalizers = f;
8269 first = false;
8271 while (!last);
8273 return MATCH_YES;
8277 const ext_attr_t ext_attr_list[] = {
8278 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8279 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8280 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
8281 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
8282 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
8283 { NULL, EXT_ATTR_LAST, NULL }
8286 /* Match a !GCC$ ATTRIBUTES statement of the form:
8287 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8288 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8290 TODO: We should support all GCC attributes using the same syntax for
8291 the attribute list, i.e. the list in C
8292 __attributes(( attribute-list ))
8293 matches then
8294 !GCC$ ATTRIBUTES attribute-list ::
8295 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8296 saved into a TREE.
8298 As there is absolutely no risk of confusion, we should never return
8299 MATCH_NO. */
8300 match
8301 gfc_match_gcc_attributes (void)
8303 symbol_attribute attr;
8304 char name[GFC_MAX_SYMBOL_LEN + 1];
8305 unsigned id;
8306 gfc_symbol *sym;
8307 match m;
8309 gfc_clear_attr (&attr);
8310 for(;;)
8312 char ch;
8314 if (gfc_match_name (name) != MATCH_YES)
8315 return MATCH_ERROR;
8317 for (id = 0; id < EXT_ATTR_LAST; id++)
8318 if (strcmp (name, ext_attr_list[id].name) == 0)
8319 break;
8321 if (id == EXT_ATTR_LAST)
8323 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8324 return MATCH_ERROR;
8327 if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
8328 == FAILURE)
8329 return MATCH_ERROR;
8331 gfc_gobble_whitespace ();
8332 ch = gfc_next_ascii_char ();
8333 if (ch == ':')
8335 /* This is the successful exit condition for the loop. */
8336 if (gfc_next_ascii_char () == ':')
8337 break;
8340 if (ch == ',')
8341 continue;
8343 goto syntax;
8346 if (gfc_match_eos () == MATCH_YES)
8347 goto syntax;
8349 for(;;)
8351 m = gfc_match_name (name);
8352 if (m != MATCH_YES)
8353 return m;
8355 if (find_special (name, &sym, true))
8356 return MATCH_ERROR;
8358 sym->attr.ext_attr |= attr.ext_attr;
8360 if (gfc_match_eos () == MATCH_YES)
8361 break;
8363 if (gfc_match_char (',') != MATCH_YES)
8364 goto syntax;
8367 return MATCH_YES;
8369 syntax:
8370 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
8371 return MATCH_ERROR;