Daily bump.
[official-gcc.git] / gcc / fortran / decl.c
blob0dbda0bfb2060bc61abbb9c2f0aa608c9d1c21d8
1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 /* Mark the variable as having appeared in a data statement. */
506 if (gfc_add_data (&sym->attr, sym->name, &sym->declared_at) == FAILURE)
508 gfc_free (newdata);
509 return MATCH_ERROR;
512 /* Chain in namespace list of DATA initializers. */
513 newdata->next = gfc_current_ns->data;
514 gfc_current_ns->data = newdata;
516 return m;
520 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
521 we are matching a DATA statement and are therefore issuing an error
522 if we encounter something unexpected, if not, we're trying to match
523 an old-style initialization expression of the form INTEGER I /2/. */
525 match
526 gfc_match_data (void)
528 gfc_data *new_data;
529 match m;
531 set_in_match_data (true);
533 for (;;)
535 new_data = gfc_get_data ();
536 new_data->where = gfc_current_locus;
538 m = top_var_list (new_data);
539 if (m != MATCH_YES)
540 goto cleanup;
542 m = top_val_list (new_data);
543 if (m != MATCH_YES)
544 goto cleanup;
546 new_data->next = gfc_current_ns->data;
547 gfc_current_ns->data = new_data;
549 if (gfc_match_eos () == MATCH_YES)
550 break;
552 gfc_match_char (','); /* Optional comma */
555 set_in_match_data (false);
557 if (gfc_pure (NULL))
559 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
560 return MATCH_ERROR;
563 return MATCH_YES;
565 cleanup:
566 set_in_match_data (false);
567 gfc_free_data (new_data);
568 return MATCH_ERROR;
572 /************************ Declaration statements *********************/
575 /* Auxilliary function to merge DIMENSION and CODIMENSION array specs. */
577 static void
578 merge_array_spec (gfc_array_spec *from, gfc_array_spec *to, bool copy)
580 int i;
582 if (to->rank == 0 && from->rank > 0)
584 to->rank = from->rank;
585 to->type = from->type;
586 to->cray_pointee = from->cray_pointee;
587 to->cp_was_assumed = from->cp_was_assumed;
589 for (i = 0; i < to->corank; i++)
591 to->lower[from->rank + i] = to->lower[i];
592 to->upper[from->rank + i] = to->upper[i];
594 for (i = 0; i < from->rank; i++)
596 if (copy)
598 to->lower[i] = gfc_copy_expr (from->lower[i]);
599 to->upper[i] = gfc_copy_expr (from->upper[i]);
601 else
603 to->lower[i] = from->lower[i];
604 to->upper[i] = from->upper[i];
608 else if (to->corank == 0 && from->corank > 0)
610 to->corank = from->corank;
611 to->cotype = from->cotype;
613 for (i = 0; i < from->corank; i++)
615 if (copy)
617 to->lower[to->rank + i] = gfc_copy_expr (from->lower[i]);
618 to->upper[to->rank + i] = gfc_copy_expr (from->upper[i]);
620 else
622 to->lower[to->rank + i] = from->lower[i];
623 to->upper[to->rank + i] = from->upper[i];
630 /* Match an intent specification. Since this can only happen after an
631 INTENT word, a legal intent-spec must follow. */
633 static sym_intent
634 match_intent_spec (void)
637 if (gfc_match (" ( in out )") == MATCH_YES)
638 return INTENT_INOUT;
639 if (gfc_match (" ( in )") == MATCH_YES)
640 return INTENT_IN;
641 if (gfc_match (" ( out )") == MATCH_YES)
642 return INTENT_OUT;
644 gfc_error ("Bad INTENT specification at %C");
645 return INTENT_UNKNOWN;
649 /* Matches a character length specification, which is either a
650 specification expression, '*', or ':'. */
652 static match
653 char_len_param_value (gfc_expr **expr, bool *deferred)
655 match m;
657 *expr = NULL;
658 *deferred = false;
660 if (gfc_match_char ('*') == MATCH_YES)
661 return MATCH_YES;
663 if (gfc_match_char (':') == MATCH_YES)
665 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: deferred type "
666 "parameter at %C") == FAILURE)
667 return MATCH_ERROR;
669 *deferred = true;
671 return MATCH_YES;
674 m = gfc_match_expr (expr);
676 if (m == MATCH_YES
677 && gfc_expr_check_typed (*expr, gfc_current_ns, false) == FAILURE)
678 return MATCH_ERROR;
680 if (m == MATCH_YES && (*expr)->expr_type == EXPR_FUNCTION)
682 if ((*expr)->value.function.actual
683 && (*expr)->value.function.actual->expr->symtree)
685 gfc_expr *e;
686 e = (*expr)->value.function.actual->expr;
687 if (e->symtree->n.sym->attr.flavor == FL_PROCEDURE
688 && e->expr_type == EXPR_VARIABLE)
690 if (e->symtree->n.sym->ts.type == BT_UNKNOWN)
691 goto syntax;
692 if (e->symtree->n.sym->ts.type == BT_CHARACTER
693 && e->symtree->n.sym->ts.u.cl
694 && e->symtree->n.sym->ts.u.cl->length->ts.type == BT_UNKNOWN)
695 goto syntax;
699 return m;
701 syntax:
702 gfc_error ("Conflict in attributes of function argument at %C");
703 return MATCH_ERROR;
707 /* A character length is a '*' followed by a literal integer or a
708 char_len_param_value in parenthesis. */
710 static match
711 match_char_length (gfc_expr **expr, bool *deferred)
713 int length;
714 match m;
716 *deferred = false;
717 m = gfc_match_char ('*');
718 if (m != MATCH_YES)
719 return m;
721 m = gfc_match_small_literal_int (&length, NULL);
722 if (m == MATCH_ERROR)
723 return m;
725 if (m == MATCH_YES)
727 if (gfc_notify_std (GFC_STD_F95_OBS, "Obsolescent feature: "
728 "Old-style character length at %C") == FAILURE)
729 return MATCH_ERROR;
730 *expr = gfc_get_int_expr (gfc_default_integer_kind, NULL, length);
731 return m;
734 if (gfc_match_char ('(') == MATCH_NO)
735 goto syntax;
737 m = char_len_param_value (expr, deferred);
738 if (m != MATCH_YES && gfc_matching_function)
740 gfc_undo_symbols ();
741 m = MATCH_YES;
744 if (m == MATCH_ERROR)
745 return m;
746 if (m == MATCH_NO)
747 goto syntax;
749 if (gfc_match_char (')') == MATCH_NO)
751 gfc_free_expr (*expr);
752 *expr = NULL;
753 goto syntax;
756 return MATCH_YES;
758 syntax:
759 gfc_error ("Syntax error in character length specification at %C");
760 return MATCH_ERROR;
764 /* Special subroutine for finding a symbol. Check if the name is found
765 in the current name space. If not, and we're compiling a function or
766 subroutine and the parent compilation unit is an interface, then check
767 to see if the name we've been given is the name of the interface
768 (located in another namespace). */
770 static int
771 find_special (const char *name, gfc_symbol **result, bool allow_subroutine)
773 gfc_state_data *s;
774 gfc_symtree *st;
775 int i;
777 i = gfc_get_sym_tree (name, NULL, &st, allow_subroutine);
778 if (i == 0)
780 *result = st ? st->n.sym : NULL;
781 goto end;
784 if (gfc_current_state () != COMP_SUBROUTINE
785 && gfc_current_state () != COMP_FUNCTION)
786 goto end;
788 s = gfc_state_stack->previous;
789 if (s == NULL)
790 goto end;
792 if (s->state != COMP_INTERFACE)
793 goto end;
794 if (s->sym == NULL)
795 goto end; /* Nameless interface. */
797 if (strcmp (name, s->sym->name) == 0)
799 *result = s->sym;
800 return 0;
803 end:
804 return i;
808 /* Special subroutine for getting a symbol node associated with a
809 procedure name, used in SUBROUTINE and FUNCTION statements. The
810 symbol is created in the parent using with symtree node in the
811 child unit pointing to the symbol. If the current namespace has no
812 parent, then the symbol is just created in the current unit. */
814 static int
815 get_proc_name (const char *name, gfc_symbol **result, bool module_fcn_entry)
817 gfc_symtree *st;
818 gfc_symbol *sym;
819 int rc = 0;
821 /* Module functions have to be left in their own namespace because
822 they have potentially (almost certainly!) already been referenced.
823 In this sense, they are rather like external functions. This is
824 fixed up in resolve.c(resolve_entries), where the symbol name-
825 space is set to point to the master function, so that the fake
826 result mechanism can work. */
827 if (module_fcn_entry)
829 /* Present if entry is declared to be a module procedure. */
830 rc = gfc_find_symbol (name, gfc_current_ns->parent, 0, result);
832 if (*result == NULL)
833 rc = gfc_get_symbol (name, NULL, result);
834 else if (!gfc_get_symbol (name, NULL, &sym) && sym
835 && (*result)->ts.type == BT_UNKNOWN
836 && sym->attr.flavor == FL_UNKNOWN)
837 /* Pick up the typespec for the entry, if declared in the function
838 body. Note that this symbol is FL_UNKNOWN because it will
839 only have appeared in a type declaration. The local symtree
840 is set to point to the module symbol and a unique symtree
841 to the local version. This latter ensures a correct clearing
842 of the symbols. */
844 /* If the ENTRY proceeds its specification, we need to ensure
845 that this does not raise a "has no IMPLICIT type" error. */
846 if (sym->ts.type == BT_UNKNOWN)
847 sym->attr.untyped = 1;
849 (*result)->ts = sym->ts;
851 /* Put the symbol in the procedure namespace so that, should
852 the ENTRY precede its specification, the specification
853 can be applied. */
854 (*result)->ns = gfc_current_ns;
856 gfc_find_sym_tree (name, gfc_current_ns, 0, &st);
857 st->n.sym = *result;
858 st = gfc_get_unique_symtree (gfc_current_ns);
859 st->n.sym = sym;
862 else
863 rc = gfc_get_symbol (name, gfc_current_ns->parent, result);
865 if (rc)
866 return rc;
868 sym = *result;
869 gfc_current_ns->refs++;
871 if (sym && !sym->gfc_new && gfc_current_state () != COMP_INTERFACE)
873 /* Trap another encompassed procedure with the same name. All
874 these conditions are necessary to avoid picking up an entry
875 whose name clashes with that of the encompassing procedure;
876 this is handled using gsymbols to register unique,globally
877 accessible names. */
878 if (sym->attr.flavor != 0
879 && sym->attr.proc != 0
880 && (sym->attr.subroutine || sym->attr.function)
881 && sym->attr.if_source != IFSRC_UNKNOWN)
882 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
883 name, &sym->declared_at);
885 /* Trap a procedure with a name the same as interface in the
886 encompassing scope. */
887 if (sym->attr.generic != 0
888 && (sym->attr.subroutine || sym->attr.function)
889 && !sym->attr.mod_proc)
890 gfc_error_now ("Name '%s' at %C is already defined"
891 " as a generic interface at %L",
892 name, &sym->declared_at);
894 /* Trap declarations of attributes in encompassing scope. The
895 signature for this is that ts.kind is set. Legitimate
896 references only set ts.type. */
897 if (sym->ts.kind != 0
898 && !sym->attr.implicit_type
899 && sym->attr.proc == 0
900 && gfc_current_ns->parent != NULL
901 && sym->attr.access == 0
902 && !module_fcn_entry)
903 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
904 "and must not have attributes declared at %L",
905 name, &sym->declared_at);
908 if (gfc_current_ns->parent == NULL || *result == NULL)
909 return rc;
911 /* Module function entries will already have a symtree in
912 the current namespace but will need one at module level. */
913 if (module_fcn_entry)
915 /* Present if entry is declared to be a module procedure. */
916 rc = gfc_find_sym_tree (name, gfc_current_ns->parent, 0, &st);
917 if (st == NULL)
918 st = gfc_new_symtree (&gfc_current_ns->parent->sym_root, name);
920 else
921 st = gfc_new_symtree (&gfc_current_ns->sym_root, name);
923 st->n.sym = sym;
924 sym->refs++;
926 /* See if the procedure should be a module procedure. */
928 if (((sym->ns->proc_name != NULL
929 && sym->ns->proc_name->attr.flavor == FL_MODULE
930 && sym->attr.proc != PROC_MODULE)
931 || (module_fcn_entry && sym->attr.proc != PROC_MODULE))
932 && gfc_add_procedure (&sym->attr, PROC_MODULE,
933 sym->name, NULL) == FAILURE)
934 rc = 2;
936 return rc;
940 /* Verify that the given symbol representing a parameter is C
941 interoperable, by checking to see if it was marked as such after
942 its declaration. If the given symbol is not interoperable, a
943 warning is reported, thus removing the need to return the status to
944 the calling function. The standard does not require the user use
945 one of the iso_c_binding named constants to declare an
946 interoperable parameter, but we can't be sure if the param is C
947 interop or not if the user doesn't. For example, integer(4) may be
948 legal Fortran, but doesn't have meaning in C. It may interop with
949 a number of the C types, which causes a problem because the
950 compiler can't know which one. This code is almost certainly not
951 portable, and the user will get what they deserve if the C type
952 across platforms isn't always interoperable with integer(4). If
953 the user had used something like integer(c_int) or integer(c_long),
954 the compiler could have automatically handled the varying sizes
955 across platforms. */
957 gfc_try
958 verify_c_interop_param (gfc_symbol *sym)
960 int is_c_interop = 0;
961 gfc_try retval = SUCCESS;
963 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
964 Don't repeat the checks here. */
965 if (sym->attr.implicit_type)
966 return SUCCESS;
968 /* For subroutines or functions that are passed to a BIND(C) procedure,
969 they're interoperable if they're BIND(C) and their params are all
970 interoperable. */
971 if (sym->attr.flavor == FL_PROCEDURE)
973 if (sym->attr.is_bind_c == 0)
975 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
976 "attribute to be C interoperable", sym->name,
977 &(sym->declared_at));
979 return FAILURE;
981 else
983 if (sym->attr.is_c_interop == 1)
984 /* We've already checked this procedure; don't check it again. */
985 return SUCCESS;
986 else
987 return verify_bind_c_sym (sym, &(sym->ts), sym->attr.in_common,
988 sym->common_block);
992 /* See if we've stored a reference to a procedure that owns sym. */
993 if (sym->ns != NULL && sym->ns->proc_name != NULL)
995 if (sym->ns->proc_name->attr.is_bind_c == 1)
997 is_c_interop =
998 (verify_c_interop (&(sym->ts))
999 == SUCCESS ? 1 : 0);
1001 if (is_c_interop != 1)
1003 /* Make personalized messages to give better feedback. */
1004 if (sym->ts.type == BT_DERIVED)
1005 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
1006 "procedure '%s' but is not C interoperable "
1007 "because derived type '%s' is not C interoperable",
1008 sym->name, &(sym->declared_at),
1009 sym->ns->proc_name->name,
1010 sym->ts.u.derived->name);
1011 else
1012 gfc_warning ("Variable '%s' at %L is a parameter to the "
1013 "BIND(C) procedure '%s' but may not be C "
1014 "interoperable",
1015 sym->name, &(sym->declared_at),
1016 sym->ns->proc_name->name);
1019 /* Character strings are only C interoperable if they have a
1020 length of 1. */
1021 if (sym->ts.type == BT_CHARACTER)
1023 gfc_charlen *cl = sym->ts.u.cl;
1024 if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
1025 || mpz_cmp_si (cl->length->value.integer, 1) != 0)
1027 gfc_error ("Character argument '%s' at %L "
1028 "must be length 1 because "
1029 "procedure '%s' is BIND(C)",
1030 sym->name, &sym->declared_at,
1031 sym->ns->proc_name->name);
1032 retval = FAILURE;
1036 /* We have to make sure that any param to a bind(c) routine does
1037 not have the allocatable, pointer, or optional attributes,
1038 according to J3/04-007, section 5.1. */
1039 if (sym->attr.allocatable == 1)
1041 gfc_error ("Variable '%s' at %L cannot have the "
1042 "ALLOCATABLE attribute because procedure '%s'"
1043 " is BIND(C)", sym->name, &(sym->declared_at),
1044 sym->ns->proc_name->name);
1045 retval = FAILURE;
1048 if (sym->attr.pointer == 1)
1050 gfc_error ("Variable '%s' at %L cannot have the "
1051 "POINTER attribute because procedure '%s'"
1052 " is BIND(C)", sym->name, &(sym->declared_at),
1053 sym->ns->proc_name->name);
1054 retval = FAILURE;
1057 if (sym->attr.optional == 1)
1059 gfc_error ("Variable '%s' at %L cannot have the "
1060 "OPTIONAL attribute because procedure '%s'"
1061 " is BIND(C)", sym->name, &(sym->declared_at),
1062 sym->ns->proc_name->name);
1063 retval = FAILURE;
1066 /* Make sure that if it has the dimension attribute, that it is
1067 either assumed size or explicit shape. */
1068 if (sym->as != NULL)
1070 if (sym->as->type == AS_ASSUMED_SHAPE)
1072 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
1073 "argument to the procedure '%s' at %L because "
1074 "the procedure is BIND(C)", sym->name,
1075 &(sym->declared_at), sym->ns->proc_name->name,
1076 &(sym->ns->proc_name->declared_at));
1077 retval = FAILURE;
1080 if (sym->as->type == AS_DEFERRED)
1082 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1083 "argument to the procedure '%s' at %L because "
1084 "the procedure is BIND(C)", sym->name,
1085 &(sym->declared_at), sym->ns->proc_name->name,
1086 &(sym->ns->proc_name->declared_at));
1087 retval = FAILURE;
1093 return retval;
1098 /* Function called by variable_decl() that adds a name to the symbol table. */
1100 static gfc_try
1101 build_sym (const char *name, gfc_charlen *cl, bool cl_deferred,
1102 gfc_array_spec **as, locus *var_locus)
1104 symbol_attribute attr;
1105 gfc_symbol *sym;
1107 if (gfc_get_symbol (name, NULL, &sym))
1108 return FAILURE;
1110 /* Start updating the symbol table. Add basic type attribute if present. */
1111 if (current_ts.type != BT_UNKNOWN
1112 && (sym->attr.implicit_type == 0
1113 || !gfc_compare_types (&sym->ts, &current_ts))
1114 && gfc_add_type (sym, &current_ts, var_locus) == FAILURE)
1115 return FAILURE;
1117 if (sym->ts.type == BT_CHARACTER)
1119 sym->ts.u.cl = cl;
1120 sym->ts.deferred = cl_deferred;
1123 /* Add dimension attribute if present. */
1124 if (gfc_set_array_spec (sym, *as, var_locus) == FAILURE)
1125 return FAILURE;
1126 *as = NULL;
1128 /* Add attribute to symbol. The copy is so that we can reset the
1129 dimension attribute. */
1130 attr = current_attr;
1131 attr.dimension = 0;
1132 attr.codimension = 0;
1134 if (gfc_copy_attr (&sym->attr, &attr, var_locus) == FAILURE)
1135 return FAILURE;
1137 /* Finish any work that may need to be done for the binding label,
1138 if it's a bind(c). The bind(c) attr is found before the symbol
1139 is made, and before the symbol name (for data decls), so the
1140 current_ts is holding the binding label, or nothing if the
1141 name= attr wasn't given. Therefore, test here if we're dealing
1142 with a bind(c) and make sure the binding label is set correctly. */
1143 if (sym->attr.is_bind_c == 1)
1145 if (sym->binding_label[0] == '\0')
1147 /* Set the binding label and verify that if a NAME= was specified
1148 then only one identifier was in the entity-decl-list. */
1149 if (set_binding_label (sym->binding_label, sym->name,
1150 num_idents_on_line) == FAILURE)
1151 return FAILURE;
1155 /* See if we know we're in a common block, and if it's a bind(c)
1156 common then we need to make sure we're an interoperable type. */
1157 if (sym->attr.in_common == 1)
1159 /* Test the common block object. */
1160 if (sym->common_block != NULL && sym->common_block->is_bind_c == 1
1161 && sym->ts.is_c_interop != 1)
1163 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1164 "must be declared with a C interoperable "
1165 "kind since common block '%s' is BIND(C)",
1166 sym->name, sym->common_block->name,
1167 sym->common_block->name);
1168 gfc_clear_error ();
1172 sym->attr.implied_index = 0;
1174 if (sym->ts.type == BT_CLASS
1175 && (sym->attr.class_ok = sym->attr.dummy || sym->attr.pointer
1176 || sym->attr.allocatable))
1177 gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
1179 return SUCCESS;
1183 /* Set character constant to the given length. The constant will be padded or
1184 truncated. If we're inside an array constructor without a typespec, we
1185 additionally check that all elements have the same length; check_len -1
1186 means no checking. */
1188 void
1189 gfc_set_constant_character_len (int len, gfc_expr *expr, int check_len)
1191 gfc_char_t *s;
1192 int slen;
1194 gcc_assert (expr->expr_type == EXPR_CONSTANT);
1195 gcc_assert (expr->ts.type == BT_CHARACTER);
1197 slen = expr->value.character.length;
1198 if (len != slen)
1200 s = gfc_get_wide_string (len + 1);
1201 memcpy (s, expr->value.character.string,
1202 MIN (len, slen) * sizeof (gfc_char_t));
1203 if (len > slen)
1204 gfc_wide_memset (&s[slen], ' ', len - slen);
1206 if (gfc_option.warn_character_truncation && slen > len)
1207 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1208 "(%d/%d)", &expr->where, slen, len);
1210 /* Apply the standard by 'hand' otherwise it gets cleared for
1211 initializers. */
1212 if (check_len != -1 && slen != check_len
1213 && !(gfc_option.allow_std & GFC_STD_GNU))
1214 gfc_error_now ("The CHARACTER elements of the array constructor "
1215 "at %L must have the same length (%d/%d)",
1216 &expr->where, slen, check_len);
1218 s[len] = '\0';
1219 gfc_free (expr->value.character.string);
1220 expr->value.character.string = s;
1221 expr->value.character.length = len;
1226 /* Function to create and update the enumerator history
1227 using the information passed as arguments.
1228 Pointer "max_enum" is also updated, to point to
1229 enum history node containing largest initializer.
1231 SYM points to the symbol node of enumerator.
1232 INIT points to its enumerator value. */
1234 static void
1235 create_enum_history (gfc_symbol *sym, gfc_expr *init)
1237 enumerator_history *new_enum_history;
1238 gcc_assert (sym != NULL && init != NULL);
1240 new_enum_history = XCNEW (enumerator_history);
1242 new_enum_history->sym = sym;
1243 new_enum_history->initializer = init;
1244 new_enum_history->next = NULL;
1246 if (enum_history == NULL)
1248 enum_history = new_enum_history;
1249 max_enum = enum_history;
1251 else
1253 new_enum_history->next = enum_history;
1254 enum_history = new_enum_history;
1256 if (mpz_cmp (max_enum->initializer->value.integer,
1257 new_enum_history->initializer->value.integer) < 0)
1258 max_enum = new_enum_history;
1263 /* Function to free enum kind history. */
1265 void
1266 gfc_free_enum_history (void)
1268 enumerator_history *current = enum_history;
1269 enumerator_history *next;
1271 while (current != NULL)
1273 next = current->next;
1274 gfc_free (current);
1275 current = next;
1277 max_enum = NULL;
1278 enum_history = NULL;
1282 /* Function called by variable_decl() that adds an initialization
1283 expression to a symbol. */
1285 static gfc_try
1286 add_init_expr_to_sym (const char *name, gfc_expr **initp, locus *var_locus)
1288 symbol_attribute attr;
1289 gfc_symbol *sym;
1290 gfc_expr *init;
1292 init = *initp;
1293 if (find_special (name, &sym, false))
1294 return FAILURE;
1296 attr = sym->attr;
1298 /* If this symbol is confirming an implicit parameter type,
1299 then an initialization expression is not allowed. */
1300 if (attr.flavor == FL_PARAMETER
1301 && sym->value != NULL
1302 && *initp != NULL)
1304 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1305 sym->name);
1306 return FAILURE;
1309 if (init == NULL)
1311 /* An initializer is required for PARAMETER declarations. */
1312 if (attr.flavor == FL_PARAMETER)
1314 gfc_error ("PARAMETER at %L is missing an initializer", var_locus);
1315 return FAILURE;
1318 else
1320 /* If a variable appears in a DATA block, it cannot have an
1321 initializer. */
1322 if (sym->attr.data)
1324 gfc_error ("Variable '%s' at %C with an initializer already "
1325 "appears in a DATA statement", sym->name);
1326 return FAILURE;
1329 /* Check if the assignment can happen. This has to be put off
1330 until later for derived type variables and procedure pointers. */
1331 if (sym->ts.type != BT_DERIVED && init->ts.type != BT_DERIVED
1332 && sym->ts.type != BT_CLASS && init->ts.type != BT_CLASS
1333 && !sym->attr.proc_pointer
1334 && gfc_check_assign_symbol (sym, init) == FAILURE)
1335 return FAILURE;
1337 if (sym->ts.type == BT_CHARACTER && sym->ts.u.cl
1338 && init->ts.type == BT_CHARACTER)
1340 /* Update symbol character length according initializer. */
1341 if (gfc_check_assign_symbol (sym, init) == FAILURE)
1342 return FAILURE;
1344 if (sym->ts.u.cl->length == NULL)
1346 int clen;
1347 /* If there are multiple CHARACTER variables declared on the
1348 same line, we don't want them to share the same length. */
1349 sym->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1351 if (sym->attr.flavor == FL_PARAMETER)
1353 if (init->expr_type == EXPR_CONSTANT)
1355 clen = init->value.character.length;
1356 sym->ts.u.cl->length
1357 = gfc_get_int_expr (gfc_default_integer_kind,
1358 NULL, clen);
1360 else if (init->expr_type == EXPR_ARRAY)
1362 gfc_constructor *c;
1363 c = gfc_constructor_first (init->value.constructor);
1364 clen = c->expr->value.character.length;
1365 sym->ts.u.cl->length
1366 = gfc_get_int_expr (gfc_default_integer_kind,
1367 NULL, clen);
1369 else if (init->ts.u.cl && init->ts.u.cl->length)
1370 sym->ts.u.cl->length =
1371 gfc_copy_expr (sym->value->ts.u.cl->length);
1374 /* Update initializer character length according symbol. */
1375 else if (sym->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1377 int len = mpz_get_si (sym->ts.u.cl->length->value.integer);
1379 if (init->expr_type == EXPR_CONSTANT)
1380 gfc_set_constant_character_len (len, init, -1);
1381 else if (init->expr_type == EXPR_ARRAY)
1383 gfc_constructor *c;
1385 /* Build a new charlen to prevent simplification from
1386 deleting the length before it is resolved. */
1387 init->ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
1388 init->ts.u.cl->length = gfc_copy_expr (sym->ts.u.cl->length);
1390 for (c = gfc_constructor_first (init->value.constructor);
1391 c; c = gfc_constructor_next (c))
1392 gfc_set_constant_character_len (len, c->expr, -1);
1397 /* If sym is implied-shape, set its upper bounds from init. */
1398 if (sym->attr.flavor == FL_PARAMETER && sym->attr.dimension
1399 && sym->as->type == AS_IMPLIED_SHAPE)
1401 int dim;
1403 if (init->rank == 0)
1405 gfc_error ("Can't initialize implied-shape array at %L"
1406 " with scalar", &sym->declared_at);
1407 return FAILURE;
1409 gcc_assert (sym->as->rank == init->rank);
1411 /* Shape should be present, we get an initialization expression. */
1412 gcc_assert (init->shape);
1414 for (dim = 0; dim < sym->as->rank; ++dim)
1416 int k;
1417 gfc_expr* lower;
1418 gfc_expr* e;
1420 lower = sym->as->lower[dim];
1421 if (lower->expr_type != EXPR_CONSTANT)
1423 gfc_error ("Non-constant lower bound in implied-shape"
1424 " declaration at %L", &lower->where);
1425 return FAILURE;
1428 /* All dimensions must be without upper bound. */
1429 gcc_assert (!sym->as->upper[dim]);
1431 k = lower->ts.kind;
1432 e = gfc_get_constant_expr (BT_INTEGER, k, &sym->declared_at);
1433 mpz_add (e->value.integer,
1434 lower->value.integer, init->shape[dim]);
1435 mpz_sub_ui (e->value.integer, e->value.integer, 1);
1436 sym->as->upper[dim] = e;
1439 sym->as->type = AS_EXPLICIT;
1442 /* Need to check if the expression we initialized this
1443 to was one of the iso_c_binding named constants. If so,
1444 and we're a parameter (constant), let it be iso_c.
1445 For example:
1446 integer(c_int), parameter :: my_int = c_int
1447 integer(my_int) :: my_int_2
1448 If we mark my_int as iso_c (since we can see it's value
1449 is equal to one of the named constants), then my_int_2
1450 will be considered C interoperable. */
1451 if (sym->ts.type != BT_CHARACTER && sym->ts.type != BT_DERIVED)
1453 sym->ts.is_iso_c |= init->ts.is_iso_c;
1454 sym->ts.is_c_interop |= init->ts.is_c_interop;
1455 /* attr bits needed for module files. */
1456 sym->attr.is_iso_c |= init->ts.is_iso_c;
1457 sym->attr.is_c_interop |= init->ts.is_c_interop;
1458 if (init->ts.is_iso_c)
1459 sym->ts.f90_type = init->ts.f90_type;
1462 /* Add initializer. Make sure we keep the ranks sane. */
1463 if (sym->attr.dimension && init->rank == 0)
1465 mpz_t size;
1466 gfc_expr *array;
1467 int n;
1468 if (sym->attr.flavor == FL_PARAMETER
1469 && init->expr_type == EXPR_CONSTANT
1470 && spec_size (sym->as, &size) == SUCCESS
1471 && mpz_cmp_si (size, 0) > 0)
1473 array = gfc_get_array_expr (init->ts.type, init->ts.kind,
1474 &init->where);
1475 for (n = 0; n < (int)mpz_get_si (size); n++)
1476 gfc_constructor_append_expr (&array->value.constructor,
1477 n == 0
1478 ? init
1479 : gfc_copy_expr (init),
1480 &init->where);
1482 array->shape = gfc_get_shape (sym->as->rank);
1483 for (n = 0; n < sym->as->rank; n++)
1484 spec_dimen_size (sym->as, n, &array->shape[n]);
1486 init = array;
1487 mpz_clear (size);
1489 init->rank = sym->as->rank;
1492 sym->value = init;
1493 if (sym->attr.save == SAVE_NONE)
1494 sym->attr.save = SAVE_IMPLICIT;
1495 *initp = NULL;
1498 return SUCCESS;
1502 /* Function called by variable_decl() that adds a name to a structure
1503 being built. */
1505 static gfc_try
1506 build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
1507 gfc_array_spec **as)
1509 gfc_component *c;
1510 gfc_try t = SUCCESS;
1512 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1513 constructing, it must have the pointer attribute. */
1514 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1515 && current_ts.u.derived == gfc_current_block ()
1516 && current_attr.pointer == 0)
1518 gfc_error ("Component at %C must have the POINTER attribute");
1519 return FAILURE;
1522 if (gfc_current_block ()->attr.pointer && (*as)->rank != 0)
1524 if ((*as)->type != AS_DEFERRED && (*as)->type != AS_EXPLICIT)
1526 gfc_error ("Array component of structure at %C must have explicit "
1527 "or deferred shape");
1528 return FAILURE;
1532 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
1533 return FAILURE;
1535 c->ts = current_ts;
1536 if (c->ts.type == BT_CHARACTER)
1537 c->ts.u.cl = cl;
1538 c->attr = current_attr;
1540 c->initializer = *init;
1541 *init = NULL;
1543 c->as = *as;
1544 if (c->as != NULL)
1546 if (c->as->corank)
1547 c->attr.codimension = 1;
1548 if (c->as->rank)
1549 c->attr.dimension = 1;
1551 *as = NULL;
1553 /* Should this ever get more complicated, combine with similar section
1554 in add_init_expr_to_sym into a separate function. */
1555 if (c->ts.type == BT_CHARACTER && !c->attr.pointer && c->initializer && c->ts.u.cl
1556 && c->ts.u.cl->length && c->ts.u.cl->length->expr_type == EXPR_CONSTANT)
1558 int len;
1560 gcc_assert (c->ts.u.cl && c->ts.u.cl->length);
1561 gcc_assert (c->ts.u.cl->length->expr_type == EXPR_CONSTANT);
1562 gcc_assert (c->ts.u.cl->length->ts.type == BT_INTEGER);
1564 len = mpz_get_si (c->ts.u.cl->length->value.integer);
1566 if (c->initializer->expr_type == EXPR_CONSTANT)
1567 gfc_set_constant_character_len (len, c->initializer, -1);
1568 else if (mpz_cmp (c->ts.u.cl->length->value.integer,
1569 c->initializer->ts.u.cl->length->value.integer))
1571 gfc_constructor *ctor;
1572 ctor = gfc_constructor_first (c->initializer->value.constructor);
1574 if (ctor)
1576 int first_len;
1577 bool has_ts = (c->initializer->ts.u.cl
1578 && c->initializer->ts.u.cl->length_from_typespec);
1580 /* Remember the length of the first element for checking
1581 that all elements *in the constructor* have the same
1582 length. This need not be the length of the LHS! */
1583 gcc_assert (ctor->expr->expr_type == EXPR_CONSTANT);
1584 gcc_assert (ctor->expr->ts.type == BT_CHARACTER);
1585 first_len = ctor->expr->value.character.length;
1587 for ( ; ctor; ctor = gfc_constructor_next (ctor))
1588 if (ctor->expr->expr_type == EXPR_CONSTANT)
1590 gfc_set_constant_character_len (len, ctor->expr,
1591 has_ts ? -1 : first_len);
1592 ctor->expr->ts.u.cl->length = gfc_copy_expr (c->ts.u.cl->length);
1598 /* Check array components. */
1599 if (!c->attr.dimension)
1600 goto scalar;
1602 if (c->attr.pointer)
1604 if (c->as->type != AS_DEFERRED)
1606 gfc_error ("Pointer array component of structure at %C must have a "
1607 "deferred shape");
1608 t = FAILURE;
1611 else if (c->attr.allocatable)
1613 if (c->as->type != AS_DEFERRED)
1615 gfc_error ("Allocatable component of structure at %C must have a "
1616 "deferred shape");
1617 t = FAILURE;
1620 else
1622 if (c->as->type != AS_EXPLICIT)
1624 gfc_error ("Array component of structure at %C must have an "
1625 "explicit shape");
1626 t = FAILURE;
1630 scalar:
1631 if (c->ts.type == BT_CLASS)
1633 bool delayed = (gfc_state_stack->sym == c->ts.u.derived)
1634 || (!c->ts.u.derived->components
1635 && !c->ts.u.derived->attr.zero_comp);
1636 gfc_build_class_symbol (&c->ts, &c->attr, &c->as, delayed);
1640 return t;
1644 /* Match a 'NULL()', and possibly take care of some side effects. */
1646 match
1647 gfc_match_null (gfc_expr **result)
1649 gfc_symbol *sym;
1650 match m;
1652 m = gfc_match (" null ( )");
1653 if (m != MATCH_YES)
1654 return m;
1656 /* The NULL symbol now has to be/become an intrinsic function. */
1657 if (gfc_get_symbol ("null", NULL, &sym))
1659 gfc_error ("NULL() initialization at %C is ambiguous");
1660 return MATCH_ERROR;
1663 gfc_intrinsic_symbol (sym);
1665 if (sym->attr.proc != PROC_INTRINSIC
1666 && (gfc_add_procedure (&sym->attr, PROC_INTRINSIC,
1667 sym->name, NULL) == FAILURE
1668 || gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE))
1669 return MATCH_ERROR;
1671 *result = gfc_get_null_expr (&gfc_current_locus);
1673 return MATCH_YES;
1677 /* Match the initialization expr for a data pointer or procedure pointer. */
1679 static match
1680 match_pointer_init (gfc_expr **init, int procptr)
1682 match m;
1684 if (gfc_pure (NULL) && gfc_state_stack->state != COMP_DERIVED)
1686 gfc_error ("Initialization of pointer at %C is not allowed in "
1687 "a PURE procedure");
1688 return MATCH_ERROR;
1691 /* Match NULL() initilization. */
1692 m = gfc_match_null (init);
1693 if (m != MATCH_NO)
1694 return m;
1696 /* Match non-NULL initialization. */
1697 gfc_matching_ptr_assignment = !procptr;
1698 gfc_matching_procptr_assignment = procptr;
1699 m = gfc_match_rvalue (init);
1700 gfc_matching_ptr_assignment = 0;
1701 gfc_matching_procptr_assignment = 0;
1702 if (m == MATCH_ERROR)
1703 return MATCH_ERROR;
1704 else if (m == MATCH_NO)
1706 gfc_error ("Error in pointer initialization at %C");
1707 return MATCH_ERROR;
1710 if (!procptr)
1711 gfc_resolve_expr (*init);
1713 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: non-NULL pointer "
1714 "initialization at %C") == FAILURE)
1715 return MATCH_ERROR;
1717 return MATCH_YES;
1721 /* Match a variable name with an optional initializer. When this
1722 subroutine is called, a variable is expected to be parsed next.
1723 Depending on what is happening at the moment, updates either the
1724 symbol table or the current interface. */
1726 static match
1727 variable_decl (int elem)
1729 char name[GFC_MAX_SYMBOL_LEN + 1];
1730 gfc_expr *initializer, *char_len;
1731 gfc_array_spec *as;
1732 gfc_array_spec *cp_as; /* Extra copy for Cray Pointees. */
1733 gfc_charlen *cl;
1734 bool cl_deferred;
1735 locus var_locus;
1736 match m;
1737 gfc_try t;
1738 gfc_symbol *sym;
1740 initializer = NULL;
1741 as = NULL;
1742 cp_as = NULL;
1744 /* When we get here, we've just matched a list of attributes and
1745 maybe a type and a double colon. The next thing we expect to see
1746 is the name of the symbol. */
1747 m = gfc_match_name (name);
1748 if (m != MATCH_YES)
1749 goto cleanup;
1751 var_locus = gfc_current_locus;
1753 /* Now we could see the optional array spec. or character length. */
1754 m = gfc_match_array_spec (&as, true, true);
1755 if (gfc_option.flag_cray_pointer && m == MATCH_YES)
1756 cp_as = gfc_copy_array_spec (as);
1757 else if (m == MATCH_ERROR)
1758 goto cleanup;
1760 if (m == MATCH_NO)
1761 as = gfc_copy_array_spec (current_as);
1762 else if (current_as)
1763 merge_array_spec (current_as, as, true);
1765 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1766 determine (and check) whether it can be implied-shape. If it
1767 was parsed as assumed-size, change it because PARAMETERs can not
1768 be assumed-size. */
1769 if (as)
1771 if (as->type == AS_IMPLIED_SHAPE && current_attr.flavor != FL_PARAMETER)
1773 m = MATCH_ERROR;
1774 gfc_error ("Non-PARAMETER symbol '%s' at %L can't be implied-shape",
1775 name, &var_locus);
1776 goto cleanup;
1779 if (as->type == AS_ASSUMED_SIZE && as->rank == 1
1780 && current_attr.flavor == FL_PARAMETER)
1781 as->type = AS_IMPLIED_SHAPE;
1783 if (as->type == AS_IMPLIED_SHAPE
1784 && gfc_notify_std (GFC_STD_F2008,
1785 "Fortran 2008: Implied-shape array at %L",
1786 &var_locus) == FAILURE)
1788 m = MATCH_ERROR;
1789 goto cleanup;
1793 char_len = NULL;
1794 cl = NULL;
1795 cl_deferred = false;
1797 if (current_ts.type == BT_CHARACTER)
1799 switch (match_char_length (&char_len, &cl_deferred))
1801 case MATCH_YES:
1802 cl = gfc_new_charlen (gfc_current_ns, NULL);
1804 cl->length = char_len;
1805 break;
1807 /* Non-constant lengths need to be copied after the first
1808 element. Also copy assumed lengths. */
1809 case MATCH_NO:
1810 if (elem > 1
1811 && (current_ts.u.cl->length == NULL
1812 || current_ts.u.cl->length->expr_type != EXPR_CONSTANT))
1814 cl = gfc_new_charlen (gfc_current_ns, NULL);
1815 cl->length = gfc_copy_expr (current_ts.u.cl->length);
1817 else
1818 cl = current_ts.u.cl;
1820 cl_deferred = current_ts.deferred;
1822 break;
1824 case MATCH_ERROR:
1825 goto cleanup;
1829 /* If this symbol has already shown up in a Cray Pointer declaration,
1830 then we want to set the type & bail out. */
1831 if (gfc_option.flag_cray_pointer)
1833 gfc_find_symbol (name, gfc_current_ns, 1, &sym);
1834 if (sym != NULL && sym->attr.cray_pointee)
1836 sym->ts.type = current_ts.type;
1837 sym->ts.kind = current_ts.kind;
1838 sym->ts.u.cl = cl;
1839 sym->ts.u.derived = current_ts.u.derived;
1840 sym->ts.is_c_interop = current_ts.is_c_interop;
1841 sym->ts.is_iso_c = current_ts.is_iso_c;
1842 m = MATCH_YES;
1844 /* Check to see if we have an array specification. */
1845 if (cp_as != NULL)
1847 if (sym->as != NULL)
1849 gfc_error ("Duplicate array spec for Cray pointee at %C");
1850 gfc_free_array_spec (cp_as);
1851 m = MATCH_ERROR;
1852 goto cleanup;
1854 else
1856 if (gfc_set_array_spec (sym, cp_as, &var_locus) == FAILURE)
1857 gfc_internal_error ("Couldn't set pointee array spec.");
1859 /* Fix the array spec. */
1860 m = gfc_mod_pointee_as (sym->as);
1861 if (m == MATCH_ERROR)
1862 goto cleanup;
1865 goto cleanup;
1867 else
1869 gfc_free_array_spec (cp_as);
1873 /* Procedure pointer as function result. */
1874 if (gfc_current_state () == COMP_FUNCTION
1875 && strcmp ("ppr@", gfc_current_block ()->name) == 0
1876 && strcmp (name, gfc_current_block ()->ns->proc_name->name) == 0)
1877 strcpy (name, "ppr@");
1879 if (gfc_current_state () == COMP_FUNCTION
1880 && strcmp (name, gfc_current_block ()->name) == 0
1881 && gfc_current_block ()->result
1882 && strcmp ("ppr@", gfc_current_block ()->result->name) == 0)
1883 strcpy (name, "ppr@");
1885 /* OK, we've successfully matched the declaration. Now put the
1886 symbol in the current namespace, because it might be used in the
1887 optional initialization expression for this symbol, e.g. this is
1888 perfectly legal:
1890 integer, parameter :: i = huge(i)
1892 This is only true for parameters or variables of a basic type.
1893 For components of derived types, it is not true, so we don't
1894 create a symbol for those yet. If we fail to create the symbol,
1895 bail out. */
1896 if (gfc_current_state () != COMP_DERIVED
1897 && build_sym (name, cl, cl_deferred, &as, &var_locus) == FAILURE)
1899 m = MATCH_ERROR;
1900 goto cleanup;
1903 /* An interface body specifies all of the procedure's
1904 characteristics and these shall be consistent with those
1905 specified in the procedure definition, except that the interface
1906 may specify a procedure that is not pure if the procedure is
1907 defined to be pure(12.3.2). */
1908 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
1909 && gfc_current_ns->proc_name
1910 && gfc_current_ns->proc_name->attr.if_source == IFSRC_IFBODY
1911 && current_ts.u.derived->ns != gfc_current_ns)
1913 gfc_symtree *st;
1914 st = gfc_find_symtree (gfc_current_ns->sym_root, current_ts.u.derived->name);
1915 if (!(current_ts.u.derived->attr.imported
1916 && st != NULL
1917 && st->n.sym == current_ts.u.derived)
1918 && !gfc_current_ns->has_import_set)
1920 gfc_error ("the type of '%s' at %C has not been declared within the "
1921 "interface", name);
1922 m = MATCH_ERROR;
1923 goto cleanup;
1927 /* In functions that have a RESULT variable defined, the function
1928 name always refers to function calls. Therefore, the name is
1929 not allowed to appear in specification statements. */
1930 if (gfc_current_state () == COMP_FUNCTION
1931 && gfc_current_block () != NULL
1932 && gfc_current_block ()->result != NULL
1933 && gfc_current_block ()->result != gfc_current_block ()
1934 && strcmp (gfc_current_block ()->name, name) == 0)
1936 gfc_error ("Function name '%s' not allowed at %C", name);
1937 m = MATCH_ERROR;
1938 goto cleanup;
1941 /* We allow old-style initializations of the form
1942 integer i /2/, j(4) /3*3, 1/
1943 (if no colon has been seen). These are different from data
1944 statements in that initializers are only allowed to apply to the
1945 variable immediately preceding, i.e.
1946 integer i, j /1, 2/
1947 is not allowed. Therefore we have to do some work manually, that
1948 could otherwise be left to the matchers for DATA statements. */
1950 if (!colon_seen && gfc_match (" /") == MATCH_YES)
1952 if (gfc_notify_std (GFC_STD_GNU, "Extension: Old-style "
1953 "initialization at %C") == FAILURE)
1954 return MATCH_ERROR;
1956 return match_old_style_init (name);
1959 /* The double colon must be present in order to have initializers.
1960 Otherwise the statement is ambiguous with an assignment statement. */
1961 if (colon_seen)
1963 if (gfc_match (" =>") == MATCH_YES)
1965 if (!current_attr.pointer)
1967 gfc_error ("Initialization at %C isn't for a pointer variable");
1968 m = MATCH_ERROR;
1969 goto cleanup;
1972 m = match_pointer_init (&initializer, 0);
1973 if (m != MATCH_YES)
1974 goto cleanup;
1976 else if (gfc_match_char ('=') == MATCH_YES)
1978 if (current_attr.pointer)
1980 gfc_error ("Pointer initialization at %C requires '=>', "
1981 "not '='");
1982 m = MATCH_ERROR;
1983 goto cleanup;
1986 m = gfc_match_init_expr (&initializer);
1987 if (m == MATCH_NO)
1989 gfc_error ("Expected an initialization expression at %C");
1990 m = MATCH_ERROR;
1993 if (current_attr.flavor != FL_PARAMETER && gfc_pure (NULL)
1994 && gfc_state_stack->state != COMP_DERIVED)
1996 gfc_error ("Initialization of variable at %C is not allowed in "
1997 "a PURE procedure");
1998 m = MATCH_ERROR;
2001 if (m != MATCH_YES)
2002 goto cleanup;
2006 if (initializer != NULL && current_attr.allocatable
2007 && gfc_current_state () == COMP_DERIVED)
2009 gfc_error ("Initialization of allocatable component at %C is not "
2010 "allowed");
2011 m = MATCH_ERROR;
2012 goto cleanup;
2015 /* Add the initializer. Note that it is fine if initializer is
2016 NULL here, because we sometimes also need to check if a
2017 declaration *must* have an initialization expression. */
2018 if (gfc_current_state () != COMP_DERIVED)
2019 t = add_init_expr_to_sym (name, &initializer, &var_locus);
2020 else
2022 if (current_ts.type == BT_DERIVED
2023 && !current_attr.pointer && !initializer)
2024 initializer = gfc_default_initializer (&current_ts);
2025 t = build_struct (name, cl, &initializer, &as);
2028 m = (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
2030 cleanup:
2031 /* Free stuff up and return. */
2032 gfc_free_expr (initializer);
2033 gfc_free_array_spec (as);
2035 return m;
2039 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2040 This assumes that the byte size is equal to the kind number for
2041 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2043 match
2044 gfc_match_old_kind_spec (gfc_typespec *ts)
2046 match m;
2047 int original_kind;
2049 if (gfc_match_char ('*') != MATCH_YES)
2050 return MATCH_NO;
2052 m = gfc_match_small_literal_int (&ts->kind, NULL);
2053 if (m != MATCH_YES)
2054 return MATCH_ERROR;
2056 original_kind = ts->kind;
2058 /* Massage the kind numbers for complex types. */
2059 if (ts->type == BT_COMPLEX)
2061 if (ts->kind % 2)
2063 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2064 gfc_basic_typename (ts->type), original_kind);
2065 return MATCH_ERROR;
2067 ts->kind /= 2;
2070 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2072 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2073 gfc_basic_typename (ts->type), original_kind);
2074 return MATCH_ERROR;
2077 if (gfc_notify_std (GFC_STD_GNU, "Nonstandard type declaration %s*%d at %C",
2078 gfc_basic_typename (ts->type), original_kind) == FAILURE)
2079 return MATCH_ERROR;
2081 return MATCH_YES;
2085 /* Match a kind specification. Since kinds are generally optional, we
2086 usually return MATCH_NO if something goes wrong. If a "kind="
2087 string is found, then we know we have an error. */
2089 match
2090 gfc_match_kind_spec (gfc_typespec *ts, bool kind_expr_only)
2092 locus where, loc;
2093 gfc_expr *e;
2094 match m, n;
2095 char c;
2096 const char *msg;
2098 m = MATCH_NO;
2099 n = MATCH_YES;
2100 e = NULL;
2102 where = loc = gfc_current_locus;
2104 if (kind_expr_only)
2105 goto kind_expr;
2107 if (gfc_match_char ('(') == MATCH_NO)
2108 return MATCH_NO;
2110 /* Also gobbles optional text. */
2111 if (gfc_match (" kind = ") == MATCH_YES)
2112 m = MATCH_ERROR;
2114 loc = gfc_current_locus;
2116 kind_expr:
2117 n = gfc_match_init_expr (&e);
2119 if (n != MATCH_YES)
2121 if (gfc_matching_function)
2123 /* The function kind expression might include use associated or
2124 imported parameters and try again after the specification
2125 expressions..... */
2126 if (gfc_match_char (')') != MATCH_YES)
2128 gfc_error ("Missing right parenthesis at %C");
2129 m = MATCH_ERROR;
2130 goto no_match;
2133 gfc_free_expr (e);
2134 gfc_undo_symbols ();
2135 return MATCH_YES;
2137 else
2139 /* ....or else, the match is real. */
2140 if (n == MATCH_NO)
2141 gfc_error ("Expected initialization expression at %C");
2142 if (n != MATCH_YES)
2143 return MATCH_ERROR;
2147 if (e->rank != 0)
2149 gfc_error ("Expected scalar initialization expression at %C");
2150 m = MATCH_ERROR;
2151 goto no_match;
2154 msg = gfc_extract_int (e, &ts->kind);
2156 if (msg != NULL)
2158 gfc_error (msg);
2159 m = MATCH_ERROR;
2160 goto no_match;
2163 /* Before throwing away the expression, let's see if we had a
2164 C interoperable kind (and store the fact). */
2165 if (e->ts.is_c_interop == 1)
2167 /* Mark this as c interoperable if being declared with one
2168 of the named constants from iso_c_binding. */
2169 ts->is_c_interop = e->ts.is_iso_c;
2170 ts->f90_type = e->ts.f90_type;
2173 gfc_free_expr (e);
2174 e = NULL;
2176 /* Ignore errors to this point, if we've gotten here. This means
2177 we ignore the m=MATCH_ERROR from above. */
2178 if (gfc_validate_kind (ts->type, ts->kind, true) < 0)
2180 gfc_error ("Kind %d not supported for type %s at %C", ts->kind,
2181 gfc_basic_typename (ts->type));
2182 gfc_current_locus = where;
2183 return MATCH_ERROR;
2186 /* Warn if, e.g., c_int is used for a REAL variable, but not
2187 if, e.g., c_double is used for COMPLEX as the standard
2188 explicitly says that the kind type parameter for complex and real
2189 variable is the same, i.e. c_float == c_float_complex. */
2190 if (ts->f90_type != BT_UNKNOWN && ts->f90_type != ts->type
2191 && !((ts->f90_type == BT_REAL && ts->type == BT_COMPLEX)
2192 || (ts->f90_type == BT_COMPLEX && ts->type == BT_REAL)))
2193 gfc_warning_now ("C kind type parameter is for type %s but type at %L "
2194 "is %s", gfc_basic_typename (ts->f90_type), &where,
2195 gfc_basic_typename (ts->type));
2197 gfc_gobble_whitespace ();
2198 if ((c = gfc_next_ascii_char ()) != ')'
2199 && (ts->type != BT_CHARACTER || c != ','))
2201 if (ts->type == BT_CHARACTER)
2202 gfc_error ("Missing right parenthesis or comma at %C");
2203 else
2204 gfc_error ("Missing right parenthesis at %C");
2205 m = MATCH_ERROR;
2207 else
2208 /* All tests passed. */
2209 m = MATCH_YES;
2211 if(m == MATCH_ERROR)
2212 gfc_current_locus = where;
2214 /* Return what we know from the test(s). */
2215 return m;
2217 no_match:
2218 gfc_free_expr (e);
2219 gfc_current_locus = where;
2220 return m;
2224 static match
2225 match_char_kind (int * kind, int * is_iso_c)
2227 locus where;
2228 gfc_expr *e;
2229 match m, n;
2230 const char *msg;
2232 m = MATCH_NO;
2233 e = NULL;
2234 where = gfc_current_locus;
2236 n = gfc_match_init_expr (&e);
2238 if (n != MATCH_YES && gfc_matching_function)
2240 /* The expression might include use-associated or imported
2241 parameters and try again after the specification
2242 expressions. */
2243 gfc_free_expr (e);
2244 gfc_undo_symbols ();
2245 return MATCH_YES;
2248 if (n == MATCH_NO)
2249 gfc_error ("Expected initialization expression at %C");
2250 if (n != MATCH_YES)
2251 return MATCH_ERROR;
2253 if (e->rank != 0)
2255 gfc_error ("Expected scalar initialization expression at %C");
2256 m = MATCH_ERROR;
2257 goto no_match;
2260 msg = gfc_extract_int (e, kind);
2261 *is_iso_c = e->ts.is_iso_c;
2262 if (msg != NULL)
2264 gfc_error (msg);
2265 m = MATCH_ERROR;
2266 goto no_match;
2269 gfc_free_expr (e);
2271 /* Ignore errors to this point, if we've gotten here. This means
2272 we ignore the m=MATCH_ERROR from above. */
2273 if (gfc_validate_kind (BT_CHARACTER, *kind, true) < 0)
2275 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind);
2276 m = MATCH_ERROR;
2278 else
2279 /* All tests passed. */
2280 m = MATCH_YES;
2282 if (m == MATCH_ERROR)
2283 gfc_current_locus = where;
2285 /* Return what we know from the test(s). */
2286 return m;
2288 no_match:
2289 gfc_free_expr (e);
2290 gfc_current_locus = where;
2291 return m;
2295 /* Match the various kind/length specifications in a CHARACTER
2296 declaration. We don't return MATCH_NO. */
2298 match
2299 gfc_match_char_spec (gfc_typespec *ts)
2301 int kind, seen_length, is_iso_c;
2302 gfc_charlen *cl;
2303 gfc_expr *len;
2304 match m;
2305 bool deferred;
2307 len = NULL;
2308 seen_length = 0;
2309 kind = 0;
2310 is_iso_c = 0;
2311 deferred = false;
2313 /* Try the old-style specification first. */
2314 old_char_selector = 0;
2316 m = match_char_length (&len, &deferred);
2317 if (m != MATCH_NO)
2319 if (m == MATCH_YES)
2320 old_char_selector = 1;
2321 seen_length = 1;
2322 goto done;
2325 m = gfc_match_char ('(');
2326 if (m != MATCH_YES)
2328 m = MATCH_YES; /* Character without length is a single char. */
2329 goto done;
2332 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2333 if (gfc_match (" kind =") == MATCH_YES)
2335 m = match_char_kind (&kind, &is_iso_c);
2337 if (m == MATCH_ERROR)
2338 goto done;
2339 if (m == MATCH_NO)
2340 goto syntax;
2342 if (gfc_match (" , len =") == MATCH_NO)
2343 goto rparen;
2345 m = char_len_param_value (&len, &deferred);
2346 if (m == MATCH_NO)
2347 goto syntax;
2348 if (m == MATCH_ERROR)
2349 goto done;
2350 seen_length = 1;
2352 goto rparen;
2355 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2356 if (gfc_match (" len =") == MATCH_YES)
2358 m = char_len_param_value (&len, &deferred);
2359 if (m == MATCH_NO)
2360 goto syntax;
2361 if (m == MATCH_ERROR)
2362 goto done;
2363 seen_length = 1;
2365 if (gfc_match_char (')') == MATCH_YES)
2366 goto done;
2368 if (gfc_match (" , kind =") != MATCH_YES)
2369 goto syntax;
2371 if (match_char_kind (&kind, &is_iso_c) == MATCH_ERROR)
2372 goto done;
2374 goto rparen;
2377 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2378 m = char_len_param_value (&len, &deferred);
2379 if (m == MATCH_NO)
2380 goto syntax;
2381 if (m == MATCH_ERROR)
2382 goto done;
2383 seen_length = 1;
2385 m = gfc_match_char (')');
2386 if (m == MATCH_YES)
2387 goto done;
2389 if (gfc_match_char (',') != MATCH_YES)
2390 goto syntax;
2392 gfc_match (" kind ="); /* Gobble optional text. */
2394 m = match_char_kind (&kind, &is_iso_c);
2395 if (m == MATCH_ERROR)
2396 goto done;
2397 if (m == MATCH_NO)
2398 goto syntax;
2400 rparen:
2401 /* Require a right-paren at this point. */
2402 m = gfc_match_char (')');
2403 if (m == MATCH_YES)
2404 goto done;
2406 syntax:
2407 gfc_error ("Syntax error in CHARACTER declaration at %C");
2408 m = MATCH_ERROR;
2409 gfc_free_expr (len);
2410 return m;
2412 done:
2413 /* Deal with character functions after USE and IMPORT statements. */
2414 if (gfc_matching_function)
2416 gfc_free_expr (len);
2417 gfc_undo_symbols ();
2418 return MATCH_YES;
2421 if (m != MATCH_YES)
2423 gfc_free_expr (len);
2424 return m;
2427 /* Do some final massaging of the length values. */
2428 cl = gfc_new_charlen (gfc_current_ns, NULL);
2430 if (seen_length == 0)
2431 cl->length = gfc_get_int_expr (gfc_default_integer_kind, NULL, 1);
2432 else
2433 cl->length = len;
2435 ts->u.cl = cl;
2436 ts->kind = kind == 0 ? gfc_default_character_kind : kind;
2437 ts->deferred = deferred;
2439 /* We have to know if it was a c interoperable kind so we can
2440 do accurate type checking of bind(c) procs, etc. */
2441 if (kind != 0)
2442 /* Mark this as c interoperable if being declared with one
2443 of the named constants from iso_c_binding. */
2444 ts->is_c_interop = is_iso_c;
2445 else if (len != NULL)
2446 /* Here, we might have parsed something such as: character(c_char)
2447 In this case, the parsing code above grabs the c_char when
2448 looking for the length (line 1690, roughly). it's the last
2449 testcase for parsing the kind params of a character variable.
2450 However, it's not actually the length. this seems like it
2451 could be an error.
2452 To see if the user used a C interop kind, test the expr
2453 of the so called length, and see if it's C interoperable. */
2454 ts->is_c_interop = len->ts.is_iso_c;
2456 return MATCH_YES;
2460 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2461 structure to the matched specification. This is necessary for FUNCTION and
2462 IMPLICIT statements.
2464 If implicit_flag is nonzero, then we don't check for the optional
2465 kind specification. Not doing so is needed for matching an IMPLICIT
2466 statement correctly. */
2468 match
2469 gfc_match_decl_type_spec (gfc_typespec *ts, int implicit_flag)
2471 char name[GFC_MAX_SYMBOL_LEN + 1];
2472 gfc_symbol *sym;
2473 match m;
2474 char c;
2475 bool seen_deferred_kind, matched_type;
2477 /* A belt and braces check that the typespec is correctly being treated
2478 as a deferred characteristic association. */
2479 seen_deferred_kind = (gfc_current_state () == COMP_FUNCTION)
2480 && (gfc_current_block ()->result->ts.kind == -1)
2481 && (ts->kind == -1);
2482 gfc_clear_ts (ts);
2483 if (seen_deferred_kind)
2484 ts->kind = -1;
2486 /* Clear the current binding label, in case one is given. */
2487 curr_binding_label[0] = '\0';
2489 if (gfc_match (" byte") == MATCH_YES)
2491 if (gfc_notify_std (GFC_STD_GNU, "Extension: BYTE type at %C")
2492 == FAILURE)
2493 return MATCH_ERROR;
2495 if (gfc_validate_kind (BT_INTEGER, 1, true) < 0)
2497 gfc_error ("BYTE type used at %C "
2498 "is not available on the target machine");
2499 return MATCH_ERROR;
2502 ts->type = BT_INTEGER;
2503 ts->kind = 1;
2504 return MATCH_YES;
2508 m = gfc_match (" type ( %n", name);
2509 matched_type = (m == MATCH_YES);
2511 if ((matched_type && strcmp ("integer", name) == 0)
2512 || (!matched_type && gfc_match (" integer") == MATCH_YES))
2514 ts->type = BT_INTEGER;
2515 ts->kind = gfc_default_integer_kind;
2516 goto get_kind;
2519 if ((matched_type && strcmp ("character", name) == 0)
2520 || (!matched_type && gfc_match (" character") == MATCH_YES))
2522 if (matched_type
2523 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2524 "intrinsic-type-spec at %C") == FAILURE)
2525 return MATCH_ERROR;
2527 ts->type = BT_CHARACTER;
2528 if (implicit_flag == 0)
2529 m = gfc_match_char_spec (ts);
2530 else
2531 m = MATCH_YES;
2533 if (matched_type && m == MATCH_YES && gfc_match_char (')') != MATCH_YES)
2534 m = MATCH_ERROR;
2536 return m;
2539 if ((matched_type && strcmp ("real", name) == 0)
2540 || (!matched_type && gfc_match (" real") == MATCH_YES))
2542 ts->type = BT_REAL;
2543 ts->kind = gfc_default_real_kind;
2544 goto get_kind;
2547 if ((matched_type
2548 && (strcmp ("doubleprecision", name) == 0
2549 || (strcmp ("double", name) == 0
2550 && gfc_match (" precision") == MATCH_YES)))
2551 || (!matched_type && gfc_match (" double precision") == MATCH_YES))
2553 if (matched_type
2554 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2555 "intrinsic-type-spec at %C") == FAILURE)
2556 return MATCH_ERROR;
2557 if (matched_type && gfc_match_char (')') != MATCH_YES)
2558 return MATCH_ERROR;
2560 ts->type = BT_REAL;
2561 ts->kind = gfc_default_double_kind;
2562 return MATCH_YES;
2565 if ((matched_type && strcmp ("complex", name) == 0)
2566 || (!matched_type && gfc_match (" complex") == MATCH_YES))
2568 ts->type = BT_COMPLEX;
2569 ts->kind = gfc_default_complex_kind;
2570 goto get_kind;
2573 if ((matched_type
2574 && (strcmp ("doublecomplex", name) == 0
2575 || (strcmp ("double", name) == 0
2576 && gfc_match (" complex") == MATCH_YES)))
2577 || (!matched_type && gfc_match (" double complex") == MATCH_YES))
2579 if (gfc_notify_std (GFC_STD_GNU, "Extension: DOUBLE COMPLEX at %C")
2580 == FAILURE)
2581 return MATCH_ERROR;
2583 if (matched_type
2584 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2585 "intrinsic-type-spec at %C") == FAILURE)
2586 return MATCH_ERROR;
2588 if (matched_type && gfc_match_char (')') != MATCH_YES)
2589 return MATCH_ERROR;
2591 ts->type = BT_COMPLEX;
2592 ts->kind = gfc_default_double_kind;
2593 return MATCH_YES;
2596 if ((matched_type && strcmp ("logical", name) == 0)
2597 || (!matched_type && gfc_match (" logical") == MATCH_YES))
2599 ts->type = BT_LOGICAL;
2600 ts->kind = gfc_default_logical_kind;
2601 goto get_kind;
2604 if (matched_type)
2605 m = gfc_match_char (')');
2607 if (m == MATCH_YES)
2608 ts->type = BT_DERIVED;
2609 else
2611 m = gfc_match (" class ( %n )", name);
2612 if (m != MATCH_YES)
2613 return m;
2614 ts->type = BT_CLASS;
2616 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: CLASS statement at %C")
2617 == FAILURE)
2618 return MATCH_ERROR;
2621 /* Defer association of the derived type until the end of the
2622 specification block. However, if the derived type can be
2623 found, add it to the typespec. */
2624 if (gfc_matching_function)
2626 ts->u.derived = NULL;
2627 if (gfc_current_state () != COMP_INTERFACE
2628 && !gfc_find_symbol (name, NULL, 1, &sym) && sym)
2629 ts->u.derived = sym;
2630 return MATCH_YES;
2633 /* Search for the name but allow the components to be defined later. If
2634 type = -1, this typespec has been seen in a function declaration but
2635 the type could not be accessed at that point. */
2636 sym = NULL;
2637 if (ts->kind != -1 && gfc_get_ha_symbol (name, &sym))
2639 gfc_error ("Type name '%s' at %C is ambiguous", name);
2640 return MATCH_ERROR;
2642 else if (ts->kind == -1)
2644 int iface = gfc_state_stack->previous->state != COMP_INTERFACE
2645 || gfc_current_ns->has_import_set;
2646 if (gfc_find_symbol (name, NULL, iface, &sym))
2648 gfc_error ("Type name '%s' at %C is ambiguous", name);
2649 return MATCH_ERROR;
2652 ts->kind = 0;
2653 if (sym == NULL)
2654 return MATCH_NO;
2657 if (sym->attr.flavor != FL_DERIVED
2658 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
2659 return MATCH_ERROR;
2661 gfc_set_sym_referenced (sym);
2662 ts->u.derived = sym;
2664 return MATCH_YES;
2666 get_kind:
2667 if (matched_type
2668 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: TYPE with "
2669 "intrinsic-type-spec at %C") == FAILURE)
2670 return MATCH_ERROR;
2672 /* For all types except double, derived and character, look for an
2673 optional kind specifier. MATCH_NO is actually OK at this point. */
2674 if (implicit_flag == 1)
2676 if (matched_type && gfc_match_char (')') != MATCH_YES)
2677 return MATCH_ERROR;
2679 return MATCH_YES;
2682 if (gfc_current_form == FORM_FREE)
2684 c = gfc_peek_ascii_char ();
2685 if (!gfc_is_whitespace (c) && c != '*' && c != '('
2686 && c != ':' && c != ',')
2688 if (matched_type && c == ')')
2690 gfc_next_ascii_char ();
2691 return MATCH_YES;
2693 return MATCH_NO;
2697 m = gfc_match_kind_spec (ts, false);
2698 if (m == MATCH_NO && ts->type != BT_CHARACTER)
2699 m = gfc_match_old_kind_spec (ts);
2701 if (matched_type && gfc_match_char (')') != MATCH_YES)
2702 return MATCH_ERROR;
2704 /* Defer association of the KIND expression of function results
2705 until after USE and IMPORT statements. */
2706 if ((gfc_current_state () == COMP_NONE && gfc_error_flag_test ())
2707 || gfc_matching_function)
2708 return MATCH_YES;
2710 if (m == MATCH_NO)
2711 m = MATCH_YES; /* No kind specifier found. */
2713 return m;
2717 /* Match an IMPLICIT NONE statement. Actually, this statement is
2718 already matched in parse.c, or we would not end up here in the
2719 first place. So the only thing we need to check, is if there is
2720 trailing garbage. If not, the match is successful. */
2722 match
2723 gfc_match_implicit_none (void)
2725 return (gfc_match_eos () == MATCH_YES) ? MATCH_YES : MATCH_NO;
2729 /* Match the letter range(s) of an IMPLICIT statement. */
2731 static match
2732 match_implicit_range (void)
2734 char c, c1, c2;
2735 int inner;
2736 locus cur_loc;
2738 cur_loc = gfc_current_locus;
2740 gfc_gobble_whitespace ();
2741 c = gfc_next_ascii_char ();
2742 if (c != '(')
2744 gfc_error ("Missing character range in IMPLICIT at %C");
2745 goto bad;
2748 inner = 1;
2749 while (inner)
2751 gfc_gobble_whitespace ();
2752 c1 = gfc_next_ascii_char ();
2753 if (!ISALPHA (c1))
2754 goto bad;
2756 gfc_gobble_whitespace ();
2757 c = gfc_next_ascii_char ();
2759 switch (c)
2761 case ')':
2762 inner = 0; /* Fall through. */
2764 case ',':
2765 c2 = c1;
2766 break;
2768 case '-':
2769 gfc_gobble_whitespace ();
2770 c2 = gfc_next_ascii_char ();
2771 if (!ISALPHA (c2))
2772 goto bad;
2774 gfc_gobble_whitespace ();
2775 c = gfc_next_ascii_char ();
2777 if ((c != ',') && (c != ')'))
2778 goto bad;
2779 if (c == ')')
2780 inner = 0;
2782 break;
2784 default:
2785 goto bad;
2788 if (c1 > c2)
2790 gfc_error ("Letters must be in alphabetic order in "
2791 "IMPLICIT statement at %C");
2792 goto bad;
2795 /* See if we can add the newly matched range to the pending
2796 implicits from this IMPLICIT statement. We do not check for
2797 conflicts with whatever earlier IMPLICIT statements may have
2798 set. This is done when we've successfully finished matching
2799 the current one. */
2800 if (gfc_add_new_implicit_range (c1, c2) != SUCCESS)
2801 goto bad;
2804 return MATCH_YES;
2806 bad:
2807 gfc_syntax_error (ST_IMPLICIT);
2809 gfc_current_locus = cur_loc;
2810 return MATCH_ERROR;
2814 /* Match an IMPLICIT statement, storing the types for
2815 gfc_set_implicit() if the statement is accepted by the parser.
2816 There is a strange looking, but legal syntactic construction
2817 possible. It looks like:
2819 IMPLICIT INTEGER (a-b) (c-d)
2821 This is legal if "a-b" is a constant expression that happens to
2822 equal one of the legal kinds for integers. The real problem
2823 happens with an implicit specification that looks like:
2825 IMPLICIT INTEGER (a-b)
2827 In this case, a typespec matcher that is "greedy" (as most of the
2828 matchers are) gobbles the character range as a kindspec, leaving
2829 nothing left. We therefore have to go a bit more slowly in the
2830 matching process by inhibiting the kindspec checking during
2831 typespec matching and checking for a kind later. */
2833 match
2834 gfc_match_implicit (void)
2836 gfc_typespec ts;
2837 locus cur_loc;
2838 char c;
2839 match m;
2841 gfc_clear_ts (&ts);
2843 /* We don't allow empty implicit statements. */
2844 if (gfc_match_eos () == MATCH_YES)
2846 gfc_error ("Empty IMPLICIT statement at %C");
2847 return MATCH_ERROR;
2852 /* First cleanup. */
2853 gfc_clear_new_implicit ();
2855 /* A basic type is mandatory here. */
2856 m = gfc_match_decl_type_spec (&ts, 1);
2857 if (m == MATCH_ERROR)
2858 goto error;
2859 if (m == MATCH_NO)
2860 goto syntax;
2862 cur_loc = gfc_current_locus;
2863 m = match_implicit_range ();
2865 if (m == MATCH_YES)
2867 /* We may have <TYPE> (<RANGE>). */
2868 gfc_gobble_whitespace ();
2869 c = gfc_next_ascii_char ();
2870 if ((c == '\n') || (c == ','))
2872 /* Check for CHARACTER with no length parameter. */
2873 if (ts.type == BT_CHARACTER && !ts.u.cl)
2875 ts.kind = gfc_default_character_kind;
2876 ts.u.cl = gfc_new_charlen (gfc_current_ns, NULL);
2877 ts.u.cl->length = gfc_get_int_expr (gfc_default_integer_kind,
2878 NULL, 1);
2881 /* Record the Successful match. */
2882 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2883 return MATCH_ERROR;
2884 continue;
2887 gfc_current_locus = cur_loc;
2890 /* Discard the (incorrectly) matched range. */
2891 gfc_clear_new_implicit ();
2893 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2894 if (ts.type == BT_CHARACTER)
2895 m = gfc_match_char_spec (&ts);
2896 else
2898 m = gfc_match_kind_spec (&ts, false);
2899 if (m == MATCH_NO)
2901 m = gfc_match_old_kind_spec (&ts);
2902 if (m == MATCH_ERROR)
2903 goto error;
2904 if (m == MATCH_NO)
2905 goto syntax;
2908 if (m == MATCH_ERROR)
2909 goto error;
2911 m = match_implicit_range ();
2912 if (m == MATCH_ERROR)
2913 goto error;
2914 if (m == MATCH_NO)
2915 goto syntax;
2917 gfc_gobble_whitespace ();
2918 c = gfc_next_ascii_char ();
2919 if ((c != '\n') && (c != ','))
2920 goto syntax;
2922 if (gfc_merge_new_implicit (&ts) != SUCCESS)
2923 return MATCH_ERROR;
2925 while (c == ',');
2927 return MATCH_YES;
2929 syntax:
2930 gfc_syntax_error (ST_IMPLICIT);
2932 error:
2933 return MATCH_ERROR;
2937 match
2938 gfc_match_import (void)
2940 char name[GFC_MAX_SYMBOL_LEN + 1];
2941 match m;
2942 gfc_symbol *sym;
2943 gfc_symtree *st;
2945 if (gfc_current_ns->proc_name == NULL
2946 || gfc_current_ns->proc_name->attr.if_source != IFSRC_IFBODY)
2948 gfc_error ("IMPORT statement at %C only permitted in "
2949 "an INTERFACE body");
2950 return MATCH_ERROR;
2953 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: IMPORT statement at %C")
2954 == FAILURE)
2955 return MATCH_ERROR;
2957 if (gfc_match_eos () == MATCH_YES)
2959 /* All host variables should be imported. */
2960 gfc_current_ns->has_import_set = 1;
2961 return MATCH_YES;
2964 if (gfc_match (" ::") == MATCH_YES)
2966 if (gfc_match_eos () == MATCH_YES)
2968 gfc_error ("Expecting list of named entities at %C");
2969 return MATCH_ERROR;
2973 for(;;)
2975 m = gfc_match (" %n", name);
2976 switch (m)
2978 case MATCH_YES:
2979 if (gfc_current_ns->parent != NULL
2980 && gfc_find_symbol (name, gfc_current_ns->parent, 1, &sym))
2982 gfc_error ("Type name '%s' at %C is ambiguous", name);
2983 return MATCH_ERROR;
2985 else if (gfc_current_ns->proc_name->ns->parent != NULL
2986 && gfc_find_symbol (name,
2987 gfc_current_ns->proc_name->ns->parent,
2988 1, &sym))
2990 gfc_error ("Type name '%s' at %C is ambiguous", name);
2991 return MATCH_ERROR;
2994 if (sym == NULL)
2996 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2997 "at %C - does not exist.", name);
2998 return MATCH_ERROR;
3001 if (gfc_find_symtree (gfc_current_ns->sym_root,name))
3003 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
3004 "at %C.", name);
3005 goto next_item;
3008 st = gfc_new_symtree (&gfc_current_ns->sym_root, sym->name);
3009 st->n.sym = sym;
3010 sym->refs++;
3011 sym->attr.imported = 1;
3013 goto next_item;
3015 case MATCH_NO:
3016 break;
3018 case MATCH_ERROR:
3019 return MATCH_ERROR;
3022 next_item:
3023 if (gfc_match_eos () == MATCH_YES)
3024 break;
3025 if (gfc_match_char (',') != MATCH_YES)
3026 goto syntax;
3029 return MATCH_YES;
3031 syntax:
3032 gfc_error ("Syntax error in IMPORT statement at %C");
3033 return MATCH_ERROR;
3037 /* A minimal implementation of gfc_match without whitespace, escape
3038 characters or variable arguments. Returns true if the next
3039 characters match the TARGET template exactly. */
3041 static bool
3042 match_string_p (const char *target)
3044 const char *p;
3046 for (p = target; *p; p++)
3047 if ((char) gfc_next_ascii_char () != *p)
3048 return false;
3049 return true;
3052 /* Matches an attribute specification including array specs. If
3053 successful, leaves the variables current_attr and current_as
3054 holding the specification. Also sets the colon_seen variable for
3055 later use by matchers associated with initializations.
3057 This subroutine is a little tricky in the sense that we don't know
3058 if we really have an attr-spec until we hit the double colon.
3059 Until that time, we can only return MATCH_NO. This forces us to
3060 check for duplicate specification at this level. */
3062 static match
3063 match_attr_spec (void)
3065 /* Modifiers that can exist in a type statement. */
3066 typedef enum
3067 { GFC_DECL_BEGIN = 0,
3068 DECL_ALLOCATABLE = GFC_DECL_BEGIN, DECL_DIMENSION, DECL_EXTERNAL,
3069 DECL_IN, DECL_OUT, DECL_INOUT, DECL_INTRINSIC, DECL_OPTIONAL,
3070 DECL_PARAMETER, DECL_POINTER, DECL_PROTECTED, DECL_PRIVATE,
3071 DECL_PUBLIC, DECL_SAVE, DECL_TARGET, DECL_VALUE, DECL_VOLATILE,
3072 DECL_IS_BIND_C, DECL_CODIMENSION, DECL_ASYNCHRONOUS, DECL_CONTIGUOUS,
3073 DECL_NONE, GFC_DECL_END /* Sentinel */
3075 decl_types;
3077 /* GFC_DECL_END is the sentinel, index starts at 0. */
3078 #define NUM_DECL GFC_DECL_END
3080 locus start, seen_at[NUM_DECL];
3081 int seen[NUM_DECL];
3082 unsigned int d;
3083 const char *attr;
3084 match m;
3085 gfc_try t;
3087 gfc_clear_attr (&current_attr);
3088 start = gfc_current_locus;
3090 current_as = NULL;
3091 colon_seen = 0;
3093 /* See if we get all of the keywords up to the final double colon. */
3094 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3095 seen[d] = 0;
3097 for (;;)
3099 char ch;
3101 d = DECL_NONE;
3102 gfc_gobble_whitespace ();
3104 ch = gfc_next_ascii_char ();
3105 if (ch == ':')
3107 /* This is the successful exit condition for the loop. */
3108 if (gfc_next_ascii_char () == ':')
3109 break;
3111 else if (ch == ',')
3113 gfc_gobble_whitespace ();
3114 switch (gfc_peek_ascii_char ())
3116 case 'a':
3117 gfc_next_ascii_char ();
3118 switch (gfc_next_ascii_char ())
3120 case 'l':
3121 if (match_string_p ("locatable"))
3123 /* Matched "allocatable". */
3124 d = DECL_ALLOCATABLE;
3126 break;
3128 case 's':
3129 if (match_string_p ("ynchronous"))
3131 /* Matched "asynchronous". */
3132 d = DECL_ASYNCHRONOUS;
3134 break;
3136 break;
3138 case 'b':
3139 /* Try and match the bind(c). */
3140 m = gfc_match_bind_c (NULL, true);
3141 if (m == MATCH_YES)
3142 d = DECL_IS_BIND_C;
3143 else if (m == MATCH_ERROR)
3144 goto cleanup;
3145 break;
3147 case 'c':
3148 gfc_next_ascii_char ();
3149 if ('o' != gfc_next_ascii_char ())
3150 break;
3151 switch (gfc_next_ascii_char ())
3153 case 'd':
3154 if (match_string_p ("imension"))
3156 d = DECL_CODIMENSION;
3157 break;
3159 case 'n':
3160 if (match_string_p ("tiguous"))
3162 d = DECL_CONTIGUOUS;
3163 break;
3166 break;
3168 case 'd':
3169 if (match_string_p ("dimension"))
3170 d = DECL_DIMENSION;
3171 break;
3173 case 'e':
3174 if (match_string_p ("external"))
3175 d = DECL_EXTERNAL;
3176 break;
3178 case 'i':
3179 if (match_string_p ("int"))
3181 ch = gfc_next_ascii_char ();
3182 if (ch == 'e')
3184 if (match_string_p ("nt"))
3186 /* Matched "intent". */
3187 /* TODO: Call match_intent_spec from here. */
3188 if (gfc_match (" ( in out )") == MATCH_YES)
3189 d = DECL_INOUT;
3190 else if (gfc_match (" ( in )") == MATCH_YES)
3191 d = DECL_IN;
3192 else if (gfc_match (" ( out )") == MATCH_YES)
3193 d = DECL_OUT;
3196 else if (ch == 'r')
3198 if (match_string_p ("insic"))
3200 /* Matched "intrinsic". */
3201 d = DECL_INTRINSIC;
3205 break;
3207 case 'o':
3208 if (match_string_p ("optional"))
3209 d = DECL_OPTIONAL;
3210 break;
3212 case 'p':
3213 gfc_next_ascii_char ();
3214 switch (gfc_next_ascii_char ())
3216 case 'a':
3217 if (match_string_p ("rameter"))
3219 /* Matched "parameter". */
3220 d = DECL_PARAMETER;
3222 break;
3224 case 'o':
3225 if (match_string_p ("inter"))
3227 /* Matched "pointer". */
3228 d = DECL_POINTER;
3230 break;
3232 case 'r':
3233 ch = gfc_next_ascii_char ();
3234 if (ch == 'i')
3236 if (match_string_p ("vate"))
3238 /* Matched "private". */
3239 d = DECL_PRIVATE;
3242 else if (ch == 'o')
3244 if (match_string_p ("tected"))
3246 /* Matched "protected". */
3247 d = DECL_PROTECTED;
3250 break;
3252 case 'u':
3253 if (match_string_p ("blic"))
3255 /* Matched "public". */
3256 d = DECL_PUBLIC;
3258 break;
3260 break;
3262 case 's':
3263 if (match_string_p ("save"))
3264 d = DECL_SAVE;
3265 break;
3267 case 't':
3268 if (match_string_p ("target"))
3269 d = DECL_TARGET;
3270 break;
3272 case 'v':
3273 gfc_next_ascii_char ();
3274 ch = gfc_next_ascii_char ();
3275 if (ch == 'a')
3277 if (match_string_p ("lue"))
3279 /* Matched "value". */
3280 d = DECL_VALUE;
3283 else if (ch == 'o')
3285 if (match_string_p ("latile"))
3287 /* Matched "volatile". */
3288 d = DECL_VOLATILE;
3291 break;
3295 /* No double colon and no recognizable decl_type, so assume that
3296 we've been looking at something else the whole time. */
3297 if (d == DECL_NONE)
3299 m = MATCH_NO;
3300 goto cleanup;
3303 /* Check to make sure any parens are paired up correctly. */
3304 if (gfc_match_parens () == MATCH_ERROR)
3306 m = MATCH_ERROR;
3307 goto cleanup;
3310 seen[d]++;
3311 seen_at[d] = gfc_current_locus;
3313 if (d == DECL_DIMENSION || d == DECL_CODIMENSION)
3315 gfc_array_spec *as = NULL;
3317 m = gfc_match_array_spec (&as, d == DECL_DIMENSION,
3318 d == DECL_CODIMENSION);
3320 if (current_as == NULL)
3321 current_as = as;
3322 else if (m == MATCH_YES)
3324 merge_array_spec (as, current_as, false);
3325 gfc_free (as);
3328 if (m == MATCH_NO)
3330 if (d == DECL_CODIMENSION)
3331 gfc_error ("Missing codimension specification at %C");
3332 else
3333 gfc_error ("Missing dimension specification at %C");
3334 m = MATCH_ERROR;
3337 if (m == MATCH_ERROR)
3338 goto cleanup;
3342 /* Since we've seen a double colon, we have to be looking at an
3343 attr-spec. This means that we can now issue errors. */
3344 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3345 if (seen[d] > 1)
3347 switch (d)
3349 case DECL_ALLOCATABLE:
3350 attr = "ALLOCATABLE";
3351 break;
3352 case DECL_ASYNCHRONOUS:
3353 attr = "ASYNCHRONOUS";
3354 break;
3355 case DECL_CODIMENSION:
3356 attr = "CODIMENSION";
3357 break;
3358 case DECL_CONTIGUOUS:
3359 attr = "CONTIGUOUS";
3360 break;
3361 case DECL_DIMENSION:
3362 attr = "DIMENSION";
3363 break;
3364 case DECL_EXTERNAL:
3365 attr = "EXTERNAL";
3366 break;
3367 case DECL_IN:
3368 attr = "INTENT (IN)";
3369 break;
3370 case DECL_OUT:
3371 attr = "INTENT (OUT)";
3372 break;
3373 case DECL_INOUT:
3374 attr = "INTENT (IN OUT)";
3375 break;
3376 case DECL_INTRINSIC:
3377 attr = "INTRINSIC";
3378 break;
3379 case DECL_OPTIONAL:
3380 attr = "OPTIONAL";
3381 break;
3382 case DECL_PARAMETER:
3383 attr = "PARAMETER";
3384 break;
3385 case DECL_POINTER:
3386 attr = "POINTER";
3387 break;
3388 case DECL_PROTECTED:
3389 attr = "PROTECTED";
3390 break;
3391 case DECL_PRIVATE:
3392 attr = "PRIVATE";
3393 break;
3394 case DECL_PUBLIC:
3395 attr = "PUBLIC";
3396 break;
3397 case DECL_SAVE:
3398 attr = "SAVE";
3399 break;
3400 case DECL_TARGET:
3401 attr = "TARGET";
3402 break;
3403 case DECL_IS_BIND_C:
3404 attr = "IS_BIND_C";
3405 break;
3406 case DECL_VALUE:
3407 attr = "VALUE";
3408 break;
3409 case DECL_VOLATILE:
3410 attr = "VOLATILE";
3411 break;
3412 default:
3413 attr = NULL; /* This shouldn't happen. */
3416 gfc_error ("Duplicate %s attribute at %L", attr, &seen_at[d]);
3417 m = MATCH_ERROR;
3418 goto cleanup;
3421 /* Now that we've dealt with duplicate attributes, add the attributes
3422 to the current attribute. */
3423 for (d = GFC_DECL_BEGIN; d != GFC_DECL_END; d++)
3425 if (seen[d] == 0)
3426 continue;
3428 if (gfc_current_state () == COMP_DERIVED
3429 && d != DECL_DIMENSION && d != DECL_CODIMENSION
3430 && d != DECL_POINTER && d != DECL_PRIVATE
3431 && d != DECL_PUBLIC && d != DECL_CONTIGUOUS && d != DECL_NONE)
3433 if (d == DECL_ALLOCATABLE)
3435 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ALLOCATABLE "
3436 "attribute at %C in a TYPE definition")
3437 == FAILURE)
3439 m = MATCH_ERROR;
3440 goto cleanup;
3443 else
3445 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3446 &seen_at[d]);
3447 m = MATCH_ERROR;
3448 goto cleanup;
3452 if ((d == DECL_PRIVATE || d == DECL_PUBLIC)
3453 && gfc_current_state () != COMP_MODULE)
3455 if (d == DECL_PRIVATE)
3456 attr = "PRIVATE";
3457 else
3458 attr = "PUBLIC";
3459 if (gfc_current_state () == COMP_DERIVED
3460 && gfc_state_stack->previous
3461 && gfc_state_stack->previous->state == COMP_MODULE)
3463 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Attribute %s "
3464 "at %L in a TYPE definition", attr,
3465 &seen_at[d])
3466 == FAILURE)
3468 m = MATCH_ERROR;
3469 goto cleanup;
3472 else
3474 gfc_error ("%s attribute at %L is not allowed outside of the "
3475 "specification part of a module", attr, &seen_at[d]);
3476 m = MATCH_ERROR;
3477 goto cleanup;
3481 switch (d)
3483 case DECL_ALLOCATABLE:
3484 t = gfc_add_allocatable (&current_attr, &seen_at[d]);
3485 break;
3487 case DECL_ASYNCHRONOUS:
3488 if (gfc_notify_std (GFC_STD_F2003,
3489 "Fortran 2003: ASYNCHRONOUS attribute at %C")
3490 == FAILURE)
3491 t = FAILURE;
3492 else
3493 t = gfc_add_asynchronous (&current_attr, NULL, &seen_at[d]);
3494 break;
3496 case DECL_CODIMENSION:
3497 t = gfc_add_codimension (&current_attr, NULL, &seen_at[d]);
3498 break;
3500 case DECL_CONTIGUOUS:
3501 if (gfc_notify_std (GFC_STD_F2008,
3502 "Fortran 2008: CONTIGUOUS attribute at %C")
3503 == FAILURE)
3504 t = FAILURE;
3505 else
3506 t = gfc_add_contiguous (&current_attr, NULL, &seen_at[d]);
3507 break;
3509 case DECL_DIMENSION:
3510 t = gfc_add_dimension (&current_attr, NULL, &seen_at[d]);
3511 break;
3513 case DECL_EXTERNAL:
3514 t = gfc_add_external (&current_attr, &seen_at[d]);
3515 break;
3517 case DECL_IN:
3518 t = gfc_add_intent (&current_attr, INTENT_IN, &seen_at[d]);
3519 break;
3521 case DECL_OUT:
3522 t = gfc_add_intent (&current_attr, INTENT_OUT, &seen_at[d]);
3523 break;
3525 case DECL_INOUT:
3526 t = gfc_add_intent (&current_attr, INTENT_INOUT, &seen_at[d]);
3527 break;
3529 case DECL_INTRINSIC:
3530 t = gfc_add_intrinsic (&current_attr, &seen_at[d]);
3531 break;
3533 case DECL_OPTIONAL:
3534 t = gfc_add_optional (&current_attr, &seen_at[d]);
3535 break;
3537 case DECL_PARAMETER:
3538 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, &seen_at[d]);
3539 break;
3541 case DECL_POINTER:
3542 t = gfc_add_pointer (&current_attr, &seen_at[d]);
3543 break;
3545 case DECL_PROTECTED:
3546 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
3548 gfc_error ("PROTECTED at %C only allowed in specification "
3549 "part of a module");
3550 t = FAILURE;
3551 break;
3554 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED "
3555 "attribute at %C")
3556 == FAILURE)
3557 t = FAILURE;
3558 else
3559 t = gfc_add_protected (&current_attr, NULL, &seen_at[d]);
3560 break;
3562 case DECL_PRIVATE:
3563 t = gfc_add_access (&current_attr, ACCESS_PRIVATE, NULL,
3564 &seen_at[d]);
3565 break;
3567 case DECL_PUBLIC:
3568 t = gfc_add_access (&current_attr, ACCESS_PUBLIC, NULL,
3569 &seen_at[d]);
3570 break;
3572 case DECL_SAVE:
3573 t = gfc_add_save (&current_attr, SAVE_EXPLICIT, NULL, &seen_at[d]);
3574 break;
3576 case DECL_TARGET:
3577 t = gfc_add_target (&current_attr, &seen_at[d]);
3578 break;
3580 case DECL_IS_BIND_C:
3581 t = gfc_add_is_bind_c(&current_attr, NULL, &seen_at[d], 0);
3582 break;
3584 case DECL_VALUE:
3585 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE attribute "
3586 "at %C")
3587 == FAILURE)
3588 t = FAILURE;
3589 else
3590 t = gfc_add_value (&current_attr, NULL, &seen_at[d]);
3591 break;
3593 case DECL_VOLATILE:
3594 if (gfc_notify_std (GFC_STD_F2003,
3595 "Fortran 2003: VOLATILE attribute at %C")
3596 == FAILURE)
3597 t = FAILURE;
3598 else
3599 t = gfc_add_volatile (&current_attr, NULL, &seen_at[d]);
3600 break;
3602 default:
3603 gfc_internal_error ("match_attr_spec(): Bad attribute");
3606 if (t == FAILURE)
3608 m = MATCH_ERROR;
3609 goto cleanup;
3613 /* Module variables implicitly have the SAVE attribute. */
3614 if (gfc_current_state () == COMP_MODULE && !current_attr.save)
3615 current_attr.save = SAVE_IMPLICIT;
3617 colon_seen = 1;
3618 return MATCH_YES;
3620 cleanup:
3621 gfc_current_locus = start;
3622 gfc_free_array_spec (current_as);
3623 current_as = NULL;
3624 return m;
3628 /* Set the binding label, dest_label, either with the binding label
3629 stored in the given gfc_typespec, ts, or if none was provided, it
3630 will be the symbol name in all lower case, as required by the draft
3631 (J3/04-007, section 15.4.1). If a binding label was given and
3632 there is more than one argument (num_idents), it is an error. */
3634 gfc_try
3635 set_binding_label (char *dest_label, const char *sym_name, int num_idents)
3637 if (num_idents > 1 && has_name_equals)
3639 gfc_error ("Multiple identifiers provided with "
3640 "single NAME= specifier at %C");
3641 return FAILURE;
3644 if (curr_binding_label[0] != '\0')
3646 /* Binding label given; store in temp holder til have sym. */
3647 strcpy (dest_label, curr_binding_label);
3649 else
3651 /* No binding label given, and the NAME= specifier did not exist,
3652 which means there was no NAME="". */
3653 if (sym_name != NULL && has_name_equals == 0)
3654 strcpy (dest_label, sym_name);
3657 return SUCCESS;
3661 /* Set the status of the given common block as being BIND(C) or not,
3662 depending on the given parameter, is_bind_c. */
3664 void
3665 set_com_block_bind_c (gfc_common_head *com_block, int is_bind_c)
3667 com_block->is_bind_c = is_bind_c;
3668 return;
3672 /* Verify that the given gfc_typespec is for a C interoperable type. */
3674 gfc_try
3675 verify_c_interop (gfc_typespec *ts)
3677 if (ts->type == BT_DERIVED && ts->u.derived != NULL)
3678 return (ts->u.derived->ts.is_c_interop || ts->u.derived->attr.is_bind_c)
3679 ? SUCCESS : FAILURE;
3680 else if (ts->is_c_interop != 1)
3681 return FAILURE;
3683 return SUCCESS;
3687 /* Verify that the variables of a given common block, which has been
3688 defined with the attribute specifier bind(c), to be of a C
3689 interoperable type. Errors will be reported here, if
3690 encountered. */
3692 gfc_try
3693 verify_com_block_vars_c_interop (gfc_common_head *com_block)
3695 gfc_symbol *curr_sym = NULL;
3696 gfc_try retval = SUCCESS;
3698 curr_sym = com_block->head;
3700 /* Make sure we have at least one symbol. */
3701 if (curr_sym == NULL)
3702 return retval;
3704 /* Here we know we have a symbol, so we'll execute this loop
3705 at least once. */
3708 /* The second to last param, 1, says this is in a common block. */
3709 retval = verify_bind_c_sym (curr_sym, &(curr_sym->ts), 1, com_block);
3710 curr_sym = curr_sym->common_next;
3711 } while (curr_sym != NULL);
3713 return retval;
3717 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3718 an appropriate error message is reported. */
3720 gfc_try
3721 verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
3722 int is_in_common, gfc_common_head *com_block)
3724 bool bind_c_function = false;
3725 gfc_try retval = SUCCESS;
3727 if (tmp_sym->attr.function && tmp_sym->attr.is_bind_c)
3728 bind_c_function = true;
3730 if (tmp_sym->attr.function && tmp_sym->result != NULL)
3732 tmp_sym = tmp_sym->result;
3733 /* Make sure it wasn't an implicitly typed result. */
3734 if (tmp_sym->attr.implicit_type)
3736 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3737 "%L may not be C interoperable", tmp_sym->name,
3738 &tmp_sym->declared_at);
3739 tmp_sym->ts.f90_type = tmp_sym->ts.type;
3740 /* Mark it as C interoperable to prevent duplicate warnings. */
3741 tmp_sym->ts.is_c_interop = 1;
3742 tmp_sym->attr.is_c_interop = 1;
3746 /* Here, we know we have the bind(c) attribute, so if we have
3747 enough type info, then verify that it's a C interop kind.
3748 The info could be in the symbol already, or possibly still in
3749 the given ts (current_ts), so look in both. */
3750 if (tmp_sym->ts.type != BT_UNKNOWN || ts->type != BT_UNKNOWN)
3752 if (verify_c_interop (&(tmp_sym->ts)) != SUCCESS)
3754 /* See if we're dealing with a sym in a common block or not. */
3755 if (is_in_common == 1)
3757 gfc_warning ("Variable '%s' in common block '%s' at %L "
3758 "may not be a C interoperable "
3759 "kind though common block '%s' is BIND(C)",
3760 tmp_sym->name, com_block->name,
3761 &(tmp_sym->declared_at), com_block->name);
3763 else
3765 if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
3766 gfc_error ("Type declaration '%s' at %L is not C "
3767 "interoperable but it is BIND(C)",
3768 tmp_sym->name, &(tmp_sym->declared_at));
3769 else
3770 gfc_warning ("Variable '%s' at %L "
3771 "may not be a C interoperable "
3772 "kind but it is bind(c)",
3773 tmp_sym->name, &(tmp_sym->declared_at));
3777 /* Variables declared w/in a common block can't be bind(c)
3778 since there's no way for C to see these variables, so there's
3779 semantically no reason for the attribute. */
3780 if (is_in_common == 1 && tmp_sym->attr.is_bind_c == 1)
3782 gfc_error ("Variable '%s' in common block '%s' at "
3783 "%L cannot be declared with BIND(C) "
3784 "since it is not a global",
3785 tmp_sym->name, com_block->name,
3786 &(tmp_sym->declared_at));
3787 retval = FAILURE;
3790 /* Scalar variables that are bind(c) can not have the pointer
3791 or allocatable attributes. */
3792 if (tmp_sym->attr.is_bind_c == 1)
3794 if (tmp_sym->attr.pointer == 1)
3796 gfc_error ("Variable '%s' at %L cannot have both the "
3797 "POINTER and BIND(C) attributes",
3798 tmp_sym->name, &(tmp_sym->declared_at));
3799 retval = FAILURE;
3802 if (tmp_sym->attr.allocatable == 1)
3804 gfc_error ("Variable '%s' at %L cannot have both the "
3805 "ALLOCATABLE and BIND(C) attributes",
3806 tmp_sym->name, &(tmp_sym->declared_at));
3807 retval = FAILURE;
3812 /* If it is a BIND(C) function, make sure the return value is a
3813 scalar value. The previous tests in this function made sure
3814 the type is interoperable. */
3815 if (bind_c_function && tmp_sym->as != NULL)
3816 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3817 "be an array", tmp_sym->name, &(tmp_sym->declared_at));
3819 /* BIND(C) functions can not return a character string. */
3820 if (bind_c_function && tmp_sym->ts.type == BT_CHARACTER)
3821 if (tmp_sym->ts.u.cl == NULL || tmp_sym->ts.u.cl->length == NULL
3822 || tmp_sym->ts.u.cl->length->expr_type != EXPR_CONSTANT
3823 || mpz_cmp_si (tmp_sym->ts.u.cl->length->value.integer, 1) != 0)
3824 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3825 "be a character string", tmp_sym->name,
3826 &(tmp_sym->declared_at));
3829 /* See if the symbol has been marked as private. If it has, make sure
3830 there is no binding label and warn the user if there is one. */
3831 if (tmp_sym->attr.access == ACCESS_PRIVATE
3832 && tmp_sym->binding_label[0] != '\0')
3833 /* Use gfc_warning_now because we won't say that the symbol fails
3834 just because of this. */
3835 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3836 "given the binding label '%s'", tmp_sym->name,
3837 &(tmp_sym->declared_at), tmp_sym->binding_label);
3839 return retval;
3843 /* Set the appropriate fields for a symbol that's been declared as
3844 BIND(C) (the is_bind_c flag and the binding label), and verify that
3845 the type is C interoperable. Errors are reported by the functions
3846 used to set/test these fields. */
3848 gfc_try
3849 set_verify_bind_c_sym (gfc_symbol *tmp_sym, int num_idents)
3851 gfc_try retval = SUCCESS;
3853 /* TODO: Do we need to make sure the vars aren't marked private? */
3855 /* Set the is_bind_c bit in symbol_attribute. */
3856 gfc_add_is_bind_c (&(tmp_sym->attr), tmp_sym->name, &gfc_current_locus, 0);
3858 if (set_binding_label (tmp_sym->binding_label, tmp_sym->name,
3859 num_idents) != SUCCESS)
3860 return FAILURE;
3862 return retval;
3866 /* Set the fields marking the given common block as BIND(C), including
3867 a binding label, and report any errors encountered. */
3869 gfc_try
3870 set_verify_bind_c_com_block (gfc_common_head *com_block, int num_idents)
3872 gfc_try retval = SUCCESS;
3874 /* destLabel, common name, typespec (which may have binding label). */
3875 if (set_binding_label (com_block->binding_label, com_block->name, num_idents)
3876 != SUCCESS)
3877 return FAILURE;
3879 /* Set the given common block (com_block) to being bind(c) (1). */
3880 set_com_block_bind_c (com_block, 1);
3882 return retval;
3886 /* Retrieve the list of one or more identifiers that the given bind(c)
3887 attribute applies to. */
3889 gfc_try
3890 get_bind_c_idents (void)
3892 char name[GFC_MAX_SYMBOL_LEN + 1];
3893 int num_idents = 0;
3894 gfc_symbol *tmp_sym = NULL;
3895 match found_id;
3896 gfc_common_head *com_block = NULL;
3898 if (gfc_match_name (name) == MATCH_YES)
3900 found_id = MATCH_YES;
3901 gfc_get_ha_symbol (name, &tmp_sym);
3903 else if (match_common_name (name) == MATCH_YES)
3905 found_id = MATCH_YES;
3906 com_block = gfc_get_common (name, 0);
3908 else
3910 gfc_error ("Need either entity or common block name for "
3911 "attribute specification statement at %C");
3912 return FAILURE;
3915 /* Save the current identifier and look for more. */
3918 /* Increment the number of identifiers found for this spec stmt. */
3919 num_idents++;
3921 /* Make sure we have a sym or com block, and verify that it can
3922 be bind(c). Set the appropriate field(s) and look for more
3923 identifiers. */
3924 if (tmp_sym != NULL || com_block != NULL)
3926 if (tmp_sym != NULL)
3928 if (set_verify_bind_c_sym (tmp_sym, num_idents)
3929 != SUCCESS)
3930 return FAILURE;
3932 else
3934 if (set_verify_bind_c_com_block(com_block, num_idents)
3935 != SUCCESS)
3936 return FAILURE;
3939 /* Look to see if we have another identifier. */
3940 tmp_sym = NULL;
3941 if (gfc_match_eos () == MATCH_YES)
3942 found_id = MATCH_NO;
3943 else if (gfc_match_char (',') != MATCH_YES)
3944 found_id = MATCH_NO;
3945 else if (gfc_match_name (name) == MATCH_YES)
3947 found_id = MATCH_YES;
3948 gfc_get_ha_symbol (name, &tmp_sym);
3950 else if (match_common_name (name) == MATCH_YES)
3952 found_id = MATCH_YES;
3953 com_block = gfc_get_common (name, 0);
3955 else
3957 gfc_error ("Missing entity or common block name for "
3958 "attribute specification statement at %C");
3959 return FAILURE;
3962 else
3964 gfc_internal_error ("Missing symbol");
3966 } while (found_id == MATCH_YES);
3968 /* if we get here we were successful */
3969 return SUCCESS;
3973 /* Try and match a BIND(C) attribute specification statement. */
3975 match
3976 gfc_match_bind_c_stmt (void)
3978 match found_match = MATCH_NO;
3979 gfc_typespec *ts;
3981 ts = &current_ts;
3983 /* This may not be necessary. */
3984 gfc_clear_ts (ts);
3985 /* Clear the temporary binding label holder. */
3986 curr_binding_label[0] = '\0';
3988 /* Look for the bind(c). */
3989 found_match = gfc_match_bind_c (NULL, true);
3991 if (found_match == MATCH_YES)
3993 /* Look for the :: now, but it is not required. */
3994 gfc_match (" :: ");
3996 /* Get the identifier(s) that needs to be updated. This may need to
3997 change to hand the flag(s) for the attr specified so all identifiers
3998 found can have all appropriate parts updated (assuming that the same
3999 spec stmt can have multiple attrs, such as both bind(c) and
4000 allocatable...). */
4001 if (get_bind_c_idents () != SUCCESS)
4002 /* Error message should have printed already. */
4003 return MATCH_ERROR;
4006 return found_match;
4010 /* Match a data declaration statement. */
4012 match
4013 gfc_match_data_decl (void)
4015 gfc_symbol *sym;
4016 match m;
4017 int elem;
4019 num_idents_on_line = 0;
4021 m = gfc_match_decl_type_spec (&current_ts, 0);
4022 if (m != MATCH_YES)
4023 return m;
4025 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4026 && gfc_current_state () != COMP_DERIVED)
4028 sym = gfc_use_derived (current_ts.u.derived);
4030 if (sym == NULL)
4032 m = MATCH_ERROR;
4033 goto cleanup;
4036 current_ts.u.derived = sym;
4039 m = match_attr_spec ();
4040 if (m == MATCH_ERROR)
4042 m = MATCH_NO;
4043 goto cleanup;
4046 if ((current_ts.type == BT_DERIVED || current_ts.type == BT_CLASS)
4047 && current_ts.u.derived->components == NULL
4048 && !current_ts.u.derived->attr.zero_comp)
4051 if (current_attr.pointer && gfc_current_state () == COMP_DERIVED)
4052 goto ok;
4054 gfc_find_symbol (current_ts.u.derived->name,
4055 current_ts.u.derived->ns->parent, 1, &sym);
4057 /* Any symbol that we find had better be a type definition
4058 which has its components defined. */
4059 if (sym != NULL && sym->attr.flavor == FL_DERIVED
4060 && (current_ts.u.derived->components != NULL
4061 || current_ts.u.derived->attr.zero_comp))
4062 goto ok;
4064 /* Now we have an error, which we signal, and then fix up
4065 because the knock-on is plain and simple confusing. */
4066 gfc_error_now ("Derived type at %C has not been previously defined "
4067 "and so cannot appear in a derived type definition");
4068 current_attr.pointer = 1;
4069 goto ok;
4073 /* If we have an old-style character declaration, and no new-style
4074 attribute specifications, then there a comma is optional between
4075 the type specification and the variable list. */
4076 if (m == MATCH_NO && current_ts.type == BT_CHARACTER && old_char_selector)
4077 gfc_match_char (',');
4079 /* Give the types/attributes to symbols that follow. Give the element
4080 a number so that repeat character length expressions can be copied. */
4081 elem = 1;
4082 for (;;)
4084 num_idents_on_line++;
4085 m = variable_decl (elem++);
4086 if (m == MATCH_ERROR)
4087 goto cleanup;
4088 if (m == MATCH_NO)
4089 break;
4091 if (gfc_match_eos () == MATCH_YES)
4092 goto cleanup;
4093 if (gfc_match_char (',') != MATCH_YES)
4094 break;
4097 if (gfc_error_flag_test () == 0)
4098 gfc_error ("Syntax error in data declaration at %C");
4099 m = MATCH_ERROR;
4101 gfc_free_data_all (gfc_current_ns);
4103 cleanup:
4104 gfc_free_array_spec (current_as);
4105 current_as = NULL;
4106 return m;
4110 /* Match a prefix associated with a function or subroutine
4111 declaration. If the typespec pointer is nonnull, then a typespec
4112 can be matched. Note that if nothing matches, MATCH_YES is
4113 returned (the null string was matched). */
4115 match
4116 gfc_match_prefix (gfc_typespec *ts)
4118 bool seen_type;
4119 bool seen_impure;
4120 bool found_prefix;
4122 gfc_clear_attr (&current_attr);
4123 seen_type = false;
4124 seen_impure = false;
4126 gcc_assert (!gfc_matching_prefix);
4127 gfc_matching_prefix = true;
4131 found_prefix = false;
4133 if (!seen_type && ts != NULL
4134 && gfc_match_decl_type_spec (ts, 0) == MATCH_YES
4135 && gfc_match_space () == MATCH_YES)
4138 seen_type = true;
4139 found_prefix = true;
4142 if (gfc_match ("elemental% ") == MATCH_YES)
4144 if (gfc_add_elemental (&current_attr, NULL) == FAILURE)
4145 goto error;
4147 found_prefix = true;
4150 if (gfc_match ("pure% ") == MATCH_YES)
4152 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
4153 goto error;
4155 found_prefix = true;
4158 if (gfc_match ("recursive% ") == MATCH_YES)
4160 if (gfc_add_recursive (&current_attr, NULL) == FAILURE)
4161 goto error;
4163 found_prefix = true;
4166 /* IMPURE is a somewhat special case, as it needs not set an actual
4167 attribute but rather only prevents ELEMENTAL routines from being
4168 automatically PURE. */
4169 if (gfc_match ("impure% ") == MATCH_YES)
4171 if (gfc_notify_std (GFC_STD_F2008,
4172 "Fortran 2008: IMPURE procedure at %C")
4173 == FAILURE)
4174 goto error;
4176 seen_impure = true;
4177 found_prefix = true;
4180 while (found_prefix);
4182 /* IMPURE and PURE must not both appear, of course. */
4183 if (seen_impure && current_attr.pure)
4185 gfc_error ("PURE and IMPURE must not appear both at %C");
4186 goto error;
4189 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4190 if (!seen_impure && current_attr.elemental && !current_attr.pure)
4192 if (gfc_add_pure (&current_attr, NULL) == FAILURE)
4193 goto error;
4196 /* At this point, the next item is not a prefix. */
4197 gcc_assert (gfc_matching_prefix);
4198 gfc_matching_prefix = false;
4199 return MATCH_YES;
4201 error:
4202 gcc_assert (gfc_matching_prefix);
4203 gfc_matching_prefix = false;
4204 return MATCH_ERROR;
4208 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4210 static gfc_try
4211 copy_prefix (symbol_attribute *dest, locus *where)
4213 if (current_attr.pure && gfc_add_pure (dest, where) == FAILURE)
4214 return FAILURE;
4216 if (current_attr.elemental && gfc_add_elemental (dest, where) == FAILURE)
4217 return FAILURE;
4219 if (current_attr.recursive && gfc_add_recursive (dest, where) == FAILURE)
4220 return FAILURE;
4222 return SUCCESS;
4226 /* Match a formal argument list. */
4228 match
4229 gfc_match_formal_arglist (gfc_symbol *progname, int st_flag, int null_flag)
4231 gfc_formal_arglist *head, *tail, *p, *q;
4232 char name[GFC_MAX_SYMBOL_LEN + 1];
4233 gfc_symbol *sym;
4234 match m;
4236 head = tail = NULL;
4238 if (gfc_match_char ('(') != MATCH_YES)
4240 if (null_flag)
4241 goto ok;
4242 return MATCH_NO;
4245 if (gfc_match_char (')') == MATCH_YES)
4246 goto ok;
4248 for (;;)
4250 if (gfc_match_char ('*') == MATCH_YES)
4251 sym = NULL;
4252 else
4254 m = gfc_match_name (name);
4255 if (m != MATCH_YES)
4256 goto cleanup;
4258 if (gfc_get_symbol (name, NULL, &sym))
4259 goto cleanup;
4262 p = gfc_get_formal_arglist ();
4264 if (head == NULL)
4265 head = tail = p;
4266 else
4268 tail->next = p;
4269 tail = p;
4272 tail->sym = sym;
4274 /* We don't add the VARIABLE flavor because the name could be a
4275 dummy procedure. We don't apply these attributes to formal
4276 arguments of statement functions. */
4277 if (sym != NULL && !st_flag
4278 && (gfc_add_dummy (&sym->attr, sym->name, NULL) == FAILURE
4279 || gfc_missing_attr (&sym->attr, NULL) == FAILURE))
4281 m = MATCH_ERROR;
4282 goto cleanup;
4285 /* The name of a program unit can be in a different namespace,
4286 so check for it explicitly. After the statement is accepted,
4287 the name is checked for especially in gfc_get_symbol(). */
4288 if (gfc_new_block != NULL && sym != NULL
4289 && strcmp (sym->name, gfc_new_block->name) == 0)
4291 gfc_error ("Name '%s' at %C is the name of the procedure",
4292 sym->name);
4293 m = MATCH_ERROR;
4294 goto cleanup;
4297 if (gfc_match_char (')') == MATCH_YES)
4298 goto ok;
4300 m = gfc_match_char (',');
4301 if (m != MATCH_YES)
4303 gfc_error ("Unexpected junk in formal argument list at %C");
4304 goto cleanup;
4309 /* Check for duplicate symbols in the formal argument list. */
4310 if (head != NULL)
4312 for (p = head; p->next; p = p->next)
4314 if (p->sym == NULL)
4315 continue;
4317 for (q = p->next; q; q = q->next)
4318 if (p->sym == q->sym)
4320 gfc_error ("Duplicate symbol '%s' in formal argument list "
4321 "at %C", p->sym->name);
4323 m = MATCH_ERROR;
4324 goto cleanup;
4329 if (gfc_add_explicit_interface (progname, IFSRC_DECL, head, NULL)
4330 == FAILURE)
4332 m = MATCH_ERROR;
4333 goto cleanup;
4336 return MATCH_YES;
4338 cleanup:
4339 gfc_free_formal_arglist (head);
4340 return m;
4344 /* Match a RESULT specification following a function declaration or
4345 ENTRY statement. Also matches the end-of-statement. */
4347 static match
4348 match_result (gfc_symbol *function, gfc_symbol **result)
4350 char name[GFC_MAX_SYMBOL_LEN + 1];
4351 gfc_symbol *r;
4352 match m;
4354 if (gfc_match (" result (") != MATCH_YES)
4355 return MATCH_NO;
4357 m = gfc_match_name (name);
4358 if (m != MATCH_YES)
4359 return m;
4361 /* Get the right paren, and that's it because there could be the
4362 bind(c) attribute after the result clause. */
4363 if (gfc_match_char(')') != MATCH_YES)
4365 /* TODO: should report the missing right paren here. */
4366 return MATCH_ERROR;
4369 if (strcmp (function->name, name) == 0)
4371 gfc_error ("RESULT variable at %C must be different than function name");
4372 return MATCH_ERROR;
4375 if (gfc_get_symbol (name, NULL, &r))
4376 return MATCH_ERROR;
4378 if (gfc_add_result (&r->attr, r->name, NULL) == FAILURE)
4379 return MATCH_ERROR;
4381 *result = r;
4383 return MATCH_YES;
4387 /* Match a function suffix, which could be a combination of a result
4388 clause and BIND(C), either one, or neither. The draft does not
4389 require them to come in a specific order. */
4391 match
4392 gfc_match_suffix (gfc_symbol *sym, gfc_symbol **result)
4394 match is_bind_c; /* Found bind(c). */
4395 match is_result; /* Found result clause. */
4396 match found_match; /* Status of whether we've found a good match. */
4397 char peek_char; /* Character we're going to peek at. */
4398 bool allow_binding_name;
4400 /* Initialize to having found nothing. */
4401 found_match = MATCH_NO;
4402 is_bind_c = MATCH_NO;
4403 is_result = MATCH_NO;
4405 /* Get the next char to narrow between result and bind(c). */
4406 gfc_gobble_whitespace ();
4407 peek_char = gfc_peek_ascii_char ();
4409 /* C binding names are not allowed for internal procedures. */
4410 if (gfc_current_state () == COMP_CONTAINS
4411 && sym->ns->proc_name->attr.flavor != FL_MODULE)
4412 allow_binding_name = false;
4413 else
4414 allow_binding_name = true;
4416 switch (peek_char)
4418 case 'r':
4419 /* Look for result clause. */
4420 is_result = match_result (sym, result);
4421 if (is_result == MATCH_YES)
4423 /* Now see if there is a bind(c) after it. */
4424 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4425 /* We've found the result clause and possibly bind(c). */
4426 found_match = MATCH_YES;
4428 else
4429 /* This should only be MATCH_ERROR. */
4430 found_match = is_result;
4431 break;
4432 case 'b':
4433 /* Look for bind(c) first. */
4434 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
4435 if (is_bind_c == MATCH_YES)
4437 /* Now see if a result clause followed it. */
4438 is_result = match_result (sym, result);
4439 found_match = MATCH_YES;
4441 else
4443 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4444 found_match = MATCH_ERROR;
4446 break;
4447 default:
4448 gfc_error ("Unexpected junk after function declaration at %C");
4449 found_match = MATCH_ERROR;
4450 break;
4453 if (is_bind_c == MATCH_YES)
4455 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4456 if (gfc_current_state () == COMP_CONTAINS
4457 && sym->ns->proc_name->attr.flavor != FL_MODULE
4458 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
4459 "at %L may not be specified for an internal "
4460 "procedure", &gfc_current_locus)
4461 == FAILURE)
4462 return MATCH_ERROR;
4464 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &gfc_current_locus, 1)
4465 == FAILURE)
4466 return MATCH_ERROR;
4469 return found_match;
4473 /* Procedure pointer return value without RESULT statement:
4474 Add "hidden" result variable named "ppr@". */
4476 static gfc_try
4477 add_hidden_procptr_result (gfc_symbol *sym)
4479 bool case1,case2;
4481 if (gfc_notification_std (GFC_STD_F2003) == ERROR)
4482 return FAILURE;
4484 /* First usage case: PROCEDURE and EXTERNAL statements. */
4485 case1 = gfc_current_state () == COMP_FUNCTION && gfc_current_block ()
4486 && strcmp (gfc_current_block ()->name, sym->name) == 0
4487 && sym->attr.external;
4488 /* Second usage case: INTERFACE statements. */
4489 case2 = gfc_current_state () == COMP_INTERFACE && gfc_state_stack->previous
4490 && gfc_state_stack->previous->state == COMP_FUNCTION
4491 && strcmp (gfc_state_stack->previous->sym->name, sym->name) == 0;
4493 if (case1 || case2)
4495 gfc_symtree *stree;
4496 if (case1)
4497 gfc_get_sym_tree ("ppr@", gfc_current_ns, &stree, false);
4498 else if (case2)
4500 gfc_symtree *st2;
4501 gfc_get_sym_tree ("ppr@", gfc_current_ns->parent, &stree, false);
4502 st2 = gfc_new_symtree (&gfc_current_ns->sym_root, "ppr@");
4503 st2->n.sym = stree->n.sym;
4505 sym->result = stree->n.sym;
4507 sym->result->attr.proc_pointer = sym->attr.proc_pointer;
4508 sym->result->attr.pointer = sym->attr.pointer;
4509 sym->result->attr.external = sym->attr.external;
4510 sym->result->attr.referenced = sym->attr.referenced;
4511 sym->result->ts = sym->ts;
4512 sym->attr.proc_pointer = 0;
4513 sym->attr.pointer = 0;
4514 sym->attr.external = 0;
4515 if (sym->result->attr.external && sym->result->attr.pointer)
4517 sym->result->attr.pointer = 0;
4518 sym->result->attr.proc_pointer = 1;
4521 return gfc_add_result (&sym->result->attr, sym->result->name, NULL);
4523 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4524 else if (sym->attr.function && !sym->attr.external && sym->attr.pointer
4525 && sym->result && sym->result != sym && sym->result->attr.external
4526 && sym == gfc_current_ns->proc_name
4527 && sym == sym->result->ns->proc_name
4528 && strcmp ("ppr@", sym->result->name) == 0)
4530 sym->result->attr.proc_pointer = 1;
4531 sym->attr.pointer = 0;
4532 return SUCCESS;
4534 else
4535 return FAILURE;
4539 /* Match the interface for a PROCEDURE declaration,
4540 including brackets (R1212). */
4542 static match
4543 match_procedure_interface (gfc_symbol **proc_if)
4545 match m;
4546 gfc_symtree *st;
4547 locus old_loc, entry_loc;
4548 gfc_namespace *old_ns = gfc_current_ns;
4549 char name[GFC_MAX_SYMBOL_LEN + 1];
4551 old_loc = entry_loc = gfc_current_locus;
4552 gfc_clear_ts (&current_ts);
4554 if (gfc_match (" (") != MATCH_YES)
4556 gfc_current_locus = entry_loc;
4557 return MATCH_NO;
4560 /* Get the type spec. for the procedure interface. */
4561 old_loc = gfc_current_locus;
4562 m = gfc_match_decl_type_spec (&current_ts, 0);
4563 gfc_gobble_whitespace ();
4564 if (m == MATCH_YES || (m == MATCH_NO && gfc_peek_ascii_char () == ')'))
4565 goto got_ts;
4567 if (m == MATCH_ERROR)
4568 return m;
4570 /* Procedure interface is itself a procedure. */
4571 gfc_current_locus = old_loc;
4572 m = gfc_match_name (name);
4574 /* First look to see if it is already accessible in the current
4575 namespace because it is use associated or contained. */
4576 st = NULL;
4577 if (gfc_find_sym_tree (name, NULL, 0, &st))
4578 return MATCH_ERROR;
4580 /* If it is still not found, then try the parent namespace, if it
4581 exists and create the symbol there if it is still not found. */
4582 if (gfc_current_ns->parent)
4583 gfc_current_ns = gfc_current_ns->parent;
4584 if (st == NULL && gfc_get_ha_sym_tree (name, &st))
4585 return MATCH_ERROR;
4587 gfc_current_ns = old_ns;
4588 *proc_if = st->n.sym;
4590 /* Various interface checks. */
4591 if (*proc_if)
4593 (*proc_if)->refs++;
4594 /* Resolve interface if possible. That way, attr.procedure is only set
4595 if it is declared by a later procedure-declaration-stmt, which is
4596 invalid per C1212. */
4597 while ((*proc_if)->ts.interface)
4598 *proc_if = (*proc_if)->ts.interface;
4600 if ((*proc_if)->generic)
4602 gfc_error ("Interface '%s' at %C may not be generic",
4603 (*proc_if)->name);
4604 return MATCH_ERROR;
4606 if ((*proc_if)->attr.proc == PROC_ST_FUNCTION)
4608 gfc_error ("Interface '%s' at %C may not be a statement function",
4609 (*proc_if)->name);
4610 return MATCH_ERROR;
4612 /* Handle intrinsic procedures. */
4613 if (!((*proc_if)->attr.external || (*proc_if)->attr.use_assoc
4614 || (*proc_if)->attr.if_source == IFSRC_IFBODY)
4615 && (gfc_is_intrinsic ((*proc_if), 0, gfc_current_locus)
4616 || gfc_is_intrinsic ((*proc_if), 1, gfc_current_locus)))
4617 (*proc_if)->attr.intrinsic = 1;
4618 if ((*proc_if)->attr.intrinsic
4619 && !gfc_intrinsic_actual_ok ((*proc_if)->name, 0))
4621 gfc_error ("Intrinsic procedure '%s' not allowed "
4622 "in PROCEDURE statement at %C", (*proc_if)->name);
4623 return MATCH_ERROR;
4627 got_ts:
4628 if (gfc_match (" )") != MATCH_YES)
4630 gfc_current_locus = entry_loc;
4631 return MATCH_NO;
4634 return MATCH_YES;
4638 /* Match a PROCEDURE declaration (R1211). */
4640 static match
4641 match_procedure_decl (void)
4643 match m;
4644 gfc_symbol *sym, *proc_if = NULL;
4645 int num;
4646 gfc_expr *initializer = NULL;
4648 /* Parse interface (with brackets). */
4649 m = match_procedure_interface (&proc_if);
4650 if (m != MATCH_YES)
4651 return m;
4653 /* Parse attributes (with colons). */
4654 m = match_attr_spec();
4655 if (m == MATCH_ERROR)
4656 return MATCH_ERROR;
4658 /* Get procedure symbols. */
4659 for(num=1;;num++)
4661 m = gfc_match_symbol (&sym, 0);
4662 if (m == MATCH_NO)
4663 goto syntax;
4664 else if (m == MATCH_ERROR)
4665 return m;
4667 /* Add current_attr to the symbol attributes. */
4668 if (gfc_copy_attr (&sym->attr, &current_attr, NULL) == FAILURE)
4669 return MATCH_ERROR;
4671 if (sym->attr.is_bind_c)
4673 /* Check for C1218. */
4674 if (!proc_if || !proc_if->attr.is_bind_c)
4676 gfc_error ("BIND(C) attribute at %C requires "
4677 "an interface with BIND(C)");
4678 return MATCH_ERROR;
4680 /* Check for C1217. */
4681 if (has_name_equals && sym->attr.pointer)
4683 gfc_error ("BIND(C) procedure with NAME may not have "
4684 "POINTER attribute at %C");
4685 return MATCH_ERROR;
4687 if (has_name_equals && sym->attr.dummy)
4689 gfc_error ("Dummy procedure at %C may not have "
4690 "BIND(C) attribute with NAME");
4691 return MATCH_ERROR;
4693 /* Set binding label for BIND(C). */
4694 if (set_binding_label (sym->binding_label, sym->name, num) != SUCCESS)
4695 return MATCH_ERROR;
4698 if (gfc_add_external (&sym->attr, NULL) == FAILURE)
4699 return MATCH_ERROR;
4701 if (add_hidden_procptr_result (sym) == SUCCESS)
4702 sym = sym->result;
4704 if (gfc_add_proc (&sym->attr, sym->name, NULL) == FAILURE)
4705 return MATCH_ERROR;
4707 /* Set interface. */
4708 if (proc_if != NULL)
4710 if (sym->ts.type != BT_UNKNOWN)
4712 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4713 sym->name, &gfc_current_locus,
4714 gfc_basic_typename (sym->ts.type));
4715 return MATCH_ERROR;
4717 sym->ts.interface = proc_if;
4718 sym->attr.untyped = 1;
4719 sym->attr.if_source = IFSRC_IFBODY;
4721 else if (current_ts.type != BT_UNKNOWN)
4723 if (gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
4724 return MATCH_ERROR;
4725 sym->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4726 sym->ts.interface->ts = current_ts;
4727 sym->ts.interface->attr.function = 1;
4728 sym->attr.function = sym->ts.interface->attr.function;
4729 sym->attr.if_source = IFSRC_UNKNOWN;
4732 if (gfc_match (" =>") == MATCH_YES)
4734 if (!current_attr.pointer)
4736 gfc_error ("Initialization at %C isn't for a pointer variable");
4737 m = MATCH_ERROR;
4738 goto cleanup;
4741 m = match_pointer_init (&initializer, 1);
4742 if (m != MATCH_YES)
4743 goto cleanup;
4745 if (add_init_expr_to_sym (sym->name, &initializer, &gfc_current_locus)
4746 != SUCCESS)
4747 goto cleanup;
4751 gfc_set_sym_referenced (sym);
4753 if (gfc_match_eos () == MATCH_YES)
4754 return MATCH_YES;
4755 if (gfc_match_char (',') != MATCH_YES)
4756 goto syntax;
4759 syntax:
4760 gfc_error ("Syntax error in PROCEDURE statement at %C");
4761 return MATCH_ERROR;
4763 cleanup:
4764 /* Free stuff up and return. */
4765 gfc_free_expr (initializer);
4766 return m;
4770 static match
4771 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc);
4774 /* Match a procedure pointer component declaration (R445). */
4776 static match
4777 match_ppc_decl (void)
4779 match m;
4780 gfc_symbol *proc_if = NULL;
4781 gfc_typespec ts;
4782 int num;
4783 gfc_component *c;
4784 gfc_expr *initializer = NULL;
4785 gfc_typebound_proc* tb;
4786 char name[GFC_MAX_SYMBOL_LEN + 1];
4788 /* Parse interface (with brackets). */
4789 m = match_procedure_interface (&proc_if);
4790 if (m != MATCH_YES)
4791 goto syntax;
4793 /* Parse attributes. */
4794 tb = XCNEW (gfc_typebound_proc);
4795 tb->where = gfc_current_locus;
4796 m = match_binding_attributes (tb, false, true);
4797 if (m == MATCH_ERROR)
4798 return m;
4800 gfc_clear_attr (&current_attr);
4801 current_attr.procedure = 1;
4802 current_attr.proc_pointer = 1;
4803 current_attr.access = tb->access;
4804 current_attr.flavor = FL_PROCEDURE;
4806 /* Match the colons (required). */
4807 if (gfc_match (" ::") != MATCH_YES)
4809 gfc_error ("Expected '::' after binding-attributes at %C");
4810 return MATCH_ERROR;
4813 /* Check for C450. */
4814 if (!tb->nopass && proc_if == NULL)
4816 gfc_error("NOPASS or explicit interface required at %C");
4817 return MATCH_ERROR;
4820 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: Procedure pointer "
4821 "component at %C") == FAILURE)
4822 return MATCH_ERROR;
4824 /* Match PPC names. */
4825 ts = current_ts;
4826 for(num=1;;num++)
4828 m = gfc_match_name (name);
4829 if (m == MATCH_NO)
4830 goto syntax;
4831 else if (m == MATCH_ERROR)
4832 return m;
4834 if (gfc_add_component (gfc_current_block (), name, &c) == FAILURE)
4835 return MATCH_ERROR;
4837 /* Add current_attr to the symbol attributes. */
4838 if (gfc_copy_attr (&c->attr, &current_attr, NULL) == FAILURE)
4839 return MATCH_ERROR;
4841 if (gfc_add_external (&c->attr, NULL) == FAILURE)
4842 return MATCH_ERROR;
4844 if (gfc_add_proc (&c->attr, name, NULL) == FAILURE)
4845 return MATCH_ERROR;
4847 c->tb = tb;
4849 /* Set interface. */
4850 if (proc_if != NULL)
4852 c->ts.interface = proc_if;
4853 c->attr.untyped = 1;
4854 c->attr.if_source = IFSRC_IFBODY;
4856 else if (ts.type != BT_UNKNOWN)
4858 c->ts = ts;
4859 c->ts.interface = gfc_new_symbol ("", gfc_current_ns);
4860 c->ts.interface->ts = ts;
4861 c->ts.interface->attr.function = 1;
4862 c->attr.function = c->ts.interface->attr.function;
4863 c->attr.if_source = IFSRC_UNKNOWN;
4866 if (gfc_match (" =>") == MATCH_YES)
4868 m = match_pointer_init (&initializer, 1);
4869 if (m != MATCH_YES)
4871 gfc_free_expr (initializer);
4872 return m;
4874 c->initializer = initializer;
4877 if (gfc_match_eos () == MATCH_YES)
4878 return MATCH_YES;
4879 if (gfc_match_char (',') != MATCH_YES)
4880 goto syntax;
4883 syntax:
4884 gfc_error ("Syntax error in procedure pointer component at %C");
4885 return MATCH_ERROR;
4889 /* Match a PROCEDURE declaration inside an interface (R1206). */
4891 static match
4892 match_procedure_in_interface (void)
4894 match m;
4895 gfc_symbol *sym;
4896 char name[GFC_MAX_SYMBOL_LEN + 1];
4898 if (current_interface.type == INTERFACE_NAMELESS
4899 || current_interface.type == INTERFACE_ABSTRACT)
4901 gfc_error ("PROCEDURE at %C must be in a generic interface");
4902 return MATCH_ERROR;
4905 for(;;)
4907 m = gfc_match_name (name);
4908 if (m == MATCH_NO)
4909 goto syntax;
4910 else if (m == MATCH_ERROR)
4911 return m;
4912 if (gfc_get_symbol (name, gfc_current_ns->parent, &sym))
4913 return MATCH_ERROR;
4915 if (gfc_add_interface (sym) == FAILURE)
4916 return MATCH_ERROR;
4918 if (gfc_match_eos () == MATCH_YES)
4919 break;
4920 if (gfc_match_char (',') != MATCH_YES)
4921 goto syntax;
4924 return MATCH_YES;
4926 syntax:
4927 gfc_error ("Syntax error in PROCEDURE statement at %C");
4928 return MATCH_ERROR;
4932 /* General matcher for PROCEDURE declarations. */
4934 static match match_procedure_in_type (void);
4936 match
4937 gfc_match_procedure (void)
4939 match m;
4941 switch (gfc_current_state ())
4943 case COMP_NONE:
4944 case COMP_PROGRAM:
4945 case COMP_MODULE:
4946 case COMP_SUBROUTINE:
4947 case COMP_FUNCTION:
4948 m = match_procedure_decl ();
4949 break;
4950 case COMP_INTERFACE:
4951 m = match_procedure_in_interface ();
4952 break;
4953 case COMP_DERIVED:
4954 m = match_ppc_decl ();
4955 break;
4956 case COMP_DERIVED_CONTAINS:
4957 m = match_procedure_in_type ();
4958 break;
4959 default:
4960 return MATCH_NO;
4963 if (m != MATCH_YES)
4964 return m;
4966 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROCEDURE statement at %C")
4967 == FAILURE)
4968 return MATCH_ERROR;
4970 return m;
4974 /* Warn if a matched procedure has the same name as an intrinsic; this is
4975 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4976 parser-state-stack to find out whether we're in a module. */
4978 static void
4979 warn_intrinsic_shadow (const gfc_symbol* sym, bool func)
4981 bool in_module;
4983 in_module = (gfc_state_stack->previous
4984 && gfc_state_stack->previous->state == COMP_MODULE);
4986 gfc_warn_intrinsic_shadow (sym, in_module, func);
4990 /* Match a function declaration. */
4992 match
4993 gfc_match_function_decl (void)
4995 char name[GFC_MAX_SYMBOL_LEN + 1];
4996 gfc_symbol *sym, *result;
4997 locus old_loc;
4998 match m;
4999 match suffix_match;
5000 match found_match; /* Status returned by match func. */
5002 if (gfc_current_state () != COMP_NONE
5003 && gfc_current_state () != COMP_INTERFACE
5004 && gfc_current_state () != COMP_CONTAINS)
5005 return MATCH_NO;
5007 gfc_clear_ts (&current_ts);
5009 old_loc = gfc_current_locus;
5011 m = gfc_match_prefix (&current_ts);
5012 if (m != MATCH_YES)
5014 gfc_current_locus = old_loc;
5015 return m;
5018 if (gfc_match ("function% %n", name) != MATCH_YES)
5020 gfc_current_locus = old_loc;
5021 return MATCH_NO;
5023 if (get_proc_name (name, &sym, false))
5024 return MATCH_ERROR;
5026 if (add_hidden_procptr_result (sym) == SUCCESS)
5027 sym = sym->result;
5029 gfc_new_block = sym;
5031 m = gfc_match_formal_arglist (sym, 0, 0);
5032 if (m == MATCH_NO)
5034 gfc_error ("Expected formal argument list in function "
5035 "definition at %C");
5036 m = MATCH_ERROR;
5037 goto cleanup;
5039 else if (m == MATCH_ERROR)
5040 goto cleanup;
5042 result = NULL;
5044 /* According to the draft, the bind(c) and result clause can
5045 come in either order after the formal_arg_list (i.e., either
5046 can be first, both can exist together or by themselves or neither
5047 one). Therefore, the match_result can't match the end of the
5048 string, and check for the bind(c) or result clause in either order. */
5049 found_match = gfc_match_eos ();
5051 /* Make sure that it isn't already declared as BIND(C). If it is, it
5052 must have been marked BIND(C) with a BIND(C) attribute and that is
5053 not allowed for procedures. */
5054 if (sym->attr.is_bind_c == 1)
5056 sym->attr.is_bind_c = 0;
5057 if (sym->old_symbol != NULL)
5058 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5059 "variables or common blocks",
5060 &(sym->old_symbol->declared_at));
5061 else
5062 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5063 "variables or common blocks", &gfc_current_locus);
5066 if (found_match != MATCH_YES)
5068 /* If we haven't found the end-of-statement, look for a suffix. */
5069 suffix_match = gfc_match_suffix (sym, &result);
5070 if (suffix_match == MATCH_YES)
5071 /* Need to get the eos now. */
5072 found_match = gfc_match_eos ();
5073 else
5074 found_match = suffix_match;
5077 if(found_match != MATCH_YES)
5078 m = MATCH_ERROR;
5079 else
5081 /* Make changes to the symbol. */
5082 m = MATCH_ERROR;
5084 if (gfc_add_function (&sym->attr, sym->name, NULL) == FAILURE)
5085 goto cleanup;
5087 if (gfc_missing_attr (&sym->attr, NULL) == FAILURE
5088 || copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5089 goto cleanup;
5091 /* Delay matching the function characteristics until after the
5092 specification block by signalling kind=-1. */
5093 sym->declared_at = old_loc;
5094 if (current_ts.type != BT_UNKNOWN)
5095 current_ts.kind = -1;
5096 else
5097 current_ts.kind = 0;
5099 if (result == NULL)
5101 if (current_ts.type != BT_UNKNOWN
5102 && gfc_add_type (sym, &current_ts, &gfc_current_locus) == FAILURE)
5103 goto cleanup;
5104 sym->result = sym;
5106 else
5108 if (current_ts.type != BT_UNKNOWN
5109 && gfc_add_type (result, &current_ts, &gfc_current_locus)
5110 == FAILURE)
5111 goto cleanup;
5112 sym->result = result;
5115 /* Warn if this procedure has the same name as an intrinsic. */
5116 warn_intrinsic_shadow (sym, true);
5118 return MATCH_YES;
5121 cleanup:
5122 gfc_current_locus = old_loc;
5123 return m;
5127 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5128 pass the name of the entry, rather than the gfc_current_block name, and
5129 to return false upon finding an existing global entry. */
5131 static bool
5132 add_global_entry (const char *name, int sub)
5134 gfc_gsymbol *s;
5135 enum gfc_symbol_type type;
5137 s = gfc_get_gsymbol(name);
5138 type = sub ? GSYM_SUBROUTINE : GSYM_FUNCTION;
5140 if (s->defined
5141 || (s->type != GSYM_UNKNOWN
5142 && s->type != type))
5143 gfc_global_used(s, NULL);
5144 else
5146 s->type = type;
5147 s->where = gfc_current_locus;
5148 s->defined = 1;
5149 s->ns = gfc_current_ns;
5150 return true;
5152 return false;
5156 /* Match an ENTRY statement. */
5158 match
5159 gfc_match_entry (void)
5161 gfc_symbol *proc;
5162 gfc_symbol *result;
5163 gfc_symbol *entry;
5164 char name[GFC_MAX_SYMBOL_LEN + 1];
5165 gfc_compile_state state;
5166 match m;
5167 gfc_entry_list *el;
5168 locus old_loc;
5169 bool module_procedure;
5170 char peek_char;
5171 match is_bind_c;
5173 m = gfc_match_name (name);
5174 if (m != MATCH_YES)
5175 return m;
5177 if (gfc_notify_std (GFC_STD_F2008_OBS, "Fortran 2008 obsolescent feature: "
5178 "ENTRY statement at %C") == FAILURE)
5179 return MATCH_ERROR;
5181 state = gfc_current_state ();
5182 if (state != COMP_SUBROUTINE && state != COMP_FUNCTION)
5184 switch (state)
5186 case COMP_PROGRAM:
5187 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5188 break;
5189 case COMP_MODULE:
5190 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5191 break;
5192 case COMP_BLOCK_DATA:
5193 gfc_error ("ENTRY statement at %C cannot appear within "
5194 "a BLOCK DATA");
5195 break;
5196 case COMP_INTERFACE:
5197 gfc_error ("ENTRY statement at %C cannot appear within "
5198 "an INTERFACE");
5199 break;
5200 case COMP_DERIVED:
5201 gfc_error ("ENTRY statement at %C cannot appear within "
5202 "a DERIVED TYPE block");
5203 break;
5204 case COMP_IF:
5205 gfc_error ("ENTRY statement at %C cannot appear within "
5206 "an IF-THEN block");
5207 break;
5208 case COMP_DO:
5209 gfc_error ("ENTRY statement at %C cannot appear within "
5210 "a DO block");
5211 break;
5212 case COMP_SELECT:
5213 gfc_error ("ENTRY statement at %C cannot appear within "
5214 "a SELECT block");
5215 break;
5216 case COMP_FORALL:
5217 gfc_error ("ENTRY statement at %C cannot appear within "
5218 "a FORALL block");
5219 break;
5220 case COMP_WHERE:
5221 gfc_error ("ENTRY statement at %C cannot appear within "
5222 "a WHERE block");
5223 break;
5224 case COMP_CONTAINS:
5225 gfc_error ("ENTRY statement at %C cannot appear within "
5226 "a contained subprogram");
5227 break;
5228 default:
5229 gfc_internal_error ("gfc_match_entry(): Bad state");
5231 return MATCH_ERROR;
5234 module_procedure = gfc_current_ns->parent != NULL
5235 && gfc_current_ns->parent->proc_name
5236 && gfc_current_ns->parent->proc_name->attr.flavor
5237 == FL_MODULE;
5239 if (gfc_current_ns->parent != NULL
5240 && gfc_current_ns->parent->proc_name
5241 && !module_procedure)
5243 gfc_error("ENTRY statement at %C cannot appear in a "
5244 "contained procedure");
5245 return MATCH_ERROR;
5248 /* Module function entries need special care in get_proc_name
5249 because previous references within the function will have
5250 created symbols attached to the current namespace. */
5251 if (get_proc_name (name, &entry,
5252 gfc_current_ns->parent != NULL
5253 && module_procedure))
5254 return MATCH_ERROR;
5256 proc = gfc_current_block ();
5258 /* Make sure that it isn't already declared as BIND(C). If it is, it
5259 must have been marked BIND(C) with a BIND(C) attribute and that is
5260 not allowed for procedures. */
5261 if (entry->attr.is_bind_c == 1)
5263 entry->attr.is_bind_c = 0;
5264 if (entry->old_symbol != NULL)
5265 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5266 "variables or common blocks",
5267 &(entry->old_symbol->declared_at));
5268 else
5269 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5270 "variables or common blocks", &gfc_current_locus);
5273 /* Check what next non-whitespace character is so we can tell if there
5274 is the required parens if we have a BIND(C). */
5275 gfc_gobble_whitespace ();
5276 peek_char = gfc_peek_ascii_char ();
5278 if (state == COMP_SUBROUTINE)
5280 /* An entry in a subroutine. */
5281 if (!gfc_current_ns->parent && !add_global_entry (name, 1))
5282 return MATCH_ERROR;
5284 m = gfc_match_formal_arglist (entry, 0, 1);
5285 if (m != MATCH_YES)
5286 return MATCH_ERROR;
5288 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5289 never be an internal procedure. */
5290 is_bind_c = gfc_match_bind_c (entry, true);
5291 if (is_bind_c == MATCH_ERROR)
5292 return MATCH_ERROR;
5293 if (is_bind_c == MATCH_YES)
5295 if (peek_char != '(')
5297 gfc_error ("Missing required parentheses before BIND(C) at %C");
5298 return MATCH_ERROR;
5300 if (gfc_add_is_bind_c (&(entry->attr), entry->name, &(entry->declared_at), 1)
5301 == FAILURE)
5302 return MATCH_ERROR;
5305 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5306 || gfc_add_subroutine (&entry->attr, entry->name, NULL) == FAILURE)
5307 return MATCH_ERROR;
5309 else
5311 /* An entry in a function.
5312 We need to take special care because writing
5313 ENTRY f()
5315 ENTRY f
5316 is allowed, whereas
5317 ENTRY f() RESULT (r)
5318 can't be written as
5319 ENTRY f RESULT (r). */
5320 if (!gfc_current_ns->parent && !add_global_entry (name, 0))
5321 return MATCH_ERROR;
5323 old_loc = gfc_current_locus;
5324 if (gfc_match_eos () == MATCH_YES)
5326 gfc_current_locus = old_loc;
5327 /* Match the empty argument list, and add the interface to
5328 the symbol. */
5329 m = gfc_match_formal_arglist (entry, 0, 1);
5331 else
5332 m = gfc_match_formal_arglist (entry, 0, 0);
5334 if (m != MATCH_YES)
5335 return MATCH_ERROR;
5337 result = NULL;
5339 if (gfc_match_eos () == MATCH_YES)
5341 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5342 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5343 return MATCH_ERROR;
5345 entry->result = entry;
5347 else
5349 m = gfc_match_suffix (entry, &result);
5350 if (m == MATCH_NO)
5351 gfc_syntax_error (ST_ENTRY);
5352 if (m != MATCH_YES)
5353 return MATCH_ERROR;
5355 if (result)
5357 if (gfc_add_result (&result->attr, result->name, NULL) == FAILURE
5358 || gfc_add_entry (&entry->attr, result->name, NULL) == FAILURE
5359 || gfc_add_function (&entry->attr, result->name, NULL)
5360 == FAILURE)
5361 return MATCH_ERROR;
5362 entry->result = result;
5364 else
5366 if (gfc_add_entry (&entry->attr, entry->name, NULL) == FAILURE
5367 || gfc_add_function (&entry->attr, entry->name, NULL) == FAILURE)
5368 return MATCH_ERROR;
5369 entry->result = entry;
5374 if (gfc_match_eos () != MATCH_YES)
5376 gfc_syntax_error (ST_ENTRY);
5377 return MATCH_ERROR;
5380 entry->attr.recursive = proc->attr.recursive;
5381 entry->attr.elemental = proc->attr.elemental;
5382 entry->attr.pure = proc->attr.pure;
5384 el = gfc_get_entry_list ();
5385 el->sym = entry;
5386 el->next = gfc_current_ns->entries;
5387 gfc_current_ns->entries = el;
5388 if (el->next)
5389 el->id = el->next->id + 1;
5390 else
5391 el->id = 1;
5393 new_st.op = EXEC_ENTRY;
5394 new_st.ext.entry = el;
5396 return MATCH_YES;
5400 /* Match a subroutine statement, including optional prefixes. */
5402 match
5403 gfc_match_subroutine (void)
5405 char name[GFC_MAX_SYMBOL_LEN + 1];
5406 gfc_symbol *sym;
5407 match m;
5408 match is_bind_c;
5409 char peek_char;
5410 bool allow_binding_name;
5412 if (gfc_current_state () != COMP_NONE
5413 && gfc_current_state () != COMP_INTERFACE
5414 && gfc_current_state () != COMP_CONTAINS)
5415 return MATCH_NO;
5417 m = gfc_match_prefix (NULL);
5418 if (m != MATCH_YES)
5419 return m;
5421 m = gfc_match ("subroutine% %n", name);
5422 if (m != MATCH_YES)
5423 return m;
5425 if (get_proc_name (name, &sym, false))
5426 return MATCH_ERROR;
5428 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
5429 the symbol existed before. */
5430 sym->declared_at = gfc_current_locus;
5432 if (add_hidden_procptr_result (sym) == SUCCESS)
5433 sym = sym->result;
5435 gfc_new_block = sym;
5437 /* Check what next non-whitespace character is so we can tell if there
5438 is the required parens if we have a BIND(C). */
5439 gfc_gobble_whitespace ();
5440 peek_char = gfc_peek_ascii_char ();
5442 if (gfc_add_subroutine (&sym->attr, sym->name, NULL) == FAILURE)
5443 return MATCH_ERROR;
5445 if (gfc_match_formal_arglist (sym, 0, 1) != MATCH_YES)
5446 return MATCH_ERROR;
5448 /* Make sure that it isn't already declared as BIND(C). If it is, it
5449 must have been marked BIND(C) with a BIND(C) attribute and that is
5450 not allowed for procedures. */
5451 if (sym->attr.is_bind_c == 1)
5453 sym->attr.is_bind_c = 0;
5454 if (sym->old_symbol != NULL)
5455 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5456 "variables or common blocks",
5457 &(sym->old_symbol->declared_at));
5458 else
5459 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5460 "variables or common blocks", &gfc_current_locus);
5463 /* C binding names are not allowed for internal procedures. */
5464 if (gfc_current_state () == COMP_CONTAINS
5465 && sym->ns->proc_name->attr.flavor != FL_MODULE)
5466 allow_binding_name = false;
5467 else
5468 allow_binding_name = true;
5470 /* Here, we are just checking if it has the bind(c) attribute, and if
5471 so, then we need to make sure it's all correct. If it doesn't,
5472 we still need to continue matching the rest of the subroutine line. */
5473 is_bind_c = gfc_match_bind_c (sym, allow_binding_name);
5474 if (is_bind_c == MATCH_ERROR)
5476 /* There was an attempt at the bind(c), but it was wrong. An
5477 error message should have been printed w/in the gfc_match_bind_c
5478 so here we'll just return the MATCH_ERROR. */
5479 return MATCH_ERROR;
5482 if (is_bind_c == MATCH_YES)
5484 /* The following is allowed in the Fortran 2008 draft. */
5485 if (gfc_current_state () == COMP_CONTAINS
5486 && sym->ns->proc_name->attr.flavor != FL_MODULE
5487 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: BIND(C) attribute "
5488 "at %L may not be specified for an internal "
5489 "procedure", &gfc_current_locus)
5490 == FAILURE)
5491 return MATCH_ERROR;
5493 if (peek_char != '(')
5495 gfc_error ("Missing required parentheses before BIND(C) at %C");
5496 return MATCH_ERROR;
5498 if (gfc_add_is_bind_c (&(sym->attr), sym->name, &(sym->declared_at), 1)
5499 == FAILURE)
5500 return MATCH_ERROR;
5503 if (gfc_match_eos () != MATCH_YES)
5505 gfc_syntax_error (ST_SUBROUTINE);
5506 return MATCH_ERROR;
5509 if (copy_prefix (&sym->attr, &sym->declared_at) == FAILURE)
5510 return MATCH_ERROR;
5512 /* Warn if it has the same name as an intrinsic. */
5513 warn_intrinsic_shadow (sym, false);
5515 return MATCH_YES;
5519 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
5520 given, and set the binding label in either the given symbol (if not
5521 NULL), or in the current_ts. The symbol may be NULL because we may
5522 encounter the BIND(C) before the declaration itself. Return
5523 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
5524 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
5525 or MATCH_YES if the specifier was correct and the binding label and
5526 bind(c) fields were set correctly for the given symbol or the
5527 current_ts. If allow_binding_name is false, no binding name may be
5528 given. */
5530 match
5531 gfc_match_bind_c (gfc_symbol *sym, bool allow_binding_name)
5533 /* binding label, if exists */
5534 char binding_label[GFC_MAX_SYMBOL_LEN + 1];
5535 match double_quote;
5536 match single_quote;
5538 /* Initialize the flag that specifies whether we encountered a NAME=
5539 specifier or not. */
5540 has_name_equals = 0;
5542 /* Init the first char to nil so we can catch if we don't have
5543 the label (name attr) or the symbol name yet. */
5544 binding_label[0] = '\0';
5546 /* This much we have to be able to match, in this order, if
5547 there is a bind(c) label. */
5548 if (gfc_match (" bind ( c ") != MATCH_YES)
5549 return MATCH_NO;
5551 /* Now see if there is a binding label, or if we've reached the
5552 end of the bind(c) attribute without one. */
5553 if (gfc_match_char (',') == MATCH_YES)
5555 if (gfc_match (" name = ") != MATCH_YES)
5557 gfc_error ("Syntax error in NAME= specifier for binding label "
5558 "at %C");
5559 /* should give an error message here */
5560 return MATCH_ERROR;
5563 has_name_equals = 1;
5565 /* Get the opening quote. */
5566 double_quote = MATCH_YES;
5567 single_quote = MATCH_YES;
5568 double_quote = gfc_match_char ('"');
5569 if (double_quote != MATCH_YES)
5570 single_quote = gfc_match_char ('\'');
5571 if (double_quote != MATCH_YES && single_quote != MATCH_YES)
5573 gfc_error ("Syntax error in NAME= specifier for binding label "
5574 "at %C");
5575 return MATCH_ERROR;
5578 /* Grab the binding label, using functions that will not lower
5579 case the names automatically. */
5580 if (gfc_match_name_C (binding_label) != MATCH_YES)
5581 return MATCH_ERROR;
5583 /* Get the closing quotation. */
5584 if (double_quote == MATCH_YES)
5586 if (gfc_match_char ('"') != MATCH_YES)
5588 gfc_error ("Missing closing quote '\"' for binding label at %C");
5589 /* User started string with '"' so looked to match it. */
5590 return MATCH_ERROR;
5593 else
5595 if (gfc_match_char ('\'') != MATCH_YES)
5597 gfc_error ("Missing closing quote '\'' for binding label at %C");
5598 /* User started string with "'" char. */
5599 return MATCH_ERROR;
5604 /* Get the required right paren. */
5605 if (gfc_match_char (')') != MATCH_YES)
5607 gfc_error ("Missing closing paren for binding label at %C");
5608 return MATCH_ERROR;
5611 if (has_name_equals && !allow_binding_name)
5613 gfc_error ("No binding name is allowed in BIND(C) at %C");
5614 return MATCH_ERROR;
5617 if (has_name_equals && sym != NULL && sym->attr.dummy)
5619 gfc_error ("For dummy procedure %s, no binding name is "
5620 "allowed in BIND(C) at %C", sym->name);
5621 return MATCH_ERROR;
5625 /* Save the binding label to the symbol. If sym is null, we're
5626 probably matching the typespec attributes of a declaration and
5627 haven't gotten the name yet, and therefore, no symbol yet. */
5628 if (binding_label[0] != '\0')
5630 if (sym != NULL)
5632 strcpy (sym->binding_label, binding_label);
5634 else
5635 strcpy (curr_binding_label, binding_label);
5637 else if (allow_binding_name)
5639 /* No binding label, but if symbol isn't null, we
5640 can set the label for it here.
5641 If name="" or allow_binding_name is false, no C binding name is
5642 created. */
5643 if (sym != NULL && sym->name != NULL && has_name_equals == 0)
5644 strncpy (sym->binding_label, sym->name, strlen (sym->name) + 1);
5647 if (has_name_equals && gfc_current_state () == COMP_INTERFACE
5648 && current_interface.type == INTERFACE_ABSTRACT)
5650 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5651 return MATCH_ERROR;
5654 return MATCH_YES;
5658 /* Return nonzero if we're currently compiling a contained procedure. */
5660 static int
5661 contained_procedure (void)
5663 gfc_state_data *s = gfc_state_stack;
5665 if ((s->state == COMP_SUBROUTINE || s->state == COMP_FUNCTION)
5666 && s->previous != NULL && s->previous->state == COMP_CONTAINS)
5667 return 1;
5669 return 0;
5672 /* Set the kind of each enumerator. The kind is selected such that it is
5673 interoperable with the corresponding C enumeration type, making
5674 sure that -fshort-enums is honored. */
5676 static void
5677 set_enum_kind(void)
5679 enumerator_history *current_history = NULL;
5680 int kind;
5681 int i;
5683 if (max_enum == NULL || enum_history == NULL)
5684 return;
5686 if (!flag_short_enums)
5687 return;
5689 i = 0;
5692 kind = gfc_integer_kinds[i++].kind;
5694 while (kind < gfc_c_int_kind
5695 && gfc_check_integer_range (max_enum->initializer->value.integer,
5696 kind) != ARITH_OK);
5698 current_history = enum_history;
5699 while (current_history != NULL)
5701 current_history->sym->ts.kind = kind;
5702 current_history = current_history->next;
5707 /* Match any of the various end-block statements. Returns the type of
5708 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
5709 and END BLOCK statements cannot be replaced by a single END statement. */
5711 match
5712 gfc_match_end (gfc_statement *st)
5714 char name[GFC_MAX_SYMBOL_LEN + 1];
5715 gfc_compile_state state;
5716 locus old_loc;
5717 const char *block_name;
5718 const char *target;
5719 int eos_ok;
5720 match m;
5722 old_loc = gfc_current_locus;
5723 if (gfc_match ("end") != MATCH_YES)
5724 return MATCH_NO;
5726 state = gfc_current_state ();
5727 block_name = gfc_current_block () == NULL
5728 ? NULL : gfc_current_block ()->name;
5730 switch (state)
5732 case COMP_ASSOCIATE:
5733 case COMP_BLOCK:
5734 if (!strcmp (block_name, "block@"))
5735 block_name = NULL;
5736 break;
5738 case COMP_CONTAINS:
5739 case COMP_DERIVED_CONTAINS:
5740 state = gfc_state_stack->previous->state;
5741 block_name = gfc_state_stack->previous->sym == NULL
5742 ? NULL : gfc_state_stack->previous->sym->name;
5743 break;
5745 default:
5746 break;
5749 switch (state)
5751 case COMP_NONE:
5752 case COMP_PROGRAM:
5753 *st = ST_END_PROGRAM;
5754 target = " program";
5755 eos_ok = 1;
5756 break;
5758 case COMP_SUBROUTINE:
5759 *st = ST_END_SUBROUTINE;
5760 target = " subroutine";
5761 eos_ok = !contained_procedure ();
5762 break;
5764 case COMP_FUNCTION:
5765 *st = ST_END_FUNCTION;
5766 target = " function";
5767 eos_ok = !contained_procedure ();
5768 break;
5770 case COMP_BLOCK_DATA:
5771 *st = ST_END_BLOCK_DATA;
5772 target = " block data";
5773 eos_ok = 1;
5774 break;
5776 case COMP_MODULE:
5777 *st = ST_END_MODULE;
5778 target = " module";
5779 eos_ok = 1;
5780 break;
5782 case COMP_INTERFACE:
5783 *st = ST_END_INTERFACE;
5784 target = " interface";
5785 eos_ok = 0;
5786 break;
5788 case COMP_DERIVED:
5789 case COMP_DERIVED_CONTAINS:
5790 *st = ST_END_TYPE;
5791 target = " type";
5792 eos_ok = 0;
5793 break;
5795 case COMP_ASSOCIATE:
5796 *st = ST_END_ASSOCIATE;
5797 target = " associate";
5798 eos_ok = 0;
5799 break;
5801 case COMP_BLOCK:
5802 *st = ST_END_BLOCK;
5803 target = " block";
5804 eos_ok = 0;
5805 break;
5807 case COMP_IF:
5808 *st = ST_ENDIF;
5809 target = " if";
5810 eos_ok = 0;
5811 break;
5813 case COMP_DO:
5814 *st = ST_ENDDO;
5815 target = " do";
5816 eos_ok = 0;
5817 break;
5819 case COMP_CRITICAL:
5820 *st = ST_END_CRITICAL;
5821 target = " critical";
5822 eos_ok = 0;
5823 break;
5825 case COMP_SELECT:
5826 case COMP_SELECT_TYPE:
5827 *st = ST_END_SELECT;
5828 target = " select";
5829 eos_ok = 0;
5830 break;
5832 case COMP_FORALL:
5833 *st = ST_END_FORALL;
5834 target = " forall";
5835 eos_ok = 0;
5836 break;
5838 case COMP_WHERE:
5839 *st = ST_END_WHERE;
5840 target = " where";
5841 eos_ok = 0;
5842 break;
5844 case COMP_ENUM:
5845 *st = ST_END_ENUM;
5846 target = " enum";
5847 eos_ok = 0;
5848 last_initializer = NULL;
5849 set_enum_kind ();
5850 gfc_free_enum_history ();
5851 break;
5853 default:
5854 gfc_error ("Unexpected END statement at %C");
5855 goto cleanup;
5858 if (gfc_match_eos () == MATCH_YES)
5860 if (!eos_ok && (*st == ST_END_SUBROUTINE || *st == ST_END_FUNCTION))
5862 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: END statement "
5863 "instead of %s statement at %L",
5864 gfc_ascii_statement (*st), &old_loc) == FAILURE)
5865 goto cleanup;
5867 else if (!eos_ok)
5869 /* We would have required END [something]. */
5870 gfc_error ("%s statement expected at %L",
5871 gfc_ascii_statement (*st), &old_loc);
5872 goto cleanup;
5875 return MATCH_YES;
5878 /* Verify that we've got the sort of end-block that we're expecting. */
5879 if (gfc_match (target) != MATCH_YES)
5881 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st));
5882 goto cleanup;
5885 /* If we're at the end, make sure a block name wasn't required. */
5886 if (gfc_match_eos () == MATCH_YES)
5889 if (*st != ST_ENDDO && *st != ST_ENDIF && *st != ST_END_SELECT
5890 && *st != ST_END_FORALL && *st != ST_END_WHERE && *st != ST_END_BLOCK
5891 && *st != ST_END_ASSOCIATE && *st != ST_END_CRITICAL)
5892 return MATCH_YES;
5894 if (!block_name)
5895 return MATCH_YES;
5897 gfc_error ("Expected block name of '%s' in %s statement at %C",
5898 block_name, gfc_ascii_statement (*st));
5900 return MATCH_ERROR;
5903 /* END INTERFACE has a special handler for its several possible endings. */
5904 if (*st == ST_END_INTERFACE)
5905 return gfc_match_end_interface ();
5907 /* We haven't hit the end of statement, so what is left must be an
5908 end-name. */
5909 m = gfc_match_space ();
5910 if (m == MATCH_YES)
5911 m = gfc_match_name (name);
5913 if (m == MATCH_NO)
5914 gfc_error ("Expected terminating name at %C");
5915 if (m != MATCH_YES)
5916 goto cleanup;
5918 if (block_name == NULL)
5919 goto syntax;
5921 if (strcmp (name, block_name) != 0 && strcmp (block_name, "ppr@") != 0)
5923 gfc_error ("Expected label '%s' for %s statement at %C", block_name,
5924 gfc_ascii_statement (*st));
5925 goto cleanup;
5927 /* Procedure pointer as function result. */
5928 else if (strcmp (block_name, "ppr@") == 0
5929 && strcmp (name, gfc_current_block ()->ns->proc_name->name) != 0)
5931 gfc_error ("Expected label '%s' for %s statement at %C",
5932 gfc_current_block ()->ns->proc_name->name,
5933 gfc_ascii_statement (*st));
5934 goto cleanup;
5937 if (gfc_match_eos () == MATCH_YES)
5938 return MATCH_YES;
5940 syntax:
5941 gfc_syntax_error (*st);
5943 cleanup:
5944 gfc_current_locus = old_loc;
5945 return MATCH_ERROR;
5950 /***************** Attribute declaration statements ****************/
5952 /* Set the attribute of a single variable. */
5954 static match
5955 attr_decl1 (void)
5957 char name[GFC_MAX_SYMBOL_LEN + 1];
5958 gfc_array_spec *as;
5959 gfc_symbol *sym;
5960 locus var_locus;
5961 match m;
5963 as = NULL;
5965 m = gfc_match_name (name);
5966 if (m != MATCH_YES)
5967 goto cleanup;
5969 if (find_special (name, &sym, false))
5970 return MATCH_ERROR;
5972 var_locus = gfc_current_locus;
5974 /* Deal with possible array specification for certain attributes. */
5975 if (current_attr.dimension
5976 || current_attr.codimension
5977 || current_attr.allocatable
5978 || current_attr.pointer
5979 || current_attr.target)
5981 m = gfc_match_array_spec (&as, !current_attr.codimension,
5982 !current_attr.dimension
5983 && !current_attr.pointer
5984 && !current_attr.target);
5985 if (m == MATCH_ERROR)
5986 goto cleanup;
5988 if (current_attr.dimension && m == MATCH_NO)
5990 gfc_error ("Missing array specification at %L in DIMENSION "
5991 "statement", &var_locus);
5992 m = MATCH_ERROR;
5993 goto cleanup;
5996 if (current_attr.dimension && sym->value)
5998 gfc_error ("Dimensions specified for %s at %L after its "
5999 "initialisation", sym->name, &var_locus);
6000 m = MATCH_ERROR;
6001 goto cleanup;
6004 if (current_attr.codimension && m == MATCH_NO)
6006 gfc_error ("Missing array specification at %L in CODIMENSION "
6007 "statement", &var_locus);
6008 m = MATCH_ERROR;
6009 goto cleanup;
6012 if ((current_attr.allocatable || current_attr.pointer)
6013 && (m == MATCH_YES) && (as->type != AS_DEFERRED))
6015 gfc_error ("Array specification must be deferred at %L", &var_locus);
6016 m = MATCH_ERROR;
6017 goto cleanup;
6021 /* Update symbol table. DIMENSION attribute is set in
6022 gfc_set_array_spec(). For CLASS variables, this must be applied
6023 to the first component, or '_data' field. */
6024 if (sym->ts.type == BT_CLASS && sym->ts.u.derived->attr.is_class)
6026 if (gfc_copy_attr (&CLASS_DATA (sym)->attr, &current_attr, &var_locus)
6027 == FAILURE)
6029 m = MATCH_ERROR;
6030 goto cleanup;
6033 else
6035 if (current_attr.dimension == 0 && current_attr.codimension == 0
6036 && gfc_copy_attr (&sym->attr, &current_attr, &var_locus) == FAILURE)
6038 m = MATCH_ERROR;
6039 goto cleanup;
6043 if (sym->ts.type == BT_CLASS && !sym->attr.class_ok
6044 && (sym->attr.class_ok = sym->attr.class_ok || current_attr.allocatable
6045 || current_attr.pointer))
6046 gfc_build_class_symbol (&sym->ts, &sym->attr, &sym->as, false);
6048 if (gfc_set_array_spec (sym, as, &var_locus) == FAILURE)
6050 m = MATCH_ERROR;
6051 goto cleanup;
6054 if (sym->attr.cray_pointee && sym->as != NULL)
6056 /* Fix the array spec. */
6057 m = gfc_mod_pointee_as (sym->as);
6058 if (m == MATCH_ERROR)
6059 goto cleanup;
6062 if (gfc_add_attribute (&sym->attr, &var_locus) == FAILURE)
6064 m = MATCH_ERROR;
6065 goto cleanup;
6068 if ((current_attr.external || current_attr.intrinsic)
6069 && sym->attr.flavor != FL_PROCEDURE
6070 && gfc_add_flavor (&sym->attr, FL_PROCEDURE, sym->name, NULL) == FAILURE)
6072 m = MATCH_ERROR;
6073 goto cleanup;
6076 add_hidden_procptr_result (sym);
6078 return MATCH_YES;
6080 cleanup:
6081 gfc_free_array_spec (as);
6082 return m;
6086 /* Generic attribute declaration subroutine. Used for attributes that
6087 just have a list of names. */
6089 static match
6090 attr_decl (void)
6092 match m;
6094 /* Gobble the optional double colon, by simply ignoring the result
6095 of gfc_match(). */
6096 gfc_match (" ::");
6098 for (;;)
6100 m = attr_decl1 ();
6101 if (m != MATCH_YES)
6102 break;
6104 if (gfc_match_eos () == MATCH_YES)
6106 m = MATCH_YES;
6107 break;
6110 if (gfc_match_char (',') != MATCH_YES)
6112 gfc_error ("Unexpected character in variable list at %C");
6113 m = MATCH_ERROR;
6114 break;
6118 return m;
6122 /* This routine matches Cray Pointer declarations of the form:
6123 pointer ( <pointer>, <pointee> )
6125 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6126 The pointer, if already declared, should be an integer. Otherwise, we
6127 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6128 be either a scalar, or an array declaration. No space is allocated for
6129 the pointee. For the statement
6130 pointer (ipt, ar(10))
6131 any subsequent uses of ar will be translated (in C-notation) as
6132 ar(i) => ((<type> *) ipt)(i)
6133 After gimplification, pointee variable will disappear in the code. */
6135 static match
6136 cray_pointer_decl (void)
6138 match m;
6139 gfc_array_spec *as = NULL;
6140 gfc_symbol *cptr; /* Pointer symbol. */
6141 gfc_symbol *cpte; /* Pointee symbol. */
6142 locus var_locus;
6143 bool done = false;
6145 while (!done)
6147 if (gfc_match_char ('(') != MATCH_YES)
6149 gfc_error ("Expected '(' at %C");
6150 return MATCH_ERROR;
6153 /* Match pointer. */
6154 var_locus = gfc_current_locus;
6155 gfc_clear_attr (&current_attr);
6156 gfc_add_cray_pointer (&current_attr, &var_locus);
6157 current_ts.type = BT_INTEGER;
6158 current_ts.kind = gfc_index_integer_kind;
6160 m = gfc_match_symbol (&cptr, 0);
6161 if (m != MATCH_YES)
6163 gfc_error ("Expected variable name at %C");
6164 return m;
6167 if (gfc_add_cray_pointer (&cptr->attr, &var_locus) == FAILURE)
6168 return MATCH_ERROR;
6170 gfc_set_sym_referenced (cptr);
6172 if (cptr->ts.type == BT_UNKNOWN) /* Override the type, if necessary. */
6174 cptr->ts.type = BT_INTEGER;
6175 cptr->ts.kind = gfc_index_integer_kind;
6177 else if (cptr->ts.type != BT_INTEGER)
6179 gfc_error ("Cray pointer at %C must be an integer");
6180 return MATCH_ERROR;
6182 else if (cptr->ts.kind < gfc_index_integer_kind)
6183 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
6184 " memory addresses require %d bytes",
6185 cptr->ts.kind, gfc_index_integer_kind);
6187 if (gfc_match_char (',') != MATCH_YES)
6189 gfc_error ("Expected \",\" at %C");
6190 return MATCH_ERROR;
6193 /* Match Pointee. */
6194 var_locus = gfc_current_locus;
6195 gfc_clear_attr (&current_attr);
6196 gfc_add_cray_pointee (&current_attr, &var_locus);
6197 current_ts.type = BT_UNKNOWN;
6198 current_ts.kind = 0;
6200 m = gfc_match_symbol (&cpte, 0);
6201 if (m != MATCH_YES)
6203 gfc_error ("Expected variable name at %C");
6204 return m;
6207 /* Check for an optional array spec. */
6208 m = gfc_match_array_spec (&as, true, false);
6209 if (m == MATCH_ERROR)
6211 gfc_free_array_spec (as);
6212 return m;
6214 else if (m == MATCH_NO)
6216 gfc_free_array_spec (as);
6217 as = NULL;
6220 if (gfc_add_cray_pointee (&cpte->attr, &var_locus) == FAILURE)
6221 return MATCH_ERROR;
6223 gfc_set_sym_referenced (cpte);
6225 if (cpte->as == NULL)
6227 if (gfc_set_array_spec (cpte, as, &var_locus) == FAILURE)
6228 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6230 else if (as != NULL)
6232 gfc_error ("Duplicate array spec for Cray pointee at %C");
6233 gfc_free_array_spec (as);
6234 return MATCH_ERROR;
6237 as = NULL;
6239 if (cpte->as != NULL)
6241 /* Fix array spec. */
6242 m = gfc_mod_pointee_as (cpte->as);
6243 if (m == MATCH_ERROR)
6244 return m;
6247 /* Point the Pointee at the Pointer. */
6248 cpte->cp_pointer = cptr;
6250 if (gfc_match_char (')') != MATCH_YES)
6252 gfc_error ("Expected \")\" at %C");
6253 return MATCH_ERROR;
6255 m = gfc_match_char (',');
6256 if (m != MATCH_YES)
6257 done = true; /* Stop searching for more declarations. */
6261 if (m == MATCH_ERROR /* Failed when trying to find ',' above. */
6262 || gfc_match_eos () != MATCH_YES)
6264 gfc_error ("Expected \",\" or end of statement at %C");
6265 return MATCH_ERROR;
6267 return MATCH_YES;
6271 match
6272 gfc_match_external (void)
6275 gfc_clear_attr (&current_attr);
6276 current_attr.external = 1;
6278 return attr_decl ();
6282 match
6283 gfc_match_intent (void)
6285 sym_intent intent;
6287 /* This is not allowed within a BLOCK construct! */
6288 if (gfc_current_state () == COMP_BLOCK)
6290 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
6291 return MATCH_ERROR;
6294 intent = match_intent_spec ();
6295 if (intent == INTENT_UNKNOWN)
6296 return MATCH_ERROR;
6298 gfc_clear_attr (&current_attr);
6299 current_attr.intent = intent;
6301 return attr_decl ();
6305 match
6306 gfc_match_intrinsic (void)
6309 gfc_clear_attr (&current_attr);
6310 current_attr.intrinsic = 1;
6312 return attr_decl ();
6316 match
6317 gfc_match_optional (void)
6319 /* This is not allowed within a BLOCK construct! */
6320 if (gfc_current_state () == COMP_BLOCK)
6322 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
6323 return MATCH_ERROR;
6326 gfc_clear_attr (&current_attr);
6327 current_attr.optional = 1;
6329 return attr_decl ();
6333 match
6334 gfc_match_pointer (void)
6336 gfc_gobble_whitespace ();
6337 if (gfc_peek_ascii_char () == '(')
6339 if (!gfc_option.flag_cray_pointer)
6341 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
6342 "flag");
6343 return MATCH_ERROR;
6345 return cray_pointer_decl ();
6347 else
6349 gfc_clear_attr (&current_attr);
6350 current_attr.pointer = 1;
6352 return attr_decl ();
6357 match
6358 gfc_match_allocatable (void)
6360 gfc_clear_attr (&current_attr);
6361 current_attr.allocatable = 1;
6363 return attr_decl ();
6367 match
6368 gfc_match_codimension (void)
6370 gfc_clear_attr (&current_attr);
6371 current_attr.codimension = 1;
6373 return attr_decl ();
6377 match
6378 gfc_match_contiguous (void)
6380 if (gfc_notify_std (GFC_STD_F2008, "Fortran 2008: CONTIGUOUS statement at %C")
6381 == FAILURE)
6382 return MATCH_ERROR;
6384 gfc_clear_attr (&current_attr);
6385 current_attr.contiguous = 1;
6387 return attr_decl ();
6391 match
6392 gfc_match_dimension (void)
6394 gfc_clear_attr (&current_attr);
6395 current_attr.dimension = 1;
6397 return attr_decl ();
6401 match
6402 gfc_match_target (void)
6404 gfc_clear_attr (&current_attr);
6405 current_attr.target = 1;
6407 return attr_decl ();
6411 /* Match the list of entities being specified in a PUBLIC or PRIVATE
6412 statement. */
6414 static match
6415 access_attr_decl (gfc_statement st)
6417 char name[GFC_MAX_SYMBOL_LEN + 1];
6418 interface_type type;
6419 gfc_user_op *uop;
6420 gfc_symbol *sym;
6421 gfc_intrinsic_op op;
6422 match m;
6424 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6425 goto done;
6427 for (;;)
6429 m = gfc_match_generic_spec (&type, name, &op);
6430 if (m == MATCH_NO)
6431 goto syntax;
6432 if (m == MATCH_ERROR)
6433 return MATCH_ERROR;
6435 switch (type)
6437 case INTERFACE_NAMELESS:
6438 case INTERFACE_ABSTRACT:
6439 goto syntax;
6441 case INTERFACE_GENERIC:
6442 if (gfc_get_symbol (name, NULL, &sym))
6443 goto done;
6445 if (gfc_add_access (&sym->attr, (st == ST_PUBLIC)
6446 ? ACCESS_PUBLIC : ACCESS_PRIVATE,
6447 sym->name, NULL) == FAILURE)
6448 return MATCH_ERROR;
6450 break;
6452 case INTERFACE_INTRINSIC_OP:
6453 if (gfc_current_ns->operator_access[op] == ACCESS_UNKNOWN)
6455 gfc_current_ns->operator_access[op] =
6456 (st == ST_PUBLIC) ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6458 else
6460 gfc_error ("Access specification of the %s operator at %C has "
6461 "already been specified", gfc_op2string (op));
6462 goto done;
6465 break;
6467 case INTERFACE_USER_OP:
6468 uop = gfc_get_uop (name);
6470 if (uop->access == ACCESS_UNKNOWN)
6472 uop->access = (st == ST_PUBLIC)
6473 ? ACCESS_PUBLIC : ACCESS_PRIVATE;
6475 else
6477 gfc_error ("Access specification of the .%s. operator at %C "
6478 "has already been specified", sym->name);
6479 goto done;
6482 break;
6485 if (gfc_match_char (',') == MATCH_NO)
6486 break;
6489 if (gfc_match_eos () != MATCH_YES)
6490 goto syntax;
6491 return MATCH_YES;
6493 syntax:
6494 gfc_syntax_error (st);
6496 done:
6497 return MATCH_ERROR;
6501 match
6502 gfc_match_protected (void)
6504 gfc_symbol *sym;
6505 match m;
6507 if (gfc_current_ns->proc_name->attr.flavor != FL_MODULE)
6509 gfc_error ("PROTECTED at %C only allowed in specification "
6510 "part of a module");
6511 return MATCH_ERROR;
6515 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: PROTECTED statement at %C")
6516 == FAILURE)
6517 return MATCH_ERROR;
6519 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6521 return MATCH_ERROR;
6524 if (gfc_match_eos () == MATCH_YES)
6525 goto syntax;
6527 for(;;)
6529 m = gfc_match_symbol (&sym, 0);
6530 switch (m)
6532 case MATCH_YES:
6533 if (gfc_add_protected (&sym->attr, sym->name, &gfc_current_locus)
6534 == FAILURE)
6535 return MATCH_ERROR;
6536 goto next_item;
6538 case MATCH_NO:
6539 break;
6541 case MATCH_ERROR:
6542 return MATCH_ERROR;
6545 next_item:
6546 if (gfc_match_eos () == MATCH_YES)
6547 break;
6548 if (gfc_match_char (',') != MATCH_YES)
6549 goto syntax;
6552 return MATCH_YES;
6554 syntax:
6555 gfc_error ("Syntax error in PROTECTED statement at %C");
6556 return MATCH_ERROR;
6560 /* The PRIVATE statement is a bit weird in that it can be an attribute
6561 declaration, but also works as a standalone statement inside of a
6562 type declaration or a module. */
6564 match
6565 gfc_match_private (gfc_statement *st)
6568 if (gfc_match ("private") != MATCH_YES)
6569 return MATCH_NO;
6571 if (gfc_current_state () != COMP_MODULE
6572 && !(gfc_current_state () == COMP_DERIVED
6573 && gfc_state_stack->previous
6574 && gfc_state_stack->previous->state == COMP_MODULE)
6575 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
6576 && gfc_state_stack->previous && gfc_state_stack->previous->previous
6577 && gfc_state_stack->previous->previous->state == COMP_MODULE))
6579 gfc_error ("PRIVATE statement at %C is only allowed in the "
6580 "specification part of a module");
6581 return MATCH_ERROR;
6584 if (gfc_current_state () == COMP_DERIVED)
6586 if (gfc_match_eos () == MATCH_YES)
6588 *st = ST_PRIVATE;
6589 return MATCH_YES;
6592 gfc_syntax_error (ST_PRIVATE);
6593 return MATCH_ERROR;
6596 if (gfc_match_eos () == MATCH_YES)
6598 *st = ST_PRIVATE;
6599 return MATCH_YES;
6602 *st = ST_ATTR_DECL;
6603 return access_attr_decl (ST_PRIVATE);
6607 match
6608 gfc_match_public (gfc_statement *st)
6611 if (gfc_match ("public") != MATCH_YES)
6612 return MATCH_NO;
6614 if (gfc_current_state () != COMP_MODULE)
6616 gfc_error ("PUBLIC statement at %C is only allowed in the "
6617 "specification part of a module");
6618 return MATCH_ERROR;
6621 if (gfc_match_eos () == MATCH_YES)
6623 *st = ST_PUBLIC;
6624 return MATCH_YES;
6627 *st = ST_ATTR_DECL;
6628 return access_attr_decl (ST_PUBLIC);
6632 /* Workhorse for gfc_match_parameter. */
6634 static match
6635 do_parm (void)
6637 gfc_symbol *sym;
6638 gfc_expr *init;
6639 match m;
6640 gfc_try t;
6642 m = gfc_match_symbol (&sym, 0);
6643 if (m == MATCH_NO)
6644 gfc_error ("Expected variable name at %C in PARAMETER statement");
6646 if (m != MATCH_YES)
6647 return m;
6649 if (gfc_match_char ('=') == MATCH_NO)
6651 gfc_error ("Expected = sign in PARAMETER statement at %C");
6652 return MATCH_ERROR;
6655 m = gfc_match_init_expr (&init);
6656 if (m == MATCH_NO)
6657 gfc_error ("Expected expression at %C in PARAMETER statement");
6658 if (m != MATCH_YES)
6659 return m;
6661 if (sym->ts.type == BT_UNKNOWN
6662 && gfc_set_default_type (sym, 1, NULL) == FAILURE)
6664 m = MATCH_ERROR;
6665 goto cleanup;
6668 if (gfc_check_assign_symbol (sym, init) == FAILURE
6669 || gfc_add_flavor (&sym->attr, FL_PARAMETER, sym->name, NULL) == FAILURE)
6671 m = MATCH_ERROR;
6672 goto cleanup;
6675 if (sym->value)
6677 gfc_error ("Initializing already initialized variable at %C");
6678 m = MATCH_ERROR;
6679 goto cleanup;
6682 t = add_init_expr_to_sym (sym->name, &init, &gfc_current_locus);
6683 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
6685 cleanup:
6686 gfc_free_expr (init);
6687 return m;
6691 /* Match a parameter statement, with the weird syntax that these have. */
6693 match
6694 gfc_match_parameter (void)
6696 match m;
6698 if (gfc_match_char ('(') == MATCH_NO)
6699 return MATCH_NO;
6701 for (;;)
6703 m = do_parm ();
6704 if (m != MATCH_YES)
6705 break;
6707 if (gfc_match (" )%t") == MATCH_YES)
6708 break;
6710 if (gfc_match_char (',') != MATCH_YES)
6712 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6713 m = MATCH_ERROR;
6714 break;
6718 return m;
6722 /* Save statements have a special syntax. */
6724 match
6725 gfc_match_save (void)
6727 char n[GFC_MAX_SYMBOL_LEN+1];
6728 gfc_common_head *c;
6729 gfc_symbol *sym;
6730 match m;
6732 if (gfc_match_eos () == MATCH_YES)
6734 if (gfc_current_ns->seen_save)
6736 if (gfc_notify_std (GFC_STD_LEGACY, "Blanket SAVE statement at %C "
6737 "follows previous SAVE statement")
6738 == FAILURE)
6739 return MATCH_ERROR;
6742 gfc_current_ns->save_all = gfc_current_ns->seen_save = 1;
6743 return MATCH_YES;
6746 if (gfc_current_ns->save_all)
6748 if (gfc_notify_std (GFC_STD_LEGACY, "SAVE statement at %C follows "
6749 "blanket SAVE statement")
6750 == FAILURE)
6751 return MATCH_ERROR;
6754 gfc_match (" ::");
6756 for (;;)
6758 m = gfc_match_symbol (&sym, 0);
6759 switch (m)
6761 case MATCH_YES:
6762 if (gfc_add_save (&sym->attr, SAVE_EXPLICIT, sym->name,
6763 &gfc_current_locus) == FAILURE)
6764 return MATCH_ERROR;
6765 goto next_item;
6767 case MATCH_NO:
6768 break;
6770 case MATCH_ERROR:
6771 return MATCH_ERROR;
6774 m = gfc_match (" / %n /", &n);
6775 if (m == MATCH_ERROR)
6776 return MATCH_ERROR;
6777 if (m == MATCH_NO)
6778 goto syntax;
6780 c = gfc_get_common (n, 0);
6781 c->saved = 1;
6783 gfc_current_ns->seen_save = 1;
6785 next_item:
6786 if (gfc_match_eos () == MATCH_YES)
6787 break;
6788 if (gfc_match_char (',') != MATCH_YES)
6789 goto syntax;
6792 return MATCH_YES;
6794 syntax:
6795 gfc_error ("Syntax error in SAVE statement at %C");
6796 return MATCH_ERROR;
6800 match
6801 gfc_match_value (void)
6803 gfc_symbol *sym;
6804 match m;
6806 /* This is not allowed within a BLOCK construct! */
6807 if (gfc_current_state () == COMP_BLOCK)
6809 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
6810 return MATCH_ERROR;
6813 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VALUE statement at %C")
6814 == FAILURE)
6815 return MATCH_ERROR;
6817 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6819 return MATCH_ERROR;
6822 if (gfc_match_eos () == MATCH_YES)
6823 goto syntax;
6825 for(;;)
6827 m = gfc_match_symbol (&sym, 0);
6828 switch (m)
6830 case MATCH_YES:
6831 if (gfc_add_value (&sym->attr, sym->name, &gfc_current_locus)
6832 == FAILURE)
6833 return MATCH_ERROR;
6834 goto next_item;
6836 case MATCH_NO:
6837 break;
6839 case MATCH_ERROR:
6840 return MATCH_ERROR;
6843 next_item:
6844 if (gfc_match_eos () == MATCH_YES)
6845 break;
6846 if (gfc_match_char (',') != MATCH_YES)
6847 goto syntax;
6850 return MATCH_YES;
6852 syntax:
6853 gfc_error ("Syntax error in VALUE statement at %C");
6854 return MATCH_ERROR;
6858 match
6859 gfc_match_volatile (void)
6861 gfc_symbol *sym;
6862 match m;
6864 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: VOLATILE statement at %C")
6865 == FAILURE)
6866 return MATCH_ERROR;
6868 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6870 return MATCH_ERROR;
6873 if (gfc_match_eos () == MATCH_YES)
6874 goto syntax;
6876 for(;;)
6878 /* VOLATILE is special because it can be added to host-associated
6879 symbols locally. Except for coarrays. */
6880 m = gfc_match_symbol (&sym, 1);
6881 switch (m)
6883 case MATCH_YES:
6884 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
6885 for variable in a BLOCK which is defined outside of the BLOCK. */
6886 if (sym->ns != gfc_current_ns && sym->attr.codimension)
6888 gfc_error ("Specifying VOLATILE for coarray variable '%s' at "
6889 "%C, which is use-/host-associated", sym->name);
6890 return MATCH_ERROR;
6892 if (gfc_add_volatile (&sym->attr, sym->name, &gfc_current_locus)
6893 == FAILURE)
6894 return MATCH_ERROR;
6895 goto next_item;
6897 case MATCH_NO:
6898 break;
6900 case MATCH_ERROR:
6901 return MATCH_ERROR;
6904 next_item:
6905 if (gfc_match_eos () == MATCH_YES)
6906 break;
6907 if (gfc_match_char (',') != MATCH_YES)
6908 goto syntax;
6911 return MATCH_YES;
6913 syntax:
6914 gfc_error ("Syntax error in VOLATILE statement at %C");
6915 return MATCH_ERROR;
6919 match
6920 gfc_match_asynchronous (void)
6922 gfc_symbol *sym;
6923 match m;
6925 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ASYNCHRONOUS statement at %C")
6926 == FAILURE)
6927 return MATCH_ERROR;
6929 if (gfc_match (" ::") == MATCH_NO && gfc_match_space () == MATCH_NO)
6931 return MATCH_ERROR;
6934 if (gfc_match_eos () == MATCH_YES)
6935 goto syntax;
6937 for(;;)
6939 /* ASYNCHRONOUS is special because it can be added to host-associated
6940 symbols locally. */
6941 m = gfc_match_symbol (&sym, 1);
6942 switch (m)
6944 case MATCH_YES:
6945 if (gfc_add_asynchronous (&sym->attr, sym->name, &gfc_current_locus)
6946 == FAILURE)
6947 return MATCH_ERROR;
6948 goto next_item;
6950 case MATCH_NO:
6951 break;
6953 case MATCH_ERROR:
6954 return MATCH_ERROR;
6957 next_item:
6958 if (gfc_match_eos () == MATCH_YES)
6959 break;
6960 if (gfc_match_char (',') != MATCH_YES)
6961 goto syntax;
6964 return MATCH_YES;
6966 syntax:
6967 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
6968 return MATCH_ERROR;
6972 /* Match a module procedure statement. Note that we have to modify
6973 symbols in the parent's namespace because the current one was there
6974 to receive symbols that are in an interface's formal argument list. */
6976 match
6977 gfc_match_modproc (void)
6979 char name[GFC_MAX_SYMBOL_LEN + 1];
6980 gfc_symbol *sym;
6981 match m;
6982 gfc_namespace *module_ns;
6983 gfc_interface *old_interface_head, *interface;
6985 if (gfc_state_stack->state != COMP_INTERFACE
6986 || gfc_state_stack->previous == NULL
6987 || current_interface.type == INTERFACE_NAMELESS
6988 || current_interface.type == INTERFACE_ABSTRACT)
6990 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6991 "interface");
6992 return MATCH_ERROR;
6995 module_ns = gfc_current_ns->parent;
6996 for (; module_ns; module_ns = module_ns->parent)
6997 if (module_ns->proc_name->attr.flavor == FL_MODULE
6998 || module_ns->proc_name->attr.flavor == FL_PROGRAM
6999 || (module_ns->proc_name->attr.flavor == FL_PROCEDURE
7000 && !module_ns->proc_name->attr.contained))
7001 break;
7003 if (module_ns == NULL)
7004 return MATCH_ERROR;
7006 /* Store the current state of the interface. We will need it if we
7007 end up with a syntax error and need to recover. */
7008 old_interface_head = gfc_current_interface_head ();
7010 for (;;)
7012 locus old_locus = gfc_current_locus;
7013 bool last = false;
7015 m = gfc_match_name (name);
7016 if (m == MATCH_NO)
7017 goto syntax;
7018 if (m != MATCH_YES)
7019 return MATCH_ERROR;
7021 /* Check for syntax error before starting to add symbols to the
7022 current namespace. */
7023 if (gfc_match_eos () == MATCH_YES)
7024 last = true;
7025 if (!last && gfc_match_char (',') != MATCH_YES)
7026 goto syntax;
7028 /* Now we're sure the syntax is valid, we process this item
7029 further. */
7030 if (gfc_get_symbol (name, module_ns, &sym))
7031 return MATCH_ERROR;
7033 if (sym->attr.intrinsic)
7035 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7036 "PROCEDURE", &old_locus);
7037 return MATCH_ERROR;
7040 if (sym->attr.proc != PROC_MODULE
7041 && gfc_add_procedure (&sym->attr, PROC_MODULE,
7042 sym->name, NULL) == FAILURE)
7043 return MATCH_ERROR;
7045 if (gfc_add_interface (sym) == FAILURE)
7046 return MATCH_ERROR;
7048 sym->attr.mod_proc = 1;
7049 sym->declared_at = old_locus;
7051 if (last)
7052 break;
7055 return MATCH_YES;
7057 syntax:
7058 /* Restore the previous state of the interface. */
7059 interface = gfc_current_interface_head ();
7060 gfc_set_current_interface_head (old_interface_head);
7062 /* Free the new interfaces. */
7063 while (interface != old_interface_head)
7065 gfc_interface *i = interface->next;
7066 gfc_free (interface);
7067 interface = i;
7070 /* And issue a syntax error. */
7071 gfc_syntax_error (ST_MODULE_PROC);
7072 return MATCH_ERROR;
7076 /* Check a derived type that is being extended. */
7077 static gfc_symbol*
7078 check_extended_derived_type (char *name)
7080 gfc_symbol *extended;
7082 if (gfc_find_symbol (name, gfc_current_ns, 1, &extended))
7084 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7085 return NULL;
7088 if (!extended)
7090 gfc_error ("No such symbol in TYPE definition at %C");
7091 return NULL;
7094 if (extended->attr.flavor != FL_DERIVED)
7096 gfc_error ("'%s' in EXTENDS expression at %C is not a "
7097 "derived type", name);
7098 return NULL;
7101 if (extended->attr.is_bind_c)
7103 gfc_error ("'%s' cannot be extended at %C because it "
7104 "is BIND(C)", extended->name);
7105 return NULL;
7108 if (extended->attr.sequence)
7110 gfc_error ("'%s' cannot be extended at %C because it "
7111 "is a SEQUENCE type", extended->name);
7112 return NULL;
7115 return extended;
7119 /* Match the optional attribute specifiers for a type declaration.
7120 Return MATCH_ERROR if an error is encountered in one of the handled
7121 attributes (public, private, bind(c)), MATCH_NO if what's found is
7122 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7123 checking on attribute conflicts needs to be done. */
7125 match
7126 gfc_get_type_attr_spec (symbol_attribute *attr, char *name)
7128 /* See if the derived type is marked as private. */
7129 if (gfc_match (" , private") == MATCH_YES)
7131 if (gfc_current_state () != COMP_MODULE)
7133 gfc_error ("Derived type at %C can only be PRIVATE in the "
7134 "specification part of a module");
7135 return MATCH_ERROR;
7138 if (gfc_add_access (attr, ACCESS_PRIVATE, NULL, NULL) == FAILURE)
7139 return MATCH_ERROR;
7141 else if (gfc_match (" , public") == MATCH_YES)
7143 if (gfc_current_state () != COMP_MODULE)
7145 gfc_error ("Derived type at %C can only be PUBLIC in the "
7146 "specification part of a module");
7147 return MATCH_ERROR;
7150 if (gfc_add_access (attr, ACCESS_PUBLIC, NULL, NULL) == FAILURE)
7151 return MATCH_ERROR;
7153 else if (gfc_match (" , bind ( c )") == MATCH_YES)
7155 /* If the type is defined to be bind(c) it then needs to make
7156 sure that all fields are interoperable. This will
7157 need to be a semantic check on the finished derived type.
7158 See 15.2.3 (lines 9-12) of F2003 draft. */
7159 if (gfc_add_is_bind_c (attr, NULL, &gfc_current_locus, 0) != SUCCESS)
7160 return MATCH_ERROR;
7162 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
7164 else if (gfc_match (" , abstract") == MATCH_YES)
7166 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ABSTRACT type at %C")
7167 == FAILURE)
7168 return MATCH_ERROR;
7170 if (gfc_add_abstract (attr, &gfc_current_locus) == FAILURE)
7171 return MATCH_ERROR;
7173 else if (name && gfc_match(" , extends ( %n )", name) == MATCH_YES)
7175 if (gfc_add_extension (attr, &gfc_current_locus) == FAILURE)
7176 return MATCH_ERROR;
7178 else
7179 return MATCH_NO;
7181 /* If we get here, something matched. */
7182 return MATCH_YES;
7186 /* Match the beginning of a derived type declaration. If a type name
7187 was the result of a function, then it is possible to have a symbol
7188 already to be known as a derived type yet have no components. */
7190 match
7191 gfc_match_derived_decl (void)
7193 char name[GFC_MAX_SYMBOL_LEN + 1];
7194 char parent[GFC_MAX_SYMBOL_LEN + 1];
7195 symbol_attribute attr;
7196 gfc_symbol *sym;
7197 gfc_symbol *extended;
7198 match m;
7199 match is_type_attr_spec = MATCH_NO;
7200 bool seen_attr = false;
7202 if (gfc_current_state () == COMP_DERIVED)
7203 return MATCH_NO;
7205 name[0] = '\0';
7206 parent[0] = '\0';
7207 gfc_clear_attr (&attr);
7208 extended = NULL;
7212 is_type_attr_spec = gfc_get_type_attr_spec (&attr, parent);
7213 if (is_type_attr_spec == MATCH_ERROR)
7214 return MATCH_ERROR;
7215 if (is_type_attr_spec == MATCH_YES)
7216 seen_attr = true;
7217 } while (is_type_attr_spec == MATCH_YES);
7219 /* Deal with derived type extensions. The extension attribute has
7220 been added to 'attr' but now the parent type must be found and
7221 checked. */
7222 if (parent[0])
7223 extended = check_extended_derived_type (parent);
7225 if (parent[0] && !extended)
7226 return MATCH_ERROR;
7228 if (gfc_match (" ::") != MATCH_YES && seen_attr)
7230 gfc_error ("Expected :: in TYPE definition at %C");
7231 return MATCH_ERROR;
7234 m = gfc_match (" %n%t", name);
7235 if (m != MATCH_YES)
7236 return m;
7238 /* Make sure the name is not the name of an intrinsic type. */
7239 if (gfc_is_intrinsic_typename (name))
7241 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
7242 "type", name);
7243 return MATCH_ERROR;
7246 if (gfc_get_symbol (name, NULL, &sym))
7247 return MATCH_ERROR;
7249 if (sym->ts.type != BT_UNKNOWN)
7251 gfc_error ("Derived type name '%s' at %C already has a basic type "
7252 "of %s", sym->name, gfc_typename (&sym->ts));
7253 return MATCH_ERROR;
7256 /* The symbol may already have the derived attribute without the
7257 components. The ways this can happen is via a function
7258 definition, an INTRINSIC statement or a subtype in another
7259 derived type that is a pointer. The first part of the AND clause
7260 is true if the symbol is not the return value of a function. */
7261 if (sym->attr.flavor != FL_DERIVED
7262 && gfc_add_flavor (&sym->attr, FL_DERIVED, sym->name, NULL) == FAILURE)
7263 return MATCH_ERROR;
7265 if (sym->components != NULL || sym->attr.zero_comp)
7267 gfc_error ("Derived type definition of '%s' at %C has already been "
7268 "defined", sym->name);
7269 return MATCH_ERROR;
7272 if (attr.access != ACCESS_UNKNOWN
7273 && gfc_add_access (&sym->attr, attr.access, sym->name, NULL) == FAILURE)
7274 return MATCH_ERROR;
7276 /* See if the derived type was labeled as bind(c). */
7277 if (attr.is_bind_c != 0)
7278 sym->attr.is_bind_c = attr.is_bind_c;
7280 /* Construct the f2k_derived namespace if it is not yet there. */
7281 if (!sym->f2k_derived)
7282 sym->f2k_derived = gfc_get_namespace (NULL, 0);
7284 if (extended && !sym->components)
7286 gfc_component *p;
7287 gfc_symtree *st;
7289 /* Add the extended derived type as the first component. */
7290 gfc_add_component (sym, parent, &p);
7291 extended->refs++;
7292 gfc_set_sym_referenced (extended);
7294 p->ts.type = BT_DERIVED;
7295 p->ts.u.derived = extended;
7296 p->initializer = gfc_default_initializer (&p->ts);
7298 /* Set extension level. */
7299 if (extended->attr.extension == 255)
7301 /* Since the extension field is 8 bit wide, we can only have
7302 up to 255 extension levels. */
7303 gfc_error ("Maximum extension level reached with type '%s' at %L",
7304 extended->name, &extended->declared_at);
7305 return MATCH_ERROR;
7307 sym->attr.extension = extended->attr.extension + 1;
7309 /* Provide the links between the extended type and its extension. */
7310 if (!extended->f2k_derived)
7311 extended->f2k_derived = gfc_get_namespace (NULL, 0);
7312 st = gfc_new_symtree (&extended->f2k_derived->sym_root, sym->name);
7313 st->n.sym = sym;
7316 if (!sym->hash_value)
7317 /* Set the hash for the compound name for this type. */
7318 sym->hash_value = gfc_hash_value (sym);
7320 /* Take over the ABSTRACT attribute. */
7321 sym->attr.abstract = attr.abstract;
7323 gfc_new_block = sym;
7325 return MATCH_YES;
7329 /* Cray Pointees can be declared as:
7330 pointer (ipt, a (n,m,...,*)) */
7332 match
7333 gfc_mod_pointee_as (gfc_array_spec *as)
7335 as->cray_pointee = true; /* This will be useful to know later. */
7336 if (as->type == AS_ASSUMED_SIZE)
7337 as->cp_was_assumed = true;
7338 else if (as->type == AS_ASSUMED_SHAPE)
7340 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
7341 return MATCH_ERROR;
7343 return MATCH_YES;
7347 /* Match the enum definition statement, here we are trying to match
7348 the first line of enum definition statement.
7349 Returns MATCH_YES if match is found. */
7351 match
7352 gfc_match_enum (void)
7354 match m;
7356 m = gfc_match_eos ();
7357 if (m != MATCH_YES)
7358 return m;
7360 if (gfc_notify_std (GFC_STD_F2003, "Fortran 2003: ENUM and ENUMERATOR at %C")
7361 == FAILURE)
7362 return MATCH_ERROR;
7364 return MATCH_YES;
7368 /* Returns an initializer whose value is one higher than the value of the
7369 LAST_INITIALIZER argument. If the argument is NULL, the
7370 initializers value will be set to zero. The initializer's kind
7371 will be set to gfc_c_int_kind.
7373 If -fshort-enums is given, the appropriate kind will be selected
7374 later after all enumerators have been parsed. A warning is issued
7375 here if an initializer exceeds gfc_c_int_kind. */
7377 static gfc_expr *
7378 enum_initializer (gfc_expr *last_initializer, locus where)
7380 gfc_expr *result;
7381 result = gfc_get_constant_expr (BT_INTEGER, gfc_c_int_kind, &where);
7383 mpz_init (result->value.integer);
7385 if (last_initializer != NULL)
7387 mpz_add_ui (result->value.integer, last_initializer->value.integer, 1);
7388 result->where = last_initializer->where;
7390 if (gfc_check_integer_range (result->value.integer,
7391 gfc_c_int_kind) != ARITH_OK)
7393 gfc_error ("Enumerator exceeds the C integer type at %C");
7394 return NULL;
7397 else
7399 /* Control comes here, if it's the very first enumerator and no
7400 initializer has been given. It will be initialized to zero. */
7401 mpz_set_si (result->value.integer, 0);
7404 return result;
7408 /* Match a variable name with an optional initializer. When this
7409 subroutine is called, a variable is expected to be parsed next.
7410 Depending on what is happening at the moment, updates either the
7411 symbol table or the current interface. */
7413 static match
7414 enumerator_decl (void)
7416 char name[GFC_MAX_SYMBOL_LEN + 1];
7417 gfc_expr *initializer;
7418 gfc_array_spec *as = NULL;
7419 gfc_symbol *sym;
7420 locus var_locus;
7421 match m;
7422 gfc_try t;
7423 locus old_locus;
7425 initializer = NULL;
7426 old_locus = gfc_current_locus;
7428 /* When we get here, we've just matched a list of attributes and
7429 maybe a type and a double colon. The next thing we expect to see
7430 is the name of the symbol. */
7431 m = gfc_match_name (name);
7432 if (m != MATCH_YES)
7433 goto cleanup;
7435 var_locus = gfc_current_locus;
7437 /* OK, we've successfully matched the declaration. Now put the
7438 symbol in the current namespace. If we fail to create the symbol,
7439 bail out. */
7440 if (build_sym (name, NULL, false, &as, &var_locus) == FAILURE)
7442 m = MATCH_ERROR;
7443 goto cleanup;
7446 /* The double colon must be present in order to have initializers.
7447 Otherwise the statement is ambiguous with an assignment statement. */
7448 if (colon_seen)
7450 if (gfc_match_char ('=') == MATCH_YES)
7452 m = gfc_match_init_expr (&initializer);
7453 if (m == MATCH_NO)
7455 gfc_error ("Expected an initialization expression at %C");
7456 m = MATCH_ERROR;
7459 if (m != MATCH_YES)
7460 goto cleanup;
7464 /* If we do not have an initializer, the initialization value of the
7465 previous enumerator (stored in last_initializer) is incremented
7466 by 1 and is used to initialize the current enumerator. */
7467 if (initializer == NULL)
7468 initializer = enum_initializer (last_initializer, old_locus);
7470 if (initializer == NULL || initializer->ts.type != BT_INTEGER)
7472 gfc_error ("ENUMERATOR %L not initialized with integer expression",
7473 &var_locus);
7474 m = MATCH_ERROR;
7475 goto cleanup;
7478 /* Store this current initializer, for the next enumerator variable
7479 to be parsed. add_init_expr_to_sym() zeros initializer, so we
7480 use last_initializer below. */
7481 last_initializer = initializer;
7482 t = add_init_expr_to_sym (name, &initializer, &var_locus);
7484 /* Maintain enumerator history. */
7485 gfc_find_symbol (name, NULL, 0, &sym);
7486 create_enum_history (sym, last_initializer);
7488 return (t == SUCCESS) ? MATCH_YES : MATCH_ERROR;
7490 cleanup:
7491 /* Free stuff up and return. */
7492 gfc_free_expr (initializer);
7494 return m;
7498 /* Match the enumerator definition statement. */
7500 match
7501 gfc_match_enumerator_def (void)
7503 match m;
7504 gfc_try t;
7506 gfc_clear_ts (&current_ts);
7508 m = gfc_match (" enumerator");
7509 if (m != MATCH_YES)
7510 return m;
7512 m = gfc_match (" :: ");
7513 if (m == MATCH_ERROR)
7514 return m;
7516 colon_seen = (m == MATCH_YES);
7518 if (gfc_current_state () != COMP_ENUM)
7520 gfc_error ("ENUM definition statement expected before %C");
7521 gfc_free_enum_history ();
7522 return MATCH_ERROR;
7525 (&current_ts)->type = BT_INTEGER;
7526 (&current_ts)->kind = gfc_c_int_kind;
7528 gfc_clear_attr (&current_attr);
7529 t = gfc_add_flavor (&current_attr, FL_PARAMETER, NULL, NULL);
7530 if (t == FAILURE)
7532 m = MATCH_ERROR;
7533 goto cleanup;
7536 for (;;)
7538 m = enumerator_decl ();
7539 if (m == MATCH_ERROR)
7541 gfc_free_enum_history ();
7542 goto cleanup;
7544 if (m == MATCH_NO)
7545 break;
7547 if (gfc_match_eos () == MATCH_YES)
7548 goto cleanup;
7549 if (gfc_match_char (',') != MATCH_YES)
7550 break;
7553 if (gfc_current_state () == COMP_ENUM)
7555 gfc_free_enum_history ();
7556 gfc_error ("Syntax error in ENUMERATOR definition at %C");
7557 m = MATCH_ERROR;
7560 cleanup:
7561 gfc_free_array_spec (current_as);
7562 current_as = NULL;
7563 return m;
7568 /* Match binding attributes. */
7570 static match
7571 match_binding_attributes (gfc_typebound_proc* ba, bool generic, bool ppc)
7573 bool found_passing = false;
7574 bool seen_ptr = false;
7575 match m = MATCH_YES;
7577 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
7578 this case the defaults are in there. */
7579 ba->access = ACCESS_UNKNOWN;
7580 ba->pass_arg = NULL;
7581 ba->pass_arg_num = 0;
7582 ba->nopass = 0;
7583 ba->non_overridable = 0;
7584 ba->deferred = 0;
7585 ba->ppc = ppc;
7587 /* If we find a comma, we believe there are binding attributes. */
7588 m = gfc_match_char (',');
7589 if (m == MATCH_NO)
7590 goto done;
7594 /* Access specifier. */
7596 m = gfc_match (" public");
7597 if (m == MATCH_ERROR)
7598 goto error;
7599 if (m == MATCH_YES)
7601 if (ba->access != ACCESS_UNKNOWN)
7603 gfc_error ("Duplicate access-specifier at %C");
7604 goto error;
7607 ba->access = ACCESS_PUBLIC;
7608 continue;
7611 m = gfc_match (" private");
7612 if (m == MATCH_ERROR)
7613 goto error;
7614 if (m == MATCH_YES)
7616 if (ba->access != ACCESS_UNKNOWN)
7618 gfc_error ("Duplicate access-specifier at %C");
7619 goto error;
7622 ba->access = ACCESS_PRIVATE;
7623 continue;
7626 /* If inside GENERIC, the following is not allowed. */
7627 if (!generic)
7630 /* NOPASS flag. */
7631 m = gfc_match (" nopass");
7632 if (m == MATCH_ERROR)
7633 goto error;
7634 if (m == MATCH_YES)
7636 if (found_passing)
7638 gfc_error ("Binding attributes already specify passing,"
7639 " illegal NOPASS at %C");
7640 goto error;
7643 found_passing = true;
7644 ba->nopass = 1;
7645 continue;
7648 /* PASS possibly including argument. */
7649 m = gfc_match (" pass");
7650 if (m == MATCH_ERROR)
7651 goto error;
7652 if (m == MATCH_YES)
7654 char arg[GFC_MAX_SYMBOL_LEN + 1];
7656 if (found_passing)
7658 gfc_error ("Binding attributes already specify passing,"
7659 " illegal PASS at %C");
7660 goto error;
7663 m = gfc_match (" ( %n )", arg);
7664 if (m == MATCH_ERROR)
7665 goto error;
7666 if (m == MATCH_YES)
7667 ba->pass_arg = gfc_get_string (arg);
7668 gcc_assert ((m == MATCH_YES) == (ba->pass_arg != NULL));
7670 found_passing = true;
7671 ba->nopass = 0;
7672 continue;
7675 if (ppc)
7677 /* POINTER flag. */
7678 m = gfc_match (" pointer");
7679 if (m == MATCH_ERROR)
7680 goto error;
7681 if (m == MATCH_YES)
7683 if (seen_ptr)
7685 gfc_error ("Duplicate POINTER attribute at %C");
7686 goto error;
7689 seen_ptr = true;
7690 continue;
7693 else
7695 /* NON_OVERRIDABLE flag. */
7696 m = gfc_match (" non_overridable");
7697 if (m == MATCH_ERROR)
7698 goto error;
7699 if (m == MATCH_YES)
7701 if (ba->non_overridable)
7703 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
7704 goto error;
7707 ba->non_overridable = 1;
7708 continue;
7711 /* DEFERRED flag. */
7712 m = gfc_match (" deferred");
7713 if (m == MATCH_ERROR)
7714 goto error;
7715 if (m == MATCH_YES)
7717 if (ba->deferred)
7719 gfc_error ("Duplicate DEFERRED at %C");
7720 goto error;
7723 ba->deferred = 1;
7724 continue;
7730 /* Nothing matching found. */
7731 if (generic)
7732 gfc_error ("Expected access-specifier at %C");
7733 else
7734 gfc_error ("Expected binding attribute at %C");
7735 goto error;
7737 while (gfc_match_char (',') == MATCH_YES);
7739 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
7740 if (ba->non_overridable && ba->deferred)
7742 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
7743 goto error;
7746 m = MATCH_YES;
7748 done:
7749 if (ba->access == ACCESS_UNKNOWN)
7750 ba->access = gfc_typebound_default_access;
7752 if (ppc && !seen_ptr)
7754 gfc_error ("POINTER attribute is required for procedure pointer component"
7755 " at %C");
7756 goto error;
7759 return m;
7761 error:
7762 return MATCH_ERROR;
7766 /* Match a PROCEDURE specific binding inside a derived type. */
7768 static match
7769 match_procedure_in_type (void)
7771 char name[GFC_MAX_SYMBOL_LEN + 1];
7772 char target_buf[GFC_MAX_SYMBOL_LEN + 1];
7773 char* target = NULL, *ifc = NULL;
7774 gfc_typebound_proc tb;
7775 bool seen_colons;
7776 bool seen_attrs;
7777 match m;
7778 gfc_symtree* stree;
7779 gfc_namespace* ns;
7780 gfc_symbol* block;
7781 int num;
7783 /* Check current state. */
7784 gcc_assert (gfc_state_stack->state == COMP_DERIVED_CONTAINS);
7785 block = gfc_state_stack->previous->sym;
7786 gcc_assert (block);
7788 /* Try to match PROCEDURE(interface). */
7789 if (gfc_match (" (") == MATCH_YES)
7791 m = gfc_match_name (target_buf);
7792 if (m == MATCH_ERROR)
7793 return m;
7794 if (m != MATCH_YES)
7796 gfc_error ("Interface-name expected after '(' at %C");
7797 return MATCH_ERROR;
7800 if (gfc_match (" )") != MATCH_YES)
7802 gfc_error ("')' expected at %C");
7803 return MATCH_ERROR;
7806 ifc = target_buf;
7809 /* Construct the data structure. */
7810 memset (&tb, 0, sizeof (tb));
7811 tb.where = gfc_current_locus;
7813 /* Match binding attributes. */
7814 m = match_binding_attributes (&tb, false, false);
7815 if (m == MATCH_ERROR)
7816 return m;
7817 seen_attrs = (m == MATCH_YES);
7819 /* Check that attribute DEFERRED is given if an interface is specified. */
7820 if (tb.deferred && !ifc)
7822 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7823 return MATCH_ERROR;
7825 if (ifc && !tb.deferred)
7827 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7828 return MATCH_ERROR;
7831 /* Match the colons. */
7832 m = gfc_match (" ::");
7833 if (m == MATCH_ERROR)
7834 return m;
7835 seen_colons = (m == MATCH_YES);
7836 if (seen_attrs && !seen_colons)
7838 gfc_error ("Expected '::' after binding-attributes at %C");
7839 return MATCH_ERROR;
7842 /* Match the binding names. */
7843 for(num=1;;num++)
7845 m = gfc_match_name (name);
7846 if (m == MATCH_ERROR)
7847 return m;
7848 if (m == MATCH_NO)
7850 gfc_error ("Expected binding name at %C");
7851 return MATCH_ERROR;
7854 if (num>1 && gfc_notify_std (GFC_STD_F2008, "Fortran 2008: PROCEDURE list"
7855 " at %C") == FAILURE)
7856 return MATCH_ERROR;
7858 /* Try to match the '=> target', if it's there. */
7859 target = ifc;
7860 m = gfc_match (" =>");
7861 if (m == MATCH_ERROR)
7862 return m;
7863 if (m == MATCH_YES)
7865 if (tb.deferred)
7867 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7868 return MATCH_ERROR;
7871 if (!seen_colons)
7873 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7874 " at %C");
7875 return MATCH_ERROR;
7878 m = gfc_match_name (target_buf);
7879 if (m == MATCH_ERROR)
7880 return m;
7881 if (m == MATCH_NO)
7883 gfc_error ("Expected binding target after '=>' at %C");
7884 return MATCH_ERROR;
7886 target = target_buf;
7889 /* If no target was found, it has the same name as the binding. */
7890 if (!target)
7891 target = name;
7893 /* Get the namespace to insert the symbols into. */
7894 ns = block->f2k_derived;
7895 gcc_assert (ns);
7897 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7898 if (tb.deferred && !block->attr.abstract)
7900 gfc_error ("Type '%s' containing DEFERRED binding at %C "
7901 "is not ABSTRACT", block->name);
7902 return MATCH_ERROR;
7905 /* See if we already have a binding with this name in the symtree which
7906 would be an error. If a GENERIC already targetted this binding, it may
7907 be already there but then typebound is still NULL. */
7908 stree = gfc_find_symtree (ns->tb_sym_root, name);
7909 if (stree && stree->n.tb)
7911 gfc_error ("There is already a procedure with binding name '%s' for "
7912 "the derived type '%s' at %C", name, block->name);
7913 return MATCH_ERROR;
7916 /* Insert it and set attributes. */
7918 if (!stree)
7920 stree = gfc_new_symtree (&ns->tb_sym_root, name);
7921 gcc_assert (stree);
7923 stree->n.tb = gfc_get_typebound_proc (&tb);
7925 if (gfc_get_sym_tree (target, gfc_current_ns, &stree->n.tb->u.specific,
7926 false))
7927 return MATCH_ERROR;
7928 gfc_set_sym_referenced (stree->n.tb->u.specific->n.sym);
7930 if (gfc_match_eos () == MATCH_YES)
7931 return MATCH_YES;
7932 if (gfc_match_char (',') != MATCH_YES)
7933 goto syntax;
7936 syntax:
7937 gfc_error ("Syntax error in PROCEDURE statement at %C");
7938 return MATCH_ERROR;
7942 /* Match a GENERIC procedure binding inside a derived type. */
7944 match
7945 gfc_match_generic (void)
7947 char name[GFC_MAX_SYMBOL_LEN + 1];
7948 char bind_name[GFC_MAX_SYMBOL_LEN + 16]; /* Allow space for OPERATOR(...). */
7949 gfc_symbol* block;
7950 gfc_typebound_proc tbattr; /* Used for match_binding_attributes. */
7951 gfc_typebound_proc* tb;
7952 gfc_namespace* ns;
7953 interface_type op_type;
7954 gfc_intrinsic_op op;
7955 match m;
7957 /* Check current state. */
7958 if (gfc_current_state () == COMP_DERIVED)
7960 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
7961 return MATCH_ERROR;
7963 if (gfc_current_state () != COMP_DERIVED_CONTAINS)
7964 return MATCH_NO;
7965 block = gfc_state_stack->previous->sym;
7966 ns = block->f2k_derived;
7967 gcc_assert (block && ns);
7969 memset (&tbattr, 0, sizeof (tbattr));
7970 tbattr.where = gfc_current_locus;
7972 /* See if we get an access-specifier. */
7973 m = match_binding_attributes (&tbattr, true, false);
7974 if (m == MATCH_ERROR)
7975 goto error;
7977 /* Now the colons, those are required. */
7978 if (gfc_match (" ::") != MATCH_YES)
7980 gfc_error ("Expected '::' at %C");
7981 goto error;
7984 /* Match the binding name; depending on type (operator / generic) format
7985 it for future error messages into bind_name. */
7987 m = gfc_match_generic_spec (&op_type, name, &op);
7988 if (m == MATCH_ERROR)
7989 return MATCH_ERROR;
7990 if (m == MATCH_NO)
7992 gfc_error ("Expected generic name or operator descriptor at %C");
7993 goto error;
7996 switch (op_type)
7998 case INTERFACE_GENERIC:
7999 snprintf (bind_name, sizeof (bind_name), "%s", name);
8000 break;
8002 case INTERFACE_USER_OP:
8003 snprintf (bind_name, sizeof (bind_name), "OPERATOR(.%s.)", name);
8004 break;
8006 case INTERFACE_INTRINSIC_OP:
8007 snprintf (bind_name, sizeof (bind_name), "OPERATOR(%s)",
8008 gfc_op2string (op));
8009 break;
8011 default:
8012 gcc_unreachable ();
8015 /* Match the required =>. */
8016 if (gfc_match (" =>") != MATCH_YES)
8018 gfc_error ("Expected '=>' at %C");
8019 goto error;
8022 /* Try to find existing GENERIC binding with this name / for this operator;
8023 if there is something, check that it is another GENERIC and then extend
8024 it rather than building a new node. Otherwise, create it and put it
8025 at the right position. */
8027 switch (op_type)
8029 case INTERFACE_USER_OP:
8030 case INTERFACE_GENERIC:
8032 const bool is_op = (op_type == INTERFACE_USER_OP);
8033 gfc_symtree* st;
8035 st = gfc_find_symtree (is_op ? ns->tb_uop_root : ns->tb_sym_root, name);
8036 if (st)
8038 tb = st->n.tb;
8039 gcc_assert (tb);
8041 else
8042 tb = NULL;
8044 break;
8047 case INTERFACE_INTRINSIC_OP:
8048 tb = ns->tb_op[op];
8049 break;
8051 default:
8052 gcc_unreachable ();
8055 if (tb)
8057 if (!tb->is_generic)
8059 gcc_assert (op_type == INTERFACE_GENERIC);
8060 gfc_error ("There's already a non-generic procedure with binding name"
8061 " '%s' for the derived type '%s' at %C",
8062 bind_name, block->name);
8063 goto error;
8066 if (tb->access != tbattr.access)
8068 gfc_error ("Binding at %C must have the same access as already"
8069 " defined binding '%s'", bind_name);
8070 goto error;
8073 else
8075 tb = gfc_get_typebound_proc (NULL);
8076 tb->where = gfc_current_locus;
8077 tb->access = tbattr.access;
8078 tb->is_generic = 1;
8079 tb->u.generic = NULL;
8081 switch (op_type)
8083 case INTERFACE_GENERIC:
8084 case INTERFACE_USER_OP:
8086 const bool is_op = (op_type == INTERFACE_USER_OP);
8087 gfc_symtree* st;
8089 st = gfc_new_symtree (is_op ? &ns->tb_uop_root : &ns->tb_sym_root,
8090 name);
8091 gcc_assert (st);
8092 st->n.tb = tb;
8094 break;
8097 case INTERFACE_INTRINSIC_OP:
8098 ns->tb_op[op] = tb;
8099 break;
8101 default:
8102 gcc_unreachable ();
8106 /* Now, match all following names as specific targets. */
8109 gfc_symtree* target_st;
8110 gfc_tbp_generic* target;
8112 m = gfc_match_name (name);
8113 if (m == MATCH_ERROR)
8114 goto error;
8115 if (m == MATCH_NO)
8117 gfc_error ("Expected specific binding name at %C");
8118 goto error;
8121 target_st = gfc_get_tbp_symtree (&ns->tb_sym_root, name);
8123 /* See if this is a duplicate specification. */
8124 for (target = tb->u.generic; target; target = target->next)
8125 if (target_st == target->specific_st)
8127 gfc_error ("'%s' already defined as specific binding for the"
8128 " generic '%s' at %C", name, bind_name);
8129 goto error;
8132 target = gfc_get_tbp_generic ();
8133 target->specific_st = target_st;
8134 target->specific = NULL;
8135 target->next = tb->u.generic;
8136 tb->u.generic = target;
8138 while (gfc_match (" ,") == MATCH_YES);
8140 /* Here should be the end. */
8141 if (gfc_match_eos () != MATCH_YES)
8143 gfc_error ("Junk after GENERIC binding at %C");
8144 goto error;
8147 return MATCH_YES;
8149 error:
8150 return MATCH_ERROR;
8154 /* Match a FINAL declaration inside a derived type. */
8156 match
8157 gfc_match_final_decl (void)
8159 char name[GFC_MAX_SYMBOL_LEN + 1];
8160 gfc_symbol* sym;
8161 match m;
8162 gfc_namespace* module_ns;
8163 bool first, last;
8164 gfc_symbol* block;
8166 if (gfc_current_form == FORM_FREE)
8168 char c = gfc_peek_ascii_char ();
8169 if (!gfc_is_whitespace (c) && c != ':')
8170 return MATCH_NO;
8173 if (gfc_state_stack->state != COMP_DERIVED_CONTAINS)
8175 if (gfc_current_form == FORM_FIXED)
8176 return MATCH_NO;
8178 gfc_error ("FINAL declaration at %C must be inside a derived type "
8179 "CONTAINS section");
8180 return MATCH_ERROR;
8183 block = gfc_state_stack->previous->sym;
8184 gcc_assert (block);
8186 if (!gfc_state_stack->previous || !gfc_state_stack->previous->previous
8187 || gfc_state_stack->previous->previous->state != COMP_MODULE)
8189 gfc_error ("Derived type declaration with FINAL at %C must be in the"
8190 " specification part of a MODULE");
8191 return MATCH_ERROR;
8194 module_ns = gfc_current_ns;
8195 gcc_assert (module_ns);
8196 gcc_assert (module_ns->proc_name->attr.flavor == FL_MODULE);
8198 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
8199 if (gfc_match (" ::") == MATCH_ERROR)
8200 return MATCH_ERROR;
8202 /* Match the sequence of procedure names. */
8203 first = true;
8204 last = false;
8207 gfc_finalizer* f;
8209 if (first && gfc_match_eos () == MATCH_YES)
8211 gfc_error ("Empty FINAL at %C");
8212 return MATCH_ERROR;
8215 m = gfc_match_name (name);
8216 if (m == MATCH_NO)
8218 gfc_error ("Expected module procedure name at %C");
8219 return MATCH_ERROR;
8221 else if (m != MATCH_YES)
8222 return MATCH_ERROR;
8224 if (gfc_match_eos () == MATCH_YES)
8225 last = true;
8226 if (!last && gfc_match_char (',') != MATCH_YES)
8228 gfc_error ("Expected ',' at %C");
8229 return MATCH_ERROR;
8232 if (gfc_get_symbol (name, module_ns, &sym))
8234 gfc_error ("Unknown procedure name \"%s\" at %C", name);
8235 return MATCH_ERROR;
8238 /* Mark the symbol as module procedure. */
8239 if (sym->attr.proc != PROC_MODULE
8240 && gfc_add_procedure (&sym->attr, PROC_MODULE,
8241 sym->name, NULL) == FAILURE)
8242 return MATCH_ERROR;
8244 /* Check if we already have this symbol in the list, this is an error. */
8245 for (f = block->f2k_derived->finalizers; f; f = f->next)
8246 if (f->proc_sym == sym)
8248 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
8249 name);
8250 return MATCH_ERROR;
8253 /* Add this symbol to the list of finalizers. */
8254 gcc_assert (block->f2k_derived);
8255 ++sym->refs;
8256 f = XCNEW (gfc_finalizer);
8257 f->proc_sym = sym;
8258 f->proc_tree = NULL;
8259 f->where = gfc_current_locus;
8260 f->next = block->f2k_derived->finalizers;
8261 block->f2k_derived->finalizers = f;
8263 first = false;
8265 while (!last);
8267 return MATCH_YES;
8271 const ext_attr_t ext_attr_list[] = {
8272 { "dllimport", EXT_ATTR_DLLIMPORT, "dllimport" },
8273 { "dllexport", EXT_ATTR_DLLEXPORT, "dllexport" },
8274 { "cdecl", EXT_ATTR_CDECL, "cdecl" },
8275 { "stdcall", EXT_ATTR_STDCALL, "stdcall" },
8276 { "fastcall", EXT_ATTR_FASTCALL, "fastcall" },
8277 { NULL, EXT_ATTR_LAST, NULL }
8280 /* Match a !GCC$ ATTRIBUTES statement of the form:
8281 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
8282 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
8284 TODO: We should support all GCC attributes using the same syntax for
8285 the attribute list, i.e. the list in C
8286 __attributes(( attribute-list ))
8287 matches then
8288 !GCC$ ATTRIBUTES attribute-list ::
8289 Cf. c-parser.c's c_parser_attributes; the data can then directly be
8290 saved into a TREE.
8292 As there is absolutely no risk of confusion, we should never return
8293 MATCH_NO. */
8294 match
8295 gfc_match_gcc_attributes (void)
8297 symbol_attribute attr;
8298 char name[GFC_MAX_SYMBOL_LEN + 1];
8299 unsigned id;
8300 gfc_symbol *sym;
8301 match m;
8303 gfc_clear_attr (&attr);
8304 for(;;)
8306 char ch;
8308 if (gfc_match_name (name) != MATCH_YES)
8309 return MATCH_ERROR;
8311 for (id = 0; id < EXT_ATTR_LAST; id++)
8312 if (strcmp (name, ext_attr_list[id].name) == 0)
8313 break;
8315 if (id == EXT_ATTR_LAST)
8317 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
8318 return MATCH_ERROR;
8321 if (gfc_add_ext_attribute (&attr, (ext_attr_id_t) id, &gfc_current_locus)
8322 == FAILURE)
8323 return MATCH_ERROR;
8325 gfc_gobble_whitespace ();
8326 ch = gfc_next_ascii_char ();
8327 if (ch == ':')
8329 /* This is the successful exit condition for the loop. */
8330 if (gfc_next_ascii_char () == ':')
8331 break;
8334 if (ch == ',')
8335 continue;
8337 goto syntax;
8340 if (gfc_match_eos () == MATCH_YES)
8341 goto syntax;
8343 for(;;)
8345 m = gfc_match_name (name);
8346 if (m != MATCH_YES)
8347 return m;
8349 if (find_special (name, &sym, true))
8350 return MATCH_ERROR;
8352 sym->attr.ext_attr |= attr.ext_attr;
8354 if (gfc_match_eos () == MATCH_YES)
8355 break;
8357 if (gfc_match_char (',') != MATCH_YES)
8358 goto syntax;
8361 return MATCH_YES;
8363 syntax:
8364 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");
8365 return MATCH_ERROR;