1 /* Declaration statement matcher
2 Copyright (C) 2002-2016 Free Software Foundation, Inc.
3 Contributed by Andy Vaught
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
23 #include "coretypes.h"
27 #include "stringpool.h"
30 #include "constructor.h"
32 /* Macros to access allocate memory for gfc_data_variable,
33 gfc_data_value and gfc_data. */
34 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
35 #define gfc_get_data_value() XCNEW (gfc_data_value)
36 #define gfc_get_data() XCNEW (gfc_data)
39 static bool set_binding_label (const char **, const char *, int);
42 /* This flag is set if an old-style length selector is matched
43 during a type-declaration statement. */
45 static int old_char_selector
;
47 /* When variables acquire types and attributes from a declaration
48 statement, they get them from the following static variables. The
49 first part of a declaration sets these variables and the second
50 part copies these into symbol structures. */
52 static gfc_typespec current_ts
;
54 static symbol_attribute current_attr
;
55 static gfc_array_spec
*current_as
;
56 static int colon_seen
;
58 /* The current binding label (if any). */
59 static const char* curr_binding_label
;
60 /* Need to know how many identifiers are on the current data declaration
61 line in case we're given the BIND(C) attribute with a NAME= specifier. */
62 static int num_idents_on_line
;
63 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
64 can supply a name if the curr_binding_label is nil and NAME= was not. */
65 static int has_name_equals
= 0;
67 /* Initializer of the previous enumerator. */
69 static gfc_expr
*last_initializer
;
71 /* History of all the enumerators is maintained, so that
72 kind values of all the enumerators could be updated depending
73 upon the maximum initialized value. */
75 typedef struct enumerator_history
78 gfc_expr
*initializer
;
79 struct enumerator_history
*next
;
83 /* Header of enum history chain. */
85 static enumerator_history
*enum_history
= NULL
;
87 /* Pointer of enum history node containing largest initializer. */
89 static enumerator_history
*max_enum
= NULL
;
91 /* gfc_new_block points to the symbol of a newly matched block. */
93 gfc_symbol
*gfc_new_block
;
95 bool gfc_matching_function
;
98 /********************* DATA statement subroutines *********************/
100 static bool in_match_data
= false;
103 gfc_in_match_data (void)
105 return in_match_data
;
109 set_in_match_data (bool set_value
)
111 in_match_data
= set_value
;
114 /* Free a gfc_data_variable structure and everything beneath it. */
117 free_variable (gfc_data_variable
*p
)
119 gfc_data_variable
*q
;
124 gfc_free_expr (p
->expr
);
125 gfc_free_iterator (&p
->iter
, 0);
126 free_variable (p
->list
);
132 /* Free a gfc_data_value structure and everything beneath it. */
135 free_value (gfc_data_value
*p
)
142 mpz_clear (p
->repeat
);
143 gfc_free_expr (p
->expr
);
149 /* Free a list of gfc_data structures. */
152 gfc_free_data (gfc_data
*p
)
159 free_variable (p
->var
);
160 free_value (p
->value
);
166 /* Free all data in a namespace. */
169 gfc_free_data_all (gfc_namespace
*ns
)
181 /* Reject data parsed since the last restore point was marked. */
184 gfc_reject_data (gfc_namespace
*ns
)
188 while (ns
->data
&& ns
->data
!= ns
->old_data
)
196 static match
var_element (gfc_data_variable
*);
198 /* Match a list of variables terminated by an iterator and a right
202 var_list (gfc_data_variable
*parent
)
204 gfc_data_variable
*tail
, var
;
207 m
= var_element (&var
);
208 if (m
== MATCH_ERROR
)
213 tail
= gfc_get_data_variable ();
220 if (gfc_match_char (',') != MATCH_YES
)
223 m
= gfc_match_iterator (&parent
->iter
, 1);
226 if (m
== MATCH_ERROR
)
229 m
= var_element (&var
);
230 if (m
== MATCH_ERROR
)
235 tail
->next
= gfc_get_data_variable ();
241 if (gfc_match_char (')') != MATCH_YES
)
246 gfc_syntax_error (ST_DATA
);
251 /* Match a single element in a data variable list, which can be a
252 variable-iterator list. */
255 var_element (gfc_data_variable
*new_var
)
260 memset (new_var
, 0, sizeof (gfc_data_variable
));
262 if (gfc_match_char ('(') == MATCH_YES
)
263 return var_list (new_var
);
265 m
= gfc_match_variable (&new_var
->expr
, 0);
269 sym
= new_var
->expr
->symtree
->n
.sym
;
271 /* Symbol should already have an associated type. */
272 if (!gfc_check_symbol_typed (sym
, gfc_current_ns
, false, gfc_current_locus
))
275 if (!sym
->attr
.function
&& gfc_current_ns
->parent
276 && gfc_current_ns
->parent
== sym
->ns
)
278 gfc_error ("Host associated variable %qs may not be in the DATA "
279 "statement at %C", sym
->name
);
283 if (gfc_current_state () != COMP_BLOCK_DATA
284 && sym
->attr
.in_common
285 && !gfc_notify_std (GFC_STD_GNU
, "initialization of "
286 "common block variable %qs in DATA statement at %C",
290 if (!gfc_add_data (&sym
->attr
, sym
->name
, &new_var
->expr
->where
))
297 /* Match the top-level list of data variables. */
300 top_var_list (gfc_data
*d
)
302 gfc_data_variable var
, *tail
, *new_var
;
309 m
= var_element (&var
);
312 if (m
== MATCH_ERROR
)
315 new_var
= gfc_get_data_variable ();
321 tail
->next
= new_var
;
325 if (gfc_match_char ('/') == MATCH_YES
)
327 if (gfc_match_char (',') != MATCH_YES
)
334 gfc_syntax_error (ST_DATA
);
335 gfc_free_data_all (gfc_current_ns
);
341 match_data_constant (gfc_expr
**result
)
343 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
344 gfc_symbol
*sym
, *dt_sym
= NULL
;
349 m
= gfc_match_literal_constant (&expr
, 1);
356 if (m
== MATCH_ERROR
)
359 m
= gfc_match_null (result
);
363 old_loc
= gfc_current_locus
;
365 /* Should this be a structure component, try to match it
366 before matching a name. */
367 m
= gfc_match_rvalue (result
);
368 if (m
== MATCH_ERROR
)
371 if (m
== MATCH_YES
&& (*result
)->expr_type
== EXPR_STRUCTURE
)
373 if (!gfc_simplify_expr (*result
, 0))
377 else if (m
== MATCH_YES
)
378 gfc_free_expr (*result
);
380 gfc_current_locus
= old_loc
;
382 m
= gfc_match_name (name
);
386 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
389 if (sym
&& sym
->attr
.generic
)
390 dt_sym
= gfc_find_dt_in_generic (sym
);
393 || (sym
->attr
.flavor
!= FL_PARAMETER
394 && (!dt_sym
|| dt_sym
->attr
.flavor
!= FL_DERIVED
)))
396 gfc_error ("Symbol %qs must be a PARAMETER in DATA statement at %C",
400 else if (dt_sym
&& dt_sym
->attr
.flavor
== FL_DERIVED
)
401 return gfc_match_structure_constructor (dt_sym
, result
);
403 /* Check to see if the value is an initialization array expression. */
404 if (sym
->value
->expr_type
== EXPR_ARRAY
)
406 gfc_current_locus
= old_loc
;
408 m
= gfc_match_init_expr (result
);
409 if (m
== MATCH_ERROR
)
414 if (!gfc_simplify_expr (*result
, 0))
417 if ((*result
)->expr_type
== EXPR_CONSTANT
)
421 gfc_error ("Invalid initializer %s in Data statement at %C", name
);
427 *result
= gfc_copy_expr (sym
->value
);
432 /* Match a list of values in a DATA statement. The leading '/' has
433 already been seen at this point. */
436 top_val_list (gfc_data
*data
)
438 gfc_data_value
*new_val
, *tail
;
446 m
= match_data_constant (&expr
);
449 if (m
== MATCH_ERROR
)
452 new_val
= gfc_get_data_value ();
453 mpz_init (new_val
->repeat
);
456 data
->value
= new_val
;
458 tail
->next
= new_val
;
462 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
465 mpz_set_ui (tail
->repeat
, 1);
469 mpz_set (tail
->repeat
, expr
->value
.integer
);
470 gfc_free_expr (expr
);
472 m
= match_data_constant (&tail
->expr
);
475 if (m
== MATCH_ERROR
)
479 if (gfc_match_char ('/') == MATCH_YES
)
481 if (gfc_match_char (',') == MATCH_NO
)
488 gfc_syntax_error (ST_DATA
);
489 gfc_free_data_all (gfc_current_ns
);
494 /* Matches an old style initialization. */
497 match_old_style_init (const char *name
)
504 /* Set up data structure to hold initializers. */
505 gfc_find_sym_tree (name
, NULL
, 0, &st
);
508 newdata
= gfc_get_data ();
509 newdata
->var
= gfc_get_data_variable ();
510 newdata
->var
->expr
= gfc_get_variable_expr (st
);
511 newdata
->where
= gfc_current_locus
;
513 /* Match initial value list. This also eats the terminal '/'. */
514 m
= top_val_list (newdata
);
523 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
527 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
529 /* Mark the variable as having appeared in a data statement. */
530 if (!gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
))
536 /* Chain in namespace list of DATA initializers. */
537 newdata
->next
= gfc_current_ns
->data
;
538 gfc_current_ns
->data
= newdata
;
544 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
545 we are matching a DATA statement and are therefore issuing an error
546 if we encounter something unexpected, if not, we're trying to match
547 an old-style initialization expression of the form INTEGER I /2/. */
550 gfc_match_data (void)
555 /* Before parsing the rest of a DATA statement, check F2008:c1206. */
556 if ((gfc_current_state () == COMP_FUNCTION
557 || gfc_current_state () == COMP_SUBROUTINE
)
558 && gfc_state_stack
->previous
->state
== COMP_INTERFACE
)
560 gfc_error ("DATA statement at %C cannot appear within an INTERFACE");
564 set_in_match_data (true);
568 new_data
= gfc_get_data ();
569 new_data
->where
= gfc_current_locus
;
571 m
= top_var_list (new_data
);
575 m
= top_val_list (new_data
);
579 new_data
->next
= gfc_current_ns
->data
;
580 gfc_current_ns
->data
= new_data
;
582 if (gfc_match_eos () == MATCH_YES
)
585 gfc_match_char (','); /* Optional comma */
588 set_in_match_data (false);
592 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
595 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
600 set_in_match_data (false);
601 gfc_free_data (new_data
);
606 /************************ Declaration statements *********************/
609 /* Auxiliary function to merge DIMENSION and CODIMENSION array specs. */
612 merge_array_spec (gfc_array_spec
*from
, gfc_array_spec
*to
, bool copy
)
616 if ((from
->type
== AS_ASSUMED_RANK
&& to
->corank
)
617 || (to
->type
== AS_ASSUMED_RANK
&& from
->corank
))
619 gfc_error ("The assumed-rank array at %C shall not have a codimension");
623 if (to
->rank
== 0 && from
->rank
> 0)
625 to
->rank
= from
->rank
;
626 to
->type
= from
->type
;
627 to
->cray_pointee
= from
->cray_pointee
;
628 to
->cp_was_assumed
= from
->cp_was_assumed
;
630 for (i
= 0; i
< to
->corank
; i
++)
632 to
->lower
[from
->rank
+ i
] = to
->lower
[i
];
633 to
->upper
[from
->rank
+ i
] = to
->upper
[i
];
635 for (i
= 0; i
< from
->rank
; i
++)
639 to
->lower
[i
] = gfc_copy_expr (from
->lower
[i
]);
640 to
->upper
[i
] = gfc_copy_expr (from
->upper
[i
]);
644 to
->lower
[i
] = from
->lower
[i
];
645 to
->upper
[i
] = from
->upper
[i
];
649 else if (to
->corank
== 0 && from
->corank
> 0)
651 to
->corank
= from
->corank
;
652 to
->cotype
= from
->cotype
;
654 for (i
= 0; i
< from
->corank
; i
++)
658 to
->lower
[to
->rank
+ i
] = gfc_copy_expr (from
->lower
[i
]);
659 to
->upper
[to
->rank
+ i
] = gfc_copy_expr (from
->upper
[i
]);
663 to
->lower
[to
->rank
+ i
] = from
->lower
[i
];
664 to
->upper
[to
->rank
+ i
] = from
->upper
[i
];
673 /* Match an intent specification. Since this can only happen after an
674 INTENT word, a legal intent-spec must follow. */
677 match_intent_spec (void)
680 if (gfc_match (" ( in out )") == MATCH_YES
)
682 if (gfc_match (" ( in )") == MATCH_YES
)
684 if (gfc_match (" ( out )") == MATCH_YES
)
687 gfc_error ("Bad INTENT specification at %C");
688 return INTENT_UNKNOWN
;
692 /* Matches a character length specification, which is either a
693 specification expression, '*', or ':'. */
696 char_len_param_value (gfc_expr
**expr
, bool *deferred
)
703 if (gfc_match_char ('*') == MATCH_YES
)
706 if (gfc_match_char (':') == MATCH_YES
)
708 if (!gfc_notify_std (GFC_STD_F2003
, "deferred type parameter at %C"))
716 m
= gfc_match_expr (expr
);
718 if (m
== MATCH_NO
|| m
== MATCH_ERROR
)
721 if (!gfc_expr_check_typed (*expr
, gfc_current_ns
, false))
724 if ((*expr
)->expr_type
== EXPR_FUNCTION
)
726 if ((*expr
)->ts
.type
== BT_INTEGER
727 || ((*expr
)->ts
.type
== BT_UNKNOWN
728 && strcmp((*expr
)->symtree
->name
, "null") != 0))
733 else if ((*expr
)->expr_type
== EXPR_CONSTANT
)
735 /* F2008, 4.4.3.1: The length is a type parameter; its kind is
736 processor dependent and its value is greater than or equal to zero.
737 F2008, 4.4.3.2: If the character length parameter value evaluates
738 to a negative value, the length of character entities declared
741 if ((*expr
)->ts
.type
== BT_INTEGER
)
743 if (mpz_cmp_si ((*expr
)->value
.integer
, 0) < 0)
744 mpz_set_si ((*expr
)->value
.integer
, 0);
749 else if ((*expr
)->expr_type
== EXPR_ARRAY
)
751 else if ((*expr
)->expr_type
== EXPR_VARIABLE
)
755 e
= gfc_copy_expr (*expr
);
757 /* This catches the invalid code "[character(m(2:3)) :: 'x', 'y']",
758 which causes an ICE if gfc_reduce_init_expr() is called. */
759 if (e
->ref
&& e
->ref
->type
== REF_ARRAY
760 && e
->ref
->u
.ar
.type
== AR_UNKNOWN
761 && e
->ref
->u
.ar
.dimen_type
[0] == DIMEN_RANGE
)
764 gfc_reduce_init_expr (e
);
766 if ((e
->ref
&& e
->ref
->type
== REF_ARRAY
767 && e
->ref
->u
.ar
.type
!= AR_ELEMENT
)
768 || (!e
->ref
&& e
->expr_type
== EXPR_ARRAY
))
780 gfc_error ("Scalar INTEGER expression expected at %L", &(*expr
)->where
);
785 /* A character length is a '*' followed by a literal integer or a
786 char_len_param_value in parenthesis. */
789 match_char_length (gfc_expr
**expr
, bool *deferred
, bool obsolescent_check
)
795 m
= gfc_match_char ('*');
799 m
= gfc_match_small_literal_int (&length
, NULL
);
800 if (m
== MATCH_ERROR
)
805 if (obsolescent_check
806 && !gfc_notify_std (GFC_STD_F95_OBS
, "Old-style character length at %C"))
808 *expr
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, length
);
812 if (gfc_match_char ('(') == MATCH_NO
)
815 m
= char_len_param_value (expr
, deferred
);
816 if (m
!= MATCH_YES
&& gfc_matching_function
)
822 if (m
== MATCH_ERROR
)
827 if (gfc_match_char (')') == MATCH_NO
)
829 gfc_free_expr (*expr
);
837 gfc_error ("Syntax error in character length specification at %C");
842 /* Special subroutine for finding a symbol. Check if the name is found
843 in the current name space. If not, and we're compiling a function or
844 subroutine and the parent compilation unit is an interface, then check
845 to see if the name we've been given is the name of the interface
846 (located in another namespace). */
849 find_special (const char *name
, gfc_symbol
**result
, bool allow_subroutine
)
855 i
= gfc_get_sym_tree (name
, NULL
, &st
, allow_subroutine
);
858 *result
= st
? st
->n
.sym
: NULL
;
862 if (gfc_current_state () != COMP_SUBROUTINE
863 && gfc_current_state () != COMP_FUNCTION
)
866 s
= gfc_state_stack
->previous
;
870 if (s
->state
!= COMP_INTERFACE
)
873 goto end
; /* Nameless interface. */
875 if (strcmp (name
, s
->sym
->name
) == 0)
886 /* Special subroutine for getting a symbol node associated with a
887 procedure name, used in SUBROUTINE and FUNCTION statements. The
888 symbol is created in the parent using with symtree node in the
889 child unit pointing to the symbol. If the current namespace has no
890 parent, then the symbol is just created in the current unit. */
893 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
899 /* Module functions have to be left in their own namespace because
900 they have potentially (almost certainly!) already been referenced.
901 In this sense, they are rather like external functions. This is
902 fixed up in resolve.c(resolve_entries), where the symbol name-
903 space is set to point to the master function, so that the fake
904 result mechanism can work. */
905 if (module_fcn_entry
)
907 /* Present if entry is declared to be a module procedure. */
908 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
911 rc
= gfc_get_symbol (name
, NULL
, result
);
912 else if (!gfc_get_symbol (name
, NULL
, &sym
) && sym
913 && (*result
)->ts
.type
== BT_UNKNOWN
914 && sym
->attr
.flavor
== FL_UNKNOWN
)
915 /* Pick up the typespec for the entry, if declared in the function
916 body. Note that this symbol is FL_UNKNOWN because it will
917 only have appeared in a type declaration. The local symtree
918 is set to point to the module symbol and a unique symtree
919 to the local version. This latter ensures a correct clearing
922 /* If the ENTRY proceeds its specification, we need to ensure
923 that this does not raise a "has no IMPLICIT type" error. */
924 if (sym
->ts
.type
== BT_UNKNOWN
)
925 sym
->attr
.untyped
= 1;
927 (*result
)->ts
= sym
->ts
;
929 /* Put the symbol in the procedure namespace so that, should
930 the ENTRY precede its specification, the specification
932 (*result
)->ns
= gfc_current_ns
;
934 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
936 st
= gfc_get_unique_symtree (gfc_current_ns
);
942 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
948 if (sym
->attr
.proc
== PROC_ST_FUNCTION
)
951 if (sym
->attr
.module_procedure
952 && sym
->attr
.if_source
== IFSRC_IFBODY
)
954 /* Create a partially populated interface symbol to carry the
955 characteristics of the procedure and the result. */
956 sym
->ts
.interface
= gfc_new_symbol (name
, sym
->ns
);
957 gfc_add_type (sym
->ts
.interface
, &(sym
->ts
),
959 gfc_copy_attr (&sym
->ts
.interface
->attr
, &sym
->attr
, NULL
);
960 if (sym
->attr
.dimension
)
961 sym
->ts
.interface
->as
= gfc_copy_array_spec (sym
->as
);
963 /* Ideally, at this point, a copy would be made of the formal
964 arguments and their namespace. However, this does not appear
965 to be necessary, albeit at the expense of not being able to
966 use gfc_compare_interfaces directly. */
968 if (sym
->result
&& sym
->result
!= sym
)
970 sym
->ts
.interface
->result
= sym
->result
;
973 else if (sym
->result
)
975 sym
->ts
.interface
->result
= sym
->ts
.interface
;
978 else if (sym
&& !sym
->gfc_new
979 && gfc_current_state () != COMP_INTERFACE
)
981 /* Trap another encompassed procedure with the same name. All
982 these conditions are necessary to avoid picking up an entry
983 whose name clashes with that of the encompassing procedure;
984 this is handled using gsymbols to register unique, globally
986 if (sym
->attr
.flavor
!= 0
987 && sym
->attr
.proc
!= 0
988 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
989 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
990 gfc_error_now ("Procedure %qs at %C is already defined at %L",
991 name
, &sym
->declared_at
);
993 /* Trap a procedure with a name the same as interface in the
994 encompassing scope. */
995 if (sym
->attr
.generic
!= 0
996 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
997 && !sym
->attr
.mod_proc
)
998 gfc_error_now ("Name %qs at %C is already defined"
999 " as a generic interface at %L",
1000 name
, &sym
->declared_at
);
1002 /* Trap declarations of attributes in encompassing scope. The
1003 signature for this is that ts.kind is set. Legitimate
1004 references only set ts.type. */
1005 if (sym
->ts
.kind
!= 0
1006 && !sym
->attr
.implicit_type
1007 && sym
->attr
.proc
== 0
1008 && gfc_current_ns
->parent
!= NULL
1009 && sym
->attr
.access
== 0
1010 && !module_fcn_entry
)
1011 gfc_error_now ("Procedure %qs at %C has an explicit interface "
1012 "and must not have attributes declared at %L",
1013 name
, &sym
->declared_at
);
1016 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
1019 /* Module function entries will already have a symtree in
1020 the current namespace but will need one at module level. */
1021 if (module_fcn_entry
)
1023 /* Present if entry is declared to be a module procedure. */
1024 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
1026 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
1029 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
1034 /* See if the procedure should be a module procedure. */
1036 if (((sym
->ns
->proc_name
!= NULL
1037 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
1038 && sym
->attr
.proc
!= PROC_MODULE
)
1039 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
1040 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
1047 /* Verify that the given symbol representing a parameter is C
1048 interoperable, by checking to see if it was marked as such after
1049 its declaration. If the given symbol is not interoperable, a
1050 warning is reported, thus removing the need to return the status to
1051 the calling function. The standard does not require the user use
1052 one of the iso_c_binding named constants to declare an
1053 interoperable parameter, but we can't be sure if the param is C
1054 interop or not if the user doesn't. For example, integer(4) may be
1055 legal Fortran, but doesn't have meaning in C. It may interop with
1056 a number of the C types, which causes a problem because the
1057 compiler can't know which one. This code is almost certainly not
1058 portable, and the user will get what they deserve if the C type
1059 across platforms isn't always interoperable with integer(4). If
1060 the user had used something like integer(c_int) or integer(c_long),
1061 the compiler could have automatically handled the varying sizes
1062 across platforms. */
1065 gfc_verify_c_interop_param (gfc_symbol
*sym
)
1067 int is_c_interop
= 0;
1070 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
1071 Don't repeat the checks here. */
1072 if (sym
->attr
.implicit_type
)
1075 /* For subroutines or functions that are passed to a BIND(C) procedure,
1076 they're interoperable if they're BIND(C) and their params are all
1078 if (sym
->attr
.flavor
== FL_PROCEDURE
)
1080 if (sym
->attr
.is_bind_c
== 0)
1082 gfc_error_now ("Procedure %qs at %L must have the BIND(C) "
1083 "attribute to be C interoperable", sym
->name
,
1084 &(sym
->declared_at
));
1089 if (sym
->attr
.is_c_interop
== 1)
1090 /* We've already checked this procedure; don't check it again. */
1093 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
1098 /* See if we've stored a reference to a procedure that owns sym. */
1099 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
1101 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
1103 is_c_interop
= (gfc_verify_c_interop(&(sym
->ts
)) ? 1 : 0);
1105 if (is_c_interop
!= 1)
1107 /* Make personalized messages to give better feedback. */
1108 if (sym
->ts
.type
== BT_DERIVED
)
1109 gfc_error ("Variable %qs at %L is a dummy argument to the "
1110 "BIND(C) procedure %qs but is not C interoperable "
1111 "because derived type %qs is not C interoperable",
1112 sym
->name
, &(sym
->declared_at
),
1113 sym
->ns
->proc_name
->name
,
1114 sym
->ts
.u
.derived
->name
);
1115 else if (sym
->ts
.type
== BT_CLASS
)
1116 gfc_error ("Variable %qs at %L is a dummy argument to the "
1117 "BIND(C) procedure %qs but is not C interoperable "
1118 "because it is polymorphic",
1119 sym
->name
, &(sym
->declared_at
),
1120 sym
->ns
->proc_name
->name
);
1121 else if (warn_c_binding_type
)
1122 gfc_warning (OPT_Wc_binding_type
,
1123 "Variable %qs at %L is a dummy argument of the "
1124 "BIND(C) procedure %qs but may not be C "
1126 sym
->name
, &(sym
->declared_at
),
1127 sym
->ns
->proc_name
->name
);
1130 /* Character strings are only C interoperable if they have a
1132 if (sym
->ts
.type
== BT_CHARACTER
)
1134 gfc_charlen
*cl
= sym
->ts
.u
.cl
;
1135 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
1136 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
1138 gfc_error ("Character argument %qs at %L "
1139 "must be length 1 because "
1140 "procedure %qs is BIND(C)",
1141 sym
->name
, &sym
->declared_at
,
1142 sym
->ns
->proc_name
->name
);
1147 /* We have to make sure that any param to a bind(c) routine does
1148 not have the allocatable, pointer, or optional attributes,
1149 according to J3/04-007, section 5.1. */
1150 if (sym
->attr
.allocatable
== 1
1151 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs at %L with "
1152 "ALLOCATABLE attribute in procedure %qs "
1153 "with BIND(C)", sym
->name
,
1154 &(sym
->declared_at
),
1155 sym
->ns
->proc_name
->name
))
1158 if (sym
->attr
.pointer
== 1
1159 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs at %L with "
1160 "POINTER attribute in procedure %qs "
1161 "with BIND(C)", sym
->name
,
1162 &(sym
->declared_at
),
1163 sym
->ns
->proc_name
->name
))
1166 if ((sym
->attr
.allocatable
|| sym
->attr
.pointer
) && !sym
->as
)
1168 gfc_error ("Scalar variable %qs at %L with POINTER or "
1169 "ALLOCATABLE in procedure %qs with BIND(C) is not yet"
1170 " supported", sym
->name
, &(sym
->declared_at
),
1171 sym
->ns
->proc_name
->name
);
1175 if (sym
->attr
.optional
== 1 && sym
->attr
.value
)
1177 gfc_error ("Variable %qs at %L cannot have both the OPTIONAL "
1178 "and the VALUE attribute because procedure %qs "
1179 "is BIND(C)", sym
->name
, &(sym
->declared_at
),
1180 sym
->ns
->proc_name
->name
);
1183 else if (sym
->attr
.optional
== 1
1184 && !gfc_notify_std (GFC_STD_F2008_TS
, "Variable %qs "
1185 "at %L with OPTIONAL attribute in "
1186 "procedure %qs which is BIND(C)",
1187 sym
->name
, &(sym
->declared_at
),
1188 sym
->ns
->proc_name
->name
))
1191 /* Make sure that if it has the dimension attribute, that it is
1192 either assumed size or explicit shape. Deferred shape is already
1193 covered by the pointer/allocatable attribute. */
1194 if (sym
->as
!= NULL
&& sym
->as
->type
== AS_ASSUMED_SHAPE
1195 && !gfc_notify_std (GFC_STD_F2008_TS
, "Assumed-shape array %qs "
1196 "at %L as dummy argument to the BIND(C) "
1197 "procedure %qs at %L", sym
->name
,
1198 &(sym
->declared_at
),
1199 sym
->ns
->proc_name
->name
,
1200 &(sym
->ns
->proc_name
->declared_at
)))
1210 /* Function called by variable_decl() that adds a name to the symbol table. */
1213 build_sym (const char *name
, gfc_charlen
*cl
, bool cl_deferred
,
1214 gfc_array_spec
**as
, locus
*var_locus
)
1216 symbol_attribute attr
;
1220 if (gfc_get_symbol (name
, NULL
, &sym
))
1223 /* Check if the name has already been defined as a type. The
1224 first letter of the symtree will be in upper case then. Of
1225 course, this is only necessary if the upper case letter is
1226 actually different. */
1228 upper
= TOUPPER(name
[0]);
1229 if (upper
!= name
[0])
1231 char u_name
[GFC_MAX_SYMBOL_LEN
+ 1];
1235 nlen
= strlen(name
);
1236 gcc_assert (nlen
<= GFC_MAX_SYMBOL_LEN
);
1237 strncpy (u_name
, name
, nlen
+ 1);
1240 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, u_name
);
1244 gfc_error ("Symbol %qs at %C also declared as a type at %L", name
,
1245 &st
->n
.sym
->declared_at
);
1250 /* Start updating the symbol table. Add basic type attribute if present. */
1251 if (current_ts
.type
!= BT_UNKNOWN
1252 && (sym
->attr
.implicit_type
== 0
1253 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
1254 && !gfc_add_type (sym
, ¤t_ts
, var_locus
))
1257 if (sym
->ts
.type
== BT_CHARACTER
)
1260 sym
->ts
.deferred
= cl_deferred
;
1263 /* Add dimension attribute if present. */
1264 if (!gfc_set_array_spec (sym
, *as
, var_locus
))
1268 /* Add attribute to symbol. The copy is so that we can reset the
1269 dimension attribute. */
1270 attr
= current_attr
;
1272 attr
.codimension
= 0;
1274 if (!gfc_copy_attr (&sym
->attr
, &attr
, var_locus
))
1277 /* Finish any work that may need to be done for the binding label,
1278 if it's a bind(c). The bind(c) attr is found before the symbol
1279 is made, and before the symbol name (for data decls), so the
1280 current_ts is holding the binding label, or nothing if the
1281 name= attr wasn't given. Therefore, test here if we're dealing
1282 with a bind(c) and make sure the binding label is set correctly. */
1283 if (sym
->attr
.is_bind_c
== 1)
1285 if (!sym
->binding_label
)
1287 /* Set the binding label and verify that if a NAME= was specified
1288 then only one identifier was in the entity-decl-list. */
1289 if (!set_binding_label (&sym
->binding_label
, sym
->name
,
1290 num_idents_on_line
))
1295 /* See if we know we're in a common block, and if it's a bind(c)
1296 common then we need to make sure we're an interoperable type. */
1297 if (sym
->attr
.in_common
== 1)
1299 /* Test the common block object. */
1300 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1301 && sym
->ts
.is_c_interop
!= 1)
1303 gfc_error_now ("Variable %qs in common block %qs at %C "
1304 "must be declared with a C interoperable "
1305 "kind since common block %qs is BIND(C)",
1306 sym
->name
, sym
->common_block
->name
,
1307 sym
->common_block
->name
);
1312 sym
->attr
.implied_index
= 0;
1314 if (sym
->ts
.type
== BT_CLASS
)
1315 return gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
);
1321 /* Set character constant to the given length. The constant will be padded or
1322 truncated. If we're inside an array constructor without a typespec, we
1323 additionally check that all elements have the same length; check_len -1
1324 means no checking. */
1327 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, int check_len
)
1332 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1334 if (expr
->ts
.type
!= BT_CHARACTER
)
1337 slen
= expr
->value
.character
.length
;
1340 s
= gfc_get_wide_string (len
+ 1);
1341 memcpy (s
, expr
->value
.character
.string
,
1342 MIN (len
, slen
) * sizeof (gfc_char_t
));
1344 gfc_wide_memset (&s
[slen
], ' ', len
- slen
);
1346 if (warn_character_truncation
&& slen
> len
)
1347 gfc_warning_now (OPT_Wcharacter_truncation
,
1348 "CHARACTER expression at %L is being truncated "
1349 "(%d/%d)", &expr
->where
, slen
, len
);
1351 /* Apply the standard by 'hand' otherwise it gets cleared for
1353 if (check_len
!= -1 && slen
!= check_len
1354 && !(gfc_option
.allow_std
& GFC_STD_GNU
))
1355 gfc_error_now ("The CHARACTER elements of the array constructor "
1356 "at %L must have the same length (%d/%d)",
1357 &expr
->where
, slen
, check_len
);
1360 free (expr
->value
.character
.string
);
1361 expr
->value
.character
.string
= s
;
1362 expr
->value
.character
.length
= len
;
1367 /* Function to create and update the enumerator history
1368 using the information passed as arguments.
1369 Pointer "max_enum" is also updated, to point to
1370 enum history node containing largest initializer.
1372 SYM points to the symbol node of enumerator.
1373 INIT points to its enumerator value. */
1376 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1378 enumerator_history
*new_enum_history
;
1379 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1381 new_enum_history
= XCNEW (enumerator_history
);
1383 new_enum_history
->sym
= sym
;
1384 new_enum_history
->initializer
= init
;
1385 new_enum_history
->next
= NULL
;
1387 if (enum_history
== NULL
)
1389 enum_history
= new_enum_history
;
1390 max_enum
= enum_history
;
1394 new_enum_history
->next
= enum_history
;
1395 enum_history
= new_enum_history
;
1397 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1398 new_enum_history
->initializer
->value
.integer
) < 0)
1399 max_enum
= new_enum_history
;
1404 /* Function to free enum kind history. */
1407 gfc_free_enum_history (void)
1409 enumerator_history
*current
= enum_history
;
1410 enumerator_history
*next
;
1412 while (current
!= NULL
)
1414 next
= current
->next
;
1419 enum_history
= NULL
;
1423 /* Function called by variable_decl() that adds an initialization
1424 expression to a symbol. */
1427 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1429 symbol_attribute attr
;
1434 if (find_special (name
, &sym
, false))
1439 /* If this symbol is confirming an implicit parameter type,
1440 then an initialization expression is not allowed. */
1441 if (attr
.flavor
== FL_PARAMETER
1442 && sym
->value
!= NULL
1445 gfc_error ("Initializer not allowed for PARAMETER %qs at %C",
1452 /* An initializer is required for PARAMETER declarations. */
1453 if (attr
.flavor
== FL_PARAMETER
)
1455 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1461 /* If a variable appears in a DATA block, it cannot have an
1465 gfc_error ("Variable %qs at %C with an initializer already "
1466 "appears in a DATA statement", sym
->name
);
1470 /* Check if the assignment can happen. This has to be put off
1471 until later for derived type variables and procedure pointers. */
1472 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1473 && sym
->ts
.type
!= BT_CLASS
&& init
->ts
.type
!= BT_CLASS
1474 && !sym
->attr
.proc_pointer
1475 && !gfc_check_assign_symbol (sym
, NULL
, init
))
1478 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.u
.cl
1479 && init
->ts
.type
== BT_CHARACTER
)
1481 /* Update symbol character length according initializer. */
1482 if (!gfc_check_assign_symbol (sym
, NULL
, init
))
1485 if (sym
->ts
.u
.cl
->length
== NULL
)
1488 /* If there are multiple CHARACTER variables declared on the
1489 same line, we don't want them to share the same length. */
1490 sym
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1492 if (sym
->attr
.flavor
== FL_PARAMETER
)
1494 if (init
->expr_type
== EXPR_CONSTANT
)
1496 clen
= init
->value
.character
.length
;
1497 sym
->ts
.u
.cl
->length
1498 = gfc_get_int_expr (gfc_default_integer_kind
,
1501 else if (init
->expr_type
== EXPR_ARRAY
)
1504 clen
= mpz_get_si (init
->ts
.u
.cl
->length
->value
.integer
);
1505 else if (init
->value
.constructor
)
1508 c
= gfc_constructor_first (init
->value
.constructor
);
1509 clen
= c
->expr
->value
.character
.length
;
1513 sym
->ts
.u
.cl
->length
1514 = gfc_get_int_expr (gfc_default_integer_kind
,
1517 else if (init
->ts
.u
.cl
&& init
->ts
.u
.cl
->length
)
1518 sym
->ts
.u
.cl
->length
=
1519 gfc_copy_expr (sym
->value
->ts
.u
.cl
->length
);
1522 /* Update initializer character length according symbol. */
1523 else if (sym
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1527 if (!gfc_specification_expr (sym
->ts
.u
.cl
->length
))
1530 len
= mpz_get_si (sym
->ts
.u
.cl
->length
->value
.integer
);
1532 if (init
->expr_type
== EXPR_CONSTANT
)
1533 gfc_set_constant_character_len (len
, init
, -1);
1534 else if (init
->expr_type
== EXPR_ARRAY
)
1538 /* Build a new charlen to prevent simplification from
1539 deleting the length before it is resolved. */
1540 init
->ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
1541 init
->ts
.u
.cl
->length
= gfc_copy_expr (sym
->ts
.u
.cl
->length
);
1543 for (c
= gfc_constructor_first (init
->value
.constructor
);
1544 c
; c
= gfc_constructor_next (c
))
1545 gfc_set_constant_character_len (len
, c
->expr
, -1);
1550 /* If sym is implied-shape, set its upper bounds from init. */
1551 if (sym
->attr
.flavor
== FL_PARAMETER
&& sym
->attr
.dimension
1552 && sym
->as
->type
== AS_IMPLIED_SHAPE
)
1556 if (init
->rank
== 0)
1558 gfc_error ("Can't initialize implied-shape array at %L"
1559 " with scalar", &sym
->declared_at
);
1563 /* Shape should be present, we get an initialization expression. */
1564 gcc_assert (init
->shape
);
1566 for (dim
= 0; dim
< sym
->as
->rank
; ++dim
)
1569 gfc_expr
*e
, *lower
;
1571 lower
= sym
->as
->lower
[dim
];
1573 /* If the lower bound is an array element from another
1574 parameterized array, then it is marked with EXPR_VARIABLE and
1575 is an initialization expression. Try to reduce it. */
1576 if (lower
->expr_type
== EXPR_VARIABLE
)
1577 gfc_reduce_init_expr (lower
);
1579 if (lower
->expr_type
== EXPR_CONSTANT
)
1581 /* All dimensions must be without upper bound. */
1582 gcc_assert (!sym
->as
->upper
[dim
]);
1585 e
= gfc_get_constant_expr (BT_INTEGER
, k
, &sym
->declared_at
);
1586 mpz_add (e
->value
.integer
, lower
->value
.integer
,
1588 mpz_sub_ui (e
->value
.integer
, e
->value
.integer
, 1);
1589 sym
->as
->upper
[dim
] = e
;
1593 gfc_error ("Non-constant lower bound in implied-shape"
1594 " declaration at %L", &lower
->where
);
1599 sym
->as
->type
= AS_EXPLICIT
;
1602 /* Need to check if the expression we initialized this
1603 to was one of the iso_c_binding named constants. If so,
1604 and we're a parameter (constant), let it be iso_c.
1606 integer(c_int), parameter :: my_int = c_int
1607 integer(my_int) :: my_int_2
1608 If we mark my_int as iso_c (since we can see it's value
1609 is equal to one of the named constants), then my_int_2
1610 will be considered C interoperable. */
1611 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1613 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1614 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1615 /* attr bits needed for module files. */
1616 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1617 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1618 if (init
->ts
.is_iso_c
)
1619 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1622 /* Add initializer. Make sure we keep the ranks sane. */
1623 if (sym
->attr
.dimension
&& init
->rank
== 0)
1628 if (sym
->attr
.flavor
== FL_PARAMETER
1629 && init
->expr_type
== EXPR_CONSTANT
1630 && spec_size (sym
->as
, &size
)
1631 && mpz_cmp_si (size
, 0) > 0)
1633 array
= gfc_get_array_expr (init
->ts
.type
, init
->ts
.kind
,
1635 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1636 gfc_constructor_append_expr (&array
->value
.constructor
,
1639 : gfc_copy_expr (init
),
1642 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1643 for (n
= 0; n
< sym
->as
->rank
; n
++)
1644 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1649 init
->rank
= sym
->as
->rank
;
1653 if (sym
->attr
.save
== SAVE_NONE
)
1654 sym
->attr
.save
= SAVE_IMPLICIT
;
1662 /* Function called by variable_decl() that adds a name to a structure
1666 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1667 gfc_array_spec
**as
)
1672 /* F03:C438/C439. If the current symbol is of the same derived type that we're
1673 constructing, it must have the pointer attribute. */
1674 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
1675 && current_ts
.u
.derived
== gfc_current_block ()
1676 && current_attr
.pointer
== 0)
1678 gfc_error ("Component at %C must have the POINTER attribute");
1682 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1684 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1686 gfc_error ("Array component of structure at %C must have explicit "
1687 "or deferred shape");
1692 if (!gfc_add_component (gfc_current_block(), name
, &c
))
1696 if (c
->ts
.type
== BT_CHARACTER
)
1698 c
->attr
= current_attr
;
1700 c
->initializer
= *init
;
1707 c
->attr
.codimension
= 1;
1709 c
->attr
.dimension
= 1;
1713 /* Should this ever get more complicated, combine with similar section
1714 in add_init_expr_to_sym into a separate function. */
1715 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.pointer
&& c
->initializer
1717 && c
->ts
.u
.cl
->length
&& c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1721 gcc_assert (c
->ts
.u
.cl
&& c
->ts
.u
.cl
->length
);
1722 gcc_assert (c
->ts
.u
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1723 gcc_assert (c
->ts
.u
.cl
->length
->ts
.type
== BT_INTEGER
);
1725 len
= mpz_get_si (c
->ts
.u
.cl
->length
->value
.integer
);
1727 if (c
->initializer
->expr_type
== EXPR_CONSTANT
)
1728 gfc_set_constant_character_len (len
, c
->initializer
, -1);
1729 else if (mpz_cmp (c
->ts
.u
.cl
->length
->value
.integer
,
1730 c
->initializer
->ts
.u
.cl
->length
->value
.integer
))
1732 gfc_constructor
*ctor
;
1733 ctor
= gfc_constructor_first (c
->initializer
->value
.constructor
);
1738 bool has_ts
= (c
->initializer
->ts
.u
.cl
1739 && c
->initializer
->ts
.u
.cl
->length_from_typespec
);
1741 /* Remember the length of the first element for checking
1742 that all elements *in the constructor* have the same
1743 length. This need not be the length of the LHS! */
1744 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
1745 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
1746 first_len
= ctor
->expr
->value
.character
.length
;
1748 for ( ; ctor
; ctor
= gfc_constructor_next (ctor
))
1749 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
1751 gfc_set_constant_character_len (len
, ctor
->expr
,
1752 has_ts
? -1 : first_len
);
1753 ctor
->expr
->ts
.u
.cl
->length
= gfc_copy_expr (c
->ts
.u
.cl
->length
);
1759 /* Check array components. */
1760 if (!c
->attr
.dimension
)
1763 if (c
->attr
.pointer
)
1765 if (c
->as
->type
!= AS_DEFERRED
)
1767 gfc_error ("Pointer array component of structure at %C must have a "
1772 else if (c
->attr
.allocatable
)
1774 if (c
->as
->type
!= AS_DEFERRED
)
1776 gfc_error ("Allocatable component of structure at %C must have a "
1783 if (c
->as
->type
!= AS_EXPLICIT
)
1785 gfc_error ("Array component of structure at %C must have an "
1792 if (c
->ts
.type
== BT_CLASS
)
1794 bool t2
= gfc_build_class_symbol (&c
->ts
, &c
->attr
, &c
->as
);
1804 /* Match a 'NULL()', and possibly take care of some side effects. */
1807 gfc_match_null (gfc_expr
**result
)
1810 match m
, m2
= MATCH_NO
;
1812 if ((m
= gfc_match (" null ( )")) == MATCH_ERROR
)
1818 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1820 if ((m2
= gfc_match (" null (")) != MATCH_YES
)
1823 old_loc
= gfc_current_locus
;
1824 if ((m2
= gfc_match (" %n ) ", name
)) == MATCH_ERROR
)
1827 && ((m2
= gfc_match (" mold = %n )", name
)) == MATCH_ERROR
))
1831 gfc_current_locus
= old_loc
;
1836 /* The NULL symbol now has to be/become an intrinsic function. */
1837 if (gfc_get_symbol ("null", NULL
, &sym
))
1839 gfc_error ("NULL() initialization at %C is ambiguous");
1843 gfc_intrinsic_symbol (sym
);
1845 if (sym
->attr
.proc
!= PROC_INTRINSIC
1846 && !(sym
->attr
.use_assoc
&& sym
->attr
.intrinsic
)
1847 && (!gfc_add_procedure(&sym
->attr
, PROC_INTRINSIC
, sym
->name
, NULL
)
1848 || !gfc_add_function (&sym
->attr
, sym
->name
, NULL
)))
1851 *result
= gfc_get_null_expr (&gfc_current_locus
);
1853 /* Invalid per F2008, C512. */
1854 if (m2
== MATCH_YES
)
1856 gfc_error ("NULL() initialization at %C may not have MOLD");
1864 /* Match the initialization expr for a data pointer or procedure pointer. */
1867 match_pointer_init (gfc_expr
**init
, int procptr
)
1871 if (gfc_pure (NULL
) && gfc_state_stack
->state
!= COMP_DERIVED
)
1873 gfc_error ("Initialization of pointer at %C is not allowed in "
1874 "a PURE procedure");
1877 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
1879 /* Match NULL() initialization. */
1880 m
= gfc_match_null (init
);
1884 /* Match non-NULL initialization. */
1885 gfc_matching_ptr_assignment
= !procptr
;
1886 gfc_matching_procptr_assignment
= procptr
;
1887 m
= gfc_match_rvalue (init
);
1888 gfc_matching_ptr_assignment
= 0;
1889 gfc_matching_procptr_assignment
= 0;
1890 if (m
== MATCH_ERROR
)
1892 else if (m
== MATCH_NO
)
1894 gfc_error ("Error in pointer initialization at %C");
1898 if (!procptr
&& !gfc_resolve_expr (*init
))
1901 if (!gfc_notify_std (GFC_STD_F2008
, "non-NULL pointer "
1902 "initialization at %C"))
1910 check_function_name (char *name
)
1912 /* In functions that have a RESULT variable defined, the function name always
1913 refers to function calls. Therefore, the name is not allowed to appear in
1914 specification statements. When checking this, be careful about
1915 'hidden' procedure pointer results ('ppr@'). */
1917 if (gfc_current_state () == COMP_FUNCTION
)
1919 gfc_symbol
*block
= gfc_current_block ();
1920 if (block
&& block
->result
&& block
->result
!= block
1921 && strcmp (block
->result
->name
, "ppr@") != 0
1922 && strcmp (block
->name
, name
) == 0)
1924 gfc_error ("Function name %qs not allowed at %C", name
);
1933 /* Match a variable name with an optional initializer. When this
1934 subroutine is called, a variable is expected to be parsed next.
1935 Depending on what is happening at the moment, updates either the
1936 symbol table or the current interface. */
1939 variable_decl (int elem
)
1941 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1942 gfc_expr
*initializer
, *char_len
;
1944 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1956 /* When we get here, we've just matched a list of attributes and
1957 maybe a type and a double colon. The next thing we expect to see
1958 is the name of the symbol. */
1959 m
= gfc_match_name (name
);
1963 var_locus
= gfc_current_locus
;
1965 /* Now we could see the optional array spec. or character length. */
1966 m
= gfc_match_array_spec (&as
, true, true);
1967 if (m
== MATCH_ERROR
)
1971 as
= gfc_copy_array_spec (current_as
);
1973 && !merge_array_spec (current_as
, as
, true))
1979 if (flag_cray_pointer
)
1980 cp_as
= gfc_copy_array_spec (as
);
1982 /* At this point, we know for sure if the symbol is PARAMETER and can thus
1983 determine (and check) whether it can be implied-shape. If it
1984 was parsed as assumed-size, change it because PARAMETERs can not
1988 if (as
->type
== AS_IMPLIED_SHAPE
&& current_attr
.flavor
!= FL_PARAMETER
)
1991 gfc_error ("Non-PARAMETER symbol %qs at %L can't be implied-shape",
1996 if (as
->type
== AS_ASSUMED_SIZE
&& as
->rank
== 1
1997 && current_attr
.flavor
== FL_PARAMETER
)
1998 as
->type
= AS_IMPLIED_SHAPE
;
2000 if (as
->type
== AS_IMPLIED_SHAPE
2001 && !gfc_notify_std (GFC_STD_F2008
, "Implied-shape array at %L",
2011 cl_deferred
= false;
2013 if (current_ts
.type
== BT_CHARACTER
)
2015 switch (match_char_length (&char_len
, &cl_deferred
, false))
2018 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2020 cl
->length
= char_len
;
2023 /* Non-constant lengths need to be copied after the first
2024 element. Also copy assumed lengths. */
2027 && (current_ts
.u
.cl
->length
== NULL
2028 || current_ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
2030 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2031 cl
->length
= gfc_copy_expr (current_ts
.u
.cl
->length
);
2034 cl
= current_ts
.u
.cl
;
2036 cl_deferred
= current_ts
.deferred
;
2045 /* The dummy arguments and result of the abreviated form of MODULE
2046 PROCEDUREs, used in SUBMODULES should not be redefined. */
2047 if (gfc_current_ns
->proc_name
2048 && gfc_current_ns
->proc_name
->abr_modproc_decl
)
2050 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
2051 if (sym
!= NULL
&& (sym
->attr
.dummy
|| sym
->attr
.result
))
2054 gfc_error ("%qs at %C is a redefinition of the declaration "
2055 "in the corresponding interface for MODULE "
2056 "PROCEDURE %qs", sym
->name
,
2057 gfc_current_ns
->proc_name
->name
);
2062 /* If this symbol has already shown up in a Cray Pointer declaration,
2063 and this is not a component declaration,
2064 then we want to set the type & bail out. */
2065 if (flag_cray_pointer
&& gfc_current_state () != COMP_DERIVED
)
2067 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
2068 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
2070 sym
->ts
.type
= current_ts
.type
;
2071 sym
->ts
.kind
= current_ts
.kind
;
2073 sym
->ts
.u
.derived
= current_ts
.u
.derived
;
2074 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
2075 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
2078 /* Check to see if we have an array specification. */
2081 if (sym
->as
!= NULL
)
2083 gfc_error ("Duplicate array spec for Cray pointee at %C");
2084 gfc_free_array_spec (cp_as
);
2090 if (!gfc_set_array_spec (sym
, cp_as
, &var_locus
))
2091 gfc_internal_error ("Couldn't set pointee array spec.");
2093 /* Fix the array spec. */
2094 m
= gfc_mod_pointee_as (sym
->as
);
2095 if (m
== MATCH_ERROR
)
2103 gfc_free_array_spec (cp_as
);
2107 /* Procedure pointer as function result. */
2108 if (gfc_current_state () == COMP_FUNCTION
2109 && strcmp ("ppr@", gfc_current_block ()->name
) == 0
2110 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) == 0)
2111 strcpy (name
, "ppr@");
2113 if (gfc_current_state () == COMP_FUNCTION
2114 && strcmp (name
, gfc_current_block ()->name
) == 0
2115 && gfc_current_block ()->result
2116 && strcmp ("ppr@", gfc_current_block ()->result
->name
) == 0)
2117 strcpy (name
, "ppr@");
2119 /* OK, we've successfully matched the declaration. Now put the
2120 symbol in the current namespace, because it might be used in the
2121 optional initialization expression for this symbol, e.g. this is
2124 integer, parameter :: i = huge(i)
2126 This is only true for parameters or variables of a basic type.
2127 For components of derived types, it is not true, so we don't
2128 create a symbol for those yet. If we fail to create the symbol,
2130 if (gfc_current_state () != COMP_DERIVED
2131 && !build_sym (name
, cl
, cl_deferred
, &as
, &var_locus
))
2137 if (!check_function_name (name
))
2143 /* We allow old-style initializations of the form
2144 integer i /2/, j(4) /3*3, 1/
2145 (if no colon has been seen). These are different from data
2146 statements in that initializers are only allowed to apply to the
2147 variable immediately preceding, i.e.
2149 is not allowed. Therefore we have to do some work manually, that
2150 could otherwise be left to the matchers for DATA statements. */
2152 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
2154 if (!gfc_notify_std (GFC_STD_GNU
, "Old-style "
2155 "initialization at %C"))
2157 else if (gfc_current_state () == COMP_DERIVED
)
2159 gfc_error ("Invalid old style initialization for derived type "
2165 return match_old_style_init (name
);
2168 /* The double colon must be present in order to have initializers.
2169 Otherwise the statement is ambiguous with an assignment statement. */
2172 if (gfc_match (" =>") == MATCH_YES
)
2174 if (!current_attr
.pointer
)
2176 gfc_error ("Initialization at %C isn't for a pointer variable");
2181 m
= match_pointer_init (&initializer
, 0);
2185 else if (gfc_match_char ('=') == MATCH_YES
)
2187 if (current_attr
.pointer
)
2189 gfc_error ("Pointer initialization at %C requires %<=>%>, "
2195 m
= gfc_match_init_expr (&initializer
);
2198 gfc_error ("Expected an initialization expression at %C");
2202 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
)
2203 && gfc_state_stack
->state
!= COMP_DERIVED
)
2205 gfc_error ("Initialization of variable at %C is not allowed in "
2206 "a PURE procedure");
2210 if (current_attr
.flavor
!= FL_PARAMETER
2211 && gfc_state_stack
->state
!= COMP_DERIVED
)
2212 gfc_unset_implicit_pure (gfc_current_ns
->proc_name
);
2219 if (initializer
!= NULL
&& current_attr
.allocatable
2220 && gfc_current_state () == COMP_DERIVED
)
2222 gfc_error ("Initialization of allocatable component at %C is not "
2228 /* Add the initializer. Note that it is fine if initializer is
2229 NULL here, because we sometimes also need to check if a
2230 declaration *must* have an initialization expression. */
2231 if (gfc_current_state () != COMP_DERIVED
)
2232 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
2235 if (current_ts
.type
== BT_DERIVED
2236 && !current_attr
.pointer
&& !initializer
)
2237 initializer
= gfc_default_initializer (¤t_ts
);
2238 t
= build_struct (name
, cl
, &initializer
, &as
);
2241 m
= (t
) ? MATCH_YES
: MATCH_ERROR
;
2244 /* Free stuff up and return. */
2245 gfc_free_expr (initializer
);
2246 gfc_free_array_spec (as
);
2252 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
2253 This assumes that the byte size is equal to the kind number for
2254 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
2257 gfc_match_old_kind_spec (gfc_typespec
*ts
)
2262 if (gfc_match_char ('*') != MATCH_YES
)
2265 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
2269 original_kind
= ts
->kind
;
2271 /* Massage the kind numbers for complex types. */
2272 if (ts
->type
== BT_COMPLEX
)
2276 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2277 gfc_basic_typename (ts
->type
), original_kind
);
2284 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && flag_integer4_kind
== 8)
2287 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2291 if (flag_real4_kind
== 8)
2293 if (flag_real4_kind
== 10)
2295 if (flag_real4_kind
== 16)
2301 if (flag_real8_kind
== 4)
2303 if (flag_real8_kind
== 10)
2305 if (flag_real8_kind
== 16)
2310 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2312 gfc_error ("Old-style type declaration %s*%d not supported at %C",
2313 gfc_basic_typename (ts
->type
), original_kind
);
2317 if (!gfc_notify_std (GFC_STD_GNU
,
2318 "Nonstandard type declaration %s*%d at %C",
2319 gfc_basic_typename(ts
->type
), original_kind
))
2326 /* Match a kind specification. Since kinds are generally optional, we
2327 usually return MATCH_NO if something goes wrong. If a "kind="
2328 string is found, then we know we have an error. */
2331 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
2343 where
= loc
= gfc_current_locus
;
2348 if (gfc_match_char ('(') == MATCH_NO
)
2351 /* Also gobbles optional text. */
2352 if (gfc_match (" kind = ") == MATCH_YES
)
2355 loc
= gfc_current_locus
;
2358 n
= gfc_match_init_expr (&e
);
2362 if (gfc_matching_function
)
2364 /* The function kind expression might include use associated or
2365 imported parameters and try again after the specification
2367 if (gfc_match_char (')') != MATCH_YES
)
2369 gfc_error ("Missing right parenthesis at %C");
2375 gfc_undo_symbols ();
2380 /* ....or else, the match is real. */
2382 gfc_error ("Expected initialization expression at %C");
2390 gfc_error ("Expected scalar initialization expression at %C");
2395 msg
= gfc_extract_int (e
, &ts
->kind
);
2404 /* Before throwing away the expression, let's see if we had a
2405 C interoperable kind (and store the fact). */
2406 if (e
->ts
.is_c_interop
== 1)
2408 /* Mark this as C interoperable if being declared with one
2409 of the named constants from iso_c_binding. */
2410 ts
->is_c_interop
= e
->ts
.is_iso_c
;
2411 ts
->f90_type
= e
->ts
.f90_type
;
2417 /* Ignore errors to this point, if we've gotten here. This means
2418 we ignore the m=MATCH_ERROR from above. */
2419 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
2421 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
2422 gfc_basic_typename (ts
->type
));
2423 gfc_current_locus
= where
;
2427 /* Warn if, e.g., c_int is used for a REAL variable, but not
2428 if, e.g., c_double is used for COMPLEX as the standard
2429 explicitly says that the kind type parameter for complex and real
2430 variable is the same, i.e. c_float == c_float_complex. */
2431 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2432 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2433 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2434 gfc_warning_now (0, "C kind type parameter is for type %s but type at %L "
2435 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2436 gfc_basic_typename (ts
->type
));
2438 gfc_gobble_whitespace ();
2439 if ((c
= gfc_next_ascii_char ()) != ')'
2440 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2442 if (ts
->type
== BT_CHARACTER
)
2443 gfc_error ("Missing right parenthesis or comma at %C");
2445 gfc_error ("Missing right parenthesis at %C");
2449 /* All tests passed. */
2452 if(m
== MATCH_ERROR
)
2453 gfc_current_locus
= where
;
2455 if (ts
->type
== BT_INTEGER
&& ts
->kind
== 4 && flag_integer4_kind
== 8)
2458 if (ts
->type
== BT_REAL
|| ts
->type
== BT_COMPLEX
)
2462 if (flag_real4_kind
== 8)
2464 if (flag_real4_kind
== 10)
2466 if (flag_real4_kind
== 16)
2472 if (flag_real8_kind
== 4)
2474 if (flag_real8_kind
== 10)
2476 if (flag_real8_kind
== 16)
2481 /* Return what we know from the test(s). */
2486 gfc_current_locus
= where
;
2492 match_char_kind (int * kind
, int * is_iso_c
)
2501 where
= gfc_current_locus
;
2503 n
= gfc_match_init_expr (&e
);
2505 if (n
!= MATCH_YES
&& gfc_matching_function
)
2507 /* The expression might include use-associated or imported
2508 parameters and try again after the specification
2511 gfc_undo_symbols ();
2516 gfc_error ("Expected initialization expression at %C");
2522 gfc_error ("Expected scalar initialization expression at %C");
2527 msg
= gfc_extract_int (e
, kind
);
2528 *is_iso_c
= e
->ts
.is_iso_c
;
2538 /* Ignore errors to this point, if we've gotten here. This means
2539 we ignore the m=MATCH_ERROR from above. */
2540 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
2542 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2546 /* All tests passed. */
2549 if (m
== MATCH_ERROR
)
2550 gfc_current_locus
= where
;
2552 /* Return what we know from the test(s). */
2557 gfc_current_locus
= where
;
2562 /* Match the various kind/length specifications in a CHARACTER
2563 declaration. We don't return MATCH_NO. */
2566 gfc_match_char_spec (gfc_typespec
*ts
)
2568 int kind
, seen_length
, is_iso_c
;
2580 /* Try the old-style specification first. */
2581 old_char_selector
= 0;
2583 m
= match_char_length (&len
, &deferred
, true);
2587 old_char_selector
= 1;
2592 m
= gfc_match_char ('(');
2595 m
= MATCH_YES
; /* Character without length is a single char. */
2599 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2600 if (gfc_match (" kind =") == MATCH_YES
)
2602 m
= match_char_kind (&kind
, &is_iso_c
);
2604 if (m
== MATCH_ERROR
)
2609 if (gfc_match (" , len =") == MATCH_NO
)
2612 m
= char_len_param_value (&len
, &deferred
);
2615 if (m
== MATCH_ERROR
)
2622 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2623 if (gfc_match (" len =") == MATCH_YES
)
2625 m
= char_len_param_value (&len
, &deferred
);
2628 if (m
== MATCH_ERROR
)
2632 if (gfc_match_char (')') == MATCH_YES
)
2635 if (gfc_match (" , kind =") != MATCH_YES
)
2638 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2644 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2645 m
= char_len_param_value (&len
, &deferred
);
2648 if (m
== MATCH_ERROR
)
2652 m
= gfc_match_char (')');
2656 if (gfc_match_char (',') != MATCH_YES
)
2659 gfc_match (" kind ="); /* Gobble optional text. */
2661 m
= match_char_kind (&kind
, &is_iso_c
);
2662 if (m
== MATCH_ERROR
)
2668 /* Require a right-paren at this point. */
2669 m
= gfc_match_char (')');
2674 gfc_error ("Syntax error in CHARACTER declaration at %C");
2676 gfc_free_expr (len
);
2680 /* Deal with character functions after USE and IMPORT statements. */
2681 if (gfc_matching_function
)
2683 gfc_free_expr (len
);
2684 gfc_undo_symbols ();
2690 gfc_free_expr (len
);
2694 /* Do some final massaging of the length values. */
2695 cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
2697 if (seen_length
== 0)
2698 cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
, NULL
, 1);
2703 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2704 ts
->deferred
= deferred
;
2706 /* We have to know if it was a C interoperable kind so we can
2707 do accurate type checking of bind(c) procs, etc. */
2709 /* Mark this as C interoperable if being declared with one
2710 of the named constants from iso_c_binding. */
2711 ts
->is_c_interop
= is_iso_c
;
2712 else if (len
!= NULL
)
2713 /* Here, we might have parsed something such as: character(c_char)
2714 In this case, the parsing code above grabs the c_char when
2715 looking for the length (line 1690, roughly). it's the last
2716 testcase for parsing the kind params of a character variable.
2717 However, it's not actually the length. this seems like it
2719 To see if the user used a C interop kind, test the expr
2720 of the so called length, and see if it's C interoperable. */
2721 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2727 /* Matches a declaration-type-spec (F03:R502). If successful, sets the ts
2728 structure to the matched specification. This is necessary for FUNCTION and
2729 IMPLICIT statements.
2731 If implicit_flag is nonzero, then we don't check for the optional
2732 kind specification. Not doing so is needed for matching an IMPLICIT
2733 statement correctly. */
2736 gfc_match_decl_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2738 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2739 gfc_symbol
*sym
, *dt_sym
;
2742 bool seen_deferred_kind
, matched_type
;
2743 const char *dt_name
;
2745 /* A belt and braces check that the typespec is correctly being treated
2746 as a deferred characteristic association. */
2747 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2748 && (gfc_current_block ()->result
->ts
.kind
== -1)
2749 && (ts
->kind
== -1);
2751 if (seen_deferred_kind
)
2754 /* Clear the current binding label, in case one is given. */
2755 curr_binding_label
= NULL
;
2757 if (gfc_match (" byte") == MATCH_YES
)
2759 if (!gfc_notify_std (GFC_STD_GNU
, "BYTE type at %C"))
2762 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2764 gfc_error ("BYTE type used at %C "
2765 "is not available on the target machine");
2769 ts
->type
= BT_INTEGER
;
2775 m
= gfc_match (" type (");
2776 matched_type
= (m
== MATCH_YES
);
2779 gfc_gobble_whitespace ();
2780 if (gfc_peek_ascii_char () == '*')
2782 if ((m
= gfc_match ("*)")) != MATCH_YES
)
2784 if (gfc_current_state () == COMP_DERIVED
)
2786 gfc_error ("Assumed type at %C is not allowed for components");
2789 if (!gfc_notify_std (GFC_STD_F2008_TS
, "Assumed type "
2792 ts
->type
= BT_ASSUMED
;
2796 m
= gfc_match ("%n", name
);
2797 matched_type
= (m
== MATCH_YES
);
2800 if ((matched_type
&& strcmp ("integer", name
) == 0)
2801 || (!matched_type
&& gfc_match (" integer") == MATCH_YES
))
2803 ts
->type
= BT_INTEGER
;
2804 ts
->kind
= gfc_default_integer_kind
;
2808 if ((matched_type
&& strcmp ("character", name
) == 0)
2809 || (!matched_type
&& gfc_match (" character") == MATCH_YES
))
2812 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2813 "intrinsic-type-spec at %C"))
2816 ts
->type
= BT_CHARACTER
;
2817 if (implicit_flag
== 0)
2818 m
= gfc_match_char_spec (ts
);
2822 if (matched_type
&& m
== MATCH_YES
&& gfc_match_char (')') != MATCH_YES
)
2828 if ((matched_type
&& strcmp ("real", name
) == 0)
2829 || (!matched_type
&& gfc_match (" real") == MATCH_YES
))
2832 ts
->kind
= gfc_default_real_kind
;
2837 && (strcmp ("doubleprecision", name
) == 0
2838 || (strcmp ("double", name
) == 0
2839 && gfc_match (" precision") == MATCH_YES
)))
2840 || (!matched_type
&& gfc_match (" double precision") == MATCH_YES
))
2843 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2844 "intrinsic-type-spec at %C"))
2846 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2850 ts
->kind
= gfc_default_double_kind
;
2854 if ((matched_type
&& strcmp ("complex", name
) == 0)
2855 || (!matched_type
&& gfc_match (" complex") == MATCH_YES
))
2857 ts
->type
= BT_COMPLEX
;
2858 ts
->kind
= gfc_default_complex_kind
;
2863 && (strcmp ("doublecomplex", name
) == 0
2864 || (strcmp ("double", name
) == 0
2865 && gfc_match (" complex") == MATCH_YES
)))
2866 || (!matched_type
&& gfc_match (" double complex") == MATCH_YES
))
2868 if (!gfc_notify_std (GFC_STD_GNU
, "DOUBLE COMPLEX at %C"))
2872 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
2873 "intrinsic-type-spec at %C"))
2876 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
2879 ts
->type
= BT_COMPLEX
;
2880 ts
->kind
= gfc_default_double_kind
;
2884 if ((matched_type
&& strcmp ("logical", name
) == 0)
2885 || (!matched_type
&& gfc_match (" logical") == MATCH_YES
))
2887 ts
->type
= BT_LOGICAL
;
2888 ts
->kind
= gfc_default_logical_kind
;
2893 m
= gfc_match_char (')');
2896 ts
->type
= BT_DERIVED
;
2899 /* Match CLASS declarations. */
2900 m
= gfc_match (" class ( * )");
2901 if (m
== MATCH_ERROR
)
2903 else if (m
== MATCH_YES
)
2907 ts
->type
= BT_CLASS
;
2908 gfc_find_symbol ("STAR", gfc_current_ns
, 1, &upe
);
2911 upe
= gfc_new_symbol ("STAR", gfc_current_ns
);
2912 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2914 gfc_set_sym_referenced (upe
);
2916 upe
->ts
.type
= BT_VOID
;
2917 upe
->attr
.unlimited_polymorphic
= 1;
2918 /* This is essential to force the construction of
2919 unlimited polymorphic component class containers. */
2920 upe
->attr
.zero_comp
= 1;
2921 if (!gfc_add_flavor (&upe
->attr
, FL_DERIVED
, NULL
,
2922 &gfc_current_locus
))
2927 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, "STAR");
2929 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "STAR");
2933 ts
->u
.derived
= upe
;
2937 m
= gfc_match (" class ( %n )", name
);
2940 ts
->type
= BT_CLASS
;
2942 if (!gfc_notify_std (GFC_STD_F2003
, "CLASS statement at %C"))
2946 /* Defer association of the derived type until the end of the
2947 specification block. However, if the derived type can be
2948 found, add it to the typespec. */
2949 if (gfc_matching_function
)
2951 ts
->u
.derived
= NULL
;
2952 if (gfc_current_state () != COMP_INTERFACE
2953 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
2955 sym
= gfc_find_dt_in_generic (sym
);
2956 ts
->u
.derived
= sym
;
2961 /* Search for the name but allow the components to be defined later. If
2962 type = -1, this typespec has been seen in a function declaration but
2963 the type could not be accessed at that point. The actual derived type is
2964 stored in a symtree with the first letter of the name capitalized; the
2965 symtree with the all lower-case name contains the associated
2966 generic function. */
2967 dt_name
= gfc_get_string ("%c%s",
2968 (char) TOUPPER ((unsigned char) name
[0]),
2969 (const char*)&name
[1]);
2974 gfc_get_ha_symbol (name
, &sym
);
2975 if (sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 0, &dt_sym
))
2977 gfc_error ("Type name %qs at %C is ambiguous", name
);
2980 if (sym
->generic
&& !dt_sym
)
2981 dt_sym
= gfc_find_dt_in_generic (sym
);
2983 else if (ts
->kind
== -1)
2985 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
2986 || gfc_current_ns
->has_import_set
;
2987 gfc_find_symbol (name
, NULL
, iface
, &sym
);
2988 if (sym
&& sym
->generic
&& gfc_find_symbol (dt_name
, NULL
, 1, &dt_sym
))
2990 gfc_error ("Type name %qs at %C is ambiguous", name
);
2993 if (sym
&& sym
->generic
&& !dt_sym
)
2994 dt_sym
= gfc_find_dt_in_generic (sym
);
3001 if ((sym
->attr
.flavor
!= FL_UNKNOWN
3002 && !(sym
->attr
.flavor
== FL_PROCEDURE
&& sym
->attr
.generic
))
3003 || sym
->attr
.subroutine
)
3005 gfc_error ("Type name %qs at %C conflicts with previously declared "
3006 "entity at %L, which has the same name", name
,
3011 gfc_save_symbol_data (sym
);
3012 gfc_set_sym_referenced (sym
);
3013 if (!sym
->attr
.generic
3014 && !gfc_add_generic (&sym
->attr
, sym
->name
, NULL
))
3017 if (!sym
->attr
.function
3018 && !gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
3023 gfc_interface
*intr
, *head
;
3025 /* Use upper case to save the actual derived-type symbol. */
3026 gfc_get_symbol (dt_name
, NULL
, &dt_sym
);
3027 dt_sym
->name
= gfc_get_string (sym
->name
);
3028 head
= sym
->generic
;
3029 intr
= gfc_get_interface ();
3031 intr
->where
= gfc_current_locus
;
3033 sym
->generic
= intr
;
3034 sym
->attr
.if_source
= IFSRC_DECL
;
3037 gfc_save_symbol_data (dt_sym
);
3039 gfc_set_sym_referenced (dt_sym
);
3041 if (dt_sym
->attr
.flavor
!= FL_DERIVED
3042 && !gfc_add_flavor (&dt_sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
3045 ts
->u
.derived
= dt_sym
;
3051 && !gfc_notify_std (GFC_STD_F2008
, "TYPE with "
3052 "intrinsic-type-spec at %C"))
3055 /* For all types except double, derived and character, look for an
3056 optional kind specifier. MATCH_NO is actually OK at this point. */
3057 if (implicit_flag
== 1)
3059 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
3065 if (gfc_current_form
== FORM_FREE
)
3067 c
= gfc_peek_ascii_char ();
3068 if (!gfc_is_whitespace (c
) && c
!= '*' && c
!= '('
3069 && c
!= ':' && c
!= ',')
3071 if (matched_type
&& c
== ')')
3073 gfc_next_ascii_char ();
3080 m
= gfc_match_kind_spec (ts
, false);
3081 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
3083 m
= gfc_match_old_kind_spec (ts
);
3084 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) == -1)
3088 if (matched_type
&& gfc_match_char (')') != MATCH_YES
)
3091 /* Defer association of the KIND expression of function results
3092 until after USE and IMPORT statements. */
3093 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
3094 || gfc_matching_function
)
3098 m
= MATCH_YES
; /* No kind specifier found. */
3104 /* Match an IMPLICIT NONE statement. Actually, this statement is
3105 already matched in parse.c, or we would not end up here in the
3106 first place. So the only thing we need to check, is if there is
3107 trailing garbage. If not, the match is successful. */
3110 gfc_match_implicit_none (void)
3114 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3116 bool external
= false;
3117 locus cur_loc
= gfc_current_locus
;
3119 if (gfc_current_ns
->seen_implicit_none
3120 || gfc_current_ns
->has_implicit_none_export
)
3122 gfc_error ("Duplicate IMPLICIT NONE statement at %C");
3126 gfc_gobble_whitespace ();
3127 c
= gfc_peek_ascii_char ();
3130 (void) gfc_next_ascii_char ();
3131 if (!gfc_notify_std (GFC_STD_F2015
, "IMPORT NONE with spec list at %C"))
3134 gfc_gobble_whitespace ();
3135 if (gfc_peek_ascii_char () == ')')
3137 (void) gfc_next_ascii_char ();
3143 m
= gfc_match (" %n", name
);
3147 if (strcmp (name
, "type") == 0)
3149 else if (strcmp (name
, "external") == 0)
3154 gfc_gobble_whitespace ();
3155 c
= gfc_next_ascii_char ();
3166 if (gfc_match_eos () != MATCH_YES
)
3169 gfc_set_implicit_none (type
, external
, &cur_loc
);
3175 /* Match the letter range(s) of an IMPLICIT statement. */
3178 match_implicit_range (void)
3184 cur_loc
= gfc_current_locus
;
3186 gfc_gobble_whitespace ();
3187 c
= gfc_next_ascii_char ();
3190 gfc_error ("Missing character range in IMPLICIT at %C");
3197 gfc_gobble_whitespace ();
3198 c1
= gfc_next_ascii_char ();
3202 gfc_gobble_whitespace ();
3203 c
= gfc_next_ascii_char ();
3208 inner
= 0; /* Fall through. */
3215 gfc_gobble_whitespace ();
3216 c2
= gfc_next_ascii_char ();
3220 gfc_gobble_whitespace ();
3221 c
= gfc_next_ascii_char ();
3223 if ((c
!= ',') && (c
!= ')'))
3236 gfc_error ("Letters must be in alphabetic order in "
3237 "IMPLICIT statement at %C");
3241 /* See if we can add the newly matched range to the pending
3242 implicits from this IMPLICIT statement. We do not check for
3243 conflicts with whatever earlier IMPLICIT statements may have
3244 set. This is done when we've successfully finished matching
3246 if (!gfc_add_new_implicit_range (c1
, c2
))
3253 gfc_syntax_error (ST_IMPLICIT
);
3255 gfc_current_locus
= cur_loc
;
3260 /* Match an IMPLICIT statement, storing the types for
3261 gfc_set_implicit() if the statement is accepted by the parser.
3262 There is a strange looking, but legal syntactic construction
3263 possible. It looks like:
3265 IMPLICIT INTEGER (a-b) (c-d)
3267 This is legal if "a-b" is a constant expression that happens to
3268 equal one of the legal kinds for integers. The real problem
3269 happens with an implicit specification that looks like:
3271 IMPLICIT INTEGER (a-b)
3273 In this case, a typespec matcher that is "greedy" (as most of the
3274 matchers are) gobbles the character range as a kindspec, leaving
3275 nothing left. We therefore have to go a bit more slowly in the
3276 matching process by inhibiting the kindspec checking during
3277 typespec matching and checking for a kind later. */
3280 gfc_match_implicit (void)
3287 if (gfc_current_ns
->seen_implicit_none
)
3289 gfc_error ("IMPLICIT statement at %C following an IMPLICIT NONE (type) "
3296 /* We don't allow empty implicit statements. */
3297 if (gfc_match_eos () == MATCH_YES
)
3299 gfc_error ("Empty IMPLICIT statement at %C");
3305 /* First cleanup. */
3306 gfc_clear_new_implicit ();
3308 /* A basic type is mandatory here. */
3309 m
= gfc_match_decl_type_spec (&ts
, 1);
3310 if (m
== MATCH_ERROR
)
3315 cur_loc
= gfc_current_locus
;
3316 m
= match_implicit_range ();
3320 /* We may have <TYPE> (<RANGE>). */
3321 gfc_gobble_whitespace ();
3322 c
= gfc_peek_ascii_char ();
3323 if (c
== ',' || c
== '\n' || c
== ';' || c
== '!')
3325 /* Check for CHARACTER with no length parameter. */
3326 if (ts
.type
== BT_CHARACTER
&& !ts
.u
.cl
)
3328 ts
.kind
= gfc_default_character_kind
;
3329 ts
.u
.cl
= gfc_new_charlen (gfc_current_ns
, NULL
);
3330 ts
.u
.cl
->length
= gfc_get_int_expr (gfc_default_integer_kind
,
3334 /* Record the Successful match. */
3335 if (!gfc_merge_new_implicit (&ts
))
3338 c
= gfc_next_ascii_char ();
3339 else if (gfc_match_eos () == MATCH_ERROR
)
3344 gfc_current_locus
= cur_loc
;
3347 /* Discard the (incorrectly) matched range. */
3348 gfc_clear_new_implicit ();
3350 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
3351 if (ts
.type
== BT_CHARACTER
)
3352 m
= gfc_match_char_spec (&ts
);
3355 m
= gfc_match_kind_spec (&ts
, false);
3358 m
= gfc_match_old_kind_spec (&ts
);
3359 if (m
== MATCH_ERROR
)
3365 if (m
== MATCH_ERROR
)
3368 m
= match_implicit_range ();
3369 if (m
== MATCH_ERROR
)
3374 gfc_gobble_whitespace ();
3375 c
= gfc_next_ascii_char ();
3376 if (c
!= ',' && gfc_match_eos () != MATCH_YES
)
3379 if (!gfc_merge_new_implicit (&ts
))
3387 gfc_syntax_error (ST_IMPLICIT
);
3395 gfc_match_import (void)
3397 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3402 if (gfc_current_ns
->proc_name
== NULL
3403 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
3405 gfc_error ("IMPORT statement at %C only permitted in "
3406 "an INTERFACE body");
3410 if (gfc_current_ns
->proc_name
->attr
.module_procedure
)
3412 gfc_error ("F2008: C1210 IMPORT statement at %C is not permitted "
3413 "in a module procedure interface body");
3417 if (!gfc_notify_std (GFC_STD_F2003
, "IMPORT statement at %C"))
3420 if (gfc_match_eos () == MATCH_YES
)
3422 /* All host variables should be imported. */
3423 gfc_current_ns
->has_import_set
= 1;
3427 if (gfc_match (" ::") == MATCH_YES
)
3429 if (gfc_match_eos () == MATCH_YES
)
3431 gfc_error ("Expecting list of named entities at %C");
3439 m
= gfc_match (" %n", name
);
3443 if (gfc_current_ns
->parent
!= NULL
3444 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
3446 gfc_error ("Type name %qs at %C is ambiguous", name
);
3449 else if (!sym
&& gfc_current_ns
->proc_name
->ns
->parent
!= NULL
3450 && gfc_find_symbol (name
,
3451 gfc_current_ns
->proc_name
->ns
->parent
,
3454 gfc_error ("Type name %qs at %C is ambiguous", name
);
3460 gfc_error ("Cannot IMPORT %qs from host scoping unit "
3461 "at %C - does not exist.", name
);
3465 if (gfc_find_symtree (gfc_current_ns
->sym_root
, name
))
3467 gfc_warning (0, "%qs is already IMPORTed from host scoping unit "
3472 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
3475 sym
->attr
.imported
= 1;
3477 if (sym
->attr
.generic
&& (sym
= gfc_find_dt_in_generic (sym
)))
3479 /* The actual derived type is stored in a symtree with the first
3480 letter of the name capitalized; the symtree with the all
3481 lower-case name contains the associated generic function. */
3482 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
,
3483 gfc_get_string ("%c%s",
3484 (char) TOUPPER ((unsigned char) name
[0]),
3488 sym
->attr
.imported
= 1;
3501 if (gfc_match_eos () == MATCH_YES
)
3503 if (gfc_match_char (',') != MATCH_YES
)
3510 gfc_error ("Syntax error in IMPORT statement at %C");
3515 /* A minimal implementation of gfc_match without whitespace, escape
3516 characters or variable arguments. Returns true if the next
3517 characters match the TARGET template exactly. */
3520 match_string_p (const char *target
)
3524 for (p
= target
; *p
; p
++)
3525 if ((char) gfc_next_ascii_char () != *p
)
3530 /* Matches an attribute specification including array specs. If
3531 successful, leaves the variables current_attr and current_as
3532 holding the specification. Also sets the colon_seen variable for
3533 later use by matchers associated with initializations.
3535 This subroutine is a little tricky in the sense that we don't know
3536 if we really have an attr-spec until we hit the double colon.
3537 Until that time, we can only return MATCH_NO. This forces us to
3538 check for duplicate specification at this level. */
3541 match_attr_spec (void)
3543 /* Modifiers that can exist in a type statement. */
3545 { GFC_DECL_BEGIN
= 0,
3546 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
3547 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
3548 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
3549 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
3550 DECL_IS_BIND_C
, DECL_CODIMENSION
, DECL_ASYNCHRONOUS
, DECL_CONTIGUOUS
,
3551 DECL_NONE
, GFC_DECL_END
/* Sentinel */
3554 /* GFC_DECL_END is the sentinel, index starts at 0. */
3555 #define NUM_DECL GFC_DECL_END
3557 locus start
, seen_at
[NUM_DECL
];
3564 gfc_clear_attr (¤t_attr
);
3565 start
= gfc_current_locus
;
3570 /* See if we get all of the keywords up to the final double colon. */
3571 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3579 gfc_gobble_whitespace ();
3581 ch
= gfc_next_ascii_char ();
3584 /* This is the successful exit condition for the loop. */
3585 if (gfc_next_ascii_char () == ':')
3590 gfc_gobble_whitespace ();
3591 switch (gfc_peek_ascii_char ())
3594 gfc_next_ascii_char ();
3595 switch (gfc_next_ascii_char ())
3598 if (match_string_p ("locatable"))
3600 /* Matched "allocatable". */
3601 d
= DECL_ALLOCATABLE
;
3606 if (match_string_p ("ynchronous"))
3608 /* Matched "asynchronous". */
3609 d
= DECL_ASYNCHRONOUS
;
3616 /* Try and match the bind(c). */
3617 m
= gfc_match_bind_c (NULL
, true);
3620 else if (m
== MATCH_ERROR
)
3625 gfc_next_ascii_char ();
3626 if ('o' != gfc_next_ascii_char ())
3628 switch (gfc_next_ascii_char ())
3631 if (match_string_p ("imension"))
3633 d
= DECL_CODIMENSION
;
3637 if (match_string_p ("tiguous"))
3639 d
= DECL_CONTIGUOUS
;
3646 if (match_string_p ("dimension"))
3651 if (match_string_p ("external"))
3656 if (match_string_p ("int"))
3658 ch
= gfc_next_ascii_char ();
3661 if (match_string_p ("nt"))
3663 /* Matched "intent". */
3664 /* TODO: Call match_intent_spec from here. */
3665 if (gfc_match (" ( in out )") == MATCH_YES
)
3667 else if (gfc_match (" ( in )") == MATCH_YES
)
3669 else if (gfc_match (" ( out )") == MATCH_YES
)
3675 if (match_string_p ("insic"))
3677 /* Matched "intrinsic". */
3685 if (match_string_p ("optional"))
3690 gfc_next_ascii_char ();
3691 switch (gfc_next_ascii_char ())
3694 if (match_string_p ("rameter"))
3696 /* Matched "parameter". */
3702 if (match_string_p ("inter"))
3704 /* Matched "pointer". */
3710 ch
= gfc_next_ascii_char ();
3713 if (match_string_p ("vate"))
3715 /* Matched "private". */
3721 if (match_string_p ("tected"))
3723 /* Matched "protected". */
3730 if (match_string_p ("blic"))
3732 /* Matched "public". */
3740 if (match_string_p ("save"))
3745 if (match_string_p ("target"))
3750 gfc_next_ascii_char ();
3751 ch
= gfc_next_ascii_char ();
3754 if (match_string_p ("lue"))
3756 /* Matched "value". */
3762 if (match_string_p ("latile"))
3764 /* Matched "volatile". */
3772 /* No double colon and no recognizable decl_type, so assume that
3773 we've been looking at something else the whole time. */
3780 /* Check to make sure any parens are paired up correctly. */
3781 if (gfc_match_parens () == MATCH_ERROR
)
3788 seen_at
[d
] = gfc_current_locus
;
3790 if (d
== DECL_DIMENSION
|| d
== DECL_CODIMENSION
)
3792 gfc_array_spec
*as
= NULL
;
3794 m
= gfc_match_array_spec (&as
, d
== DECL_DIMENSION
,
3795 d
== DECL_CODIMENSION
);
3797 if (current_as
== NULL
)
3799 else if (m
== MATCH_YES
)
3801 if (!merge_array_spec (as
, current_as
, false))
3808 if (d
== DECL_CODIMENSION
)
3809 gfc_error ("Missing codimension specification at %C");
3811 gfc_error ("Missing dimension specification at %C");
3815 if (m
== MATCH_ERROR
)
3820 /* Since we've seen a double colon, we have to be looking at an
3821 attr-spec. This means that we can now issue errors. */
3822 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3827 case DECL_ALLOCATABLE
:
3828 attr
= "ALLOCATABLE";
3830 case DECL_ASYNCHRONOUS
:
3831 attr
= "ASYNCHRONOUS";
3833 case DECL_CODIMENSION
:
3834 attr
= "CODIMENSION";
3836 case DECL_CONTIGUOUS
:
3837 attr
= "CONTIGUOUS";
3839 case DECL_DIMENSION
:
3846 attr
= "INTENT (IN)";
3849 attr
= "INTENT (OUT)";
3852 attr
= "INTENT (IN OUT)";
3854 case DECL_INTRINSIC
:
3860 case DECL_PARAMETER
:
3866 case DECL_PROTECTED
:
3881 case DECL_IS_BIND_C
:
3891 attr
= NULL
; /* This shouldn't happen. */
3894 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
3899 /* Now that we've dealt with duplicate attributes, add the attributes
3900 to the current attribute. */
3901 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3906 if (gfc_current_state () == COMP_DERIVED
3907 && d
!= DECL_DIMENSION
&& d
!= DECL_CODIMENSION
3908 && d
!= DECL_POINTER
&& d
!= DECL_PRIVATE
3909 && d
!= DECL_PUBLIC
&& d
!= DECL_CONTIGUOUS
&& d
!= DECL_NONE
)
3911 if (d
== DECL_ALLOCATABLE
)
3913 if (!gfc_notify_std (GFC_STD_F2003
, "ALLOCATABLE "
3914 "attribute at %C in a TYPE definition"))
3922 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3929 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
3930 && gfc_current_state () != COMP_MODULE
)
3932 if (d
== DECL_PRIVATE
)
3936 if (gfc_current_state () == COMP_DERIVED
3937 && gfc_state_stack
->previous
3938 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
3940 if (!gfc_notify_std (GFC_STD_F2003
, "Attribute %s "
3941 "at %L in a TYPE definition", attr
,
3950 gfc_error ("%s attribute at %L is not allowed outside of the "
3951 "specification part of a module", attr
, &seen_at
[d
]);
3959 case DECL_ALLOCATABLE
:
3960 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
3963 case DECL_ASYNCHRONOUS
:
3964 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS attribute at %C"))
3967 t
= gfc_add_asynchronous (¤t_attr
, NULL
, &seen_at
[d
]);
3970 case DECL_CODIMENSION
:
3971 t
= gfc_add_codimension (¤t_attr
, NULL
, &seen_at
[d
]);
3974 case DECL_CONTIGUOUS
:
3975 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS attribute at %C"))
3978 t
= gfc_add_contiguous (¤t_attr
, NULL
, &seen_at
[d
]);
3981 case DECL_DIMENSION
:
3982 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
3986 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
3990 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
3994 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
3998 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
4001 case DECL_INTRINSIC
:
4002 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
4006 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
4009 case DECL_PARAMETER
:
4010 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
4014 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
4017 case DECL_PROTECTED
:
4018 if (gfc_current_state () != COMP_MODULE
4019 || (gfc_current_ns
->proc_name
4020 && gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
))
4022 gfc_error ("PROTECTED at %C only allowed in specification "
4023 "part of a module");
4028 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED attribute at %C"))
4031 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
4035 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
4040 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
4045 t
= gfc_add_save (¤t_attr
, SAVE_EXPLICIT
, NULL
, &seen_at
[d
]);
4049 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
4052 case DECL_IS_BIND_C
:
4053 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
4057 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE attribute at %C"))
4060 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
4064 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE attribute at %C"))
4067 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
4071 gfc_internal_error ("match_attr_spec(): Bad attribute");
4081 /* Since Fortran 2008 module variables implicitly have the SAVE attribute. */
4082 if ((gfc_current_state () == COMP_MODULE
4083 || gfc_current_state () == COMP_SUBMODULE
)
4084 && !current_attr
.save
4085 && (gfc_option
.allow_std
& GFC_STD_F2008
) != 0)
4086 current_attr
.save
= SAVE_IMPLICIT
;
4092 gfc_current_locus
= start
;
4093 gfc_free_array_spec (current_as
);
4099 /* Set the binding label, dest_label, either with the binding label
4100 stored in the given gfc_typespec, ts, or if none was provided, it
4101 will be the symbol name in all lower case, as required by the draft
4102 (J3/04-007, section 15.4.1). If a binding label was given and
4103 there is more than one argument (num_idents), it is an error. */
4106 set_binding_label (const char **dest_label
, const char *sym_name
,
4109 if (num_idents
> 1 && has_name_equals
)
4111 gfc_error ("Multiple identifiers provided with "
4112 "single NAME= specifier at %C");
4116 if (curr_binding_label
)
4117 /* Binding label given; store in temp holder till have sym. */
4118 *dest_label
= curr_binding_label
;
4121 /* No binding label given, and the NAME= specifier did not exist,
4122 which means there was no NAME="". */
4123 if (sym_name
!= NULL
&& has_name_equals
== 0)
4124 *dest_label
= IDENTIFIER_POINTER (get_identifier (sym_name
));
4131 /* Set the status of the given common block as being BIND(C) or not,
4132 depending on the given parameter, is_bind_c. */
4135 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
4137 com_block
->is_bind_c
= is_bind_c
;
4142 /* Verify that the given gfc_typespec is for a C interoperable type. */
4145 gfc_verify_c_interop (gfc_typespec
*ts
)
4147 if (ts
->type
== BT_DERIVED
&& ts
->u
.derived
!= NULL
)
4148 return (ts
->u
.derived
->ts
.is_c_interop
|| ts
->u
.derived
->attr
.is_bind_c
)
4150 else if (ts
->type
== BT_CLASS
)
4152 else if (ts
->is_c_interop
!= 1 && ts
->type
!= BT_ASSUMED
)
4159 /* Verify that the variables of a given common block, which has been
4160 defined with the attribute specifier bind(c), to be of a C
4161 interoperable type. Errors will be reported here, if
4165 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
4167 gfc_symbol
*curr_sym
= NULL
;
4170 curr_sym
= com_block
->head
;
4172 /* Make sure we have at least one symbol. */
4173 if (curr_sym
== NULL
)
4176 /* Here we know we have a symbol, so we'll execute this loop
4180 /* The second to last param, 1, says this is in a common block. */
4181 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
4182 curr_sym
= curr_sym
->common_next
;
4183 } while (curr_sym
!= NULL
);
4189 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
4190 an appropriate error message is reported. */
4193 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
4194 int is_in_common
, gfc_common_head
*com_block
)
4196 bool bind_c_function
= false;
4199 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
4200 bind_c_function
= true;
4202 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
4204 tmp_sym
= tmp_sym
->result
;
4205 /* Make sure it wasn't an implicitly typed result. */
4206 if (tmp_sym
->attr
.implicit_type
&& warn_c_binding_type
)
4208 gfc_warning (OPT_Wc_binding_type
,
4209 "Implicitly declared BIND(C) function %qs at "
4210 "%L may not be C interoperable", tmp_sym
->name
,
4211 &tmp_sym
->declared_at
);
4212 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
4213 /* Mark it as C interoperable to prevent duplicate warnings. */
4214 tmp_sym
->ts
.is_c_interop
= 1;
4215 tmp_sym
->attr
.is_c_interop
= 1;
4219 /* Here, we know we have the bind(c) attribute, so if we have
4220 enough type info, then verify that it's a C interop kind.
4221 The info could be in the symbol already, or possibly still in
4222 the given ts (current_ts), so look in both. */
4223 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
4225 if (!gfc_verify_c_interop (&(tmp_sym
->ts
)))
4227 /* See if we're dealing with a sym in a common block or not. */
4228 if (is_in_common
== 1 && warn_c_binding_type
)
4230 gfc_warning (OPT_Wc_binding_type
,
4231 "Variable %qs in common block %qs at %L "
4232 "may not be a C interoperable "
4233 "kind though common block %qs is BIND(C)",
4234 tmp_sym
->name
, com_block
->name
,
4235 &(tmp_sym
->declared_at
), com_block
->name
);
4239 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
4240 gfc_error ("Type declaration %qs at %L is not C "
4241 "interoperable but it is BIND(C)",
4242 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4243 else if (warn_c_binding_type
)
4244 gfc_warning (OPT_Wc_binding_type
, "Variable %qs at %L "
4245 "may not be a C interoperable "
4246 "kind but it is BIND(C)",
4247 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4251 /* Variables declared w/in a common block can't be bind(c)
4252 since there's no way for C to see these variables, so there's
4253 semantically no reason for the attribute. */
4254 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
4256 gfc_error ("Variable %qs in common block %qs at "
4257 "%L cannot be declared with BIND(C) "
4258 "since it is not a global",
4259 tmp_sym
->name
, com_block
->name
,
4260 &(tmp_sym
->declared_at
));
4264 /* Scalar variables that are bind(c) can not have the pointer
4265 or allocatable attributes. */
4266 if (tmp_sym
->attr
.is_bind_c
== 1)
4268 if (tmp_sym
->attr
.pointer
== 1)
4270 gfc_error ("Variable %qs at %L cannot have both the "
4271 "POINTER and BIND(C) attributes",
4272 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4276 if (tmp_sym
->attr
.allocatable
== 1)
4278 gfc_error ("Variable %qs at %L cannot have both the "
4279 "ALLOCATABLE and BIND(C) attributes",
4280 tmp_sym
->name
, &(tmp_sym
->declared_at
));
4286 /* If it is a BIND(C) function, make sure the return value is a
4287 scalar value. The previous tests in this function made sure
4288 the type is interoperable. */
4289 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
4290 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4291 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
4293 /* BIND(C) functions can not return a character string. */
4294 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
4295 if (tmp_sym
->ts
.u
.cl
== NULL
|| tmp_sym
->ts
.u
.cl
->length
== NULL
4296 || tmp_sym
->ts
.u
.cl
->length
->expr_type
!= EXPR_CONSTANT
4297 || mpz_cmp_si (tmp_sym
->ts
.u
.cl
->length
->value
.integer
, 1) != 0)
4298 gfc_error ("Return type of BIND(C) function %qs at %L cannot "
4299 "be a character string", tmp_sym
->name
,
4300 &(tmp_sym
->declared_at
));
4303 /* See if the symbol has been marked as private. If it has, make sure
4304 there is no binding label and warn the user if there is one. */
4305 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
4306 && tmp_sym
->binding_label
)
4307 /* Use gfc_warning_now because we won't say that the symbol fails
4308 just because of this. */
4309 gfc_warning_now (0, "Symbol %qs at %L is marked PRIVATE but has been "
4310 "given the binding label %qs", tmp_sym
->name
,
4311 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
4317 /* Set the appropriate fields for a symbol that's been declared as
4318 BIND(C) (the is_bind_c flag and the binding label), and verify that
4319 the type is C interoperable. Errors are reported by the functions
4320 used to set/test these fields. */
4323 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
4327 /* TODO: Do we need to make sure the vars aren't marked private? */
4329 /* Set the is_bind_c bit in symbol_attribute. */
4330 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
4332 if (!set_binding_label (&tmp_sym
->binding_label
, tmp_sym
->name
, num_idents
))
4339 /* Set the fields marking the given common block as BIND(C), including
4340 a binding label, and report any errors encountered. */
4343 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
4347 /* destLabel, common name, typespec (which may have binding label). */
4348 if (!set_binding_label (&com_block
->binding_label
, com_block
->name
,
4352 /* Set the given common block (com_block) to being bind(c) (1). */
4353 set_com_block_bind_c (com_block
, 1);
4359 /* Retrieve the list of one or more identifiers that the given bind(c)
4360 attribute applies to. */
4363 get_bind_c_idents (void)
4365 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4367 gfc_symbol
*tmp_sym
= NULL
;
4369 gfc_common_head
*com_block
= NULL
;
4371 if (gfc_match_name (name
) == MATCH_YES
)
4373 found_id
= MATCH_YES
;
4374 gfc_get_ha_symbol (name
, &tmp_sym
);
4376 else if (match_common_name (name
) == MATCH_YES
)
4378 found_id
= MATCH_YES
;
4379 com_block
= gfc_get_common (name
, 0);
4383 gfc_error ("Need either entity or common block name for "
4384 "attribute specification statement at %C");
4388 /* Save the current identifier and look for more. */
4391 /* Increment the number of identifiers found for this spec stmt. */
4394 /* Make sure we have a sym or com block, and verify that it can
4395 be bind(c). Set the appropriate field(s) and look for more
4397 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
4399 if (tmp_sym
!= NULL
)
4401 if (!set_verify_bind_c_sym (tmp_sym
, num_idents
))
4406 if (!set_verify_bind_c_com_block (com_block
, num_idents
))
4410 /* Look to see if we have another identifier. */
4412 if (gfc_match_eos () == MATCH_YES
)
4413 found_id
= MATCH_NO
;
4414 else if (gfc_match_char (',') != MATCH_YES
)
4415 found_id
= MATCH_NO
;
4416 else if (gfc_match_name (name
) == MATCH_YES
)
4418 found_id
= MATCH_YES
;
4419 gfc_get_ha_symbol (name
, &tmp_sym
);
4421 else if (match_common_name (name
) == MATCH_YES
)
4423 found_id
= MATCH_YES
;
4424 com_block
= gfc_get_common (name
, 0);
4428 gfc_error ("Missing entity or common block name for "
4429 "attribute specification statement at %C");
4435 gfc_internal_error ("Missing symbol");
4437 } while (found_id
== MATCH_YES
);
4439 /* if we get here we were successful */
4444 /* Try and match a BIND(C) attribute specification statement. */
4447 gfc_match_bind_c_stmt (void)
4449 match found_match
= MATCH_NO
;
4454 /* This may not be necessary. */
4456 /* Clear the temporary binding label holder. */
4457 curr_binding_label
= NULL
;
4459 /* Look for the bind(c). */
4460 found_match
= gfc_match_bind_c (NULL
, true);
4462 if (found_match
== MATCH_YES
)
4464 if (!gfc_notify_std (GFC_STD_F2003
, "BIND(C) statement at %C"))
4467 /* Look for the :: now, but it is not required. */
4470 /* Get the identifier(s) that needs to be updated. This may need to
4471 change to hand the flag(s) for the attr specified so all identifiers
4472 found can have all appropriate parts updated (assuming that the same
4473 spec stmt can have multiple attrs, such as both bind(c) and
4475 if (!get_bind_c_idents ())
4476 /* Error message should have printed already. */
4484 /* Match a data declaration statement. */
4487 gfc_match_data_decl (void)
4493 num_idents_on_line
= 0;
4495 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
4499 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4500 && gfc_current_state () != COMP_DERIVED
)
4502 sym
= gfc_use_derived (current_ts
.u
.derived
);
4510 current_ts
.u
.derived
= sym
;
4513 m
= match_attr_spec ();
4514 if (m
== MATCH_ERROR
)
4520 if (current_ts
.type
== BT_CLASS
4521 && current_ts
.u
.derived
->attr
.unlimited_polymorphic
)
4524 if ((current_ts
.type
== BT_DERIVED
|| current_ts
.type
== BT_CLASS
)
4525 && current_ts
.u
.derived
->components
== NULL
4526 && !current_ts
.u
.derived
->attr
.zero_comp
)
4529 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
4532 gfc_find_symbol (current_ts
.u
.derived
->name
,
4533 current_ts
.u
.derived
->ns
, 1, &sym
);
4535 /* Any symbol that we find had better be a type definition
4536 which has its components defined. */
4537 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
4538 && (current_ts
.u
.derived
->components
!= NULL
4539 || current_ts
.u
.derived
->attr
.zero_comp
))
4542 gfc_error ("Derived type at %C has not been previously defined "
4543 "and so cannot appear in a derived type definition");
4549 /* If we have an old-style character declaration, and no new-style
4550 attribute specifications, then there a comma is optional between
4551 the type specification and the variable list. */
4552 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
4553 gfc_match_char (',');
4555 /* Give the types/attributes to symbols that follow. Give the element
4556 a number so that repeat character length expressions can be copied. */
4560 num_idents_on_line
++;
4561 m
= variable_decl (elem
++);
4562 if (m
== MATCH_ERROR
)
4567 if (gfc_match_eos () == MATCH_YES
)
4569 if (gfc_match_char (',') != MATCH_YES
)
4573 if (!gfc_error_flag_test ())
4574 gfc_error ("Syntax error in data declaration at %C");
4577 gfc_free_data_all (gfc_current_ns
);
4580 gfc_free_array_spec (current_as
);
4586 /* Match a prefix associated with a function or subroutine
4587 declaration. If the typespec pointer is nonnull, then a typespec
4588 can be matched. Note that if nothing matches, MATCH_YES is
4589 returned (the null string was matched). */
4592 gfc_match_prefix (gfc_typespec
*ts
)
4598 gfc_clear_attr (¤t_attr
);
4600 seen_impure
= false;
4602 gcc_assert (!gfc_matching_prefix
);
4603 gfc_matching_prefix
= true;
4607 found_prefix
= false;
4609 /* MODULE is a prefix like PURE, ELEMENTAL, etc., having a
4610 corresponding attribute seems natural and distinguishes these
4611 procedures from procedure types of PROC_MODULE, which these are
4613 if (gfc_match ("module% ") == MATCH_YES
)
4615 if (!gfc_notify_std (GFC_STD_F2008
, "MODULE prefix at %C"))
4618 current_attr
.module_procedure
= 1;
4619 found_prefix
= true;
4622 if (!seen_type
&& ts
!= NULL
4623 && gfc_match_decl_type_spec (ts
, 0) == MATCH_YES
4624 && gfc_match_space () == MATCH_YES
)
4628 found_prefix
= true;
4631 if (gfc_match ("elemental% ") == MATCH_YES
)
4633 if (!gfc_add_elemental (¤t_attr
, NULL
))
4636 found_prefix
= true;
4639 if (gfc_match ("pure% ") == MATCH_YES
)
4641 if (!gfc_add_pure (¤t_attr
, NULL
))
4644 found_prefix
= true;
4647 if (gfc_match ("recursive% ") == MATCH_YES
)
4649 if (!gfc_add_recursive (¤t_attr
, NULL
))
4652 found_prefix
= true;
4655 /* IMPURE is a somewhat special case, as it needs not set an actual
4656 attribute but rather only prevents ELEMENTAL routines from being
4657 automatically PURE. */
4658 if (gfc_match ("impure% ") == MATCH_YES
)
4660 if (!gfc_notify_std (GFC_STD_F2008
, "IMPURE procedure at %C"))
4664 found_prefix
= true;
4667 while (found_prefix
);
4669 /* IMPURE and PURE must not both appear, of course. */
4670 if (seen_impure
&& current_attr
.pure
)
4672 gfc_error ("PURE and IMPURE must not appear both at %C");
4676 /* If IMPURE it not seen but the procedure is ELEMENTAL, mark it as PURE. */
4677 if (!seen_impure
&& current_attr
.elemental
&& !current_attr
.pure
)
4679 if (!gfc_add_pure (¤t_attr
, NULL
))
4683 /* At this point, the next item is not a prefix. */
4684 gcc_assert (gfc_matching_prefix
);
4686 gfc_matching_prefix
= false;
4690 gcc_assert (gfc_matching_prefix
);
4691 gfc_matching_prefix
= false;
4696 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
4699 copy_prefix (symbol_attribute
*dest
, locus
*where
)
4701 if (current_attr
.pure
&& !gfc_add_pure (dest
, where
))
4704 if (current_attr
.elemental
&& !gfc_add_elemental (dest
, where
))
4707 if (current_attr
.recursive
&& !gfc_add_recursive (dest
, where
))
4714 /* Match a formal argument list. */
4717 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
4719 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
4720 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4723 gfc_formal_arglist
*formal
= NULL
;
4727 /* Keep the interface formal argument list and null it so that the
4728 matching for the new declaration can be done. The numbers and
4729 names of the arguments are checked here. The interface formal
4730 arguments are retained in formal_arglist and the characteristics
4731 are compared in resolve.c(resolve_fl_procedure). See the remark
4732 in get_proc_name about the eventual need to copy the formal_arglist
4733 and populate the formal namespace of the interface symbol. */
4734 if (progname
->attr
.module_procedure
4735 && progname
->attr
.host_assoc
)
4737 formal
= progname
->formal
;
4738 progname
->formal
= NULL
;
4741 if (gfc_match_char ('(') != MATCH_YES
)
4748 if (gfc_match_char (')') == MATCH_YES
)
4753 if (gfc_match_char ('*') == MATCH_YES
)
4756 if (!gfc_notify_std (GFC_STD_F95_OBS
, "Alternate-return argument "
4765 m
= gfc_match_name (name
);
4769 if (gfc_get_symbol (name
, NULL
, &sym
))
4773 p
= gfc_get_formal_arglist ();
4785 /* We don't add the VARIABLE flavor because the name could be a
4786 dummy procedure. We don't apply these attributes to formal
4787 arguments of statement functions. */
4788 if (sym
!= NULL
&& !st_flag
4789 && (!gfc_add_dummy(&sym
->attr
, sym
->name
, NULL
)
4790 || !gfc_missing_attr (&sym
->attr
, NULL
)))
4796 /* The name of a program unit can be in a different namespace,
4797 so check for it explicitly. After the statement is accepted,
4798 the name is checked for especially in gfc_get_symbol(). */
4799 if (gfc_new_block
!= NULL
&& sym
!= NULL
4800 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
4802 gfc_error ("Name %qs at %C is the name of the procedure",
4808 if (gfc_match_char (')') == MATCH_YES
)
4811 m
= gfc_match_char (',');
4814 gfc_error ("Unexpected junk in formal argument list at %C");
4820 /* Check for duplicate symbols in the formal argument list. */
4823 for (p
= head
; p
->next
; p
= p
->next
)
4828 for (q
= p
->next
; q
; q
= q
->next
)
4829 if (p
->sym
== q
->sym
)
4831 gfc_error ("Duplicate symbol %qs in formal argument list "
4832 "at %C", p
->sym
->name
);
4840 if (!gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
))
4846 /* gfc_error_now used in following and return with MATCH_YES because
4847 doing otherwise results in a cascade of extraneous errors and in
4848 some cases an ICE in symbol.c(gfc_release_symbol). */
4849 if (progname
->attr
.module_procedure
&& progname
->attr
.host_assoc
)
4851 bool arg_count_mismatch
= false;
4853 if (!formal
&& head
)
4854 arg_count_mismatch
= true;
4856 /* Abbreviated module procedure declaration is not meant to have any
4857 formal arguments! */
4858 if (!progname
->abr_modproc_decl
&& formal
&& !head
)
4859 arg_count_mismatch
= true;
4861 for (p
= formal
, q
= head
; p
&& q
; p
= p
->next
, q
= q
->next
)
4863 if ((p
->next
!= NULL
&& q
->next
== NULL
)
4864 || (p
->next
== NULL
&& q
->next
!= NULL
))
4865 arg_count_mismatch
= true;
4866 else if ((p
->sym
== NULL
&& q
->sym
== NULL
)
4867 || strcmp (p
->sym
->name
, q
->sym
->name
) == 0)
4870 gfc_error_now ("Mismatch in MODULE PROCEDURE formal "
4871 "argument names (%s/%s) at %C",
4872 p
->sym
->name
, q
->sym
->name
);
4875 if (arg_count_mismatch
)
4876 gfc_error_now ("Mismatch in number of MODULE PROCEDURE "
4877 "formal arguments at %C");
4883 gfc_free_formal_arglist (head
);
4888 /* Match a RESULT specification following a function declaration or
4889 ENTRY statement. Also matches the end-of-statement. */
4892 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
4894 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4898 if (gfc_match (" result (") != MATCH_YES
)
4901 m
= gfc_match_name (name
);
4905 /* Get the right paren, and that's it because there could be the
4906 bind(c) attribute after the result clause. */
4907 if (gfc_match_char (')') != MATCH_YES
)
4909 /* TODO: should report the missing right paren here. */
4913 if (strcmp (function
->name
, name
) == 0)
4915 gfc_error ("RESULT variable at %C must be different than function name");
4919 if (gfc_get_symbol (name
, NULL
, &r
))
4922 if (!gfc_add_result (&r
->attr
, r
->name
, NULL
))
4931 /* Match a function suffix, which could be a combination of a result
4932 clause and BIND(C), either one, or neither. The draft does not
4933 require them to come in a specific order. */
4936 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
4938 match is_bind_c
; /* Found bind(c). */
4939 match is_result
; /* Found result clause. */
4940 match found_match
; /* Status of whether we've found a good match. */
4941 char peek_char
; /* Character we're going to peek at. */
4942 bool allow_binding_name
;
4944 /* Initialize to having found nothing. */
4945 found_match
= MATCH_NO
;
4946 is_bind_c
= MATCH_NO
;
4947 is_result
= MATCH_NO
;
4949 /* Get the next char to narrow between result and bind(c). */
4950 gfc_gobble_whitespace ();
4951 peek_char
= gfc_peek_ascii_char ();
4953 /* C binding names are not allowed for internal procedures. */
4954 if (gfc_current_state () == COMP_CONTAINS
4955 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4956 allow_binding_name
= false;
4958 allow_binding_name
= true;
4963 /* Look for result clause. */
4964 is_result
= match_result (sym
, result
);
4965 if (is_result
== MATCH_YES
)
4967 /* Now see if there is a bind(c) after it. */
4968 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4969 /* We've found the result clause and possibly bind(c). */
4970 found_match
= MATCH_YES
;
4973 /* This should only be MATCH_ERROR. */
4974 found_match
= is_result
;
4977 /* Look for bind(c) first. */
4978 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4979 if (is_bind_c
== MATCH_YES
)
4981 /* Now see if a result clause followed it. */
4982 is_result
= match_result (sym
, result
);
4983 found_match
= MATCH_YES
;
4987 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4988 found_match
= MATCH_ERROR
;
4992 gfc_error ("Unexpected junk after function declaration at %C");
4993 found_match
= MATCH_ERROR
;
4997 if (is_bind_c
== MATCH_YES
)
4999 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
5000 if (gfc_current_state () == COMP_CONTAINS
5001 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
5002 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
5003 "at %L may not be specified for an internal "
5004 "procedure", &gfc_current_locus
))
5007 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1))
5015 /* Procedure pointer return value without RESULT statement:
5016 Add "hidden" result variable named "ppr@". */
5019 add_hidden_procptr_result (gfc_symbol
*sym
)
5023 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
5026 /* First usage case: PROCEDURE and EXTERNAL statements. */
5027 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
5028 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
5029 && sym
->attr
.external
;
5030 /* Second usage case: INTERFACE statements. */
5031 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
5032 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
5033 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
5039 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
, false);
5043 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
, false);
5044 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
5045 st2
->n
.sym
= stree
->n
.sym
;
5047 sym
->result
= stree
->n
.sym
;
5049 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
5050 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
5051 sym
->result
->attr
.external
= sym
->attr
.external
;
5052 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
5053 sym
->result
->ts
= sym
->ts
;
5054 sym
->attr
.proc_pointer
= 0;
5055 sym
->attr
.pointer
= 0;
5056 sym
->attr
.external
= 0;
5057 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
5059 sym
->result
->attr
.pointer
= 0;
5060 sym
->result
->attr
.proc_pointer
= 1;
5063 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
5065 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
5066 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
5067 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
5068 && sym
== gfc_current_ns
->proc_name
5069 && sym
== sym
->result
->ns
->proc_name
5070 && strcmp ("ppr@", sym
->result
->name
) == 0)
5072 sym
->result
->attr
.proc_pointer
= 1;
5073 sym
->attr
.pointer
= 0;
5081 /* Match the interface for a PROCEDURE declaration,
5082 including brackets (R1212). */
5085 match_procedure_interface (gfc_symbol
**proc_if
)
5089 locus old_loc
, entry_loc
;
5090 gfc_namespace
*old_ns
= gfc_current_ns
;
5091 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5093 old_loc
= entry_loc
= gfc_current_locus
;
5094 gfc_clear_ts (¤t_ts
);
5096 if (gfc_match (" (") != MATCH_YES
)
5098 gfc_current_locus
= entry_loc
;
5102 /* Get the type spec. for the procedure interface. */
5103 old_loc
= gfc_current_locus
;
5104 m
= gfc_match_decl_type_spec (¤t_ts
, 0);
5105 gfc_gobble_whitespace ();
5106 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
5109 if (m
== MATCH_ERROR
)
5112 /* Procedure interface is itself a procedure. */
5113 gfc_current_locus
= old_loc
;
5114 m
= gfc_match_name (name
);
5116 /* First look to see if it is already accessible in the current
5117 namespace because it is use associated or contained. */
5119 if (gfc_find_sym_tree (name
, NULL
, 0, &st
))
5122 /* If it is still not found, then try the parent namespace, if it
5123 exists and create the symbol there if it is still not found. */
5124 if (gfc_current_ns
->parent
)
5125 gfc_current_ns
= gfc_current_ns
->parent
;
5126 if (st
== NULL
&& gfc_get_ha_sym_tree (name
, &st
))
5129 gfc_current_ns
= old_ns
;
5130 *proc_if
= st
->n
.sym
;
5135 /* Resolve interface if possible. That way, attr.procedure is only set
5136 if it is declared by a later procedure-declaration-stmt, which is
5137 invalid per F08:C1216 (cf. resolve_procedure_interface). */
5138 while ((*proc_if
)->ts
.interface
)
5139 *proc_if
= (*proc_if
)->ts
.interface
;
5141 if ((*proc_if
)->attr
.flavor
== FL_UNKNOWN
5142 && (*proc_if
)->ts
.type
== BT_UNKNOWN
5143 && !gfc_add_flavor (&(*proc_if
)->attr
, FL_PROCEDURE
,
5144 (*proc_if
)->name
, NULL
))
5149 if (gfc_match (" )") != MATCH_YES
)
5151 gfc_current_locus
= entry_loc
;
5159 /* Match a PROCEDURE declaration (R1211). */
5162 match_procedure_decl (void)
5165 gfc_symbol
*sym
, *proc_if
= NULL
;
5167 gfc_expr
*initializer
= NULL
;
5169 /* Parse interface (with brackets). */
5170 m
= match_procedure_interface (&proc_if
);
5174 /* Parse attributes (with colons). */
5175 m
= match_attr_spec();
5176 if (m
== MATCH_ERROR
)
5179 if (proc_if
&& proc_if
->attr
.is_bind_c
&& !current_attr
.is_bind_c
)
5181 current_attr
.is_bind_c
= 1;
5182 has_name_equals
= 0;
5183 curr_binding_label
= NULL
;
5186 /* Get procedure symbols. */
5189 m
= gfc_match_symbol (&sym
, 0);
5192 else if (m
== MATCH_ERROR
)
5195 /* Add current_attr to the symbol attributes. */
5196 if (!gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
))
5199 if (sym
->attr
.is_bind_c
)
5201 /* Check for C1218. */
5202 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
5204 gfc_error ("BIND(C) attribute at %C requires "
5205 "an interface with BIND(C)");
5208 /* Check for C1217. */
5209 if (has_name_equals
&& sym
->attr
.pointer
)
5211 gfc_error ("BIND(C) procedure with NAME may not have "
5212 "POINTER attribute at %C");
5215 if (has_name_equals
&& sym
->attr
.dummy
)
5217 gfc_error ("Dummy procedure at %C may not have "
5218 "BIND(C) attribute with NAME");
5221 /* Set binding label for BIND(C). */
5222 if (!set_binding_label (&sym
->binding_label
, sym
->name
, num
))
5226 if (!gfc_add_external (&sym
->attr
, NULL
))
5229 if (add_hidden_procptr_result (sym
))
5232 if (!gfc_add_proc (&sym
->attr
, sym
->name
, NULL
))
5235 /* Set interface. */
5236 if (proc_if
!= NULL
)
5238 if (sym
->ts
.type
!= BT_UNKNOWN
)
5240 gfc_error ("Procedure %qs at %L already has basic type of %s",
5241 sym
->name
, &gfc_current_locus
,
5242 gfc_basic_typename (sym
->ts
.type
));
5245 sym
->ts
.interface
= proc_if
;
5246 sym
->attr
.untyped
= 1;
5247 sym
->attr
.if_source
= IFSRC_IFBODY
;
5249 else if (current_ts
.type
!= BT_UNKNOWN
)
5251 if (!gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
5253 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5254 sym
->ts
.interface
->ts
= current_ts
;
5255 sym
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5256 sym
->ts
.interface
->attr
.function
= 1;
5257 sym
->attr
.function
= 1;
5258 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
5261 if (gfc_match (" =>") == MATCH_YES
)
5263 if (!current_attr
.pointer
)
5265 gfc_error ("Initialization at %C isn't for a pointer variable");
5270 m
= match_pointer_init (&initializer
, 1);
5274 if (!add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
))
5279 if (gfc_match_eos () == MATCH_YES
)
5281 if (gfc_match_char (',') != MATCH_YES
)
5286 gfc_error ("Syntax error in PROCEDURE statement at %C");
5290 /* Free stuff up and return. */
5291 gfc_free_expr (initializer
);
5297 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
);
5300 /* Match a procedure pointer component declaration (R445). */
5303 match_ppc_decl (void)
5306 gfc_symbol
*proc_if
= NULL
;
5310 gfc_expr
*initializer
= NULL
;
5311 gfc_typebound_proc
* tb
;
5312 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5314 /* Parse interface (with brackets). */
5315 m
= match_procedure_interface (&proc_if
);
5319 /* Parse attributes. */
5320 tb
= XCNEW (gfc_typebound_proc
);
5321 tb
->where
= gfc_current_locus
;
5322 m
= match_binding_attributes (tb
, false, true);
5323 if (m
== MATCH_ERROR
)
5326 gfc_clear_attr (¤t_attr
);
5327 current_attr
.procedure
= 1;
5328 current_attr
.proc_pointer
= 1;
5329 current_attr
.access
= tb
->access
;
5330 current_attr
.flavor
= FL_PROCEDURE
;
5332 /* Match the colons (required). */
5333 if (gfc_match (" ::") != MATCH_YES
)
5335 gfc_error ("Expected %<::%> after binding-attributes at %C");
5339 /* Check for C450. */
5340 if (!tb
->nopass
&& proc_if
== NULL
)
5342 gfc_error("NOPASS or explicit interface required at %C");
5346 if (!gfc_notify_std (GFC_STD_F2003
, "Procedure pointer component at %C"))
5349 /* Match PPC names. */
5353 m
= gfc_match_name (name
);
5356 else if (m
== MATCH_ERROR
)
5359 if (!gfc_add_component (gfc_current_block(), name
, &c
))
5362 /* Add current_attr to the symbol attributes. */
5363 if (!gfc_copy_attr (&c
->attr
, ¤t_attr
, NULL
))
5366 if (!gfc_add_external (&c
->attr
, NULL
))
5369 if (!gfc_add_proc (&c
->attr
, name
, NULL
))
5376 c
->tb
= XCNEW (gfc_typebound_proc
);
5377 c
->tb
->where
= gfc_current_locus
;
5381 /* Set interface. */
5382 if (proc_if
!= NULL
)
5384 c
->ts
.interface
= proc_if
;
5385 c
->attr
.untyped
= 1;
5386 c
->attr
.if_source
= IFSRC_IFBODY
;
5388 else if (ts
.type
!= BT_UNKNOWN
)
5391 c
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
5392 c
->ts
.interface
->result
= c
->ts
.interface
;
5393 c
->ts
.interface
->ts
= ts
;
5394 c
->ts
.interface
->attr
.flavor
= FL_PROCEDURE
;
5395 c
->ts
.interface
->attr
.function
= 1;
5396 c
->attr
.function
= 1;
5397 c
->attr
.if_source
= IFSRC_UNKNOWN
;
5400 if (gfc_match (" =>") == MATCH_YES
)
5402 m
= match_pointer_init (&initializer
, 1);
5405 gfc_free_expr (initializer
);
5408 c
->initializer
= initializer
;
5411 if (gfc_match_eos () == MATCH_YES
)
5413 if (gfc_match_char (',') != MATCH_YES
)
5418 gfc_error ("Syntax error in procedure pointer component at %C");
5423 /* Match a PROCEDURE declaration inside an interface (R1206). */
5426 match_procedure_in_interface (void)
5430 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5433 if (current_interface
.type
== INTERFACE_NAMELESS
5434 || current_interface
.type
== INTERFACE_ABSTRACT
)
5436 gfc_error ("PROCEDURE at %C must be in a generic interface");
5440 /* Check if the F2008 optional double colon appears. */
5441 gfc_gobble_whitespace ();
5442 old_locus
= gfc_current_locus
;
5443 if (gfc_match ("::") == MATCH_YES
)
5445 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
5446 "MODULE PROCEDURE statement at %L", &old_locus
))
5450 gfc_current_locus
= old_locus
;
5454 m
= gfc_match_name (name
);
5457 else if (m
== MATCH_ERROR
)
5459 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
5462 if (!gfc_add_interface (sym
))
5465 if (gfc_match_eos () == MATCH_YES
)
5467 if (gfc_match_char (',') != MATCH_YES
)
5474 gfc_error ("Syntax error in PROCEDURE statement at %C");
5479 /* General matcher for PROCEDURE declarations. */
5481 static match
match_procedure_in_type (void);
5484 gfc_match_procedure (void)
5488 switch (gfc_current_state ())
5493 case COMP_SUBMODULE
:
5494 case COMP_SUBROUTINE
:
5497 m
= match_procedure_decl ();
5499 case COMP_INTERFACE
:
5500 m
= match_procedure_in_interface ();
5503 m
= match_ppc_decl ();
5505 case COMP_DERIVED_CONTAINS
:
5506 m
= match_procedure_in_type ();
5515 if (!gfc_notify_std (GFC_STD_F2003
, "PROCEDURE statement at %C"))
5522 /* Warn if a matched procedure has the same name as an intrinsic; this is
5523 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
5524 parser-state-stack to find out whether we're in a module. */
5527 do_warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
5531 in_module
= (gfc_state_stack
->previous
5532 && (gfc_state_stack
->previous
->state
== COMP_MODULE
5533 || gfc_state_stack
->previous
->state
== COMP_SUBMODULE
));
5535 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
5539 /* Match a function declaration. */
5542 gfc_match_function_decl (void)
5544 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5545 gfc_symbol
*sym
, *result
;
5549 match found_match
; /* Status returned by match func. */
5551 if (gfc_current_state () != COMP_NONE
5552 && gfc_current_state () != COMP_INTERFACE
5553 && gfc_current_state () != COMP_CONTAINS
)
5556 gfc_clear_ts (¤t_ts
);
5558 old_loc
= gfc_current_locus
;
5560 m
= gfc_match_prefix (¤t_ts
);
5563 gfc_current_locus
= old_loc
;
5567 if (gfc_match ("function% %n", name
) != MATCH_YES
)
5569 gfc_current_locus
= old_loc
;
5573 if (get_proc_name (name
, &sym
, false))
5576 if (add_hidden_procptr_result (sym
))
5579 if (current_attr
.module_procedure
)
5580 sym
->attr
.module_procedure
= 1;
5582 gfc_new_block
= sym
;
5584 m
= gfc_match_formal_arglist (sym
, 0, 0);
5587 gfc_error ("Expected formal argument list in function "
5588 "definition at %C");
5592 else if (m
== MATCH_ERROR
)
5597 /* According to the draft, the bind(c) and result clause can
5598 come in either order after the formal_arg_list (i.e., either
5599 can be first, both can exist together or by themselves or neither
5600 one). Therefore, the match_result can't match the end of the
5601 string, and check for the bind(c) or result clause in either order. */
5602 found_match
= gfc_match_eos ();
5604 /* Make sure that it isn't already declared as BIND(C). If it is, it
5605 must have been marked BIND(C) with a BIND(C) attribute and that is
5606 not allowed for procedures. */
5607 if (sym
->attr
.is_bind_c
== 1)
5609 sym
->attr
.is_bind_c
= 0;
5610 if (sym
->old_symbol
!= NULL
)
5611 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5612 "variables or common blocks",
5613 &(sym
->old_symbol
->declared_at
));
5615 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5616 "variables or common blocks", &gfc_current_locus
);
5619 if (found_match
!= MATCH_YES
)
5621 /* If we haven't found the end-of-statement, look for a suffix. */
5622 suffix_match
= gfc_match_suffix (sym
, &result
);
5623 if (suffix_match
== MATCH_YES
)
5624 /* Need to get the eos now. */
5625 found_match
= gfc_match_eos ();
5627 found_match
= suffix_match
;
5630 if(found_match
!= MATCH_YES
)
5634 /* Make changes to the symbol. */
5637 if (!gfc_add_function (&sym
->attr
, sym
->name
, NULL
))
5640 if (!gfc_missing_attr (&sym
->attr
, NULL
))
5643 if (!copy_prefix (&sym
->attr
, &sym
->declared_at
))
5645 if(!sym
->attr
.module_procedure
)
5651 /* Delay matching the function characteristics until after the
5652 specification block by signalling kind=-1. */
5653 sym
->declared_at
= old_loc
;
5654 if (current_ts
.type
!= BT_UNKNOWN
)
5655 current_ts
.kind
= -1;
5657 current_ts
.kind
= 0;
5661 if (current_ts
.type
!= BT_UNKNOWN
5662 && !gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
))
5668 if (current_ts
.type
!= BT_UNKNOWN
5669 && !gfc_add_type (result
, ¤t_ts
, &gfc_current_locus
))
5671 sym
->result
= result
;
5675 /* Warn if this procedure has the same name as an intrinsic. */
5676 do_warn_intrinsic_shadow (sym
, true);
5682 gfc_current_locus
= old_loc
;
5687 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
5688 pass the name of the entry, rather than the gfc_current_block name, and
5689 to return false upon finding an existing global entry. */
5692 add_global_entry (const char *name
, const char *binding_label
, bool sub
,
5696 enum gfc_symbol_type type
;
5698 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
5700 /* Only in Fortran 2003: For procedures with a binding label also the Fortran
5701 name is a global identifier. */
5702 if (!binding_label
|| gfc_notification_std (GFC_STD_F2008
))
5704 s
= gfc_get_gsymbol (name
);
5706 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5708 gfc_global_used (s
, where
);
5717 s
->ns
= gfc_current_ns
;
5721 /* Don't add the symbol multiple times. */
5723 && (!gfc_notification_std (GFC_STD_F2008
)
5724 || strcmp (name
, binding_label
) != 0))
5726 s
= gfc_get_gsymbol (binding_label
);
5728 if (s
->defined
|| (s
->type
!= GSYM_UNKNOWN
&& s
->type
!= type
))
5730 gfc_global_used (s
, where
);
5737 s
->binding_label
= binding_label
;
5740 s
->ns
= gfc_current_ns
;
5748 /* Match an ENTRY statement. */
5751 gfc_match_entry (void)
5756 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5757 gfc_compile_state state
;
5761 bool module_procedure
;
5765 m
= gfc_match_name (name
);
5769 if (!gfc_notify_std (GFC_STD_F2008_OBS
, "ENTRY statement at %C"))
5772 state
= gfc_current_state ();
5773 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
5778 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
5781 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
5783 case COMP_SUBMODULE
:
5784 gfc_error ("ENTRY statement at %C cannot appear within a SUBMODULE");
5786 case COMP_BLOCK_DATA
:
5787 gfc_error ("ENTRY statement at %C cannot appear within "
5790 case COMP_INTERFACE
:
5791 gfc_error ("ENTRY statement at %C cannot appear within "
5795 gfc_error ("ENTRY statement at %C cannot appear within "
5796 "a DERIVED TYPE block");
5799 gfc_error ("ENTRY statement at %C cannot appear within "
5800 "an IF-THEN block");
5803 case COMP_DO_CONCURRENT
:
5804 gfc_error ("ENTRY statement at %C cannot appear within "
5808 gfc_error ("ENTRY statement at %C cannot appear within "
5812 gfc_error ("ENTRY statement at %C cannot appear within "
5816 gfc_error ("ENTRY statement at %C cannot appear within "
5820 gfc_error ("ENTRY statement at %C cannot appear within "
5821 "a contained subprogram");
5824 gfc_error ("Unexpected ENTRY statement at %C");
5829 if ((state
== COMP_SUBROUTINE
|| state
== COMP_FUNCTION
)
5830 && gfc_state_stack
->previous
->state
== COMP_INTERFACE
)
5832 gfc_error ("ENTRY statement at %C cannot appear within an INTERFACE");
5836 module_procedure
= gfc_current_ns
->parent
!= NULL
5837 && gfc_current_ns
->parent
->proc_name
5838 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
5841 if (gfc_current_ns
->parent
!= NULL
5842 && gfc_current_ns
->parent
->proc_name
5843 && !module_procedure
)
5845 gfc_error("ENTRY statement at %C cannot appear in a "
5846 "contained procedure");
5850 /* Module function entries need special care in get_proc_name
5851 because previous references within the function will have
5852 created symbols attached to the current namespace. */
5853 if (get_proc_name (name
, &entry
,
5854 gfc_current_ns
->parent
!= NULL
5855 && module_procedure
))
5858 proc
= gfc_current_block ();
5860 /* Make sure that it isn't already declared as BIND(C). If it is, it
5861 must have been marked BIND(C) with a BIND(C) attribute and that is
5862 not allowed for procedures. */
5863 if (entry
->attr
.is_bind_c
== 1)
5865 entry
->attr
.is_bind_c
= 0;
5866 if (entry
->old_symbol
!= NULL
)
5867 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5868 "variables or common blocks",
5869 &(entry
->old_symbol
->declared_at
));
5871 gfc_error_now ("BIND(C) attribute at %L can only be used for "
5872 "variables or common blocks", &gfc_current_locus
);
5875 /* Check what next non-whitespace character is so we can tell if there
5876 is the required parens if we have a BIND(C). */
5877 old_loc
= gfc_current_locus
;
5878 gfc_gobble_whitespace ();
5879 peek_char
= gfc_peek_ascii_char ();
5881 if (state
== COMP_SUBROUTINE
)
5883 m
= gfc_match_formal_arglist (entry
, 0, 1);
5887 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
5888 never be an internal procedure. */
5889 is_bind_c
= gfc_match_bind_c (entry
, true);
5890 if (is_bind_c
== MATCH_ERROR
)
5892 if (is_bind_c
== MATCH_YES
)
5894 if (peek_char
!= '(')
5896 gfc_error ("Missing required parentheses before BIND(C) at %C");
5899 if (!gfc_add_is_bind_c (&(entry
->attr
), entry
->name
,
5900 &(entry
->declared_at
), 1))
5904 if (!gfc_current_ns
->parent
5905 && !add_global_entry (name
, entry
->binding_label
, true,
5909 /* An entry in a subroutine. */
5910 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5911 || !gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
))
5916 /* An entry in a function.
5917 We need to take special care because writing
5922 ENTRY f() RESULT (r)
5924 ENTRY f RESULT (r). */
5925 if (gfc_match_eos () == MATCH_YES
)
5927 gfc_current_locus
= old_loc
;
5928 /* Match the empty argument list, and add the interface to
5930 m
= gfc_match_formal_arglist (entry
, 0, 1);
5933 m
= gfc_match_formal_arglist (entry
, 0, 0);
5940 if (gfc_match_eos () == MATCH_YES
)
5942 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5943 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5946 entry
->result
= entry
;
5950 m
= gfc_match_suffix (entry
, &result
);
5952 gfc_syntax_error (ST_ENTRY
);
5958 if (!gfc_add_result (&result
->attr
, result
->name
, NULL
)
5959 || !gfc_add_entry (&entry
->attr
, result
->name
, NULL
)
5960 || !gfc_add_function (&entry
->attr
, result
->name
, NULL
))
5962 entry
->result
= result
;
5966 if (!gfc_add_entry (&entry
->attr
, entry
->name
, NULL
)
5967 || !gfc_add_function (&entry
->attr
, entry
->name
, NULL
))
5969 entry
->result
= entry
;
5973 if (!gfc_current_ns
->parent
5974 && !add_global_entry (name
, entry
->binding_label
, false,
5979 if (gfc_match_eos () != MATCH_YES
)
5981 gfc_syntax_error (ST_ENTRY
);
5985 entry
->attr
.recursive
= proc
->attr
.recursive
;
5986 entry
->attr
.elemental
= proc
->attr
.elemental
;
5987 entry
->attr
.pure
= proc
->attr
.pure
;
5989 el
= gfc_get_entry_list ();
5991 el
->next
= gfc_current_ns
->entries
;
5992 gfc_current_ns
->entries
= el
;
5994 el
->id
= el
->next
->id
+ 1;
5998 new_st
.op
= EXEC_ENTRY
;
5999 new_st
.ext
.entry
= el
;
6005 /* Match a subroutine statement, including optional prefixes. */
6008 gfc_match_subroutine (void)
6010 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6015 bool allow_binding_name
;
6017 if (gfc_current_state () != COMP_NONE
6018 && gfc_current_state () != COMP_INTERFACE
6019 && gfc_current_state () != COMP_CONTAINS
)
6022 m
= gfc_match_prefix (NULL
);
6026 m
= gfc_match ("subroutine% %n", name
);
6030 if (get_proc_name (name
, &sym
, false))
6033 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
6034 the symbol existed before. */
6035 sym
->declared_at
= gfc_current_locus
;
6037 if (current_attr
.module_procedure
)
6038 sym
->attr
.module_procedure
= 1;
6040 if (add_hidden_procptr_result (sym
))
6043 gfc_new_block
= sym
;
6045 /* Check what next non-whitespace character is so we can tell if there
6046 is the required parens if we have a BIND(C). */
6047 gfc_gobble_whitespace ();
6048 peek_char
= gfc_peek_ascii_char ();
6050 if (!gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
))
6053 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
6056 /* Make sure that it isn't already declared as BIND(C). If it is, it
6057 must have been marked BIND(C) with a BIND(C) attribute and that is
6058 not allowed for procedures. */
6059 if (sym
->attr
.is_bind_c
== 1)
6061 sym
->attr
.is_bind_c
= 0;
6062 if (sym
->old_symbol
!= NULL
)
6063 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6064 "variables or common blocks",
6065 &(sym
->old_symbol
->declared_at
));
6067 gfc_error_now ("BIND(C) attribute at %L can only be used for "
6068 "variables or common blocks", &gfc_current_locus
);
6071 /* C binding names are not allowed for internal procedures. */
6072 if (gfc_current_state () == COMP_CONTAINS
6073 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
6074 allow_binding_name
= false;
6076 allow_binding_name
= true;
6078 /* Here, we are just checking if it has the bind(c) attribute, and if
6079 so, then we need to make sure it's all correct. If it doesn't,
6080 we still need to continue matching the rest of the subroutine line. */
6081 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
6082 if (is_bind_c
== MATCH_ERROR
)
6084 /* There was an attempt at the bind(c), but it was wrong. An
6085 error message should have been printed w/in the gfc_match_bind_c
6086 so here we'll just return the MATCH_ERROR. */
6090 if (is_bind_c
== MATCH_YES
)
6092 /* The following is allowed in the Fortran 2008 draft. */
6093 if (gfc_current_state () == COMP_CONTAINS
6094 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
6095 && !gfc_notify_std (GFC_STD_F2008
, "BIND(C) attribute "
6096 "at %L may not be specified for an internal "
6097 "procedure", &gfc_current_locus
))
6100 if (peek_char
!= '(')
6102 gfc_error ("Missing required parentheses before BIND(C) at %C");
6105 if (!gfc_add_is_bind_c (&(sym
->attr
), sym
->name
,
6106 &(sym
->declared_at
), 1))
6110 if (gfc_match_eos () != MATCH_YES
)
6112 gfc_syntax_error (ST_SUBROUTINE
);
6116 if (!copy_prefix (&sym
->attr
, &sym
->declared_at
))
6118 if(!sym
->attr
.module_procedure
)
6124 /* Warn if it has the same name as an intrinsic. */
6125 do_warn_intrinsic_shadow (sym
, false);
6131 /* Check that the NAME identifier in a BIND attribute or statement
6132 is conform to C identifier rules. */
6135 check_bind_name_identifier (char **name
)
6137 char *n
= *name
, *p
;
6139 /* Remove leading spaces. */
6143 /* On an empty string, free memory and set name to NULL. */
6151 /* Remove trailing spaces. */
6152 p
= n
+ strlen(n
) - 1;
6156 /* Insert the identifier into the symbol table. */
6161 /* Now check that identifier is valid under C rules. */
6164 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6169 if (!(ISALNUM (*p
) || *p
== '_' || *p
== '$'))
6171 gfc_error ("Invalid C identifier in NAME= specifier at %C");
6179 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
6180 given, and set the binding label in either the given symbol (if not
6181 NULL), or in the current_ts. The symbol may be NULL because we may
6182 encounter the BIND(C) before the declaration itself. Return
6183 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
6184 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
6185 or MATCH_YES if the specifier was correct and the binding label and
6186 bind(c) fields were set correctly for the given symbol or the
6187 current_ts. If allow_binding_name is false, no binding name may be
6191 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
6193 char *binding_label
= NULL
;
6196 /* Initialize the flag that specifies whether we encountered a NAME=
6197 specifier or not. */
6198 has_name_equals
= 0;
6200 /* This much we have to be able to match, in this order, if
6201 there is a bind(c) label. */
6202 if (gfc_match (" bind ( c ") != MATCH_YES
)
6205 /* Now see if there is a binding label, or if we've reached the
6206 end of the bind(c) attribute without one. */
6207 if (gfc_match_char (',') == MATCH_YES
)
6209 if (gfc_match (" name = ") != MATCH_YES
)
6211 gfc_error ("Syntax error in NAME= specifier for binding label "
6213 /* should give an error message here */
6217 has_name_equals
= 1;
6219 if (gfc_match_init_expr (&e
) != MATCH_YES
)
6225 if (!gfc_simplify_expr(e
, 0))
6227 gfc_error ("NAME= specifier at %C should be a constant expression");
6232 if (e
->expr_type
!= EXPR_CONSTANT
|| e
->ts
.type
!= BT_CHARACTER
6233 || e
->ts
.kind
!= gfc_default_character_kind
|| e
->rank
!= 0)
6235 gfc_error ("NAME= specifier at %C should be a scalar of "
6236 "default character kind");
6241 // Get a C string from the Fortran string constant
6242 binding_label
= gfc_widechar_to_char (e
->value
.character
.string
,
6243 e
->value
.character
.length
);
6246 // Check that it is valid (old gfc_match_name_C)
6247 if (check_bind_name_identifier (&binding_label
) != MATCH_YES
)
6251 /* Get the required right paren. */
6252 if (gfc_match_char (')') != MATCH_YES
)
6254 gfc_error ("Missing closing paren for binding label at %C");
6258 if (has_name_equals
&& !allow_binding_name
)
6260 gfc_error ("No binding name is allowed in BIND(C) at %C");
6264 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
6266 gfc_error ("For dummy procedure %s, no binding name is "
6267 "allowed in BIND(C) at %C", sym
->name
);
6272 /* Save the binding label to the symbol. If sym is null, we're
6273 probably matching the typespec attributes of a declaration and
6274 haven't gotten the name yet, and therefore, no symbol yet. */
6278 sym
->binding_label
= binding_label
;
6280 curr_binding_label
= binding_label
;
6282 else if (allow_binding_name
)
6284 /* No binding label, but if symbol isn't null, we
6285 can set the label for it here.
6286 If name="" or allow_binding_name is false, no C binding name is
6288 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
6289 sym
->binding_label
= IDENTIFIER_POINTER (get_identifier (sym
->name
));
6292 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
6293 && current_interface
.type
== INTERFACE_ABSTRACT
)
6295 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
6303 /* Return nonzero if we're currently compiling a contained procedure. */
6306 contained_procedure (void)
6308 gfc_state_data
*s
= gfc_state_stack
;
6310 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
6311 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
6317 /* Set the kind of each enumerator. The kind is selected such that it is
6318 interoperable with the corresponding C enumeration type, making
6319 sure that -fshort-enums is honored. */
6324 enumerator_history
*current_history
= NULL
;
6328 if (max_enum
== NULL
|| enum_history
== NULL
)
6331 if (!flag_short_enums
)
6337 kind
= gfc_integer_kinds
[i
++].kind
;
6339 while (kind
< gfc_c_int_kind
6340 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
6343 current_history
= enum_history
;
6344 while (current_history
!= NULL
)
6346 current_history
->sym
->ts
.kind
= kind
;
6347 current_history
= current_history
->next
;
6352 /* Match any of the various end-block statements. Returns the type of
6353 END to the caller. The END INTERFACE, END IF, END DO, END SELECT
6354 and END BLOCK statements cannot be replaced by a single END statement. */
6357 gfc_match_end (gfc_statement
*st
)
6359 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6360 gfc_compile_state state
;
6362 const char *block_name
;
6366 gfc_namespace
*parent_ns
, *ns
, *prev_ns
;
6367 gfc_namespace
**nsp
;
6368 bool abreviated_modproc_decl
;
6369 bool got_matching_end
= false;
6371 old_loc
= gfc_current_locus
;
6372 if (gfc_match ("end") != MATCH_YES
)
6375 state
= gfc_current_state ();
6376 block_name
= gfc_current_block () == NULL
6377 ? NULL
: gfc_current_block ()->name
;
6381 case COMP_ASSOCIATE
:
6383 if (!strncmp (block_name
, "block@", strlen("block@")))
6388 case COMP_DERIVED_CONTAINS
:
6389 state
= gfc_state_stack
->previous
->state
;
6390 block_name
= gfc_state_stack
->previous
->sym
== NULL
6391 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
6398 abreviated_modproc_decl
6399 = gfc_current_block ()
6400 && gfc_current_block ()->abr_modproc_decl
;
6406 *st
= ST_END_PROGRAM
;
6407 target
= " program";
6411 case COMP_SUBROUTINE
:
6412 *st
= ST_END_SUBROUTINE
;
6413 if (!abreviated_modproc_decl
)
6414 target
= " subroutine";
6416 target
= " procedure";
6417 eos_ok
= !contained_procedure ();
6421 *st
= ST_END_FUNCTION
;
6422 if (!abreviated_modproc_decl
)
6423 target
= " function";
6425 target
= " procedure";
6426 eos_ok
= !contained_procedure ();
6429 case COMP_BLOCK_DATA
:
6430 *st
= ST_END_BLOCK_DATA
;
6431 target
= " block data";
6436 *st
= ST_END_MODULE
;
6441 case COMP_SUBMODULE
:
6442 *st
= ST_END_SUBMODULE
;
6443 target
= " submodule";
6447 case COMP_INTERFACE
:
6448 *st
= ST_END_INTERFACE
;
6449 target
= " interface";
6454 case COMP_DERIVED_CONTAINS
:
6460 case COMP_ASSOCIATE
:
6461 *st
= ST_END_ASSOCIATE
;
6462 target
= " associate";
6479 case COMP_DO_CONCURRENT
:
6486 *st
= ST_END_CRITICAL
;
6487 target
= " critical";
6492 case COMP_SELECT_TYPE
:
6493 *st
= ST_END_SELECT
;
6499 *st
= ST_END_FORALL
;
6514 last_initializer
= NULL
;
6516 gfc_free_enum_history ();
6520 gfc_error ("Unexpected END statement at %C");
6524 old_loc
= gfc_current_locus
;
6525 if (gfc_match_eos () == MATCH_YES
)
6527 if (!eos_ok
&& (*st
== ST_END_SUBROUTINE
|| *st
== ST_END_FUNCTION
))
6529 if (!gfc_notify_std (GFC_STD_F2008
, "END statement "
6530 "instead of %s statement at %L",
6531 abreviated_modproc_decl
? "END PROCEDURE"
6532 : gfc_ascii_statement(*st
), &old_loc
))
6537 /* We would have required END [something]. */
6538 gfc_error ("%s statement expected at %L",
6539 gfc_ascii_statement (*st
), &old_loc
);
6546 /* Verify that we've got the sort of end-block that we're expecting. */
6547 if (gfc_match (target
) != MATCH_YES
)
6549 gfc_error ("Expecting %s statement at %L", abreviated_modproc_decl
6550 ? "END PROCEDURE" : gfc_ascii_statement(*st
), &old_loc
);
6554 got_matching_end
= true;
6556 old_loc
= gfc_current_locus
;
6557 /* If we're at the end, make sure a block name wasn't required. */
6558 if (gfc_match_eos () == MATCH_YES
)
6561 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
6562 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
&& *st
!= ST_END_BLOCK
6563 && *st
!= ST_END_ASSOCIATE
&& *st
!= ST_END_CRITICAL
)
6569 gfc_error ("Expected block name of %qs in %s statement at %L",
6570 block_name
, gfc_ascii_statement (*st
), &old_loc
);
6575 /* END INTERFACE has a special handler for its several possible endings. */
6576 if (*st
== ST_END_INTERFACE
)
6577 return gfc_match_end_interface ();
6579 /* We haven't hit the end of statement, so what is left must be an
6581 m
= gfc_match_space ();
6583 m
= gfc_match_name (name
);
6586 gfc_error ("Expected terminating name at %C");
6590 if (block_name
== NULL
)
6593 /* We have to pick out the declared submodule name from the composite
6594 required by F2008:11.2.3 para 2, which ends in the declared name. */
6595 if (state
== COMP_SUBMODULE
)
6596 block_name
= strchr (block_name
, '.') + 1;
6598 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
6600 gfc_error ("Expected label %qs for %s statement at %C", block_name
,
6601 gfc_ascii_statement (*st
));
6604 /* Procedure pointer as function result. */
6605 else if (strcmp (block_name
, "ppr@") == 0
6606 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
6608 gfc_error ("Expected label %qs for %s statement at %C",
6609 gfc_current_block ()->ns
->proc_name
->name
,
6610 gfc_ascii_statement (*st
));
6614 if (gfc_match_eos () == MATCH_YES
)
6618 gfc_syntax_error (*st
);
6621 gfc_current_locus
= old_loc
;
6623 /* If we are missing an END BLOCK, we created a half-ready namespace.
6624 Remove it from the parent namespace's sibling list. */
6626 while (state
== COMP_BLOCK
&& !got_matching_end
)
6628 parent_ns
= gfc_current_ns
->parent
;
6630 nsp
= &(gfc_state_stack
->previous
->tail
->ext
.block
.ns
);
6636 if (ns
== gfc_current_ns
)
6638 if (prev_ns
== NULL
)
6641 prev_ns
->sibling
= ns
->sibling
;
6647 gfc_free_namespace (gfc_current_ns
);
6648 gfc_current_ns
= parent_ns
;
6649 gfc_state_stack
= gfc_state_stack
->previous
;
6650 state
= gfc_current_state ();
6658 /***************** Attribute declaration statements ****************/
6660 /* Set the attribute of a single variable. */
6665 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6668 /* Workaround -Wmaybe-uninitialized false positive during
6669 profiledbootstrap by initializing them. */
6670 gfc_symbol
*sym
= NULL
;
6676 m
= gfc_match_name (name
);
6680 if (find_special (name
, &sym
, false))
6683 if (!check_function_name (name
))
6689 var_locus
= gfc_current_locus
;
6691 /* Deal with possible array specification for certain attributes. */
6692 if (current_attr
.dimension
6693 || current_attr
.codimension
6694 || current_attr
.allocatable
6695 || current_attr
.pointer
6696 || current_attr
.target
)
6698 m
= gfc_match_array_spec (&as
, !current_attr
.codimension
,
6699 !current_attr
.dimension
6700 && !current_attr
.pointer
6701 && !current_attr
.target
);
6702 if (m
== MATCH_ERROR
)
6705 if (current_attr
.dimension
&& m
== MATCH_NO
)
6707 gfc_error ("Missing array specification at %L in DIMENSION "
6708 "statement", &var_locus
);
6713 if (current_attr
.dimension
&& sym
->value
)
6715 gfc_error ("Dimensions specified for %s at %L after its "
6716 "initialisation", sym
->name
, &var_locus
);
6721 if (current_attr
.codimension
&& m
== MATCH_NO
)
6723 gfc_error ("Missing array specification at %L in CODIMENSION "
6724 "statement", &var_locus
);
6729 if ((current_attr
.allocatable
|| current_attr
.pointer
)
6730 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
6732 gfc_error ("Array specification must be deferred at %L", &var_locus
);
6738 /* Update symbol table. DIMENSION attribute is set in
6739 gfc_set_array_spec(). For CLASS variables, this must be applied
6740 to the first component, or '_data' field. */
6741 if (sym
->ts
.type
== BT_CLASS
&& sym
->ts
.u
.derived
->attr
.is_class
)
6743 if (!gfc_copy_attr (&CLASS_DATA(sym
)->attr
, ¤t_attr
, &var_locus
))
6751 if (current_attr
.dimension
== 0 && current_attr
.codimension
== 0
6752 && !gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
))
6759 if (sym
->ts
.type
== BT_CLASS
6760 && !gfc_build_class_symbol (&sym
->ts
, &sym
->attr
, &sym
->as
))
6766 if (!gfc_set_array_spec (sym
, as
, &var_locus
))
6772 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
6774 /* Fix the array spec. */
6775 m
= gfc_mod_pointee_as (sym
->as
);
6776 if (m
== MATCH_ERROR
)
6780 if (!gfc_add_attribute (&sym
->attr
, &var_locus
))
6786 if ((current_attr
.external
|| current_attr
.intrinsic
)
6787 && sym
->attr
.flavor
!= FL_PROCEDURE
6788 && !gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
))
6794 add_hidden_procptr_result (sym
);
6799 gfc_free_array_spec (as
);
6804 /* Generic attribute declaration subroutine. Used for attributes that
6805 just have a list of names. */
6812 /* Gobble the optional double colon, by simply ignoring the result
6822 if (gfc_match_eos () == MATCH_YES
)
6828 if (gfc_match_char (',') != MATCH_YES
)
6830 gfc_error ("Unexpected character in variable list at %C");
6840 /* This routine matches Cray Pointer declarations of the form:
6841 pointer ( <pointer>, <pointee> )
6843 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
6844 The pointer, if already declared, should be an integer. Otherwise, we
6845 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
6846 be either a scalar, or an array declaration. No space is allocated for
6847 the pointee. For the statement
6848 pointer (ipt, ar(10))
6849 any subsequent uses of ar will be translated (in C-notation) as
6850 ar(i) => ((<type> *) ipt)(i)
6851 After gimplification, pointee variable will disappear in the code. */
6854 cray_pointer_decl (void)
6857 gfc_array_spec
*as
= NULL
;
6858 gfc_symbol
*cptr
; /* Pointer symbol. */
6859 gfc_symbol
*cpte
; /* Pointee symbol. */
6865 if (gfc_match_char ('(') != MATCH_YES
)
6867 gfc_error ("Expected %<(%> at %C");
6871 /* Match pointer. */
6872 var_locus
= gfc_current_locus
;
6873 gfc_clear_attr (¤t_attr
);
6874 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
6875 current_ts
.type
= BT_INTEGER
;
6876 current_ts
.kind
= gfc_index_integer_kind
;
6878 m
= gfc_match_symbol (&cptr
, 0);
6881 gfc_error ("Expected variable name at %C");
6885 if (!gfc_add_cray_pointer (&cptr
->attr
, &var_locus
))
6888 gfc_set_sym_referenced (cptr
);
6890 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
6892 cptr
->ts
.type
= BT_INTEGER
;
6893 cptr
->ts
.kind
= gfc_index_integer_kind
;
6895 else if (cptr
->ts
.type
!= BT_INTEGER
)
6897 gfc_error ("Cray pointer at %C must be an integer");
6900 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
6901 gfc_warning (0, "Cray pointer at %C has %d bytes of precision;"
6902 " memory addresses require %d bytes",
6903 cptr
->ts
.kind
, gfc_index_integer_kind
);
6905 if (gfc_match_char (',') != MATCH_YES
)
6907 gfc_error ("Expected \",\" at %C");
6911 /* Match Pointee. */
6912 var_locus
= gfc_current_locus
;
6913 gfc_clear_attr (¤t_attr
);
6914 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
6915 current_ts
.type
= BT_UNKNOWN
;
6916 current_ts
.kind
= 0;
6918 m
= gfc_match_symbol (&cpte
, 0);
6921 gfc_error ("Expected variable name at %C");
6925 /* Check for an optional array spec. */
6926 m
= gfc_match_array_spec (&as
, true, false);
6927 if (m
== MATCH_ERROR
)
6929 gfc_free_array_spec (as
);
6932 else if (m
== MATCH_NO
)
6934 gfc_free_array_spec (as
);
6938 if (!gfc_add_cray_pointee (&cpte
->attr
, &var_locus
))
6941 gfc_set_sym_referenced (cpte
);
6943 if (cpte
->as
== NULL
)
6945 if (!gfc_set_array_spec (cpte
, as
, &var_locus
))
6946 gfc_internal_error ("Couldn't set Cray pointee array spec.");
6948 else if (as
!= NULL
)
6950 gfc_error ("Duplicate array spec for Cray pointee at %C");
6951 gfc_free_array_spec (as
);
6957 if (cpte
->as
!= NULL
)
6959 /* Fix array spec. */
6960 m
= gfc_mod_pointee_as (cpte
->as
);
6961 if (m
== MATCH_ERROR
)
6965 /* Point the Pointee at the Pointer. */
6966 cpte
->cp_pointer
= cptr
;
6968 if (gfc_match_char (')') != MATCH_YES
)
6970 gfc_error ("Expected \")\" at %C");
6973 m
= gfc_match_char (',');
6975 done
= true; /* Stop searching for more declarations. */
6979 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
6980 || gfc_match_eos () != MATCH_YES
)
6982 gfc_error ("Expected %<,%> or end of statement at %C");
6990 gfc_match_external (void)
6993 gfc_clear_attr (¤t_attr
);
6994 current_attr
.external
= 1;
6996 return attr_decl ();
7001 gfc_match_intent (void)
7005 /* This is not allowed within a BLOCK construct! */
7006 if (gfc_current_state () == COMP_BLOCK
)
7008 gfc_error ("INTENT is not allowed inside of BLOCK at %C");
7012 intent
= match_intent_spec ();
7013 if (intent
== INTENT_UNKNOWN
)
7016 gfc_clear_attr (¤t_attr
);
7017 current_attr
.intent
= intent
;
7019 return attr_decl ();
7024 gfc_match_intrinsic (void)
7027 gfc_clear_attr (¤t_attr
);
7028 current_attr
.intrinsic
= 1;
7030 return attr_decl ();
7035 gfc_match_optional (void)
7037 /* This is not allowed within a BLOCK construct! */
7038 if (gfc_current_state () == COMP_BLOCK
)
7040 gfc_error ("OPTIONAL is not allowed inside of BLOCK at %C");
7044 gfc_clear_attr (¤t_attr
);
7045 current_attr
.optional
= 1;
7047 return attr_decl ();
7052 gfc_match_pointer (void)
7054 gfc_gobble_whitespace ();
7055 if (gfc_peek_ascii_char () == '(')
7057 if (!flag_cray_pointer
)
7059 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
7063 return cray_pointer_decl ();
7067 gfc_clear_attr (¤t_attr
);
7068 current_attr
.pointer
= 1;
7070 return attr_decl ();
7076 gfc_match_allocatable (void)
7078 gfc_clear_attr (¤t_attr
);
7079 current_attr
.allocatable
= 1;
7081 return attr_decl ();
7086 gfc_match_codimension (void)
7088 gfc_clear_attr (¤t_attr
);
7089 current_attr
.codimension
= 1;
7091 return attr_decl ();
7096 gfc_match_contiguous (void)
7098 if (!gfc_notify_std (GFC_STD_F2008
, "CONTIGUOUS statement at %C"))
7101 gfc_clear_attr (¤t_attr
);
7102 current_attr
.contiguous
= 1;
7104 return attr_decl ();
7109 gfc_match_dimension (void)
7111 gfc_clear_attr (¤t_attr
);
7112 current_attr
.dimension
= 1;
7114 return attr_decl ();
7119 gfc_match_target (void)
7121 gfc_clear_attr (¤t_attr
);
7122 current_attr
.target
= 1;
7124 return attr_decl ();
7128 /* Match the list of entities being specified in a PUBLIC or PRIVATE
7132 access_attr_decl (gfc_statement st
)
7134 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7135 interface_type type
;
7137 gfc_symbol
*sym
, *dt_sym
;
7138 gfc_intrinsic_op op
;
7141 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7146 m
= gfc_match_generic_spec (&type
, name
, &op
);
7149 if (m
== MATCH_ERROR
)
7154 case INTERFACE_NAMELESS
:
7155 case INTERFACE_ABSTRACT
:
7158 case INTERFACE_GENERIC
:
7159 if (gfc_get_symbol (name
, NULL
, &sym
))
7162 if (!gfc_add_access (&sym
->attr
,
7164 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
7168 if (sym
->attr
.generic
&& (dt_sym
= gfc_find_dt_in_generic (sym
))
7169 && !gfc_add_access (&dt_sym
->attr
,
7171 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
7177 case INTERFACE_INTRINSIC_OP
:
7178 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
7180 gfc_intrinsic_op other_op
;
7182 gfc_current_ns
->operator_access
[op
] =
7183 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
7185 /* Handle the case if there is another op with the same
7186 function, for INTRINSIC_EQ vs. INTRINSIC_EQ_OS and so on. */
7187 other_op
= gfc_equivalent_op (op
);
7189 if (other_op
!= INTRINSIC_NONE
)
7190 gfc_current_ns
->operator_access
[other_op
] =
7191 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
7196 gfc_error ("Access specification of the %s operator at %C has "
7197 "already been specified", gfc_op2string (op
));
7203 case INTERFACE_USER_OP
:
7204 uop
= gfc_get_uop (name
);
7206 if (uop
->access
== ACCESS_UNKNOWN
)
7208 uop
->access
= (st
== ST_PUBLIC
)
7209 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
7213 gfc_error ("Access specification of the .%s. operator at %C "
7214 "has already been specified", sym
->name
);
7221 if (gfc_match_char (',') == MATCH_NO
)
7225 if (gfc_match_eos () != MATCH_YES
)
7230 gfc_syntax_error (st
);
7238 gfc_match_protected (void)
7243 if (!gfc_current_ns
->proc_name
7244 || gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
7246 gfc_error ("PROTECTED at %C only allowed in specification "
7247 "part of a module");
7252 if (!gfc_notify_std (GFC_STD_F2003
, "PROTECTED statement at %C"))
7255 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7260 if (gfc_match_eos () == MATCH_YES
)
7265 m
= gfc_match_symbol (&sym
, 0);
7269 if (!gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7281 if (gfc_match_eos () == MATCH_YES
)
7283 if (gfc_match_char (',') != MATCH_YES
)
7290 gfc_error ("Syntax error in PROTECTED statement at %C");
7295 /* The PRIVATE statement is a bit weird in that it can be an attribute
7296 declaration, but also works as a standalone statement inside of a
7297 type declaration or a module. */
7300 gfc_match_private (gfc_statement
*st
)
7303 if (gfc_match ("private") != MATCH_YES
)
7306 if (gfc_current_state () != COMP_MODULE
7307 && !(gfc_current_state () == COMP_DERIVED
7308 && gfc_state_stack
->previous
7309 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
7310 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
7311 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
7312 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
7314 gfc_error ("PRIVATE statement at %C is only allowed in the "
7315 "specification part of a module");
7319 if (gfc_current_state () == COMP_DERIVED
)
7321 if (gfc_match_eos () == MATCH_YES
)
7327 gfc_syntax_error (ST_PRIVATE
);
7331 if (gfc_match_eos () == MATCH_YES
)
7338 return access_attr_decl (ST_PRIVATE
);
7343 gfc_match_public (gfc_statement
*st
)
7346 if (gfc_match ("public") != MATCH_YES
)
7349 if (gfc_current_state () != COMP_MODULE
)
7351 gfc_error ("PUBLIC statement at %C is only allowed in the "
7352 "specification part of a module");
7356 if (gfc_match_eos () == MATCH_YES
)
7363 return access_attr_decl (ST_PUBLIC
);
7367 /* Workhorse for gfc_match_parameter. */
7377 m
= gfc_match_symbol (&sym
, 0);
7379 gfc_error ("Expected variable name at %C in PARAMETER statement");
7384 if (gfc_match_char ('=') == MATCH_NO
)
7386 gfc_error ("Expected = sign in PARAMETER statement at %C");
7390 m
= gfc_match_init_expr (&init
);
7392 gfc_error ("Expected expression at %C in PARAMETER statement");
7396 if (sym
->ts
.type
== BT_UNKNOWN
7397 && !gfc_set_default_type (sym
, 1, NULL
))
7403 if (!gfc_check_assign_symbol (sym
, NULL
, init
)
7404 || !gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
))
7412 gfc_error ("Initializing already initialized variable at %C");
7417 t
= add_init_expr_to_sym (sym
->name
, &init
, &gfc_current_locus
);
7418 return (t
) ? MATCH_YES
: MATCH_ERROR
;
7421 gfc_free_expr (init
);
7426 /* Match a parameter statement, with the weird syntax that these have. */
7429 gfc_match_parameter (void)
7433 if (gfc_match_char ('(') == MATCH_NO
)
7442 if (gfc_match (" )%t") == MATCH_YES
)
7445 if (gfc_match_char (',') != MATCH_YES
)
7447 gfc_error ("Unexpected characters in PARAMETER statement at %C");
7457 /* Save statements have a special syntax. */
7460 gfc_match_save (void)
7462 char n
[GFC_MAX_SYMBOL_LEN
+1];
7467 if (gfc_match_eos () == MATCH_YES
)
7469 if (gfc_current_ns
->seen_save
)
7471 if (!gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
7472 "follows previous SAVE statement"))
7476 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
7480 if (gfc_current_ns
->save_all
)
7482 if (!gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
7483 "blanket SAVE statement"))
7491 m
= gfc_match_symbol (&sym
, 0);
7495 if (!gfc_add_save (&sym
->attr
, SAVE_EXPLICIT
, sym
->name
,
7496 &gfc_current_locus
))
7507 m
= gfc_match (" / %n /", &n
);
7508 if (m
== MATCH_ERROR
)
7513 c
= gfc_get_common (n
, 0);
7516 gfc_current_ns
->seen_save
= 1;
7519 if (gfc_match_eos () == MATCH_YES
)
7521 if (gfc_match_char (',') != MATCH_YES
)
7528 gfc_error ("Syntax error in SAVE statement at %C");
7534 gfc_match_value (void)
7539 /* This is not allowed within a BLOCK construct! */
7540 if (gfc_current_state () == COMP_BLOCK
)
7542 gfc_error ("VALUE is not allowed inside of BLOCK at %C");
7546 if (!gfc_notify_std (GFC_STD_F2003
, "VALUE statement at %C"))
7549 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7554 if (gfc_match_eos () == MATCH_YES
)
7559 m
= gfc_match_symbol (&sym
, 0);
7563 if (!gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7575 if (gfc_match_eos () == MATCH_YES
)
7577 if (gfc_match_char (',') != MATCH_YES
)
7584 gfc_error ("Syntax error in VALUE statement at %C");
7590 gfc_match_volatile (void)
7595 if (!gfc_notify_std (GFC_STD_F2003
, "VOLATILE statement at %C"))
7598 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7603 if (gfc_match_eos () == MATCH_YES
)
7608 /* VOLATILE is special because it can be added to host-associated
7609 symbols locally. Except for coarrays. */
7610 m
= gfc_match_symbol (&sym
, 1);
7614 /* F2008, C560+C561. VOLATILE for host-/use-associated variable or
7615 for variable in a BLOCK which is defined outside of the BLOCK. */
7616 if (sym
->ns
!= gfc_current_ns
&& sym
->attr
.codimension
)
7618 gfc_error ("Specifying VOLATILE for coarray variable %qs at "
7619 "%C, which is use-/host-associated", sym
->name
);
7622 if (!gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7634 if (gfc_match_eos () == MATCH_YES
)
7636 if (gfc_match_char (',') != MATCH_YES
)
7643 gfc_error ("Syntax error in VOLATILE statement at %C");
7649 gfc_match_asynchronous (void)
7654 if (!gfc_notify_std (GFC_STD_F2003
, "ASYNCHRONOUS statement at %C"))
7657 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
7662 if (gfc_match_eos () == MATCH_YES
)
7667 /* ASYNCHRONOUS is special because it can be added to host-associated
7669 m
= gfc_match_symbol (&sym
, 1);
7673 if (!gfc_add_asynchronous (&sym
->attr
, sym
->name
, &gfc_current_locus
))
7685 if (gfc_match_eos () == MATCH_YES
)
7687 if (gfc_match_char (',') != MATCH_YES
)
7694 gfc_error ("Syntax error in ASYNCHRONOUS statement at %C");
7699 /* Match a module procedure statement in a submodule. */
7702 gfc_match_submod_proc (void)
7704 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7705 gfc_symbol
*sym
, *fsym
;
7707 gfc_formal_arglist
*formal
, *head
, *tail
;
7709 if (gfc_current_state () != COMP_CONTAINS
7710 || !(gfc_state_stack
->previous
7711 && (gfc_state_stack
->previous
->state
== COMP_SUBMODULE
7712 || gfc_state_stack
->previous
->state
== COMP_MODULE
)))
7715 m
= gfc_match (" module% procedure% %n", name
);
7719 if (!gfc_notify_std (GFC_STD_F2008
, "MODULE PROCEDURE declaration "
7723 if (get_proc_name (name
, &sym
, false))
7726 /* Make sure that the result field is appropriately filled, even though
7727 the result symbol will be replaced later on. */
7728 if (sym
->ts
.interface
&& sym
->ts
.interface
->attr
.function
)
7730 if (sym
->ts
.interface
->result
7731 && sym
->ts
.interface
->result
!= sym
->ts
.interface
)
7732 sym
->result
= sym
->ts
.interface
->result
;
7737 /* Set declared_at as it might point to, e.g., a PUBLIC statement, if
7738 the symbol existed before. */
7739 sym
->declared_at
= gfc_current_locus
;
7741 if (!sym
->attr
.module_procedure
)
7744 /* Signal match_end to expect "end procedure". */
7745 sym
->abr_modproc_decl
= 1;
7747 /* Change from IFSRC_IFBODY coming from the interface declaration. */
7748 sym
->attr
.if_source
= IFSRC_DECL
;
7750 gfc_new_block
= sym
;
7752 /* Make a new formal arglist with the symbols in the procedure
7755 for (formal
= sym
->formal
; formal
&& formal
->sym
; formal
= formal
->next
)
7757 if (formal
== sym
->formal
)
7758 head
= tail
= gfc_get_formal_arglist ();
7761 tail
->next
= gfc_get_formal_arglist ();
7765 if (gfc_copy_dummy_sym (&fsym
, formal
->sym
, 0))
7769 gfc_set_sym_referenced (fsym
);
7772 /* The dummy symbols get cleaned up, when the formal_namespace of the
7773 interface declaration is cleared. This allows us to add the
7774 explicit interface as is done for other type of procedure. */
7775 if (!gfc_add_explicit_interface (sym
, IFSRC_DECL
, head
,
7776 &gfc_current_locus
))
7779 if (gfc_match_eos () != MATCH_YES
)
7781 gfc_syntax_error (ST_MODULE_PROC
);
7788 gfc_free_formal_arglist (head
);
7793 /* Match a module procedure statement. Note that we have to modify
7794 symbols in the parent's namespace because the current one was there
7795 to receive symbols that are in an interface's formal argument list. */
7798 gfc_match_modproc (void)
7800 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7804 gfc_namespace
*module_ns
;
7805 gfc_interface
*old_interface_head
, *interface
;
7807 if (gfc_state_stack
->state
!= COMP_INTERFACE
7808 || gfc_state_stack
->previous
== NULL
7809 || current_interface
.type
== INTERFACE_NAMELESS
7810 || current_interface
.type
== INTERFACE_ABSTRACT
)
7812 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
7817 module_ns
= gfc_current_ns
->parent
;
7818 for (; module_ns
; module_ns
= module_ns
->parent
)
7819 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
7820 || module_ns
->proc_name
->attr
.flavor
== FL_PROGRAM
7821 || (module_ns
->proc_name
->attr
.flavor
== FL_PROCEDURE
7822 && !module_ns
->proc_name
->attr
.contained
))
7825 if (module_ns
== NULL
)
7828 /* Store the current state of the interface. We will need it if we
7829 end up with a syntax error and need to recover. */
7830 old_interface_head
= gfc_current_interface_head ();
7832 /* Check if the F2008 optional double colon appears. */
7833 gfc_gobble_whitespace ();
7834 old_locus
= gfc_current_locus
;
7835 if (gfc_match ("::") == MATCH_YES
)
7837 if (!gfc_notify_std (GFC_STD_F2008
, "double colon in "
7838 "MODULE PROCEDURE statement at %L", &old_locus
))
7842 gfc_current_locus
= old_locus
;
7847 old_locus
= gfc_current_locus
;
7849 m
= gfc_match_name (name
);
7855 /* Check for syntax error before starting to add symbols to the
7856 current namespace. */
7857 if (gfc_match_eos () == MATCH_YES
)
7860 if (!last
&& gfc_match_char (',') != MATCH_YES
)
7863 /* Now we're sure the syntax is valid, we process this item
7865 if (gfc_get_symbol (name
, module_ns
, &sym
))
7868 if (sym
->attr
.intrinsic
)
7870 gfc_error ("Intrinsic procedure at %L cannot be a MODULE "
7871 "PROCEDURE", &old_locus
);
7875 if (sym
->attr
.proc
!= PROC_MODULE
7876 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
7879 if (!gfc_add_interface (sym
))
7882 sym
->attr
.mod_proc
= 1;
7883 sym
->declared_at
= old_locus
;
7892 /* Restore the previous state of the interface. */
7893 interface
= gfc_current_interface_head ();
7894 gfc_set_current_interface_head (old_interface_head
);
7896 /* Free the new interfaces. */
7897 while (interface
!= old_interface_head
)
7899 gfc_interface
*i
= interface
->next
;
7904 /* And issue a syntax error. */
7905 gfc_syntax_error (ST_MODULE_PROC
);
7910 /* Check a derived type that is being extended. */
7913 check_extended_derived_type (char *name
)
7915 gfc_symbol
*extended
;
7917 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
7919 gfc_error ("Ambiguous symbol in TYPE definition at %C");
7923 extended
= gfc_find_dt_in_generic (extended
);
7928 gfc_error ("Symbol %qs at %C has not been previously defined", name
);
7932 if (extended
->attr
.flavor
!= FL_DERIVED
)
7934 gfc_error ("%qs in EXTENDS expression at %C is not a "
7935 "derived type", name
);
7939 if (extended
->attr
.is_bind_c
)
7941 gfc_error ("%qs cannot be extended at %C because it "
7942 "is BIND(C)", extended
->name
);
7946 if (extended
->attr
.sequence
)
7948 gfc_error ("%qs cannot be extended at %C because it "
7949 "is a SEQUENCE type", extended
->name
);
7957 /* Match the optional attribute specifiers for a type declaration.
7958 Return MATCH_ERROR if an error is encountered in one of the handled
7959 attributes (public, private, bind(c)), MATCH_NO if what's found is
7960 not a handled attribute, and MATCH_YES otherwise. TODO: More error
7961 checking on attribute conflicts needs to be done. */
7964 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
7966 /* See if the derived type is marked as private. */
7967 if (gfc_match (" , private") == MATCH_YES
)
7969 if (gfc_current_state () != COMP_MODULE
)
7971 gfc_error ("Derived type at %C can only be PRIVATE in the "
7972 "specification part of a module");
7976 if (!gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
))
7979 else if (gfc_match (" , public") == MATCH_YES
)
7981 if (gfc_current_state () != COMP_MODULE
)
7983 gfc_error ("Derived type at %C can only be PUBLIC in the "
7984 "specification part of a module");
7988 if (!gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
))
7991 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
7993 /* If the type is defined to be bind(c) it then needs to make
7994 sure that all fields are interoperable. This will
7995 need to be a semantic check on the finished derived type.
7996 See 15.2.3 (lines 9-12) of F2003 draft. */
7997 if (!gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0))
8000 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
8002 else if (gfc_match (" , abstract") == MATCH_YES
)
8004 if (!gfc_notify_std (GFC_STD_F2003
, "ABSTRACT type at %C"))
8007 if (!gfc_add_abstract (attr
, &gfc_current_locus
))
8010 else if (name
&& gfc_match (" , extends ( %n )", name
) == MATCH_YES
)
8012 if (!gfc_add_extension (attr
, &gfc_current_locus
))
8018 /* If we get here, something matched. */
8023 /* Match the beginning of a derived type declaration. If a type name
8024 was the result of a function, then it is possible to have a symbol
8025 already to be known as a derived type yet have no components. */
8028 gfc_match_derived_decl (void)
8030 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8031 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
8032 symbol_attribute attr
;
8033 gfc_symbol
*sym
, *gensym
;
8034 gfc_symbol
*extended
;
8036 match is_type_attr_spec
= MATCH_NO
;
8037 bool seen_attr
= false;
8038 gfc_interface
*intr
= NULL
, *head
;
8040 if (gfc_current_state () == COMP_DERIVED
)
8045 gfc_clear_attr (&attr
);
8050 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
8051 if (is_type_attr_spec
== MATCH_ERROR
)
8053 if (is_type_attr_spec
== MATCH_YES
)
8055 } while (is_type_attr_spec
== MATCH_YES
);
8057 /* Deal with derived type extensions. The extension attribute has
8058 been added to 'attr' but now the parent type must be found and
8061 extended
= check_extended_derived_type (parent
);
8063 if (parent
[0] && !extended
)
8066 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
8068 gfc_error ("Expected :: in TYPE definition at %C");
8072 m
= gfc_match (" %n%t", name
);
8076 /* Make sure the name is not the name of an intrinsic type. */
8077 if (gfc_is_intrinsic_typename (name
))
8079 gfc_error ("Type name %qs at %C cannot be the same as an intrinsic "
8084 if (gfc_get_symbol (name
, NULL
, &gensym
))
8087 if (!gensym
->attr
.generic
&& gensym
->ts
.type
!= BT_UNKNOWN
)
8089 gfc_error ("Derived type name %qs at %C already has a basic type "
8090 "of %s", gensym
->name
, gfc_typename (&gensym
->ts
));
8094 if (!gensym
->attr
.generic
8095 && !gfc_add_generic (&gensym
->attr
, gensym
->name
, NULL
))
8098 if (!gensym
->attr
.function
8099 && !gfc_add_function (&gensym
->attr
, gensym
->name
, NULL
))
8102 sym
= gfc_find_dt_in_generic (gensym
);
8104 if (sym
&& (sym
->components
!= NULL
|| sym
->attr
.zero_comp
))
8106 gfc_error ("Derived type definition of %qs at %C has already been "
8107 "defined", sym
->name
);
8113 /* Use upper case to save the actual derived-type symbol. */
8114 gfc_get_symbol (gfc_get_string ("%c%s",
8115 (char) TOUPPER ((unsigned char) gensym
->name
[0]),
8116 &gensym
->name
[1]), NULL
, &sym
);
8117 sym
->name
= gfc_get_string (gensym
->name
);
8118 head
= gensym
->generic
;
8119 intr
= gfc_get_interface ();
8121 intr
->where
= gfc_current_locus
;
8122 intr
->sym
->declared_at
= gfc_current_locus
;
8124 gensym
->generic
= intr
;
8125 gensym
->attr
.if_source
= IFSRC_DECL
;
8128 /* The symbol may already have the derived attribute without the
8129 components. The ways this can happen is via a function
8130 definition, an INTRINSIC statement or a subtype in another
8131 derived type that is a pointer. The first part of the AND clause
8132 is true if the symbol is not the return value of a function. */
8133 if (sym
->attr
.flavor
!= FL_DERIVED
8134 && !gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
))
8137 if (attr
.access
!= ACCESS_UNKNOWN
8138 && !gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
))
8140 else if (sym
->attr
.access
== ACCESS_UNKNOWN
8141 && gensym
->attr
.access
!= ACCESS_UNKNOWN
8142 && !gfc_add_access (&sym
->attr
, gensym
->attr
.access
,
8146 if (sym
->attr
.access
!= ACCESS_UNKNOWN
8147 && gensym
->attr
.access
== ACCESS_UNKNOWN
)
8148 gensym
->attr
.access
= sym
->attr
.access
;
8150 /* See if the derived type was labeled as bind(c). */
8151 if (attr
.is_bind_c
!= 0)
8152 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
8154 /* Construct the f2k_derived namespace if it is not yet there. */
8155 if (!sym
->f2k_derived
)
8156 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
8158 if (extended
&& !sym
->components
)
8162 /* Add the extended derived type as the first component. */
8163 gfc_add_component (sym
, parent
, &p
);
8165 gfc_set_sym_referenced (extended
);
8167 p
->ts
.type
= BT_DERIVED
;
8168 p
->ts
.u
.derived
= extended
;
8169 p
->initializer
= gfc_default_initializer (&p
->ts
);
8171 /* Set extension level. */
8172 if (extended
->attr
.extension
== 255)
8174 /* Since the extension field is 8 bit wide, we can only have
8175 up to 255 extension levels. */
8176 gfc_error ("Maximum extension level reached with type %qs at %L",
8177 extended
->name
, &extended
->declared_at
);
8180 sym
->attr
.extension
= extended
->attr
.extension
+ 1;
8182 /* Provide the links between the extended type and its extension. */
8183 if (!extended
->f2k_derived
)
8184 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
8187 if (!sym
->hash_value
)
8188 /* Set the hash for the compound name for this type. */
8189 sym
->hash_value
= gfc_hash_value (sym
);
8191 /* Take over the ABSTRACT attribute. */
8192 sym
->attr
.abstract
= attr
.abstract
;
8194 gfc_new_block
= sym
;
8200 /* Cray Pointees can be declared as:
8201 pointer (ipt, a (n,m,...,*)) */
8204 gfc_mod_pointee_as (gfc_array_spec
*as
)
8206 as
->cray_pointee
= true; /* This will be useful to know later. */
8207 if (as
->type
== AS_ASSUMED_SIZE
)
8208 as
->cp_was_assumed
= true;
8209 else if (as
->type
== AS_ASSUMED_SHAPE
)
8211 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
8218 /* Match the enum definition statement, here we are trying to match
8219 the first line of enum definition statement.
8220 Returns MATCH_YES if match is found. */
8223 gfc_match_enum (void)
8227 m
= gfc_match_eos ();
8231 if (!gfc_notify_std (GFC_STD_F2003
, "ENUM and ENUMERATOR at %C"))
8238 /* Returns an initializer whose value is one higher than the value of the
8239 LAST_INITIALIZER argument. If the argument is NULL, the
8240 initializers value will be set to zero. The initializer's kind
8241 will be set to gfc_c_int_kind.
8243 If -fshort-enums is given, the appropriate kind will be selected
8244 later after all enumerators have been parsed. A warning is issued
8245 here if an initializer exceeds gfc_c_int_kind. */
8248 enum_initializer (gfc_expr
*last_initializer
, locus where
)
8251 result
= gfc_get_constant_expr (BT_INTEGER
, gfc_c_int_kind
, &where
);
8253 mpz_init (result
->value
.integer
);
8255 if (last_initializer
!= NULL
)
8257 mpz_add_ui (result
->value
.integer
, last_initializer
->value
.integer
, 1);
8258 result
->where
= last_initializer
->where
;
8260 if (gfc_check_integer_range (result
->value
.integer
,
8261 gfc_c_int_kind
) != ARITH_OK
)
8263 gfc_error ("Enumerator exceeds the C integer type at %C");
8269 /* Control comes here, if it's the very first enumerator and no
8270 initializer has been given. It will be initialized to zero. */
8271 mpz_set_si (result
->value
.integer
, 0);
8278 /* Match a variable name with an optional initializer. When this
8279 subroutine is called, a variable is expected to be parsed next.
8280 Depending on what is happening at the moment, updates either the
8281 symbol table or the current interface. */
8284 enumerator_decl (void)
8286 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8287 gfc_expr
*initializer
;
8288 gfc_array_spec
*as
= NULL
;
8296 old_locus
= gfc_current_locus
;
8298 /* When we get here, we've just matched a list of attributes and
8299 maybe a type and a double colon. The next thing we expect to see
8300 is the name of the symbol. */
8301 m
= gfc_match_name (name
);
8305 var_locus
= gfc_current_locus
;
8307 /* OK, we've successfully matched the declaration. Now put the
8308 symbol in the current namespace. If we fail to create the symbol,
8310 if (!build_sym (name
, NULL
, false, &as
, &var_locus
))
8316 /* The double colon must be present in order to have initializers.
8317 Otherwise the statement is ambiguous with an assignment statement. */
8320 if (gfc_match_char ('=') == MATCH_YES
)
8322 m
= gfc_match_init_expr (&initializer
);
8325 gfc_error ("Expected an initialization expression at %C");
8334 /* If we do not have an initializer, the initialization value of the
8335 previous enumerator (stored in last_initializer) is incremented
8336 by 1 and is used to initialize the current enumerator. */
8337 if (initializer
== NULL
)
8338 initializer
= enum_initializer (last_initializer
, old_locus
);
8340 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
8342 gfc_error ("ENUMERATOR %L not initialized with integer expression",
8348 /* Store this current initializer, for the next enumerator variable
8349 to be parsed. add_init_expr_to_sym() zeros initializer, so we
8350 use last_initializer below. */
8351 last_initializer
= initializer
;
8352 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
8354 /* Maintain enumerator history. */
8355 gfc_find_symbol (name
, NULL
, 0, &sym
);
8356 create_enum_history (sym
, last_initializer
);
8358 return (t
) ? MATCH_YES
: MATCH_ERROR
;
8361 /* Free stuff up and return. */
8362 gfc_free_expr (initializer
);
8368 /* Match the enumerator definition statement. */
8371 gfc_match_enumerator_def (void)
8376 gfc_clear_ts (¤t_ts
);
8378 m
= gfc_match (" enumerator");
8382 m
= gfc_match (" :: ");
8383 if (m
== MATCH_ERROR
)
8386 colon_seen
= (m
== MATCH_YES
);
8388 if (gfc_current_state () != COMP_ENUM
)
8390 gfc_error ("ENUM definition statement expected before %C");
8391 gfc_free_enum_history ();
8395 (¤t_ts
)->type
= BT_INTEGER
;
8396 (¤t_ts
)->kind
= gfc_c_int_kind
;
8398 gfc_clear_attr (¤t_attr
);
8399 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
8408 m
= enumerator_decl ();
8409 if (m
== MATCH_ERROR
)
8411 gfc_free_enum_history ();
8417 if (gfc_match_eos () == MATCH_YES
)
8419 if (gfc_match_char (',') != MATCH_YES
)
8423 if (gfc_current_state () == COMP_ENUM
)
8425 gfc_free_enum_history ();
8426 gfc_error ("Syntax error in ENUMERATOR definition at %C");
8431 gfc_free_array_spec (current_as
);
8438 /* Match binding attributes. */
8441 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
, bool ppc
)
8443 bool found_passing
= false;
8444 bool seen_ptr
= false;
8445 match m
= MATCH_YES
;
8447 /* Initialize to defaults. Do so even before the MATCH_NO check so that in
8448 this case the defaults are in there. */
8449 ba
->access
= ACCESS_UNKNOWN
;
8450 ba
->pass_arg
= NULL
;
8451 ba
->pass_arg_num
= 0;
8453 ba
->non_overridable
= 0;
8457 /* If we find a comma, we believe there are binding attributes. */
8458 m
= gfc_match_char (',');
8464 /* Access specifier. */
8466 m
= gfc_match (" public");
8467 if (m
== MATCH_ERROR
)
8471 if (ba
->access
!= ACCESS_UNKNOWN
)
8473 gfc_error ("Duplicate access-specifier at %C");
8477 ba
->access
= ACCESS_PUBLIC
;
8481 m
= gfc_match (" private");
8482 if (m
== MATCH_ERROR
)
8486 if (ba
->access
!= ACCESS_UNKNOWN
)
8488 gfc_error ("Duplicate access-specifier at %C");
8492 ba
->access
= ACCESS_PRIVATE
;
8496 /* If inside GENERIC, the following is not allowed. */
8501 m
= gfc_match (" nopass");
8502 if (m
== MATCH_ERROR
)
8508 gfc_error ("Binding attributes already specify passing,"
8509 " illegal NOPASS at %C");
8513 found_passing
= true;
8518 /* PASS possibly including argument. */
8519 m
= gfc_match (" pass");
8520 if (m
== MATCH_ERROR
)
8524 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
8528 gfc_error ("Binding attributes already specify passing,"
8529 " illegal PASS at %C");
8533 m
= gfc_match (" ( %n )", arg
);
8534 if (m
== MATCH_ERROR
)
8537 ba
->pass_arg
= gfc_get_string (arg
);
8538 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
8540 found_passing
= true;
8548 m
= gfc_match (" pointer");
8549 if (m
== MATCH_ERROR
)
8555 gfc_error ("Duplicate POINTER attribute at %C");
8565 /* NON_OVERRIDABLE flag. */
8566 m
= gfc_match (" non_overridable");
8567 if (m
== MATCH_ERROR
)
8571 if (ba
->non_overridable
)
8573 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
8577 ba
->non_overridable
= 1;
8581 /* DEFERRED flag. */
8582 m
= gfc_match (" deferred");
8583 if (m
== MATCH_ERROR
)
8589 gfc_error ("Duplicate DEFERRED at %C");
8600 /* Nothing matching found. */
8602 gfc_error ("Expected access-specifier at %C");
8604 gfc_error ("Expected binding attribute at %C");
8607 while (gfc_match_char (',') == MATCH_YES
);
8609 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
8610 if (ba
->non_overridable
&& ba
->deferred
)
8612 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
8619 if (ba
->access
== ACCESS_UNKNOWN
)
8620 ba
->access
= gfc_typebound_default_access
;
8622 if (ppc
&& !seen_ptr
)
8624 gfc_error ("POINTER attribute is required for procedure pointer component"
8636 /* Match a PROCEDURE specific binding inside a derived type. */
8639 match_procedure_in_type (void)
8641 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8642 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
8643 char* target
= NULL
, *ifc
= NULL
;
8644 gfc_typebound_proc tb
;
8653 /* Check current state. */
8654 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
8655 block
= gfc_state_stack
->previous
->sym
;
8658 /* Try to match PROCEDURE(interface). */
8659 if (gfc_match (" (") == MATCH_YES
)
8661 m
= gfc_match_name (target_buf
);
8662 if (m
== MATCH_ERROR
)
8666 gfc_error ("Interface-name expected after %<(%> at %C");
8670 if (gfc_match (" )") != MATCH_YES
)
8672 gfc_error ("%<)%> expected at %C");
8679 /* Construct the data structure. */
8680 memset (&tb
, 0, sizeof (tb
));
8681 tb
.where
= gfc_current_locus
;
8683 /* Match binding attributes. */
8684 m
= match_binding_attributes (&tb
, false, false);
8685 if (m
== MATCH_ERROR
)
8687 seen_attrs
= (m
== MATCH_YES
);
8689 /* Check that attribute DEFERRED is given if an interface is specified. */
8690 if (tb
.deferred
&& !ifc
)
8692 gfc_error ("Interface must be specified for DEFERRED binding at %C");
8695 if (ifc
&& !tb
.deferred
)
8697 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
8701 /* Match the colons. */
8702 m
= gfc_match (" ::");
8703 if (m
== MATCH_ERROR
)
8705 seen_colons
= (m
== MATCH_YES
);
8706 if (seen_attrs
&& !seen_colons
)
8708 gfc_error ("Expected %<::%> after binding-attributes at %C");
8712 /* Match the binding names. */
8715 m
= gfc_match_name (name
);
8716 if (m
== MATCH_ERROR
)
8720 gfc_error ("Expected binding name at %C");
8724 if (num
>1 && !gfc_notify_std (GFC_STD_F2008
, "PROCEDURE list at %C"))
8727 /* Try to match the '=> target', if it's there. */
8729 m
= gfc_match (" =>");
8730 if (m
== MATCH_ERROR
)
8736 gfc_error ("%<=> target%> is invalid for DEFERRED binding at %C");
8742 gfc_error ("%<::%> needed in PROCEDURE binding with explicit target"
8747 m
= gfc_match_name (target_buf
);
8748 if (m
== MATCH_ERROR
)
8752 gfc_error ("Expected binding target after %<=>%> at %C");
8755 target
= target_buf
;
8758 /* If no target was found, it has the same name as the binding. */
8762 /* Get the namespace to insert the symbols into. */
8763 ns
= block
->f2k_derived
;
8766 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
8767 if (tb
.deferred
&& !block
->attr
.abstract
)
8769 gfc_error ("Type %qs containing DEFERRED binding at %C "
8770 "is not ABSTRACT", block
->name
);
8774 /* See if we already have a binding with this name in the symtree which
8775 would be an error. If a GENERIC already targeted this binding, it may
8776 be already there but then typebound is still NULL. */
8777 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
8778 if (stree
&& stree
->n
.tb
)
8780 gfc_error ("There is already a procedure with binding name %qs for "
8781 "the derived type %qs at %C", name
, block
->name
);
8785 /* Insert it and set attributes. */
8789 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
8792 stree
->n
.tb
= gfc_get_typebound_proc (&tb
);
8794 if (gfc_get_sym_tree (target
, gfc_current_ns
, &stree
->n
.tb
->u
.specific
,
8797 gfc_set_sym_referenced (stree
->n
.tb
->u
.specific
->n
.sym
);
8799 if (gfc_match_eos () == MATCH_YES
)
8801 if (gfc_match_char (',') != MATCH_YES
)
8806 gfc_error ("Syntax error in PROCEDURE statement at %C");
8811 /* Match a GENERIC procedure binding inside a derived type. */
8814 gfc_match_generic (void)
8816 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
8817 char bind_name
[GFC_MAX_SYMBOL_LEN
+ 16]; /* Allow space for OPERATOR(...). */
8819 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
8820 gfc_typebound_proc
* tb
;
8822 interface_type op_type
;
8823 gfc_intrinsic_op op
;
8826 /* Check current state. */
8827 if (gfc_current_state () == COMP_DERIVED
)
8829 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
8832 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
8834 block
= gfc_state_stack
->previous
->sym
;
8835 ns
= block
->f2k_derived
;
8836 gcc_assert (block
&& ns
);
8838 memset (&tbattr
, 0, sizeof (tbattr
));
8839 tbattr
.where
= gfc_current_locus
;
8841 /* See if we get an access-specifier. */
8842 m
= match_binding_attributes (&tbattr
, true, false);
8843 if (m
== MATCH_ERROR
)
8846 /* Now the colons, those are required. */
8847 if (gfc_match (" ::") != MATCH_YES
)
8849 gfc_error ("Expected %<::%> at %C");
8853 /* Match the binding name; depending on type (operator / generic) format
8854 it for future error messages into bind_name. */
8856 m
= gfc_match_generic_spec (&op_type
, name
, &op
);
8857 if (m
== MATCH_ERROR
)
8861 gfc_error ("Expected generic name or operator descriptor at %C");
8867 case INTERFACE_GENERIC
:
8868 snprintf (bind_name
, sizeof (bind_name
), "%s", name
);
8871 case INTERFACE_USER_OP
:
8872 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(.%s.)", name
);
8875 case INTERFACE_INTRINSIC_OP
:
8876 snprintf (bind_name
, sizeof (bind_name
), "OPERATOR(%s)",
8877 gfc_op2string (op
));
8880 case INTERFACE_NAMELESS
:
8881 gfc_error ("Malformed GENERIC statement at %C");
8889 /* Match the required =>. */
8890 if (gfc_match (" =>") != MATCH_YES
)
8892 gfc_error ("Expected %<=>%> at %C");
8896 /* Try to find existing GENERIC binding with this name / for this operator;
8897 if there is something, check that it is another GENERIC and then extend
8898 it rather than building a new node. Otherwise, create it and put it
8899 at the right position. */
8903 case INTERFACE_USER_OP
:
8904 case INTERFACE_GENERIC
:
8906 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8909 st
= gfc_find_symtree (is_op
? ns
->tb_uop_root
: ns
->tb_sym_root
, name
);
8921 case INTERFACE_INTRINSIC_OP
:
8931 if (!tb
->is_generic
)
8933 gcc_assert (op_type
== INTERFACE_GENERIC
);
8934 gfc_error ("There's already a non-generic procedure with binding name"
8935 " %qs for the derived type %qs at %C",
8936 bind_name
, block
->name
);
8940 if (tb
->access
!= tbattr
.access
)
8942 gfc_error ("Binding at %C must have the same access as already"
8943 " defined binding %qs", bind_name
);
8949 tb
= gfc_get_typebound_proc (NULL
);
8950 tb
->where
= gfc_current_locus
;
8951 tb
->access
= tbattr
.access
;
8953 tb
->u
.generic
= NULL
;
8957 case INTERFACE_GENERIC
:
8958 case INTERFACE_USER_OP
:
8960 const bool is_op
= (op_type
== INTERFACE_USER_OP
);
8963 st
= gfc_new_symtree (is_op
? &ns
->tb_uop_root
: &ns
->tb_sym_root
,
8971 case INTERFACE_INTRINSIC_OP
:
8980 /* Now, match all following names as specific targets. */
8983 gfc_symtree
* target_st
;
8984 gfc_tbp_generic
* target
;
8986 m
= gfc_match_name (name
);
8987 if (m
== MATCH_ERROR
)
8991 gfc_error ("Expected specific binding name at %C");
8995 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
8997 /* See if this is a duplicate specification. */
8998 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
8999 if (target_st
== target
->specific_st
)
9001 gfc_error ("%qs already defined as specific binding for the"
9002 " generic %qs at %C", name
, bind_name
);
9006 target
= gfc_get_tbp_generic ();
9007 target
->specific_st
= target_st
;
9008 target
->specific
= NULL
;
9009 target
->next
= tb
->u
.generic
;
9010 target
->is_operator
= ((op_type
== INTERFACE_USER_OP
)
9011 || (op_type
== INTERFACE_INTRINSIC_OP
));
9012 tb
->u
.generic
= target
;
9014 while (gfc_match (" ,") == MATCH_YES
);
9016 /* Here should be the end. */
9017 if (gfc_match_eos () != MATCH_YES
)
9019 gfc_error ("Junk after GENERIC binding at %C");
9030 /* Match a FINAL declaration inside a derived type. */
9033 gfc_match_final_decl (void)
9035 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
9038 gfc_namespace
* module_ns
;
9042 if (gfc_current_form
== FORM_FREE
)
9044 char c
= gfc_peek_ascii_char ();
9045 if (!gfc_is_whitespace (c
) && c
!= ':')
9049 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
9051 if (gfc_current_form
== FORM_FIXED
)
9054 gfc_error ("FINAL declaration at %C must be inside a derived type "
9055 "CONTAINS section");
9059 block
= gfc_state_stack
->previous
->sym
;
9062 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
9063 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
9065 gfc_error ("Derived type declaration with FINAL at %C must be in the"
9066 " specification part of a MODULE");
9070 module_ns
= gfc_current_ns
;
9071 gcc_assert (module_ns
);
9072 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
9074 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
9075 if (gfc_match (" ::") == MATCH_ERROR
)
9078 /* Match the sequence of procedure names. */
9085 if (first
&& gfc_match_eos () == MATCH_YES
)
9087 gfc_error ("Empty FINAL at %C");
9091 m
= gfc_match_name (name
);
9094 gfc_error ("Expected module procedure name at %C");
9097 else if (m
!= MATCH_YES
)
9100 if (gfc_match_eos () == MATCH_YES
)
9102 if (!last
&& gfc_match_char (',') != MATCH_YES
)
9104 gfc_error ("Expected %<,%> at %C");
9108 if (gfc_get_symbol (name
, module_ns
, &sym
))
9110 gfc_error ("Unknown procedure name %qs at %C", name
);
9114 /* Mark the symbol as module procedure. */
9115 if (sym
->attr
.proc
!= PROC_MODULE
9116 && !gfc_add_procedure (&sym
->attr
, PROC_MODULE
, sym
->name
, NULL
))
9119 /* Check if we already have this symbol in the list, this is an error. */
9120 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
9121 if (f
->proc_sym
== sym
)
9123 gfc_error ("%qs at %C is already defined as FINAL procedure!",
9128 /* Add this symbol to the list of finalizers. */
9129 gcc_assert (block
->f2k_derived
);
9131 f
= XCNEW (gfc_finalizer
);
9133 f
->proc_tree
= NULL
;
9134 f
->where
= gfc_current_locus
;
9135 f
->next
= block
->f2k_derived
->finalizers
;
9136 block
->f2k_derived
->finalizers
= f
;
9146 const ext_attr_t ext_attr_list
[] = {
9147 { "dllimport", EXT_ATTR_DLLIMPORT
, "dllimport" },
9148 { "dllexport", EXT_ATTR_DLLEXPORT
, "dllexport" },
9149 { "cdecl", EXT_ATTR_CDECL
, "cdecl" },
9150 { "stdcall", EXT_ATTR_STDCALL
, "stdcall" },
9151 { "fastcall", EXT_ATTR_FASTCALL
, "fastcall" },
9152 { "no_arg_check", EXT_ATTR_NO_ARG_CHECK
, NULL
},
9153 { NULL
, EXT_ATTR_LAST
, NULL
}
9156 /* Match a !GCC$ ATTRIBUTES statement of the form:
9157 !GCC$ ATTRIBUTES attribute-list :: var-name [, var-name] ...
9158 When we come here, we have already matched the !GCC$ ATTRIBUTES string.
9160 TODO: We should support all GCC attributes using the same syntax for
9161 the attribute list, i.e. the list in C
9162 __attributes(( attribute-list ))
9164 !GCC$ ATTRIBUTES attribute-list ::
9165 Cf. c-parser.c's c_parser_attributes; the data can then directly be
9168 As there is absolutely no risk of confusion, we should never return
9171 gfc_match_gcc_attributes (void)
9173 symbol_attribute attr
;
9174 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
9179 gfc_clear_attr (&attr
);
9184 if (gfc_match_name (name
) != MATCH_YES
)
9187 for (id
= 0; id
< EXT_ATTR_LAST
; id
++)
9188 if (strcmp (name
, ext_attr_list
[id
].name
) == 0)
9191 if (id
== EXT_ATTR_LAST
)
9193 gfc_error ("Unknown attribute in !GCC$ ATTRIBUTES statement at %C");
9197 if (!gfc_add_ext_attribute (&attr
, (ext_attr_id_t
)id
, &gfc_current_locus
))
9200 gfc_gobble_whitespace ();
9201 ch
= gfc_next_ascii_char ();
9204 /* This is the successful exit condition for the loop. */
9205 if (gfc_next_ascii_char () == ':')
9215 if (gfc_match_eos () == MATCH_YES
)
9220 m
= gfc_match_name (name
);
9224 if (find_special (name
, &sym
, true))
9227 sym
->attr
.ext_attr
|= attr
.ext_attr
;
9229 if (gfc_match_eos () == MATCH_YES
)
9232 if (gfc_match_char (',') != MATCH_YES
)
9239 gfc_error ("Syntax error in !GCC$ ATTRIBUTES statement at %C");