1 /* Declaration statement matcher
2 Copyright (C) 2002, 2004, 2005, 2006, 2007, 2008
3 Free Software Foundation, Inc.
4 Contributed by Andy Vaught
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
29 /* Macros to access allocate memory for gfc_data_variable,
30 gfc_data_value and gfc_data. */
31 #define gfc_get_data_variable() XCNEW (gfc_data_variable)
32 #define gfc_get_data_value() XCNEW (gfc_data_value)
33 #define gfc_get_data() XCNEW (gfc_data)
36 /* This flag is set if an old-style length selector is matched
37 during a type-declaration statement. */
39 static int old_char_selector
;
41 /* When variables acquire types and attributes from a declaration
42 statement, they get them from the following static variables. The
43 first part of a declaration sets these variables and the second
44 part copies these into symbol structures. */
46 static gfc_typespec current_ts
;
48 static symbol_attribute current_attr
;
49 static gfc_array_spec
*current_as
;
50 static int colon_seen
;
52 /* The current binding label (if any). */
53 static char curr_binding_label
[GFC_MAX_BINDING_LABEL_LEN
+ 1];
54 /* Need to know how many identifiers are on the current data declaration
55 line in case we're given the BIND(C) attribute with a NAME= specifier. */
56 static int num_idents_on_line
;
57 /* Need to know if a NAME= specifier was found during gfc_match_bind_c so we
58 can supply a name if the curr_binding_label is nil and NAME= was not. */
59 static int has_name_equals
= 0;
61 /* Initializer of the previous enumerator. */
63 static gfc_expr
*last_initializer
;
65 /* History of all the enumerators is maintained, so that
66 kind values of all the enumerators could be updated depending
67 upon the maximum initialized value. */
69 typedef struct enumerator_history
72 gfc_expr
*initializer
;
73 struct enumerator_history
*next
;
77 /* Header of enum history chain. */
79 static enumerator_history
*enum_history
= NULL
;
81 /* Pointer of enum history node containing largest initializer. */
83 static enumerator_history
*max_enum
= NULL
;
85 /* gfc_new_block points to the symbol of a newly matched block. */
87 gfc_symbol
*gfc_new_block
;
89 bool gfc_matching_function
;
92 /********************* DATA statement subroutines *********************/
94 static bool in_match_data
= false;
97 gfc_in_match_data (void)
103 set_in_match_data (bool set_value
)
105 in_match_data
= set_value
;
108 /* Free a gfc_data_variable structure and everything beneath it. */
111 free_variable (gfc_data_variable
*p
)
113 gfc_data_variable
*q
;
118 gfc_free_expr (p
->expr
);
119 gfc_free_iterator (&p
->iter
, 0);
120 free_variable (p
->list
);
126 /* Free a gfc_data_value structure and everything beneath it. */
129 free_value (gfc_data_value
*p
)
136 gfc_free_expr (p
->expr
);
142 /* Free a list of gfc_data structures. */
145 gfc_free_data (gfc_data
*p
)
152 free_variable (p
->var
);
153 free_value (p
->value
);
159 /* Free all data in a namespace. */
162 gfc_free_data_all (gfc_namespace
*ns
)
175 static match
var_element (gfc_data_variable
*);
177 /* Match a list of variables terminated by an iterator and a right
181 var_list (gfc_data_variable
*parent
)
183 gfc_data_variable
*tail
, var
;
186 m
= var_element (&var
);
187 if (m
== MATCH_ERROR
)
192 tail
= gfc_get_data_variable ();
199 if (gfc_match_char (',') != MATCH_YES
)
202 m
= gfc_match_iterator (&parent
->iter
, 1);
205 if (m
== MATCH_ERROR
)
208 m
= var_element (&var
);
209 if (m
== MATCH_ERROR
)
214 tail
->next
= gfc_get_data_variable ();
220 if (gfc_match_char (')') != MATCH_YES
)
225 gfc_syntax_error (ST_DATA
);
230 /* Match a single element in a data variable list, which can be a
231 variable-iterator list. */
234 var_element (gfc_data_variable
*new_var
)
239 memset (new_var
, 0, sizeof (gfc_data_variable
));
241 if (gfc_match_char ('(') == MATCH_YES
)
242 return var_list (new_var
);
244 m
= gfc_match_variable (&new_var
->expr
, 0);
248 sym
= new_var
->expr
->symtree
->n
.sym
;
250 /* Symbol should already have an associated type. */
251 if (gfc_check_symbol_typed (sym
, gfc_current_ns
,
252 false, gfc_current_locus
) == FAILURE
)
255 if (!sym
->attr
.function
&& gfc_current_ns
->parent
256 && gfc_current_ns
->parent
== sym
->ns
)
258 gfc_error ("Host associated variable '%s' may not be in the DATA "
259 "statement at %C", sym
->name
);
263 if (gfc_current_state () != COMP_BLOCK_DATA
264 && sym
->attr
.in_common
265 && gfc_notify_std (GFC_STD_GNU
, "Extension: initialization of "
266 "common block variable '%s' in DATA statement at %C",
267 sym
->name
) == FAILURE
)
270 if (gfc_add_data (&sym
->attr
, sym
->name
, &new_var
->expr
->where
) == FAILURE
)
277 /* Match the top-level list of data variables. */
280 top_var_list (gfc_data
*d
)
282 gfc_data_variable var
, *tail
, *new_var
;
289 m
= var_element (&var
);
292 if (m
== MATCH_ERROR
)
295 new_var
= gfc_get_data_variable ();
301 tail
->next
= new_var
;
305 if (gfc_match_char ('/') == MATCH_YES
)
307 if (gfc_match_char (',') != MATCH_YES
)
314 gfc_syntax_error (ST_DATA
);
315 gfc_free_data_all (gfc_current_ns
);
321 match_data_constant (gfc_expr
**result
)
323 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
329 m
= gfc_match_literal_constant (&expr
, 1);
336 if (m
== MATCH_ERROR
)
339 m
= gfc_match_null (result
);
343 old_loc
= gfc_current_locus
;
345 /* Should this be a structure component, try to match it
346 before matching a name. */
347 m
= gfc_match_rvalue (result
);
348 if (m
== MATCH_ERROR
)
351 if (m
== MATCH_YES
&& (*result
)->expr_type
== EXPR_STRUCTURE
)
353 if (gfc_simplify_expr (*result
, 0) == FAILURE
)
358 gfc_current_locus
= old_loc
;
360 m
= gfc_match_name (name
);
364 if (gfc_find_symbol (name
, NULL
, 1, &sym
))
368 || (sym
->attr
.flavor
!= FL_PARAMETER
&& sym
->attr
.flavor
!= FL_DERIVED
))
370 gfc_error ("Symbol '%s' must be a PARAMETER in DATA statement at %C",
374 else if (sym
->attr
.flavor
== FL_DERIVED
)
375 return gfc_match_structure_constructor (sym
, result
, false);
377 /* Check to see if the value is an initialization array expression. */
378 if (sym
->value
->expr_type
== EXPR_ARRAY
)
380 gfc_current_locus
= old_loc
;
382 m
= gfc_match_init_expr (result
);
383 if (m
== MATCH_ERROR
)
388 if (gfc_simplify_expr (*result
, 0) == FAILURE
)
391 if ((*result
)->expr_type
== EXPR_CONSTANT
)
395 gfc_error ("Invalid initializer %s in Data statement at %C", name
);
401 *result
= gfc_copy_expr (sym
->value
);
406 /* Match a list of values in a DATA statement. The leading '/' has
407 already been seen at this point. */
410 top_val_list (gfc_data
*data
)
412 gfc_data_value
*new_val
, *tail
;
420 m
= match_data_constant (&expr
);
423 if (m
== MATCH_ERROR
)
426 new_val
= gfc_get_data_value ();
427 mpz_init (new_val
->repeat
);
430 data
->value
= new_val
;
432 tail
->next
= new_val
;
436 if (expr
->ts
.type
!= BT_INTEGER
|| gfc_match_char ('*') != MATCH_YES
)
439 mpz_set_ui (tail
->repeat
, 1);
443 if (expr
->ts
.type
== BT_INTEGER
)
444 mpz_set (tail
->repeat
, expr
->value
.integer
);
445 gfc_free_expr (expr
);
447 m
= match_data_constant (&tail
->expr
);
450 if (m
== MATCH_ERROR
)
454 if (gfc_match_char ('/') == MATCH_YES
)
456 if (gfc_match_char (',') == MATCH_NO
)
463 gfc_syntax_error (ST_DATA
);
464 gfc_free_data_all (gfc_current_ns
);
469 /* Matches an old style initialization. */
472 match_old_style_init (const char *name
)
479 /* Set up data structure to hold initializers. */
480 gfc_find_sym_tree (name
, NULL
, 0, &st
);
483 newdata
= gfc_get_data ();
484 newdata
->var
= gfc_get_data_variable ();
485 newdata
->var
->expr
= gfc_get_variable_expr (st
);
486 newdata
->where
= gfc_current_locus
;
488 /* Match initial value list. This also eats the terminal '/'. */
489 m
= top_val_list (newdata
);
498 gfc_error ("Initialization at %C is not allowed in a PURE procedure");
503 /* Mark the variable as having appeared in a data statement. */
504 if (gfc_add_data (&sym
->attr
, sym
->name
, &sym
->declared_at
) == FAILURE
)
510 /* Chain in namespace list of DATA initializers. */
511 newdata
->next
= gfc_current_ns
->data
;
512 gfc_current_ns
->data
= newdata
;
518 /* Match the stuff following a DATA statement. If ERROR_FLAG is set,
519 we are matching a DATA statement and are therefore issuing an error
520 if we encounter something unexpected, if not, we're trying to match
521 an old-style initialization expression of the form INTEGER I /2/. */
524 gfc_match_data (void)
529 set_in_match_data (true);
533 new_data
= gfc_get_data ();
534 new_data
->where
= gfc_current_locus
;
536 m
= top_var_list (new_data
);
540 m
= top_val_list (new_data
);
544 new_data
->next
= gfc_current_ns
->data
;
545 gfc_current_ns
->data
= new_data
;
547 if (gfc_match_eos () == MATCH_YES
)
550 gfc_match_char (','); /* Optional comma */
553 set_in_match_data (false);
557 gfc_error ("DATA statement at %C is not allowed in a PURE procedure");
564 set_in_match_data (false);
565 gfc_free_data (new_data
);
570 /************************ Declaration statements *********************/
572 /* Match an intent specification. Since this can only happen after an
573 INTENT word, a legal intent-spec must follow. */
576 match_intent_spec (void)
579 if (gfc_match (" ( in out )") == MATCH_YES
)
581 if (gfc_match (" ( in )") == MATCH_YES
)
583 if (gfc_match (" ( out )") == MATCH_YES
)
586 gfc_error ("Bad INTENT specification at %C");
587 return INTENT_UNKNOWN
;
591 /* Matches a character length specification, which is either a
592 specification expression or a '*'. */
595 char_len_param_value (gfc_expr
**expr
)
599 if (gfc_match_char ('*') == MATCH_YES
)
605 m
= gfc_match_expr (expr
);
608 && gfc_expr_check_typed (*expr
, gfc_current_ns
, false) == FAILURE
)
611 if (m
== MATCH_YES
&& (*expr
)->expr_type
== EXPR_FUNCTION
)
613 if ((*expr
)->value
.function
.actual
614 && (*expr
)->value
.function
.actual
->expr
->symtree
)
617 e
= (*expr
)->value
.function
.actual
->expr
;
618 if (e
->symtree
->n
.sym
->attr
.flavor
== FL_PROCEDURE
619 && e
->expr_type
== EXPR_VARIABLE
)
621 if (e
->symtree
->n
.sym
->ts
.type
== BT_UNKNOWN
)
623 if (e
->symtree
->n
.sym
->ts
.type
== BT_CHARACTER
624 && e
->symtree
->n
.sym
->ts
.cl
625 && e
->symtree
->n
.sym
->ts
.cl
->length
->ts
.type
== BT_UNKNOWN
)
633 gfc_error ("Conflict in attributes of function argument at %C");
638 /* A character length is a '*' followed by a literal integer or a
639 char_len_param_value in parenthesis. */
642 match_char_length (gfc_expr
**expr
)
647 m
= gfc_match_char ('*');
651 m
= gfc_match_small_literal_int (&length
, NULL
);
652 if (m
== MATCH_ERROR
)
657 *expr
= gfc_int_expr (length
);
661 if (gfc_match_char ('(') == MATCH_NO
)
664 m
= char_len_param_value (expr
);
665 if (m
!= MATCH_YES
&& gfc_matching_function
)
671 if (m
== MATCH_ERROR
)
676 if (gfc_match_char (')') == MATCH_NO
)
678 gfc_free_expr (*expr
);
686 gfc_error ("Syntax error in character length specification at %C");
691 /* Special subroutine for finding a symbol. Check if the name is found
692 in the current name space. If not, and we're compiling a function or
693 subroutine and the parent compilation unit is an interface, then check
694 to see if the name we've been given is the name of the interface
695 (located in another namespace). */
698 find_special (const char *name
, gfc_symbol
**result
)
703 i
= gfc_get_symbol (name
, NULL
, result
);
707 if (gfc_current_state () != COMP_SUBROUTINE
708 && gfc_current_state () != COMP_FUNCTION
)
711 s
= gfc_state_stack
->previous
;
715 if (s
->state
!= COMP_INTERFACE
)
718 goto end
; /* Nameless interface. */
720 if (strcmp (name
, s
->sym
->name
) == 0)
731 /* Special subroutine for getting a symbol node associated with a
732 procedure name, used in SUBROUTINE and FUNCTION statements. The
733 symbol is created in the parent using with symtree node in the
734 child unit pointing to the symbol. If the current namespace has no
735 parent, then the symbol is just created in the current unit. */
738 get_proc_name (const char *name
, gfc_symbol
**result
, bool module_fcn_entry
)
744 /* Module functions have to be left in their own namespace because
745 they have potentially (almost certainly!) already been referenced.
746 In this sense, they are rather like external functions. This is
747 fixed up in resolve.c(resolve_entries), where the symbol name-
748 space is set to point to the master function, so that the fake
749 result mechanism can work. */
750 if (module_fcn_entry
)
752 /* Present if entry is declared to be a module procedure. */
753 rc
= gfc_find_symbol (name
, gfc_current_ns
->parent
, 0, result
);
756 rc
= gfc_get_symbol (name
, NULL
, result
);
757 else if (!gfc_get_symbol (name
, NULL
, &sym
) && sym
758 && (*result
)->ts
.type
== BT_UNKNOWN
759 && sym
->attr
.flavor
== FL_UNKNOWN
)
760 /* Pick up the typespec for the entry, if declared in the function
761 body. Note that this symbol is FL_UNKNOWN because it will
762 only have appeared in a type declaration. The local symtree
763 is set to point to the module symbol and a unique symtree
764 to the local version. This latter ensures a correct clearing
767 /* If the ENTRY proceeds its specification, we need to ensure
768 that this does not raise a "has no IMPLICIT type" error. */
769 if (sym
->ts
.type
== BT_UNKNOWN
)
770 sym
->attr
.untyped
= 1;
772 (*result
)->ts
= sym
->ts
;
774 /* Put the symbol in the procedure namespace so that, should
775 the ENTRY precede its specification, the specification
777 (*result
)->ns
= gfc_current_ns
;
779 gfc_find_sym_tree (name
, gfc_current_ns
, 0, &st
);
781 st
= gfc_get_unique_symtree (gfc_current_ns
);
786 rc
= gfc_get_symbol (name
, gfc_current_ns
->parent
, result
);
792 gfc_current_ns
->refs
++;
794 if (sym
&& !sym
->gfc_new
&& gfc_current_state () != COMP_INTERFACE
)
796 /* Trap another encompassed procedure with the same name. All
797 these conditions are necessary to avoid picking up an entry
798 whose name clashes with that of the encompassing procedure;
799 this is handled using gsymbols to register unique,globally
801 if (sym
->attr
.flavor
!= 0
802 && sym
->attr
.proc
!= 0
803 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
804 && sym
->attr
.if_source
!= IFSRC_UNKNOWN
)
805 gfc_error_now ("Procedure '%s' at %C is already defined at %L",
806 name
, &sym
->declared_at
);
808 /* Trap a procedure with a name the same as interface in the
809 encompassing scope. */
810 if (sym
->attr
.generic
!= 0
811 && (sym
->attr
.subroutine
|| sym
->attr
.function
)
812 && !sym
->attr
.mod_proc
)
813 gfc_error_now ("Name '%s' at %C is already defined"
814 " as a generic interface at %L",
815 name
, &sym
->declared_at
);
817 /* Trap declarations of attributes in encompassing scope. The
818 signature for this is that ts.kind is set. Legitimate
819 references only set ts.type. */
820 if (sym
->ts
.kind
!= 0
821 && !sym
->attr
.implicit_type
822 && sym
->attr
.proc
== 0
823 && gfc_current_ns
->parent
!= NULL
824 && sym
->attr
.access
== 0
825 && !module_fcn_entry
)
826 gfc_error_now ("Procedure '%s' at %C has an explicit interface "
827 "and must not have attributes declared at %L",
828 name
, &sym
->declared_at
);
831 if (gfc_current_ns
->parent
== NULL
|| *result
== NULL
)
834 /* Module function entries will already have a symtree in
835 the current namespace but will need one at module level. */
836 if (module_fcn_entry
)
838 /* Present if entry is declared to be a module procedure. */
839 rc
= gfc_find_sym_tree (name
, gfc_current_ns
->parent
, 0, &st
);
841 st
= gfc_new_symtree (&gfc_current_ns
->parent
->sym_root
, name
);
844 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
849 /* See if the procedure should be a module procedure. */
851 if (((sym
->ns
->proc_name
!= NULL
852 && sym
->ns
->proc_name
->attr
.flavor
== FL_MODULE
853 && sym
->attr
.proc
!= PROC_MODULE
)
854 || (module_fcn_entry
&& sym
->attr
.proc
!= PROC_MODULE
))
855 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
856 sym
->name
, NULL
) == FAILURE
)
863 /* Verify that the given symbol representing a parameter is C
864 interoperable, by checking to see if it was marked as such after
865 its declaration. If the given symbol is not interoperable, a
866 warning is reported, thus removing the need to return the status to
867 the calling function. The standard does not require the user use
868 one of the iso_c_binding named constants to declare an
869 interoperable parameter, but we can't be sure if the param is C
870 interop or not if the user doesn't. For example, integer(4) may be
871 legal Fortran, but doesn't have meaning in C. It may interop with
872 a number of the C types, which causes a problem because the
873 compiler can't know which one. This code is almost certainly not
874 portable, and the user will get what they deserve if the C type
875 across platforms isn't always interoperable with integer(4). If
876 the user had used something like integer(c_int) or integer(c_long),
877 the compiler could have automatically handled the varying sizes
881 verify_c_interop_param (gfc_symbol
*sym
)
883 int is_c_interop
= 0;
884 gfc_try retval
= SUCCESS
;
886 /* We check implicitly typed variables in symbol.c:gfc_set_default_type().
887 Don't repeat the checks here. */
888 if (sym
->attr
.implicit_type
)
891 /* For subroutines or functions that are passed to a BIND(C) procedure,
892 they're interoperable if they're BIND(C) and their params are all
894 if (sym
->attr
.flavor
== FL_PROCEDURE
)
896 if (sym
->attr
.is_bind_c
== 0)
898 gfc_error_now ("Procedure '%s' at %L must have the BIND(C) "
899 "attribute to be C interoperable", sym
->name
,
900 &(sym
->declared_at
));
906 if (sym
->attr
.is_c_interop
== 1)
907 /* We've already checked this procedure; don't check it again. */
910 return verify_bind_c_sym (sym
, &(sym
->ts
), sym
->attr
.in_common
,
915 /* See if we've stored a reference to a procedure that owns sym. */
916 if (sym
->ns
!= NULL
&& sym
->ns
->proc_name
!= NULL
)
918 if (sym
->ns
->proc_name
->attr
.is_bind_c
== 1)
921 (verify_c_interop (&(sym
->ts
))
924 if (is_c_interop
!= 1)
926 /* Make personalized messages to give better feedback. */
927 if (sym
->ts
.type
== BT_DERIVED
)
928 gfc_error ("Type '%s' at %L is a parameter to the BIND(C) "
929 " procedure '%s' but is not C interoperable "
930 "because derived type '%s' is not C interoperable",
931 sym
->name
, &(sym
->declared_at
),
932 sym
->ns
->proc_name
->name
,
933 sym
->ts
.derived
->name
);
935 gfc_warning ("Variable '%s' at %L is a parameter to the "
936 "BIND(C) procedure '%s' but may not be C "
938 sym
->name
, &(sym
->declared_at
),
939 sym
->ns
->proc_name
->name
);
942 /* Character strings are only C interoperable if they have a
944 if (sym
->ts
.type
== BT_CHARACTER
)
946 gfc_charlen
*cl
= sym
->ts
.cl
;
947 if (!cl
|| !cl
->length
|| cl
->length
->expr_type
!= EXPR_CONSTANT
948 || mpz_cmp_si (cl
->length
->value
.integer
, 1) != 0)
950 gfc_error ("Character argument '%s' at %L "
951 "must be length 1 because "
952 "procedure '%s' is BIND(C)",
953 sym
->name
, &sym
->declared_at
,
954 sym
->ns
->proc_name
->name
);
959 /* We have to make sure that any param to a bind(c) routine does
960 not have the allocatable, pointer, or optional attributes,
961 according to J3/04-007, section 5.1. */
962 if (sym
->attr
.allocatable
== 1)
964 gfc_error ("Variable '%s' at %L cannot have the "
965 "ALLOCATABLE attribute because procedure '%s'"
966 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
967 sym
->ns
->proc_name
->name
);
971 if (sym
->attr
.pointer
== 1)
973 gfc_error ("Variable '%s' at %L cannot have the "
974 "POINTER attribute because procedure '%s'"
975 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
976 sym
->ns
->proc_name
->name
);
980 if (sym
->attr
.optional
== 1)
982 gfc_error ("Variable '%s' at %L cannot have the "
983 "OPTIONAL attribute because procedure '%s'"
984 " is BIND(C)", sym
->name
, &(sym
->declared_at
),
985 sym
->ns
->proc_name
->name
);
989 /* Make sure that if it has the dimension attribute, that it is
990 either assumed size or explicit shape. */
993 if (sym
->as
->type
== AS_ASSUMED_SHAPE
)
995 gfc_error ("Assumed-shape array '%s' at %L cannot be an "
996 "argument to the procedure '%s' at %L because "
997 "the procedure is BIND(C)", sym
->name
,
998 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
999 &(sym
->ns
->proc_name
->declared_at
));
1003 if (sym
->as
->type
== AS_DEFERRED
)
1005 gfc_error ("Deferred-shape array '%s' at %L cannot be an "
1006 "argument to the procedure '%s' at %L because "
1007 "the procedure is BIND(C)", sym
->name
,
1008 &(sym
->declared_at
), sym
->ns
->proc_name
->name
,
1009 &(sym
->ns
->proc_name
->declared_at
));
1020 /* Function called by variable_decl() that adds a name to the symbol table. */
1023 build_sym (const char *name
, gfc_charlen
*cl
,
1024 gfc_array_spec
**as
, locus
*var_locus
)
1026 symbol_attribute attr
;
1029 if (gfc_get_symbol (name
, NULL
, &sym
))
1032 /* Start updating the symbol table. Add basic type attribute if present. */
1033 if (current_ts
.type
!= BT_UNKNOWN
1034 && (sym
->attr
.implicit_type
== 0
1035 || !gfc_compare_types (&sym
->ts
, ¤t_ts
))
1036 && gfc_add_type (sym
, ¤t_ts
, var_locus
) == FAILURE
)
1039 if (sym
->ts
.type
== BT_CHARACTER
)
1042 /* Add dimension attribute if present. */
1043 if (gfc_set_array_spec (sym
, *as
, var_locus
) == FAILURE
)
1047 /* Add attribute to symbol. The copy is so that we can reset the
1048 dimension attribute. */
1049 attr
= current_attr
;
1052 if (gfc_copy_attr (&sym
->attr
, &attr
, var_locus
) == FAILURE
)
1055 /* Finish any work that may need to be done for the binding label,
1056 if it's a bind(c). The bind(c) attr is found before the symbol
1057 is made, and before the symbol name (for data decls), so the
1058 current_ts is holding the binding label, or nothing if the
1059 name= attr wasn't given. Therefore, test here if we're dealing
1060 with a bind(c) and make sure the binding label is set correctly. */
1061 if (sym
->attr
.is_bind_c
== 1)
1063 if (sym
->binding_label
[0] == '\0')
1065 /* Set the binding label and verify that if a NAME= was specified
1066 then only one identifier was in the entity-decl-list. */
1067 if (set_binding_label (sym
->binding_label
, sym
->name
,
1068 num_idents_on_line
) == FAILURE
)
1073 /* See if we know we're in a common block, and if it's a bind(c)
1074 common then we need to make sure we're an interoperable type. */
1075 if (sym
->attr
.in_common
== 1)
1077 /* Test the common block object. */
1078 if (sym
->common_block
!= NULL
&& sym
->common_block
->is_bind_c
== 1
1079 && sym
->ts
.is_c_interop
!= 1)
1081 gfc_error_now ("Variable '%s' in common block '%s' at %C "
1082 "must be declared with a C interoperable "
1083 "kind since common block '%s' is BIND(C)",
1084 sym
->name
, sym
->common_block
->name
,
1085 sym
->common_block
->name
);
1090 sym
->attr
.implied_index
= 0;
1096 /* Set character constant to the given length. The constant will be padded or
1097 truncated. If we're inside an array constructor without a typespec, we
1098 additionally check that all elements have the same length; check_len -1
1099 means no checking. */
1102 gfc_set_constant_character_len (int len
, gfc_expr
*expr
, int check_len
)
1107 gcc_assert (expr
->expr_type
== EXPR_CONSTANT
);
1108 gcc_assert (expr
->ts
.type
== BT_CHARACTER
);
1110 slen
= expr
->value
.character
.length
;
1113 s
= gfc_get_wide_string (len
+ 1);
1114 memcpy (s
, expr
->value
.character
.string
,
1115 MIN (len
, slen
) * sizeof (gfc_char_t
));
1117 gfc_wide_memset (&s
[slen
], ' ', len
- slen
);
1119 if (gfc_option
.warn_character_truncation
&& slen
> len
)
1120 gfc_warning_now ("CHARACTER expression at %L is being truncated "
1121 "(%d/%d)", &expr
->where
, slen
, len
);
1123 /* Apply the standard by 'hand' otherwise it gets cleared for
1125 if (check_len
!= -1 && slen
!= check_len
1126 && !(gfc_option
.allow_std
& GFC_STD_GNU
))
1127 gfc_error_now ("The CHARACTER elements of the array constructor "
1128 "at %L must have the same length (%d/%d)",
1129 &expr
->where
, slen
, check_len
);
1132 gfc_free (expr
->value
.character
.string
);
1133 expr
->value
.character
.string
= s
;
1134 expr
->value
.character
.length
= len
;
1139 /* Function to create and update the enumerator history
1140 using the information passed as arguments.
1141 Pointer "max_enum" is also updated, to point to
1142 enum history node containing largest initializer.
1144 SYM points to the symbol node of enumerator.
1145 INIT points to its enumerator value. */
1148 create_enum_history (gfc_symbol
*sym
, gfc_expr
*init
)
1150 enumerator_history
*new_enum_history
;
1151 gcc_assert (sym
!= NULL
&& init
!= NULL
);
1153 new_enum_history
= XCNEW (enumerator_history
);
1155 new_enum_history
->sym
= sym
;
1156 new_enum_history
->initializer
= init
;
1157 new_enum_history
->next
= NULL
;
1159 if (enum_history
== NULL
)
1161 enum_history
= new_enum_history
;
1162 max_enum
= enum_history
;
1166 new_enum_history
->next
= enum_history
;
1167 enum_history
= new_enum_history
;
1169 if (mpz_cmp (max_enum
->initializer
->value
.integer
,
1170 new_enum_history
->initializer
->value
.integer
) < 0)
1171 max_enum
= new_enum_history
;
1176 /* Function to free enum kind history. */
1179 gfc_free_enum_history (void)
1181 enumerator_history
*current
= enum_history
;
1182 enumerator_history
*next
;
1184 while (current
!= NULL
)
1186 next
= current
->next
;
1191 enum_history
= NULL
;
1195 /* Function called by variable_decl() that adds an initialization
1196 expression to a symbol. */
1199 add_init_expr_to_sym (const char *name
, gfc_expr
**initp
, locus
*var_locus
)
1201 symbol_attribute attr
;
1206 if (find_special (name
, &sym
))
1211 /* If this symbol is confirming an implicit parameter type,
1212 then an initialization expression is not allowed. */
1213 if (attr
.flavor
== FL_PARAMETER
1214 && sym
->value
!= NULL
1217 gfc_error ("Initializer not allowed for PARAMETER '%s' at %C",
1224 /* An initializer is required for PARAMETER declarations. */
1225 if (attr
.flavor
== FL_PARAMETER
)
1227 gfc_error ("PARAMETER at %L is missing an initializer", var_locus
);
1233 /* If a variable appears in a DATA block, it cannot have an
1237 gfc_error ("Variable '%s' at %C with an initializer already "
1238 "appears in a DATA statement", sym
->name
);
1242 /* Check if the assignment can happen. This has to be put off
1243 until later for a derived type variable. */
1244 if (sym
->ts
.type
!= BT_DERIVED
&& init
->ts
.type
!= BT_DERIVED
1245 && gfc_check_assign_symbol (sym
, init
) == FAILURE
)
1248 if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.cl
)
1250 /* Update symbol character length according initializer. */
1251 if (sym
->ts
.cl
->length
== NULL
)
1254 /* If there are multiple CHARACTER variables declared on the
1255 same line, we don't want them to share the same length. */
1256 sym
->ts
.cl
= gfc_get_charlen ();
1257 sym
->ts
.cl
->next
= gfc_current_ns
->cl_list
;
1258 gfc_current_ns
->cl_list
= sym
->ts
.cl
;
1260 if (sym
->attr
.flavor
== FL_PARAMETER
)
1262 if (init
->expr_type
== EXPR_CONSTANT
)
1264 clen
= init
->value
.character
.length
;
1265 sym
->ts
.cl
->length
= gfc_int_expr (clen
);
1267 else if (init
->expr_type
== EXPR_ARRAY
)
1269 gfc_expr
*p
= init
->value
.constructor
->expr
;
1270 clen
= p
->value
.character
.length
;
1271 sym
->ts
.cl
->length
= gfc_int_expr (clen
);
1273 else if (init
->ts
.cl
&& init
->ts
.cl
->length
)
1274 sym
->ts
.cl
->length
=
1275 gfc_copy_expr (sym
->value
->ts
.cl
->length
);
1278 /* Update initializer character length according symbol. */
1279 else if (sym
->ts
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1281 int len
= mpz_get_si (sym
->ts
.cl
->length
->value
.integer
);
1282 gfc_constructor
* p
;
1284 if (init
->expr_type
== EXPR_CONSTANT
)
1285 gfc_set_constant_character_len (len
, init
, -1);
1286 else if (init
->expr_type
== EXPR_ARRAY
)
1288 /* Build a new charlen to prevent simplification from
1289 deleting the length before it is resolved. */
1290 init
->ts
.cl
= gfc_get_charlen ();
1291 init
->ts
.cl
->next
= gfc_current_ns
->cl_list
;
1292 gfc_current_ns
->cl_list
= sym
->ts
.cl
;
1293 init
->ts
.cl
->length
= gfc_copy_expr (sym
->ts
.cl
->length
);
1295 for (p
= init
->value
.constructor
; p
; p
= p
->next
)
1296 gfc_set_constant_character_len (len
, p
->expr
, -1);
1301 /* Need to check if the expression we initialized this
1302 to was one of the iso_c_binding named constants. If so,
1303 and we're a parameter (constant), let it be iso_c.
1305 integer(c_int), parameter :: my_int = c_int
1306 integer(my_int) :: my_int_2
1307 If we mark my_int as iso_c (since we can see it's value
1308 is equal to one of the named constants), then my_int_2
1309 will be considered C interoperable. */
1310 if (sym
->ts
.type
!= BT_CHARACTER
&& sym
->ts
.type
!= BT_DERIVED
)
1312 sym
->ts
.is_iso_c
|= init
->ts
.is_iso_c
;
1313 sym
->ts
.is_c_interop
|= init
->ts
.is_c_interop
;
1314 /* attr bits needed for module files. */
1315 sym
->attr
.is_iso_c
|= init
->ts
.is_iso_c
;
1316 sym
->attr
.is_c_interop
|= init
->ts
.is_c_interop
;
1317 if (init
->ts
.is_iso_c
)
1318 sym
->ts
.f90_type
= init
->ts
.f90_type
;
1321 /* Add initializer. Make sure we keep the ranks sane. */
1322 if (sym
->attr
.dimension
&& init
->rank
== 0)
1328 if (sym
->attr
.flavor
== FL_PARAMETER
1329 && init
->expr_type
== EXPR_CONSTANT
1330 && spec_size (sym
->as
, &size
) == SUCCESS
1331 && mpz_cmp_si (size
, 0) > 0)
1333 array
= gfc_start_constructor (init
->ts
.type
, init
->ts
.kind
,
1336 array
->value
.constructor
= c
= NULL
;
1337 for (n
= 0; n
< (int)mpz_get_si (size
); n
++)
1339 if (array
->value
.constructor
== NULL
)
1341 array
->value
.constructor
= c
= gfc_get_constructor ();
1346 c
->next
= gfc_get_constructor ();
1348 c
->expr
= gfc_copy_expr (init
);
1352 array
->shape
= gfc_get_shape (sym
->as
->rank
);
1353 for (n
= 0; n
< sym
->as
->rank
; n
++)
1354 spec_dimen_size (sym
->as
, n
, &array
->shape
[n
]);
1359 init
->rank
= sym
->as
->rank
;
1363 if (sym
->attr
.save
== SAVE_NONE
)
1364 sym
->attr
.save
= SAVE_IMPLICIT
;
1372 /* Function called by variable_decl() that adds a name to a structure
1376 build_struct (const char *name
, gfc_charlen
*cl
, gfc_expr
**init
,
1377 gfc_array_spec
**as
)
1381 /* If the current symbol is of the same derived type that we're
1382 constructing, it must have the pointer attribute. */
1383 if (current_ts
.type
== BT_DERIVED
1384 && current_ts
.derived
== gfc_current_block ()
1385 && current_attr
.pointer
== 0)
1387 gfc_error ("Component at %C must have the POINTER attribute");
1391 if (gfc_current_block ()->attr
.pointer
&& (*as
)->rank
!= 0)
1393 if ((*as
)->type
!= AS_DEFERRED
&& (*as
)->type
!= AS_EXPLICIT
)
1395 gfc_error ("Array component of structure at %C must have explicit "
1396 "or deferred shape");
1401 if (gfc_add_component (gfc_current_block (), name
, &c
) == FAILURE
)
1406 c
->attr
= current_attr
;
1408 c
->initializer
= *init
;
1413 c
->attr
.dimension
= 1;
1416 /* Should this ever get more complicated, combine with similar section
1417 in add_init_expr_to_sym into a separate function. */
1418 if (c
->ts
.type
== BT_CHARACTER
&& !c
->attr
.pointer
&& c
->initializer
&& c
->ts
.cl
1419 && c
->ts
.cl
->length
&& c
->ts
.cl
->length
->expr_type
== EXPR_CONSTANT
)
1423 gcc_assert (c
->ts
.cl
&& c
->ts
.cl
->length
);
1424 gcc_assert (c
->ts
.cl
->length
->expr_type
== EXPR_CONSTANT
);
1425 gcc_assert (c
->ts
.cl
->length
->ts
.type
== BT_INTEGER
);
1427 len
= mpz_get_si (c
->ts
.cl
->length
->value
.integer
);
1429 if (c
->initializer
->expr_type
== EXPR_CONSTANT
)
1430 gfc_set_constant_character_len (len
, c
->initializer
, -1);
1431 else if (mpz_cmp (c
->ts
.cl
->length
->value
.integer
,
1432 c
->initializer
->ts
.cl
->length
->value
.integer
))
1435 gfc_constructor
*ctor
= c
->initializer
->value
.constructor
;
1440 has_ts
= (c
->initializer
->ts
.cl
1441 && c
->initializer
->ts
.cl
->length_from_typespec
);
1443 for (; ctor
; ctor
= ctor
->next
)
1445 /* Remember the length of the first element for checking that
1446 all elements *in the constructor* have the same length. This
1447 need not be the length of the LHS! */
1450 gcc_assert (ctor
->expr
->expr_type
== EXPR_CONSTANT
);
1451 gcc_assert (ctor
->expr
->ts
.type
== BT_CHARACTER
);
1452 first_len
= ctor
->expr
->value
.character
.length
;
1456 if (ctor
->expr
->expr_type
== EXPR_CONSTANT
)
1457 gfc_set_constant_character_len (len
, ctor
->expr
,
1458 has_ts
? -1 : first_len
);
1463 /* Check array components. */
1464 if (!c
->attr
.dimension
)
1466 if (c
->attr
.allocatable
)
1468 gfc_error ("Allocatable component at %C must be an array");
1475 if (c
->attr
.pointer
)
1477 if (c
->as
->type
!= AS_DEFERRED
)
1479 gfc_error ("Pointer array component of structure at %C must have a "
1484 else if (c
->attr
.allocatable
)
1486 if (c
->as
->type
!= AS_DEFERRED
)
1488 gfc_error ("Allocatable component of structure at %C must have a "
1495 if (c
->as
->type
!= AS_EXPLICIT
)
1497 gfc_error ("Array component of structure at %C must have an "
1507 /* Match a 'NULL()', and possibly take care of some side effects. */
1510 gfc_match_null (gfc_expr
**result
)
1516 m
= gfc_match (" null ( )");
1520 /* The NULL symbol now has to be/become an intrinsic function. */
1521 if (gfc_get_symbol ("null", NULL
, &sym
))
1523 gfc_error ("NULL() initialization at %C is ambiguous");
1527 gfc_intrinsic_symbol (sym
);
1529 if (sym
->attr
.proc
!= PROC_INTRINSIC
1530 && (gfc_add_procedure (&sym
->attr
, PROC_INTRINSIC
,
1531 sym
->name
, NULL
) == FAILURE
1532 || gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
))
1535 e
= gfc_get_expr ();
1536 e
->where
= gfc_current_locus
;
1537 e
->expr_type
= EXPR_NULL
;
1538 e
->ts
.type
= BT_UNKNOWN
;
1546 /* Match a variable name with an optional initializer. When this
1547 subroutine is called, a variable is expected to be parsed next.
1548 Depending on what is happening at the moment, updates either the
1549 symbol table or the current interface. */
1552 variable_decl (int elem
)
1554 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
1555 gfc_expr
*initializer
, *char_len
;
1557 gfc_array_spec
*cp_as
; /* Extra copy for Cray Pointees. */
1568 old_locus
= gfc_current_locus
;
1570 /* When we get here, we've just matched a list of attributes and
1571 maybe a type and a double colon. The next thing we expect to see
1572 is the name of the symbol. */
1573 m
= gfc_match_name (name
);
1577 var_locus
= gfc_current_locus
;
1579 /* Now we could see the optional array spec. or character length. */
1580 m
= gfc_match_array_spec (&as
);
1581 if (gfc_option
.flag_cray_pointer
&& m
== MATCH_YES
)
1582 cp_as
= gfc_copy_array_spec (as
);
1583 else if (m
== MATCH_ERROR
)
1587 as
= gfc_copy_array_spec (current_as
);
1592 if (current_ts
.type
== BT_CHARACTER
)
1594 switch (match_char_length (&char_len
))
1597 cl
= gfc_get_charlen ();
1598 cl
->next
= gfc_current_ns
->cl_list
;
1599 gfc_current_ns
->cl_list
= cl
;
1601 cl
->length
= char_len
;
1604 /* Non-constant lengths need to be copied after the first
1605 element. Also copy assumed lengths. */
1608 && (current_ts
.cl
->length
== NULL
1609 || current_ts
.cl
->length
->expr_type
!= EXPR_CONSTANT
))
1611 cl
= gfc_get_charlen ();
1612 cl
->next
= gfc_current_ns
->cl_list
;
1613 gfc_current_ns
->cl_list
= cl
;
1614 cl
->length
= gfc_copy_expr (current_ts
.cl
->length
);
1626 /* If this symbol has already shown up in a Cray Pointer declaration,
1627 then we want to set the type & bail out. */
1628 if (gfc_option
.flag_cray_pointer
)
1630 gfc_find_symbol (name
, gfc_current_ns
, 1, &sym
);
1631 if (sym
!= NULL
&& sym
->attr
.cray_pointee
)
1633 sym
->ts
.type
= current_ts
.type
;
1634 sym
->ts
.kind
= current_ts
.kind
;
1636 sym
->ts
.derived
= current_ts
.derived
;
1637 sym
->ts
.is_c_interop
= current_ts
.is_c_interop
;
1638 sym
->ts
.is_iso_c
= current_ts
.is_iso_c
;
1641 /* Check to see if we have an array specification. */
1644 if (sym
->as
!= NULL
)
1646 gfc_error ("Duplicate array spec for Cray pointee at %C");
1647 gfc_free_array_spec (cp_as
);
1653 if (gfc_set_array_spec (sym
, cp_as
, &var_locus
) == FAILURE
)
1654 gfc_internal_error ("Couldn't set pointee array spec.");
1656 /* Fix the array spec. */
1657 m
= gfc_mod_pointee_as (sym
->as
);
1658 if (m
== MATCH_ERROR
)
1666 gfc_free_array_spec (cp_as
);
1670 /* Procedure pointer as function result. */
1671 if (gfc_current_state () == COMP_FUNCTION
1672 && strcmp ("ppr@", gfc_current_block ()->name
) == 0
1673 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) == 0)
1674 strcpy (name
, "ppr@");
1676 if (gfc_current_state () == COMP_FUNCTION
1677 && strcmp (name
, gfc_current_block ()->name
) == 0
1678 && gfc_current_block ()->result
1679 && strcmp ("ppr@", gfc_current_block ()->result
->name
) == 0)
1680 strcpy (name
, "ppr@");
1682 /* OK, we've successfully matched the declaration. Now put the
1683 symbol in the current namespace, because it might be used in the
1684 optional initialization expression for this symbol, e.g. this is
1687 integer, parameter :: i = huge(i)
1689 This is only true for parameters or variables of a basic type.
1690 For components of derived types, it is not true, so we don't
1691 create a symbol for those yet. If we fail to create the symbol,
1693 if (gfc_current_state () != COMP_DERIVED
1694 && build_sym (name
, cl
, &as
, &var_locus
) == FAILURE
)
1700 /* An interface body specifies all of the procedure's
1701 characteristics and these shall be consistent with those
1702 specified in the procedure definition, except that the interface
1703 may specify a procedure that is not pure if the procedure is
1704 defined to be pure(12.3.2). */
1705 if (current_ts
.type
== BT_DERIVED
1706 && gfc_current_ns
->proc_name
1707 && gfc_current_ns
->proc_name
->attr
.if_source
== IFSRC_IFBODY
1708 && current_ts
.derived
->ns
!= gfc_current_ns
)
1711 st
= gfc_find_symtree (gfc_current_ns
->sym_root
, current_ts
.derived
->name
);
1712 if (!(current_ts
.derived
->attr
.imported
1714 && st
->n
.sym
== current_ts
.derived
)
1715 && !gfc_current_ns
->has_import_set
)
1717 gfc_error ("the type of '%s' at %C has not been declared within the "
1724 /* In functions that have a RESULT variable defined, the function
1725 name always refers to function calls. Therefore, the name is
1726 not allowed to appear in specification statements. */
1727 if (gfc_current_state () == COMP_FUNCTION
1728 && gfc_current_block () != NULL
1729 && gfc_current_block ()->result
!= NULL
1730 && gfc_current_block ()->result
!= gfc_current_block ()
1731 && strcmp (gfc_current_block ()->name
, name
) == 0)
1733 gfc_error ("Function name '%s' not allowed at %C", name
);
1738 /* We allow old-style initializations of the form
1739 integer i /2/, j(4) /3*3, 1/
1740 (if no colon has been seen). These are different from data
1741 statements in that initializers are only allowed to apply to the
1742 variable immediately preceding, i.e.
1744 is not allowed. Therefore we have to do some work manually, that
1745 could otherwise be left to the matchers for DATA statements. */
1747 if (!colon_seen
&& gfc_match (" /") == MATCH_YES
)
1749 if (gfc_notify_std (GFC_STD_GNU
, "Extension: Old-style "
1750 "initialization at %C") == FAILURE
)
1753 return match_old_style_init (name
);
1756 /* The double colon must be present in order to have initializers.
1757 Otherwise the statement is ambiguous with an assignment statement. */
1760 if (gfc_match (" =>") == MATCH_YES
)
1762 if (!current_attr
.pointer
)
1764 gfc_error ("Initialization at %C isn't for a pointer variable");
1769 m
= gfc_match_null (&initializer
);
1772 gfc_error ("Pointer initialization requires a NULL() at %C");
1776 if (gfc_pure (NULL
))
1778 gfc_error ("Initialization of pointer at %C is not allowed in "
1779 "a PURE procedure");
1787 else if (gfc_match_char ('=') == MATCH_YES
)
1789 if (current_attr
.pointer
)
1791 gfc_error ("Pointer initialization at %C requires '=>', "
1797 m
= gfc_match_init_expr (&initializer
);
1800 gfc_error ("Expected an initialization expression at %C");
1804 if (current_attr
.flavor
!= FL_PARAMETER
&& gfc_pure (NULL
))
1806 gfc_error ("Initialization of variable at %C is not allowed in "
1807 "a PURE procedure");
1816 if (initializer
!= NULL
&& current_attr
.allocatable
1817 && gfc_current_state () == COMP_DERIVED
)
1819 gfc_error ("Initialization of allocatable component at %C is not "
1825 /* Add the initializer. Note that it is fine if initializer is
1826 NULL here, because we sometimes also need to check if a
1827 declaration *must* have an initialization expression. */
1828 if (gfc_current_state () != COMP_DERIVED
)
1829 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
1832 if (current_ts
.type
== BT_DERIVED
1833 && !current_attr
.pointer
&& !initializer
)
1834 initializer
= gfc_default_initializer (¤t_ts
);
1835 t
= build_struct (name
, cl
, &initializer
, &as
);
1838 m
= (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
1841 /* Free stuff up and return. */
1842 gfc_free_expr (initializer
);
1843 gfc_free_array_spec (as
);
1849 /* Match an extended-f77 "TYPESPEC*bytesize"-style kind specification.
1850 This assumes that the byte size is equal to the kind number for
1851 non-COMPLEX types, and equal to twice the kind number for COMPLEX. */
1854 gfc_match_old_kind_spec (gfc_typespec
*ts
)
1859 if (gfc_match_char ('*') != MATCH_YES
)
1862 m
= gfc_match_small_literal_int (&ts
->kind
, NULL
);
1866 original_kind
= ts
->kind
;
1868 /* Massage the kind numbers for complex types. */
1869 if (ts
->type
== BT_COMPLEX
)
1873 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1874 gfc_basic_typename (ts
->type
), original_kind
);
1880 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
1882 gfc_error ("Old-style type declaration %s*%d not supported at %C",
1883 gfc_basic_typename (ts
->type
), original_kind
);
1887 if (gfc_notify_std (GFC_STD_GNU
, "Nonstandard type declaration %s*%d at %C",
1888 gfc_basic_typename (ts
->type
), original_kind
) == FAILURE
)
1895 /* Match a kind specification. Since kinds are generally optional, we
1896 usually return MATCH_NO if something goes wrong. If a "kind="
1897 string is found, then we know we have an error. */
1900 gfc_match_kind_spec (gfc_typespec
*ts
, bool kind_expr_only
)
1912 where
= loc
= gfc_current_locus
;
1917 if (gfc_match_char ('(') == MATCH_NO
)
1920 /* Also gobbles optional text. */
1921 if (gfc_match (" kind = ") == MATCH_YES
)
1924 loc
= gfc_current_locus
;
1927 n
= gfc_match_init_expr (&e
);
1931 if (gfc_matching_function
)
1933 /* The function kind expression might include use associated or
1934 imported parameters and try again after the specification
1936 if (gfc_match_char (')') != MATCH_YES
)
1938 gfc_error ("Missing right parenthesis at %C");
1944 gfc_undo_symbols ();
1949 /* ....or else, the match is real. */
1951 gfc_error ("Expected initialization expression at %C");
1959 gfc_error ("Expected scalar initialization expression at %C");
1964 msg
= gfc_extract_int (e
, &ts
->kind
);
1973 /* Before throwing away the expression, let's see if we had a
1974 C interoperable kind (and store the fact). */
1975 if (e
->ts
.is_c_interop
== 1)
1977 /* Mark this as c interoperable if being declared with one
1978 of the named constants from iso_c_binding. */
1979 ts
->is_c_interop
= e
->ts
.is_iso_c
;
1980 ts
->f90_type
= e
->ts
.f90_type
;
1986 /* Ignore errors to this point, if we've gotten here. This means
1987 we ignore the m=MATCH_ERROR from above. */
1988 if (gfc_validate_kind (ts
->type
, ts
->kind
, true) < 0)
1990 gfc_error ("Kind %d not supported for type %s at %C", ts
->kind
,
1991 gfc_basic_typename (ts
->type
));
1992 gfc_current_locus
= where
;
1996 /* Warn if, e.g., c_int is used for a REAL variable, but not
1997 if, e.g., c_double is used for COMPLEX as the standard
1998 explicitly says that the kind type parameter for complex and real
1999 variable is the same, i.e. c_float == c_float_complex. */
2000 if (ts
->f90_type
!= BT_UNKNOWN
&& ts
->f90_type
!= ts
->type
2001 && !((ts
->f90_type
== BT_REAL
&& ts
->type
== BT_COMPLEX
)
2002 || (ts
->f90_type
== BT_COMPLEX
&& ts
->type
== BT_REAL
)))
2003 gfc_error_now ("C kind type parameter is for type %s but type at %L "
2004 "is %s", gfc_basic_typename (ts
->f90_type
), &where
,
2005 gfc_basic_typename (ts
->type
));
2007 gfc_gobble_whitespace ();
2008 if ((c
= gfc_next_ascii_char ()) != ')'
2009 && (ts
->type
!= BT_CHARACTER
|| c
!= ','))
2011 if (ts
->type
== BT_CHARACTER
)
2012 gfc_error ("Missing right parenthesis or comma at %C");
2014 gfc_error ("Missing right parenthesis at %C");
2018 /* All tests passed. */
2021 if(m
== MATCH_ERROR
)
2022 gfc_current_locus
= where
;
2024 /* Return what we know from the test(s). */
2029 gfc_current_locus
= where
;
2035 match_char_kind (int * kind
, int * is_iso_c
)
2044 where
= gfc_current_locus
;
2046 n
= gfc_match_init_expr (&e
);
2048 if (n
!= MATCH_YES
&& gfc_matching_function
)
2050 /* The expression might include use-associated or imported
2051 parameters and try again after the specification
2054 gfc_undo_symbols ();
2059 gfc_error ("Expected initialization expression at %C");
2065 gfc_error ("Expected scalar initialization expression at %C");
2070 msg
= gfc_extract_int (e
, kind
);
2071 *is_iso_c
= e
->ts
.is_iso_c
;
2081 /* Ignore errors to this point, if we've gotten here. This means
2082 we ignore the m=MATCH_ERROR from above. */
2083 if (gfc_validate_kind (BT_CHARACTER
, *kind
, true) < 0)
2085 gfc_error ("Kind %d is not supported for CHARACTER at %C", *kind
);
2089 /* All tests passed. */
2092 if (m
== MATCH_ERROR
)
2093 gfc_current_locus
= where
;
2095 /* Return what we know from the test(s). */
2100 gfc_current_locus
= where
;
2104 /* Match the various kind/length specifications in a CHARACTER
2105 declaration. We don't return MATCH_NO. */
2108 match_char_spec (gfc_typespec
*ts
)
2110 int kind
, seen_length
, is_iso_c
;
2120 /* Try the old-style specification first. */
2121 old_char_selector
= 0;
2123 m
= match_char_length (&len
);
2127 old_char_selector
= 1;
2132 m
= gfc_match_char ('(');
2135 m
= MATCH_YES
; /* Character without length is a single char. */
2139 /* Try the weird case: ( KIND = <int> [ , LEN = <len-param> ] ). */
2140 if (gfc_match (" kind =") == MATCH_YES
)
2142 m
= match_char_kind (&kind
, &is_iso_c
);
2144 if (m
== MATCH_ERROR
)
2149 if (gfc_match (" , len =") == MATCH_NO
)
2152 m
= char_len_param_value (&len
);
2155 if (m
== MATCH_ERROR
)
2162 /* Try to match "LEN = <len-param>" or "LEN = <len-param>, KIND = <int>". */
2163 if (gfc_match (" len =") == MATCH_YES
)
2165 m
= char_len_param_value (&len
);
2168 if (m
== MATCH_ERROR
)
2172 if (gfc_match_char (')') == MATCH_YES
)
2175 if (gfc_match (" , kind =") != MATCH_YES
)
2178 if (match_char_kind (&kind
, &is_iso_c
) == MATCH_ERROR
)
2184 /* Try to match ( <len-param> ) or ( <len-param> , [ KIND = ] <int> ). */
2185 m
= char_len_param_value (&len
);
2188 if (m
== MATCH_ERROR
)
2192 m
= gfc_match_char (')');
2196 if (gfc_match_char (',') != MATCH_YES
)
2199 gfc_match (" kind ="); /* Gobble optional text. */
2201 m
= match_char_kind (&kind
, &is_iso_c
);
2202 if (m
== MATCH_ERROR
)
2208 /* Require a right-paren at this point. */
2209 m
= gfc_match_char (')');
2214 gfc_error ("Syntax error in CHARACTER declaration at %C");
2216 gfc_free_expr (len
);
2220 /* Deal with character functions after USE and IMPORT statements. */
2221 if (gfc_matching_function
)
2223 gfc_free_expr (len
);
2224 gfc_undo_symbols ();
2230 gfc_free_expr (len
);
2234 /* Do some final massaging of the length values. */
2235 cl
= gfc_get_charlen ();
2236 cl
->next
= gfc_current_ns
->cl_list
;
2237 gfc_current_ns
->cl_list
= cl
;
2239 if (seen_length
== 0)
2240 cl
->length
= gfc_int_expr (1);
2245 ts
->kind
= kind
== 0 ? gfc_default_character_kind
: kind
;
2247 /* We have to know if it was a c interoperable kind so we can
2248 do accurate type checking of bind(c) procs, etc. */
2250 /* Mark this as c interoperable if being declared with one
2251 of the named constants from iso_c_binding. */
2252 ts
->is_c_interop
= is_iso_c
;
2253 else if (len
!= NULL
)
2254 /* Here, we might have parsed something such as: character(c_char)
2255 In this case, the parsing code above grabs the c_char when
2256 looking for the length (line 1690, roughly). it's the last
2257 testcase for parsing the kind params of a character variable.
2258 However, it's not actually the length. this seems like it
2260 To see if the user used a C interop kind, test the expr
2261 of the so called length, and see if it's C interoperable. */
2262 ts
->is_c_interop
= len
->ts
.is_iso_c
;
2268 /* Matches a type specification. If successful, sets the ts structure
2269 to the matched specification. This is necessary for FUNCTION and
2270 IMPLICIT statements.
2272 If implicit_flag is nonzero, then we don't check for the optional
2273 kind specification. Not doing so is needed for matching an IMPLICIT
2274 statement correctly. */
2277 gfc_match_type_spec (gfc_typespec
*ts
, int implicit_flag
)
2279 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2283 bool seen_deferred_kind
;
2285 /* A belt and braces check that the typespec is correctly being treated
2286 as a deferred characteristic association. */
2287 seen_deferred_kind
= (gfc_current_state () == COMP_FUNCTION
)
2288 && (gfc_current_block ()->result
->ts
.kind
== -1)
2289 && (ts
->kind
== -1);
2291 if (seen_deferred_kind
)
2294 /* Clear the current binding label, in case one is given. */
2295 curr_binding_label
[0] = '\0';
2297 if (gfc_match (" byte") == MATCH_YES
)
2299 if (gfc_notify_std(GFC_STD_GNU
, "Extension: BYTE type at %C")
2303 if (gfc_validate_kind (BT_INTEGER
, 1, true) < 0)
2305 gfc_error ("BYTE type used at %C "
2306 "is not available on the target machine");
2310 ts
->type
= BT_INTEGER
;
2315 if (gfc_match (" integer") == MATCH_YES
)
2317 ts
->type
= BT_INTEGER
;
2318 ts
->kind
= gfc_default_integer_kind
;
2322 if (gfc_match (" character") == MATCH_YES
)
2324 ts
->type
= BT_CHARACTER
;
2325 if (implicit_flag
== 0)
2326 return match_char_spec (ts
);
2331 if (gfc_match (" real") == MATCH_YES
)
2334 ts
->kind
= gfc_default_real_kind
;
2338 if (gfc_match (" double precision") == MATCH_YES
)
2341 ts
->kind
= gfc_default_double_kind
;
2345 if (gfc_match (" complex") == MATCH_YES
)
2347 ts
->type
= BT_COMPLEX
;
2348 ts
->kind
= gfc_default_complex_kind
;
2352 if (gfc_match (" double complex") == MATCH_YES
)
2354 if (gfc_notify_std (GFC_STD_GNU
, "DOUBLE COMPLEX at %C does not "
2355 "conform to the Fortran 95 standard") == FAILURE
)
2358 ts
->type
= BT_COMPLEX
;
2359 ts
->kind
= gfc_default_double_kind
;
2363 if (gfc_match (" logical") == MATCH_YES
)
2365 ts
->type
= BT_LOGICAL
;
2366 ts
->kind
= gfc_default_logical_kind
;
2370 m
= gfc_match (" type ( %n )", name
);
2374 ts
->type
= BT_DERIVED
;
2376 /* Defer association of the derived type until the end of the
2377 specification block. However, if the derived type can be
2378 found, add it to the typespec. */
2379 if (gfc_matching_function
)
2382 if (gfc_current_state () != COMP_INTERFACE
2383 && !gfc_find_symbol (name
, NULL
, 1, &sym
) && sym
)
2388 /* Search for the name but allow the components to be defined later. If
2389 type = -1, this typespec has been seen in a function declaration but
2390 the type could not be accessed at that point. */
2392 if (ts
->kind
!= -1 && gfc_get_ha_symbol (name
, &sym
))
2394 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2397 else if (ts
->kind
== -1)
2399 int iface
= gfc_state_stack
->previous
->state
!= COMP_INTERFACE
2400 || gfc_current_ns
->has_import_set
;
2401 if (gfc_find_symbol (name
, NULL
, iface
, &sym
))
2403 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2412 if (sym
->attr
.flavor
!= FL_DERIVED
2413 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
2416 gfc_set_sym_referenced (sym
);
2422 /* For all types except double, derived and character, look for an
2423 optional kind specifier. MATCH_NO is actually OK at this point. */
2424 if (implicit_flag
== 1)
2427 if (gfc_current_form
== FORM_FREE
)
2429 c
= gfc_peek_ascii_char();
2430 if (!gfc_is_whitespace(c
) && c
!= '*' && c
!= '('
2431 && c
!= ':' && c
!= ',')
2435 m
= gfc_match_kind_spec (ts
, false);
2436 if (m
== MATCH_NO
&& ts
->type
!= BT_CHARACTER
)
2437 m
= gfc_match_old_kind_spec (ts
);
2439 /* Defer association of the KIND expression of function results
2440 until after USE and IMPORT statements. */
2441 if ((gfc_current_state () == COMP_NONE
&& gfc_error_flag_test ())
2442 || gfc_matching_function
)
2446 m
= MATCH_YES
; /* No kind specifier found. */
2452 /* Match an IMPLICIT NONE statement. Actually, this statement is
2453 already matched in parse.c, or we would not end up here in the
2454 first place. So the only thing we need to check, is if there is
2455 trailing garbage. If not, the match is successful. */
2458 gfc_match_implicit_none (void)
2460 return (gfc_match_eos () == MATCH_YES
) ? MATCH_YES
: MATCH_NO
;
2464 /* Match the letter range(s) of an IMPLICIT statement. */
2467 match_implicit_range (void)
2473 cur_loc
= gfc_current_locus
;
2475 gfc_gobble_whitespace ();
2476 c
= gfc_next_ascii_char ();
2479 gfc_error ("Missing character range in IMPLICIT at %C");
2486 gfc_gobble_whitespace ();
2487 c1
= gfc_next_ascii_char ();
2491 gfc_gobble_whitespace ();
2492 c
= gfc_next_ascii_char ();
2497 inner
= 0; /* Fall through. */
2504 gfc_gobble_whitespace ();
2505 c2
= gfc_next_ascii_char ();
2509 gfc_gobble_whitespace ();
2510 c
= gfc_next_ascii_char ();
2512 if ((c
!= ',') && (c
!= ')'))
2525 gfc_error ("Letters must be in alphabetic order in "
2526 "IMPLICIT statement at %C");
2530 /* See if we can add the newly matched range to the pending
2531 implicits from this IMPLICIT statement. We do not check for
2532 conflicts with whatever earlier IMPLICIT statements may have
2533 set. This is done when we've successfully finished matching
2535 if (gfc_add_new_implicit_range (c1
, c2
) != SUCCESS
)
2542 gfc_syntax_error (ST_IMPLICIT
);
2544 gfc_current_locus
= cur_loc
;
2549 /* Match an IMPLICIT statement, storing the types for
2550 gfc_set_implicit() if the statement is accepted by the parser.
2551 There is a strange looking, but legal syntactic construction
2552 possible. It looks like:
2554 IMPLICIT INTEGER (a-b) (c-d)
2556 This is legal if "a-b" is a constant expression that happens to
2557 equal one of the legal kinds for integers. The real problem
2558 happens with an implicit specification that looks like:
2560 IMPLICIT INTEGER (a-b)
2562 In this case, a typespec matcher that is "greedy" (as most of the
2563 matchers are) gobbles the character range as a kindspec, leaving
2564 nothing left. We therefore have to go a bit more slowly in the
2565 matching process by inhibiting the kindspec checking during
2566 typespec matching and checking for a kind later. */
2569 gfc_match_implicit (void)
2578 /* We don't allow empty implicit statements. */
2579 if (gfc_match_eos () == MATCH_YES
)
2581 gfc_error ("Empty IMPLICIT statement at %C");
2587 /* First cleanup. */
2588 gfc_clear_new_implicit ();
2590 /* A basic type is mandatory here. */
2591 m
= gfc_match_type_spec (&ts
, 1);
2592 if (m
== MATCH_ERROR
)
2597 cur_loc
= gfc_current_locus
;
2598 m
= match_implicit_range ();
2602 /* We may have <TYPE> (<RANGE>). */
2603 gfc_gobble_whitespace ();
2604 c
= gfc_next_ascii_char ();
2605 if ((c
== '\n') || (c
== ','))
2607 /* Check for CHARACTER with no length parameter. */
2608 if (ts
.type
== BT_CHARACTER
&& !ts
.cl
)
2610 ts
.kind
= gfc_default_character_kind
;
2611 ts
.cl
= gfc_get_charlen ();
2612 ts
.cl
->next
= gfc_current_ns
->cl_list
;
2613 gfc_current_ns
->cl_list
= ts
.cl
;
2614 ts
.cl
->length
= gfc_int_expr (1);
2617 /* Record the Successful match. */
2618 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2623 gfc_current_locus
= cur_loc
;
2626 /* Discard the (incorrectly) matched range. */
2627 gfc_clear_new_implicit ();
2629 /* Last chance -- check <TYPE> <SELECTOR> (<RANGE>). */
2630 if (ts
.type
== BT_CHARACTER
)
2631 m
= match_char_spec (&ts
);
2634 m
= gfc_match_kind_spec (&ts
, false);
2637 m
= gfc_match_old_kind_spec (&ts
);
2638 if (m
== MATCH_ERROR
)
2644 if (m
== MATCH_ERROR
)
2647 m
= match_implicit_range ();
2648 if (m
== MATCH_ERROR
)
2653 gfc_gobble_whitespace ();
2654 c
= gfc_next_ascii_char ();
2655 if ((c
!= '\n') && (c
!= ','))
2658 if (gfc_merge_new_implicit (&ts
) != SUCCESS
)
2666 gfc_syntax_error (ST_IMPLICIT
);
2674 gfc_match_import (void)
2676 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
2681 if (gfc_current_ns
->proc_name
== NULL
2682 || gfc_current_ns
->proc_name
->attr
.if_source
!= IFSRC_IFBODY
)
2684 gfc_error ("IMPORT statement at %C only permitted in "
2685 "an INTERFACE body");
2689 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: IMPORT statement at %C")
2693 if (gfc_match_eos () == MATCH_YES
)
2695 /* All host variables should be imported. */
2696 gfc_current_ns
->has_import_set
= 1;
2700 if (gfc_match (" ::") == MATCH_YES
)
2702 if (gfc_match_eos () == MATCH_YES
)
2704 gfc_error ("Expecting list of named entities at %C");
2711 m
= gfc_match (" %n", name
);
2715 if (gfc_current_ns
->parent
!= NULL
2716 && gfc_find_symbol (name
, gfc_current_ns
->parent
, 1, &sym
))
2718 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2721 else if (gfc_current_ns
->proc_name
->ns
->parent
!= NULL
2722 && gfc_find_symbol (name
,
2723 gfc_current_ns
->proc_name
->ns
->parent
,
2726 gfc_error ("Type name '%s' at %C is ambiguous", name
);
2732 gfc_error ("Cannot IMPORT '%s' from host scoping unit "
2733 "at %C - does not exist.", name
);
2737 if (gfc_find_symtree (gfc_current_ns
->sym_root
,name
))
2739 gfc_warning ("'%s' is already IMPORTed from host scoping unit "
2744 st
= gfc_new_symtree (&gfc_current_ns
->sym_root
, name
);
2747 sym
->attr
.imported
= 1;
2759 if (gfc_match_eos () == MATCH_YES
)
2761 if (gfc_match_char (',') != MATCH_YES
)
2768 gfc_error ("Syntax error in IMPORT statement at %C");
2773 /* A minimal implementation of gfc_match without whitespace, escape
2774 characters or variable arguments. Returns true if the next
2775 characters match the TARGET template exactly. */
2778 match_string_p (const char *target
)
2782 for (p
= target
; *p
; p
++)
2783 if ((char) gfc_next_ascii_char () != *p
)
2788 /* Matches an attribute specification including array specs. If
2789 successful, leaves the variables current_attr and current_as
2790 holding the specification. Also sets the colon_seen variable for
2791 later use by matchers associated with initializations.
2793 This subroutine is a little tricky in the sense that we don't know
2794 if we really have an attr-spec until we hit the double colon.
2795 Until that time, we can only return MATCH_NO. This forces us to
2796 check for duplicate specification at this level. */
2799 match_attr_spec (void)
2801 /* Modifiers that can exist in a type statement. */
2803 { GFC_DECL_BEGIN
= 0,
2804 DECL_ALLOCATABLE
= GFC_DECL_BEGIN
, DECL_DIMENSION
, DECL_EXTERNAL
,
2805 DECL_IN
, DECL_OUT
, DECL_INOUT
, DECL_INTRINSIC
, DECL_OPTIONAL
,
2806 DECL_PARAMETER
, DECL_POINTER
, DECL_PROTECTED
, DECL_PRIVATE
,
2807 DECL_PUBLIC
, DECL_SAVE
, DECL_TARGET
, DECL_VALUE
, DECL_VOLATILE
,
2808 DECL_IS_BIND_C
, DECL_NONE
,
2809 GFC_DECL_END
/* Sentinel */
2813 /* GFC_DECL_END is the sentinel, index starts at 0. */
2814 #define NUM_DECL GFC_DECL_END
2816 locus start
, seen_at
[NUM_DECL
];
2823 gfc_clear_attr (¤t_attr
);
2824 start
= gfc_current_locus
;
2829 /* See if we get all of the keywords up to the final double colon. */
2830 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
2838 gfc_gobble_whitespace ();
2840 ch
= gfc_next_ascii_char ();
2843 /* This is the successful exit condition for the loop. */
2844 if (gfc_next_ascii_char () == ':')
2849 gfc_gobble_whitespace ();
2850 switch (gfc_peek_ascii_char ())
2853 if (match_string_p ("allocatable"))
2854 d
= DECL_ALLOCATABLE
;
2858 /* Try and match the bind(c). */
2859 m
= gfc_match_bind_c (NULL
, true);
2862 else if (m
== MATCH_ERROR
)
2867 if (match_string_p ("dimension"))
2872 if (match_string_p ("external"))
2877 if (match_string_p ("int"))
2879 ch
= gfc_next_ascii_char ();
2882 if (match_string_p ("nt"))
2884 /* Matched "intent". */
2885 /* TODO: Call match_intent_spec from here. */
2886 if (gfc_match (" ( in out )") == MATCH_YES
)
2888 else if (gfc_match (" ( in )") == MATCH_YES
)
2890 else if (gfc_match (" ( out )") == MATCH_YES
)
2896 if (match_string_p ("insic"))
2898 /* Matched "intrinsic". */
2906 if (match_string_p ("optional"))
2911 gfc_next_ascii_char ();
2912 switch (gfc_next_ascii_char ())
2915 if (match_string_p ("rameter"))
2917 /* Matched "parameter". */
2923 if (match_string_p ("inter"))
2925 /* Matched "pointer". */
2931 ch
= gfc_next_ascii_char ();
2934 if (match_string_p ("vate"))
2936 /* Matched "private". */
2942 if (match_string_p ("tected"))
2944 /* Matched "protected". */
2951 if (match_string_p ("blic"))
2953 /* Matched "public". */
2961 if (match_string_p ("save"))
2966 if (match_string_p ("target"))
2971 gfc_next_ascii_char ();
2972 ch
= gfc_next_ascii_char ();
2975 if (match_string_p ("lue"))
2977 /* Matched "value". */
2983 if (match_string_p ("latile"))
2985 /* Matched "volatile". */
2993 /* No double colon and no recognizable decl_type, so assume that
2994 we've been looking at something else the whole time. */
3001 /* Check to make sure any parens are paired up correctly. */
3002 if (gfc_match_parens () == MATCH_ERROR
)
3009 seen_at
[d
] = gfc_current_locus
;
3011 if (d
== DECL_DIMENSION
)
3013 m
= gfc_match_array_spec (¤t_as
);
3017 gfc_error ("Missing dimension specification at %C");
3021 if (m
== MATCH_ERROR
)
3026 /* Since we've seen a double colon, we have to be looking at an
3027 attr-spec. This means that we can now issue errors. */
3028 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3033 case DECL_ALLOCATABLE
:
3034 attr
= "ALLOCATABLE";
3036 case DECL_DIMENSION
:
3043 attr
= "INTENT (IN)";
3046 attr
= "INTENT (OUT)";
3049 attr
= "INTENT (IN OUT)";
3051 case DECL_INTRINSIC
:
3057 case DECL_PARAMETER
:
3063 case DECL_PROTECTED
:
3078 case DECL_IS_BIND_C
:
3088 attr
= NULL
; /* This shouldn't happen. */
3091 gfc_error ("Duplicate %s attribute at %L", attr
, &seen_at
[d
]);
3096 /* Now that we've dealt with duplicate attributes, add the attributes
3097 to the current attribute. */
3098 for (d
= GFC_DECL_BEGIN
; d
!= GFC_DECL_END
; d
++)
3103 if (gfc_current_state () == COMP_DERIVED
3104 && d
!= DECL_DIMENSION
&& d
!= DECL_POINTER
3105 && d
!= DECL_PRIVATE
&& d
!= DECL_PUBLIC
3108 if (d
== DECL_ALLOCATABLE
)
3110 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ALLOCATABLE "
3111 "attribute at %C in a TYPE definition")
3120 gfc_error ("Attribute at %L is not allowed in a TYPE definition",
3127 if ((d
== DECL_PRIVATE
|| d
== DECL_PUBLIC
)
3128 && gfc_current_state () != COMP_MODULE
)
3130 if (d
== DECL_PRIVATE
)
3134 if (gfc_current_state () == COMP_DERIVED
3135 && gfc_state_stack
->previous
3136 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
3138 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: Attribute %s "
3139 "at %L in a TYPE definition", attr
,
3149 gfc_error ("%s attribute at %L is not allowed outside of the "
3150 "specification part of a module", attr
, &seen_at
[d
]);
3158 case DECL_ALLOCATABLE
:
3159 t
= gfc_add_allocatable (¤t_attr
, &seen_at
[d
]);
3162 case DECL_DIMENSION
:
3163 t
= gfc_add_dimension (¤t_attr
, NULL
, &seen_at
[d
]);
3167 t
= gfc_add_external (¤t_attr
, &seen_at
[d
]);
3171 t
= gfc_add_intent (¤t_attr
, INTENT_IN
, &seen_at
[d
]);
3175 t
= gfc_add_intent (¤t_attr
, INTENT_OUT
, &seen_at
[d
]);
3179 t
= gfc_add_intent (¤t_attr
, INTENT_INOUT
, &seen_at
[d
]);
3182 case DECL_INTRINSIC
:
3183 t
= gfc_add_intrinsic (¤t_attr
, &seen_at
[d
]);
3187 t
= gfc_add_optional (¤t_attr
, &seen_at
[d
]);
3190 case DECL_PARAMETER
:
3191 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, &seen_at
[d
]);
3195 t
= gfc_add_pointer (¤t_attr
, &seen_at
[d
]);
3198 case DECL_PROTECTED
:
3199 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
3201 gfc_error ("PROTECTED at %C only allowed in specification "
3202 "part of a module");
3207 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED "
3212 t
= gfc_add_protected (¤t_attr
, NULL
, &seen_at
[d
]);
3216 t
= gfc_add_access (¤t_attr
, ACCESS_PRIVATE
, NULL
,
3221 t
= gfc_add_access (¤t_attr
, ACCESS_PUBLIC
, NULL
,
3226 t
= gfc_add_save (¤t_attr
, NULL
, &seen_at
[d
]);
3230 t
= gfc_add_target (¤t_attr
, &seen_at
[d
]);
3233 case DECL_IS_BIND_C
:
3234 t
= gfc_add_is_bind_c(¤t_attr
, NULL
, &seen_at
[d
], 0);
3238 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE attribute "
3243 t
= gfc_add_value (¤t_attr
, NULL
, &seen_at
[d
]);
3247 if (gfc_notify_std (GFC_STD_F2003
,
3248 "Fortran 2003: VOLATILE attribute at %C")
3252 t
= gfc_add_volatile (¤t_attr
, NULL
, &seen_at
[d
]);
3256 gfc_internal_error ("match_attr_spec(): Bad attribute");
3270 gfc_current_locus
= start
;
3271 gfc_free_array_spec (current_as
);
3277 /* Set the binding label, dest_label, either with the binding label
3278 stored in the given gfc_typespec, ts, or if none was provided, it
3279 will be the symbol name in all lower case, as required by the draft
3280 (J3/04-007, section 15.4.1). If a binding label was given and
3281 there is more than one argument (num_idents), it is an error. */
3284 set_binding_label (char *dest_label
, const char *sym_name
, int num_idents
)
3286 if (num_idents
> 1 && has_name_equals
)
3288 gfc_error ("Multiple identifiers provided with "
3289 "single NAME= specifier at %C");
3293 if (curr_binding_label
[0] != '\0')
3295 /* Binding label given; store in temp holder til have sym. */
3296 strcpy (dest_label
, curr_binding_label
);
3300 /* No binding label given, and the NAME= specifier did not exist,
3301 which means there was no NAME="". */
3302 if (sym_name
!= NULL
&& has_name_equals
== 0)
3303 strcpy (dest_label
, sym_name
);
3310 /* Set the status of the given common block as being BIND(C) or not,
3311 depending on the given parameter, is_bind_c. */
3314 set_com_block_bind_c (gfc_common_head
*com_block
, int is_bind_c
)
3316 com_block
->is_bind_c
= is_bind_c
;
3321 /* Verify that the given gfc_typespec is for a C interoperable type. */
3324 verify_c_interop (gfc_typespec
*ts
)
3326 if (ts
->type
== BT_DERIVED
&& ts
->derived
!= NULL
)
3327 return (ts
->derived
->ts
.is_c_interop
? SUCCESS
: FAILURE
);
3328 else if (ts
->is_c_interop
!= 1)
3335 /* Verify that the variables of a given common block, which has been
3336 defined with the attribute specifier bind(c), to be of a C
3337 interoperable type. Errors will be reported here, if
3341 verify_com_block_vars_c_interop (gfc_common_head
*com_block
)
3343 gfc_symbol
*curr_sym
= NULL
;
3344 gfc_try retval
= SUCCESS
;
3346 curr_sym
= com_block
->head
;
3348 /* Make sure we have at least one symbol. */
3349 if (curr_sym
== NULL
)
3352 /* Here we know we have a symbol, so we'll execute this loop
3356 /* The second to last param, 1, says this is in a common block. */
3357 retval
= verify_bind_c_sym (curr_sym
, &(curr_sym
->ts
), 1, com_block
);
3358 curr_sym
= curr_sym
->common_next
;
3359 } while (curr_sym
!= NULL
);
3365 /* Verify that a given BIND(C) symbol is C interoperable. If it is not,
3366 an appropriate error message is reported. */
3369 verify_bind_c_sym (gfc_symbol
*tmp_sym
, gfc_typespec
*ts
,
3370 int is_in_common
, gfc_common_head
*com_block
)
3372 bool bind_c_function
= false;
3373 gfc_try retval
= SUCCESS
;
3375 if (tmp_sym
->attr
.function
&& tmp_sym
->attr
.is_bind_c
)
3376 bind_c_function
= true;
3378 if (tmp_sym
->attr
.function
&& tmp_sym
->result
!= NULL
)
3380 tmp_sym
= tmp_sym
->result
;
3381 /* Make sure it wasn't an implicitly typed result. */
3382 if (tmp_sym
->attr
.implicit_type
)
3384 gfc_warning ("Implicitly declared BIND(C) function '%s' at "
3385 "%L may not be C interoperable", tmp_sym
->name
,
3386 &tmp_sym
->declared_at
);
3387 tmp_sym
->ts
.f90_type
= tmp_sym
->ts
.type
;
3388 /* Mark it as C interoperable to prevent duplicate warnings. */
3389 tmp_sym
->ts
.is_c_interop
= 1;
3390 tmp_sym
->attr
.is_c_interop
= 1;
3394 /* Here, we know we have the bind(c) attribute, so if we have
3395 enough type info, then verify that it's a C interop kind.
3396 The info could be in the symbol already, or possibly still in
3397 the given ts (current_ts), so look in both. */
3398 if (tmp_sym
->ts
.type
!= BT_UNKNOWN
|| ts
->type
!= BT_UNKNOWN
)
3400 if (verify_c_interop (&(tmp_sym
->ts
)) != SUCCESS
)
3402 /* See if we're dealing with a sym in a common block or not. */
3403 if (is_in_common
== 1)
3405 gfc_warning ("Variable '%s' in common block '%s' at %L "
3406 "may not be a C interoperable "
3407 "kind though common block '%s' is BIND(C)",
3408 tmp_sym
->name
, com_block
->name
,
3409 &(tmp_sym
->declared_at
), com_block
->name
);
3413 if (tmp_sym
->ts
.type
== BT_DERIVED
|| ts
->type
== BT_DERIVED
)
3414 gfc_error ("Type declaration '%s' at %L is not C "
3415 "interoperable but it is BIND(C)",
3416 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3418 gfc_warning ("Variable '%s' at %L "
3419 "may not be a C interoperable "
3420 "kind but it is bind(c)",
3421 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3425 /* Variables declared w/in a common block can't be bind(c)
3426 since there's no way for C to see these variables, so there's
3427 semantically no reason for the attribute. */
3428 if (is_in_common
== 1 && tmp_sym
->attr
.is_bind_c
== 1)
3430 gfc_error ("Variable '%s' in common block '%s' at "
3431 "%L cannot be declared with BIND(C) "
3432 "since it is not a global",
3433 tmp_sym
->name
, com_block
->name
,
3434 &(tmp_sym
->declared_at
));
3438 /* Scalar variables that are bind(c) can not have the pointer
3439 or allocatable attributes. */
3440 if (tmp_sym
->attr
.is_bind_c
== 1)
3442 if (tmp_sym
->attr
.pointer
== 1)
3444 gfc_error ("Variable '%s' at %L cannot have both the "
3445 "POINTER and BIND(C) attributes",
3446 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3450 if (tmp_sym
->attr
.allocatable
== 1)
3452 gfc_error ("Variable '%s' at %L cannot have both the "
3453 "ALLOCATABLE and BIND(C) attributes",
3454 tmp_sym
->name
, &(tmp_sym
->declared_at
));
3460 /* If it is a BIND(C) function, make sure the return value is a
3461 scalar value. The previous tests in this function made sure
3462 the type is interoperable. */
3463 if (bind_c_function
&& tmp_sym
->as
!= NULL
)
3464 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3465 "be an array", tmp_sym
->name
, &(tmp_sym
->declared_at
));
3467 /* BIND(C) functions can not return a character string. */
3468 if (bind_c_function
&& tmp_sym
->ts
.type
== BT_CHARACTER
)
3469 if (tmp_sym
->ts
.cl
== NULL
|| tmp_sym
->ts
.cl
->length
== NULL
3470 || tmp_sym
->ts
.cl
->length
->expr_type
!= EXPR_CONSTANT
3471 || mpz_cmp_si (tmp_sym
->ts
.cl
->length
->value
.integer
, 1) != 0)
3472 gfc_error ("Return type of BIND(C) function '%s' at %L cannot "
3473 "be a character string", tmp_sym
->name
,
3474 &(tmp_sym
->declared_at
));
3477 /* See if the symbol has been marked as private. If it has, make sure
3478 there is no binding label and warn the user if there is one. */
3479 if (tmp_sym
->attr
.access
== ACCESS_PRIVATE
3480 && tmp_sym
->binding_label
[0] != '\0')
3481 /* Use gfc_warning_now because we won't say that the symbol fails
3482 just because of this. */
3483 gfc_warning_now ("Symbol '%s' at %L is marked PRIVATE but has been "
3484 "given the binding label '%s'", tmp_sym
->name
,
3485 &(tmp_sym
->declared_at
), tmp_sym
->binding_label
);
3491 /* Set the appropriate fields for a symbol that's been declared as
3492 BIND(C) (the is_bind_c flag and the binding label), and verify that
3493 the type is C interoperable. Errors are reported by the functions
3494 used to set/test these fields. */
3497 set_verify_bind_c_sym (gfc_symbol
*tmp_sym
, int num_idents
)
3499 gfc_try retval
= SUCCESS
;
3501 /* TODO: Do we need to make sure the vars aren't marked private? */
3503 /* Set the is_bind_c bit in symbol_attribute. */
3504 gfc_add_is_bind_c (&(tmp_sym
->attr
), tmp_sym
->name
, &gfc_current_locus
, 0);
3506 if (set_binding_label (tmp_sym
->binding_label
, tmp_sym
->name
,
3507 num_idents
) != SUCCESS
)
3514 /* Set the fields marking the given common block as BIND(C), including
3515 a binding label, and report any errors encountered. */
3518 set_verify_bind_c_com_block (gfc_common_head
*com_block
, int num_idents
)
3520 gfc_try retval
= SUCCESS
;
3522 /* destLabel, common name, typespec (which may have binding label). */
3523 if (set_binding_label (com_block
->binding_label
, com_block
->name
, num_idents
)
3527 /* Set the given common block (com_block) to being bind(c) (1). */
3528 set_com_block_bind_c (com_block
, 1);
3534 /* Retrieve the list of one or more identifiers that the given bind(c)
3535 attribute applies to. */
3538 get_bind_c_idents (void)
3540 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3542 gfc_symbol
*tmp_sym
= NULL
;
3544 gfc_common_head
*com_block
= NULL
;
3546 if (gfc_match_name (name
) == MATCH_YES
)
3548 found_id
= MATCH_YES
;
3549 gfc_get_ha_symbol (name
, &tmp_sym
);
3551 else if (match_common_name (name
) == MATCH_YES
)
3553 found_id
= MATCH_YES
;
3554 com_block
= gfc_get_common (name
, 0);
3558 gfc_error ("Need either entity or common block name for "
3559 "attribute specification statement at %C");
3563 /* Save the current identifier and look for more. */
3566 /* Increment the number of identifiers found for this spec stmt. */
3569 /* Make sure we have a sym or com block, and verify that it can
3570 be bind(c). Set the appropriate field(s) and look for more
3572 if (tmp_sym
!= NULL
|| com_block
!= NULL
)
3574 if (tmp_sym
!= NULL
)
3576 if (set_verify_bind_c_sym (tmp_sym
, num_idents
)
3582 if (set_verify_bind_c_com_block(com_block
, num_idents
)
3587 /* Look to see if we have another identifier. */
3589 if (gfc_match_eos () == MATCH_YES
)
3590 found_id
= MATCH_NO
;
3591 else if (gfc_match_char (',') != MATCH_YES
)
3592 found_id
= MATCH_NO
;
3593 else if (gfc_match_name (name
) == MATCH_YES
)
3595 found_id
= MATCH_YES
;
3596 gfc_get_ha_symbol (name
, &tmp_sym
);
3598 else if (match_common_name (name
) == MATCH_YES
)
3600 found_id
= MATCH_YES
;
3601 com_block
= gfc_get_common (name
, 0);
3605 gfc_error ("Missing entity or common block name for "
3606 "attribute specification statement at %C");
3612 gfc_internal_error ("Missing symbol");
3614 } while (found_id
== MATCH_YES
);
3616 /* if we get here we were successful */
3621 /* Try and match a BIND(C) attribute specification statement. */
3624 gfc_match_bind_c_stmt (void)
3626 match found_match
= MATCH_NO
;
3631 /* This may not be necessary. */
3633 /* Clear the temporary binding label holder. */
3634 curr_binding_label
[0] = '\0';
3636 /* Look for the bind(c). */
3637 found_match
= gfc_match_bind_c (NULL
, true);
3639 if (found_match
== MATCH_YES
)
3641 /* Look for the :: now, but it is not required. */
3644 /* Get the identifier(s) that needs to be updated. This may need to
3645 change to hand the flag(s) for the attr specified so all identifiers
3646 found can have all appropriate parts updated (assuming that the same
3647 spec stmt can have multiple attrs, such as both bind(c) and
3649 if (get_bind_c_idents () != SUCCESS
)
3650 /* Error message should have printed already. */
3658 /* Match a data declaration statement. */
3661 gfc_match_data_decl (void)
3667 num_idents_on_line
= 0;
3669 m
= gfc_match_type_spec (¤t_ts
, 0);
3673 if (current_ts
.type
== BT_DERIVED
&& gfc_current_state () != COMP_DERIVED
)
3675 sym
= gfc_use_derived (current_ts
.derived
);
3683 current_ts
.derived
= sym
;
3686 m
= match_attr_spec ();
3687 if (m
== MATCH_ERROR
)
3693 if (current_ts
.type
== BT_DERIVED
&& current_ts
.derived
->components
== NULL
3694 && !current_ts
.derived
->attr
.zero_comp
)
3697 if (current_attr
.pointer
&& gfc_current_state () == COMP_DERIVED
)
3700 gfc_find_symbol (current_ts
.derived
->name
,
3701 current_ts
.derived
->ns
->parent
, 1, &sym
);
3703 /* Any symbol that we find had better be a type definition
3704 which has its components defined. */
3705 if (sym
!= NULL
&& sym
->attr
.flavor
== FL_DERIVED
3706 && (current_ts
.derived
->components
!= NULL
3707 || current_ts
.derived
->attr
.zero_comp
))
3710 /* Now we have an error, which we signal, and then fix up
3711 because the knock-on is plain and simple confusing. */
3712 gfc_error_now ("Derived type at %C has not been previously defined "
3713 "and so cannot appear in a derived type definition");
3714 current_attr
.pointer
= 1;
3719 /* If we have an old-style character declaration, and no new-style
3720 attribute specifications, then there a comma is optional between
3721 the type specification and the variable list. */
3722 if (m
== MATCH_NO
&& current_ts
.type
== BT_CHARACTER
&& old_char_selector
)
3723 gfc_match_char (',');
3725 /* Give the types/attributes to symbols that follow. Give the element
3726 a number so that repeat character length expressions can be copied. */
3730 num_idents_on_line
++;
3731 m
= variable_decl (elem
++);
3732 if (m
== MATCH_ERROR
)
3737 if (gfc_match_eos () == MATCH_YES
)
3739 if (gfc_match_char (',') != MATCH_YES
)
3743 if (gfc_error_flag_test () == 0)
3744 gfc_error ("Syntax error in data declaration at %C");
3747 gfc_free_data_all (gfc_current_ns
);
3750 gfc_free_array_spec (current_as
);
3756 /* Match a prefix associated with a function or subroutine
3757 declaration. If the typespec pointer is nonnull, then a typespec
3758 can be matched. Note that if nothing matches, MATCH_YES is
3759 returned (the null string was matched). */
3762 gfc_match_prefix (gfc_typespec
*ts
)
3766 gfc_clear_attr (¤t_attr
);
3769 gcc_assert (!gfc_matching_prefix
);
3770 gfc_matching_prefix
= true;
3773 if (!seen_type
&& ts
!= NULL
3774 && gfc_match_type_spec (ts
, 0) == MATCH_YES
3775 && gfc_match_space () == MATCH_YES
)
3782 if (gfc_match ("elemental% ") == MATCH_YES
)
3784 if (gfc_add_elemental (¤t_attr
, NULL
) == FAILURE
)
3790 if (gfc_match ("pure% ") == MATCH_YES
)
3792 if (gfc_add_pure (¤t_attr
, NULL
) == FAILURE
)
3798 if (gfc_match ("recursive% ") == MATCH_YES
)
3800 if (gfc_add_recursive (¤t_attr
, NULL
) == FAILURE
)
3806 /* At this point, the next item is not a prefix. */
3807 gcc_assert (gfc_matching_prefix
);
3808 gfc_matching_prefix
= false;
3812 gcc_assert (gfc_matching_prefix
);
3813 gfc_matching_prefix
= false;
3818 /* Copy attributes matched by gfc_match_prefix() to attributes on a symbol. */
3821 copy_prefix (symbol_attribute
*dest
, locus
*where
)
3823 if (current_attr
.pure
&& gfc_add_pure (dest
, where
) == FAILURE
)
3826 if (current_attr
.elemental
&& gfc_add_elemental (dest
, where
) == FAILURE
)
3829 if (current_attr
.recursive
&& gfc_add_recursive (dest
, where
) == FAILURE
)
3836 /* Match a formal argument list. */
3839 gfc_match_formal_arglist (gfc_symbol
*progname
, int st_flag
, int null_flag
)
3841 gfc_formal_arglist
*head
, *tail
, *p
, *q
;
3842 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3848 if (gfc_match_char ('(') != MATCH_YES
)
3855 if (gfc_match_char (')') == MATCH_YES
)
3860 if (gfc_match_char ('*') == MATCH_YES
)
3864 m
= gfc_match_name (name
);
3868 if (gfc_get_symbol (name
, NULL
, &sym
))
3872 p
= gfc_get_formal_arglist ();
3884 /* We don't add the VARIABLE flavor because the name could be a
3885 dummy procedure. We don't apply these attributes to formal
3886 arguments of statement functions. */
3887 if (sym
!= NULL
&& !st_flag
3888 && (gfc_add_dummy (&sym
->attr
, sym
->name
, NULL
) == FAILURE
3889 || gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
))
3895 /* The name of a program unit can be in a different namespace,
3896 so check for it explicitly. After the statement is accepted,
3897 the name is checked for especially in gfc_get_symbol(). */
3898 if (gfc_new_block
!= NULL
&& sym
!= NULL
3899 && strcmp (sym
->name
, gfc_new_block
->name
) == 0)
3901 gfc_error ("Name '%s' at %C is the name of the procedure",
3907 if (gfc_match_char (')') == MATCH_YES
)
3910 m
= gfc_match_char (',');
3913 gfc_error ("Unexpected junk in formal argument list at %C");
3919 /* Check for duplicate symbols in the formal argument list. */
3922 for (p
= head
; p
->next
; p
= p
->next
)
3927 for (q
= p
->next
; q
; q
= q
->next
)
3928 if (p
->sym
== q
->sym
)
3930 gfc_error ("Duplicate symbol '%s' in formal argument list "
3931 "at %C", p
->sym
->name
);
3939 if (gfc_add_explicit_interface (progname
, IFSRC_DECL
, head
, NULL
)
3949 gfc_free_formal_arglist (head
);
3954 /* Match a RESULT specification following a function declaration or
3955 ENTRY statement. Also matches the end-of-statement. */
3958 match_result (gfc_symbol
*function
, gfc_symbol
**result
)
3960 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
3964 if (gfc_match (" result (") != MATCH_YES
)
3967 m
= gfc_match_name (name
);
3971 /* Get the right paren, and that's it because there could be the
3972 bind(c) attribute after the result clause. */
3973 if (gfc_match_char(')') != MATCH_YES
)
3975 /* TODO: should report the missing right paren here. */
3979 if (strcmp (function
->name
, name
) == 0)
3981 gfc_error ("RESULT variable at %C must be different than function name");
3985 if (gfc_get_symbol (name
, NULL
, &r
))
3988 if (gfc_add_result (&r
->attr
, r
->name
, NULL
) == FAILURE
)
3997 /* Match a function suffix, which could be a combination of a result
3998 clause and BIND(C), either one, or neither. The draft does not
3999 require them to come in a specific order. */
4002 gfc_match_suffix (gfc_symbol
*sym
, gfc_symbol
**result
)
4004 match is_bind_c
; /* Found bind(c). */
4005 match is_result
; /* Found result clause. */
4006 match found_match
; /* Status of whether we've found a good match. */
4007 char peek_char
; /* Character we're going to peek at. */
4008 bool allow_binding_name
;
4010 /* Initialize to having found nothing. */
4011 found_match
= MATCH_NO
;
4012 is_bind_c
= MATCH_NO
;
4013 is_result
= MATCH_NO
;
4015 /* Get the next char to narrow between result and bind(c). */
4016 gfc_gobble_whitespace ();
4017 peek_char
= gfc_peek_ascii_char ();
4019 /* C binding names are not allowed for internal procedures. */
4020 if (gfc_current_state () == COMP_CONTAINS
4021 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4022 allow_binding_name
= false;
4024 allow_binding_name
= true;
4029 /* Look for result clause. */
4030 is_result
= match_result (sym
, result
);
4031 if (is_result
== MATCH_YES
)
4033 /* Now see if there is a bind(c) after it. */
4034 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4035 /* We've found the result clause and possibly bind(c). */
4036 found_match
= MATCH_YES
;
4039 /* This should only be MATCH_ERROR. */
4040 found_match
= is_result
;
4043 /* Look for bind(c) first. */
4044 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4045 if (is_bind_c
== MATCH_YES
)
4047 /* Now see if a result clause followed it. */
4048 is_result
= match_result (sym
, result
);
4049 found_match
= MATCH_YES
;
4053 /* Should only be a MATCH_ERROR if we get here after seeing 'b'. */
4054 found_match
= MATCH_ERROR
;
4058 gfc_error ("Unexpected junk after function declaration at %C");
4059 found_match
= MATCH_ERROR
;
4063 if (is_bind_c
== MATCH_YES
)
4065 /* Fortran 2008 draft allows BIND(C) for internal procedures. */
4066 if (gfc_current_state () == COMP_CONTAINS
4067 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
4068 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: BIND(C) attribute "
4069 "at %L may not be specified for an internal "
4070 "procedure", &gfc_current_locus
)
4074 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &gfc_current_locus
, 1)
4083 /* Procedure pointer return value without RESULT statement:
4084 Add "hidden" result variable named "ppr@". */
4087 add_hidden_procptr_result (gfc_symbol
*sym
)
4091 if (gfc_notification_std (GFC_STD_F2003
) == ERROR
)
4094 /* First usage case: PROCEDURE and EXTERNAL statements. */
4095 case1
= gfc_current_state () == COMP_FUNCTION
&& gfc_current_block ()
4096 && strcmp (gfc_current_block ()->name
, sym
->name
) == 0
4097 && sym
->attr
.external
;
4098 /* Second usage case: INTERFACE statements. */
4099 case2
= gfc_current_state () == COMP_INTERFACE
&& gfc_state_stack
->previous
4100 && gfc_state_stack
->previous
->state
== COMP_FUNCTION
4101 && strcmp (gfc_state_stack
->previous
->sym
->name
, sym
->name
) == 0;
4107 gfc_get_sym_tree ("ppr@", gfc_current_ns
, &stree
);
4111 gfc_get_sym_tree ("ppr@", gfc_current_ns
->parent
, &stree
);
4112 st2
= gfc_new_symtree (&gfc_current_ns
->sym_root
, "ppr@");
4113 st2
->n
.sym
= stree
->n
.sym
;
4115 sym
->result
= stree
->n
.sym
;
4117 sym
->result
->attr
.proc_pointer
= sym
->attr
.proc_pointer
;
4118 sym
->result
->attr
.pointer
= sym
->attr
.pointer
;
4119 sym
->result
->attr
.external
= sym
->attr
.external
;
4120 sym
->result
->attr
.referenced
= sym
->attr
.referenced
;
4121 sym
->attr
.proc_pointer
= 0;
4122 sym
->attr
.pointer
= 0;
4123 sym
->attr
.external
= 0;
4124 if (sym
->result
->attr
.external
&& sym
->result
->attr
.pointer
)
4126 sym
->result
->attr
.pointer
= 0;
4127 sym
->result
->attr
.proc_pointer
= 1;
4130 return gfc_add_result (&sym
->result
->attr
, sym
->result
->name
, NULL
);
4132 /* POINTER after PROCEDURE/EXTERNAL/INTERFACE statement. */
4133 else if (sym
->attr
.function
&& !sym
->attr
.external
&& sym
->attr
.pointer
4134 && sym
->result
&& sym
->result
!= sym
&& sym
->result
->attr
.external
4135 && sym
== gfc_current_ns
->proc_name
4136 && sym
== sym
->result
->ns
->proc_name
4137 && strcmp ("ppr@", sym
->result
->name
) == 0)
4139 sym
->result
->attr
.proc_pointer
= 1;
4140 sym
->attr
.pointer
= 0;
4148 /* Match a PROCEDURE declaration (R1211). */
4151 match_procedure_decl (void)
4154 locus old_loc
, entry_loc
;
4155 gfc_symbol
*sym
, *proc_if
= NULL
;
4157 gfc_expr
*initializer
= NULL
;
4159 old_loc
= entry_loc
= gfc_current_locus
;
4161 gfc_clear_ts (¤t_ts
);
4163 if (gfc_match (" (") != MATCH_YES
)
4165 gfc_current_locus
= entry_loc
;
4169 /* Get the type spec. for the procedure interface. */
4170 old_loc
= gfc_current_locus
;
4171 m
= gfc_match_type_spec (¤t_ts
, 0);
4172 gfc_gobble_whitespace ();
4173 if (m
== MATCH_YES
|| (m
== MATCH_NO
&& gfc_peek_ascii_char () == ')'))
4176 if (m
== MATCH_ERROR
)
4179 gfc_current_locus
= old_loc
;
4181 /* Get the name of the procedure or abstract interface
4182 to inherit the interface from. */
4183 m
= gfc_match_symbol (&proc_if
, 1);
4187 else if (m
== MATCH_ERROR
)
4190 /* Various interface checks. */
4194 /* Resolve interface if possible. That way, attr.procedure is only set
4195 if it is declared by a later procedure-declaration-stmt, which is
4196 invalid per C1212. */
4197 while (proc_if
->ts
.interface
)
4198 proc_if
= proc_if
->ts
.interface
;
4200 if (proc_if
->generic
)
4202 gfc_error ("Interface '%s' at %C may not be generic", proc_if
->name
);
4205 if (proc_if
->attr
.proc
== PROC_ST_FUNCTION
)
4207 gfc_error ("Interface '%s' at %C may not be a statement function",
4211 /* Handle intrinsic procedures. */
4212 if (!(proc_if
->attr
.external
|| proc_if
->attr
.use_assoc
4213 || proc_if
->attr
.if_source
== IFSRC_IFBODY
)
4214 && (gfc_is_intrinsic (proc_if
, 0, gfc_current_locus
)
4215 || gfc_is_intrinsic (proc_if
, 1, gfc_current_locus
)))
4216 proc_if
->attr
.intrinsic
= 1;
4217 if (proc_if
->attr
.intrinsic
4218 && !gfc_intrinsic_actual_ok (proc_if
->name
, 0))
4220 gfc_error ("Intrinsic procedure '%s' not allowed "
4221 "in PROCEDURE statement at %C", proc_if
->name
);
4227 if (gfc_match (" )") != MATCH_YES
)
4229 gfc_current_locus
= entry_loc
;
4233 /* Parse attributes. */
4234 m
= match_attr_spec();
4235 if (m
== MATCH_ERROR
)
4238 /* Get procedure symbols. */
4241 m
= gfc_match_symbol (&sym
, 0);
4244 else if (m
== MATCH_ERROR
)
4247 /* Add current_attr to the symbol attributes. */
4248 if (gfc_copy_attr (&sym
->attr
, ¤t_attr
, NULL
) == FAILURE
)
4251 if (sym
->attr
.is_bind_c
)
4253 /* Check for C1218. */
4254 if (!proc_if
|| !proc_if
->attr
.is_bind_c
)
4256 gfc_error ("BIND(C) attribute at %C requires "
4257 "an interface with BIND(C)");
4260 /* Check for C1217. */
4261 if (has_name_equals
&& sym
->attr
.pointer
)
4263 gfc_error ("BIND(C) procedure with NAME may not have "
4264 "POINTER attribute at %C");
4267 if (has_name_equals
&& sym
->attr
.dummy
)
4269 gfc_error ("Dummy procedure at %C may not have "
4270 "BIND(C) attribute with NAME");
4273 /* Set binding label for BIND(C). */
4274 if (set_binding_label (sym
->binding_label
, sym
->name
, num
) != SUCCESS
)
4278 if (gfc_add_external (&sym
->attr
, NULL
) == FAILURE
)
4281 if (add_hidden_procptr_result (sym
) == SUCCESS
)
4284 if (gfc_add_proc (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4287 /* Set interface. */
4288 if (proc_if
!= NULL
)
4290 if (sym
->ts
.type
!= BT_UNKNOWN
)
4292 gfc_error ("Procedure '%s' at %L already has basic type of %s",
4293 sym
->name
, &gfc_current_locus
,
4294 gfc_basic_typename (sym
->ts
.type
));
4297 sym
->ts
.interface
= proc_if
;
4298 sym
->attr
.untyped
= 1;
4299 sym
->attr
.if_source
= IFSRC_IFBODY
;
4301 else if (current_ts
.type
!= BT_UNKNOWN
)
4303 if (gfc_add_type (sym
, ¤t_ts
, &gfc_current_locus
) == FAILURE
)
4305 sym
->ts
.interface
= gfc_new_symbol ("", gfc_current_ns
);
4306 sym
->ts
.interface
->ts
= current_ts
;
4307 sym
->ts
.interface
->attr
.function
= 1;
4308 sym
->attr
.function
= sym
->ts
.interface
->attr
.function
;
4309 sym
->attr
.if_source
= IFSRC_UNKNOWN
;
4312 if (gfc_match (" =>") == MATCH_YES
)
4314 if (!current_attr
.pointer
)
4316 gfc_error ("Initialization at %C isn't for a pointer variable");
4321 m
= gfc_match_null (&initializer
);
4324 gfc_error ("Pointer initialization requires a NULL() at %C");
4328 if (gfc_pure (NULL
))
4330 gfc_error ("Initialization of pointer at %C is not allowed in "
4331 "a PURE procedure");
4338 if (add_init_expr_to_sym (sym
->name
, &initializer
, &gfc_current_locus
)
4344 gfc_set_sym_referenced (sym
);
4346 if (gfc_match_eos () == MATCH_YES
)
4348 if (gfc_match_char (',') != MATCH_YES
)
4353 gfc_error ("Syntax error in PROCEDURE statement at %C");
4357 /* Free stuff up and return. */
4358 gfc_free_expr (initializer
);
4363 /* Match a PROCEDURE declaration inside an interface (R1206). */
4366 match_procedure_in_interface (void)
4370 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4372 if (current_interface
.type
== INTERFACE_NAMELESS
4373 || current_interface
.type
== INTERFACE_ABSTRACT
)
4375 gfc_error ("PROCEDURE at %C must be in a generic interface");
4381 m
= gfc_match_name (name
);
4384 else if (m
== MATCH_ERROR
)
4386 if (gfc_get_symbol (name
, gfc_current_ns
->parent
, &sym
))
4389 if (gfc_add_interface (sym
) == FAILURE
)
4392 if (gfc_match_eos () == MATCH_YES
)
4394 if (gfc_match_char (',') != MATCH_YES
)
4401 gfc_error ("Syntax error in PROCEDURE statement at %C");
4406 /* General matcher for PROCEDURE declarations. */
4408 static match
match_procedure_in_type (void);
4411 gfc_match_procedure (void)
4415 switch (gfc_current_state ())
4420 case COMP_SUBROUTINE
:
4422 m
= match_procedure_decl ();
4424 case COMP_INTERFACE
:
4425 m
= match_procedure_in_interface ();
4428 gfc_error ("Fortran 2003: Procedure components at %C are not yet"
4429 " implemented in gfortran");
4431 case COMP_DERIVED_CONTAINS
:
4432 m
= match_procedure_in_type ();
4441 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROCEDURE statement at %C")
4449 /* Warn if a matched procedure has the same name as an intrinsic; this is
4450 simply a wrapper around gfc_warn_intrinsic_shadow that interprets the current
4451 parser-state-stack to find out whether we're in a module. */
4454 warn_intrinsic_shadow (const gfc_symbol
* sym
, bool func
)
4458 in_module
= (gfc_state_stack
->previous
4459 && gfc_state_stack
->previous
->state
== COMP_MODULE
);
4461 gfc_warn_intrinsic_shadow (sym
, in_module
, func
);
4465 /* Match a function declaration. */
4468 gfc_match_function_decl (void)
4470 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4471 gfc_symbol
*sym
, *result
;
4475 match found_match
; /* Status returned by match func. */
4477 if (gfc_current_state () != COMP_NONE
4478 && gfc_current_state () != COMP_INTERFACE
4479 && gfc_current_state () != COMP_CONTAINS
)
4482 gfc_clear_ts (¤t_ts
);
4484 old_loc
= gfc_current_locus
;
4486 m
= gfc_match_prefix (¤t_ts
);
4489 gfc_current_locus
= old_loc
;
4493 if (gfc_match ("function% %n", name
) != MATCH_YES
)
4495 gfc_current_locus
= old_loc
;
4498 if (get_proc_name (name
, &sym
, false))
4501 if (add_hidden_procptr_result (sym
) == SUCCESS
)
4504 gfc_new_block
= sym
;
4506 m
= gfc_match_formal_arglist (sym
, 0, 0);
4509 gfc_error ("Expected formal argument list in function "
4510 "definition at %C");
4514 else if (m
== MATCH_ERROR
)
4519 /* According to the draft, the bind(c) and result clause can
4520 come in either order after the formal_arg_list (i.e., either
4521 can be first, both can exist together or by themselves or neither
4522 one). Therefore, the match_result can't match the end of the
4523 string, and check for the bind(c) or result clause in either order. */
4524 found_match
= gfc_match_eos ();
4526 /* Make sure that it isn't already declared as BIND(C). If it is, it
4527 must have been marked BIND(C) with a BIND(C) attribute and that is
4528 not allowed for procedures. */
4529 if (sym
->attr
.is_bind_c
== 1)
4531 sym
->attr
.is_bind_c
= 0;
4532 if (sym
->old_symbol
!= NULL
)
4533 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4534 "variables or common blocks",
4535 &(sym
->old_symbol
->declared_at
));
4537 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4538 "variables or common blocks", &gfc_current_locus
);
4541 if (found_match
!= MATCH_YES
)
4543 /* If we haven't found the end-of-statement, look for a suffix. */
4544 suffix_match
= gfc_match_suffix (sym
, &result
);
4545 if (suffix_match
== MATCH_YES
)
4546 /* Need to get the eos now. */
4547 found_match
= gfc_match_eos ();
4549 found_match
= suffix_match
;
4552 if(found_match
!= MATCH_YES
)
4556 /* Make changes to the symbol. */
4559 if (gfc_add_function (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4562 if (gfc_missing_attr (&sym
->attr
, NULL
) == FAILURE
4563 || copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
4566 if (current_ts
.type
!= BT_UNKNOWN
&& sym
->ts
.type
!= BT_UNKNOWN
4567 && !sym
->attr
.implicit_type
)
4569 gfc_error ("Function '%s' at %C already has a type of %s", name
,
4570 gfc_basic_typename (sym
->ts
.type
));
4574 /* Delay matching the function characteristics until after the
4575 specification block by signalling kind=-1. */
4576 sym
->declared_at
= old_loc
;
4577 if (current_ts
.type
!= BT_UNKNOWN
)
4578 current_ts
.kind
= -1;
4580 current_ts
.kind
= 0;
4584 sym
->ts
= current_ts
;
4589 result
->ts
= current_ts
;
4590 sym
->result
= result
;
4593 /* Warn if this procedure has the same name as an intrinsic. */
4594 warn_intrinsic_shadow (sym
, true);
4600 gfc_current_locus
= old_loc
;
4605 /* This is mostly a copy of parse.c(add_global_procedure) but modified to
4606 pass the name of the entry, rather than the gfc_current_block name, and
4607 to return false upon finding an existing global entry. */
4610 add_global_entry (const char *name
, int sub
)
4615 s
= gfc_get_gsymbol(name
);
4616 type
= sub
? GSYM_SUBROUTINE
: GSYM_FUNCTION
;
4619 || (s
->type
!= GSYM_UNKNOWN
4620 && s
->type
!= type
))
4621 gfc_global_used(s
, NULL
);
4625 s
->where
= gfc_current_locus
;
4627 s
->ns
= gfc_current_ns
;
4634 /* Match an ENTRY statement. */
4637 gfc_match_entry (void)
4642 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4643 gfc_compile_state state
;
4647 bool module_procedure
;
4651 m
= gfc_match_name (name
);
4655 state
= gfc_current_state ();
4656 if (state
!= COMP_SUBROUTINE
&& state
!= COMP_FUNCTION
)
4661 gfc_error ("ENTRY statement at %C cannot appear within a PROGRAM");
4664 gfc_error ("ENTRY statement at %C cannot appear within a MODULE");
4666 case COMP_BLOCK_DATA
:
4667 gfc_error ("ENTRY statement at %C cannot appear within "
4670 case COMP_INTERFACE
:
4671 gfc_error ("ENTRY statement at %C cannot appear within "
4675 gfc_error ("ENTRY statement at %C cannot appear within "
4676 "a DERIVED TYPE block");
4679 gfc_error ("ENTRY statement at %C cannot appear within "
4680 "an IF-THEN block");
4683 gfc_error ("ENTRY statement at %C cannot appear within "
4687 gfc_error ("ENTRY statement at %C cannot appear within "
4691 gfc_error ("ENTRY statement at %C cannot appear within "
4695 gfc_error ("ENTRY statement at %C cannot appear within "
4699 gfc_error ("ENTRY statement at %C cannot appear within "
4700 "a contained subprogram");
4703 gfc_internal_error ("gfc_match_entry(): Bad state");
4708 module_procedure
= gfc_current_ns
->parent
!= NULL
4709 && gfc_current_ns
->parent
->proc_name
4710 && gfc_current_ns
->parent
->proc_name
->attr
.flavor
4713 if (gfc_current_ns
->parent
!= NULL
4714 && gfc_current_ns
->parent
->proc_name
4715 && !module_procedure
)
4717 gfc_error("ENTRY statement at %C cannot appear in a "
4718 "contained procedure");
4722 /* Module function entries need special care in get_proc_name
4723 because previous references within the function will have
4724 created symbols attached to the current namespace. */
4725 if (get_proc_name (name
, &entry
,
4726 gfc_current_ns
->parent
!= NULL
4727 && module_procedure
))
4730 proc
= gfc_current_block ();
4732 /* Make sure that it isn't already declared as BIND(C). If it is, it
4733 must have been marked BIND(C) with a BIND(C) attribute and that is
4734 not allowed for procedures. */
4735 if (entry
->attr
.is_bind_c
== 1)
4737 entry
->attr
.is_bind_c
= 0;
4738 if (entry
->old_symbol
!= NULL
)
4739 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4740 "variables or common blocks",
4741 &(entry
->old_symbol
->declared_at
));
4743 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4744 "variables or common blocks", &gfc_current_locus
);
4747 /* Check what next non-whitespace character is so we can tell if there
4748 is the required parens if we have a BIND(C). */
4749 gfc_gobble_whitespace ();
4750 peek_char
= gfc_peek_ascii_char ();
4752 if (state
== COMP_SUBROUTINE
)
4754 /* An entry in a subroutine. */
4755 if (!gfc_current_ns
->parent
&& !add_global_entry (name
, 1))
4758 m
= gfc_match_formal_arglist (entry
, 0, 1);
4762 /* Call gfc_match_bind_c with allow_binding_name = true as ENTRY can
4763 never be an internal procedure. */
4764 is_bind_c
= gfc_match_bind_c (entry
, true);
4765 if (is_bind_c
== MATCH_ERROR
)
4767 if (is_bind_c
== MATCH_YES
)
4769 if (peek_char
!= '(')
4771 gfc_error ("Missing required parentheses before BIND(C) at %C");
4774 if (gfc_add_is_bind_c (&(entry
->attr
), entry
->name
, &(entry
->declared_at
), 1)
4779 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
4780 || gfc_add_subroutine (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
4785 /* An entry in a function.
4786 We need to take special care because writing
4791 ENTRY f() RESULT (r)
4793 ENTRY f RESULT (r). */
4794 if (!gfc_current_ns
->parent
&& !add_global_entry (name
, 0))
4797 old_loc
= gfc_current_locus
;
4798 if (gfc_match_eos () == MATCH_YES
)
4800 gfc_current_locus
= old_loc
;
4801 /* Match the empty argument list, and add the interface to
4803 m
= gfc_match_formal_arglist (entry
, 0, 1);
4806 m
= gfc_match_formal_arglist (entry
, 0, 0);
4813 if (gfc_match_eos () == MATCH_YES
)
4815 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
4816 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
4819 entry
->result
= entry
;
4823 m
= gfc_match_suffix (entry
, &result
);
4825 gfc_syntax_error (ST_ENTRY
);
4831 if (gfc_add_result (&result
->attr
, result
->name
, NULL
) == FAILURE
4832 || gfc_add_entry (&entry
->attr
, result
->name
, NULL
) == FAILURE
4833 || gfc_add_function (&entry
->attr
, result
->name
, NULL
)
4836 entry
->result
= result
;
4840 if (gfc_add_entry (&entry
->attr
, entry
->name
, NULL
) == FAILURE
4841 || gfc_add_function (&entry
->attr
, entry
->name
, NULL
) == FAILURE
)
4843 entry
->result
= entry
;
4848 if (gfc_match_eos () != MATCH_YES
)
4850 gfc_syntax_error (ST_ENTRY
);
4854 entry
->attr
.recursive
= proc
->attr
.recursive
;
4855 entry
->attr
.elemental
= proc
->attr
.elemental
;
4856 entry
->attr
.pure
= proc
->attr
.pure
;
4858 el
= gfc_get_entry_list ();
4860 el
->next
= gfc_current_ns
->entries
;
4861 gfc_current_ns
->entries
= el
;
4863 el
->id
= el
->next
->id
+ 1;
4867 new_st
.op
= EXEC_ENTRY
;
4868 new_st
.ext
.entry
= el
;
4874 /* Match a subroutine statement, including optional prefixes. */
4877 gfc_match_subroutine (void)
4879 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
4884 bool allow_binding_name
;
4886 if (gfc_current_state () != COMP_NONE
4887 && gfc_current_state () != COMP_INTERFACE
4888 && gfc_current_state () != COMP_CONTAINS
)
4891 m
= gfc_match_prefix (NULL
);
4895 m
= gfc_match ("subroutine% %n", name
);
4899 if (get_proc_name (name
, &sym
, false))
4902 if (add_hidden_procptr_result (sym
) == SUCCESS
)
4905 gfc_new_block
= sym
;
4907 /* Check what next non-whitespace character is so we can tell if there
4908 is the required parens if we have a BIND(C). */
4909 gfc_gobble_whitespace ();
4910 peek_char
= gfc_peek_ascii_char ();
4912 if (gfc_add_subroutine (&sym
->attr
, sym
->name
, NULL
) == FAILURE
)
4915 if (gfc_match_formal_arglist (sym
, 0, 1) != MATCH_YES
)
4918 /* Make sure that it isn't already declared as BIND(C). If it is, it
4919 must have been marked BIND(C) with a BIND(C) attribute and that is
4920 not allowed for procedures. */
4921 if (sym
->attr
.is_bind_c
== 1)
4923 sym
->attr
.is_bind_c
= 0;
4924 if (sym
->old_symbol
!= NULL
)
4925 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4926 "variables or common blocks",
4927 &(sym
->old_symbol
->declared_at
));
4929 gfc_error_now ("BIND(C) attribute at %L can only be used for "
4930 "variables or common blocks", &gfc_current_locus
);
4933 /* C binding names are not allowed for internal procedures. */
4934 if (gfc_current_state () == COMP_CONTAINS
4935 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
4936 allow_binding_name
= false;
4938 allow_binding_name
= true;
4940 /* Here, we are just checking if it has the bind(c) attribute, and if
4941 so, then we need to make sure it's all correct. If it doesn't,
4942 we still need to continue matching the rest of the subroutine line. */
4943 is_bind_c
= gfc_match_bind_c (sym
, allow_binding_name
);
4944 if (is_bind_c
== MATCH_ERROR
)
4946 /* There was an attempt at the bind(c), but it was wrong. An
4947 error message should have been printed w/in the gfc_match_bind_c
4948 so here we'll just return the MATCH_ERROR. */
4952 if (is_bind_c
== MATCH_YES
)
4954 /* The following is allowed in the Fortran 2008 draft. */
4955 if (gfc_current_state () == COMP_CONTAINS
4956 && sym
->ns
->proc_name
->attr
.flavor
!= FL_MODULE
4957 && gfc_notify_std (GFC_STD_F2008
, "Fortran 2008: BIND(C) attribute "
4958 "at %L may not be specified for an internal "
4959 "procedure", &gfc_current_locus
)
4963 if (peek_char
!= '(')
4965 gfc_error ("Missing required parentheses before BIND(C) at %C");
4968 if (gfc_add_is_bind_c (&(sym
->attr
), sym
->name
, &(sym
->declared_at
), 1)
4973 if (gfc_match_eos () != MATCH_YES
)
4975 gfc_syntax_error (ST_SUBROUTINE
);
4979 if (copy_prefix (&sym
->attr
, &sym
->declared_at
) == FAILURE
)
4982 /* Warn if it has the same name as an intrinsic. */
4983 warn_intrinsic_shadow (sym
, false);
4989 /* Match a BIND(C) specifier, with the optional 'name=' specifier if
4990 given, and set the binding label in either the given symbol (if not
4991 NULL), or in the current_ts. The symbol may be NULL because we may
4992 encounter the BIND(C) before the declaration itself. Return
4993 MATCH_NO if what we're looking at isn't a BIND(C) specifier,
4994 MATCH_ERROR if it is a BIND(C) clause but an error was encountered,
4995 or MATCH_YES if the specifier was correct and the binding label and
4996 bind(c) fields were set correctly for the given symbol or the
4997 current_ts. If allow_binding_name is false, no binding name may be
5001 gfc_match_bind_c (gfc_symbol
*sym
, bool allow_binding_name
)
5003 /* binding label, if exists */
5004 char binding_label
[GFC_MAX_SYMBOL_LEN
+ 1];
5008 /* Initialize the flag that specifies whether we encountered a NAME=
5009 specifier or not. */
5010 has_name_equals
= 0;
5012 /* Init the first char to nil so we can catch if we don't have
5013 the label (name attr) or the symbol name yet. */
5014 binding_label
[0] = '\0';
5016 /* This much we have to be able to match, in this order, if
5017 there is a bind(c) label. */
5018 if (gfc_match (" bind ( c ") != MATCH_YES
)
5021 /* Now see if there is a binding label, or if we've reached the
5022 end of the bind(c) attribute without one. */
5023 if (gfc_match_char (',') == MATCH_YES
)
5025 if (gfc_match (" name = ") != MATCH_YES
)
5027 gfc_error ("Syntax error in NAME= specifier for binding label "
5029 /* should give an error message here */
5033 has_name_equals
= 1;
5035 /* Get the opening quote. */
5036 double_quote
= MATCH_YES
;
5037 single_quote
= MATCH_YES
;
5038 double_quote
= gfc_match_char ('"');
5039 if (double_quote
!= MATCH_YES
)
5040 single_quote
= gfc_match_char ('\'');
5041 if (double_quote
!= MATCH_YES
&& single_quote
!= MATCH_YES
)
5043 gfc_error ("Syntax error in NAME= specifier for binding label "
5048 /* Grab the binding label, using functions that will not lower
5049 case the names automatically. */
5050 if (gfc_match_name_C (binding_label
) != MATCH_YES
)
5053 /* Get the closing quotation. */
5054 if (double_quote
== MATCH_YES
)
5056 if (gfc_match_char ('"') != MATCH_YES
)
5058 gfc_error ("Missing closing quote '\"' for binding label at %C");
5059 /* User started string with '"' so looked to match it. */
5065 if (gfc_match_char ('\'') != MATCH_YES
)
5067 gfc_error ("Missing closing quote '\'' for binding label at %C");
5068 /* User started string with "'" char. */
5074 /* Get the required right paren. */
5075 if (gfc_match_char (')') != MATCH_YES
)
5077 gfc_error ("Missing closing paren for binding label at %C");
5081 if (has_name_equals
&& !allow_binding_name
)
5083 gfc_error ("No binding name is allowed in BIND(C) at %C");
5087 if (has_name_equals
&& sym
!= NULL
&& sym
->attr
.dummy
)
5089 gfc_error ("For dummy procedure %s, no binding name is "
5090 "allowed in BIND(C) at %C", sym
->name
);
5095 /* Save the binding label to the symbol. If sym is null, we're
5096 probably matching the typespec attributes of a declaration and
5097 haven't gotten the name yet, and therefore, no symbol yet. */
5098 if (binding_label
[0] != '\0')
5102 strcpy (sym
->binding_label
, binding_label
);
5105 strcpy (curr_binding_label
, binding_label
);
5107 else if (allow_binding_name
)
5109 /* No binding label, but if symbol isn't null, we
5110 can set the label for it here.
5111 If name="" or allow_binding_name is false, no C binding name is
5113 if (sym
!= NULL
&& sym
->name
!= NULL
&& has_name_equals
== 0)
5114 strncpy (sym
->binding_label
, sym
->name
, strlen (sym
->name
) + 1);
5117 if (has_name_equals
&& gfc_current_state () == COMP_INTERFACE
5118 && current_interface
.type
== INTERFACE_ABSTRACT
)
5120 gfc_error ("NAME not allowed on BIND(C) for ABSTRACT INTERFACE at %C");
5128 /* Return nonzero if we're currently compiling a contained procedure. */
5131 contained_procedure (void)
5133 gfc_state_data
*s
= gfc_state_stack
;
5135 if ((s
->state
== COMP_SUBROUTINE
|| s
->state
== COMP_FUNCTION
)
5136 && s
->previous
!= NULL
&& s
->previous
->state
== COMP_CONTAINS
)
5142 /* Set the kind of each enumerator. The kind is selected such that it is
5143 interoperable with the corresponding C enumeration type, making
5144 sure that -fshort-enums is honored. */
5149 enumerator_history
*current_history
= NULL
;
5153 if (max_enum
== NULL
|| enum_history
== NULL
)
5156 if (!gfc_option
.fshort_enums
)
5162 kind
= gfc_integer_kinds
[i
++].kind
;
5164 while (kind
< gfc_c_int_kind
5165 && gfc_check_integer_range (max_enum
->initializer
->value
.integer
,
5168 current_history
= enum_history
;
5169 while (current_history
!= NULL
)
5171 current_history
->sym
->ts
.kind
= kind
;
5172 current_history
= current_history
->next
;
5177 /* Match any of the various end-block statements. Returns the type of
5178 END to the caller. The END INTERFACE, END IF, END DO and END
5179 SELECT statements cannot be replaced by a single END statement. */
5182 gfc_match_end (gfc_statement
*st
)
5184 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5185 gfc_compile_state state
;
5187 const char *block_name
;
5192 old_loc
= gfc_current_locus
;
5193 if (gfc_match ("end") != MATCH_YES
)
5196 state
= gfc_current_state ();
5197 block_name
= gfc_current_block () == NULL
5198 ? NULL
: gfc_current_block ()->name
;
5200 if (state
== COMP_CONTAINS
|| state
== COMP_DERIVED_CONTAINS
)
5202 state
= gfc_state_stack
->previous
->state
;
5203 block_name
= gfc_state_stack
->previous
->sym
== NULL
5204 ? NULL
: gfc_state_stack
->previous
->sym
->name
;
5211 *st
= ST_END_PROGRAM
;
5212 target
= " program";
5216 case COMP_SUBROUTINE
:
5217 *st
= ST_END_SUBROUTINE
;
5218 target
= " subroutine";
5219 eos_ok
= !contained_procedure ();
5223 *st
= ST_END_FUNCTION
;
5224 target
= " function";
5225 eos_ok
= !contained_procedure ();
5228 case COMP_BLOCK_DATA
:
5229 *st
= ST_END_BLOCK_DATA
;
5230 target
= " block data";
5235 *st
= ST_END_MODULE
;
5240 case COMP_INTERFACE
:
5241 *st
= ST_END_INTERFACE
;
5242 target
= " interface";
5247 case COMP_DERIVED_CONTAINS
:
5266 *st
= ST_END_SELECT
;
5272 *st
= ST_END_FORALL
;
5287 last_initializer
= NULL
;
5289 gfc_free_enum_history ();
5293 gfc_error ("Unexpected END statement at %C");
5297 if (gfc_match_eos () == MATCH_YES
)
5301 /* We would have required END [something]. */
5302 gfc_error ("%s statement expected at %L",
5303 gfc_ascii_statement (*st
), &old_loc
);
5310 /* Verify that we've got the sort of end-block that we're expecting. */
5311 if (gfc_match (target
) != MATCH_YES
)
5313 gfc_error ("Expecting %s statement at %C", gfc_ascii_statement (*st
));
5317 /* If we're at the end, make sure a block name wasn't required. */
5318 if (gfc_match_eos () == MATCH_YES
)
5321 if (*st
!= ST_ENDDO
&& *st
!= ST_ENDIF
&& *st
!= ST_END_SELECT
5322 && *st
!= ST_END_FORALL
&& *st
!= ST_END_WHERE
)
5325 if (gfc_current_block () == NULL
)
5328 gfc_error ("Expected block name of '%s' in %s statement at %C",
5329 block_name
, gfc_ascii_statement (*st
));
5334 /* END INTERFACE has a special handler for its several possible endings. */
5335 if (*st
== ST_END_INTERFACE
)
5336 return gfc_match_end_interface ();
5338 /* We haven't hit the end of statement, so what is left must be an
5340 m
= gfc_match_space ();
5342 m
= gfc_match_name (name
);
5345 gfc_error ("Expected terminating name at %C");
5349 if (block_name
== NULL
)
5352 if (strcmp (name
, block_name
) != 0 && strcmp (block_name
, "ppr@") != 0)
5354 gfc_error ("Expected label '%s' for %s statement at %C", block_name
,
5355 gfc_ascii_statement (*st
));
5358 /* Procedure pointer as function result. */
5359 else if (strcmp (block_name
, "ppr@") == 0
5360 && strcmp (name
, gfc_current_block ()->ns
->proc_name
->name
) != 0)
5362 gfc_error ("Expected label '%s' for %s statement at %C",
5363 gfc_current_block ()->ns
->proc_name
->name
,
5364 gfc_ascii_statement (*st
));
5368 if (gfc_match_eos () == MATCH_YES
)
5372 gfc_syntax_error (*st
);
5375 gfc_current_locus
= old_loc
;
5381 /***************** Attribute declaration statements ****************/
5383 /* Set the attribute of a single variable. */
5388 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5396 m
= gfc_match_name (name
);
5400 if (find_special (name
, &sym
))
5403 var_locus
= gfc_current_locus
;
5405 /* Deal with possible array specification for certain attributes. */
5406 if (current_attr
.dimension
5407 || current_attr
.allocatable
5408 || current_attr
.pointer
5409 || current_attr
.target
)
5411 m
= gfc_match_array_spec (&as
);
5412 if (m
== MATCH_ERROR
)
5415 if (current_attr
.dimension
&& m
== MATCH_NO
)
5417 gfc_error ("Missing array specification at %L in DIMENSION "
5418 "statement", &var_locus
);
5423 if (current_attr
.dimension
&& sym
->value
)
5425 gfc_error ("Dimensions specified for %s at %L after its "
5426 "initialisation", sym
->name
, &var_locus
);
5431 if ((current_attr
.allocatable
|| current_attr
.pointer
)
5432 && (m
== MATCH_YES
) && (as
->type
!= AS_DEFERRED
))
5434 gfc_error ("Array specification must be deferred at %L", &var_locus
);
5440 /* Update symbol table. DIMENSION attribute is set
5441 in gfc_set_array_spec(). */
5442 if (current_attr
.dimension
== 0
5443 && gfc_copy_attr (&sym
->attr
, ¤t_attr
, &var_locus
) == FAILURE
)
5449 if (gfc_set_array_spec (sym
, as
, &var_locus
) == FAILURE
)
5455 if (sym
->attr
.cray_pointee
&& sym
->as
!= NULL
)
5457 /* Fix the array spec. */
5458 m
= gfc_mod_pointee_as (sym
->as
);
5459 if (m
== MATCH_ERROR
)
5463 if (gfc_add_attribute (&sym
->attr
, &var_locus
) == FAILURE
)
5469 if ((current_attr
.external
|| current_attr
.intrinsic
)
5470 && sym
->attr
.flavor
!= FL_PROCEDURE
5471 && gfc_add_flavor (&sym
->attr
, FL_PROCEDURE
, sym
->name
, NULL
) == FAILURE
)
5477 add_hidden_procptr_result (sym
);
5482 gfc_free_array_spec (as
);
5487 /* Generic attribute declaration subroutine. Used for attributes that
5488 just have a list of names. */
5495 /* Gobble the optional double colon, by simply ignoring the result
5505 if (gfc_match_eos () == MATCH_YES
)
5511 if (gfc_match_char (',') != MATCH_YES
)
5513 gfc_error ("Unexpected character in variable list at %C");
5523 /* This routine matches Cray Pointer declarations of the form:
5524 pointer ( <pointer>, <pointee> )
5526 pointer ( <pointer1>, <pointee1> ), ( <pointer2>, <pointee2> ), ...
5527 The pointer, if already declared, should be an integer. Otherwise, we
5528 set it as BT_INTEGER with kind gfc_index_integer_kind. The pointee may
5529 be either a scalar, or an array declaration. No space is allocated for
5530 the pointee. For the statement
5531 pointer (ipt, ar(10))
5532 any subsequent uses of ar will be translated (in C-notation) as
5533 ar(i) => ((<type> *) ipt)(i)
5534 After gimplification, pointee variable will disappear in the code. */
5537 cray_pointer_decl (void)
5541 gfc_symbol
*cptr
; /* Pointer symbol. */
5542 gfc_symbol
*cpte
; /* Pointee symbol. */
5548 if (gfc_match_char ('(') != MATCH_YES
)
5550 gfc_error ("Expected '(' at %C");
5554 /* Match pointer. */
5555 var_locus
= gfc_current_locus
;
5556 gfc_clear_attr (¤t_attr
);
5557 gfc_add_cray_pointer (¤t_attr
, &var_locus
);
5558 current_ts
.type
= BT_INTEGER
;
5559 current_ts
.kind
= gfc_index_integer_kind
;
5561 m
= gfc_match_symbol (&cptr
, 0);
5564 gfc_error ("Expected variable name at %C");
5568 if (gfc_add_cray_pointer (&cptr
->attr
, &var_locus
) == FAILURE
)
5571 gfc_set_sym_referenced (cptr
);
5573 if (cptr
->ts
.type
== BT_UNKNOWN
) /* Override the type, if necessary. */
5575 cptr
->ts
.type
= BT_INTEGER
;
5576 cptr
->ts
.kind
= gfc_index_integer_kind
;
5578 else if (cptr
->ts
.type
!= BT_INTEGER
)
5580 gfc_error ("Cray pointer at %C must be an integer");
5583 else if (cptr
->ts
.kind
< gfc_index_integer_kind
)
5584 gfc_warning ("Cray pointer at %C has %d bytes of precision;"
5585 " memory addresses require %d bytes",
5586 cptr
->ts
.kind
, gfc_index_integer_kind
);
5588 if (gfc_match_char (',') != MATCH_YES
)
5590 gfc_error ("Expected \",\" at %C");
5594 /* Match Pointee. */
5595 var_locus
= gfc_current_locus
;
5596 gfc_clear_attr (¤t_attr
);
5597 gfc_add_cray_pointee (¤t_attr
, &var_locus
);
5598 current_ts
.type
= BT_UNKNOWN
;
5599 current_ts
.kind
= 0;
5601 m
= gfc_match_symbol (&cpte
, 0);
5604 gfc_error ("Expected variable name at %C");
5608 /* Check for an optional array spec. */
5609 m
= gfc_match_array_spec (&as
);
5610 if (m
== MATCH_ERROR
)
5612 gfc_free_array_spec (as
);
5615 else if (m
== MATCH_NO
)
5617 gfc_free_array_spec (as
);
5621 if (gfc_add_cray_pointee (&cpte
->attr
, &var_locus
) == FAILURE
)
5624 gfc_set_sym_referenced (cpte
);
5626 if (cpte
->as
== NULL
)
5628 if (gfc_set_array_spec (cpte
, as
, &var_locus
) == FAILURE
)
5629 gfc_internal_error ("Couldn't set Cray pointee array spec.");
5631 else if (as
!= NULL
)
5633 gfc_error ("Duplicate array spec for Cray pointee at %C");
5634 gfc_free_array_spec (as
);
5640 if (cpte
->as
!= NULL
)
5642 /* Fix array spec. */
5643 m
= gfc_mod_pointee_as (cpte
->as
);
5644 if (m
== MATCH_ERROR
)
5648 /* Point the Pointee at the Pointer. */
5649 cpte
->cp_pointer
= cptr
;
5651 if (gfc_match_char (')') != MATCH_YES
)
5653 gfc_error ("Expected \")\" at %C");
5656 m
= gfc_match_char (',');
5658 done
= true; /* Stop searching for more declarations. */
5662 if (m
== MATCH_ERROR
/* Failed when trying to find ',' above. */
5663 || gfc_match_eos () != MATCH_YES
)
5665 gfc_error ("Expected \",\" or end of statement at %C");
5673 gfc_match_external (void)
5676 gfc_clear_attr (¤t_attr
);
5677 current_attr
.external
= 1;
5679 return attr_decl ();
5684 gfc_match_intent (void)
5688 intent
= match_intent_spec ();
5689 if (intent
== INTENT_UNKNOWN
)
5692 gfc_clear_attr (¤t_attr
);
5693 current_attr
.intent
= intent
;
5695 return attr_decl ();
5700 gfc_match_intrinsic (void)
5703 gfc_clear_attr (¤t_attr
);
5704 current_attr
.intrinsic
= 1;
5706 return attr_decl ();
5711 gfc_match_optional (void)
5714 gfc_clear_attr (¤t_attr
);
5715 current_attr
.optional
= 1;
5717 return attr_decl ();
5722 gfc_match_pointer (void)
5724 gfc_gobble_whitespace ();
5725 if (gfc_peek_ascii_char () == '(')
5727 if (!gfc_option
.flag_cray_pointer
)
5729 gfc_error ("Cray pointer declaration at %C requires -fcray-pointer "
5733 return cray_pointer_decl ();
5737 gfc_clear_attr (¤t_attr
);
5738 current_attr
.pointer
= 1;
5740 return attr_decl ();
5746 gfc_match_allocatable (void)
5748 gfc_clear_attr (¤t_attr
);
5749 current_attr
.allocatable
= 1;
5751 return attr_decl ();
5756 gfc_match_dimension (void)
5758 gfc_clear_attr (¤t_attr
);
5759 current_attr
.dimension
= 1;
5761 return attr_decl ();
5766 gfc_match_target (void)
5768 gfc_clear_attr (¤t_attr
);
5769 current_attr
.target
= 1;
5771 return attr_decl ();
5775 /* Match the list of entities being specified in a PUBLIC or PRIVATE
5779 access_attr_decl (gfc_statement st
)
5781 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
5782 interface_type type
;
5785 gfc_intrinsic_op op
;
5788 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
5793 m
= gfc_match_generic_spec (&type
, name
, &op
);
5796 if (m
== MATCH_ERROR
)
5801 case INTERFACE_NAMELESS
:
5802 case INTERFACE_ABSTRACT
:
5805 case INTERFACE_GENERIC
:
5806 if (gfc_get_symbol (name
, NULL
, &sym
))
5809 if (gfc_add_access (&sym
->attr
, (st
== ST_PUBLIC
)
5810 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
,
5811 sym
->name
, NULL
) == FAILURE
)
5816 case INTERFACE_INTRINSIC_OP
:
5817 if (gfc_current_ns
->operator_access
[op
] == ACCESS_UNKNOWN
)
5819 gfc_current_ns
->operator_access
[op
] =
5820 (st
== ST_PUBLIC
) ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
5824 gfc_error ("Access specification of the %s operator at %C has "
5825 "already been specified", gfc_op2string (op
));
5831 case INTERFACE_USER_OP
:
5832 uop
= gfc_get_uop (name
);
5834 if (uop
->access
== ACCESS_UNKNOWN
)
5836 uop
->access
= (st
== ST_PUBLIC
)
5837 ? ACCESS_PUBLIC
: ACCESS_PRIVATE
;
5841 gfc_error ("Access specification of the .%s. operator at %C "
5842 "has already been specified", sym
->name
);
5849 if (gfc_match_char (',') == MATCH_NO
)
5853 if (gfc_match_eos () != MATCH_YES
)
5858 gfc_syntax_error (st
);
5866 gfc_match_protected (void)
5871 if (gfc_current_ns
->proc_name
->attr
.flavor
!= FL_MODULE
)
5873 gfc_error ("PROTECTED at %C only allowed in specification "
5874 "part of a module");
5879 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: PROTECTED statement at %C")
5883 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
5888 if (gfc_match_eos () == MATCH_YES
)
5893 m
= gfc_match_symbol (&sym
, 0);
5897 if (gfc_add_protected (&sym
->attr
, sym
->name
, &gfc_current_locus
)
5910 if (gfc_match_eos () == MATCH_YES
)
5912 if (gfc_match_char (',') != MATCH_YES
)
5919 gfc_error ("Syntax error in PROTECTED statement at %C");
5924 /* The PRIVATE statement is a bit weird in that it can be an attribute
5925 declaration, but also works as a standalone statement inside of a
5926 type declaration or a module. */
5929 gfc_match_private (gfc_statement
*st
)
5932 if (gfc_match ("private") != MATCH_YES
)
5935 if (gfc_current_state () != COMP_MODULE
5936 && !(gfc_current_state () == COMP_DERIVED
5937 && gfc_state_stack
->previous
5938 && gfc_state_stack
->previous
->state
== COMP_MODULE
)
5939 && !(gfc_current_state () == COMP_DERIVED_CONTAINS
5940 && gfc_state_stack
->previous
&& gfc_state_stack
->previous
->previous
5941 && gfc_state_stack
->previous
->previous
->state
== COMP_MODULE
))
5943 gfc_error ("PRIVATE statement at %C is only allowed in the "
5944 "specification part of a module");
5948 if (gfc_current_state () == COMP_DERIVED
)
5950 if (gfc_match_eos () == MATCH_YES
)
5956 gfc_syntax_error (ST_PRIVATE
);
5960 if (gfc_match_eos () == MATCH_YES
)
5967 return access_attr_decl (ST_PRIVATE
);
5972 gfc_match_public (gfc_statement
*st
)
5975 if (gfc_match ("public") != MATCH_YES
)
5978 if (gfc_current_state () != COMP_MODULE
)
5980 gfc_error ("PUBLIC statement at %C is only allowed in the "
5981 "specification part of a module");
5985 if (gfc_match_eos () == MATCH_YES
)
5992 return access_attr_decl (ST_PUBLIC
);
5996 /* Workhorse for gfc_match_parameter. */
6005 m
= gfc_match_symbol (&sym
, 0);
6007 gfc_error ("Expected variable name at %C in PARAMETER statement");
6012 if (gfc_match_char ('=') == MATCH_NO
)
6014 gfc_error ("Expected = sign in PARAMETER statement at %C");
6018 m
= gfc_match_init_expr (&init
);
6020 gfc_error ("Expected expression at %C in PARAMETER statement");
6024 if (sym
->ts
.type
== BT_UNKNOWN
6025 && gfc_set_default_type (sym
, 1, NULL
) == FAILURE
)
6031 if (gfc_check_assign_symbol (sym
, init
) == FAILURE
6032 || gfc_add_flavor (&sym
->attr
, FL_PARAMETER
, sym
->name
, NULL
) == FAILURE
)
6040 gfc_error ("Initializing already initialized variable at %C");
6045 if (sym
->ts
.type
== BT_CHARACTER
6046 && sym
->ts
.cl
!= NULL
6047 && sym
->ts
.cl
->length
!= NULL
6048 && sym
->ts
.cl
->length
->expr_type
== EXPR_CONSTANT
6049 && init
->expr_type
== EXPR_CONSTANT
6050 && init
->ts
.type
== BT_CHARACTER
)
6051 gfc_set_constant_character_len (
6052 mpz_get_si (sym
->ts
.cl
->length
->value
.integer
), init
, -1);
6053 else if (sym
->ts
.type
== BT_CHARACTER
&& sym
->ts
.cl
!= NULL
6054 && sym
->ts
.cl
->length
== NULL
)
6057 if (init
->expr_type
== EXPR_CONSTANT
)
6059 clen
= init
->value
.character
.length
;
6060 sym
->ts
.cl
->length
= gfc_int_expr (clen
);
6062 else if (init
->expr_type
== EXPR_ARRAY
)
6064 gfc_expr
*p
= init
->value
.constructor
->expr
;
6065 clen
= p
->value
.character
.length
;
6066 sym
->ts
.cl
->length
= gfc_int_expr (clen
);
6068 else if (init
->ts
.cl
&& init
->ts
.cl
->length
)
6069 sym
->ts
.cl
->length
= gfc_copy_expr (sym
->value
->ts
.cl
->length
);
6076 gfc_free_expr (init
);
6081 /* Match a parameter statement, with the weird syntax that these have. */
6084 gfc_match_parameter (void)
6088 if (gfc_match_char ('(') == MATCH_NO
)
6097 if (gfc_match (" )%t") == MATCH_YES
)
6100 if (gfc_match_char (',') != MATCH_YES
)
6102 gfc_error ("Unexpected characters in PARAMETER statement at %C");
6112 /* Save statements have a special syntax. */
6115 gfc_match_save (void)
6117 char n
[GFC_MAX_SYMBOL_LEN
+1];
6122 if (gfc_match_eos () == MATCH_YES
)
6124 if (gfc_current_ns
->seen_save
)
6126 if (gfc_notify_std (GFC_STD_LEGACY
, "Blanket SAVE statement at %C "
6127 "follows previous SAVE statement")
6132 gfc_current_ns
->save_all
= gfc_current_ns
->seen_save
= 1;
6136 if (gfc_current_ns
->save_all
)
6138 if (gfc_notify_std (GFC_STD_LEGACY
, "SAVE statement at %C follows "
6139 "blanket SAVE statement")
6148 m
= gfc_match_symbol (&sym
, 0);
6152 if (gfc_add_save (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6164 m
= gfc_match (" / %n /", &n
);
6165 if (m
== MATCH_ERROR
)
6170 c
= gfc_get_common (n
, 0);
6173 gfc_current_ns
->seen_save
= 1;
6176 if (gfc_match_eos () == MATCH_YES
)
6178 if (gfc_match_char (',') != MATCH_YES
)
6185 gfc_error ("Syntax error in SAVE statement at %C");
6191 gfc_match_value (void)
6196 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VALUE statement at %C")
6200 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6205 if (gfc_match_eos () == MATCH_YES
)
6210 m
= gfc_match_symbol (&sym
, 0);
6214 if (gfc_add_value (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6227 if (gfc_match_eos () == MATCH_YES
)
6229 if (gfc_match_char (',') != MATCH_YES
)
6236 gfc_error ("Syntax error in VALUE statement at %C");
6242 gfc_match_volatile (void)
6247 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: VOLATILE statement at %C")
6251 if (gfc_match (" ::") == MATCH_NO
&& gfc_match_space () == MATCH_NO
)
6256 if (gfc_match_eos () == MATCH_YES
)
6261 /* VOLATILE is special because it can be added to host-associated
6263 m
= gfc_match_symbol (&sym
, 1);
6267 if (gfc_add_volatile (&sym
->attr
, sym
->name
, &gfc_current_locus
)
6280 if (gfc_match_eos () == MATCH_YES
)
6282 if (gfc_match_char (',') != MATCH_YES
)
6289 gfc_error ("Syntax error in VOLATILE statement at %C");
6294 /* Match a module procedure statement. Note that we have to modify
6295 symbols in the parent's namespace because the current one was there
6296 to receive symbols that are in an interface's formal argument list. */
6299 gfc_match_modproc (void)
6301 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6304 gfc_namespace
*module_ns
;
6305 gfc_interface
*old_interface_head
, *interface
;
6307 if (gfc_state_stack
->state
!= COMP_INTERFACE
6308 || gfc_state_stack
->previous
== NULL
6309 || current_interface
.type
== INTERFACE_NAMELESS
6310 || current_interface
.type
== INTERFACE_ABSTRACT
)
6312 gfc_error ("MODULE PROCEDURE at %C must be in a generic module "
6317 module_ns
= gfc_current_ns
->parent
;
6318 for (; module_ns
; module_ns
= module_ns
->parent
)
6319 if (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
)
6322 if (module_ns
== NULL
)
6325 /* Store the current state of the interface. We will need it if we
6326 end up with a syntax error and need to recover. */
6327 old_interface_head
= gfc_current_interface_head ();
6333 m
= gfc_match_name (name
);
6339 /* Check for syntax error before starting to add symbols to the
6340 current namespace. */
6341 if (gfc_match_eos () == MATCH_YES
)
6343 if (!last
&& gfc_match_char (',') != MATCH_YES
)
6346 /* Now we're sure the syntax is valid, we process this item
6348 if (gfc_get_symbol (name
, module_ns
, &sym
))
6351 if (sym
->attr
.proc
!= PROC_MODULE
6352 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
6353 sym
->name
, NULL
) == FAILURE
)
6356 if (gfc_add_interface (sym
) == FAILURE
)
6359 sym
->attr
.mod_proc
= 1;
6368 /* Restore the previous state of the interface. */
6369 interface
= gfc_current_interface_head ();
6370 gfc_set_current_interface_head (old_interface_head
);
6372 /* Free the new interfaces. */
6373 while (interface
!= old_interface_head
)
6375 gfc_interface
*i
= interface
->next
;
6376 gfc_free (interface
);
6380 /* And issue a syntax error. */
6381 gfc_syntax_error (ST_MODULE_PROC
);
6386 /* Check a derived type that is being extended. */
6388 check_extended_derived_type (char *name
)
6390 gfc_symbol
*extended
;
6392 if (gfc_find_symbol (name
, gfc_current_ns
, 1, &extended
))
6394 gfc_error ("Ambiguous symbol in TYPE definition at %C");
6400 gfc_error ("No such symbol in TYPE definition at %C");
6404 if (extended
->attr
.flavor
!= FL_DERIVED
)
6406 gfc_error ("'%s' in EXTENDS expression at %C is not a "
6407 "derived type", name
);
6411 if (extended
->attr
.is_bind_c
)
6413 gfc_error ("'%s' cannot be extended at %C because it "
6414 "is BIND(C)", extended
->name
);
6418 if (extended
->attr
.sequence
)
6420 gfc_error ("'%s' cannot be extended at %C because it "
6421 "is a SEQUENCE type", extended
->name
);
6429 /* Match the optional attribute specifiers for a type declaration.
6430 Return MATCH_ERROR if an error is encountered in one of the handled
6431 attributes (public, private, bind(c)), MATCH_NO if what's found is
6432 not a handled attribute, and MATCH_YES otherwise. TODO: More error
6433 checking on attribute conflicts needs to be done. */
6436 gfc_get_type_attr_spec (symbol_attribute
*attr
, char *name
)
6438 /* See if the derived type is marked as private. */
6439 if (gfc_match (" , private") == MATCH_YES
)
6441 if (gfc_current_state () != COMP_MODULE
)
6443 gfc_error ("Derived type at %C can only be PRIVATE in the "
6444 "specification part of a module");
6448 if (gfc_add_access (attr
, ACCESS_PRIVATE
, NULL
, NULL
) == FAILURE
)
6451 else if (gfc_match (" , public") == MATCH_YES
)
6453 if (gfc_current_state () != COMP_MODULE
)
6455 gfc_error ("Derived type at %C can only be PUBLIC in the "
6456 "specification part of a module");
6460 if (gfc_add_access (attr
, ACCESS_PUBLIC
, NULL
, NULL
) == FAILURE
)
6463 else if (gfc_match (" , bind ( c )") == MATCH_YES
)
6465 /* If the type is defined to be bind(c) it then needs to make
6466 sure that all fields are interoperable. This will
6467 need to be a semantic check on the finished derived type.
6468 See 15.2.3 (lines 9-12) of F2003 draft. */
6469 if (gfc_add_is_bind_c (attr
, NULL
, &gfc_current_locus
, 0) != SUCCESS
)
6472 /* TODO: attr conflicts need to be checked, probably in symbol.c. */
6474 else if (gfc_match (" , abstract") == MATCH_YES
)
6476 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ABSTRACT type at %C")
6480 if (gfc_add_abstract (attr
, &gfc_current_locus
) == FAILURE
)
6483 else if (name
&& gfc_match(" , extends ( %n )", name
) == MATCH_YES
)
6485 if (gfc_add_extension (attr
, &gfc_current_locus
) == FAILURE
)
6491 /* If we get here, something matched. */
6496 /* Match the beginning of a derived type declaration. If a type name
6497 was the result of a function, then it is possible to have a symbol
6498 already to be known as a derived type yet have no components. */
6501 gfc_match_derived_decl (void)
6503 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6504 char parent
[GFC_MAX_SYMBOL_LEN
+ 1];
6505 symbol_attribute attr
;
6507 gfc_symbol
*extended
;
6509 match is_type_attr_spec
= MATCH_NO
;
6510 bool seen_attr
= false;
6512 if (gfc_current_state () == COMP_DERIVED
)
6517 gfc_clear_attr (&attr
);
6522 is_type_attr_spec
= gfc_get_type_attr_spec (&attr
, parent
);
6523 if (is_type_attr_spec
== MATCH_ERROR
)
6525 if (is_type_attr_spec
== MATCH_YES
)
6527 } while (is_type_attr_spec
== MATCH_YES
);
6529 /* Deal with derived type extensions. The extension attribute has
6530 been added to 'attr' but now the parent type must be found and
6533 extended
= check_extended_derived_type (parent
);
6535 if (parent
[0] && !extended
)
6538 if (gfc_match (" ::") != MATCH_YES
&& seen_attr
)
6540 gfc_error ("Expected :: in TYPE definition at %C");
6544 m
= gfc_match (" %n%t", name
);
6548 /* Make sure the name is not the name of an intrinsic type. */
6549 if (gfc_is_intrinsic_typename (name
))
6551 gfc_error ("Type name '%s' at %C cannot be the same as an intrinsic "
6556 if (gfc_get_symbol (name
, NULL
, &sym
))
6559 if (sym
->ts
.type
!= BT_UNKNOWN
)
6561 gfc_error ("Derived type name '%s' at %C already has a basic type "
6562 "of %s", sym
->name
, gfc_typename (&sym
->ts
));
6566 /* The symbol may already have the derived attribute without the
6567 components. The ways this can happen is via a function
6568 definition, an INTRINSIC statement or a subtype in another
6569 derived type that is a pointer. The first part of the AND clause
6570 is true if the symbol is not the return value of a function. */
6571 if (sym
->attr
.flavor
!= FL_DERIVED
6572 && gfc_add_flavor (&sym
->attr
, FL_DERIVED
, sym
->name
, NULL
) == FAILURE
)
6575 if (sym
->components
!= NULL
|| sym
->attr
.zero_comp
)
6577 gfc_error ("Derived type definition of '%s' at %C has already been "
6578 "defined", sym
->name
);
6582 if (attr
.access
!= ACCESS_UNKNOWN
6583 && gfc_add_access (&sym
->attr
, attr
.access
, sym
->name
, NULL
) == FAILURE
)
6586 /* See if the derived type was labeled as bind(c). */
6587 if (attr
.is_bind_c
!= 0)
6588 sym
->attr
.is_bind_c
= attr
.is_bind_c
;
6590 /* Construct the f2k_derived namespace if it is not yet there. */
6591 if (!sym
->f2k_derived
)
6592 sym
->f2k_derived
= gfc_get_namespace (NULL
, 0);
6594 if (extended
&& !sym
->components
)
6599 /* Add the extended derived type as the first component. */
6600 gfc_add_component (sym
, parent
, &p
);
6601 sym
->attr
.extension
= attr
.extension
;
6603 gfc_set_sym_referenced (extended
);
6605 p
->ts
.type
= BT_DERIVED
;
6606 p
->ts
.derived
= extended
;
6607 p
->initializer
= gfc_default_initializer (&p
->ts
);
6609 /* Provide the links between the extended type and its extension. */
6610 if (!extended
->f2k_derived
)
6611 extended
->f2k_derived
= gfc_get_namespace (NULL
, 0);
6612 st
= gfc_new_symtree (&extended
->f2k_derived
->sym_root
, sym
->name
);
6616 /* Take over the ABSTRACT attribute. */
6617 sym
->attr
.abstract
= attr
.abstract
;
6619 gfc_new_block
= sym
;
6625 /* Cray Pointees can be declared as:
6626 pointer (ipt, a (n,m,...,*))
6627 By default, this is treated as an AS_ASSUMED_SIZE array. We'll
6628 cheat and set a constant bound of 1 for the last dimension, if this
6629 is the case. Since there is no bounds-checking for Cray Pointees,
6630 this will be okay. */
6633 gfc_mod_pointee_as (gfc_array_spec
*as
)
6635 as
->cray_pointee
= true; /* This will be useful to know later. */
6636 if (as
->type
== AS_ASSUMED_SIZE
)
6638 as
->type
= AS_EXPLICIT
;
6639 as
->upper
[as
->rank
- 1] = gfc_int_expr (1);
6640 as
->cp_was_assumed
= true;
6642 else if (as
->type
== AS_ASSUMED_SHAPE
)
6644 gfc_error ("Cray Pointee at %C cannot be assumed shape array");
6651 /* Match the enum definition statement, here we are trying to match
6652 the first line of enum definition statement.
6653 Returns MATCH_YES if match is found. */
6656 gfc_match_enum (void)
6660 m
= gfc_match_eos ();
6664 if (gfc_notify_std (GFC_STD_F2003
, "Fortran 2003: ENUM and ENUMERATOR at %C")
6672 /* Match a variable name with an optional initializer. When this
6673 subroutine is called, a variable is expected to be parsed next.
6674 Depending on what is happening at the moment, updates either the
6675 symbol table or the current interface. */
6678 enumerator_decl (void)
6680 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
6681 gfc_expr
*initializer
;
6682 gfc_array_spec
*as
= NULL
;
6690 old_locus
= gfc_current_locus
;
6692 /* When we get here, we've just matched a list of attributes and
6693 maybe a type and a double colon. The next thing we expect to see
6694 is the name of the symbol. */
6695 m
= gfc_match_name (name
);
6699 var_locus
= gfc_current_locus
;
6701 /* OK, we've successfully matched the declaration. Now put the
6702 symbol in the current namespace. If we fail to create the symbol,
6704 if (build_sym (name
, NULL
, &as
, &var_locus
) == FAILURE
)
6710 /* The double colon must be present in order to have initializers.
6711 Otherwise the statement is ambiguous with an assignment statement. */
6714 if (gfc_match_char ('=') == MATCH_YES
)
6716 m
= gfc_match_init_expr (&initializer
);
6719 gfc_error ("Expected an initialization expression at %C");
6728 /* If we do not have an initializer, the initialization value of the
6729 previous enumerator (stored in last_initializer) is incremented
6730 by 1 and is used to initialize the current enumerator. */
6731 if (initializer
== NULL
)
6732 initializer
= gfc_enum_initializer (last_initializer
, old_locus
);
6734 if (initializer
== NULL
|| initializer
->ts
.type
!= BT_INTEGER
)
6736 gfc_error("ENUMERATOR %L not initialized with integer expression",
6739 gfc_free_enum_history ();
6743 /* Store this current initializer, for the next enumerator variable
6744 to be parsed. add_init_expr_to_sym() zeros initializer, so we
6745 use last_initializer below. */
6746 last_initializer
= initializer
;
6747 t
= add_init_expr_to_sym (name
, &initializer
, &var_locus
);
6749 /* Maintain enumerator history. */
6750 gfc_find_symbol (name
, NULL
, 0, &sym
);
6751 create_enum_history (sym
, last_initializer
);
6753 return (t
== SUCCESS
) ? MATCH_YES
: MATCH_ERROR
;
6756 /* Free stuff up and return. */
6757 gfc_free_expr (initializer
);
6763 /* Match the enumerator definition statement. */
6766 gfc_match_enumerator_def (void)
6771 gfc_clear_ts (¤t_ts
);
6773 m
= gfc_match (" enumerator");
6777 m
= gfc_match (" :: ");
6778 if (m
== MATCH_ERROR
)
6781 colon_seen
= (m
== MATCH_YES
);
6783 if (gfc_current_state () != COMP_ENUM
)
6785 gfc_error ("ENUM definition statement expected before %C");
6786 gfc_free_enum_history ();
6790 (¤t_ts
)->type
= BT_INTEGER
;
6791 (¤t_ts
)->kind
= gfc_c_int_kind
;
6793 gfc_clear_attr (¤t_attr
);
6794 t
= gfc_add_flavor (¤t_attr
, FL_PARAMETER
, NULL
, NULL
);
6803 m
= enumerator_decl ();
6804 if (m
== MATCH_ERROR
)
6809 if (gfc_match_eos () == MATCH_YES
)
6811 if (gfc_match_char (',') != MATCH_YES
)
6815 if (gfc_current_state () == COMP_ENUM
)
6817 gfc_free_enum_history ();
6818 gfc_error ("Syntax error in ENUMERATOR definition at %C");
6823 gfc_free_array_spec (current_as
);
6830 /* Match binding attributes. */
6833 match_binding_attributes (gfc_typebound_proc
* ba
, bool generic
)
6835 bool found_passing
= false;
6838 /* Intialize to defaults. Do so even before the MATCH_NO check so that in
6839 this case the defaults are in there. */
6840 ba
->access
= ACCESS_UNKNOWN
;
6841 ba
->pass_arg
= NULL
;
6842 ba
->pass_arg_num
= 0;
6844 ba
->non_overridable
= 0;
6847 /* If we find a comma, we believe there are binding attributes. */
6848 if (gfc_match_char (',') == MATCH_NO
)
6850 ba
->access
= gfc_typebound_default_access
;
6856 /* Access specifier. */
6858 m
= gfc_match (" public");
6859 if (m
== MATCH_ERROR
)
6863 if (ba
->access
!= ACCESS_UNKNOWN
)
6865 gfc_error ("Duplicate access-specifier at %C");
6869 ba
->access
= ACCESS_PUBLIC
;
6873 m
= gfc_match (" private");
6874 if (m
== MATCH_ERROR
)
6878 if (ba
->access
!= ACCESS_UNKNOWN
)
6880 gfc_error ("Duplicate access-specifier at %C");
6884 ba
->access
= ACCESS_PRIVATE
;
6888 /* If inside GENERIC, the following is not allowed. */
6893 m
= gfc_match (" nopass");
6894 if (m
== MATCH_ERROR
)
6900 gfc_error ("Binding attributes already specify passing,"
6901 " illegal NOPASS at %C");
6905 found_passing
= true;
6910 /* NON_OVERRIDABLE flag. */
6911 m
= gfc_match (" non_overridable");
6912 if (m
== MATCH_ERROR
)
6916 if (ba
->non_overridable
)
6918 gfc_error ("Duplicate NON_OVERRIDABLE at %C");
6922 ba
->non_overridable
= 1;
6926 /* DEFERRED flag. */
6927 m
= gfc_match (" deferred");
6928 if (m
== MATCH_ERROR
)
6934 gfc_error ("Duplicate DEFERRED at %C");
6942 /* PASS possibly including argument. */
6943 m
= gfc_match (" pass");
6944 if (m
== MATCH_ERROR
)
6948 char arg
[GFC_MAX_SYMBOL_LEN
+ 1];
6952 gfc_error ("Binding attributes already specify passing,"
6953 " illegal PASS at %C");
6957 m
= gfc_match (" ( %n )", arg
);
6958 if (m
== MATCH_ERROR
)
6961 ba
->pass_arg
= xstrdup (arg
);
6962 gcc_assert ((m
== MATCH_YES
) == (ba
->pass_arg
!= NULL
));
6964 found_passing
= true;
6971 /* Nothing matching found. */
6973 gfc_error ("Expected access-specifier at %C");
6975 gfc_error ("Expected binding attribute at %C");
6978 while (gfc_match_char (',') == MATCH_YES
);
6980 /* NON_OVERRIDABLE and DEFERRED exclude themselves. */
6981 if (ba
->non_overridable
&& ba
->deferred
)
6983 gfc_error ("NON_OVERRIDABLE and DEFERRED can't both appear at %C");
6987 if (ba
->access
== ACCESS_UNKNOWN
)
6988 ba
->access
= gfc_typebound_default_access
;
6993 gfc_free (ba
->pass_arg
);
6998 /* Match a PROCEDURE specific binding inside a derived type. */
7001 match_procedure_in_type (void)
7003 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7004 char target_buf
[GFC_MAX_SYMBOL_LEN
+ 1];
7005 char* target
= NULL
;
7006 gfc_typebound_proc
* tb
;
7014 /* Check current state. */
7015 gcc_assert (gfc_state_stack
->state
== COMP_DERIVED_CONTAINS
);
7016 block
= gfc_state_stack
->previous
->sym
;
7019 /* Try to match PROCEDURE(interface). */
7020 if (gfc_match (" (") == MATCH_YES
)
7022 m
= gfc_match_name (target_buf
);
7023 if (m
== MATCH_ERROR
)
7027 gfc_error ("Interface-name expected after '(' at %C");
7031 if (gfc_match (" )") != MATCH_YES
)
7033 gfc_error ("')' expected at %C");
7037 target
= target_buf
;
7040 /* Construct the data structure. */
7041 tb
= gfc_get_typebound_proc ();
7042 tb
->where
= gfc_current_locus
;
7045 /* Match binding attributes. */
7046 m
= match_binding_attributes (tb
, false);
7047 if (m
== MATCH_ERROR
)
7049 seen_attrs
= (m
== MATCH_YES
);
7051 /* Check that attribute DEFERRED is given iff an interface is specified, which
7052 means target != NULL. */
7053 if (tb
->deferred
&& !target
)
7055 gfc_error ("Interface must be specified for DEFERRED binding at %C");
7058 if (target
&& !tb
->deferred
)
7060 gfc_error ("PROCEDURE(interface) at %C should be declared DEFERRED");
7064 /* Match the colons. */
7065 m
= gfc_match (" ::");
7066 if (m
== MATCH_ERROR
)
7068 seen_colons
= (m
== MATCH_YES
);
7069 if (seen_attrs
&& !seen_colons
)
7071 gfc_error ("Expected '::' after binding-attributes at %C");
7075 /* Match the binding name. */
7076 m
= gfc_match_name (name
);
7077 if (m
== MATCH_ERROR
)
7081 gfc_error ("Expected binding name at %C");
7085 /* Try to match the '=> target', if it's there. */
7086 m
= gfc_match (" =>");
7087 if (m
== MATCH_ERROR
)
7093 gfc_error ("'=> target' is invalid for DEFERRED binding at %C");
7099 gfc_error ("'::' needed in PROCEDURE binding with explicit target"
7104 m
= gfc_match_name (target_buf
);
7105 if (m
== MATCH_ERROR
)
7109 gfc_error ("Expected binding target after '=>' at %C");
7112 target
= target_buf
;
7115 /* Now we should have the end. */
7116 m
= gfc_match_eos ();
7117 if (m
== MATCH_ERROR
)
7121 gfc_error ("Junk after PROCEDURE declaration at %C");
7125 /* If no target was found, it has the same name as the binding. */
7129 /* Get the namespace to insert the symbols into. */
7130 ns
= block
->f2k_derived
;
7133 /* If the binding is DEFERRED, check that the containing type is ABSTRACT. */
7134 if (tb
->deferred
&& !block
->attr
.abstract
)
7136 gfc_error ("Type '%s' containing DEFERRED binding at %C is not ABSTRACT",
7141 /* See if we already have a binding with this name in the symtree which would
7142 be an error. If a GENERIC already targetted this binding, it may be
7143 already there but then typebound is still NULL. */
7144 stree
= gfc_find_symtree (ns
->tb_sym_root
, name
);
7145 if (stree
&& stree
->n
.tb
)
7147 gfc_error ("There's already a procedure with binding name '%s' for the"
7148 " derived type '%s' at %C", name
, block
->name
);
7152 /* Insert it and set attributes. */
7156 stree
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
7161 if (gfc_get_sym_tree (target
, gfc_current_ns
, &tb
->u
.specific
))
7163 gfc_set_sym_referenced (tb
->u
.specific
->n
.sym
);
7169 /* Match a GENERIC procedure binding inside a derived type. */
7172 gfc_match_generic (void)
7174 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7176 gfc_typebound_proc tbattr
; /* Used for match_binding_attributes. */
7177 gfc_typebound_proc
* tb
;
7182 /* Check current state. */
7183 if (gfc_current_state () == COMP_DERIVED
)
7185 gfc_error ("GENERIC at %C must be inside a derived-type CONTAINS");
7188 if (gfc_current_state () != COMP_DERIVED_CONTAINS
)
7190 block
= gfc_state_stack
->previous
->sym
;
7191 ns
= block
->f2k_derived
;
7192 gcc_assert (block
&& ns
);
7194 /* See if we get an access-specifier. */
7195 m
= match_binding_attributes (&tbattr
, true);
7196 if (m
== MATCH_ERROR
)
7199 /* Now the colons, those are required. */
7200 if (gfc_match (" ::") != MATCH_YES
)
7202 gfc_error ("Expected '::' at %C");
7206 /* The binding name and =>. */
7207 m
= gfc_match (" %n =>", name
);
7208 if (m
== MATCH_ERROR
)
7212 gfc_error ("Expected generic name at %C");
7216 /* If there's already something with this name, check that it is another
7217 GENERIC and then extend that rather than build a new node. */
7218 st
= gfc_find_symtree (ns
->tb_sym_root
, name
);
7221 gcc_assert (st
->n
.tb
);
7224 if (!tb
->is_generic
)
7226 gfc_error ("There's already a non-generic procedure with binding name"
7227 " '%s' for the derived type '%s' at %C",
7232 if (tb
->access
!= tbattr
.access
)
7234 gfc_error ("Binding at %C must have the same access as already"
7235 " defined binding '%s'", name
);
7241 st
= gfc_new_symtree (&ns
->tb_sym_root
, name
);
7244 st
->n
.tb
= tb
= gfc_get_typebound_proc ();
7245 tb
->where
= gfc_current_locus
;
7246 tb
->access
= tbattr
.access
;
7248 tb
->u
.generic
= NULL
;
7251 /* Now, match all following names as specific targets. */
7254 gfc_symtree
* target_st
;
7255 gfc_tbp_generic
* target
;
7257 m
= gfc_match_name (name
);
7258 if (m
== MATCH_ERROR
)
7262 gfc_error ("Expected specific binding name at %C");
7266 target_st
= gfc_get_tbp_symtree (&ns
->tb_sym_root
, name
);
7268 /* See if this is a duplicate specification. */
7269 for (target
= tb
->u
.generic
; target
; target
= target
->next
)
7270 if (target_st
== target
->specific_st
)
7272 gfc_error ("'%s' already defined as specific binding for the"
7273 " generic '%s' at %C", name
, st
->name
);
7277 target
= gfc_get_tbp_generic ();
7278 target
->specific_st
= target_st
;
7279 target
->specific
= NULL
;
7280 target
->next
= tb
->u
.generic
;
7281 tb
->u
.generic
= target
;
7283 while (gfc_match (" ,") == MATCH_YES
);
7285 /* Here should be the end. */
7286 if (gfc_match_eos () != MATCH_YES
)
7288 gfc_error ("Junk after GENERIC binding at %C");
7299 /* Match a FINAL declaration inside a derived type. */
7302 gfc_match_final_decl (void)
7304 char name
[GFC_MAX_SYMBOL_LEN
+ 1];
7307 gfc_namespace
* module_ns
;
7311 if (gfc_state_stack
->state
!= COMP_DERIVED_CONTAINS
)
7313 gfc_error ("FINAL declaration at %C must be inside a derived type "
7314 "CONTAINS section");
7318 block
= gfc_state_stack
->previous
->sym
;
7321 if (!gfc_state_stack
->previous
|| !gfc_state_stack
->previous
->previous
7322 || gfc_state_stack
->previous
->previous
->state
!= COMP_MODULE
)
7324 gfc_error ("Derived type declaration with FINAL at %C must be in the"
7325 " specification part of a MODULE");
7329 module_ns
= gfc_current_ns
;
7330 gcc_assert (module_ns
);
7331 gcc_assert (module_ns
->proc_name
->attr
.flavor
== FL_MODULE
);
7333 /* Match optional ::, don't care about MATCH_YES or MATCH_NO. */
7334 if (gfc_match (" ::") == MATCH_ERROR
)
7337 /* Match the sequence of procedure names. */
7344 if (first
&& gfc_match_eos () == MATCH_YES
)
7346 gfc_error ("Empty FINAL at %C");
7350 m
= gfc_match_name (name
);
7353 gfc_error ("Expected module procedure name at %C");
7356 else if (m
!= MATCH_YES
)
7359 if (gfc_match_eos () == MATCH_YES
)
7361 if (!last
&& gfc_match_char (',') != MATCH_YES
)
7363 gfc_error ("Expected ',' at %C");
7367 if (gfc_get_symbol (name
, module_ns
, &sym
))
7369 gfc_error ("Unknown procedure name \"%s\" at %C", name
);
7373 /* Mark the symbol as module procedure. */
7374 if (sym
->attr
.proc
!= PROC_MODULE
7375 && gfc_add_procedure (&sym
->attr
, PROC_MODULE
,
7376 sym
->name
, NULL
) == FAILURE
)
7379 /* Check if we already have this symbol in the list, this is an error. */
7380 for (f
= block
->f2k_derived
->finalizers
; f
; f
= f
->next
)
7381 if (f
->proc_sym
== sym
)
7383 gfc_error ("'%s' at %C is already defined as FINAL procedure!",
7388 /* Add this symbol to the list of finalizers. */
7389 gcc_assert (block
->f2k_derived
);
7391 f
= XCNEW (gfc_finalizer
);
7393 f
->proc_tree
= NULL
;
7394 f
->where
= gfc_current_locus
;
7395 f
->next
= block
->f2k_derived
->finalizers
;
7396 block
->f2k_derived
->finalizers
= f
;